From f8e215c8173ce01db8826015cf5e045536644c08 Mon Sep 17 00:00:00 2001 From: ychiao Date: Mon, 2 Jan 2023 11:20:05 +0800 Subject: [PATCH 1/2] itests: Eth JSON-RPC: EthGetBlockByHash and EthGetBlockByNumber --- itests/eth_deploy_test.go | 54 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/itests/eth_deploy_test.go b/itests/eth_deploy_test.go index 3fb49087e..b082664bc 100644 --- a/itests/eth_deploy_test.go +++ b/itests/eth_deploy_test.go @@ -3,8 +3,11 @@ package itests import ( "context" "encoding/hex" + "encoding/json" "fmt" "os" + "reflect" + "strconv" "testing" "time" @@ -145,6 +148,57 @@ func TestDeployment(t *testing.T) { require.NotNil(t, chainTx.TransactionIndex) require.Equal(t, uint64(*chainTx.TransactionIndex), uint64(0)) // only transaction + // 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) From 7029364f4dfb84df9b3bce8b48b63be38bb24961 Mon Sep 17 00:00:00 2001 From: ychiao Date: Mon, 2 Jan 2023 17:21:47 +0800 Subject: [PATCH 2/2] check non-existent block hash --- api/docgen/docgen.go | 4 ++-- chain/types/ethtypes/eth_types.go | 6 +++--- chain/types/ethtypes/eth_types_test.go | 12 ++++++------ itests/eth_deploy_test.go | 6 ++++++ itests/eth_filter_test.go | 2 +- 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/api/docgen/docgen.go b/api/docgen/docgen.go index e2114ee70..b16a8691a 100644 --- a/api/docgen/docgen.go +++ b/api/docgen/docgen.go @@ -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 } diff --git a/chain/types/ethtypes/eth_types.go b/chain/types/ethtypes/eth_types.go index 0b100afc6..d3c3d7226 100644 --- a/chain/types/ethtypes/eth_types.go +++ b/chain/types/ethtypes/eth_types.go @@ -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 { diff --git a/chain/types/ethtypes/eth_types_test.go b/chain/types/ethtypes/eth_types_test.go index fe7b07b11..cb9c607ea 100644 --- a/chain/types/ethtypes/eth_types_test.go +++ b/chain/types/ethtypes/eth_types_test.go @@ -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 { diff --git a/itests/eth_deploy_test.go b/itests/eth_deploy_test.go index b082664bc..1f242eeb3 100644 --- a/itests/eth_deploy_test.go +++ b/itests/eth_deploy_test.go @@ -148,6 +148,12 @@ 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) diff --git a/itests/eth_filter_test.go b/itests/eth_filter_test.go index 3a4d8a5f0..d39b39be9 100644 --- a/itests/eth_filter_test.go +++ b/itests/eth_filter_test.go @@ -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) {