Skip to content

Commit

Permalink
parse block hash as a free param (#12539)
Browse files Browse the repository at this point in the history
  • Loading branch information
aarshkshah1992 authored Oct 2, 2024
1 parent 37b073c commit 918a68f
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
10 changes: 10 additions & 0 deletions chain/types/ethtypes/eth_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,16 @@ func (e *EthBlockNumberOrHash) UnmarshalJSON(b []byte) error {
return nil
}

// check if input is a block hash (66 characters long)
if len(str) == 66 && strings.HasPrefix(str, "0x") {
hash, err := ParseEthHash(str)
if err != nil {
return err
}
e.BlockHash = &hash
return nil
}

// check if block param is a number (decimal or hex)
var num EthUint64
if err := num.UnmarshalJSON(b); err == nil {
Expand Down
98 changes: 98 additions & 0 deletions chain/types/ethtypes/eth_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ethtypes

import (
"encoding/json"
"fmt"
"strings"
"testing"

Expand Down Expand Up @@ -534,6 +535,103 @@ func TestFilecoinAddressToEthAddressParams(t *testing.T) {
}
}

func TestEthBlockNumberOrHashUnmarshalJSON(t *testing.T) {
testCases := []struct {
name string
input string
expected func() EthBlockNumberOrHash
wantErr bool
}{
{
name: "Valid block number",
input: `{"blockNumber": "0x1234"}`,
expected: func() EthBlockNumberOrHash {
v := uint64(0x1234)
return EthBlockNumberOrHash{BlockNumber: (*EthUint64)(&v)}
},
},
{
name: "Valid block hash",
input: `{"blockHash": "0x1234567890123456789012345678901234567890123456789012345678901234"}`,
expected: func() EthBlockNumberOrHash {
h, _ := ParseEthHash("0x1234567890123456789012345678901234567890123456789012345678901234")
return EthBlockNumberOrHash{BlockHash: &h}
},
},
{
name: "Valid block number as string",
input: `"0x1234"`,
expected: func() EthBlockNumberOrHash {
v := uint64(0x1234)
return EthBlockNumberOrHash{BlockNumber: (*EthUint64)(&v)}
},
},
{
name: "Valid block hash as string",
input: `"0x1234567890123456789012345678901234567890123456789012345678901234"`,
expected: func() EthBlockNumberOrHash {
h, _ := ParseEthHash("0x1234567890123456789012345678901234567890123456789012345678901234")
return EthBlockNumberOrHash{BlockHash: &h}
},
},
{
name: "Valid 'latest' string",
input: `"latest"`,
expected: func() EthBlockNumberOrHash {
return EthBlockNumberOrHash{PredefinedBlock: stringPtr("latest")}
},
},
{
name: "Valid 'earliest' string",
input: `"earliest"`,
expected: func() EthBlockNumberOrHash {
return EthBlockNumberOrHash{PredefinedBlock: stringPtr("earliest")}
},
},
{
name: "Valid 'pending' string",
input: `"pending"`,
expected: func() EthBlockNumberOrHash {
return EthBlockNumberOrHash{PredefinedBlock: stringPtr("pending")}
},
},
{
name: "Invalid: both block number and hash",
input: `{"blockNumber": "0x1234", "blockHash": "0x1234567890123456789012345678901234567890123456789012345678901234"}`,
wantErr: true,
},
{
name: "Invalid block number",
input: `{"blockNumber": "invalid"}`,
wantErr: true,
},
{
name: "Invalid block hash",
input: `{"blockHash": "invalid"}`,
wantErr: true,
},
{
name: "Invalid string",
input: `"invalid"`,
wantErr: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var got EthBlockNumberOrHash
err := got.UnmarshalJSON([]byte(tc.input))

if tc.wantErr {
require.Error(t, err)
} else {
require.NoError(t, err, fmt.Sprintf("did not expect error but got %s", err))
require.Equal(t, tc.expected(), got)
}
})
}
}

func stringPtr(s string) *string {
return &s
}
Expand Down
10 changes: 10 additions & 0 deletions itests/fevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,16 @@ func TestEthGetBlockReceipts(t *testing.T) {
require.NoError(t, err)
require.Equal(t, txReceipt, receipt)
}

// try with the geth request format for `EthBlockNumberOrHash`
var req ethtypes.EthBlockNumberOrHash
reqStr := fmt.Sprintf(`"%s"`, lastReceipt.BlockHash.String())
err = req.UnmarshalJSON([]byte(reqStr))
require.NoError(t, err)

gethBlockReceipts, err := client.EthGetBlockReceipts(ctx, req)
require.NoError(t, err)
require.Len(t, gethBlockReceipts, 3)
}

func deployContractWithEth(ctx context.Context, t *testing.T, client *kit.TestFullNode, ethAddr ethtypes.EthAddress,
Expand Down

0 comments on commit 918a68f

Please sign in to comment.