UNPKG

@hashgraph/sdk

Version:
3 lines (2 loc) 2.87 kB
import t from"../account/AccountId.js";import e from"../Node.js";import{PREVIEWNET_ADDRESS_BOOK as s,TESTNET_ADDRESS_BOOK as r,MAINNET_ADDRESS_BOOK as o}from"../address_book/AddressBooks.js";import n from"./ManagedNetwork.js";class i extends n{constructor(t){super(t),this._maxNodesPerTransaction=-1,this._addressBook=null,this._transportSecurity=!1}setNetwork(e){this._setNetwork(new Map(Object.entries(e).map(([e,s])=>[e,"string"==typeof s?t.fromString(s):s])))}setNetworkFromAddressBook(t){const e={},s=this.isTransportSecurity()?50212:50211;for(const r of t.nodeAddresses)for(const t of r.addresses)t.port===s&&null!=r.accountId&&(e[t.toString()]=r.accountId);return this.setNetwork(e),this}get network(){var t={};for(const e of this._nodes)t[e.address.toString()]=e.accountId;return t}setNetworkName(t){switch(super.setLedgerId(t),t){case"mainnet":this._addressBook=o;break;case"testnet":this._addressBook=r;break;case"previewnet":this._addressBook=s}if(null!=this._addressBook)for(const t of this._nodes)for(const e of this._addressBook.nodeAddresses)null!=e.accountId&&e.accountId.toString()===t.accountId.toString()&&t.setNodeAddress(e);return this}get networkName(){return null!=this._ledgerId?this._ledgerId.toString():null}_createNodeFromNetworkEntry(s){const r="string"==typeof s[1]?t.fromString(s[1]):s[1];return new e({newNode:{address:s[0],accountId:r,channelInitFunction:this._createNetworkChannel}}).setMinBackoff(this._minBackoff)}_getNodesToRemove(t){const e=[];for(let s=this._nodes.length-1;s>=0;s--){const r=this._nodes[s],o=t.get(r.address.toString());null!=o&&o.toString()===r.accountId.toString()||e.push(s)}return e}_checkNetworkContainsEntry(t){for(const e of this._nodes)if(e.address.toString()===t[0])return!0;return!1}get maxNodesPerTransaction(){return this._maxNodesPerTransaction}setMaxNodesPerTransaction(t){return this._maxNodesPerTransaction=t,this}get maxNodeAttempts(){return this._maxNodeAttempts}setMaxNodeAttempts(t){return this._maxNodeAttempts=t,this}isTransportSecurity(){return this._transportSecurity}setTransportSecurity(t){if(this._transportSecurity==t)return this;this._network.clear();for(let e=0;e<this._nodes.length;e++){let s=this._nodes[e];s.close(),s=t?s.toSecure().setCert(null!=this._ledgerId?this._ledgerId.toString():""):s.toInsecure(),this._nodes[e]=s;const r=null!=this._network.get(s.getKey())?this._network.get(s.getKey()):[];r.push(s),this._network.set(s.getKey(),r)}return this._healthyNodes=[...this._nodes],this._transportSecurity=t,this}getNumberOfNodesForTransaction(){return this._maxNodesPerTransaction>0?this._maxNodesPerTransaction:this._nodes.length<=9?this._nodes.length:Math.floor((this._nodes.length+3-1)/3)}getNodeAccountIdsForExecute(){return this._getNumberOfMostHealthyNodes(this.getNumberOfNodesForTransaction()).map(t=>t.accountId)}}export{i as default}; //# sourceMappingURL=Network.js.map