Merge pull request #9955 from filecoin-project/itest/eth-block

itests: Eth JSON-RPC: EthGetBlockByHash and EthGetBlockByNumber
This commit is contained in:
ychiao 2023-01-08 17:31:03 +09:00 committed by GitHub
commit 402173ea16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 72 additions and 12 deletions

View File

@ -387,10 +387,10 @@ func init() {
addExample(&uuid.UUID{})
filterid, _ := ethtypes.EthHashFromHex("0x5CbEeC012345673f25E309Cc264f240bb0664031")
filterid, _ := ethtypes.NewEthHashFromHex("0x5CbEeC012345673f25E309Cc264f240bb0664031")
addExample(ethtypes.EthFilterID(filterid))
subid, _ := ethtypes.EthHashFromHex("0x5CbEeCF99d3fDB301234567c264f240bb0664031")
subid, _ := ethtypes.NewEthHashFromHex("0x5CbEeCF99d3fDB301234567c264f240bb0664031")
addExample(ethtypes.EthSubscriptionID(subid))
pstring := func(s string) *string { return &s }

View File

@ -342,7 +342,7 @@ func (h *EthHash) UnmarshalJSON(b []byte) error {
if err := json.Unmarshal(b, &s); err != nil {
return err
}
hash, err := EthHashFromHex(s)
hash, err := NewEthHashFromHex(s)
if err != nil {
return err
}
@ -373,10 +373,10 @@ func decodeHexString(s string, length int) ([]byte, error) {
}
func NewEthHashFromCid(c cid.Cid) (EthHash, error) {
return EthHashFromHex(c.Hash().HexString()[8:])
return NewEthHashFromHex(c.Hash().HexString()[8:])
}
func EthHashFromHex(s string) (EthHash, error) {
func NewEthHashFromHex(s string) (EthHash, error) {
handlePrefix(&s)
b, err := decodeHexString(s, EthHashLength)
if err != nil {

View File

@ -158,10 +158,10 @@ func TestUnmarshalEthBytes(t *testing.T) {
}
func TestEthFilterResultMarshalJSON(t *testing.T) {
hash1, err := EthHashFromHex("013dbb9442ca9667baccc6230fcd5c1c4b2d4d2870f4bd20681d4d47cfd15184")
hash1, err := NewEthHashFromHex("013dbb9442ca9667baccc6230fcd5c1c4b2d4d2870f4bd20681d4d47cfd15184")
require.NoError(t, err, "eth hash")
hash2, err := EthHashFromHex("ab8653edf9f51785664a643b47605a7ba3d917b5339a0724e7642c114d0e4738")
hash2, err := NewEthHashFromHex("ab8653edf9f51785664a643b47605a7ba3d917b5339a0724e7642c114d0e4738")
require.NoError(t, err, "eth hash")
addr, err := EthAddressFromHex("d4c5fb16488Aa48081296299d54b0c648C9333dA")
@ -223,10 +223,10 @@ func TestEthFilterResultMarshalJSON(t *testing.T) {
}
func TestEthFilterSpecUnmarshalJSON(t *testing.T) {
hash1, err := EthHashFromHex("013dbb9442ca9667baccc6230fcd5c1c4b2d4d2870f4bd20681d4d47cfd15184")
hash1, err := NewEthHashFromHex("013dbb9442ca9667baccc6230fcd5c1c4b2d4d2870f4bd20681d4d47cfd15184")
require.NoError(t, err, "eth hash")
hash2, err := EthHashFromHex("ab8653edf9f51785664a643b47605a7ba3d917b5339a0724e7642c114d0e4738")
hash2, err := NewEthHashFromHex("ab8653edf9f51785664a643b47605a7ba3d917b5339a0724e7642c114d0e4738")
require.NoError(t, err, "eth hash")
addr, err := EthAddressFromHex("d4c5fb16488Aa48081296299d54b0c648C9333dA")
@ -348,10 +348,10 @@ func TestEthAddressListUnmarshalJSON(t *testing.T) {
}
func TestEthHashListUnmarshalJSON(t *testing.T) {
hash1, err := EthHashFromHex("013dbb9442ca9667baccc6230fcd5c1c4b2d4d2870f4bd20681d4d47cfd15184")
hash1, err := NewEthHashFromHex("013dbb9442ca9667baccc6230fcd5c1c4b2d4d2870f4bd20681d4d47cfd15184")
require.NoError(t, err, "eth hash")
hash2, err := EthHashFromHex("ab8653edf9f51785664a643b47605a7ba3d917b5339a0724e7642c114d0e4738")
hash2, err := NewEthHashFromHex("ab8653edf9f51785664a643b47605a7ba3d917b5339a0724e7642c114d0e4738")
require.NoError(t, err, "eth hash")
testcases := []struct {

View File

@ -3,8 +3,11 @@ package itests
import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"reflect"
"strconv"
"testing"
"time"
@ -145,6 +148,63 @@ func TestDeployment(t *testing.T) {
require.NotNil(t, chainTx.TransactionIndex)
require.Equal(t, uint64(*chainTx.TransactionIndex), uint64(0)) // only transaction
// should return error with non-existent block hash
nonExistentHash, err := ethtypes.NewEthHashFromHex("0x62a80aa9262a3e1d3db0706af41c8535257b6275a283174cabf9d108d8946059")
require.Nil(t, err)
_, err = client.EthGetBlockByHash(ctx, nonExistentHash, false)
require.NotNil(t, err)
// verify block information
block1, err := client.EthGetBlockByHash(ctx, *chainTx.BlockHash, false)
require.Nil(t, err)
require.Equal(t, block1.Hash, *chainTx.BlockHash)
require.Equal(t, block1.Number, *chainTx.BlockNumber)
for _, tx := range block1.Transactions {
_, ok := tx.(string)
require.True(t, ok)
}
require.Contains(t, block1.Transactions, hash.String())
// make sure the block got from EthGetBlockByNumber is the same
blkNum := strconv.FormatInt(int64(*chainTx.BlockNumber), 10)
block2, err := client.EthGetBlockByNumber(ctx, blkNum, false)
require.Nil(t, err)
require.True(t, reflect.DeepEqual(block1, block2))
// should be able to get the block using latest as well
block3, err := client.EthGetBlockByNumber(ctx, "latest", false)
require.Nil(t, err)
require.True(t, reflect.DeepEqual(block2, block3))
// verify that the block contains full tx objects
block4, err := client.EthGetBlockByHash(ctx, *chainTx.BlockHash, true)
require.Nil(t, err)
require.Equal(t, block4.Hash, *chainTx.BlockHash)
require.Equal(t, block4.Number, *chainTx.BlockNumber)
// the call went through json-rpc and the response was unmarshaled
// into map[string]interface{}, so it has to be converted into ethtypes.EthTx
var foundTx *ethtypes.EthTx
for _, obj := range block4.Transactions {
j, err := json.Marshal(obj)
require.Nil(t, err)
var tx ethtypes.EthTx
err = json.Unmarshal(j, &tx)
require.Nil(t, err)
if tx.Hash == chainTx.Hash {
foundTx = &tx
}
}
require.NotNil(t, foundTx)
require.True(t, reflect.DeepEqual(*foundTx, *chainTx))
// make sure the block got from EthGetBlockByNumber is the same
block5, err := client.EthGetBlockByNumber(ctx, blkNum, true)
require.Nil(t, err)
require.True(t, reflect.DeepEqual(block4, block5))
// Verify that the deployer is now an account.
client.AssertActorType(ctx, deployer, manifest.EthAccountKey)

View File

@ -345,7 +345,7 @@ func ParseEthLog(in map[string]interface{}) (*ethtypes.EthLog, error) {
if !ok {
return ethtypes.EthHash{}, xerrors.Errorf(k + " not a string")
}
return ethtypes.EthHashFromHex(s)
return ethtypes.NewEthHashFromHex(s)
}
ethUint64 := func(k string, v interface{}) (ethtypes.EthUint64, error) {