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

     1  //go:build all || unit
     2  // +build all unit
     3  
     4  package hedera
     5  
     6  /*-
     7   *
     8   * Hedera Go SDK
     9   *
    10   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
    11   *
    12   * Licensed under the Apache License, Version 2.0 (the "License");
    13   * you may not use this file except in compliance with the License.
    14   * You may obtain a copy of the License at
    15   *
    16   *      http://www.apache.org/licenses/LICENSE-2.0
    17   *
    18   * Unless required by applicable law or agreed to in writing, software
    19   * distributed under the License is distributed on an "AS IS" BASIS,
    20   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    21   * See the License for the specific language governing permissions and
    22   * limitations under the License.
    23   *
    24   */
    25  
    26  import (
    27  	"encoding/hex"
    28  	"testing"
    29  	"time"
    30  
    31  	"github.com/hashgraph/hedera-protobufs-go/services"
    32  	protobuf "google.golang.org/protobuf/proto"
    33  
    34  	"github.com/stretchr/testify/assert"
    35  
    36  	"github.com/stretchr/testify/require"
    37  )
    38  
    39  func TestUnitContractUpdateTransactionValidate(t *testing.T) {
    40  	t.Parallel()
    41  
    42  	client, err := _NewMockClient()
    43  	client.SetLedgerID(*NewLedgerIDTestnet())
    44  	require.NoError(t, err)
    45  	client.SetAutoValidateChecksums(true)
    46  	contractID, err := ContractIDFromString("0.0.123-esxsf")
    47  	require.NoError(t, err)
    48  	accountID, err := AccountIDFromString("0.0.123-esxsf")
    49  	require.NoError(t, err)
    50  	fileID, err := FileIDFromString("0.0.123-esxsf")
    51  	require.NoError(t, err)
    52  
    53  	contractInfoQuery := NewContractUpdateTransaction().
    54  		SetContractID(contractID).
    55  		SetProxyAccountID(accountID).
    56  		SetBytecodeFileID(fileID)
    57  
    58  	err = contractInfoQuery.validateNetworkOnIDs(client)
    59  	require.NoError(t, err)
    60  }
    61  
    62  func TestUnitContractUpdateTransactionValidateWrong(t *testing.T) {
    63  	t.Parallel()
    64  
    65  	client, err := _NewMockClient()
    66  	client.SetLedgerID(*NewLedgerIDTestnet())
    67  	require.NoError(t, err)
    68  	client.SetAutoValidateChecksums(true)
    69  	contractID, err := ContractIDFromString("0.0.123-rmkykd")
    70  	require.NoError(t, err)
    71  	accountID, err := AccountIDFromString("0.0.123-rmkykd")
    72  	require.NoError(t, err)
    73  	fileID, err := FileIDFromString("0.0.123-rmkykd")
    74  	require.NoError(t, err)
    75  
    76  	contractInfoQuery := NewContractUpdateTransaction().
    77  		SetContractID(contractID).
    78  		SetProxyAccountID(accountID).
    79  		SetBytecodeFileID(fileID)
    80  
    81  	err = contractInfoQuery.validateNetworkOnIDs(client)
    82  	assert.Error(t, err)
    83  	if err != nil {
    84  		assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error())
    85  	}
    86  }
    87  
    88  func TestUnitContractUpdateTransactionMock(t *testing.T) {
    89  	t.Parallel()
    90  
    91  	call := func(request *services.Transaction) *services.TransactionResponse {
    92  		require.NotEmpty(t, request.SignedTransactionBytes)
    93  		signedTransaction := services.SignedTransaction{}
    94  		_ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction)
    95  
    96  		require.NotEmpty(t, signedTransaction.BodyBytes)
    97  		transactionBody := services.TransactionBody{}
    98  		_ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody)
    99  
   100  		require.NotNil(t, transactionBody.TransactionID)
   101  		transactionId := transactionBody.TransactionID.String()
   102  		require.NotEqual(t, "", transactionId)
   103  
   104  		sigMap := signedTransaction.GetSigMap()
   105  		require.NotNil(t, sigMap)
   106  
   107  		for _, sigPair := range sigMap.SigPair {
   108  			verified := false
   109  
   110  			switch k := sigPair.Signature.(type) {
   111  			case *services.SignaturePair_Ed25519:
   112  				pbTemp, _ := PublicKeyFromBytesEd25519(sigPair.PubKeyPrefix)
   113  				verified = pbTemp.Verify(signedTransaction.BodyBytes, k.Ed25519)
   114  			case *services.SignaturePair_ECDSASecp256K1:
   115  				pbTemp, _ := PublicKeyFromBytesECDSA(sigPair.PubKeyPrefix)
   116  				verified = pbTemp.Verify(signedTransaction.BodyBytes, k.ECDSASecp256K1)
   117  			}
   118  			require.True(t, verified)
   119  		}
   120  
   121  		if bod, ok := transactionBody.Data.(*services.TransactionBody_ContractUpdateInstance); ok {
   122  			require.Equal(t, bod.ContractUpdateInstance.ContractID.GetContractNum(), int64(3))
   123  			if mem, ok2 := bod.ContractUpdateInstance.MemoField.(*services.ContractUpdateTransactionBody_MemoWrapper); ok2 {
   124  				require.Equal(t, mem.MemoWrapper.GetValue(), "yes")
   125  			}
   126  			require.Equal(t, hex.EncodeToString(bod.ContractUpdateInstance.GetAdminKey().GetEd25519()), "1480272863d39c42f902bc11601a968eaf30ad662694e3044c86d5df46fabfd2")
   127  		}
   128  
   129  		return &services.TransactionResponse{
   130  			NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK,
   131  		}
   132  	}
   133  	responses := [][]interface{}{{
   134  		call,
   135  	}}
   136  
   137  	client, server := NewMockClientAndServer(responses)
   138  	defer server.Close()
   139  	//302a300506032b65700321001480272863d39c42f902bc11601a968eaf30ad662694e3044c86d5df46fabfd2
   140  	newKey, err := PrivateKeyFromStringEd25519("302e020100300506032b657004220420278184257eb568d0e5fcfc1df99828b039b4776da05855dc5af105996e6200d1")
   141  	require.NoError(t, err)
   142  
   143  	tran := TransactionIDGenerate(AccountID{Account: 3})
   144  
   145  	_, err = NewContractUpdateTransaction().
   146  		SetNodeAccountIDs([]AccountID{{Account: 3}}).
   147  		SetTransactionID(tran).
   148  		SetAdminKey(newKey.PublicKey()).
   149  		SetContractMemo("yes").
   150  		SetContractID(ContractID{Contract: 3}).
   151  		Execute(client)
   152  	require.NoError(t, err)
   153  }
   154  
   155  func TestUnitContractUpdateTransactionGet(t *testing.T) {
   156  	t.Parallel()
   157  
   158  	contractID := ContractID{Contract: 7}
   159  
   160  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   161  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   162  
   163  	newKey, err := PrivateKeyGenerateEd25519()
   164  	require.NoError(t, err)
   165  
   166  	transaction, err := NewContractUpdateTransaction().
   167  		SetTransactionID(transactionID).
   168  		SetNodeAccountIDs(nodeAccountID).
   169  		SetContractID(contractID).
   170  		SetAdminKey(newKey.PublicKey()).
   171  		SetContractMemo("yes").
   172  		SetTransactionMemo("").
   173  		SetTransactionValidDuration(60 * time.Second).
   174  		SetRegenerateTransactionID(false).
   175  		Freeze()
   176  	require.NoError(t, err)
   177  
   178  	transaction.GetTransactionID()
   179  	transaction.GetNodeAccountIDs()
   180  
   181  	_, err = transaction.GetTransactionHash()
   182  	require.NoError(t, err)
   183  
   184  	transaction.GetContractID()
   185  	transaction.GetMaxTransactionFee()
   186  	transaction.GetTransactionMemo()
   187  	transaction.GetRegenerateTransactionID()
   188  	_, err = transaction.GetSignatures()
   189  	require.NoError(t, err)
   190  	transaction.GetRegenerateTransactionID()
   191  	transaction.GetMaxTransactionFee()
   192  	transaction.GetAdminKey()
   193  	transaction.GetRegenerateTransactionID()
   194  	transaction.GetContractMemo()
   195  }
   196  
   197  func TestUnitContractUpdateTransactionSetNothing(t *testing.T) {
   198  	t.Parallel()
   199  
   200  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   201  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   202  
   203  	transaction, err := NewContractUpdateTransaction().
   204  		SetTransactionID(transactionID).
   205  		SetNodeAccountIDs(nodeAccountID).
   206  		Freeze()
   207  	require.NoError(t, err)
   208  
   209  	transaction.GetTransactionID()
   210  	transaction.GetNodeAccountIDs()
   211  
   212  	_, err = transaction.GetTransactionHash()
   213  	require.NoError(t, err)
   214  
   215  	transaction.GetContractID()
   216  	transaction.GetMaxTransactionFee()
   217  	transaction.GetTransactionMemo()
   218  	transaction.GetRegenerateTransactionID()
   219  	_, err = transaction.GetSignatures()
   220  	require.NoError(t, err)
   221  	transaction.GetRegenerateTransactionID()
   222  	transaction.GetMaxTransactionFee()
   223  	transaction.GetAdminKey()
   224  	transaction.GetRegenerateTransactionID()
   225  	transaction.GetContractMemo()
   226  }
   227  
   228  func TestUnitContractUpdateTransactionProtoCheck(t *testing.T) {
   229  	t.Parallel()
   230  
   231  	contractID := ContractID{Contract: 7}
   232  	accountID := AccountID{Account: 7}
   233  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   234  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   235  
   236  	newKey, err := PrivateKeyGenerateEd25519()
   237  	require.NoError(t, err)
   238  
   239  	transaction, err := NewContractUpdateTransaction().
   240  		SetTransactionID(transactionID).
   241  		SetNodeAccountIDs(nodeAccountID).
   242  		SetContractID(contractID).
   243  		SetAdminKey(newKey.PublicKey()).
   244  		SetContractMemo("yes").
   245  		SetStakedAccountID(accountID).
   246  		SetMaxAutomaticTokenAssociations(3).
   247  		SetAutoRenewPeriod(time.Second * 3).
   248  		SetExpirationTime(time.Unix(34, 3)).
   249  		SetAutoRenewAccountID(accountID).
   250  		SetTransactionMemo("").
   251  		SetTransactionValidDuration(60 * time.Second).
   252  		SetRegenerateTransactionID(false).
   253  		Freeze()
   254  	require.NoError(t, err)
   255  
   256  	transaction.GetTransactionID()
   257  	transaction.GetNodeAccountIDs()
   258  
   259  	proto := transaction.build().GetContractUpdateInstance()
   260  	require.Equal(t, proto.AdminKey.String(), newKey._ToProtoKey().String())
   261  	require.Equal(t, proto.ContractID.String(), contractID._ToProtobuf().String())
   262  	require.Equal(t, proto.MemoField.(*services.ContractUpdateTransactionBody_MemoWrapper).MemoWrapper.Value, "yes")
   263  	require.Equal(t, proto.StakedId.(*services.ContractUpdateTransactionBody_StakedAccountId).StakedAccountId.String(),
   264  		accountID._ToProtobuf().String())
   265  	require.Equal(t, proto.MaxAutomaticTokenAssociations.Value, int32(3))
   266  	require.Equal(t, proto.AutoRenewPeriod.String(), _DurationToProtobuf(time.Second*3).String())
   267  	require.Equal(t, proto.ExpirationTime.String(), _TimeToProtobuf(time.Unix(34, 3)).String())
   268  	require.Equal(t, proto.AutoRenewAccountId.String(), accountID._ToProtobuf().String())
   269  }
   270  
   271  func TestUnitContractUpdateTransactionCoverage(t *testing.T) {
   272  	t.Parallel()
   273  
   274  	checksum := "dmqui"
   275  	grpc := time.Second * 30
   276  	file := FileID{File: 3, checksum: &checksum}
   277  	account := AccountID{Account: 3, checksum: &checksum}
   278  	contract := ContractID{Contract: 3, checksum: &checksum}
   279  	nodeAccountID := []AccountID{{Account: 10}}
   280  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   281  
   282  	newKey, err := PrivateKeyGenerateEd25519()
   283  	require.NoError(t, err)
   284  
   285  	client, err := _NewMockClient()
   286  	client.SetLedgerID(*NewLedgerIDTestnet())
   287  	require.NoError(t, err)
   288  	client.SetAutoValidateChecksums(true)
   289  
   290  	transaction, err := NewContractUpdateTransaction().
   291  		SetTransactionID(transactionID).
   292  		SetNodeAccountIDs(nodeAccountID).
   293  		SetAdminKey(newKey.PublicKey()).
   294  		SetBytecodeFileID(file).
   295  		SetContractMemo("yes").
   296  		SetStakedAccountID(account).
   297  		SetStakedNodeID(3).
   298  		SetContractID(contract).
   299  		SetProxyAccountID(account).
   300  		SetDeclineStakingReward(true).
   301  		SetAutoRenewPeriod(time.Second * 30).
   302  		SetExpirationTime(time.Unix(345, 566)).
   303  		SetGrpcDeadline(&grpc).
   304  		SetMaxTransactionFee(NewHbar(3)).
   305  		SetMaxRetry(3).
   306  		SetMaxBackoff(time.Second * 30).
   307  		SetMinBackoff(time.Second * 10).
   308  		SetTransactionMemo("no").
   309  		SetTransactionValidDuration(time.Second * 30).
   310  		SetRegenerateTransactionID(false).
   311  		Freeze()
   312  	require.NoError(t, err)
   313  
   314  	transaction.validateNetworkOnIDs(client)
   315  
   316  	_, err = transaction.Schedule()
   317  	require.NoError(t, err)
   318  	transaction.GetTransactionID()
   319  	transaction.GetNodeAccountIDs()
   320  	transaction.GetMaxRetry()
   321  	transaction.GetMaxTransactionFee()
   322  	transaction.GetMaxBackoff()
   323  	transaction.GetMinBackoff()
   324  	transaction.GetRegenerateTransactionID()
   325  	byt, err := transaction.ToBytes()
   326  	require.NoError(t, err)
   327  	txFromBytes, err := TransactionFromBytes(byt)
   328  	require.NoError(t, err)
   329  	sig, err := newKey.SignTransaction(&transaction.Transaction)
   330  	require.NoError(t, err)
   331  
   332  	_, err = transaction.GetTransactionHash()
   333  	require.NoError(t, err)
   334  	transaction.GetMaxTransactionFee()
   335  	transaction.GetTransactionMemo()
   336  	transaction.GetRegenerateTransactionID()
   337  	transaction.GetStakedAccountID()
   338  	transaction.GetStakedNodeID()
   339  	transaction.GetProxyAccountID()
   340  	transaction.GetBytecodeFileID()
   341  	transaction.GetAutoRenewPeriod()
   342  	transaction.GetAutoRenewAccountID()
   343  	transaction.GetExpirationTime()
   344  	transaction.GetDeclineStakingReward()
   345  	transaction.ClearStakedAccountID()
   346  	transaction.ClearStakedNodeID()
   347  	_, err = transaction.GetSignatures()
   348  	require.NoError(t, err)
   349  	transaction.getName()
   350  	switch b := txFromBytes.(type) {
   351  	case ContractUpdateTransaction:
   352  		b.AddSignature(newKey.PublicKey(), sig)
   353  	}
   354  }