Skip to content

Commit b6bbd1f

Browse files
authored
Merge pull request #103 from XinFinOrg/upgrade-websocket-epochnumber
EpochNumber ummarshal
2 parents 9489158 + 1bc3363 commit b6bbd1f

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

rpc/types.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,25 @@ func (bn BlockNumber) Int64() int64 {
119119
return (int64)(bn)
120120
}
121121

122+
func (e *EpochNumber) UnmarshalJSON(data []byte) error {
123+
input := trimData(data)
124+
if input == "latest" {
125+
*e = LatestEpochNumber
126+
return nil
127+
}
128+
129+
eNum, err := hexutil.DecodeUint64(input)
130+
if err != nil {
131+
return err
132+
}
133+
if eNum > math.MaxInt64 {
134+
return fmt.Errorf("EpochNumber too high")
135+
}
136+
137+
*e = EpochNumber(eNum)
138+
return nil
139+
}
140+
122141
func (e EpochNumber) Int64() int64 {
123142
return (int64)(e)
124143
}
@@ -217,3 +236,11 @@ func BlockNumberOrHashWithHash(hash common.Hash, canonical bool) BlockNumberOrHa
217236
RequireCanonical: canonical,
218237
}
219238
}
239+
240+
func trimData(data []byte) string {
241+
input := strings.TrimSpace(string(data))
242+
if len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"' {
243+
input = input[1 : len(input)-1]
244+
}
245+
return input
246+
}

0 commit comments

Comments
 (0)