Skip to content

Commit

Permalink
Merge pull request #103 from XinFinOrg/upgrade-websocket-epochnumber
Browse files Browse the repository at this point in the history
EpochNumber ummarshal
  • Loading branch information
wjrjerome authored Oct 25, 2023
2 parents 9489158 + 1bc3363 commit b6bbd1f
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions rpc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,25 @@ func (bn BlockNumber) Int64() int64 {
return (int64)(bn)
}

func (e *EpochNumber) UnmarshalJSON(data []byte) error {
input := trimData(data)
if input == "latest" {
*e = LatestEpochNumber
return nil
}

eNum, err := hexutil.DecodeUint64(input)
if err != nil {
return err
}
if eNum > math.MaxInt64 {
return fmt.Errorf("EpochNumber too high")
}

*e = EpochNumber(eNum)
return nil
}

func (e EpochNumber) Int64() int64 {
return (int64)(e)
}
Expand Down Expand Up @@ -217,3 +236,11 @@ func BlockNumberOrHashWithHash(hash common.Hash, canonical bool) BlockNumberOrHa
RequireCanonical: canonical,
}
}

func trimData(data []byte) string {
input := strings.TrimSpace(string(data))
if len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"' {
input = input[1 : len(input)-1]
}
return input
}

0 comments on commit b6bbd1f

Please sign in to comment.