Skip to content

feat: implement encoding.TextMarshaler/Unmarshaler for EthUint64 #13071

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
59 changes: 39 additions & 20 deletions chain/types/ethtypes/eth_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ethtypes

import (
"bytes"
"encoding"
"encoding/binary"
"encoding/hex"
"encoding/json"
Expand Down Expand Up @@ -55,34 +56,52 @@ const (

type EthUint64 uint64

var _ encoding.TextUnmarshaler = (*EthUint64)(nil)
var _ encoding.TextMarshaler = (*EthUint64)(nil)

// UnmarshalText should be able to parse these types of input:
// 1. a string containing a hex-encoded uint64 starting with 0x
// 2. a string containing an uint64 in decimal
func (e *EthUint64) UnmarshalText(text []byte) error {
s := string(text)
base := 10
if strings.HasPrefix(s, "0x") {
base = 16
s = s[2:]
}
parsedInt, err := strconv.ParseUint(s, base, 64)
if err != nil {
return xerrors.Errorf("cannot parse uint64: %w", err)
}
eint := EthUint64(parsedInt)
*e = eint
return nil
}

// MarshalText implements encoding.TextMarshaler.
func (e *EthUint64) MarshalText() ([]byte, error) {
return []byte(e.Hex()), nil
}

// MarshalJSON implements json.Marshaler.
func (e EthUint64) MarshalJSON() ([]byte, error) {
return json.Marshal(e.Hex())
}

// UnmarshalJSON should be able to parse these types of input:
// 1. a JSON string containing a hex-encoded uint64 starting with 0x
// 2. a JSON string containing an uint64 in decimal
// 3. a string containing an uint64 in decimal
// 3. a numeral literal containing an uint64 in decimal
func (e *EthUint64) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err == nil {
base := 10
if strings.HasPrefix(s, "0x") {
base = 16
s = s[2:]
}
parsedInt, err := strconv.ParseUint(s, base, 64)
if err != nil {
return err
}
eint := EthUint64(parsedInt)
*e = eint
return nil
} else if eint, err := strconv.ParseUint(string(b), 10, 64); err == nil {
*e = EthUint64(eint)
return nil
// if string literal, remove quotes
if b[0] == '"' && b[len(b)-1] == '"' {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use json.Unmarshal. This won't handle:

  1. Strings < 2 bytes.
  2. JSON escape sequences.

Etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

100% missed the slice lengts, will do

b = b[1 : len(b)-1]
return e.UnmarshalText(b)
}
if bytes.HasPrefix(b, []byte("0x")) {
return xerrors.New("cannot parse uint64: 0x prefix is not allowed in integer literals")
}
return xerrors.Errorf("cannot interpret %s as a hex-encoded uint64, or a number", string(b))
return e.UnmarshalText(b)
}

func EthUint64FromHex(s string) (EthUint64, error) {
Expand All @@ -96,7 +115,7 @@ func EthUint64FromHex(s string) (EthUint64, error) {
// EthUint64FromString parses a uint64 from a string. It accepts both decimal and hex strings.
func EthUint64FromString(s string) (EthUint64, error) {
var parsedInt EthUint64
err := parsedInt.UnmarshalJSON([]byte(`"` + s + `"`))
err := parsedInt.UnmarshalText([]byte(s))
if err != nil {
return 0, err
}
Expand Down
2 changes: 1 addition & 1 deletion gateway/proxy_eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (gw *Node) checkBlkParam(ctx context.Context, blkParam string, lookback eth
case "finalized":
num = ethtypes.EthUint64(head.Height()) - lookback - ethtypes.EthUint64(policy.ChainFinality)
default:
if err := num.UnmarshalJSON([]byte(`"` + blkParam + `"`)); err != nil {
if err := num.UnmarshalText([]byte(blkParam)); err != nil {
return fmt.Errorf("cannot parse block number: %v", err)
}

Expand Down
Loading