From 9769a9628b4db32cef9a781b2efae8421cb12897 Mon Sep 17 00:00:00 2001 From: Emanuel Pargov Date: Wed, 21 Jun 2023 16:24:05 +0300 Subject: [PATCH] Feat: Add hip 729 support Signed-off-by: Emanuel Pargov --- contract_function_parameters_e2e_test.go | 32 +++++++++++++++--- contract_function_result.go | 10 ++++++ contract_nonce_info.go | 43 ++++++++++++++++++++++++ contract_nonce_info_unit_test.go | 31 +++++++++++++++++ go.mod | 2 +- go.sum | 2 ++ 6 files changed, 115 insertions(+), 5 deletions(-) create mode 100644 contract_nonce_info.go create mode 100644 contract_nonce_info_unit_test.go diff --git a/contract_function_parameters_e2e_test.go b/contract_function_parameters_e2e_test.go index f4f38ce28..c86cd5af2 100644 --- a/contract_function_parameters_e2e_test.go +++ b/contract_function_parameters_e2e_test.go @@ -690,7 +690,7 @@ func TestUint160Max(t *testing.T) { t.Parallel() env := NewIntegrationTestEnv(t) deployContract(env) - intType(t, env, "uint168", "1461501637330902918203684832716283019655932542975") + intType(t, env, "uint160", "1461501637330902918203684832716283019655932542975") err := CloseIntegrationTestEnv(env, nil) require.NoError(t, err) } @@ -738,7 +738,7 @@ func TestUint184Max(t *testing.T) { t.Parallel() env := NewIntegrationTestEnv(t) deployContract(env) - intType(t, env, "uint192", "24519928653854221733733552434404946937899825954937634815") + intType(t, env, "uint184", "24519928653854221733733552434404946937899825954937634815") err := CloseIntegrationTestEnv(env, nil) require.NoError(t, err) } @@ -794,7 +794,7 @@ func TestUint216Min(t *testing.T) { t.Parallel() env := NewIntegrationTestEnv(t) deployContract(env) - intType(t, env, "uint224", "0") + intType(t, env, "uint216", "0") err := CloseIntegrationTestEnv(env, nil) require.NoError(t, err) } @@ -802,7 +802,7 @@ func TestUint216Max(t *testing.T) { t.Parallel() env := NewIntegrationTestEnv(t) deployContract(env) - intType(t, env, "uint224", "105312291668557186697918027683670432318895095400549111254310977535") + intType(t, env, "uint216", "105312291668557186697918027683670432318895095400549111254310977535") err := CloseIntegrationTestEnv(env, nil) require.NoError(t, err) } @@ -1781,3 +1781,27 @@ func TestBytes32Array(t *testing.T) { err = CloseIntegrationTestEnv(env, nil) require.NoError(t, err) } + +func TestContractNonces(t *testing.T){ + t.Parallel() + env := NewIntegrationTestEnv(t) + bytecode := []byte(`6080604052348015600f57600080fd5b50604051601a90603b565b604051809103906000f0801580156035573d6000803e3d6000fd5b50506047565b605c8061009483390190565b603f806100556000396000f3fe6080604052600080fdfea2646970667358221220a20122cbad3457fedcc0600363d6e895f17048f5caa4afdab9e655123737567d64736f6c634300081200336080604052348015600f57600080fd5b50603f80601d6000396000f3fe6080604052600080fdfea264697066735822122053dfd8835e3dc6fedfb8b4806460b9b7163f8a7248bac510c6d6808d9da9d6d364736f6c63430008120033`) + fileCreate, err := NewFileCreateTransaction(). + SetKeys(env.OperatorKey.PublicKey()).SetContents(bytecode). + Execute(env.Client) + require.NoError(t, err) + fileCreate.SetValidateStatus(true) + receipt, err := fileCreate.GetReceipt(env.Client) + require.NoError(t, err) + require.Equal(t, StatusSuccess, receipt.Status) + contractCreate, err := NewContractCreateTransaction(). + SetBytecodeFileID(*receipt.FileID). + SetGas(10000000).Execute(env.Client) + require.NoError(t, err) + contractCreate.SetValidateStatus(true) + record,err:=contractCreate.GetRecord(env.Client) + require.NoError(t, err) + require.Equal(t, StatusSuccess, record.Receipt.Status) + require.Equal(t, int64(2), record.CallResult.ContractNonces[0].Nonce) + require.Equal(t, int64(1), record.CallResult.ContractNonces[1].Nonce) +} \ No newline at end of file diff --git a/contract_function_result.go b/contract_function_result.go index c14d89e5c..9976abcee 100644 --- a/contract_function_result.go +++ b/contract_function_result.go @@ -65,6 +65,7 @@ type ContractFunctionResult struct { GasAvailable int64 Amount Hbar FunctionParameters []byte + ContractNonces []*ContractNonceInfo } // GetBool gets a _Solidity bool from the result at the given index @@ -474,6 +475,14 @@ func _ContractFunctionResultFromProtobuf(pb *services.ContractFunctionResult) Co } } + var nonces []*ContractNonceInfo + if len(pb.ContractNonces) > 0 { + nonces = make([]*ContractNonceInfo, len(pb.ContractNonces)) + for i, nonce := range pb.ContractNonces { + nonces[i] = _ContractNonceInfoFromProtobuf(nonce) + } + } + result := ContractFunctionResult{ ContractCallResult: pb.ContractCallResult, ErrorMessage: pb.ErrorMessage, @@ -485,6 +494,7 @@ func _ContractFunctionResultFromProtobuf(pb *services.ContractFunctionResult) Co GasAvailable: pb.Gas, Amount: HbarFromTinybar(pb.Amount), FunctionParameters: pb.FunctionParameters, + ContractNonces: nonces, } if pb.ContractID != nil { diff --git a/contract_nonce_info.go b/contract_nonce_info.go new file mode 100644 index 000000000..ce4eadc75 --- /dev/null +++ b/contract_nonce_info.go @@ -0,0 +1,43 @@ +package hedera + +/*- + * + * Hedera Go SDK + * + * Copyright (C) 2020 - 2022 Hedera Hashgraph, LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +import ( + "github.com/hashgraph/hedera-protobufs-go/services" +) + +// ContractID is the ID for a Hedera smart contract +type ContractNonceInfo struct { + ContractID *ContractID + Nonce int64 +} + +func _ContractNonceInfoFromProtobuf(contractNonceInfo *services.ContractNonceInfo) *ContractNonceInfo { + if contractNonceInfo == nil { + return nil + } + + result := ContractNonceInfo{ + ContractID: _ContractIDFromProtobuf(contractNonceInfo.GetContractId()), + Nonce: contractNonceInfo.GetNonce(), + } + return &result +} diff --git a/contract_nonce_info_unit_test.go b/contract_nonce_info_unit_test.go new file mode 100644 index 000000000..2f39f2a98 --- /dev/null +++ b/contract_nonce_info_unit_test.go @@ -0,0 +1,31 @@ +//go:build all || unit +// +build all unit + +package hedera + +import ( + "testing" + + "github.com/hashgraph/hedera-protobufs-go/services" + "github.com/stretchr/testify/assert" +) + +func TestContractNonceInfoFromProtobuf(t *testing.T) { + contractID := &ContractID{Shard: 0, Realm: 0, Contract: 123} + nonce := int64(456) + protobuf := &services.ContractNonceInfo{ + ContractId: contractID._ToProtobuf(), + Nonce: nonce, + } + + result := _ContractNonceInfoFromProtobuf(protobuf) + + assert.Equal(t, contractID, result.ContractID) + assert.Equal(t, nonce, result.Nonce) +} + +func TestContractNonceInfoFromProtobuf_NilInput(t *testing.T) { + result := _ContractNonceInfoFromProtobuf(nil) + + assert.Nil(t, result) +} \ No newline at end of file diff --git a/go.mod b/go.mod index 2c45e0d00..27f5cc37a 100644 --- a/go.mod +++ b/go.mod @@ -7,8 +7,8 @@ require ( github.com/cenkalti/backoff/v4 v4.2.1 github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect github.com/ethereum/go-ethereum v1.12.0 - github.com/hashgraph/hedera-protobufs-go v0.2.1-0.20230222133122-6549ac627de9 github.com/holiman/uint256 v1.2.3 // indirect + github.com/hashgraph/hedera-protobufs-go v0.2.1-0.20230620121835-2f2656b1f434 github.com/mattn/go-isatty v0.0.19 // indirect github.com/pkg/errors v0.9.1 github.com/rs/zerolog v1.29.1 diff --git a/go.sum b/go.sum index 96e5e8472..4c5818e65 100644 --- a/go.sum +++ b/go.sum @@ -1174,6 +1174,8 @@ github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0/go.mod h1:hgWBS7lorOAVIJEQMi4Zs github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3/go.mod h1:o//XUCC/F+yRGJoPO/VU0GSB0f8Nhgmxx0VIRUvaC0w= github.com/hashgraph/hedera-protobufs-go v0.2.1-0.20230222133122-6549ac627de9 h1:FcrKvjugTHGz5wZxdx6R/43EJwBDyal4paS+el+BXxc= github.com/hashgraph/hedera-protobufs-go v0.2.1-0.20230222133122-6549ac627de9/go.mod h1:av0VF39ClcbPoUcDfMWnocYdjyPXKJJYB30h51fuuyg= +github.com/hashgraph/hedera-protobufs-go v0.2.1-0.20230620121835-2f2656b1f434 h1:zrc6tknD5Ry2lmED1mScl2THVMuLOAcRiUzl5v0qx+k= +github.com/hashgraph/hedera-protobufs-go v0.2.1-0.20230620121835-2f2656b1f434/go.mod h1:av0VF39ClcbPoUcDfMWnocYdjyPXKJJYB30h51fuuyg= github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q= github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=