fix(rpc): different result from eth_getProof comparing with Ethereum (#1431)

* align with eth_getProof

for more info, see https://eips.ethereum.org/EIPS/eip-1186

* add GetHexProofs

* add change doc

* keep default res

* fix lint

* add e2e test

* Apply suggestions from code review

* fix lint

* nix run -f ./nix gomod2nix
This commit is contained in:
mmsqe
2022-11-06 09:27:33 +01:00
committed by GitHub
parent 708e781a04
commit 41425fc6d4
7 changed files with 114 additions and 36 deletions
+2 -14
View File
@@ -75,16 +75,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),
}
}
@@ -105,12 +99,6 @@ 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")
@@ -118,7 +106,7 @@ func (b *Backend) GetProof(address common.Address, storageKeys []string, blockNr
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),
+18
View File
@@ -21,6 +21,7 @@ import (
"github.com/evmos/ethermint/rpc/types"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/tendermint/tendermint/proto/tendermint/crypto"
)
type txGasAndReward struct {
@@ -263,3 +264,20 @@ func GetLogsFromBlockResults(blockRes *tmrpctypes.ResultBlockResults) ([][]*etht
return blockLogs, nil
}
// GetHexProofs returns list of hex data of proof op
func GetHexProofs(proof *crypto.ProofOps) []string {
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
}
+52
View File
@@ -0,0 +1,52 @@
package backend
import (
"fmt"
"github.com/tendermint/tendermint/proto/tendermint/crypto"
)
func mookProofs(num int, withData bool) *crypto.ProofOps {
var proofOps *crypto.ProofOps
if num > 0 {
proofOps = new(crypto.ProofOps)
for i := 0; i < num; i++ {
proof := crypto.ProofOp{}
if withData {
proof.Data = []byte("\n\031\n\003KEY\022\005VALUE\032\013\010\001\030\001 \001*\003\000\002\002")
}
proofOps.Ops = append(proofOps.Ops, 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{"0x0a190a034b4559120556414c55451a0b0801180120012a03000202"},
},
}
for _, tc := range testCases {
suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
suite.Require().Equal(tc.exp, GetHexProofs(tc.proof))
})
}
}