github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/network.go (about) 1 package hedera 2 3 /*- 4 * 5 * Hedera Go SDK 6 * 7 * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC 8 * 9 * Licensed under the Apache License, Version 2.0 (the "License"); 10 * you may not use this file except in compliance with the License. 11 * You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 * 21 */ 22 23 import ( 24 "math/rand" 25 "time" 26 ) 27 28 type _Network struct { 29 _ManagedNetwork 30 addressBook map[AccountID]NodeAddress 31 } 32 33 func _NewNetwork() _Network { 34 return _Network{ 35 _ManagedNetwork: _NewManagedNetwork(), 36 addressBook: nil, 37 } 38 } 39 40 // SetNetwork sets the network to the given map of node addresses. 41 func (network *_Network) SetNetwork(net map[string]AccountID) (err error) { 42 newNetwork := make(map[string]_IManagedNode) 43 44 for url, id := range net { 45 node, err := _NewNode(id, url, network.minBackoff) 46 if err != nil { 47 return err 48 } 49 newNetwork[url] = node 50 } 51 52 return network._ManagedNetwork._SetNetwork(newNetwork) 53 } 54 55 func (network *_Network) _GetNetwork() map[string]AccountID { 56 temp := make(map[string]AccountID) 57 for _, node := range network._ManagedNetwork.nodes { 58 switch n := node.(type) { //nolint 59 case *_Node: 60 temp[n._GetAddress()] = n.accountID 61 } 62 } 63 64 return temp 65 } 66 67 func (network *_Network) _IncreaseBackoff(node *_Node) { 68 network.healthyNodesMutex.Lock() 69 defer network.healthyNodesMutex.Unlock() 70 node._IncreaseBackoff() 71 72 index := -1 73 for i, healthyNode := range network.healthyNodes { 74 if node == healthyNode { 75 index = i 76 break 77 } 78 } 79 80 if index >= 0 && index < len(network.healthyNodes) { 81 network.healthyNodes = append(network.healthyNodes[:index], network.healthyNodes[index+1:]...) 82 } 83 } 84 85 func (network *_Network) _GetNodeForAccountID(id AccountID) (*_Node, bool) { 86 node, ok := network.network[id.String()] 87 if !ok || node == nil { 88 return nil, false 89 } 90 return node[0].(*_Node), ok 91 } 92 93 func (network *_Network) _GetNode() *_Node { 94 return network._ManagedNetwork._GetNode().(*_Node) 95 } 96 97 func (network *_Network) _GetLedgerID() *LedgerID { 98 if network._ManagedNetwork._GetLedgerID() != nil { 99 return network._ManagedNetwork._GetLedgerID() 100 } 101 102 return &LedgerID{} 103 } 104 105 func (network *_Network) _SetLedgerID(id LedgerID) { 106 network._ManagedNetwork._SetLedgerID(id) 107 108 if network._ManagedNetwork.transportSecurity && network.ledgerID != nil { 109 switch { 110 case id.IsMainnet(): 111 network.addressBook = mainnetAddressBook._ToMap() 112 case id.IsTestnet(): 113 network.addressBook = testnetAddressBook._ToMap() 114 case id.IsPreviewnet(): 115 network.addressBook = previewnetAddressBook._ToMap() 116 } 117 118 if network.addressBook != nil { 119 for _, node := range network._ManagedNetwork.nodes { 120 if node, ok := node.(*_Node); ok { 121 temp := network.addressBook[node.accountID] 122 node.addressBook = &temp 123 } 124 } 125 for _, nodes := range network._ManagedNetwork.network { 126 for _, node := range nodes { 127 if node, ok := node.(*_Node); ok { 128 temp := network.addressBook[node.accountID] 129 node.addressBook = &temp 130 } 131 } 132 } 133 } 134 } 135 } 136 137 func (network *_Network) _GetNodeAccountIDsForExecute() []AccountID { //nolint 138 nodes := make([]AccountID, 0) 139 nodesForTransaction := network._GetNumberOfNodesForTransaction() 140 141 network.healthyNodesMutex.RLock() 142 defer network.healthyNodesMutex.RUnlock() 143 144 healthyNodes := make([]_IManagedNode, len(network.healthyNodes)) 145 copy(healthyNodes, network.healthyNodes) 146 147 // shuffle the nodes, so that the first node is not always the same 148 for i := range healthyNodes { 149 j := rand.Intn(i + 1) // #nosec 150 healthyNodes[i], healthyNodes[j] = healthyNodes[j], healthyNodes[i] 151 } 152 for i := 0; i < nodesForTransaction; i++ { 153 nodes = append(nodes, healthyNodes[i].(*_Node).accountID) 154 } 155 return nodes 156 } 157 158 func (network *_Network) _SetMaxNodesPerTransaction(max int) { 159 network._ManagedNetwork._SetMaxNodesPerTransaction(max) 160 } 161 162 func (network *_Network) _SetMaxNodeAttempts(max int) { 163 network._ManagedNetwork._SetMaxNodeAttempts(max) 164 } 165 166 func (network *_Network) _GetMaxNodeAttempts() int { 167 return network._ManagedNetwork._GetMaxNodeAttempts() 168 } 169 170 func (network *_Network) _SetNodeMinBackoff(backoff time.Duration) { 171 network._ManagedNetwork._SetMinBackoff(backoff) 172 } 173 174 func (network *_Network) _SetNodeMaxBackoff(backoff time.Duration) { 175 network._ManagedNetwork._SetMaxBackoff(backoff) 176 } 177 178 func (network *_Network) _GetNodeMinBackoff() time.Duration { 179 return network._ManagedNetwork._GetMinBackoff() 180 } 181 182 func (network *_Network) _GetNodeMaxBackoff() time.Duration { 183 return network._ManagedNetwork._GetMaxBackoff() 184 } 185 186 func (network *_Network) _SetTransportSecurity(transportSecurity bool) *_Network { 187 _ = network._ManagedNetwork._SetTransportSecurity(transportSecurity) 188 return network 189 } 190 191 func (network *_Network) _SetVerifyCertificate(verify bool) *_ManagedNetwork { 192 return network._ManagedNetwork._SetVerifyCertificate(verify) 193 } 194 195 func (network *_Network) _GetVerifyCertificate() bool { 196 return network._ManagedNetwork._GetVerifyCertificate() 197 } 198 199 func (network *_Network) _SetNodeMinReadmitPeriod(period time.Duration) { 200 network._ManagedNetwork._SetMinNodeReadmitPeriod(period) 201 } 202 203 func (network *_Network) _SetNodeMaxReadmitPeriod(period time.Duration) { 204 network._ManagedNetwork._SetMaxNodeReadmitPeriod(period) 205 } 206 207 func (network *_Network) _GetNodeMinReadmitPeriod() time.Duration { 208 return network._ManagedNetwork.minNodeReadmitPeriod 209 } 210 211 func (network *_Network) _GetNodeMaxReadmitPeriod() time.Duration { 212 return network._ManagedNetwork.maxNodeReadmitPeriod 213 } 214 215 // Close closes the network. 216 func (network *_Network) Close() error { 217 err := network._ManagedNetwork._Close() 218 if err != nil { 219 return err 220 } 221 222 return nil 223 } 224 225 func _NetworkForMainnet(nodeAddresses map[AccountID]NodeAddress) *_Network { 226 network := _NewNetwork() 227 network.addressBook = nodeAddresses 228 network._SetLedgerID(*NewLedgerIDMainnet()) 229 _ = network.SetNetwork(network._ToNet()) 230 return &network 231 } 232 233 func _NetworkForTestnet(nodeAddresses map[AccountID]NodeAddress) *_Network { 234 network := _NewNetwork() 235 network.addressBook = nodeAddresses 236 network._SetLedgerID(*NewLedgerIDTestnet()) 237 _ = network.SetNetwork(network._ToNet()) 238 return &network 239 } 240 241 func _NetworkForPreviewnet(nodeAddresses map[AccountID]NodeAddress) *_Network { 242 network := _NewNetwork() 243 network.addressBook = nodeAddresses 244 network._SetLedgerID(*NewLedgerIDPreviewnet()) 245 _ = network.SetNetwork(network._ToNet()) 246 return &network 247 } 248 249 func (network *_Network) _SetNetworkFromAddressBook(addressBook NodeAddressBook) { 250 network.addressBook = addressBook._ToMap() 251 _ = network.SetNetwork(network._ToNet()) 252 } 253 254 func (network *_Network) _ToNet() map[string]AccountID { 255 newNetwork := make(map[string]AccountID) 256 for accountID, node := range network.addressBook { 257 for _, address := range node.Addresses { 258 newNetwork[address.String()] = accountID 259 } 260 } 261 return newNetwork 262 }