mirror of
https://github.com/logos-storage/nim-json-rpc.git
synced 2026-01-05 23:23:12 +00:00
enable styleCheck:usages (#135)
This commit is contained in:
parent
b80313bfed
commit
2e3d22b9e2
@ -20,7 +20,13 @@ requires "nim >= 1.2.0",
|
|||||||
proc buildBinary(name: string, srcDir = "./", params = "", cmdParams = "") =
|
proc buildBinary(name: string, srcDir = "./", params = "", cmdParams = "") =
|
||||||
if not dirExists "build":
|
if not dirExists "build":
|
||||||
mkDir "build"
|
mkDir "build"
|
||||||
exec "nim " & getEnv("TEST_LANG", "c") & " " & getEnv("NIMFLAGS") & " -r -f --skipUserCfg:on --skipParentCfg:on --verbosity:0 --hints:off --debuginfo --path:'.' --threads:on -d:chronicles_log_level=ERROR --out:./build/" & name & " " & params & " " & srcDir & name & ".nim" & " " & cmdParams
|
exec "nim " & getEnv("TEST_LANG", "c") & " " & getEnv("NIMFLAGS") &
|
||||||
|
" -r -f --skipUserCfg:on --skipParentCfg:on --verbosity:0" &
|
||||||
|
" --debuginfo --path:'.' --threads:on -d:chronicles_log_level=ERROR" &
|
||||||
|
" --styleCheck:usages --styleCheck:hint" &
|
||||||
|
" --hint[XDeclaredButNotUsed]:off --hint[Processing]:off " &
|
||||||
|
" --out:./build/" & name & " " & params & " " & srcDir & name & ".nim" &
|
||||||
|
" " & cmdParams
|
||||||
|
|
||||||
task test, "run tests":
|
task test, "run tests":
|
||||||
buildBinary "all", "tests/",
|
buildBinary "all", "tests/",
|
||||||
|
|||||||
@ -87,7 +87,7 @@ proc createRpcProc(procName, parameters, callBody: NimNode): NimNode =
|
|||||||
# make proc async
|
# make proc async
|
||||||
result.addPragma ident"async"
|
result.addPragma ident"async"
|
||||||
# export this proc
|
# export this proc
|
||||||
result[0] = nnkPostFix.newTree(ident"*", newIdentNode($procName))
|
result[0] = nnkPostfix.newTree(ident"*", newIdentNode($procName))
|
||||||
|
|
||||||
proc toJsonArray(parameters: NimNode): NimNode =
|
proc toJsonArray(parameters: NimNode): NimNode =
|
||||||
# outputs an array of jsonified parameters
|
# outputs an array of jsonified parameters
|
||||||
|
|||||||
@ -79,7 +79,7 @@ proc processData(client: RpcWebSocketClient) {.async.} =
|
|||||||
else:
|
else:
|
||||||
let ws = client.transport
|
let ws = client.transport
|
||||||
try:
|
try:
|
||||||
while ws.readystate != ReadyState.Closed:
|
while ws.readyState != ReadyState.Closed:
|
||||||
var value = await ws.recvMsg()
|
var value = await ws.recvMsg()
|
||||||
|
|
||||||
if value.len == 0:
|
if value.len == 0:
|
||||||
|
|||||||
@ -69,7 +69,7 @@ proc fromJson*(n: JsonNode, argName: string, result: var int) =
|
|||||||
proc fromJson*[T: ref object](n: JsonNode, argName: string, result: var T) =
|
proc fromJson*[T: ref object](n: JsonNode, argName: string, result: var T) =
|
||||||
n.kind.expect(JObject, argName)
|
n.kind.expect(JObject, argName)
|
||||||
result = new T
|
result = new T
|
||||||
for k, v in fieldpairs(result[]):
|
for k, v in fieldPairs(result[]):
|
||||||
fromJson(n[k], k, v)
|
fromJson(n[k], k, v)
|
||||||
|
|
||||||
proc fromJson*(n: JsonNode, argName: string, result: var int64) =
|
proc fromJson*(n: JsonNode, argName: string, result: var int64) =
|
||||||
@ -165,7 +165,7 @@ iterator paramsIter(params: NimNode): tuple[name, ntype: NimNode] =
|
|||||||
yield (arg[j], argType)
|
yield (arg[j], argType)
|
||||||
|
|
||||||
iterator paramsRevIter(params: NimNode): tuple[name, ntype: NimNode] =
|
iterator paramsRevIter(params: NimNode): tuple[name, ntype: NimNode] =
|
||||||
for i in countDown(params.len-1,1):
|
for i in countdown(params.len-1,1):
|
||||||
let arg = params[i]
|
let arg = params[i]
|
||||||
let argType = arg[^2]
|
let argType = arg[^2]
|
||||||
for j in 0 ..< arg.len-2:
|
for j in 0 ..< arg.len-2:
|
||||||
|
|||||||
@ -43,7 +43,7 @@ proc addEthRpcs*(server: RpcServer) =
|
|||||||
var rawData: seq[byte]
|
var rawData: seq[byte]
|
||||||
rawData = nimcrypto.fromHex(data.string)
|
rawData = nimcrypto.fromHex(data.string)
|
||||||
# data will have 0x prefix
|
# data will have 0x prefix
|
||||||
result = hexDataStr "0x" & $keccak_256.digest(rawData)
|
result = hexDataStr "0x" & $keccak256.digest(rawData)
|
||||||
|
|
||||||
server.rpc("net_version") do() -> string:
|
server.rpc("net_version") do() -> string:
|
||||||
## Returns string of the current network id:
|
## Returns string of the current network id:
|
||||||
|
|||||||
@ -13,9 +13,9 @@ type
|
|||||||
gasPrice*: int # (optional, default: To-Be-Determined) integer of the gasPrice used for each paid gas.
|
gasPrice*: int # (optional, default: To-Be-Determined) integer of the gasPrice used for each paid gas.
|
||||||
value*: int # (optional) integer of the value sent with this transaction.
|
value*: int # (optional) integer of the value sent with this transaction.
|
||||||
data*: int # the compiled code of a contract OR the hash of the invoked method signature and encoded parameters. For details see Ethereum Contract ABI.
|
data*: int # the compiled code of a contract OR the hash of the invoked method signature and encoded parameters. For details see Ethereum Contract ABI.
|
||||||
nonce*: int # (optional) integer of a nonce. This allows to overwrite your own pending transactions that use the same nonce
|
nonce*: int # (optional) integer of a nonce. This allows to overwrite your own pending transactions that use the same nonce
|
||||||
|
|
||||||
EthCall* = object
|
EthCall* = object
|
||||||
source*: array[20, byte] # (optional) The address the transaction is send from.
|
source*: array[20, byte] # (optional) The address the transaction is send from.
|
||||||
to*: array[20, byte] # The address the transaction is directed to.
|
to*: array[20, byte] # The address the transaction is directed to.
|
||||||
gas*: int # (optional) Integer of the gas provided for the transaction execution. eth_call consumes zero gas, but this parameter may be needed by some executions.
|
gas*: int # (optional) Integer of the gas provided for the transaction execution. eth_call consumes zero gas, but this parameter may be needed by some executions.
|
||||||
@ -42,8 +42,8 @@ type
|
|||||||
gasLimit*: int # the maximum gas allowed in this block.
|
gasLimit*: int # the maximum gas allowed in this block.
|
||||||
gasUsed*: int # the total used gas by all transactions in this block.
|
gasUsed*: int # the total used gas by all transactions in this block.
|
||||||
timestamp*: int # the unix timestamp for when the block was collated.
|
timestamp*: int # the unix timestamp for when the block was collated.
|
||||||
transactions*: seq[Uint256] # list of transaction objects, or 32 Bytes transaction hashes depending on the last given parameter.
|
transactions*: seq[UInt256] # list of transaction objects, or 32 Bytes transaction hashes depending on the last given parameter.
|
||||||
uncles*: seq[Uint256] # list of uncle hashes.
|
uncles*: seq[UInt256] # list of uncle hashes.
|
||||||
|
|
||||||
TransactionObject* = object # A transaction object, or null when no transaction was found:
|
TransactionObject* = object # A transaction object, or null when no transaction was found:
|
||||||
hash*: UInt256 # hash of the transaction.
|
hash*: UInt256 # hash of the transaction.
|
||||||
@ -70,7 +70,7 @@ type
|
|||||||
contractAddress*: array[20, byte] # the contract address created, if the transaction was a contract creation, otherwise null.
|
contractAddress*: array[20, byte] # the contract address created, if the transaction was a contract creation, otherwise null.
|
||||||
logs*: seq[string] # TODO: See Wiki for details. list of log objects, which this transaction generated.
|
logs*: seq[string] # TODO: See Wiki for details. list of log objects, which this transaction generated.
|
||||||
logsBloom*: array[256, byte] # bloom filter for light clients to quickly retrieve related logs.
|
logsBloom*: array[256, byte] # bloom filter for light clients to quickly retrieve related logs.
|
||||||
# TODO:
|
# TODO:
|
||||||
#case kind*: ReceiptKind
|
#case kind*: ReceiptKind
|
||||||
#of rkRoot: root*: UInt256 # post-transaction stateroot (pre Byzantium).
|
#of rkRoot: root*: UInt256 # post-transaction stateroot (pre Byzantium).
|
||||||
#of rkStatus: status*: int # 1 = success, 0 = failure.
|
#of rkStatus: status*: int # 1 = success, 0 = failure.
|
||||||
|
|||||||
@ -22,7 +22,7 @@ func rpcDynamicName(name: string): string =
|
|||||||
|
|
||||||
## Create custom RPC with StUint input parameter
|
## Create custom RPC with StUint input parameter
|
||||||
server.rpc(rpcDynamicName "uint256Param") do(i: UInt256):
|
server.rpc(rpcDynamicName "uint256Param") do(i: UInt256):
|
||||||
let r = i + 1.stUint(256)
|
let r = i + 1.stuint(256)
|
||||||
return %r
|
return %r
|
||||||
|
|
||||||
## Create custom RPC with StUInt return parameter
|
## Create custom RPC with StUInt return parameter
|
||||||
|
|||||||
@ -153,39 +153,39 @@ suite "Server types":
|
|||||||
check r == inp
|
check r == inp
|
||||||
|
|
||||||
test "Array parameters":
|
test "Array parameters":
|
||||||
let r1 = waitfor s.executeMethod("rpc.arrayParam", %[%[1, 2, 3], %"hello"])
|
let r1 = waitFor s.executeMethod("rpc.arrayParam", %[%[1, 2, 3], %"hello"])
|
||||||
var ckR1 = %[1, 2, 3, 0, 0, 0]
|
var ckR1 = %[1, 2, 3, 0, 0, 0]
|
||||||
ckR1.elems.add %"hello"
|
ckR1.elems.add %"hello"
|
||||||
check r1 == ckR1
|
check r1 == ckR1
|
||||||
|
|
||||||
test "Seq parameters":
|
test "Seq parameters":
|
||||||
let r2 = waitfor s.executeMethod("rpc.seqParam", %[%"abc", %[1, 2, 3, 4, 5]])
|
let r2 = waitFor s.executeMethod("rpc.seqParam", %[%"abc", %[1, 2, 3, 4, 5]])
|
||||||
var ckR2 = %["abc"]
|
var ckR2 = %["abc"]
|
||||||
for i in 0..4: ckR2.add %(i + 1)
|
for i in 0..4: ckR2.add %(i + 1)
|
||||||
check r2 == ckR2
|
check r2 == ckR2
|
||||||
|
|
||||||
test "Object parameters":
|
test "Object parameters":
|
||||||
let r = waitfor s.executeMethod("rpc.objParam", %[%"abc", testObj])
|
let r = waitFor s.executeMethod("rpc.objParam", %[%"abc", testObj])
|
||||||
check r == testObj
|
check r == testObj
|
||||||
|
|
||||||
test "Simple return types":
|
test "Simple return types":
|
||||||
let
|
let
|
||||||
inp = %99
|
inp = %99
|
||||||
r1 = waitfor s.executeMethod("rpc.returnTypeSimple", %[%inp])
|
r1 = waitFor s.executeMethod("rpc.returnTypeSimple", %[%inp])
|
||||||
check r1 == inp
|
check r1 == inp
|
||||||
|
|
||||||
test "Complex return types":
|
test "Complex return types":
|
||||||
let
|
let
|
||||||
inp = 99
|
inp = 99
|
||||||
r1 = waitfor s.executeMethod("rpc.returnTypeComplex", %[%inp])
|
r1 = waitFor s.executeMethod("rpc.returnTypeComplex", %[%inp])
|
||||||
check r1 == %*{"x": %[1, inp, 3], "y": "test"}
|
check r1 == %*{"x": %[1, inp, 3], "y": "test"}
|
||||||
|
|
||||||
test "Option types":
|
test "Option types":
|
||||||
let
|
let
|
||||||
inp1 = MyOptional(maybeInt: some(75))
|
inp1 = MyOptional(maybeInt: some(75))
|
||||||
inp2 = MyOptional()
|
inp2 = MyOptional()
|
||||||
r1 = waitfor s.executeMethod("rpc.optional", %[%inp1])
|
r1 = waitFor s.executeMethod("rpc.optional", %[%inp1])
|
||||||
r2 = waitfor s.executeMethod("rpc.optional", %[%inp2])
|
r2 = waitFor s.executeMethod("rpc.optional", %[%inp2])
|
||||||
check r1 == %inp1
|
check r1 == %inp1
|
||||||
check r2 == %inp2
|
check r2 == %inp2
|
||||||
|
|
||||||
@ -196,19 +196,19 @@ suite "Server types":
|
|||||||
test "Runtime errors":
|
test "Runtime errors":
|
||||||
expect ValueError:
|
expect ValueError:
|
||||||
# root param not array
|
# root param not array
|
||||||
discard waitfor s.executeMethod("rpc.arrayParam", %"test")
|
discard waitFor s.executeMethod("rpc.arrayParam", %"test")
|
||||||
expect ValueError:
|
expect ValueError:
|
||||||
# too big for array
|
# too big for array
|
||||||
discard waitfor s.executeMethod("rpc.arrayParam", %[%[0, 1, 2, 3, 4, 5, 6], %"hello"])
|
discard waitFor s.executeMethod("rpc.arrayParam", %[%[0, 1, 2, 3, 4, 5, 6], %"hello"])
|
||||||
expect ValueError:
|
expect ValueError:
|
||||||
# wrong sub parameter type
|
# wrong sub parameter type
|
||||||
discard waitfor s.executeMethod("rpc.arrayParam", %[%"test", %"hello"])
|
discard waitFor s.executeMethod("rpc.arrayParam", %[%"test", %"hello"])
|
||||||
expect ValueError:
|
expect ValueError:
|
||||||
# wrong param type
|
# wrong param type
|
||||||
discard waitFor s.executeMethod("rpc.differentParams", %[%"abc", %1])
|
discard waitFor s.executeMethod("rpc.differentParams", %[%"abc", %1])
|
||||||
|
|
||||||
test "Multiple variables of one type":
|
test "Multiple variables of one type":
|
||||||
let r = waitfor s.executeMethod("rpc.multiVarsOfOneType", %[%"hello", %"world"])
|
let r = waitFor s.executeMethod("rpc.multiVarsOfOneType", %[%"hello", %"world"])
|
||||||
check r == %"hello world"
|
check r == %"hello world"
|
||||||
|
|
||||||
test "Optional arg":
|
test "Optional arg":
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user