Skip to content
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

Migrate to json-serialization #114

Merged
merged 2 commits into from
Jan 8, 2024
Merged
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
1 change: 0 additions & 1 deletion tests/all_tests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import
test_primitives,
test_contracts,
test_deposit_contract,
test_ethhexstrings,
test_logs,
test_json_marshalling,
test_signed_tx,
Expand Down
29 changes: 29 additions & 0 deletions tests/helpers/primitives_utils.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import
stint,
../../web3/primitives

func ethToWei*(eth: UInt256): UInt256 =
eth * 1000000000000000000.u256

type
BlobData* = DynamicBytes[0, 512]

func conv*(T: type, x: int): T =
type BaseType = distinctBase T
var res: BaseType
when BaseType is seq:
res.setLen(1)
res[^1] = x.byte
T(res)

func address*(x: int): Address =
conv(typeof result, x)

func txhash*(x: int): TxHash =
conv(typeof result, x)

func blob*(x: int): BlobData =
conv(typeof result, x)

func h256*(x: int): Hash256 =
conv(typeof result, x)
28 changes: 1 addition & 27 deletions tests/helpers/utils.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import
std/options,
chronos, stint,
chronos,
stew/byteutils,
../../web3,
../../web3/primitives
Expand All @@ -16,29 +16,3 @@ proc deployContract*(web3: Web3, code: string, gasPrice = 0): Future[ReceiptObje

let r = await web3.send(tr)
return await web3.getMinedTransactionReceipt(r)

func ethToWei*(eth: UInt256): UInt256 =
eth * 1000000000000000000.u256

type
BlobData* = DynamicBytes[0, 512]

func conv*(T: type, x: int): T =
type BaseType = distinctBase T
var res: BaseType
when BaseType is seq:
res.setLen(1)
res[^1] = x.byte
T(res)

func address*(x: int): Address =
conv(typeof result, x)

func txhash*(x: int): TxHash =
conv(typeof result, x)

func blob*(x: int): BlobData =
conv(typeof result, x)

func h256*(x: int): Hash256 =
conv(typeof result, x)
4 changes: 3 additions & 1 deletion tests/test_deposit_contract.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import
chronos, stint,
../web3,
./helpers/utils,
./helpers/primitives_utils,
./helpers/depositcontract

contract(DepositContract):
Expand Down Expand Up @@ -45,7 +46,8 @@ suite "Deposit contract":

var fut = newFuture[void]()

let s = await ns.subscribe(DepositEvent, %*{"fromBlock": "0x0"}) do (
let options = FilterOptions(fromBlock: some(blockId(0)))
let s = await ns.subscribe(DepositEvent, options) do (
pubkey: DynamicBytes[0, 48], withdrawalCredentials: DynamicBytes[0, 32], amount: DynamicBytes[0, 8], signature: DynamicBytes[0, 96], merkleTreeIndex: DynamicBytes[0, 8])
{.raises: [], gcsafe.}:
try:
Expand Down
86 changes: 0 additions & 86 deletions tests/test_ethhexstrings.nim

This file was deleted.

2 changes: 1 addition & 1 deletion tests/test_execution_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import
pkg/unittest2,
stew/byteutils,
../web3/execution_types,
./helpers/utils
./helpers/primitives_utils

suite "Execution types tests":
let
Expand Down
157 changes: 140 additions & 17 deletions tests/test_json_marshalling.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,105 @@ import
std/[typetraits, json],
stint,
unittest2,
nimcrypto,
stew/endians2,
json_rpc/jsonmarshal, json_serialization,
../web3/engine_api_types,
../web3/execution_types,
../web3/[conversions, eth_api_types]

proc `==`(x, y: Quantity): bool {.borrow, noSideEffect.}
proc rand[N](_: type FixedBytes[N]): FixedBytes[N] =
discard randomBytes(distinctBase result)

proc rand[M,N](_: type DynamicBytes[M,N]): DynamicBytes[M,N] =
discard randomBytes(distinctBase result)

proc rand(_: type Address): Address =
discard randomBytes(distinctBase result)

proc rand(_: type Quantity): Quantity =
var res: array[8, byte]
discard randomBytes(res)
result = Quantity(uint64.fromBytesBE(res))

proc rand(_: type RlpEncodedBytes): RlpEncodedBytes =
discard randomBytes(distinctBase result)

proc rand(_: type TypedTransaction): TypedTransaction =
discard randomBytes(distinctBase result)

proc rand(_: type string): string =
result = "random bytes"

proc rand(_: type bool): bool =
var x: array[1, byte]
discard randomBytes(x)
result = x[0].int mod 2 == 0

proc rand(_: type byte): byte =
var x: array[1, byte]
discard randomBytes(x)
result = x[0]

proc rand(_: type UInt256): UInt256 =
var x: array[32, byte]
discard randomBytes(x)
result = UInt256.fromBytesBE(x)

proc rand(_: type RtBlockIdentifier): RtBlockIdentifier =
RtBlockIdentifier(kind: bidNumber, number: rand(Quantity).uint64)

proc rand(_: type PayloadExecutionStatus): PayloadExecutionStatus =
var x: array[1, byte]
discard randomBytes(x)
if x[0].int <= high(PayloadExecutionStatus).int and
x[0].int >= low(PayloadExecutionStatus).int:
result = PayloadExecutionStatus(x[0].int)

proc rand(_: type TxOrHash): TxOrHash =
TxOrHash(kind: tohHash, hash: rand(TxHash))

proc rand[X: object](T: type X): T

proc rand[T](_: type seq[T]): seq[T] =
result = newSeq[T](3)
for i in 0..<3:
result[i] = rand(T)

proc rand[T](_: type SingleOrList[T]): SingleOrList[T] =
SingleOrList[T](kind: slkSingle, single: rand(T))

proc rand[X: object](T: type X): T =
result = T()
for field in fields(result):
field = rand(typeof(field))

proc rand[X: ref](T: type X): T =
result = T()
for field in fields(result[]):
field = rand(typeof(field))

template checkRandomObject(T: type) =
let obj = rand(T)
let bytes = JrpcConv.encode(obj)
let decoded = JrpcConv.decode(bytes, T)
let bytes2 = JrpcConv.encode(decoded)
check bytes == bytes2

suite "JSON-RPC Quantity":
test "Valid":
for (validQuantityStr, validQuantity) in [
("0x0", Quantity 0),
("0x123", Quantity 291),
("0x1234", Quantity 4660)]:
let validQuantityJson = $(%validQuantityStr)
var resQuantity: Quantity
var resUInt256: UInt256
var resUInt256Ref: ref UInt256
fromJson(%validQuantityStr, "", resQuantity)
fromJson(%validQuantityStr, "", resUInt256)
fromJson(%validQuantityStr, "", resUInt256Ref)
let validQuantityJson = JrpcConv.encode(validQuantityStr)
let resQuantity = JrpcConv.decode(validQuantityJson, Quantity)
let resUInt256 = JrpcConv.decode(validQuantityJson, UInt256)
let resUInt256Ref = JrpcConv.decode(validQuantityJson, ref UInt256)

check:
Json.decode(validQuantityJson, Quantity) == validQuantity
Json.encode(validQuantity) == validQuantityJson
JrpcConv.decode(validQuantityJson, Quantity) == validQuantity
JrpcConv.encode(validQuantity) == validQuantityJson
resQuantity == validQuantity
resUInt256 == validQuantity.distinctBase.u256
resUInt256Ref[] == validQuantity.distinctBase.u256
Expand All @@ -44,10 +122,11 @@ suite "JSON-RPC Quantity":
template checkInvalids(typeName: untyped) =
var resQuantity: `typeName`
try:
fromJson(%invalidStr, "", resQuantity)
echo `typeName`, invalidStr
let jsonBytes = JrpcConv.encode(invalidStr)
resQuantity = JrpcConv.decode(jsonBytes, `typeName`)
echo `typeName`, " ", invalidStr
check: false
except ValueError:
except SerializationError:
check: true
except CatchableError:
check: false
Expand All @@ -60,7 +139,51 @@ suite "JSON-RPC Quantity":
const blockJson = """
{"difficulty":"0x1","extraData":"0x696e667572612d696f00000000000000000000000000000000000000000000004ede22d16eaf5bbab47534ee64a1ec1728ed63b1243672ee9623532fffd747b368cddb4674f849e467884f0e6c6563440ea5fd812cc33fcd19fb9c323b0c92c300","gasLimit":"0x7a1200","gasUsed":"0x3aca3e","hash":"0x5ac670562dbf877a45039d65ec3c2e3402a40eda9b1dba931c2376ab7d0927c2","logsBloom":"0x40000000004000120000000100030000882000040000001800010000020000000000000000001400080040004080400402024800000004000088f0320000a0000016000100110060800000080000020000000020000000000050000009010000080000040220280000080240048008000001381a1100000000000010000440000000004200000018001102280400003040040020001a000026000488000101000120000a0002000081220000100000000200000000040440a02400010000000002000002400100840000080000000080000008c0c080000008000000220860082002000000001000000041040002000000008000010004000400010400040001","miner":"0x0000000000000000000000000000000000000000","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0000000000000000","number":"0x42a3da","parentHash":"0xe0190ed0683835483c35e0c0a98bf0958ed2ca7313428c9026db51604007e299","receiptsRoot":"0x12fff235455db65dcdc525d2491b9e0526e02d70a1515fba155b1de9f648abf3","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","size":"0x2a5d","stateRoot":"0xccb37180b5fca41e43395d524a0ee83a1efc69f2fb61f90a51f3dc8f40f2144e","timestamp":"0x603cab8c","totalDifficulty":"0x624910","transactions":["0xa3c07dc59bfdb1bfc2d50920fed2ef2c1c4e0a09fe2325dbc14e07702f965a78","0x7b33b36e905c8e83a519216444aff4952bfbce5c49247ecc70f227a56068247e","0x11fa3f25957caa1918ecb1f2c1eaeef46c1af983e1b7eee65cefe2a752f7e9da","0x7028ef43993c84e751bbcb126cbaa52d7b732efe4daf95e6e0fbff06e43f0277","0xe287b4939a51aff8a78b836c56db18ca1559ba77303b1e0cee9075ff737f4a57","0xb79cb96a3ff5bb9aca4ce2024a57023cfda163d6135f3ff6a0d9f0b9fac44efb","0xad0d4bfb6b4276616c7d88fe2576903d6c17f6bee1d10db15de203a88bda2898","0x73127fadab4c4ceed35be81e3e97549d2005bfdbc49083ce81f135662edd6869","0xff522f3e2a2d451acf2df2341d8ed9e982dbf7160b327f34b8b5ca25b377a74f","0x2d100b5abba751743920cf56614195c0e3685d93bfdbe5325c35a933f5195e2c","0x9611c7cac2e14fed4051d34f74eed1bace9da8e79446d4e03f479c318de24087","0x43f29106dac821e5069b3c0b27a61d01116a8e69096388d57b70a9e5354e0457","0xb71fe345141491f54cb53e4af44d581cbd631c0b7bd12019077c1dc354b1c5ab"],"transactionsRoot":"0x08fd011674202c6df63822f877e88d7ca0fe41e5deb8bc2b8830f1a29ce864f0","uncles":[]}
"""
var b1, b2: BlockObject
fromJson(parseJson(blockJson), "", b1)
fromJson(parseJson($(%b1)), "", b2)
check $(%b1) == $(%b2)
let
b1 = JrpcConv.decode(blockJson, BlockObject)
jsonBytes = JrpcConv.encode(b1)
b2 = JrpcConv.decode(jsonBytes, BlockObject)
b1Bytes = JrpcConv.encode(b1)
b2Bytes = JrpcConv.encode(b2)

check b1Bytes == b2Bytes

test "Random object encoding":
checkRandomObject(SyncObject)
checkRandomObject(WithdrawalObject)
checkRandomObject(AccessTuple)
checkRandomObject(AccessListResult)
checkRandomObject(LogObject)
checkRandomObject(StorageProof)
checkRandomObject(ProofResponse)
checkRandomObject(FilterOptions)
checkRandomObject(EthSend)
checkRandomObject(EthCall)

checkRandomObject(BlockHeader)
checkRandomObject(BlockObject)
checkRandomObject(TransactionObject)
checkRandomObject(ReceiptObject)

checkRandomObject(WithdrawalV1)
checkRandomObject(ExecutionPayloadV1)
checkRandomObject(ExecutionPayloadV2)
checkRandomObject(ExecutionPayloadV1OrV2)
checkRandomObject(ExecutionPayloadV3)
checkRandomObject(BlobsBundleV1)
checkRandomObject(ExecutionPayloadBodyV1)
checkRandomObject(PayloadAttributesV1)
checkRandomObject(PayloadAttributesV2)
checkRandomObject(PayloadAttributesV3)
checkRandomObject(PayloadAttributesV1OrV2)
checkRandomObject(PayloadStatusV1)
checkRandomObject(ForkchoiceStateV1)
checkRandomObject(ForkchoiceUpdatedResponse)
checkRandomObject(TransitionConfigurationV1)
checkRandomObject(GetPayloadV2Response)
checkRandomObject(GetPayloadV2ResponseExact)
checkRandomObject(GetPayloadV3Response)

checkRandomObject(ExecutionPayload)
checkRandomObject(PayloadAttributes)
checkRandomObject(GetPayloadResponse)
3 changes: 2 additions & 1 deletion tests/test_logs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ suite "Logs":
let notifFut = newFuture[void]()
var notificationsReceived = 0

let s = await ns.subscribe(MyEvent, %*{"fromBlock": "0x0"}) do (
let options = FilterOptions(fromBlock: some(blockId(0)))
let s = await ns.subscribe(MyEvent, options) do (
sender: Address, value: UInt256)
{.raises: [], gcsafe.}:
try:
Expand Down
Loading