github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/contract_bytecode_query_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  
    33  	"github.com/stretchr/testify/assert"
    34  
    35  	"github.com/stretchr/testify/require"
    36  )
    37  
    38  func TestUnitContractBytecodeQueryValidate(t *testing.T) {
    39  	t.Parallel()
    40  
    41  	client, err := _NewMockClient()
    42  	client.SetLedgerID(*NewLedgerIDTestnet())
    43  	require.NoError(t, err)
    44  	client.SetAutoValidateChecksums(true)
    45  	contractID, err := ContractIDFromString("0.0.123-esxsf")
    46  	require.NoError(t, err)
    47  
    48  	contractBytecode := NewContractBytecodeQuery().
    49  		SetContractID(contractID)
    50  
    51  	err = contractBytecode.validateNetworkOnIDs(client)
    52  	require.NoError(t, err)
    53  }
    54  
    55  func TestUnitContractBytecodeQueryValidateWrong(t *testing.T) {
    56  	t.Parallel()
    57  
    58  	client, err := _NewMockClient()
    59  	client.SetLedgerID(*NewLedgerIDTestnet())
    60  	require.NoError(t, err)
    61  	client.SetAutoValidateChecksums(true)
    62  	contractID, err := ContractIDFromString("0.0.123-rmkykd")
    63  	require.NoError(t, err)
    64  
    65  	contractBytecode := NewContractBytecodeQuery().
    66  		SetContractID(contractID)
    67  
    68  	err = contractBytecode.validateNetworkOnIDs(client)
    69  	assert.Error(t, err)
    70  	if err != nil {
    71  		assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error())
    72  	}
    73  }
    74  
    75  func TestUnitContractBytecodeQueryMock(t *testing.T) {
    76  	t.Parallel()
    77  
    78  	smartContractBytecode := []byte("608060405234801561001057600080fd5b506040516104d73803806104d78339818101604052602081101561003357600080fd5b810190808051604051939291908464010000000082111561005357600080fd5b90830190602082018581111561006857600080fd5b825164010000000081118282018810171561008257600080fd5b82525081516020918201929091019080838360005b838110156100af578181015183820152602001610097565b50505050905090810190601f1680156100dc5780820380516001836020036101000a031916815260200191505b506040525050600080546001600160a01b0319163317905550805161010890600190602084019061010f565b50506101aa565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061015057805160ff191683800117855561017d565b8280016001018555821561017d579182015b8281111561017d578251825591602001919060010190610162565b5061018992915061018d565b5090565b6101a791905b808211156101895760008155600101610193565b90565b61031e806101b96000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c8063368b87721461004657806341c0e1b5146100ee578063ce6d41de146100f6575b600080fd5b6100ec6004803603602081101561005c57600080fd5b81019060208101813564010000000081111561007757600080fd5b82018360208201111561008957600080fd5b803590602001918460018302840111640100000000831117156100ab57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550610173945050505050565b005b6100ec6101a2565b6100fe6101ba565b6040805160208082528351818301528351919283929083019185019080838360005b83811015610138578181015183820152602001610120565b50505050905090810190601f1680156101655780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000546001600160a01b0316331461018a5761019f565b805161019d906001906020840190610250565b505b50565b6000546001600160a01b03163314156101b85733ff5b565b60018054604080516020601f600260001961010087891615020190951694909404938401819004810282018101909252828152606093909290918301828280156102455780601f1061021a57610100808354040283529160200191610245565b820191906000526020600020905b81548152906001019060200180831161022857829003601f168201915b505050505090505b90565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061029157805160ff19168380011785556102be565b828001600101855582156102be579182015b828111156102be5782518255916020019190600101906102a3565b506102ca9291506102ce565b5090565b61024d91905b808211156102ca57600081556001016102d456fea264697066735822122084964d4c3f6bc912a9d20e14e449721012d625aa3c8a12de41ae5519752fc89064736f6c63430006000033")
    79  	responses := [][]interface{}{{
    80  		&services.Response{
    81  			Response: &services.Response_ContractGetBytecodeResponse{
    82  				ContractGetBytecodeResponse: &services.ContractGetBytecodeResponse{
    83  					Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_COST_ANSWER, Cost: 2},
    84  				},
    85  			},
    86  		},
    87  		&services.Response{
    88  			Response: &services.Response_ContractGetBytecodeResponse{
    89  				ContractGetBytecodeResponse: &services.ContractGetBytecodeResponse{
    90  					Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_COST_ANSWER, Cost: 2},
    91  				},
    92  			},
    93  		},
    94  		&services.Response{
    95  			Response: &services.Response_ContractGetBytecodeResponse{
    96  				ContractGetBytecodeResponse: &services.ContractGetBytecodeResponse{
    97  					Header:   &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_ANSWER_ONLY, Cost: 2},
    98  					Bytecode: smartContractBytecode,
    99  				},
   100  			},
   101  		},
   102  	}}
   103  
   104  	client, server := NewMockClientAndServer(responses)
   105  	defer server.Close()
   106  
   107  	query := NewContractBytecodeQuery().
   108  		SetNodeAccountIDs([]AccountID{{Account: 3}}).
   109  		SetContractID(ContractID{Contract: 3}).
   110  		SetMaxQueryPayment(NewHbar(2))
   111  
   112  	_, err := query.GetCost(client)
   113  	require.NoError(t, err)
   114  	bytecode, err := query.Execute(client)
   115  	require.NoError(t, err)
   116  
   117  	require.Equal(t, bytes.Compare(bytecode, smartContractBytecode), 0)
   118  }
   119  
   120  func TestUnitContractBytecodeQueryGet(t *testing.T) {
   121  	t.Parallel()
   122  
   123  	contractID := ContractID{Contract: 7}
   124  
   125  	balance := NewContractBytecodeQuery().
   126  		SetContractID(contractID).
   127  		SetQueryPayment(NewHbar(2)).
   128  		SetMaxQueryPayment(NewHbar(10)).
   129  		SetNodeAccountIDs([]AccountID{{Account: 10}, {Account: 11}, {Account: 12}})
   130  
   131  	balance.GetContractID()
   132  	balance.GetNodeAccountIDs()
   133  	balance.GetMinBackoff()
   134  	balance.GetMaxBackoff()
   135  	balance.GetMaxRetryCount()
   136  	balance.GetPaymentTransactionID()
   137  	balance.GetQueryPayment()
   138  	balance.GetMaxQueryPayment()
   139  }
   140  
   141  func TestUnitContractBytecodeQuerySetNothing(t *testing.T) {
   142  	t.Parallel()
   143  
   144  	balance := NewContractBytecodeQuery()
   145  
   146  	balance.GetContractID()
   147  	balance.GetNodeAccountIDs()
   148  	balance.GetMinBackoff()
   149  	balance.GetMaxBackoff()
   150  	balance.GetMaxRetryCount()
   151  	balance.GetPaymentTransactionID()
   152  	balance.GetQueryPayment()
   153  	balance.GetMaxQueryPayment()
   154  }
   155  
   156  func TestUnitContractBytecodeQueryCoverage(t *testing.T) {
   157  	t.Parallel()
   158  
   159  	checksum := "dmqui"
   160  	grpc := time.Second * 3
   161  	contract := ContractID{Contract: 3, checksum: &checksum}
   162  	nodeAccountID := []AccountID{{Account: 10}}
   163  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   164  
   165  	client, err := _NewMockClient()
   166  	client.SetLedgerID(*NewLedgerIDTestnet())
   167  	require.NoError(t, err)
   168  	client.SetAutoValidateChecksums(true)
   169  
   170  	query := NewContractBytecodeQuery().
   171  		SetMaxRetry(3).
   172  		SetMaxBackoff(time.Second * 30).
   173  		SetMinBackoff(time.Second * 10).
   174  		SetContractID(contract).
   175  		SetNodeAccountIDs(nodeAccountID).
   176  		SetPaymentTransactionID(transactionID).
   177  		SetMaxQueryPayment(NewHbar(23)).
   178  		SetQueryPayment(NewHbar(3)).
   179  		SetGrpcDeadline(&grpc)
   180  
   181  	err = query.validateNetworkOnIDs(client)
   182  	require.NoError(t, err)
   183  	query.GetNodeAccountIDs()
   184  	query.GetMaxBackoff()
   185  	query.GetMinBackoff()
   186  	query.getName()
   187  	query.GetContractID()
   188  }