Skip to content

Commit

Permalink
Merge pull request #9955 from filecoin-project/itest/eth-block
Browse files Browse the repository at this point in the history
itests: Eth JSON-RPC: EthGetBlockByHash and EthGetBlockByNumber
  • Loading branch information
ychiaoli18 authored Jan 8, 2023
2 parents 77b235d + 7029364 commit 402173e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 12 deletions.
4 changes: 2 additions & 2 deletions api/docgen/docgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
6 changes: 3 additions & 3 deletions chain/types/ethtypes/eth_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions chain/types/ethtypes/eth_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down
60 changes: 60 additions & 0 deletions itests/eth_deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package itests
import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"reflect"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion itests/eth_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 402173e

Please sign in to comment.