Skip to content

Commit

Permalink
Migrate to json-serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed Jan 7, 2024
1 parent be1bb30 commit 2ad3f2a
Show file tree
Hide file tree
Showing 21 changed files with 537 additions and 704 deletions.
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
35 changes: 19 additions & 16 deletions tests/test_json_marshalling.nim
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ suite "JSON-RPC Quantity":
("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 +42,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 +59,11 @@ 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
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
48 changes: 23 additions & 25 deletions tests/test_null_conversion.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import
pkg/unittest2,
stint,
json_rpc/jsonmarshal,
../web3,
../web3/[conversions, eth_api_types, engine_api_types]

template should_be_value_error(input: string, value: untyped): void =
expect ValueError:
fromJson(%input, "", value)
expect SerializationError:
value = JrpcConv.decode(input, typeof(value))

template should_not_error(input: string, value: untyped): void =
fromJson(%input, "", value)
value = JrpcConv.decode(input, typeof(value))

suite "Null conversion":
var resAddress: Address
Expand All @@ -24,7 +23,7 @@ suite "Null conversion":
var resUInt256Ref: ref UInt256

## Covers the converters which can be found in web3/conversions.nim
## Ensure that when passing a nully value they respond with a ValueError
## Ensure that when passing a nully value they respond with a SerializationError
test "passing null values to normal convertors":
should_be_value_error("null", resAddress)
should_be_value_error("null", resDynamicBytes)
Expand All @@ -33,7 +32,7 @@ suite "Null conversion":
should_be_value_error("null", resRlpEncodedBytes)
should_be_value_error("null", resTypedTransaction)
should_be_value_error("null", resUInt256)
should_be_value_error("null", resUInt256Ref)
should_not_error("null", resUInt256Ref)

test "passing empty values to normal convertors":
should_be_value_error("", resAddress)
Expand All @@ -46,30 +45,30 @@ suite "Null conversion":
should_be_value_error("", resUInt256Ref)

test "passing invalid hex (0x) values to normal convertors":
should_be_value_error("0x", resAddress)
should_be_value_error("0x", resDynamicBytes)
should_be_value_error("0x", resFixedBytes)
should_be_value_error("0x", resQuantity)
should_be_value_error("0x", resUInt256)
should_be_value_error("0x", resUInt256Ref)
should_be_value_error("\"0x\"", resAddress)
should_be_value_error("\"0x\"", resDynamicBytes)
should_be_value_error("\"0x\"", resFixedBytes)
should_be_value_error("\"0x\"", resQuantity)
should_be_value_error("\"0x\"", resUInt256)
should_be_value_error("\"0x\"", resUInt256Ref)

test "passing hex (0x) values to normal convertors":
should_not_error("0x", resRlpEncodedBytes)
should_not_error("0x", resTypedTransaction)
should_not_error("\"0x\"", resRlpEncodedBytes)
should_not_error("\"0x\"", resTypedTransaction)

test "passing malformed hex (0x_) values to normal convertors":
should_be_value_error("0x_", resAddress)
should_be_value_error("0x_", resDynamicBytes)
should_be_value_error("0x_", resFixedBytes)
should_be_value_error("0x_", resQuantity)
should_be_value_error("0x_", resRlpEncodedBytes)
should_be_value_error("0x_", resTypedTransaction)
should_be_value_error("0x_", resUInt256)
should_be_value_error("0x_", resUInt256Ref)
should_be_value_error("\"0x_\"", resAddress)
should_be_value_error("\"0x_\"", resDynamicBytes)
should_be_value_error("\"0x_\"", resFixedBytes)
should_be_value_error("\"0x_\"", resQuantity)
should_be_value_error("\"0x_\"", resRlpEncodedBytes)
should_be_value_error("\"0x_\"", resTypedTransaction)
should_be_value_error("\"0x_\"", resUInt256)
should_be_value_error("\"0x_\"", resUInt256Ref)

## Covering the web3/engine_api_types
##
## NOTE: These will be transformed by the fromJson imported from
## NOTE: These will be transformed by the JrpcConv imported from
## nim-json-rpc/json_rpc/jsonmarshal
test "passing nully values to specific convertors":

Expand All @@ -92,7 +91,6 @@ suite "Null conversion":
should_be_value_error(forkchoiceUpdatedResponse.format(), resForkchoiceUpdatedResponse)
should_be_value_error(transitionConfigurationV1.format(), resTransitionConfigurationV1)


## If different status types can have branching logic
## we should cover each status type with different null ops
test "passing nully values to specific status types":
Expand All @@ -106,4 +104,4 @@ suite "Null conversion":
"validationError": null
}""".replace("status_name", $status_type)

should_be_value_error(payloadStatusV1, resPayloadStatusV1)
should_not_error(payloadStatusV1, resPayloadStatusV1)
2 changes: 1 addition & 1 deletion tests/test_primitives.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import
pkg/unittest2,
stew/byteutils,
../web3/primitives,
./helpers/utils
./helpers/primitives_utils

suite "Primitives":
const
Expand Down
Loading

0 comments on commit 2ad3f2a

Please sign in to comment.