github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/node_create_transaction.go (about) 1 package hedera 2 3 import ( 4 "time" 5 6 "github.com/hashgraph/hedera-protobufs-go/services" 7 ) 8 9 /*- 10 * 11 * Hedera Go SDK 12 * 13 * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC 14 * 15 * Licensed under the Apache License, Version 2.0 (the "License"); 16 * you may not use this file except in compliance with the License. 17 * You may obtain a copy of the License at 18 * 19 * http://www.apache.org/licenses/LICENSE-2.0 20 * 21 * Unless required by applicable law or agreed to in writing, software 22 * distributed under the License is distributed on an "AS IS" BASIS, 23 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 24 * See the License for the specific language governing permissions and 25 * limitations under the License. 26 * 27 */ 28 29 /** 30 * A transaction to create a new node in the network address book. 31 * The transaction, once complete, enables a new consensus node 32 * to join the network, and requires governing council authorization. 33 * <p> 34 * This transaction body SHALL be considered a "privileged transaction". 35 * <p> 36 * 37 * - MUST be signed by the governing council. 38 * - MUST be signed by the `Key` assigned to the 39 * `admin_key` field. 40 * - The newly created node information SHALL be added to the network address 41 * book information in the network state. 42 * - The new entry SHALL be created in "state" but SHALL NOT participate in 43 * network consensus and SHALL NOT be present in network "configuration" 44 * until the next "upgrade" transaction (as noted below). 45 * - All new address book entries SHALL be added to the active network 46 * configuration during the next `freeze` transaction with the field 47 * `freeze_type` set to `PREPARE_UPGRADE`. 48 * 49 * ### Record Stream Effects 50 * Upon completion the newly assigned `node_id` SHALL be in the transaction 51 * receipt. 52 */ 53 type NodeCreateTransaction struct { 54 Transaction 55 accountID *AccountID 56 description string 57 gossipEndpoints []Endpoint 58 serviceEndpoints []Endpoint 59 gossipCaCertificate []byte 60 grpcCertificateHash []byte 61 adminKey Key 62 } 63 64 func NewNodeCreateTransaction() *NodeCreateTransaction { 65 tx := &NodeCreateTransaction{ 66 Transaction: _NewTransaction(), 67 } 68 tx._SetDefaultMaxTransactionFee(NewHbar(5)) 69 70 return tx 71 } 72 73 func _NodeCreateTransactionFromProtobuf(transaction Transaction, pb *services.TransactionBody) *NodeCreateTransaction { 74 adminKey, err := _KeyFromProtobuf(pb.GetNodeCreate().GetAdminKey()) 75 if err != nil { 76 return &NodeCreateTransaction{} 77 } 78 79 accountID := _AccountIDFromProtobuf(pb.GetNodeCreate().GetAccountId()) 80 gossipEndpoints := make([]Endpoint, 0) 81 for _, endpoint := range pb.GetNodeCreate().GetGossipEndpoint() { 82 gossipEndpoints = append(gossipEndpoints, EndpointFromProtobuf(endpoint)) 83 } 84 serviceEndpoints := make([]Endpoint, 0) 85 for _, endpoint := range pb.GetNodeCreate().GetServiceEndpoint() { 86 serviceEndpoints = append(serviceEndpoints, EndpointFromProtobuf(endpoint)) 87 } 88 89 return &NodeCreateTransaction{ 90 Transaction: transaction, 91 accountID: accountID, 92 description: pb.GetNodeCreate().GetDescription(), 93 gossipEndpoints: gossipEndpoints, 94 serviceEndpoints: serviceEndpoints, 95 gossipCaCertificate: pb.GetNodeCreate().GetGossipCaCertificate(), 96 grpcCertificateHash: pb.GetNodeCreate().GetGrpcCertificateHash(), 97 adminKey: adminKey, 98 } 99 } 100 101 // GetAccountID AccountID of the node 102 func (tx *NodeCreateTransaction) GetAccountID() AccountID { 103 if tx.accountID == nil { 104 return AccountID{} 105 } 106 107 return *tx.accountID 108 } 109 110 // SetAccountID get the AccountID of the node 111 func (tx *NodeCreateTransaction) SetAccountID(accountID AccountID) *NodeCreateTransaction { 112 tx._RequireNotFrozen() 113 tx.accountID = &accountID 114 return tx 115 } 116 117 // GetDescription get the description of the node 118 func (tx *NodeCreateTransaction) GetDescription() string { 119 return tx.description 120 } 121 122 // SetDescription set the description of the node 123 func (tx *NodeCreateTransaction) SetDescription(description string) *NodeCreateTransaction { 124 tx._RequireNotFrozen() 125 tx.description = description 126 return tx 127 } 128 129 // GetServiceEndpoints the list of service endpoints for gossip. 130 func (tx *NodeCreateTransaction) GetGossipEndpoints() []Endpoint { 131 return tx.gossipEndpoints 132 } 133 134 // SetServiceEndpoints the list of service endpoints for gossip. 135 func (tx *NodeCreateTransaction) SetGossipEndpoints(gossipEndpoints []Endpoint) *NodeCreateTransaction { 136 tx._RequireNotFrozen() 137 tx.gossipEndpoints = gossipEndpoints 138 return tx 139 } 140 141 // AddGossipEndpoint add an endpoint for gossip to the list of service endpoints for gossip. 142 func (tx *NodeCreateTransaction) AddGossipEndpoint(endpoint Endpoint) *NodeCreateTransaction { 143 tx._RequireNotFrozen() 144 tx.gossipEndpoints = append(tx.gossipEndpoints, endpoint) 145 return tx 146 } 147 148 // GetServiceEndpoints the list of service endpoints for gRPC calls. 149 func (tx *NodeCreateTransaction) GetServiceEndpoints() []Endpoint { 150 return tx.serviceEndpoints 151 } 152 153 // SetServiceEndpoints the list of service endpoints for gRPC calls. 154 func (tx *NodeCreateTransaction) SetServiceEndpoints(serviceEndpoints []Endpoint) *NodeCreateTransaction { 155 tx._RequireNotFrozen() 156 tx.serviceEndpoints = serviceEndpoints 157 return tx 158 } 159 160 // AddServiceEndpoint the list of service endpoints for gRPC calls. 161 func (tx *NodeCreateTransaction) AddServiceEndpoint(endpoint Endpoint) *NodeCreateTransaction { 162 tx._RequireNotFrozen() 163 tx.serviceEndpoints = append(tx.serviceEndpoints, endpoint) 164 return tx 165 } 166 167 // GetGossipCaCertificate the certificate used to sign gossip events. 168 func (tx *NodeCreateTransaction) GetGossipCaCertificate() []byte { 169 return tx.gossipCaCertificate 170 } 171 172 // SetGossipCaCertificate the certificate used to sign gossip events. 173 // This value MUST be the DER encoding of the certificate presented. 174 func (tx *NodeCreateTransaction) SetGossipCaCertificate(gossipCaCertificate []byte) *NodeCreateTransaction { 175 tx._RequireNotFrozen() 176 tx.gossipCaCertificate = gossipCaCertificate 177 return tx 178 } 179 180 // GetGrpcCertificateHash the hash of the node gRPC TLS certificate. 181 func (tx *NodeCreateTransaction) GetGrpcCertificateHash() []byte { 182 return tx.grpcCertificateHash 183 } 184 185 // SetGrpcCertificateHash the hash of the node gRPC TLS certificate. 186 // This value MUST be a SHA-384 hash. 187 func (tx *NodeCreateTransaction) SetGrpcCertificateHash(grpcCertificateHash []byte) *NodeCreateTransaction { 188 tx._RequireNotFrozen() 189 tx.grpcCertificateHash = grpcCertificateHash 190 return tx 191 } 192 193 // GetAdminKey an administrative key controlled by the node operator. 194 func (tx *NodeCreateTransaction) GetAdminKey() Key { 195 return tx.adminKey 196 } 197 198 // SetAdminKey an administrative key controlled by the node operator. 199 func (tx *NodeCreateTransaction) SetAdminKey(adminKey Key) *NodeCreateTransaction { 200 tx._RequireNotFrozen() 201 tx.adminKey = adminKey 202 return tx 203 } 204 205 // ---- Required Interfaces ---- // 206 207 // Sign uses the provided privateKey to sign the transaction. 208 func (tx *NodeCreateTransaction) Sign(privateKey PrivateKey) *NodeCreateTransaction { 209 tx.Transaction.Sign(privateKey) 210 return tx 211 } 212 213 // SignWithOperator signs the transaction with client's operator privateKey. 214 func (tx *NodeCreateTransaction) SignWithOperator(client *Client) (*NodeCreateTransaction, error) { 215 _, err := tx.Transaction.signWithOperator(client, tx) 216 if err != nil { 217 return nil, err 218 } 219 return tx, nil 220 } 221 222 // SignWith executes the TransactionSigner and adds the resulting signature data to the Transaction's signature map 223 // with the publicKey as the map key. 224 func (tx *NodeCreateTransaction) SignWith( 225 publicKey PublicKey, 226 signer TransactionSigner, 227 ) *NodeCreateTransaction { 228 tx.Transaction.SignWith(publicKey, signer) 229 return tx 230 } 231 232 // AddSignature adds a signature to the transaction. 233 func (tx *NodeCreateTransaction) AddSignature(publicKey PublicKey, signature []byte) *NodeCreateTransaction { 234 tx.Transaction.AddSignature(publicKey, signature) 235 return tx 236 } 237 238 // When execution is attempted, a single attempt will timeout when this deadline is reached. (The SDK may subsequently retry the execution.) 239 func (tx *NodeCreateTransaction) SetGrpcDeadline(deadline *time.Duration) *NodeCreateTransaction { 240 tx.Transaction.SetGrpcDeadline(deadline) 241 return tx 242 } 243 244 func (tx *NodeCreateTransaction) Freeze() (*NodeCreateTransaction, error) { 245 return tx.FreezeWith(nil) 246 } 247 248 func (tx *NodeCreateTransaction) FreezeWith(client *Client) (*NodeCreateTransaction, error) { 249 _, err := tx.Transaction.freezeWith(client, tx) 250 return tx, err 251 } 252 253 // SetMaxTransactionFee sets the max transaction fee for this NodeCreateTransaction. 254 func (tx *NodeCreateTransaction) SetMaxTransactionFee(fee Hbar) *NodeCreateTransaction { 255 tx.Transaction.SetMaxTransactionFee(fee) 256 return tx 257 } 258 259 // SetRegenerateTransactionID sets if transaction IDs should be regenerated when `TRANSACTION_EXPIRED` is received 260 func (tx *NodeCreateTransaction) SetRegenerateTransactionID(regenerateTransactionID bool) *NodeCreateTransaction { 261 tx.Transaction.SetRegenerateTransactionID(regenerateTransactionID) 262 return tx 263 } 264 265 // SetTransactionMemo sets the memo for this NodeCreateTransaction. 266 func (tx *NodeCreateTransaction) SetTransactionMemo(memo string) *NodeCreateTransaction { 267 tx.Transaction.SetTransactionMemo(memo) 268 return tx 269 } 270 271 // SetTransactionValidDuration sets the valid duration for this NodeCreateTransaction. 272 func (tx *NodeCreateTransaction) SetTransactionValidDuration(duration time.Duration) *NodeCreateTransaction { 273 tx.Transaction.SetTransactionValidDuration(duration) 274 return tx 275 } 276 277 // ToBytes serialise the tx to bytes, no matter if it is signed (locked), or not 278 func (tx *NodeCreateTransaction) ToBytes() ([]byte, error) { 279 bytes, err := tx.Transaction.toBytes(tx) 280 if err != nil { 281 return nil, err 282 } 283 return bytes, nil 284 } 285 286 // SetTransactionID sets the TransactionID for this NodeCreateTransaction. 287 func (tx *NodeCreateTransaction) SetTransactionID(transactionID TransactionID) *NodeCreateTransaction { 288 tx.Transaction.SetTransactionID(transactionID) 289 return tx 290 } 291 292 // SetNodeAccountIDs sets the _Node AccountID for this NodeCreateTransaction. 293 func (tx *NodeCreateTransaction) SetNodeAccountIDs(nodeID []AccountID) *NodeCreateTransaction { 294 tx.Transaction.SetNodeAccountIDs(nodeID) 295 return tx 296 } 297 298 // SetMaxRetry sets the max number of errors before execution will fail. 299 func (tx *NodeCreateTransaction) SetMaxRetry(count int) *NodeCreateTransaction { 300 tx.Transaction.SetMaxRetry(count) 301 return tx 302 } 303 304 // SetMaxBackoff The maximum amount of time to wait between retries. 305 // Every retry attempt will increase the wait time exponentially until it reaches this time. 306 func (tx *NodeCreateTransaction) SetMaxBackoff(max time.Duration) *NodeCreateTransaction { 307 tx.Transaction.SetMaxBackoff(max) 308 return tx 309 } 310 311 // SetMinBackoff sets the minimum amount of time to wait between retries. 312 func (tx *NodeCreateTransaction) SetMinBackoff(min time.Duration) *NodeCreateTransaction { 313 tx.Transaction.SetMinBackoff(min) 314 return tx 315 } 316 317 func (tx *NodeCreateTransaction) SetLogLevel(level LogLevel) *NodeCreateTransaction { 318 tx.Transaction.SetLogLevel(level) 319 return tx 320 } 321 322 func (tx *NodeCreateTransaction) Execute(client *Client) (TransactionResponse, error) { 323 return tx.Transaction.execute(client, tx) 324 } 325 326 func (tx *NodeCreateTransaction) Schedule() (*ScheduleCreateTransaction, error) { 327 return tx.Transaction.schedule(tx) 328 } 329 330 // ----------- Overridden functions ---------------- 331 332 func (tx *NodeCreateTransaction) getName() string { 333 return "NodeCreateTransaction" 334 } 335 336 func (tx *NodeCreateTransaction) validateNetworkOnIDs(client *Client) error { 337 if client == nil || !client.autoValidateChecksums { 338 return nil 339 } 340 341 if tx.accountID != nil { 342 if err := tx.accountID.ValidateChecksum(client); err != nil { 343 return err 344 } 345 } 346 347 return nil 348 } 349 350 func (tx *NodeCreateTransaction) build() *services.TransactionBody { 351 return &services.TransactionBody{ 352 TransactionFee: tx.transactionFee, 353 Memo: tx.Transaction.memo, 354 TransactionValidDuration: _DurationToProtobuf(tx.GetTransactionValidDuration()), 355 TransactionID: tx.transactionID._ToProtobuf(), 356 Data: &services.TransactionBody_NodeCreate{ 357 NodeCreate: tx.buildProtoBody(), 358 }, 359 } 360 } 361 362 func (tx *NodeCreateTransaction) buildScheduled() (*services.SchedulableTransactionBody, error) { 363 return &services.SchedulableTransactionBody{ 364 TransactionFee: tx.transactionFee, 365 Memo: tx.Transaction.memo, 366 Data: &services.SchedulableTransactionBody_NodeCreate{ 367 NodeCreate: tx.buildProtoBody(), 368 }, 369 }, nil 370 } 371 372 func (tx *NodeCreateTransaction) buildProtoBody() *services.NodeCreateTransactionBody { 373 body := &services.NodeCreateTransactionBody{ 374 Description: tx.description, 375 } 376 377 if tx.accountID != nil { 378 body.AccountId = tx.accountID._ToProtobuf() 379 } 380 381 for _, endpoint := range tx.gossipEndpoints { 382 body.GossipEndpoint = append(body.GossipEndpoint, endpoint._ToProtobuf()) 383 } 384 385 for _, endpoint := range tx.serviceEndpoints { 386 body.ServiceEndpoint = append(body.ServiceEndpoint, endpoint._ToProtobuf()) 387 } 388 389 if tx.gossipCaCertificate != nil { 390 body.GossipCaCertificate = tx.gossipCaCertificate 391 } 392 393 if tx.grpcCertificateHash != nil { 394 body.GrpcCertificateHash = tx.grpcCertificateHash 395 } 396 397 if tx.adminKey != nil { 398 body.AdminKey = tx.adminKey._ToProtoKey() 399 } 400 401 return body 402 } 403 404 func (tx *NodeCreateTransaction) getMethod(channel *_Channel) _Method { 405 return _Method{ 406 transaction: channel._GetAddressBook().CreateNode, 407 } 408 } 409 410 func (tx *NodeCreateTransaction) preFreezeWith(client *Client) { 411 // No special actions needed. 412 } 413 414 func (tx *NodeCreateTransaction) _ConstructScheduleProtobuf() (*services.SchedulableTransactionBody, error) { 415 return tx.buildScheduled() 416 }