From c2f643fcb32a5c3018533e9763072d7c7965168b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Negovanovi=C4=87?= <93934272+Stefan-Ethernal@users.noreply.github.com> Date: Fri, 26 May 2023 17:26:59 +0200 Subject: [PATCH] Fix `eth_getStorageAt` always returning 0 (#1558) * fix eth_getStorageAt always returning 0 (#1546) * Fix failing unit tests --------- Co-authored-by: typestring <23285275+typestring@users.noreply.github.com> --- .../test-contracts/TestSimple.json | 45 ++++++++++++------- .../test-contracts/TestSimple.sol | 4 ++ e2e-polybft/e2e/jsonrpc_test.go | 21 ++++++++- jsonrpc/eth_endpoint.go | 20 +-------- jsonrpc/eth_state_test.go | 6 +-- 5 files changed, 55 insertions(+), 41 deletions(-) diff --git a/consensus/polybft/contractsapi/test-contracts/TestSimple.json b/consensus/polybft/contractsapi/test-contracts/TestSimple.json index 3e99f69810..2772c448ee 100644 --- a/consensus/polybft/contractsapi/test-contracts/TestSimple.json +++ b/consensus/polybft/contractsapi/test-contracts/TestSimple.json @@ -3,22 +3,37 @@ "contractName": "TestSimple", "sourceName": "contracts/TestSimple.sol", "abi": [ - { - "inputs": [], - "name": "getValue", - "outputs": [ - { - "internalType": "uint256", - "name": "", - "type": "uint256" - } - ], - "stateMutability": "view", - "type": "function" - } + { + "inputs": [], + "name": "getValue", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_val", + "type": "uint256" + } + ], + "name": "setValue", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + } ], - "bytecode": "0x608060405234801561001057600080fd5b5060b68061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80632096525514602d575b600080fd5b60336047565b604051603e91906067565b60405180910390f35b60008054905090565b6000819050919050565b6061816050565b82525050565b6000602082019050607a6000830184605a565b9291505056fea26469706673582212203adde2f97bef70ed52dd170d2965805f1ed0cd3eaa229edc27180041091175ac64736f6c63430008110033", - "deployedBytecode": "0x6080604052348015600f57600080fd5b506004361060285760003560e01c80632096525514602d575b600080fd5b60336047565b604051603e91906067565b60405180910390f35b60008054905090565b6000819050919050565b6061816050565b82525050565b6000602082019050607a6000830184605a565b9291505056fea26469706673582212203adde2f97bef70ed52dd170d2965805f1ed0cd3eaa229edc27180041091175ac64736f6c63430008110033", + "bytecode": "0x608060405234801561001057600080fd5b50610150806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063209652551461003b5780635524107714610059575b600080fd5b610043610075565b60405161005091906100a1565b60405180910390f35b610073600480360381019061006e91906100ed565b61007e565b005b60008054905090565b8060008190555050565b6000819050919050565b61009b81610088565b82525050565b60006020820190506100b66000830184610092565b92915050565b600080fd5b6100ca81610088565b81146100d557600080fd5b50565b6000813590506100e7816100c1565b92915050565b600060208284031215610103576101026100bc565b5b6000610111848285016100d8565b9150509291505056fea2646970667358221220470fde08a8031bec841c7cc72e8ff6af3fefb578f1a87c4d89d5fb45f57a532464736f6c63430008110033", + "deployedBytecode": "0x608060405234801561001057600080fd5b50600436106100365760003560e01c8063209652551461003b5780635524107714610059575b600080fd5b610043610075565b60405161005091906100a1565b60405180910390f35b610073600480360381019061006e91906100ed565b61007e565b005b60008054905090565b8060008190555050565b6000819050919050565b61009b81610088565b82525050565b60006020820190506100b66000830184610092565b92915050565b600080fd5b6100ca81610088565b81146100d557600080fd5b50565b6000813590506100e7816100c1565b92915050565b600060208284031215610103576101026100bc565b5b6000610111848285016100d8565b9150509291505056fea2646970667358221220470fde08a8031bec841c7cc72e8ff6af3fefb578f1a87c4d89d5fb45f57a532464736f6c63430008110033", "linkReferences": {}, "deployedLinkReferences": {} } + + diff --git a/consensus/polybft/contractsapi/test-contracts/TestSimple.sol b/consensus/polybft/contractsapi/test-contracts/TestSimple.sol index 6d62fdf7f8..9366467e74 100644 --- a/consensus/polybft/contractsapi/test-contracts/TestSimple.sol +++ b/consensus/polybft/contractsapi/test-contracts/TestSimple.sol @@ -7,4 +7,8 @@ contract TestSimple { function getValue() public view returns (uint256) { return val; } + + function setValue(uint256 _val) public { + val = _val; + } } diff --git a/e2e-polybft/e2e/jsonrpc_test.go b/e2e-polybft/e2e/jsonrpc_test.go index 98e411fe00..9dc68fa1cd 100644 --- a/e2e-polybft/e2e/jsonrpc_test.go +++ b/e2e-polybft/e2e/jsonrpc_test.go @@ -145,7 +145,7 @@ func TestE2E_JsonRPC(t *testing.T) { require.NoError(t, err) }) - t.Run("eth_getStorage", func(t *testing.T) { + t.Run("eth_getStorageAt", func(t *testing.T) { key1, err := wallet.GenerateKey() require.NoError(t, err) @@ -157,9 +157,26 @@ func TestE2E_JsonRPC(t *testing.T) { require.NoError(t, txn.Wait()) require.True(t, txn.Succeed()) - resp, err := client.GetStorageAt(txn.Receipt().ContractAddress, ethgo.Hash{}, ethgo.Latest) + target := txn.Receipt().ContractAddress + + resp, err := client.GetStorageAt(target, ethgo.Hash{}, ethgo.Latest) require.NoError(t, err) require.Equal(t, "0x0000000000000000000000000000000000000000000000000000000000000000", resp.String()) + + setValueFn := abi.MustNewMethod("function setValue(uint256 _val) public") + + newVal := big.NewInt(1) + + input, err := setValueFn.Encode([]interface{}{newVal}) + require.NoError(t, err) + + txn = cluster.SendTxn(t, acct, ðgo.Transaction{Input: input, To: &target}) + require.NoError(t, txn.Wait()) + require.True(t, txn.Succeed()) + + resp, err = client.GetStorageAt(target, ethgo.Hash{}, ethgo.Latest) + require.NoError(t, err) + require.Equal(t, "0x0000000000000000000000000000000000000000000000000000000000000001", resp.String()) }) t.Run("eth_getCode", func(t *testing.T) { diff --git a/jsonrpc/eth_endpoint.go b/jsonrpc/eth_endpoint.go index 7adac86791..0f7d07bdde 100644 --- a/jsonrpc/eth_endpoint.go +++ b/jsonrpc/eth_endpoint.go @@ -6,7 +6,6 @@ import ( "math/big" "github.com/hashicorp/go-hclog" - "github.com/umbracle/fastrlp" "github.com/0xPolygon/polygon-edge/chain" "github.com/0xPolygon/polygon-edge/helper/common" @@ -389,24 +388,7 @@ func (e *Eth) GetStorageAt( return nil, err } - //nolint:godox - // TODO: GetStorage should return the values already parsed (to be fixed in EVM-522) - - // Parse the RLP value - p := &fastrlp.Parser{} - - v, err := p.Parse(result) - if err != nil { - return argBytesPtr(types.ZeroHash[:]), nil - } - - data, err := v.Bytes() - if err != nil { - return argBytesPtr(types.ZeroHash[:]), nil - } - - // Pad to return 32 bytes data - return argBytesPtr(types.BytesToHash(data).Bytes()), nil + return argBytesPtr(result), nil } // GasPrice returns the average gas price based on the last x blocks diff --git a/jsonrpc/eth_state_test.go b/jsonrpc/eth_state_test.go index 5f18b7ede1..02b2a9a86b 100644 --- a/jsonrpc/eth_state_test.go +++ b/jsonrpc/eth_state_test.go @@ -11,7 +11,6 @@ import ( "github.com/0xPolygon/polygon-edge/state/runtime" "github.com/0xPolygon/polygon-edge/types" "github.com/stretchr/testify/assert" - "github.com/umbracle/fastrlp" ) var ( @@ -555,10 +554,7 @@ func TestEth_State_GetStorageAt(t *testing.T) { } account := store.account for index, data := range storage { - a := &fastrlp.Arena{} - value := a.NewBytes(data.Bytes()) - newData := value.MarshalTo(nil) - account.Storage(index, newData) + account.Storage(index, data.Bytes()) } }