Skip to content

Commit

Permalink
Fix eth_getStorageAt always returning 0 (0xPolygon#1558)
Browse files Browse the repository at this point in the history
* fix eth_getStorageAt always returning 0 (0xPolygon#1546)

* Fix failing unit tests

---------

Co-authored-by: typestring <23285275+typestring@users.noreply.github.com>
  • Loading branch information
Stefan-Ethernal and typestring authored May 26, 2023
1 parent 03a5352 commit c2f643f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 41 deletions.
45 changes: 30 additions & 15 deletions consensus/polybft/contractsapi/test-contracts/TestSimple.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {}
}


4 changes: 4 additions & 0 deletions consensus/polybft/contractsapi/test-contracts/TestSimple.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ contract TestSimple {
function getValue() public view returns (uint256) {
return val;
}

function setValue(uint256 _val) public {
val = _val;
}
}
21 changes: 19 additions & 2 deletions e2e-polybft/e2e/jsonrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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, &ethgo.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) {
Expand Down
20 changes: 1 addition & 19 deletions jsonrpc/eth_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions jsonrpc/eth_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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())
}
}

Expand Down

0 comments on commit c2f643f

Please sign in to comment.