UNPKG

@hashgraph/sdk

Version:
3 lines (2 loc) 2.44 kB
import t from"../query/Query.js";import e from"../address_book/NodeAddress.js";import r from"../address_book/NodeAddressBook.js";import*as i from"@hashgraph/proto";import s from"../file/FileId.js";import{RST_STREAM as o}from"../Executable.js";class m extends t{constructor(t={}){super(),this._fileId=null,null!=t.fileId&&this.setFileId(t.fileId),this._limit=null,null!=t.limit&&this.setLimit(t.limit),this._retryHandler=t=>{if(null!=t){if(t instanceof Error)return!0;switch(t.code){case 13:return o.test(t.details.toString());case 5:case 8:case 14:case 17:return!0;default:return!1}}return!1},this._addresses=[],this._attempt=0}get fileId(){return this._fileId}setFileId(t){return this._fileId="string"==typeof t?s.fromString(t):t.clone(),this}get limit(){return this._limit}setLimit(t){return this._limit=t,this}setMaxAttempts(t){return this._maxAttempts=t,this}setMaxBackoff(t){return this._maxBackoff=t,this}execute(t,e){if(0===t._network._network.size&&!t._timer)throw new Error("The client's network update period is required. Please set it using the setNetworkUpdatePeriod method.");return new Promise((r,i)=>{this._makeServerStreamRequest(t,r,i,e)})}_makeServerStreamRequest(t,s,o,m){const l=i.com.hedera.mirror.api.proto.AddressBookQuery.encode({fileId:null!=this._fileId?this._fileId._toProtobuf():null,limit:this._limit}).finish();t._mirrorNetwork.getNextMirrorNode().getChannel().makeServerStreamRequest("NetworkService","getNodes",l,t=>{this._addresses.push(e._fromProtobuf(i.proto.NodeAddress.decode(t))),null!=this._limit&&this._limit>0&&(this._limit=this._limit-1)},e=>{const r=e instanceof Error?e.message:e.details;if(this._attempt<this._maxAttempts&&!t.isClientShutDown&&this._retryHandler(e)){const e=Math.min(250*2**this._attempt,this._maxBackoff);this._attempt>=this._maxAttempts&&console.warn(`Error getting nodes from mirror for file ${null!=this._fileId?this._fileId.toString():"UNKNOWN"} during attempt ${this._attempt}. Waiting ${e} ms before next attempt: ${r}`),this._logger&&this._logger.debug(`Error getting nodes from mirror for file ${null!=this._fileId?this._fileId.toString():"UNKNOWN"} during attempt ${this._attempt}. Waiting ${e} ms before next attempt: ${r}`),this._attempt+=1,setTimeout(()=>{this._makeServerStreamRequest(t,s,o,m)},e)}else o(new Error("failed to query address book"))},()=>{s(new r({nodeAddresses:this._addresses}))})}}export{m as default}; //# sourceMappingURL=AddressBookQuery.js.map