Skip to content

Commit aa6f1ea

Browse files
committed
Add tests
1 parent f14ffb4 commit aa6f1ea

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

tests/test_json_marshalling.nim

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ proc rand(_: type uint64): uint64 =
3535
discard randomBytes(res)
3636
uint64.fromBytesBE(res)
3737

38+
proc rand(_: type int): int =
39+
var res: array[sizeof(result), byte]
40+
discard randomBytes(res)
41+
copyMem(result.addr, res[0].addr, sizeof(result))
42+
3843
proc rand[T: Quantity](_: type T): T =
3944
var res: array[sizeof(T), byte]
4045
discard randomBytes(res)
@@ -105,6 +110,9 @@ proc rand(_: type seq[seq[byte]]): seq[seq[byte]] =
105110
discard randomBytes(z)
106111
@[z, z, z]
107112

113+
proc rand(_: type Table[Hash32, Hash32]): Table[Hash32, Hash32] =
114+
result[Hash32.rand()] = Hash32.rand()
115+
108116
proc rand[T](_: type SingleOrList[T]): SingleOrList[T] =
109117
SingleOrList[T](kind: slkSingle, single: rand(T))
110118

@@ -235,6 +243,14 @@ suite "JSON-RPC Quantity":
235243
checkRandomObject(EthConfigObject)
236244
checkRandomObject(StorageValuesRequest)
237245

246+
checkRandomObject(OverrideAccount)
247+
checkRandomObject(BlockOverrides)
248+
checkRandomObject(BlockStateCall)
249+
checkRandomObject(SimulationRequest)
250+
checkRandomObject(CallError)
251+
checkRandomObject(SimulateCallResult)
252+
checkRandomObject(SimulateBlockResult)
253+
238254
test "check blockId":
239255
let a = RtBlockIdentifier(kind: bidNumber, number: 77.Quantity)
240256
let x = EthJson.encode(a)

web3/conversions.nim

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ EthJson.automaticSerialization(seq, true)
4141
EthJson.automaticSerialization(bool, true)
4242
EthJson.automaticSerialization(float64, true)
4343
EthJson.automaticSerialization(array, true)
44+
EthJson.automaticSerialization(int, true)
4445

4546
#------------------------------------------------------------------------------
4647
# eth_api_types
@@ -66,6 +67,14 @@ BlobScheduleObject.useDefaultSerializationIn EthJson
6667
ConfigObject.useDefaultSerializationIn EthJson
6768
EthConfigObject.useDefaultSerializationIn EthJson
6869

70+
OverrideAccount.useDefaultSerializationIn EthJson
71+
BlockOverrides.useDefaultSerializationIn EthJson
72+
BlockStateCall.useDefaultSerializationIn EthJson
73+
SimulationRequest.useDefaultSerializationIn EthJson
74+
CallError.useDefaultSerializationIn EthJson
75+
SimulateCallResult.useDefaultSerializationIn EthJson
76+
SimulateBlockResult.useDefaultSerializationIn EthJson
77+
6978
#------------------------------------------------------------------------------
7079
# engine_api_types
7180
#------------------------------------------------------------------------------
@@ -565,6 +574,18 @@ proc writeValue*(w: var JsonWriter[EthJson], v: StorageValuesRequest)
565574
w.writeMember(x.address.to0xHex, x.data)
566575
w.endObject()
567576

577+
proc writeValue*(w: var JsonWriter[EthJson], v: Table[Hash32, Hash32])
578+
{.gcsafe, raises: [IOError].} =
579+
w.beginObject()
580+
for k, val in v:
581+
w.writeMember(k.to0xHex, val)
582+
w.endObject()
583+
584+
proc readValue*(r: var JsonReader[EthJson], val: var Table[Hash32, Hash32])
585+
{.gcsafe, raises: [IOError, SerializationError].} =
586+
for k,v in readObject(r, Hash32, Hash32):
587+
val[k] = v
588+
568589
func `$`*(v: Quantity): string {.inline.} =
569590
encodeQuantity(distinctBase(v))
570591

0 commit comments

Comments
 (0)