Skip to content

Commit

Permalink
Reduce compiler warnings (#127)
Browse files Browse the repository at this point in the history
* Reduce compiler warnings

* Fix test_null_conversion

* Fix regression because of json-rpc too smart when handling null value
  • Loading branch information
jangko authored Jan 24, 2024
1 parent ce47cde commit baff215
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 46 deletions.
64 changes: 33 additions & 31 deletions tests/helpers/handlers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ import
type
Hash256 = w3.Hash256

func decodeFromString(x: JsonString, T: type): T =
let jsonBytes = JrpcConv.decode(x.string, string)
result = JrpcConv.decode(jsonBytes, T)

proc installHandlers*(server: RpcServer) =
server.rpc("eth_syncing") do(x: JsonString, ) -> SyncingStatus:
server.rpc("eth_syncing") do(x: JsonString) -> SyncingStatus:
return SyncingStatus(syncing: false)

server.rpc("eth_sendRawTransaction") do(x: JsonString, data: seq[byte]) -> TxHash:
Expand All @@ -30,130 +34,128 @@ proc installHandlers*(server: RpcServer) =
server.rpc("eth_getTransactionReceipt") do(x: JsonString, data: TxHash) -> ReceiptObject:
var r: ReceiptObject
if x != "-1".JsonString:
r = JrpcConv.decode(x.string, ReceiptObject)
r = decodeFromString(x, ReceiptObject)
return r

server.rpc("eth_getTransactionByHash") do(x: JsonString, data: TxHash) -> TransactionObject:
var tx: TransactionObject
if x != "-1".JsonString:
tx = JrpcConv.decode(x.string, TransactionObject)
tx = decodeFromString(x, TransactionObject)
return tx

server.rpc("eth_getTransactionByBlockNumberAndIndex") do(x: JsonString, blockId: RtBlockIdentifier, quantity: Quantity) -> TransactionObject:
var tx: TransactionObject
if x != "-1".JsonString:
tx = JrpcConv.decode(x.string, TransactionObject)
tx = decodeFromString(x, TransactionObject)
return tx

server.rpc("eth_getTransactionByBlockHashAndIndex") do(x: JsonString, data: Hash256, quantity: Quantity) -> TransactionObject:
var tx: TransactionObject
if x != "-1".JsonString:
tx = JrpcConv.decode(x.string, TransactionObject)
tx = decodeFromString(x, TransactionObject)
return tx

server.rpc("eth_getTransactionCount") do(x: JsonString, data: Address, blockId: RtBlockIdentifier) -> Quantity:
if x != "-1".JsonString:
result = JrpcConv.decode(x.string, Quantity)
result = decodeFromString(x, Quantity)

server.rpc("eth_getStorageAt") do(x: JsonString, data: Address, slot: UInt256, blockId: RtBlockIdentifier) -> FixedBytes[32]:
if x != "-1".JsonString:
result = JrpcConv.decode(x.string, FixedBytes[32])
result = decodeFromString(x, FixedBytes[32])

server.rpc("eth_getProof") do(x: JsonString, address: Address, slots: seq[UInt256], blockId: RtBlockIdentifier) -> ProofResponse:
var p: ProofResponse
if x != "-1".JsonString:
p = JrpcConv.decode(x.string, ProofResponse)
p = decodeFromString(x, ProofResponse)
return p

server.rpc("eth_getCode") do(x: JsonString, data: Address, blockId: RtBlockIdentifier) -> seq[byte]:
if x != "-1".JsonString:
result = JrpcConv.decode(x.string, seq[byte])
result = decodeFromString(x, seq[byte])

server.rpc("eth_getBlockTransactionCountByNumber") do(x: JsonString, blockId: RtBlockIdentifier) -> Quantity:
if x != "-1".JsonString:
result = JrpcConv.decode(x.string, Quantity)
result = decodeFromString(x, Quantity)

server.rpc("eth_getBlockTransactionCountByHash") do(x: JsonString, data: BlockHash) -> Quantity:
if x != "-1".JsonString:
result = JrpcConv.decode(x.string, Quantity)
result = decodeFromString(x, Quantity)

when NimMajor >= 2:
server.rpc("eth_getBlockReceipts") do(x: JsonString, blockId: RtBlockIdentifier) -> JsonString:
# TODO: cannot prove obj is not nil
return x
let jsonBytes = JrpcConv.decode(x.string, string)
return jsonBytes.JsonString
else:
server.rpc("eth_getBlockReceipts") do(x: JsonString, blockId: RtBlockIdentifier) -> Option[seq[ReceiptObject]]:
if x == "null".JsonString:
var n: Option[seq[ReceiptObject]]
return n
server.rpc("eth_getBlockReceipts") do(x: JsonString, blockId: RtBlockIdentifier) -> Option[seq[ReceiptObject]]:
if x != "-1".JsonString:
let r = JrpcConv.decode(x.string, seq[ReceiptObject])
return some(r)
let r = decodeFromString(x, Option[seq[ReceiptObject]])
return r

server.rpc("eth_getBlockByNumber") do(x: JsonString, blockId: RtBlockIdentifier, fullTransactions: bool) -> BlockObject:
var blk: BlockObject
if x != "-1".JsonString:
blk = JrpcConv.decode(x.string, BlockObject)
blk = decodeFromString(x, BlockObject)
return blk

server.rpc("eth_getBlockByHash") do(x: JsonString, data: BlockHash, fullTransactions: bool) -> BlockObject:
var blk: BlockObject
if x != "-1".JsonString:
blk = JrpcConv.decode(x.string, BlockObject)
blk = decodeFromString(x, BlockObject)
return blk

server.rpc("eth_getBalance") do(x: JsonString, data: Address, blockId: RtBlockIdentifier) -> UInt256:
if x != "-1".JsonString:
result = JrpcConv.decode(x.string, UInt256)
result = decodeFromString(x, UInt256)

server.rpc("eth_feeHistory") do(x: JsonString, blockCount: Quantity, newestBlock: RtBlockIdentifier, rewardPercentiles: Option[seq[float64]]) -> FeeHistoryResult:
var fh: FeeHistoryResult
if x != "-1".JsonString:
fh = JrpcConv.decode(x.string, FeeHistoryResult)
fh = decodeFromString(x, FeeHistoryResult)
return fh

server.rpc("eth_estimateGas") do(x: JsonString, call: EthCall) -> Quantity:
if x != "-1".JsonString:
result = JrpcConv.decode(x.string, Quantity)
result = decodeFromString(x, Quantity)

server.rpc("eth_createAccessList") do(x: JsonString, call: EthCall, blockId: RtBlockIdentifier) -> AccessListResult:
var z: AccessListResult
if x != "-1".JsonString:
z = JrpcConv.decode(x.string, AccessListResult)
z = decodeFromString(x, AccessListResult)
return z

server.rpc("eth_chainId") do(x: JsonString, ) -> Quantity:
if x != "-1".JsonString:
result = JrpcConv.decode(x.string, Quantity)
result = decodeFromString(x, Quantity)

server.rpc("eth_call") do(x: JsonString, call: EthCall, blockId: RtBlockIdentifier) -> seq[byte]:
if x != "-1".JsonString:
result = JrpcConv.decode(x.string, seq[byte])
result = decodeFromString(x, seq[byte])

server.rpc("eth_blockNumber") do(x: JsonString) -> Quantity:
if x != "-1".JsonString:
result = JrpcConv.decode(x.string, Quantity)
result = decodeFromString(x, Quantity)

server.rpc("debug_getRawTransaction") do(x: JsonString, data: TxHash) -> RlpEncodedBytes:
var res: seq[byte]
if x != "-1".JsonString:
res = JrpcConv.decode(x.string, seq[byte])
res = decodeFromString(x, seq[byte])
return res.RlpEncodedBytes

server.rpc("debug_getRawReceipts") do(x: JsonString, blockId: RtBlockIdentifier) -> seq[RlpEncodedBytes]:
var res: seq[RlpEncodedBytes]
if x != "-1".JsonString:
res = JrpcConv.decode(x.string, seq[RlpEncodedBytes])
res = decodeFromString(x, seq[RlpEncodedBytes])
return res

server.rpc("debug_getRawHeader") do(x: JsonString, blockId: RtBlockIdentifier) -> RlpEncodedBytes:
var res: seq[byte]
if x != "-1".JsonString:
res = JrpcConv.decode(x.string, seq[byte])
res = decodeFromString(x, seq[byte])
return res.RlpEncodedBytes

server.rpc("debug_getRawBlock") do(x: JsonString, blockId: RtBlockIdentifier) -> RlpEncodedBytes:
var res: seq[byte]
if x != "-1".JsonString:
res = JrpcConv.decode(x.string, seq[byte])
res = decodeFromString(x, seq[byte])
return res.RlpEncodedBytes
4 changes: 1 addition & 3 deletions tests/test_deposit_contract.nim
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ suite "Deposit contract":

var ns = web3.contractSender(DepositContract, contractAddress)

let notifFut = newFuture[void]()
var notificationsReceived = 0

var pk = DynamicBytes[0, 48].fromHex("0xa20469ec49fdfdcaaa68c470642feb9d7d0e612026c6243928772a7277bde77d081e63cc9034cee9eb5abee66ea12861")
var cr = DynamicBytes[0, 32].fromHex("0x0012c7b99594801d513ae92396379e5ffcf60e23127cbcabb166db28586f01aa")
var sig = DynamicBytes[0, 96].fromHex("0x81c7536816ff1e4ca6a52b5e853c19e9def14c01b07f0e1ac9b1e8a198bf78c98e98e74465d13e2978ae720dcab0a7da10fa56221477773ad7c3f82317c3e0f12a76f47332b9b5350b655ae196db33221f64183d1da3784f608001489ff523d5")
Expand All @@ -65,6 +62,7 @@ suite "Deposit contract":
do (err: CatchableError):
echo "Error from DepositEvent subscription: ", err.msg

discard s
discard await ns.deposit(pk, cr, sig, dataRoot).send(value = 32.u256.ethToWei, gasPrice=gasPrice)

await fut
Expand Down
7 changes: 4 additions & 3 deletions tests/test_execution_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ proc callWithParams(client: RpcClient, data: TestData): Future[bool] {.async.} =
try:
var params = data.input.params
if data.output.result.string.len > 0:
params.positional.insert(data.output.result, 0)
let jsonBytes = JrpcConv.encode(data.output.result.string)
params.positional.insert(jsonBytes.JsonString, 0)
else:
params.positional.insert("-1".JsonString, 0)

Expand Down Expand Up @@ -92,7 +93,7 @@ suite "Ethereum execution api":

for idx, item in testCases:
let input = item.input
let methodName = input.`method`
let methodName = input.`method`

test methodName:
let (_, fileName, ext) = splitFile(item.file)
Expand All @@ -105,6 +106,6 @@ suite "Ethereum execution api":
else:
check response
waitFor client.close()

waitFor srv.stop()
waitFor srv.closeWait()
10 changes: 9 additions & 1 deletion tests/test_null_conversion.nim
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ suite "Null conversion":
var resForkchoiceUpdatedResponse: ForkchoiceUpdatedResponse
var resTransitionConfigurationV1: TransitionConfigurationV1

for item in @["null", "\"\"", "\"0x\"", "\"0x_\"", ""]:
for item in @["\"\"", "\"0x\"", "\"0x_\"", ""]:
template format(str: string): string =
str.replace("{item}", item)

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

template setNull(str: string): string =
str.replace("{item}", "null")

should_not_error(payloadAttributesV1.setNull(), resPayloadAttributesV1)
should_not_error(forkchoiceStateV1.setNull(), resForkchoiceStateV1)
should_not_error(forkchoiceUpdatedResponse.setNull(), resForkchoiceUpdatedResponse)
should_not_error(transitionConfigurationV1.setNull(), 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 Down
16 changes: 9 additions & 7 deletions tests/test_string_decoder.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import
import
pkg/unittest2,
stew/byteutils,
../web3/encoding,
../web3/primitives

Expand All @@ -9,13 +10,14 @@ type
SignatureBytes = DynamicBytes[96, 96]
Int64LeBytes = DynamicBytes[8, 8]

const logData = "00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000030b2f5263a3454de3a9116b0edaa3cfbb2795a99482ee268b7aed5b15b532d9b20c34b67c82877ba1326326f3ae6cc5ad3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020010000000000000000000000a3f7076718fa4fed91b5830a45489053eb367afb0000000000000000000000000000000000000000000000000000000000000008004059730700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000608ee47adfab0819ec0d9144a3f76c0a663f8d7d66a2cfd994eb5d23d15d779430ec85ecd3c91bf7b4d229a9c4a8bee83e0096913d0b05525307114d75fa9baf79b4634ba80d3d262dd769c66fb6af25bff30a5ce04600940d2271278fad5b3096000000000000000000000000000000000000000000000000000000000000000802f3000000000000000000000000000000000000000000000000000000000000"
const input = "00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000030b2f5263a3454de3a9116b0edaa3cfbb2795a99482ee268b7aed5b15b532d9b20c34b67c82877ba1326326f3ae6cc5ad3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020010000000000000000000000a3f7076718fa4fed91b5830a45489053eb367afb0000000000000000000000000000000000000000000000000000000000000008004059730700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000608ee47adfab0819ec0d9144a3f76c0a663f8d7d66a2cfd994eb5d23d15d779430ec85ecd3c91bf7b4d229a9c4a8bee83e0096913d0b05525307114d75fa9baf79b4634ba80d3d262dd769c66fb6af25bff30a5ce04600940d2271278fad5b3096000000000000000000000000000000000000000000000000000000000000000802f3000000000000000000000000000000000000000000000000000000000000"

template init[N: static int](T: type DynamicBytes[N, N]): T =
T newSeq[byte](N)

suite "String decoders":
test "Log message decoding":
let logData = hexToSeqByte(input)
var
pubkey = init PubKeyBytes
withdrawalCredentials = init WithdrawalCredentialsBytes
Expand All @@ -24,11 +26,11 @@ suite "String decoders":
index = init Int64LeBytes

var offset = 0
offset += decode(logData, offset, pubkey)
offset += decode(logData, offset, withdrawalCredentials)
offset += decode(logData, offset, amount)
offset += decode(logData, offset, signature)
offset += decode(logData, offset, index)
offset += decode(logData, 0, offset, pubkey)
offset += decode(logData, 0, offset, withdrawalCredentials)
offset += decode(logData, 0, offset, amount)
offset += decode(logData, 0, offset, signature)
offset += decode(logData, 0, offset, index)

assert($pubkey == "0xb2f5263a3454de3a9116b0edaa3cfbb2795a99482ee268b7aed5b15b532d9b20c34b67c82877ba1326326f3ae6cc5ad3")
assert($withdrawalCredentials == "0x010000000000000000000000a3f7076718fa4fed91b5830a45489053eb367afb")
Expand Down
3 changes: 2 additions & 1 deletion web3.nim
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ proc handleSubscriptionNotification(w: Web3, params: RequestParamsRx):
s.pendingEvents.add(par)

ok()

proc newWeb3*(provider: RpcClient): Web3 =
result = Web3(provider: provider)
result.subscriptions = initTable[string, Subscription]()
Expand Down Expand Up @@ -443,6 +443,7 @@ proc createMutableContractInvocation*(sender: Web3AsyncSenderImpl, ReturnType: t
assert(sender.gas > 0)
let h = await sendData(sender.web3, sender.contractAddress, sender.defaultAccount, data, sender.value, sender.gas, sender.gasPrice, sender.chainId)
let receipt = await sender.web3.getMinedTransactionReceipt(h)
discard receipt

proc createImmutableContractInvocation*(sender: Web3AsyncSenderImpl, ReturnType: typedesc, data: sink seq[byte]): Future[ReturnType] {.async.} =
let response = await callAux(sender.web3, sender.contractAddress, sender.defaultAccount, data, sender.value, sender.gas, sender.blockNumber)
Expand Down

0 comments on commit baff215

Please sign in to comment.