Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

fix: different result from eth_getProof comparing with Ethereum #1431

Merged
merged 9 commits into from
Nov 6, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (cli) [#1362](https://github.com/evmos/ethermint/pull/1362) Fix `index-eth-tx` error when the indexer db is empty.
* (state) [#1320](https://github.com/evmos/ethermint/pull/1320) Fix codehash check mismatch when the code has been deleted in the evm state.
* (rpc) [#1392](https://github.com/evmos/ethermint/pull/1392) Allow fill the proposer address in json-rpc through tendermint api, and pass explicitly to grpc query handler.
* (rpc) [#1431](https://github.com/evmos/ethermint/pull/1431) Align hex-strings proof fields in `eth_getProof` as Ethereum.

## [v0.19.3] - 2022-10-14

Expand Down
34 changes: 20 additions & 14 deletions rpc/backend/account_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
rpctypes "github.com/evmos/ethermint/rpc/types"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/pkg/errors"
"github.com/tendermint/tendermint/proto/tendermint/crypto"
)

// GetCode returns the contract code at the given address and block number.
Expand All @@ -34,6 +35,23 @@ func (b *Backend) GetCode(address common.Address, blockNrOrHash rpctypes.BlockNu
return res.Code, nil
}

// GetHexProofs returns list of hex data of proof op
func GetHexProofs(proof *crypto.ProofOps) []string {
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
if proof == nil {
return []string{""}
}
proofs := []string{}
// check for proof
for _, p := range proof.Ops {
proof := ""
if len(p.Data) > 0 {
proof = hexutil.Encode(p.Data)
}
proofs = append(proofs, proof)
}
return proofs
}

// GetProof returns an account object with proof and any storage proofs
func (b *Backend) GetProof(address common.Address, storageKeys []string, blockNrOrHash rpctypes.BlockNumberOrHash) (*rpctypes.AccountResult, error) {
blockNum, err := b.BlockNumberFromTendermint(blockNrOrHash)
Expand Down Expand Up @@ -75,16 +93,10 @@ func (b *Backend) GetProof(address common.Address, storageKeys []string, blockNr
return nil, err
}

// check for proof
var proofStr string
if proof != nil {
proofStr = proof.String()
}

storageProofs[i] = rpctypes.StorageResult{
Key: key,
Value: (*hexutil.Big)(new(big.Int).SetBytes(valueBz)),
Proof: []string{proofStr},
Proof: GetHexProofs(proof),
}
}

Expand All @@ -105,20 +117,14 @@ func (b *Backend) GetProof(address common.Address, storageKeys []string, blockNr
return nil, err
}

// check for proof
var accProofStr string
if proof != nil {
accProofStr = proof.String()
}

balance, ok := sdkmath.NewIntFromString(res.Balance)
if !ok {
return nil, errors.New("invalid balance")
}

return &rpctypes.AccountResult{
Address: address,
AccountProof: []string{accProofStr},
AccountProof: GetHexProofs(proof),
Balance: (*hexutil.Big)(balance.BigInt()),
CodeHash: common.HexToHash(res.CodeHash),
Nonce: hexutil.Uint64(res.Nonce),
Expand Down
50 changes: 50 additions & 0 deletions rpc/backend/account_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
rpctypes "github.com/evmos/ethermint/rpc/types"
"github.com/evmos/ethermint/tests"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/tendermint/tendermint/proto/tendermint/crypto"
)

func (suite *BackendTestSuite) TestGetCode() {
Expand Down Expand Up @@ -373,3 +374,52 @@ func (suite *BackendTestSuite) TestGetTransactionCount() {
})
}
}

func mookProofs(num int, withData bool) *crypto.ProofOps {
var proofOps *crypto.ProofOps
if num > 0 {
proofOps = &crypto.ProofOps{
Ops: make([]crypto.ProofOp, num),
}
for i := 0; i < num; i++ {
proof := crypto.ProofOp{
Key: []byte{1, 2, 3},
}
if withData {
proof.Data = []byte{4, 5, 6}
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
}
proofOps.Ops[i] = proof
}
}
return proofOps
}

func (suite *BackendTestSuite) TestGetHexProofs() {
defaultRes := []string{""}
testCases := []struct {
name string
proof *crypto.ProofOps
exp []string
}{
{
"no proof provided",
mookProofs(0, false),
defaultRes,
},
{
"no proof data provided",
mookProofs(1, false),
defaultRes,
},
{
"valid proof provided",
mookProofs(1, true),
[]string{"0x040506"},
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
},
}
for _, tc := range testCases {
suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
suite.Require().Equal(tc.exp, GetHexProofs(tc.proof))
})
}
}