github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/node_update_transaction.go (about)

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