github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/contract_create_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  	"bytes"
    28  	"encoding/hex"
    29  	"testing"
    30  	"time"
    31  
    32  	"github.com/hashgraph/hedera-protobufs-go/services"
    33  	protobuf "google.golang.org/protobuf/proto"
    34  
    35  	"github.com/stretchr/testify/assert"
    36  
    37  	"github.com/stretchr/testify/require"
    38  )
    39  
    40  func TestUnitContractCreateTransactionValidate(t *testing.T) {
    41  	t.Parallel()
    42  
    43  	client, err := _NewMockClient()
    44  	client.SetLedgerID(*NewLedgerIDTestnet())
    45  	require.NoError(t, err)
    46  	client.SetAutoValidateChecksums(true)
    47  	accountID, err := AccountIDFromString("0.0.123-esxsf")
    48  	require.NoError(t, err)
    49  	fileID, err := FileIDFromString("0.0.123-esxsf")
    50  	require.NoError(t, err)
    51  
    52  	contractCreate := NewContractCreateTransaction().
    53  		SetProxyAccountID(accountID).
    54  		SetBytecodeFileID(fileID)
    55  
    56  	err = contractCreate.validateNetworkOnIDs(client)
    57  	require.NoError(t, err)
    58  }
    59  
    60  func TestUnitContractCreateTransactionValidateWrong(t *testing.T) {
    61  	t.Parallel()
    62  
    63  	client, err := _NewMockClient()
    64  	client.SetLedgerID(*NewLedgerIDTestnet())
    65  	require.NoError(t, err)
    66  	client.SetAutoValidateChecksums(true)
    67  	accountID, err := AccountIDFromString("0.0.123-rmkykd")
    68  	require.NoError(t, err)
    69  	fileID, err := FileIDFromString("0.0.123-rmkykd")
    70  	require.NoError(t, err)
    71  
    72  	contractCreate := NewContractCreateTransaction().
    73  		SetProxyAccountID(accountID).
    74  		SetBytecodeFileID(fileID)
    75  
    76  	err = contractCreate.validateNetworkOnIDs(client)
    77  	assert.Error(t, err)
    78  	if err != nil {
    79  		assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error())
    80  	}
    81  }
    82  
    83  func TestUnitContractCreateTransactionMock(t *testing.T) {
    84  	t.Parallel()
    85  
    86  	key, err := PrivateKeyFromStringEd25519("302e020100300506032b657004220420d45e1557156908c967804615af59a000be88c7aa7058bfcbe0f46b16c28f887d")
    87  	require.NoError(t, err)
    88  
    89  	call := func(request *services.Transaction) *services.TransactionResponse {
    90  		require.NotEmpty(t, request.SignedTransactionBytes)
    91  		signedTransaction := services.SignedTransaction{}
    92  		_ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction)
    93  
    94  		require.NotEmpty(t, signedTransaction.BodyBytes)
    95  		transactionBody := services.TransactionBody{}
    96  		_ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody)
    97  
    98  		require.NotNil(t, transactionBody.TransactionID)
    99  		transactionId := transactionBody.TransactionID.String()
   100  		require.NotEqual(t, "", transactionId)
   101  
   102  		sigMap := signedTransaction.GetSigMap()
   103  		require.NotNil(t, sigMap)
   104  
   105  		for _, sigPair := range sigMap.SigPair {
   106  			verified := false
   107  
   108  			switch k := sigPair.Signature.(type) {
   109  			case *services.SignaturePair_Ed25519:
   110  				pbTemp, _ := PublicKeyFromBytesEd25519(sigPair.PubKeyPrefix)
   111  				verified = pbTemp.Verify(signedTransaction.BodyBytes, k.Ed25519)
   112  			case *services.SignaturePair_ECDSASecp256K1:
   113  				pbTemp, _ := PublicKeyFromBytesECDSA(sigPair.PubKeyPrefix)
   114  				verified = pbTemp.Verify(signedTransaction.BodyBytes, k.ECDSASecp256K1)
   115  			}
   116  			require.True(t, verified)
   117  		}
   118  
   119  		if bod, ok := transactionBody.Data.(*services.TransactionBody_ContractCreateInstance); ok {
   120  			require.Equal(t, bod.ContractCreateInstance.InitcodeSource.(*services.ContractCreateTransactionBody_FileID).FileID.FileNum, int64(123))
   121  			params := NewContractFunctionParameters().AddString("hello from hedera")
   122  			require.Equal(t, bytes.Compare(bod.ContractCreateInstance.ConstructorParameters, params._Build(nil)), 0)
   123  			require.Equal(t, bod.ContractCreateInstance.Memo, "hedera-sdk-go::TestContractCreateTransaction_Execute")
   124  			require.Equal(t, bod.ContractCreateInstance.Gas, int64(100000))
   125  			require.Equal(t, hex.EncodeToString(bod.ContractCreateInstance.AdminKey.GetEd25519()), key.PublicKey().StringRaw())
   126  		}
   127  
   128  		return &services.TransactionResponse{
   129  			NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK,
   130  		}
   131  	}
   132  	responses := [][]interface{}{{
   133  		call,
   134  	}}
   135  
   136  	client, server := NewMockClientAndServer(responses)
   137  	defer server.Close()
   138  
   139  	_, err = NewContractCreateTransaction().
   140  		SetAdminKey(client.GetOperatorPublicKey()).
   141  		SetNodeAccountIDs([]AccountID{{Account: 3}}).
   142  		SetGas(100000).
   143  		SetConstructorParameters(NewContractFunctionParameters().AddString("hello from hedera")).
   144  		SetBytecodeFileID(FileID{File: 123}).
   145  		SetContractMemo("hedera-sdk-go::TestContractCreateTransaction_Execute").
   146  		Execute(client)
   147  	require.NoError(t, err)
   148  }
   149  
   150  func TestUnitContractCreateTransactionGet(t *testing.T) {
   151  	t.Parallel()
   152  
   153  	spenderAccountID1 := AccountID{Account: 7}
   154  	fileID := FileID{File: 7}
   155  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   156  
   157  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   158  
   159  	transaction, err := NewContractCreateTransaction().
   160  		SetTransactionID(transactionID).
   161  		SetNodeAccountIDs(nodeAccountID).
   162  		SetGas(21341).
   163  		SetProxyAccountID(spenderAccountID1).
   164  		SetAutoRenewPeriod(60 * time.Second).
   165  		SetTransactionMemo("").
   166  		SetTransactionValidDuration(60 * time.Second).
   167  		SetContractMemo("yes").
   168  		SetBytecodeFileID(fileID).
   169  		SetRegenerateTransactionID(false).
   170  		Freeze()
   171  	require.NoError(t, err)
   172  
   173  	transaction.GetTransactionID()
   174  	transaction.GetNodeAccountIDs()
   175  
   176  	_, err = transaction.GetTransactionHash()
   177  	require.NoError(t, err)
   178  
   179  	transaction.GetContractMemo()
   180  	transaction.GetMaxTransactionFee()
   181  	transaction.GetTransactionMemo()
   182  	transaction.GetRegenerateTransactionID()
   183  	_, err = transaction.GetSignatures()
   184  	require.NoError(t, err)
   185  	transaction.GetRegenerateTransactionID()
   186  	transaction.GetMaxTransactionFee()
   187  	transaction.GetProxyAccountID()
   188  	transaction.GetRegenerateTransactionID()
   189  	transaction.GetAutoRenewPeriod()
   190  	transaction.GetBytecodeFileID()
   191  	transaction.GetContractMemo()
   192  	transaction.GetGas()
   193  }
   194  
   195  func TestUnitContractCreateTransactionSetNothing(t *testing.T) {
   196  	t.Parallel()
   197  
   198  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   199  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   200  
   201  	transaction, err := NewContractCreateTransaction().
   202  		SetTransactionID(transactionID).
   203  		SetNodeAccountIDs(nodeAccountID).
   204  		Freeze()
   205  	require.NoError(t, err)
   206  
   207  	transaction.GetTransactionID()
   208  	transaction.GetNodeAccountIDs()
   209  
   210  	_, err = transaction.GetTransactionHash()
   211  	require.NoError(t, err)
   212  
   213  	transaction.GetContractMemo()
   214  	transaction.GetMaxTransactionFee()
   215  	transaction.GetTransactionMemo()
   216  	transaction.GetRegenerateTransactionID()
   217  	_, err = transaction.GetSignatures()
   218  	require.NoError(t, err)
   219  	transaction.GetRegenerateTransactionID()
   220  	transaction.GetMaxTransactionFee()
   221  	transaction.GetProxyAccountID()
   222  	transaction.GetRegenerateTransactionID()
   223  	transaction.GetAutoRenewPeriod()
   224  	transaction.GetBytecodeFileID()
   225  	transaction.GetContractMemo()
   226  	transaction.GetGas()
   227  }
   228  
   229  func TestUnitContractCreateTransactionProtoCheck(t *testing.T) {
   230  	t.Parallel()
   231  
   232  	accountID := AccountID{Account: 7}
   233  	fileID := FileID{File: 7}
   234  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   235  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   236  
   237  	newKey, err := PrivateKeyGenerateEd25519()
   238  	require.NoError(t, err)
   239  
   240  	transaction, err := NewContractCreateTransaction().
   241  		SetTransactionID(transactionID).
   242  		SetNodeAccountIDs(nodeAccountID).
   243  		SetAdminKey(newKey.PublicKey()).
   244  		SetBytecodeFileID(fileID).
   245  		SetGas(500).
   246  		SetInitialBalance(NewHbar(50)).
   247  		SetConstructorParametersRaw([]byte{34}).
   248  		SetContractMemo("yes").
   249  		SetStakedAccountID(accountID).
   250  		SetMaxAutomaticTokenAssociations(3).
   251  		SetAutoRenewPeriod(time.Second * 3).
   252  		SetAutoRenewAccountID(accountID).
   253  		SetDeclineStakingReward(true).
   254  		SetTransactionMemo("").
   255  		SetTransactionValidDuration(60 * time.Second).
   256  		SetRegenerateTransactionID(false).
   257  		Freeze()
   258  	require.NoError(t, err)
   259  
   260  	transaction.GetTransactionID()
   261  	transaction.GetNodeAccountIDs()
   262  
   263  	proto := transaction.build().GetContractCreateInstance()
   264  	require.Equal(t, proto.AdminKey.String(), newKey._ToProtoKey().String())
   265  	require.Equal(t, proto.GetFileID().String(), fileID._ToProtobuf().String())
   266  	require.Equal(t, proto.Memo, "yes")
   267  	require.Equal(t, proto.StakedId.(*services.ContractCreateTransactionBody_StakedAccountId).StakedAccountId.String(),
   268  		accountID._ToProtobuf().String())
   269  	require.Equal(t, proto.MaxAutomaticTokenAssociations, int32(3))
   270  	require.Equal(t, proto.AutoRenewPeriod.String(), _DurationToProtobuf(time.Second*3).String())
   271  	require.Equal(t, proto.AutoRenewAccountId.String(), accountID._ToProtobuf().String())
   272  	require.Equal(t, proto.DeclineReward, true)
   273  	require.Equal(t, proto.ConstructorParameters, []byte{34})
   274  	require.Equal(t, proto.InitialBalance, NewHbar(50).AsTinybar())
   275  	require.Equal(t, proto.Gas, int64(500))
   276  }
   277  
   278  func TestUnitContractCreateTransactionCoverage(t *testing.T) {
   279  	t.Parallel()
   280  
   281  	checksum := "dmqui"
   282  	grpc := time.Second * 30
   283  	file := FileID{File: 3, checksum: &checksum}
   284  	account := AccountID{Account: 3, checksum: &checksum}
   285  	nodeAccountID := []AccountID{{Account: 10}}
   286  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   287  
   288  	newKey, err := PrivateKeyGenerateEd25519()
   289  	require.NoError(t, err)
   290  
   291  	client, err := _NewMockClient()
   292  	client.SetLedgerID(*NewLedgerIDTestnet())
   293  	require.NoError(t, err)
   294  	client.SetAutoValidateChecksums(true)
   295  
   296  	transaction, err := NewContractCreateTransaction().
   297  		SetTransactionID(transactionID).
   298  		SetNodeAccountIDs(nodeAccountID).
   299  		SetAdminKey(newKey.PublicKey()).
   300  		SetBytecodeFileID(file).
   301  		SetGas(500).
   302  		SetInitialBalance(NewHbar(50)).
   303  		SetConstructorParametersRaw([]byte{34}).
   304  		SetContractMemo("yes").
   305  		SetStakedAccountID(account).
   306  		SetStakedNodeID(3).
   307  		SetBytecode([]byte{0}).
   308  		SetGrpcDeadline(&grpc).
   309  		SetMaxTransactionFee(NewHbar(3)).
   310  		SetMaxRetry(3).
   311  		SetMaxBackoff(time.Second * 30).
   312  		SetMinBackoff(time.Second * 10).
   313  		SetTransactionMemo("no").
   314  		SetTransactionValidDuration(time.Second * 30).
   315  		SetRegenerateTransactionID(false).
   316  		Freeze()
   317  	require.NoError(t, err)
   318  
   319  	transaction.validateNetworkOnIDs(client)
   320  
   321  	_, err = transaction.Schedule()
   322  	require.NoError(t, err)
   323  	transaction.GetTransactionID()
   324  	transaction.GetNodeAccountIDs()
   325  	transaction.GetMaxRetry()
   326  	transaction.GetMaxTransactionFee()
   327  	transaction.GetMaxBackoff()
   328  	transaction.GetMinBackoff()
   329  	transaction.GetRegenerateTransactionID()
   330  	byt, err := transaction.ToBytes()
   331  	require.NoError(t, err)
   332  	txFromBytes, err := TransactionFromBytes(byt)
   333  	require.NoError(t, err)
   334  	sig, err := newKey.SignTransaction(&transaction.Transaction)
   335  	require.NoError(t, err)
   336  
   337  	_, err = transaction.GetTransactionHash()
   338  	require.NoError(t, err)
   339  	transaction.GetMaxTransactionFee()
   340  	transaction.GetTransactionMemo()
   341  	transaction.GetRegenerateTransactionID()
   342  	transaction.GetStakedAccountID()
   343  	transaction.GetStakedNodeID()
   344  	transaction.GetConstructorParameters()
   345  	transaction.GetBytecode()
   346  	transaction.GetDeclineStakingReward()
   347  	transaction.GetInitialBalance()
   348  	transaction.GetGas()
   349  	_, err = transaction.GetSignatures()
   350  	require.NoError(t, err)
   351  	transaction.getName()
   352  	switch b := txFromBytes.(type) {
   353  	case ContractCreateTransaction:
   354  		b.AddSignature(newKey.PublicKey(), sig)
   355  	}
   356  }