Skip to content

Commit 54c7653

Browse files
committed
Chore:Update Json rpc errors
1 parent e1de14a commit 54c7653

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

tests/test_json_marshalling.nim

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,14 @@ suite "JSON-RPC Quantity":
241241
check c.kind == bidNumber
242242
check c.number == 77.Quantity
243243

244-
let d = JrpcConv.decode("\"10\"", RtBlockIdentifier)
244+
let d = JrpcConv.decode("\"latest\"", RtBlockIdentifier)
245245
check d.kind == bidAlias
246-
check d.alias == "10"
246+
check d.alias == "latest"
247+
248+
# https://github.com/ethereum/execution-apis/blob/main/tests/debug_getRawBlock/get-invalid-number.io
249+
expect JsonReaderError:
250+
let invalid = JrpcConv.decode("\"10\"", RtBlockIdentifier)
251+
discard invalid
247252

248253
expect JsonReaderError:
249254
let d = JrpcConv.decode("10", RtBlockIdentifier)

web3/conversions.nim

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@ func valid(hex: string): bool =
189189
if x notin HexDigits: return false
190190
true
191191

192+
# https://business.1inch.com/portal/documentation/apis/web3/ethereum/methods/debug_getRawBlock
193+
func isBlockTag(tag: string): bool =
194+
case tag.toLowerAscii
195+
of "earliest", "latest", "pending", "safe", "finalized": true
196+
else: false
197+
192198
func validHash(hex: string): bool =
193199
# assume `hex` has been checked before by `valid`
194200
const hexHashLen = 32*2
@@ -263,8 +269,11 @@ proc readValue*(r: var JsonReader[JrpcConv], val: var Address)
263269

264270
proc readValue*(r: var JsonReader[JrpcConv], val: var Hash32)
265271
{.gcsafe, raises: [IOError, JsonReaderError].} =
266-
wrapValueError:
267-
val = fromHex(Hash32, r.parseString())
272+
let hexStr = r.parseString()
273+
if not valid(hexStr):
274+
r.raiseUnexpectedValue("hex string without 0x prefix")
275+
wrapValueError:
276+
val = fromHex(Hash32, hexStr)
268277

269278
proc writeValue*(w: var JsonWriter[JrpcConv], v: Number)
270279
{.gcsafe, raises: [IOError].} =
@@ -378,19 +387,20 @@ proc readValue*(r: var JsonReader[JrpcConv], val: var seq[byte])
378387
# skip empty hex
379388
val = hexToSeqByte(hexStr)
380389

381-
proc readValue*(r: var JsonReader[JrpcConv], val: var RtBlockIdentifier)
390+
proc readValue*( r: var JsonReader[JrpcConv], val: var RtBlockIdentifier)
382391
{.gcsafe, raises: [IOError, JsonReaderError].} =
383-
let hexStr = r.parseString()
384-
wrapValueError:
385-
if valid(hexStr):
386-
if validHash(hexStr):
387-
val = RtBlockIdentifier(
388-
kind: bidHash, hash: fromHex(Hash32, hexStr))
392+
let blockId = r.parseString()
393+
wrapValueError:
394+
if valid(blockId) and validHash(blockId):
395+
val = RtBlockIdentifier(kind: bidHash, hash: fromHex(Hash32, blockId))
396+
elif valid(blockId):
397+
val = RtBlockIdentifier(kind: bidNumber, number: Quantity fromHex[uint64](blockId))
398+
elif isBlockTag(blockId):
399+
val = RtBlockIdentifier(kind: bidAlias, alias: blockId)
400+
elif blockId.len > 0 and blockId.allCharsInSet(HexDigits):
401+
r.raiseUnexpectedValue("hex string without 0x prefix")
389402
else:
390-
val = RtBlockIdentifier(
391-
kind: bidNumber, number: Quantity fromHex[uint64](hexStr))
392-
else:
393-
val = RtBlockIdentifier(kind: bidAlias, alias: hexStr)
403+
r.raiseUnexpectedValue("invalid block tag")
394404

395405
proc writeValue*(w: var JsonWriter[JrpcConv], v: RtBlockIdentifier)
396406
{.gcsafe, raises: [IOError].} =

0 commit comments

Comments
 (0)