github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/contract_execute_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 "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 TestUnitContractExecuteTransactionValidate(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 49 contractExecute := NewContractExecuteTransaction(). 50 SetContractID(contractID) 51 52 err = contractExecute.validateNetworkOnIDs(client) 53 require.NoError(t, err) 54 } 55 56 func TestUnitContractExecuteTransactionValidateWrong(t *testing.T) { 57 t.Parallel() 58 59 client, err := _NewMockClient() 60 client.SetLedgerID(*NewLedgerIDTestnet()) 61 require.NoError(t, err) 62 client.SetAutoValidateChecksums(true) 63 contractID, err := ContractIDFromString("0.0.123-rmkykd") 64 require.NoError(t, err) 65 66 contractExecute := NewContractExecuteTransaction(). 67 SetContractID(contractID) 68 69 err = contractExecute.validateNetworkOnIDs(client) 70 assert.Error(t, err) 71 if err != nil { 72 assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error()) 73 } 74 } 75 76 func TestUnitContractExecuteTransactionMock(t *testing.T) { 77 t.Parallel() 78 79 params := NewContractFunctionParameters().AddString("new message") 80 81 call := func(request *services.Transaction) *services.TransactionResponse { 82 require.NotEmpty(t, request.SignedTransactionBytes) 83 signedTransaction := services.SignedTransaction{} 84 _ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction) 85 86 require.NotEmpty(t, signedTransaction.BodyBytes) 87 transactionBody := services.TransactionBody{} 88 _ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody) 89 90 require.NotNil(t, transactionBody.TransactionID) 91 transactionId := transactionBody.TransactionID.String() 92 require.NotEqual(t, "", transactionId) 93 94 sigMap := signedTransaction.GetSigMap() 95 require.NotNil(t, sigMap) 96 97 for _, sigPair := range sigMap.SigPair { 98 verified := false 99 100 switch k := sigPair.Signature.(type) { 101 case *services.SignaturePair_Ed25519: 102 pbTemp, _ := PublicKeyFromBytesEd25519(sigPair.PubKeyPrefix) 103 verified = pbTemp.Verify(signedTransaction.BodyBytes, k.Ed25519) 104 case *services.SignaturePair_ECDSASecp256K1: 105 pbTemp, _ := PublicKeyFromBytesECDSA(sigPair.PubKeyPrefix) 106 verified = pbTemp.Verify(signedTransaction.BodyBytes, k.ECDSASecp256K1) 107 } 108 require.True(t, verified) 109 } 110 111 if bod, ok := transactionBody.Data.(*services.TransactionBody_ContractCall); ok { 112 require.Equal(t, bod.ContractCall.ContractID.GetContractNum(), int64(123)) 113 require.Equal(t, bod.ContractCall.GetGas(), int64(100000)) 114 message := "setMessage" 115 require.Equal(t, bytes.Compare(bod.ContractCall.FunctionParameters, params._Build(&message)), 0) 116 } 117 118 return &services.TransactionResponse{ 119 NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, 120 } 121 } 122 responses := [][]interface{}{{ 123 call, 124 }} 125 126 client, server := NewMockClientAndServer(responses) 127 defer server.Close() 128 129 _, err := NewContractExecuteTransaction(). 130 SetContractID(ContractID{Contract: 123}). 131 SetNodeAccountIDs([]AccountID{{Account: 3}}). 132 SetGas(100000). 133 SetFunction("setMessage", NewContractFunctionParameters().AddString("new message")). 134 Execute(client) 135 require.NoError(t, err) 136 } 137 138 func TestUnitContractExecuteTransactionGet(t *testing.T) { 139 t.Parallel() 140 141 contractID := ContractID{Contract: 7} 142 143 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 144 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 145 146 transaction, err := NewContractExecuteTransaction(). 147 SetTransactionID(transactionID). 148 SetNodeAccountIDs(nodeAccountID). 149 SetContractID(contractID). 150 SetGas(100000). 151 SetFunction("setMessage", NewContractFunctionParameters().AddString("new message")). 152 SetFunctionParameters([]byte{}). 153 SetPayableAmount(NewHbar(1)). 154 SetTransactionMemo(""). 155 SetTransactionValidDuration(60 * time.Second). 156 SetRegenerateTransactionID(false). 157 Freeze() 158 require.NoError(t, err) 159 160 transaction.GetTransactionID() 161 transaction.GetNodeAccountIDs() 162 163 _, err = transaction.GetTransactionHash() 164 require.NoError(t, err) 165 166 transaction.GetContractID() 167 transaction.GetMaxTransactionFee() 168 transaction.GetTransactionMemo() 169 transaction.GetRegenerateTransactionID() 170 _, err = transaction.GetSignatures() 171 require.NoError(t, err) 172 transaction.GetRegenerateTransactionID() 173 transaction.GetMaxTransactionFee() 174 transaction.GetFunctionParameters() 175 transaction.GetGas() 176 transaction.GetRegenerateTransactionID() 177 transaction.GetPayableAmount() 178 } 179 180 func TestUnitContractExecuteTransactionSetNothing(t *testing.T) { 181 t.Parallel() 182 183 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 184 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 185 186 transaction, err := NewContractExecuteTransaction(). 187 SetTransactionID(transactionID). 188 SetNodeAccountIDs(nodeAccountID). 189 Freeze() 190 require.NoError(t, err) 191 192 transaction.GetTransactionID() 193 transaction.GetNodeAccountIDs() 194 195 _, err = transaction.GetTransactionHash() 196 require.NoError(t, err) 197 198 transaction.GetContractID() 199 transaction.GetMaxTransactionFee() 200 transaction.GetTransactionMemo() 201 transaction.GetRegenerateTransactionID() 202 _, err = transaction.GetSignatures() 203 require.NoError(t, err) 204 transaction.GetRegenerateTransactionID() 205 transaction.GetMaxTransactionFee() 206 transaction.GetFunctionParameters() 207 transaction.GetGas() 208 transaction.GetRegenerateTransactionID() 209 transaction.GetPayableAmount() 210 } 211 212 func TestUnitContractExecuteTransactionProtoCheck(t *testing.T) { 213 t.Parallel() 214 215 contractID := ContractID{Contract: 7} 216 217 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 218 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 219 220 transaction, err := NewContractExecuteTransaction(). 221 SetTransactionID(transactionID). 222 SetNodeAccountIDs(nodeAccountID). 223 SetContractID(contractID). 224 SetGas(100000). 225 SetFunctionParameters([]byte{34}). 226 SetPayableAmount(NewHbar(1)). 227 SetTransactionMemo(""). 228 SetTransactionValidDuration(60 * time.Second). 229 SetRegenerateTransactionID(false). 230 Freeze() 231 require.NoError(t, err) 232 233 transaction.GetTransactionID() 234 transaction.GetNodeAccountIDs() 235 236 proto := transaction.build().GetContractCall() 237 require.Equal(t, proto.ContractID.String(), contractID._ToProtobuf().String()) 238 require.Equal(t, proto.Gas, int64(100000)) 239 require.Equal(t, proto.Amount, NewHbar(1).AsTinybar()) 240 require.Equal(t, proto.FunctionParameters, []byte{34}) 241 } 242 243 func TestUnitContractExecuteTransactionCoverage(t *testing.T) { 244 t.Parallel() 245 246 checksum := "dmqui" 247 grpc := time.Second * 30 248 contract := ContractID{Contract: 3, checksum: &checksum} 249 nodeAccountID := []AccountID{{Account: 10}} 250 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 251 252 newKey, err := PrivateKeyGenerateEd25519() 253 require.NoError(t, err) 254 255 client, err := _NewMockClient() 256 client.SetLedgerID(*NewLedgerIDTestnet()) 257 require.NoError(t, err) 258 client.SetAutoValidateChecksums(true) 259 260 transaction, err := NewContractExecuteTransaction(). 261 SetTransactionID(transactionID). 262 SetNodeAccountIDs(nodeAccountID). 263 SetContractID(contract). 264 SetGas(100000). 265 SetFunctionParameters([]byte{34}). 266 SetPayableAmount(NewHbar(1)). 267 SetGrpcDeadline(&grpc). 268 SetMaxTransactionFee(NewHbar(3)). 269 SetMaxRetry(3). 270 SetMaxBackoff(time.Second * 30). 271 SetMinBackoff(time.Second * 10). 272 SetTransactionMemo("no"). 273 SetTransactionValidDuration(time.Second * 30). 274 SetRegenerateTransactionID(false). 275 Freeze() 276 require.NoError(t, err) 277 278 transaction.validateNetworkOnIDs(client) 279 280 _, err = transaction.Schedule() 281 require.NoError(t, err) 282 transaction.GetTransactionID() 283 transaction.GetNodeAccountIDs() 284 transaction.GetMaxRetry() 285 transaction.GetMaxTransactionFee() 286 transaction.GetMaxBackoff() 287 transaction.GetMinBackoff() 288 transaction.GetRegenerateTransactionID() 289 byt, err := transaction.ToBytes() 290 require.NoError(t, err) 291 txFromBytes, err := TransactionFromBytes(byt) 292 require.NoError(t, err) 293 sig, err := newKey.SignTransaction(&transaction.Transaction) 294 require.NoError(t, err) 295 296 _, err = transaction.GetTransactionHash() 297 require.NoError(t, err) 298 transaction.GetMaxTransactionFee() 299 transaction.GetTransactionMemo() 300 transaction.GetRegenerateTransactionID() 301 transaction.GetGas() 302 303 _, err = transaction.GetSignatures() 304 require.NoError(t, err) 305 transaction.getName() 306 switch b := txFromBytes.(type) { 307 case ContractExecuteTransaction: 308 b.AddSignature(newKey.PublicKey(), sig) 309 } 310 }