Skip to content

Commit

Permalink
ethtypes: Serialize EthFilterID/EthSubscriptionID correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k committed Jan 31, 2023
1 parent 965b1cf commit 9701b11
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 296 deletions.
Binary file modified build/openrpc/full.json.gz
Binary file not shown.
Binary file modified build/openrpc/gateway.json.gz
Binary file not shown.
48 changes: 36 additions & 12 deletions chain/types/ethtypes/eth_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,18 @@ func (h *EthHash) UnmarshalJSON(b []byte) error {
return nil
}

func (h EthHash) String() string {
return "0x" + hex.EncodeToString(h[:])
}

// Should ONLY be used for blocks and Filecoin messages. Eth transactions expect a different hashing scheme.
func (h EthHash) ToCid() cid.Cid {
// err is always nil
mh, _ := multihash.EncodeName(h[:], "blake2b-256")

return cid.NewCidV1(cid.DagCBOR, mh)
}

func decodeHexString(s string, expectedLen int) ([]byte, error) {
s = handleHexStringPrefix(s)
if len(s) != expectedLen*2 {
Expand Down Expand Up @@ -420,18 +432,6 @@ func EthHashFromTxBytes(b []byte) EthHash {
return ethHash
}

func (h EthHash) String() string {
return "0x" + hex.EncodeToString(h[:])
}

// Should ONLY be used for blocks and Filecoin messages. Eth transactions expect a different hashing scheme.
func (h EthHash) ToCid() cid.Cid {
// err is always nil
mh, _ := multihash.EncodeName(h[:], "blake2b-256")

return cid.NewCidV1(cid.DagCBOR, mh)
}

type EthFeeHistory struct {
OldestBlock uint64 `json:"oldestBlock"`
BaseFeePerGas []EthBigInt `json:"baseFeePerGas"`
Expand All @@ -441,9 +441,33 @@ type EthFeeHistory struct {

type EthFilterID EthHash

func (h EthFilterID) MarshalJSON() ([]byte, error) {
return (EthHash)(h).MarshalJSON()
}

func (h *EthFilterID) UnmarshalJSON(b []byte) error {
return (*EthHash)(h).UnmarshalJSON(b)
}

func (h EthFilterID) String() string {
return (EthHash)(h).String()
}

// An opaque identifier generated by the Lotus node to refer to an active subscription.
type EthSubscriptionID EthHash

func (h EthSubscriptionID) MarshalJSON() ([]byte, error) {
return (EthHash)(h).MarshalJSON()
}

func (h *EthSubscriptionID) UnmarshalJSON(b []byte) error {
return (*EthHash)(h).UnmarshalJSON(b)
}

func (h EthSubscriptionID) String() string {
return (EthHash)(h).String()
}

type EthFilterSpec struct {
// Interpreted as an epoch or one of "latest" for last mined block, "earliest" for first,
// "pending" for not yet committed messages.
Expand Down
42 changes: 42 additions & 0 deletions chain/types/ethtypes/eth_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,48 @@ func TestEthHash(t *testing.T) {
h1, err := EthHashFromCid(c)
require.Nil(t, err)
require.Equal(t, h, h1)

jm, err := json.Marshal(h)
require.NoError(t, err)
require.Equal(t, hash, string(jm))
}
}

func TestEthFilterID(t *testing.T) {
testcases := []string{
`"0x013dbb9442ca9667baccc6230fcd5c1c4b2d4d2870f4bd20681d4d47cfd15184"`,
`"0xab8653edf9f51785664a643b47605a7ba3d917b5339a0724e7642c114d0e4738"`,
}

for _, hash := range testcases {
var h EthFilterID
err := h.UnmarshalJSON([]byte(hash))

require.Nil(t, err)
require.Equal(t, h.String(), strings.Replace(hash, `"`, "", -1))

jm, err := json.Marshal(h)
require.NoError(t, err)
require.Equal(t, hash, string(jm))
}
}

func TestEthSubscriptionID(t *testing.T) {
testcases := []string{
`"0x013dbb9442ca9667baccc6230fcd5c1c4b2d4d2870f4bd20681d4d47cfd15184"`,
`"0xab8653edf9f51785664a643b47605a7ba3d917b5339a0724e7642c114d0e4738"`,
}

for _, hash := range testcases {
var h EthSubscriptionID
err := h.UnmarshalJSON([]byte(hash))

require.Nil(t, err)
require.Equal(t, h.String(), strings.Replace(hash, `"`, "", -1))

jm, err := json.Marshal(h)
require.NoError(t, err)
require.Equal(t, hash, string(jm))
}
}

Expand Down
Loading

0 comments on commit 9701b11

Please sign in to comment.