Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: eth_getBlockReceipts should work similarly as geth #6420

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions venus-shared/actors/types/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,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
}

return errors.New("invalid block param")
}

Expand Down
102 changes: 102 additions & 0 deletions venus-shared/actors/types/eth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

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

Expand Down Expand Up @@ -467,3 +468,104 @@ func BenchmarkEthHashFromCid(b *testing.B) {
}
}
}

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
}
Loading