Style fixes according to --styleCheck:usages (#452)

Currently only setting `--styleCheck:hint` as there are some
dependency fixes required and the compiler seems to trip over the
findnode MessageKind, findnode Message field and the findNode
proc. Also over protocol.Protocol usage.
This commit is contained in:
Kim De Mey 2021-12-20 13:14:50 +01:00 committed by GitHub
parent 0cfe7df817
commit 2c236f6495
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
65 changed files with 527 additions and 527 deletions

View File

@ -67,7 +67,7 @@ the network. To start the connection process, call `node.connectToNetwork`:
``` nim ``` nim
proc connectToNetwork*(node: var EthereumNode, proc connectToNetwork*(node: var EthereumNode,
bootstrapNodes: openarray[ENode], bootstrapNodes: openArray[ENode],
startListening = true, startListening = true,
enableDiscovery = true) enableDiscovery = true)
``` ```
@ -106,7 +106,7 @@ p2pProtocol DevP2P(version = 0, rlpxName = "p2p"):
proc hello(peer: Peer, proc hello(peer: Peer,
version: uint, version: uint,
clientId: string, clientId: string,
capabilities: openarray[Capability], capabilities: openArray[Capability],
listenPort: uint, listenPort: uint,
nodeId: P2PNodeId) = nodeId: P2PNodeId) =
peer.id = nodeId peer.id = nodeId
@ -195,7 +195,7 @@ There are few things to note in the above example:
2. Each message defined in the protocol received a corresponding type name, 2. Each message defined in the protocol received a corresponding type name,
matching the message name (e.g. `p2p.hello`). This type will have fields matching the message name (e.g. `p2p.hello`). This type will have fields
matching the parameter names of the message. If the messages has `openarray` matching the parameter names of the message. If the messages has `openArray`
params, these will be remapped to `seq` types. params, these will be remapped to `seq` types.
If the designated messages also has an attached handler, the future returned If the designated messages also has an attached handler, the future returned
@ -219,8 +219,8 @@ p2pProtocol les(version = 2):
... ...
requestResponse: requestResponse:
proc getProofs(p: Peer, proofs: openarray[ProofRequest]) proc getProofs(p: Peer, proofs: openArray[ProofRequest])
proc proofs(p: Peer, BV: uint, proofs: openarray[Blob]) proc proofs(p: Peer, BV: uint, proofs: openArray[Blob])
... ...
``` ```

View File

@ -19,7 +19,8 @@ requires "nim >= 1.2.0",
"testutils", "testutils",
"unittest2" "unittest2"
var commonParams = " --verbosity:0 --hints:off --skipUserCfg:on --warning[ObservableStores]:off " & let commonParams = " --verbosity:0 --hints:off --skipUserCfg:on " &
"--warning[ObservableStores]:off --styleCheck:usages --styleCheck:hint " &
getEnv("NIMFLAGS") & " " getEnv("NIMFLAGS") & " "
proc runTest(path: string, release: bool = true, chronosStrict = true) = proc runTest(path: string, release: bool = true, chronosStrict = true) =

View File

@ -28,12 +28,12 @@ proc init*(_: type BloomFilter, h: MDigest[256]): BloomFilter =
# TODO: The following 2 procs should be one genric, but it doesn't compile. Nim bug? # TODO: The following 2 procs should be one genric, but it doesn't compile. Nim bug?
proc incl*(f: var BloomFilter, v: string) = f.incl(keccak256.digest(v)) proc incl*(f: var BloomFilter, v: string) = f.incl(keccak256.digest(v))
proc incl*(f: var BloomFilter, v: openarray[byte]) = f.incl(keccak256.digest(v)) proc incl*(f: var BloomFilter, v: openArray[byte]) = f.incl(keccak256.digest(v))
proc contains*(f: BloomFilter, h: MDigest[256]): bool = proc contains*(f: BloomFilter, h: MDigest[256]): bool =
for bits in bloomBits(h): for bits in bloomBits(h):
if (f.value and bits).isZero: return false if (f.value and bits).isZero: return false
return true return true
template contains*[T](f: BloomFilter, v: openarray[T]): bool = template contains*[T](f: BloomFilter, v: openArray[T]): bool =
f.contains(keccak256.digest(v)) f.contains(keccak256.digest(v))

View File

@ -304,13 +304,13 @@ proc append*(rlpWriter: var RlpWriter, value: StUint) =
else: else:
rlpWriter.append(value.truncate(int)) rlpWriter.append(value.truncate(int))
proc read*(rlp: var Rlp, T: type Stint): T {.inline.} = proc read*(rlp: var Rlp, T: type StInt): T {.inline.} =
# The Ethereum Yellow Paper defines the RLP serialization only # The Ethereum Yellow Paper defines the RLP serialization only
# for unsigned integers: # for unsigned integers:
{.fatal: "RLP serialization of signed integers is not allowed".} {.fatal: "RLP serialization of signed integers is not allowed".}
discard discard
proc append*(rlpWriter: var RlpWriter, value: Stint) = proc append*(rlpWriter: var RlpWriter, value: StInt) =
# The Ethereum Yellow Paper defines the RLP serialization only # The Ethereum Yellow Paper defines the RLP serialization only
# for unsigned integers: # for unsigned integers:
{.fatal: "RLP serialization of signed integers is not allowed".} {.fatal: "RLP serialization of signed integers is not allowed".}
@ -671,7 +671,7 @@ method getCodeByHash*(db: AbstractChainDB, hash: KeccakHash): Blob {.base, gcsaf
method getSetting*(db: AbstractChainDB, key: string): seq[byte] {.base, gcsafe.} = method getSetting*(db: AbstractChainDB, key: string): seq[byte] {.base, gcsafe.} =
notImplemented() notImplemented()
method setSetting*(db: AbstractChainDB, key: string, val: openarray[byte]) {.base, gcsafe.} = method setSetting*(db: AbstractChainDB, key: string, val: openArray[byte]) {.base, gcsafe.} =
notImplemented() notImplemented()
method getHeaderProof*(db: AbstractChainDB, req: ProofRequest): Blob {.base, gcsafe.} = method getHeaderProof*(db: AbstractChainDB, req: ProofRequest): Blob {.base, gcsafe.} =
@ -686,10 +686,10 @@ method getHelperTrieProof*(db: AbstractChainDB, req: HelperTrieProofRequest): Bl
method getTransactionStatus*(db: AbstractChainDB, txHash: KeccakHash): TransactionStatusMsg {.base, gcsafe.} = method getTransactionStatus*(db: AbstractChainDB, txHash: KeccakHash): TransactionStatusMsg {.base, gcsafe.} =
notImplemented() notImplemented()
method addTransactions*(db: AbstractChainDB, transactions: openarray[Transaction]) {.base, gcsafe.} = method addTransactions*(db: AbstractChainDB, transactions: openArray[Transaction]) {.base, gcsafe.} =
notImplemented() notImplemented()
method persistBlocks*(db: AbstractChainDB, headers: openarray[BlockHeader], bodies: openarray[BlockBody]): ValidationResult {.base, gcsafe.} = method persistBlocks*(db: AbstractChainDB, headers: openArray[BlockHeader], bodies: openArray[BlockBody]): ValidationResult {.base, gcsafe.} =
notImplemented() notImplemented()
method getForkId*(db: AbstractChainDB, n: BlockNumber): ForkID {.base, gcsafe.} = method getForkId*(db: AbstractChainDB, n: BlockNumber): ForkID {.base, gcsafe.} =

View File

@ -13,19 +13,19 @@ type
RocksStoreRef* = ref object of RootObj RocksStoreRef* = ref object of RootObj
store: RocksDBInstance store: RocksDBInstance
proc get*(db: RocksStoreRef, key: openarray[byte], onData: kvstore.DataProc): KvResult[bool] = proc get*(db: RocksStoreRef, key: openArray[byte], onData: kvstore.DataProc): KvResult[bool] =
db.store.get(key, onData) db.store.get(key, onData)
proc find*(db: RocksStoreRef, prefix: openarray[byte], onFind: kvstore.KeyValueProc): KvResult[int] = proc find*(db: RocksStoreRef, prefix: openArray[byte], onFind: kvstore.KeyValueProc): KvResult[int] =
raiseAssert "Unimplemented" raiseAssert "Unimplemented"
proc put*(db: RocksStoreRef, key, value: openarray[byte]): KvResult[void] = proc put*(db: RocksStoreRef, key, value: openArray[byte]): KvResult[void] =
db.store.put(key, value) db.store.put(key, value)
proc contains*(db: RocksStoreRef, key: openarray[byte]): KvResult[bool] = proc contains*(db: RocksStoreRef, key: openArray[byte]): KvResult[bool] =
db.store.contains(key) db.store.contains(key)
proc del*(db: RocksStoreRef, key: openarray[byte]): KvResult[void] = proc del*(db: RocksStoreRef, key: openArray[byte]): KvResult[void] =
db.store.del(key) db.store.del(key)
proc close*(db: RocksStoreRef) = proc close*(db: RocksStoreRef) =

View File

@ -461,7 +461,7 @@ proc init*(
let let
name = name =
if inMemory: ":memory:" if inMemory: ":memory:"
else: basepath / name & ".sqlite3" else: basePath / name & ".sqlite3"
flags = flags =
# For some reason, opening multiple in-memory databases doesn't work if # For some reason, opening multiple in-memory databases doesn't work if
# one of them is read-only - for now, disable read-only mode for them # one of them is read-only - for now, disable read-only mode for them

View File

@ -221,8 +221,8 @@ proc deriveKey(password: string, salt: string,
proc encryptKey(seckey: PrivateKey, proc encryptKey(seckey: PrivateKey,
cryptkind: CryptKind, cryptkind: CryptKind,
key: openarray[byte], key: openArray[byte],
iv: openarray[byte]): KfResult[array[KeyLength, byte]] = iv: openArray[byte]): KfResult[array[KeyLength, byte]] =
if cryptkind == AES128CTR: if cryptkind == AES128CTR:
var crypttext: array[KeyLength, byte] var crypttext: array[KeyLength, byte]
var ctx: CTR[aes128] var ctx: CTR[aes128]
@ -233,10 +233,10 @@ proc encryptKey(seckey: PrivateKey,
else: else:
err(NotImplemented) err(NotImplemented)
proc decryptKey(ciphertext: openarray[byte], proc decryptKey(ciphertext: openArray[byte],
cryptkind: CryptKind, cryptkind: CryptKind,
key: openarray[byte], key: openArray[byte],
iv: openarray[byte]): KfResult[array[KeyLength, byte]] = iv: openArray[byte]): KfResult[array[KeyLength, byte]] =
if cryptkind == AES128CTR: if cryptkind == AES128CTR:
if len(iv) != aes128.sizeBlock: if len(iv) != aes128.sizeBlock:
return err(IncorrectIV) return err(IncorrectIV)
@ -295,7 +295,7 @@ proc decodeSalt(m: string): string =
else: else:
result = "" result = ""
proc compareMac(m1: openarray[byte], m2: openarray[byte]): bool = proc compareMac(m1: openArray[byte], m2: openArray[byte]): bool =
if len(m1) == len(m2) and len(m1) > 0: if len(m1) == len(m2) and len(m1) > 0:
result = equalMem(unsafeAddr m1[0], unsafeAddr m2[0], len(m1)) result = equalMem(unsafeAddr m1[0], unsafeAddr m2[0], len(m1))

View File

@ -39,7 +39,7 @@ logScope:
## Also does threadvar initialisation. ## Also does threadvar initialisation.
## Must be called before redirectPorts() in each thread. ## Must be called before redirectPorts() in each thread.
proc getExternalIP*(natStrategy: NatStrategy, quiet = false): Option[IpAddress] = proc getExternalIP*(natStrategy: NatStrategy, quiet = false): Option[IpAddress] =
var externalIP: IPAddress var externalIP: IpAddress
if natStrategy == NatAny or natStrategy == NatUpnp: if natStrategy == NatAny or natStrategy == NatUpnp:
if upnp == nil: if upnp == nil:
@ -245,7 +245,7 @@ proc setupNat*(natStrategy: NatStrategy, tcpPort, udpPort: Port,
## original ports as best effort. ## original ports as best effort.
## TODO: Allow for tcp or udp port mapping to be optional. ## TODO: Allow for tcp or udp port mapping to be optional.
let extIp = getExternalIP(natStrategy) let extIp = getExternalIP(natStrategy)
if extIP.isSome: if extIp.isSome:
let ip = ValidIpAddress.init(extIp.get) let ip = ValidIpAddress.init(extIp.get)
let extPorts = ({.gcsafe.}: let extPorts = ({.gcsafe.}:
redirectPorts(tcpPort = tcpPort, redirectPorts(tcpPort = tcpPort,
@ -308,7 +308,7 @@ proc setupAddress*(natConfig: NatConfig, bindIp: ValidIpAddress,
case natConfig.nat: case natConfig.nat:
of NatAny: of NatAny:
let bindAddress = initTAddress(bindIP, Port(0)) let bindAddress = initTAddress(bindIp, Port(0))
if bindAddress.isAnyLocal(): if bindAddress.isAnyLocal():
let ip = getRouteIpv4() let ip = getRouteIpv4()
if ip.isErr(): if ip.isErr():
@ -326,11 +326,11 @@ proc setupAddress*(natConfig: NatConfig, bindIp: ValidIpAddress,
return setupNat(natConfig.nat, tcpPort, udpPort, clientId) return setupNat(natConfig.nat, tcpPort, udpPort, clientId)
elif bindAddress.isPublic(): elif bindAddress.isPublic():
# When a specific public interface is provided, use that one. # When a specific public interface is provided, use that one.
return (some(ValidIpAddress.init(bindIP)), some(tcpPort), some(udpPort)) return (some(ValidIpAddress.init(bindIp)), some(tcpPort), some(udpPort))
else: else:
return setupNat(natConfig.nat, tcpPort, udpPort, clientId) return setupNat(natConfig.nat, tcpPort, udpPort, clientId)
of NatNone: of NatNone:
let bindAddress = initTAddress(bindIP, Port(0)) let bindAddress = initTAddress(bindIp, Port(0))
if bindAddress.isAnyLocal(): if bindAddress.isAnyLocal():
let ip = getRouteIpv4() let ip = getRouteIpv4()
if ip.isErr(): if ip.isErr():
@ -345,7 +345,7 @@ proc setupAddress*(natConfig: NatConfig, bindIp: ValidIpAddress,
return (none(ValidIpAddress), some(tcpPort), some(udpPort)) return (none(ValidIpAddress), some(tcpPort), some(udpPort))
elif bindAddress.isPublic(): elif bindAddress.isPublic():
# When a specific public interface is provided, use that one. # When a specific public interface is provided, use that one.
return (some(ValidIpAddress.init(bindIP)), some(tcpPort), some(udpPort)) return (some(ValidIpAddress.init(bindIp)), some(tcpPort), some(udpPort))
else: else:
error "Bind IP is not a public IP address. Should not use --nat:none option" error "Bind IP is not a public IP address. Should not use --nat:none option"
return (none(ValidIpAddress), some(tcpPort), some(udpPort)) return (none(ValidIpAddress), some(tcpPort), some(udpPort))

View File

@ -53,7 +53,7 @@ type
HandshakeFlag* = enum HandshakeFlag* = enum
Initiator, ## `Handshake` owner is connection initiator Initiator, ## `Handshake` owner is connection initiator
Responder, ## `Handshake` owner is connection responder Responder, ## `Handshake` owner is connection responder
Eip8 ## Flag indicates that EIP-8 handshake is used EIP8 ## Flag indicates that EIP-8 handshake is used
AuthError* = enum AuthError* = enum
EcdhError = "auth: ECDH shared secret could not be calculated" EcdhError = "auth: ECDH shared secret could not be calculated"
@ -127,7 +127,7 @@ proc tryInit*(
proc authMessagePreEIP8(h: var Handshake, proc authMessagePreEIP8(h: var Handshake,
rng: var BrHmacDrbgContext, rng: var BrHmacDrbgContext,
pubkey: PublicKey, pubkey: PublicKey,
output: var openarray[byte], output: var openArray[byte],
outlen: var int, outlen: var int,
flag: byte = 0, flag: byte = 0,
encrypt: bool = true): AuthResult[void] = encrypt: bool = true): AuthResult[void] =
@ -166,7 +166,7 @@ proc authMessagePreEIP8(h: var Handshake,
proc authMessageEIP8(h: var Handshake, proc authMessageEIP8(h: var Handshake,
rng: var BrHmacDrbgContext, rng: var BrHmacDrbgContext,
pubkey: PublicKey, pubkey: PublicKey,
output: var openarray[byte], output: var openArray[byte],
outlen: var int, outlen: var int,
flag: byte = 0, flag: byte = 0,
encrypt: bool = true): AuthResult[void] = encrypt: bool = true): AuthResult[void] =
@ -225,7 +225,7 @@ proc authMessageEIP8(h: var Handshake,
proc ackMessagePreEIP8(h: var Handshake, proc ackMessagePreEIP8(h: var Handshake,
rng: var BrHmacDrbgContext, rng: var BrHmacDrbgContext,
output: var openarray[byte], output: var openArray[byte],
outlen: var int, outlen: var int,
flag: byte = 0, flag: byte = 0,
encrypt: bool = true): AuthResult[void] = encrypt: bool = true): AuthResult[void] =
@ -252,7 +252,7 @@ proc ackMessagePreEIP8(h: var Handshake,
proc ackMessageEIP8(h: var Handshake, proc ackMessageEIP8(h: var Handshake,
rng: var BrHmacDrbgContext, rng: var BrHmacDrbgContext,
output: var openarray[byte], output: var openArray[byte],
outlen: var int, outlen: var int,
flag: byte = 0, flag: byte = 0,
encrypt: bool = true): AuthResult[void] = encrypt: bool = true): AuthResult[void] =
@ -314,7 +314,7 @@ template ackSize*(h: Handshake, encrypt: bool = true): int =
proc authMessage*(h: var Handshake, rng: var BrHmacDrbgContext, proc authMessage*(h: var Handshake, rng: var BrHmacDrbgContext,
pubkey: PublicKey, pubkey: PublicKey,
output: var openarray[byte], output: var openArray[byte],
outlen: var int, flag: byte = 0, outlen: var int, flag: byte = 0,
encrypt: bool = true): AuthResult[void] = encrypt: bool = true): AuthResult[void] =
## Create new AuthMessage for specified `pubkey` and store it inside ## Create new AuthMessage for specified `pubkey` and store it inside
@ -325,7 +325,7 @@ proc authMessage*(h: var Handshake, rng: var BrHmacDrbgContext,
authMessagePreEIP8(h, rng, pubkey, output, outlen, flag, encrypt) authMessagePreEIP8(h, rng, pubkey, output, outlen, flag, encrypt)
proc ackMessage*(h: var Handshake, rng: var BrHmacDrbgContext, proc ackMessage*(h: var Handshake, rng: var BrHmacDrbgContext,
output: var openarray[byte], output: var openArray[byte],
outlen: var int, flag: byte = 0, outlen: var int, flag: byte = 0,
encrypt: bool = true): AuthResult[void] = encrypt: bool = true): AuthResult[void] =
## Create new AckMessage and store it inside of `output`, size of generated ## Create new AckMessage and store it inside of `output`, size of generated
@ -335,7 +335,7 @@ proc ackMessage*(h: var Handshake, rng: var BrHmacDrbgContext,
else: else:
ackMessagePreEIP8(h, rng, output, outlen, flag, encrypt) ackMessagePreEIP8(h, rng, output, outlen, flag, encrypt)
proc decodeAuthMessageV4(h: var Handshake, m: openarray[byte]): AuthResult[void] = proc decodeAuthMessageV4(h: var Handshake, m: openArray[byte]): AuthResult[void] =
## Decodes V4 AuthMessage. ## Decodes V4 AuthMessage.
var var
buffer: array[PlainAuthMessageV4Length, byte] buffer: array[PlainAuthMessageV4Length, byte]
@ -361,7 +361,7 @@ proc decodeAuthMessageV4(h: var Handshake, m: openarray[byte]): AuthResult[void]
ok() ok()
proc decodeAuthMessageEip8(h: var Handshake, m: openarray[byte]): AuthResult[void] = proc decodeAuthMessageEIP8(h: var Handshake, m: openArray[byte]): AuthResult[void] =
## Decodes EIP-8 AuthMessage. ## Decodes EIP-8 AuthMessage.
let size = uint16.fromBytesBE(m) let size = uint16.fromBytesBE(m)
h.expectedLength = int(size) + 2 h.expectedLength = int(size) + 2
@ -408,7 +408,7 @@ proc decodeAuthMessageEip8(h: var Handshake, m: openarray[byte]): AuthResult[voi
except CatchableError: except CatchableError:
err(RlpError) err(RlpError)
proc decodeAckMessageEip8*(h: var Handshake, m: openarray[byte]): AuthResult[void] = proc decodeAckMessageEIP8*(h: var Handshake, m: openArray[byte]): AuthResult[void] =
## Decodes EIP-8 AckMessage. ## Decodes EIP-8 AckMessage.
let size = uint16.fromBytesBE(m) let size = uint16.fromBytesBE(m)
@ -442,7 +442,7 @@ proc decodeAckMessageEip8*(h: var Handshake, m: openarray[byte]): AuthResult[voi
except CatchableError: except CatchableError:
err(RlpError) err(RlpError)
proc decodeAckMessageV4(h: var Handshake, m: openarray[byte]): AuthResult[void] = proc decodeAckMessageV4(h: var Handshake, m: openArray[byte]): AuthResult[void] =
## Decodes V4 AckMessage. ## Decodes V4 AckMessage.
var var
buffer: array[PlainAckMessageV4Length, byte] buffer: array[PlainAckMessageV4Length, byte]
@ -457,7 +457,7 @@ proc decodeAckMessageV4(h: var Handshake, m: openarray[byte]): AuthResult[void]
ok() ok()
proc decodeAuthMessage*(h: var Handshake, input: openarray[byte]): AuthResult[void] = proc decodeAuthMessage*(h: var Handshake, input: openArray[byte]): AuthResult[void] =
## Decodes AuthMessage from `input`. ## Decodes AuthMessage from `input`.
if len(input) < AuthMessageV4Length: if len(input) < AuthMessageV4Length:
return err(IncompleteError) return err(IncompleteError)
@ -466,12 +466,12 @@ proc decodeAuthMessage*(h: var Handshake, input: openarray[byte]): AuthResult[vo
let res = h.decodeAuthMessageV4(input) let res = h.decodeAuthMessageV4(input)
if res.isOk(): return res if res.isOk(): return res
let res = h.decodeAuthMessageEip8(input) let res = h.decodeAuthMessageEIP8(input)
if res.isOk(): if res.isOk():
h.flags.incl(EIP8) h.flags.incl(EIP8)
res res
proc decodeAckMessage*(h: var Handshake, input: openarray[byte]): AuthResult[void] = proc decodeAckMessage*(h: var Handshake, input: openArray[byte]): AuthResult[void] =
## Decodes AckMessage from `input`. ## Decodes AckMessage from `input`.
if len(input) < AckMessageV4Length: if len(input) < AckMessageV4Length:
return err(IncompleteError) return err(IncompleteError)
@ -479,13 +479,13 @@ proc decodeAckMessage*(h: var Handshake, input: openarray[byte]): AuthResult[voi
let res = h.decodeAckMessageV4(input) let res = h.decodeAckMessageV4(input)
if res.isOk(): return res if res.isOk(): return res
let res = h.decodeAckMessageEip8(input) let res = h.decodeAckMessageEIP8(input)
if res.isOk(): h.flags.incl(EIP8) if res.isOk(): h.flags.incl(EIP8)
res res
proc getSecrets*( proc getSecrets*(
h: Handshake, authmsg: openarray[byte], h: Handshake, authmsg: openArray[byte],
ackmsg: openarray[byte]): ConnectionSecret = ackmsg: openArray[byte]): ConnectionSecret =
## Derive secrets from handshake `h` using encrypted AuthMessage `authmsg` and ## Derive secrets from handshake `h` using encrypted AuthMessage `authmsg` and
## encrypted AckMessage `ackmsg`. ## encrypted AckMessage `ackmsg`.
var var

View File

@ -30,7 +30,7 @@ proc getBlockHeaders*(db: AbstractChainDB, req: BlocksRequest): seq[BlockHeader]
template fetcher*(fetcherName, fetchingFunc, InputType, ResultType: untyped) = template fetcher*(fetcherName, fetchingFunc, InputType, ResultType: untyped) =
proc fetcherName*(db: AbstractChainDB, proc fetcherName*(db: AbstractChainDB,
lookups: openarray[InputType]): seq[ResultType] {.gcsafe.} = lookups: openArray[InputType]): seq[ResultType] {.gcsafe.} =
for lookup in lookups: for lookup in lookups:
let fetched = fetchingFunc(db, lookup) let fetched = fetchingFunc(db, lookup)
if fetched.hasData: if fetched.hasData:
@ -47,6 +47,6 @@ fetcher getProofs, getProof, ProofRequest, Blob
fetcher getHeaderProofs, getHeaderProof, ProofRequest, Blob fetcher getHeaderProofs, getHeaderProof, ProofRequest, Blob
proc getHelperTrieProofs*(db: AbstractChainDB, proc getHelperTrieProofs*(db: AbstractChainDB,
reqs: openarray[HelperTrieProofRequest], reqs: openArray[HelperTrieProofRequest],
outNodes: var seq[Blob], outAuxData: var seq[Blob]) = outNodes: var seq[Blob], outAuxData: var seq[Blob]) =
discard discard

View File

@ -141,7 +141,7 @@ proc sendNeighbours*(d: DiscoveryProtocol, node: Node, neighbours: seq[Node]) =
template flush() = template flush() =
block: block:
let payload = rlp.encode((nodes, expiration())) let payload = rlp.encode((nodes, expiration()))
let msg = pack(cmdNeighbours, payload, d.privkey) let msg = pack(cmdNeighbours, payload, d.privKey)
trace "Neighbours to", node, nodes trace "Neighbours to", node, nodes
d.send(node, msg) d.send(node, msg)
nodes.setLen(0) nodes.setLen(0)
@ -155,7 +155,7 @@ proc sendNeighbours*(d: DiscoveryProtocol, node: Node, neighbours: seq[Node]) =
if nodes.len != 0: flush() if nodes.len != 0: flush()
proc newDiscoveryProtocol*(privKey: PrivateKey, address: Address, proc newDiscoveryProtocol*(privKey: PrivateKey, address: Address,
bootstrapNodes: openarray[ENode], rng = newRng() bootstrapNodes: openArray[ENode], rng = newRng()
): DiscoveryProtocol = ): DiscoveryProtocol =
result.new() result.new()
result.privKey = privKey result.privKey = privKey
@ -214,7 +214,7 @@ proc recvFindNode(d: DiscoveryProtocol, node: Node, payload: openArray[byte])
let rng = rlp.listElem(0).toBytes let rng = rlp.listElem(0).toBytes
# Check for pubkey len # Check for pubkey len
if rng.len == 64: if rng.len == 64:
let nodeId = readUIntBE[256](rng[32 .. ^1]) let nodeId = readUintBE[256](rng[32 .. ^1])
d.kademlia.recvFindNode(node, nodeId) d.kademlia.recvFindNode(node, nodeId)
else: else:
trace "Invalid target public key received" trace "Invalid target public key received"

View File

@ -187,7 +187,7 @@ proc run(config: DiscoveryConf) =
echo "No Talk Response message returned" echo "No Talk Response message returned"
of noCommand: of noCommand:
d.start() d.start()
waitfor(discover(d)) waitFor(discover(d))
when isMainModule: when isMainModule:
let config = DiscoveryConf.load() let config = DiscoveryConf.load()

View File

@ -89,7 +89,7 @@ type
Codec* = object Codec* = object
localNode*: Node localNode*: Node
privKey*: PrivateKey privKey*: PrivateKey
handshakes*: Table[HandShakeKey, Challenge] handshakes*: Table[HandshakeKey, Challenge]
sessions*: Sessions sessions*: Sessions
DecodeResult*[T] = Result[T, cstring] DecodeResult*[T] = Result[T, cstring]
@ -101,7 +101,7 @@ func hash*(key: HandshakeKey): Hash =
result = key.nodeId.hash !& key.address.hash result = key.nodeId.hash !& key.address.hash
result = !$result result = !$result
proc idHash(challengeData, ephkey: openarray[byte], nodeId: NodeId): proc idHash(challengeData, ephkey: openArray[byte], nodeId: NodeId):
MDigest[256] = MDigest[256] =
var ctx: sha256 var ctx: sha256
ctx.init() ctx.init()
@ -113,16 +113,16 @@ proc idHash(challengeData, ephkey: openarray[byte], nodeId: NodeId):
ctx.clear() ctx.clear()
proc createIdSignature*(privKey: PrivateKey, challengeData, proc createIdSignature*(privKey: PrivateKey, challengeData,
ephKey: openarray[byte], nodeId: NodeId): SignatureNR = ephKey: openArray[byte], nodeId: NodeId): SignatureNR =
signNR(privKey, SkMessage(idHash(challengeData, ephKey, nodeId).data)) signNR(privKey, SkMessage(idHash(challengeData, ephKey, nodeId).data))
proc verifyIdSignature*(sig: SignatureNR, challengeData, ephKey: openarray[byte], proc verifyIdSignature*(sig: SignatureNR, challengeData, ephKey: openArray[byte],
nodeId: NodeId, pubKey: PublicKey): bool = nodeId: NodeId, pubkey: PublicKey): bool =
let h = idHash(challengeData, ephKey, nodeId) let h = idHash(challengeData, ephKey, nodeId)
verify(sig, SkMessage(h.data), pubKey) verify(sig, SkMessage(h.data), pubkey)
proc deriveKeys*(n1, n2: NodeID, priv: PrivateKey, pub: PublicKey, proc deriveKeys*(n1, n2: NodeId, priv: PrivateKey, pub: PublicKey,
challengeData: openarray[byte]): HandshakeSecrets = challengeData: openArray[byte]): HandshakeSecrets =
let eph = ecdhRawFull(priv, pub) let eph = ecdhRawFull(priv, pub)
var info = newSeqOfCap[byte](keyAgreementPrefix.len + 32 * 2) var info = newSeqOfCap[byte](keyAgreementPrefix.len + 32 * 2)
@ -138,7 +138,7 @@ proc deriveKeys*(n1, n2: NodeID, priv: PrivateKey, pub: PublicKey,
toOpenArray(res, 0, sizeof(secrets) - 1)) toOpenArray(res, 0, sizeof(secrets) - 1))
secrets secrets
proc encryptGCM*(key: AesKey, nonce, pt, authData: openarray[byte]): seq[byte] = proc encryptGCM*(key: AesKey, nonce, pt, authData: openArray[byte]): seq[byte] =
var ectx: GCM[aes128] var ectx: GCM[aes128]
ectx.init(key, nonce, authData) ectx.init(key, nonce, authData)
result = newSeq[byte](pt.len + gcmTagSize) result = newSeq[byte](pt.len + gcmTagSize)
@ -146,7 +146,7 @@ proc encryptGCM*(key: AesKey, nonce, pt, authData: openarray[byte]): seq[byte] =
ectx.getTag(result.toOpenArray(pt.len, result.high)) ectx.getTag(result.toOpenArray(pt.len, result.high))
ectx.clear() ectx.clear()
proc decryptGCM*(key: AesKey, nonce, ct, authData: openarray[byte]): proc decryptGCM*(key: AesKey, nonce, ct, authData: openArray[byte]):
Option[seq[byte]] = Option[seq[byte]] =
if ct.len <= gcmTagSize: if ct.len <= gcmTagSize:
debug "cipher is missing tag", len = ct.len debug "cipher is missing tag", len = ct.len
@ -165,14 +165,14 @@ proc decryptGCM*(key: AesKey, nonce, ct, authData: openarray[byte]):
return some(res) return some(res)
proc encryptHeader*(id: NodeId, iv, header: openarray[byte]): seq[byte] = proc encryptHeader*(id: NodeId, iv, header: openArray[byte]): seq[byte] =
var ectx: CTR[aes128] var ectx: CTR[aes128]
ectx.init(id.toByteArrayBE().toOpenArray(0, 15), iv) ectx.init(id.toByteArrayBE().toOpenArray(0, 15), iv)
result = newSeq[byte](header.len) result = newSeq[byte](header.len)
ectx.encrypt(header, result) ectx.encrypt(header, result)
ectx.clear() ectx.clear()
proc hasHandshake*(c: Codec, key: HandShakeKey): bool = proc hasHandshake*(c: Codec, key: HandshakeKey): bool =
c.handshakes.hasKey(key) c.handshakes.hasKey(key)
proc encodeStaticHeader*(flag: Flag, nonce: AESGCMNonce, authSize: int): proc encodeStaticHeader*(flag: Flag, nonce: AESGCMNonce, authSize: int):
@ -185,7 +185,7 @@ proc encodeStaticHeader*(flag: Flag, nonce: AESGCMNonce, authSize: int):
result.add((uint16(authSize)).toBytesBE()) result.add((uint16(authSize)).toBytesBE())
proc encodeMessagePacket*(rng: var BrHmacDrbgContext, c: var Codec, proc encodeMessagePacket*(rng: var BrHmacDrbgContext, c: var Codec,
toId: NodeID, toAddr: Address, message: openarray[byte]): toId: NodeId, toAddr: Address, message: openArray[byte]):
(seq[byte], AESGCMNonce) = (seq[byte], AESGCMNonce) =
var nonce: AESGCMNonce var nonce: AESGCMNonce
brHmacDrbgGenerate(rng, nonce) # Random AESGCM nonce brHmacDrbgGenerate(rng, nonce) # Random AESGCM nonce
@ -228,7 +228,7 @@ proc encodeMessagePacket*(rng: var BrHmacDrbgContext, c: var Codec,
return (packet, nonce) return (packet, nonce)
proc encodeWhoareyouPacket*(rng: var BrHmacDrbgContext, c: var Codec, proc encodeWhoareyouPacket*(rng: var BrHmacDrbgContext, c: var Codec,
toId: NodeID, toAddr: Address, requestNonce: AESGCMNonce, recordSeq: uint64, toId: NodeId, toAddr: Address, requestNonce: AESGCMNonce, recordSeq: uint64,
pubkey: Option[PublicKey]): seq[byte] = pubkey: Option[PublicKey]): seq[byte] =
var idNonce: IdNonce var idNonce: IdNonce
brHmacDrbgGenerate(rng, idNonce) brHmacDrbgGenerate(rng, idNonce)
@ -263,14 +263,14 @@ proc encodeWhoareyouPacket*(rng: var BrHmacDrbgContext, c: var Codec,
recordSeq: recordSeq, recordSeq: recordSeq,
challengeData: @iv & header) challengeData: @iv & header)
challenge = Challenge(whoareyouData: whoareyouData, pubkey: pubkey) challenge = Challenge(whoareyouData: whoareyouData, pubkey: pubkey)
key = HandShakeKey(nodeId: toId, address: toAddr) key = HandshakeKey(nodeId: toId, address: toAddr)
c.handshakes[key] = challenge c.handshakes[key] = challenge
return packet return packet
proc encodeHandshakePacket*(rng: var BrHmacDrbgContext, c: var Codec, proc encodeHandshakePacket*(rng: var BrHmacDrbgContext, c: var Codec,
toId: NodeID, toAddr: Address, message: openarray[byte], toId: NodeId, toAddr: Address, message: openArray[byte],
whoareyouData: WhoareyouData, pubkey: PublicKey): seq[byte] = whoareyouData: WhoareyouData, pubkey: PublicKey): seq[byte] =
var header: seq[byte] var header: seq[byte]
var nonce: AESGCMNonce var nonce: AESGCMNonce
@ -321,7 +321,7 @@ proc encodeHandshakePacket*(rng: var BrHmacDrbgContext, c: var Codec,
return packet return packet
proc decodeHeader*(id: NodeId, iv, maskedHeader: openarray[byte]): proc decodeHeader*(id: NodeId, iv, maskedHeader: openArray[byte]):
DecodeResult[(StaticHeader, seq[byte])] = DecodeResult[(StaticHeader, seq[byte])] =
# No need to check staticHeader size as that is included in minimum packet # No need to check staticHeader size as that is included in minimum packet
# size check in decodePacket # size check in decodePacket
@ -361,7 +361,7 @@ proc decodeHeader*(id: NodeId, iv, maskedHeader: openarray[byte]):
ok((StaticHeader(authdataSize: authdataSize, flag: flag, nonce: nonce), ok((StaticHeader(authdataSize: authdataSize, flag: flag, nonce: nonce),
staticHeader & authdata)) staticHeader & authdata))
proc decodeMessage*(body: openarray[byte]): DecodeResult[Message] = proc decodeMessage*(body: openArray[byte]): DecodeResult[Message] =
## Decodes to the specific `Message` type. ## Decodes to the specific `Message` type.
if body.len < 1: if body.len < 1:
return err("No message data") return err("No message data")
@ -388,7 +388,7 @@ proc decodeMessage*(body: openarray[byte]): DecodeResult[Message] =
of unused: return err("Invalid message type") of unused: return err("Invalid message type")
of ping: rlp.decode(message.ping) of ping: rlp.decode(message.ping)
of pong: rlp.decode(message.pong) of pong: rlp.decode(message.pong)
of findNode: rlp.decode(message.findNode) of findnode: rlp.decode(message.findNode)
of nodes: rlp.decode(message.nodes) of nodes: rlp.decode(message.nodes)
of talkreq: rlp.decode(message.talkreq) of talkreq: rlp.decode(message.talkreq)
of talkresp: rlp.decode(message.talkresp) of talkresp: rlp.decode(message.talkresp)
@ -484,7 +484,7 @@ proc decodeHandshakePacket(c: var Codec, fromAddr: Address, nonce: AESGCMNonce,
if header.len < staticHeaderSize + authdataHeadSize + int(sigSize) + int(ephKeySize): if header.len < staticHeaderSize + authdataHeadSize + int(sigSize) + int(ephKeySize):
return err("Invalid header for handshake message packet") return err("Invalid header for handshake message packet")
let key = HandShakeKey(nodeId: srcId, address: fromAddr) let key = HandshakeKey(nodeId: srcId, address: fromAddr)
var challenge: Challenge var challenge: Challenge
if not c.handshakes.pop(key, challenge): if not c.handshakes.pop(key, challenge):
return err("No challenge found: timed out or unsolicited packet") return err("No challenge found: timed out or unsolicited packet")
@ -508,7 +508,7 @@ proc decodeHandshakePacket(c: var Codec, fromAddr: Address, nonce: AESGCMNonce,
except RlpError, ValueError: except RlpError, ValueError:
return err("Invalid encoded ENR") return err("Invalid encoded ENR")
var pubKey: PublicKey var pubkey: PublicKey
var newNode: Option[Node] var newNode: Option[Node]
# TODO: Shall we return Node or Record? Record makes more sense, but we do # TODO: Shall we return Node or Record? Record makes more sense, but we do
# need the pubkey and the nodeid # need the pubkey and the nodeid
@ -520,12 +520,12 @@ proc decodeHandshakePacket(c: var Codec, fromAddr: Address, nonce: AESGCMNonce,
# Note: Not checking if the record seqNum is higher than the one we might # Note: Not checking if the record seqNum is higher than the one we might
# have stored as it comes from this node directly. # have stored as it comes from this node directly.
pubKey = node.pubKey pubkey = node.pubkey
newNode = some(node) newNode = some(node)
else: else:
# TODO: Hmm, should we still verify node id of the ENR of this node? # TODO: Hmm, should we still verify node id of the ENR of this node?
if challenge.pubkey.isSome(): if challenge.pubkey.isSome():
pubKey = challenge.pubkey.get() pubkey = challenge.pubkey.get()
else: else:
# We should have received a Record in this case. # We should have received a Record in this case.
return err("Missing ENR in handshake packet") return err("Missing ENR in handshake packet")

View File

@ -94,9 +94,9 @@ proc `==`(a, b: Field): bool =
proc cmp(a, b: FieldPair): int = cmp(a[0], b[0]) proc cmp(a, b: FieldPair): int = cmp(a[0], b[0])
proc makeEnrRaw(seqNum: uint64, pk: PrivateKey, proc makeEnrRaw(seqNum: uint64, pk: PrivateKey,
pairs: openarray[FieldPair]): EnrResult[seq[byte]] = pairs: openArray[FieldPair]): EnrResult[seq[byte]] =
proc append(w: var RlpWriter, seqNum: uint64, proc append(w: var RlpWriter, seqNum: uint64,
pairs: openarray[FieldPair]): seq[byte] = pairs: openArray[FieldPair]): seq[byte] =
w.append(seqNum) w.append(seqNum)
for (k, v) in pairs: for (k, v) in pairs:
w.append(k) w.append(k)
@ -124,7 +124,7 @@ proc makeEnrRaw(seqNum: uint64, pk: PrivateKey,
ok(raw) ok(raw)
proc makeEnrAux(seqNum: uint64, pk: PrivateKey, proc makeEnrAux(seqNum: uint64, pk: PrivateKey,
pairs: openarray[FieldPair]): EnrResult[Record] = pairs: openArray[FieldPair]): EnrResult[Record] =
var record: Record var record: Record
record.pairs = @pairs record.pairs = @pairs
record.seqNum = seqNum record.seqNum = seqNum
@ -185,7 +185,7 @@ proc init*(T: type Record, seqNum: uint64,
pk: PrivateKey, pk: PrivateKey,
ip: Option[ValidIpAddress], ip: Option[ValidIpAddress],
tcpPort, udpPort: Option[Port], tcpPort, udpPort: Option[Port],
extraFields: openarray[FieldPair] = []): extraFields: openArray[FieldPair] = []):
EnrResult[T] = EnrResult[T] =
## Initialize a `Record` with given sequence number, private key, optional ## Initialize a `Record` with given sequence number, private key, optional
## ip address, tcp port, udp port, and optional custom k:v pairs. ## ip address, tcp port, udp port, and optional custom k:v pairs.
@ -263,7 +263,7 @@ proc find(r: Record, key: string): Option[int] =
return some(i) return some(i)
proc update*(record: var Record, pk: PrivateKey, proc update*(record: var Record, pk: PrivateKey,
fieldPairs: openarray[FieldPair]): EnrResult[void] = fieldPairs: openArray[FieldPair]): EnrResult[void] =
## Update a `Record` k:v pairs. ## Update a `Record` k:v pairs.
## ##
## In case any of the k:v pairs is updated or added (new), the sequence number ## In case any of the k:v pairs is updated or added (new), the sequence number
@ -306,7 +306,7 @@ proc update*(record: var Record, pk: PrivateKey,
proc update*(r: var Record, pk: PrivateKey, proc update*(r: var Record, pk: PrivateKey,
ip: Option[ValidIpAddress], ip: Option[ValidIpAddress],
tcpPort, udpPort: Option[Port] = none[Port](), tcpPort, udpPort: Option[Port] = none[Port](),
extraFields: openarray[FieldPair] = []): extraFields: openArray[FieldPair] = []):
EnrResult[void] = EnrResult[void] =
## Update a `Record` with given ip address, tcp port, udp port and optional ## Update a `Record` with given ip address, tcp port, udp port and optional
## custom k:v pairs. ## custom k:v pairs.
@ -362,7 +362,7 @@ proc contains*(r: Record, fp: (string, seq[byte])): bool =
if field.get() == fp[1]: if field.get() == fp[1]:
return true return true
proc verifySignatureV4(r: Record, sigData: openarray[byte], content: seq[byte]): proc verifySignatureV4(r: Record, sigData: openArray[byte], content: seq[byte]):
bool = bool =
let publicKey = r.get(PublicKey) let publicKey = r.get(PublicKey)
if publicKey.isSome: if publicKey.isSome:
@ -441,7 +441,7 @@ proc fromBytesAux(r: var Record): bool {.raises: [RlpError, Defect].} =
verifySignature(r) verifySignature(r)
proc fromBytes*(r: var Record, s: openarray[byte]): bool = proc fromBytes*(r: var Record, s: openArray[byte]): bool =
## Loads ENR from rlp-encoded bytes, and validates the signature. ## Loads ENR from rlp-encoded bytes, and validates the signature.
r.raw = @s r.raw = @s
try: try:

View File

@ -1,7 +1,7 @@
import nimcrypto import nimcrypto
proc hkdf*(HashType: typedesc, ikm, salt, info: openarray[byte], proc hkdf*(HashType: typedesc, ikm, salt, info: openArray[byte],
output: var openarray[byte]) = output: var openArray[byte]) =
var ctx: HMAC[HashType] var ctx: HMAC[HashType]
ctx.init(salt) ctx.init(salt)
ctx.update(ikm) ctx.update(ikm)

View File

@ -77,7 +77,7 @@ type
of pong: of pong:
pong*: PongMessage pong*: PongMessage
of findnode: of findnode:
findNode*: FindNodeMessage findnode*: FindNodeMessage
of nodes: of nodes:
nodes*: NodesMessage nodes*: NodesMessage
of talkreq: of talkreq:
@ -98,7 +98,7 @@ type
template messageKind*(T: typedesc[SomeMessage]): MessageKind = template messageKind*(T: typedesc[SomeMessage]): MessageKind =
when T is PingMessage: ping when T is PingMessage: ping
elif T is PongMessage: pong elif T is PongMessage: pong
elif T is FindNodeMessage: findNode elif T is FindNodeMessage: findnode
elif T is NodesMessage: nodes elif T is NodesMessage: nodes
elif T is TalkReqMessage: talkreq elif T is TalkReqMessage: talkreq
elif T is TalkRespMessage: talkresp elif T is TalkRespMessage: talkresp

View File

@ -60,7 +60,7 @@ func newNode*(r: Record): Result[Node, cstring] =
func update*(n: Node, pk: PrivateKey, ip: Option[ValidIpAddress], func update*(n: Node, pk: PrivateKey, ip: Option[ValidIpAddress],
tcpPort, udpPort: Option[Port] = none[Port](), tcpPort, udpPort: Option[Port] = none[Port](),
extraFields: openarray[FieldPair] = []): Result[void, cstring] = extraFields: openArray[FieldPair] = []): Result[void, cstring] =
? n.record.update(pk, ip, tcpPort, udpPort, extraFields) ? n.record.update(pk, ip, tcpPort, udpPort, extraFields)
if ip.isSome(): if ip.isSome():

View File

@ -25,7 +25,7 @@ proc validIp(sender, address: IpAddress): bool =
# https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml # https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml
return true return true
proc verifyNodesRecords(enrs: openarray[Record], fromNode: Node, nodesLimit: int, proc verifyNodesRecords(enrs: openArray[Record], fromNode: Node, nodesLimit: int,
distances: Option[seq[uint16]]): seq[Node] = distances: Option[seq[uint16]]): seq[Node] =
## Verify and convert ENRs to a sequence of nodes. Only ENRs that pass ## Verify and convert ENRs to a sequence of nodes. Only ENRs that pass
## verification will be added. ENRs are verified for duplicates, invalid ## verification will be added. ENRs are verified for duplicates, invalid
@ -79,8 +79,8 @@ proc verifyNodesRecords(enrs: openarray[Record], fromNode: Node, nodesLimit: int
seen.incl(n) seen.incl(n)
result.add(n) result.add(n)
proc verifyNodesRecords*(enrs: openarray[Record], fromNode: Node, nodesLimit: int): seq[Node] = proc verifyNodesRecords*(enrs: openArray[Record], fromNode: Node, nodesLimit: int): seq[Node] =
verifyNodesRecords(enrs, fromNode, nodesLimit, none[seq[uint16]]()) verifyNodesRecords(enrs, fromNode, nodesLimit, none[seq[uint16]]())
proc verifyNodesRecords*(enrs: openarray[Record], fromNode: Node, nodesLimit: int, distances: seq[uint16]): seq[Node] = proc verifyNodesRecords*(enrs: openArray[Record], fromNode: Node, nodesLimit: int, distances: seq[uint16]): seq[Node] =
verifyNodesRecords(enrs, fromNode, nodesLimit, some[seq[uint16]](distances)) verifyNodesRecords(enrs, fromNode, nodesLimit, some[seq[uint16]](distances))

View File

@ -173,7 +173,7 @@ proc addNode*(d: Protocol, enr: EnrUri): bool =
## Returns false if no valid ENR URI, or on the conditions of `addNode` from ## Returns false if no valid ENR URI, or on the conditions of `addNode` from
## an `Record`. ## an `Record`.
var r: Record var r: Record
let res = r.fromUri(enr) let res = r.fromURI(enr)
if res: if res:
return d.addNode(r) return d.addNode(r)
@ -217,7 +217,7 @@ func getRecord*(d: Protocol): Record =
d.localNode.record d.localNode.record
proc updateRecord*( proc updateRecord*(
d: Protocol, enrFields: openarray[(string, seq[byte])]): DiscResult[void] = d: Protocol, enrFields: openArray[(string, seq[byte])]): DiscResult[void] =
## Update the ENR of the local node with provided `enrFields` k:v pairs. ## Update the ENR of the local node with provided `enrFields` k:v pairs.
let fields = mapIt(enrFields, toFieldPair(it[0], it[1])) let fields = mapIt(enrFields, toFieldPair(it[0], it[1]))
d.localNode.record.update(d.privateKey, fields) d.localNode.record.update(d.privateKey, fields)
@ -246,7 +246,7 @@ proc send(d: Protocol, n: Node, data: seq[byte]) =
d.send(n.address.get(), data) d.send(n.address.get(), data)
proc sendNodes(d: Protocol, toId: NodeId, toAddr: Address, reqId: RequestId, proc sendNodes(d: Protocol, toId: NodeId, toAddr: Address, reqId: RequestId,
nodes: openarray[Node]) = nodes: openArray[Node]) =
proc sendNodes(d: Protocol, toId: NodeId, toAddr: Address, proc sendNodes(d: Protocol, toId: NodeId, toAddr: Address,
message: NodesMessage, reqId: RequestId) {.nimcall.} = message: NodesMessage, reqId: RequestId) {.nimcall.} =
let (data, _) = encodeMessagePacket(d.rng[], d.codec, toId, toAddr, let (data, _) = encodeMessagePacket(d.rng[], d.codec, toId, toAddr,
@ -332,9 +332,9 @@ proc handleMessage(d: Protocol, srcId: NodeId, fromAddr: Address,
of ping: of ping:
discovery_message_requests_incoming.inc() discovery_message_requests_incoming.inc()
d.handlePing(srcId, fromAddr, message.ping, message.reqId) d.handlePing(srcId, fromAddr, message.ping, message.reqId)
of findNode: of findnode:
discovery_message_requests_incoming.inc() discovery_message_requests_incoming.inc()
d.handleFindNode(srcId, fromAddr, message.findNode, message.reqId) d.handleFindNode(srcId, fromAddr, message.findnode, message.reqId)
of talkreq: of talkreq:
discovery_message_requests_incoming.inc() discovery_message_requests_incoming.inc()
d.handleTalkReq(srcId, fromAddr, message.talkreq, message.reqId) d.handleTalkReq(srcId, fromAddr, message.talkreq, message.reqId)
@ -362,7 +362,7 @@ proc registerTalkProtocol*(d: Protocol, protocolId: seq[byte],
proc sendWhoareyou(d: Protocol, toId: NodeId, a: Address, proc sendWhoareyou(d: Protocol, toId: NodeId, a: Address,
requestNonce: AESGCMNonce, node: Option[Node]) = requestNonce: AESGCMNonce, node: Option[Node]) =
let key = HandShakeKey(nodeId: toId, address: a) let key = HandshakeKey(nodeId: toId, address: a)
if not d.codec.hasHandshake(key): if not d.codec.hasHandshake(key):
let let
recordSeq = if node.isSome(): node.get().record.seqNum recordSeq = if node.isSome(): node.get().record.seqNum
@ -791,7 +791,7 @@ proc populateTable*(d: Protocol) {.async.} =
proc revalidateNode*(d: Protocol, n: Node) {.async.} = proc revalidateNode*(d: Protocol, n: Node) {.async.} =
let pong = await d.ping(n) let pong = await d.ping(n)
if pong.isOK(): if pong.isOk():
let res = pong.get() let res = pong.get()
if res.enrSeq > n.record.seqNum: if res.enrSeq > n.record.seqNum:
# Request new ENR # Request new ENR
@ -883,8 +883,8 @@ proc ipMajorityLoop(d: Protocol) {.async.} =
proc newProtocol*(privKey: PrivateKey, proc newProtocol*(privKey: PrivateKey,
enrIp: Option[ValidIpAddress], enrIp: Option[ValidIpAddress],
enrTcpPort, enrUdpPort: Option[Port], enrTcpPort, enrUdpPort: Option[Port],
localEnrFields: openarray[(string, seq[byte])] = [], localEnrFields: openArray[(string, seq[byte])] = [],
bootstrapRecords: openarray[Record] = [], bootstrapRecords: openArray[Record] = [],
previousRecord = none[enr.Record](), previousRecord = none[enr.Record](),
bindPort: Port, bindPort: Port,
bindIp = IPv4_any(), bindIp = IPv4_any(),

View File

@ -13,10 +13,10 @@ proc rand*(rng: var BrHmacDrbgContext, max: Natural): int =
if x < randMax - (randMax mod (uint64(max) + 1'u64)): # against modulo bias if x < randMax - (randMax mod (uint64(max) + 1'u64)): # against modulo bias
return int(x mod (uint64(max) + 1'u64)) return int(x mod (uint64(max) + 1'u64))
proc sample*[T](rng: var BrHmacDrbgContext, a: openarray[T]): T = proc sample*[T](rng: var BrHmacDrbgContext, a: openArray[T]): T =
result = a[rng.rand(a.high)] result = a[rng.rand(a.high)]
proc shuffle*[T](rng: var BrHmacDrbgContext, a: var openarray[T]) = proc shuffle*[T](rng: var BrHmacDrbgContext, a: var openArray[T]) =
for i in countdown(a.high, 1): for i in countdown(a.high, 1):
let j = rng.rand(i) let j = rng.rand(i)
swap(a[i], a[j]) swap(a[i], a[j])

View File

@ -93,7 +93,7 @@ type
NoAddress NoAddress
# xor distance functions # xor distance functions
func distance*(a, b: NodeId): Uint256 = func distance*(a, b: NodeId): UInt256 =
## Calculate the distance to a NodeId. ## Calculate the distance to a NodeId.
a xor b a xor b
@ -137,7 +137,7 @@ const
XorDistanceCalculator* = DistanceCalculator(calculateDistance: distance, XorDistanceCalculator* = DistanceCalculator(calculateDistance: distance,
calculateLogDistance: logDistance, calculateIdAtDistance: idAtDistance) calculateLogDistance: logDistance, calculateIdAtDistance: idAtDistance)
func distance*(r: RoutingTable, a, b: NodeId): Uint256 = func distance*(r: RoutingTable, a, b: NodeId): UInt256 =
r.distanceCalculator.calculateDistance(a, b) r.distanceCalculator.calculateDistance(a, b)
func logDistance*(r: RoutingTable, a, b: NodeId): uint16 = func logDistance*(r: RoutingTable, a, b: NodeId): uint16 =
@ -172,7 +172,7 @@ proc ipLimitInc(r: var RoutingTable, b: KBucket, n: Node): bool =
return false return false
# Check ip limit for routing table # Check ip limit for routing table
if not r.ipLimits.inc(ip): if not r.ipLimits.inc(ip):
b.iplimits.dec(ip) b.ipLimits.dec(ip)
return false return false
return true return true
@ -225,7 +225,7 @@ proc inRange(k: KBucket, n: Node): bool =
proc contains(k: KBucket, n: Node): bool = n in k.nodes proc contains(k: KBucket, n: Node): bool = n in k.nodes
proc binaryGetBucketForNode*(buckets: openarray[KBucket], proc binaryGetBucketForNode*(buckets: openArray[KBucket],
id: NodeId): KBucket = id: NodeId): KBucket =
## Given a list of ordered buckets, returns the bucket for a given `NodeId`. ## Given a list of ordered buckets, returns the bucket for a given `NodeId`.
## Returns nil if no bucket in range for given `id` is found. ## Returns nil if no bucket in range for given `id` is found.
@ -233,13 +233,13 @@ proc binaryGetBucketForNode*(buckets: openarray[KBucket],
cmp(a.iend, b) cmp(a.iend, b)
# Prevent cases where `lowerBound` returns an out of range index e.g. at empty # Prevent cases where `lowerBound` returns an out of range index e.g. at empty
# openarray, or when the id is out range for all buckets in the openarray. # openArray, or when the id is out range for all buckets in the openArray.
if bucketPos < buckets.len: if bucketPos < buckets.len:
let bucket = buckets[bucketPos] let bucket = buckets[bucketPos]
if bucket.istart <= id and id <= bucket.iend: if bucket.istart <= id and id <= bucket.iend:
result = bucket result = bucket
proc computeSharedPrefixBits(nodes: openarray[NodeId]): int = proc computeSharedPrefixBits(nodes: openArray[NodeId]): int =
## Count the number of prefix bits shared by all nodes. ## Count the number of prefix bits shared by all nodes.
if nodes.len < 2: if nodes.len < 2:
return ID_SIZE return ID_SIZE
@ -266,7 +266,7 @@ proc init*(T: type RoutingTable, localNode: Node, bitsPerHop = DefaultBitsPerHop
## `bitsPerHop` is default set to 5 as recommended by original Kademlia paper. ## `bitsPerHop` is default set to 5 as recommended by original Kademlia paper.
RoutingTable( RoutingTable(
localNode: localNode, localNode: localNode,
buckets: @[KBucket.new(0.u256, high(Uint256), ipLimits.bucketIpLimit)], buckets: @[KBucket.new(0.u256, high(UInt256), ipLimits.bucketIpLimit)],
bitsPerHop: bitsPerHop, bitsPerHop: bitsPerHop,
ipLimits: IpLimits(limit: ipLimits.tableIpLimit), ipLimits: IpLimits(limit: ipLimits.tableIpLimit),
distanceCalculator: distanceCalculator, distanceCalculator: distanceCalculator,
@ -478,7 +478,7 @@ proc neighboursAtDistances*(r: RoutingTable, distances: seq[uint16],
proc len*(r: RoutingTable): int = proc len*(r: RoutingTable): int =
for b in r.buckets: result += b.len for b in r.buckets: result += b.len
proc moveRight[T](arr: var openarray[T], a, b: int) = proc moveRight[T](arr: var openArray[T], a, b: int) =
## In `arr` move elements in range [a, b] right by 1. ## In `arr` move elements in range [a, b] right by 1.
var t: T var t: T
shallowCopy(t, arr[b + 1]) shallowCopy(t, arr[b + 1])

View File

@ -73,7 +73,7 @@ template eciesIvPos(): int =
template eciesTagPos(size: int): int = template eciesTagPos(size: int): int =
1 + sizeof(PublicKey) + aes128.sizeBlock + size 1 + sizeof(PublicKey) + aes128.sizeBlock + size
proc kdf*(data: openarray[byte]): array[KeyLength, byte] {.noInit.} = proc kdf*(data: openArray[byte]): array[KeyLength, byte] {.noinit.} =
## NIST SP 800-56a Concatenation Key Derivation Function (see section 5.8.1) ## NIST SP 800-56a Concatenation Key Derivation Function (see section 5.8.1)
var ctx: sha256 var ctx: sha256
var counter: uint32 var counter: uint32
@ -93,9 +93,9 @@ proc kdf*(data: openarray[byte]): array[KeyLength, byte] {.noInit.} =
ctx.clear() # clean ctx ctx.clear() # clean ctx
copyMem(addr result[0], addr storage[0], KeyLength) copyMem(addr result[0], addr storage[0], KeyLength)
proc eciesEncrypt*(rng: var BrHmacDrbgContext, input: openarray[byte], proc eciesEncrypt*(rng: var BrHmacDrbgContext, input: openArray[byte],
output: var openarray[byte], pubkey: PublicKey, output: var openArray[byte], pubkey: PublicKey,
sharedmac: openarray[byte] = emptyMac): EciesResult[void] = sharedmac: openArray[byte] = emptyMac): EciesResult[void] =
## Encrypt data with ECIES method using given public key `pubkey`. ## Encrypt data with ECIES method using given public key `pubkey`.
## ``input`` - input data ## ``input`` - input data
## ``output`` - output data ## ``output`` - output data
@ -156,10 +156,10 @@ proc eciesEncrypt*(rng: var BrHmacDrbgContext, input: openarray[byte],
ok() ok()
proc eciesDecrypt*(input: openarray[byte], proc eciesDecrypt*(input: openArray[byte],
output: var openarray[byte], output: var openArray[byte],
seckey: PrivateKey, seckey: PrivateKey,
sharedmac: openarray[byte] = emptyMac): EciesResult[void] = sharedmac: openArray[byte] = emptyMac): EciesResult[void] =
## Decrypt data with ECIES method using given private key `seckey`. ## Decrypt data with ECIES method using given private key `seckey`.
## ``input`` - input data ## ``input`` - input data
## ``output`` - output data ## ``output`` - output data

View File

@ -144,7 +144,7 @@ proc isFull(k: KBucket): bool = k.len == BUCKET_SIZE
proc contains(k: KBucket, n: Node): bool = n in k.nodes proc contains(k: KBucket, n: Node): bool = n in k.nodes
proc binaryGetBucketForNode(buckets: openarray[KBucket], n: Node): proc binaryGetBucketForNode(buckets: openArray[KBucket], n: Node):
KBucket {.raises: [ValueError, Defect].} = KBucket {.raises: [ValueError, Defect].} =
## Given a list of ordered buckets, returns the bucket for a given node. ## Given a list of ordered buckets, returns the bucket for a given node.
let bucketPos = lowerBound(buckets, n.id) do(a: KBucket, b: NodeId) -> int: let bucketPos = lowerBound(buckets, n.id) do(a: KBucket, b: NodeId) -> int:
@ -158,7 +158,7 @@ proc binaryGetBucketForNode(buckets: openarray[KBucket], n: Node):
if result.isNil: if result.isNil:
raise newException(ValueError, "No bucket found for node with id " & $n.id) raise newException(ValueError, "No bucket found for node with id " & $n.id)
proc computeSharedPrefixBits(nodes: openarray[Node]): int = proc computeSharedPrefixBits(nodes: openArray[Node]): int =
## Count the number of prefix bits shared by all nodes. ## Count the number of prefix bits shared by all nodes.
if nodes.len < 2: if nodes.len < 2:
return ID_SIZE return ID_SIZE
@ -176,7 +176,7 @@ proc computeSharedPrefixBits(nodes: openarray[Node]): int =
proc init(r: var RoutingTable, thisNode: Node) = proc init(r: var RoutingTable, thisNode: Node) =
r.thisNode = thisNode r.thisNode = thisNode
r.buckets = @[newKBucket(0.u256, high(Uint256))] r.buckets = @[newKBucket(0.u256, high(UInt256))]
randomize() # for later `randomNodes` selection randomize() # for later `randomNodes` selection
proc splitBucket(r: var RoutingTable, index: int) = proc splitBucket(r: var RoutingTable, index: int) =

View File

@ -32,7 +32,7 @@ proc initProtocolState*[T](state: T, x: Peer|EthereumNode)
{.gcsafe, raises: [Defect].} = {.gcsafe, raises: [Defect].} =
discard discard
proc initProtocolStates(peer: Peer, protocols: openarray[ProtocolInfo]) proc initProtocolStates(peer: Peer, protocols: openArray[ProtocolInfo])
{.raises: [Defect].} = {.raises: [Defect].} =
# Initialize all the active protocol states # Initialize all the active protocol states
newSeq(peer.protocolStates, allProtocols.len) newSeq(peer.protocolStates, allProtocols.len)

View File

@ -272,7 +272,7 @@ proc chooseFieldType(n: NimNode): NimNode =
## and selects the corresponding field type for use in the ## and selects the corresponding field type for use in the
## message object type (i.e. `p2p.hello`). ## message object type (i.e. `p2p.hello`).
## ##
## For now, only openarray types are remapped to sequences. ## For now, only openArray types are remapped to sequences.
result = n result = n
if n.kind == nnkBracketExpr and eqIdent(n[0], "openArray"): if n.kind == nnkBracketExpr and eqIdent(n[0], "openArray"):
result = n.copyNimTree result = n.copyNimTree
@ -352,7 +352,7 @@ proc augmentUserHandler(p: P2PProtocol, userHandlerProc: NimNode, msgId = -1) =
userHandlerProc.body.insert 0, prelude userHandlerProc.body.insert 0, prelude
# We allow the user handler to use `openarray` params, but we turn # We allow the user handler to use `openArray` params, but we turn
# those into sequences to make the `async` pragma happy. # those into sequences to make the `async` pragma happy.
for i in 1 ..< userHandlerProc.params.len: for i in 1 ..< userHandlerProc.params.len:
var param = userHandlerProc.params[i] var param = userHandlerProc.params[i]
@ -428,7 +428,7 @@ proc newMsg(protocol: P2PProtocol, kind: MessageKind, id: int,
for param, paramType in procDef.typedInputParams(skip = 1): for param, paramType in procDef.typedInputParams(skip = 1):
recFields.add newTree(nnkIdentDefs, recFields.add newTree(nnkIdentDefs,
newTree(nnkPostfix, ident("*"), param), # The fields are public newTree(nnkPostfix, ident("*"), param), # The fields are public
chooseFieldType(paramType), # some types such as openarray chooseFieldType(paramType), # some types such as openArray
newEmptyNode()) # are automatically remapped newEmptyNode()) # are automatically remapped
if recFields.len == 1 and protocol.useSingleRecordInlining: if recFields.len == 1 and protocol.useSingleRecordInlining:
@ -564,7 +564,7 @@ proc createSendProc*(msg: Message,
newEmptyNode(), newEmptyNode(),
newStmtList()) ## body newStmtList()) ## body
if proctype == nnkProcDef: if procType == nnkProcDef:
for p in msg.procDef.pragma: for p in msg.procDef.pragma:
if not eqIdent(p, "async"): if not eqIdent(p, "async"):
def.addPragma p def.addPragma p

View File

@ -170,7 +170,7 @@ proc numProtocols(d: Dispatcher): int =
d.activeProtocols.len d.activeProtocols.len
proc getDispatcher(node: EthereumNode, proc getDispatcher(node: EthereumNode,
otherPeerCapabilities: openarray[Capability]): Dispatcher = otherPeerCapabilities: openArray[Capability]): Dispatcher =
# TODO: sub-optimal solution until progress is made here: # TODO: sub-optimal solution until progress is made here:
# https://github.com/nim-lang/Nim/issues/7457 # https://github.com/nim-lang/Nim/issues/7457
# We should be able to find an existing dispatcher without allocating a new one # We should be able to find an existing dispatcher without allocating a new one
@ -945,7 +945,7 @@ proc checkUselessPeer(peer: Peer) {.raises: [UselessPeerError, Defect].} =
# XXX: Send disconnect + UselessPeer # XXX: Send disconnect + UselessPeer
raise newException(UselessPeerError, "Useless peer") raise newException(UselessPeerError, "Useless peer")
proc initPeerState*(peer: Peer, capabilities: openarray[Capability]) proc initPeerState*(peer: Peer, capabilities: openArray[Capability])
{.raises: [UselessPeerError, Defect].} = {.raises: [UselessPeerError, Defect].} =
peer.dispatcher = getDispatcher(peer.network, capabilities) peer.dispatcher = getDispatcher(peer.network, capabilities)
checkUselessPeer(peer) checkUselessPeer(peer)
@ -1015,7 +1015,7 @@ template `^`(arr): auto =
# variable as an open array # variable as an open array
arr.toOpenArray(0, `arr Len` - 1) arr.toOpenArray(0, `arr Len` - 1)
proc initSecretState(hs: var Handshake, authMsg, ackMsg: openarray[byte], proc initSecretState(hs: var Handshake, authMsg, ackMsg: openArray[byte],
p: Peer) = p: Peer) =
var secrets = hs.getSecrets(authMsg, ackMsg) var secrets = hs.getSecrets(authMsg, ackMsg)
initSecretState(secrets, p.secretsState) initSecretState(secrets, p.secretsState)
@ -1101,7 +1101,7 @@ proc rlpxConnect*(node: EthereumNode, remote: Node): Future[Peer] {.async.} =
result.waitSingleMsg(DevP2P.hello), result.waitSingleMsg(DevP2P.hello),
10.seconds) 10.seconds)
if not validatePubKeyInHello(response, remote.node.pubKey): if not validatePubKeyInHello(response, remote.node.pubkey):
warn "Remote nodeId is not its public key" # XXX: Do we care? warn "Remote nodeId is not its public key" # XXX: Do we care?
trace "DevP2P handshake completed", peer = remote, trace "DevP2P handshake completed", peer = remote,
@ -1145,7 +1145,7 @@ proc rlpxAccept*(node: EthereumNode,
result.network = node result.network = node
var handshake = var handshake =
HandShake.tryInit(node.rng[], node.keys, {auth.Responder}).tryGet Handshake.tryInit(node.rng[], node.keys, {auth.Responder}).tryGet
var ok = false var ok = false
try: try:

View File

@ -71,10 +71,10 @@ p2pProtocol eth(version = protocolVersion,
bestHash: KeccakHash, bestHash: KeccakHash,
genesisHash: KeccakHash) genesisHash: KeccakHash)
proc newBlockHashes(peer: Peer, hashes: openarray[NewBlockHashesAnnounce]) = proc newBlockHashes(peer: Peer, hashes: openArray[NewBlockHashesAnnounce]) =
discard discard
proc transactions(peer: Peer, transactions: openarray[Transaction]) = proc transactions(peer: Peer, transactions: openArray[Transaction]) =
discard discard
requestResponse: requestResponse:
@ -85,17 +85,17 @@ p2pProtocol eth(version = protocolVersion,
await response.send(peer.network.chain.getBlockHeaders(request)) await response.send(peer.network.chain.getBlockHeaders(request))
proc blockHeaders(p: Peer, headers: openarray[BlockHeader]) proc blockHeaders(p: Peer, headers: openArray[BlockHeader])
requestResponse: requestResponse:
proc getBlockBodies(peer: Peer, hashes: openarray[KeccakHash]) {.gcsafe.} = proc getBlockBodies(peer: Peer, hashes: openArray[KeccakHash]) {.gcsafe.} =
if hashes.len > maxBodiesFetch: if hashes.len > maxBodiesFetch:
await peer.disconnect(BreachOfProtocol) await peer.disconnect(BreachOfProtocol)
return return
await response.send(peer.network.chain.getBlockBodies(hashes)) await response.send(peer.network.chain.getBlockBodies(hashes))
proc blockBodies(peer: Peer, blocks: openarray[BlockBody]) proc blockBodies(peer: Peer, blocks: openArray[BlockBody])
proc newBlock(peer: Peer, bh: NewBlockAnnounce, totalDifficulty: DifficultyInt) = proc newBlock(peer: Peer, bh: NewBlockAnnounce, totalDifficulty: DifficultyInt) =
discard discard
@ -103,15 +103,15 @@ p2pProtocol eth(version = protocolVersion,
nextID 13 nextID 13
requestResponse: requestResponse:
proc getNodeData(peer: Peer, hashes: openarray[KeccakHash]) = proc getNodeData(peer: Peer, hashes: openArray[KeccakHash]) =
await response.send(peer.network.chain.getStorageNodes(hashes)) await response.send(peer.network.chain.getStorageNodes(hashes))
proc nodeData(peer: Peer, data: openarray[Blob]) proc nodeData(peer: Peer, data: openArray[Blob])
requestResponse: requestResponse:
proc getReceipts(peer: Peer, hashes: openarray[KeccakHash]) = discard proc getReceipts(peer: Peer, hashes: openArray[KeccakHash]) = discard
# TODO: implement `getReceipts` and reactivate this code # TODO: implement `getReceipts` and reactivate this code
# await response.send(peer.network.chain.getReceipts(hashes)) # await response.send(peer.network.chain.getReceipts(hashes))
proc receipts(peer: Peer, receipts: openarray[Receipt]) proc receipts(peer: Peer, receipts: openArray[Receipt])

View File

@ -144,13 +144,13 @@ template updateBV: BufValueInt =
bufValueAfterRequest(lesNetwork, lesPeer, bufValueAfterRequest(lesNetwork, lesPeer,
perProtocolMsgId, requestCostQuantity) perProtocolMsgId, requestCostQuantity)
func getValue(values: openarray[KeyValuePair], func getValue(values: openArray[KeyValuePair],
key: string, T: typedesc): Option[T] = key: string, T: typedesc): Option[T] =
for v in values: for v in values:
if v.key == key: if v.key == key:
return some(rlp.decode(v.value, T)) return some(rlp.decode(v.value, T))
func getRequiredValue(values: openarray[KeyValuePair], func getRequiredValue(values: openArray[KeyValuePair],
key: string, T: typedesc): T = key: string, T: typedesc): T =
for v in values: for v in values:
if v.key == key: if v.key == key:
@ -166,7 +166,7 @@ p2pProtocol les(version = lesVersion,
incomingRequestDecorator = incomingRequestDecorator, incomingRequestDecorator = incomingRequestDecorator,
incomingResponseThunkDecorator = incomingResponseDecorator): incomingResponseThunkDecorator = incomingResponseDecorator):
handshake: handshake:
proc status(p: Peer, values: openarray[KeyValuePair]) proc status(p: Peer, values: openArray[KeyValuePair])
onPeerConnected do (peer: Peer): onPeerConnected do (peer: Peer):
let let
@ -254,7 +254,7 @@ p2pProtocol les(version = lesVersion,
headNumber: BlockNumber, headNumber: BlockNumber,
headTotalDifficulty: DifficultyInt, headTotalDifficulty: DifficultyInt,
reorgDepth: BlockNumber, reorgDepth: BlockNumber,
values: openarray[KeyValuePair], values: openArray[KeyValuePair],
announceType: AnnounceType) = announceType: AnnounceType) =
if peer.state.announceType == AnnounceType.None: if peer.state.announceType == AnnounceType.None:
@ -288,7 +288,7 @@ p2pProtocol les(version = lesVersion,
proc blockHeaders( proc blockHeaders(
peer: Peer, peer: Peer,
bufValue: BufValueInt, bufValue: BufValueInt,
blocks: openarray[BlockHeader]) blocks: openArray[BlockHeader])
## On-damand data retrieval ## On-damand data retrieval
## ##
@ -296,7 +296,7 @@ p2pProtocol les(version = lesVersion,
requestResponse: requestResponse:
proc getBlockBodies( proc getBlockBodies(
peer: Peer, peer: Peer,
blocks: openarray[KeccakHash]) {. blocks: openArray[KeccakHash]) {.
costQuantity(blocks.len, max = maxBodiesFetch), gcsafe.} = costQuantity(blocks.len, max = maxBodiesFetch), gcsafe.} =
let blocks = peer.network.chain.getBlockBodies(blocks) let blocks = peer.network.chain.getBlockBodies(blocks)
@ -305,12 +305,12 @@ p2pProtocol les(version = lesVersion,
proc blockBodies( proc blockBodies(
peer: Peer, peer: Peer,
bufValue: BufValueInt, bufValue: BufValueInt,
bodies: openarray[BlockBody]) bodies: openArray[BlockBody])
requestResponse: requestResponse:
proc getReceipts( proc getReceipts(
peer: Peer, peer: Peer,
hashes: openarray[KeccakHash]) hashes: openArray[KeccakHash])
{.costQuantity(hashes.len, max = maxReceiptsFetch).} = {.costQuantity(hashes.len, max = maxReceiptsFetch).} =
let receipts = peer.network.chain.getReceipts(hashes) let receipts = peer.network.chain.getReceipts(hashes)
@ -319,12 +319,12 @@ p2pProtocol les(version = lesVersion,
proc receipts( proc receipts(
peer: Peer, peer: Peer,
bufValue: BufValueInt, bufValue: BufValueInt,
receipts: openarray[Receipt]) receipts: openArray[Receipt])
requestResponse: requestResponse:
proc getProofs( proc getProofs(
peer: Peer, peer: Peer,
proofs: openarray[ProofRequest]) {. proofs: openArray[ProofRequest]) {.
costQuantity(proofs.len, max = maxProofsFetch).} = costQuantity(proofs.len, max = maxProofsFetch).} =
let proofs = peer.network.chain.getProofs(proofs) let proofs = peer.network.chain.getProofs(proofs)
@ -333,7 +333,7 @@ p2pProtocol les(version = lesVersion,
proc proofs( proc proofs(
peer: Peer, peer: Peer,
bufValue: BufValueInt, bufValue: BufValueInt,
proofs: openarray[Blob]) proofs: openArray[Blob])
requestResponse: requestResponse:
proc getContractCodes( proc getContractCodes(
@ -354,7 +354,7 @@ p2pProtocol les(version = lesVersion,
requestResponse: requestResponse:
proc getHeaderProofs( proc getHeaderProofs(
peer: Peer, peer: Peer,
reqs: openarray[ProofRequest]) {. reqs: openArray[ProofRequest]) {.
costQuantity(reqs.len, max = maxHeaderProofsFetch).} = costQuantity(reqs.len, max = maxHeaderProofsFetch).} =
let proofs = peer.network.chain.getHeaderProofs(reqs) let proofs = peer.network.chain.getHeaderProofs(reqs)
@ -363,12 +363,12 @@ p2pProtocol les(version = lesVersion,
proc headerProofs( proc headerProofs(
peer: Peer, peer: Peer,
bufValue: BufValueInt, bufValue: BufValueInt,
proofs: openarray[Blob]) proofs: openArray[Blob])
requestResponse: requestResponse:
proc getHelperTrieProofs( proc getHelperTrieProofs(
peer: Peer, peer: Peer,
reqs: openarray[HelperTrieProofRequest]) {. reqs: openArray[HelperTrieProofRequest]) {.
costQuantity(reqs.len, max = maxProofsFetch).} = costQuantity(reqs.len, max = maxProofsFetch).} =
var nodes, auxData: seq[Blob] var nodes, auxData: seq[Blob]
@ -387,7 +387,7 @@ p2pProtocol les(version = lesVersion,
requestResponse: requestResponse:
proc sendTxV2( proc sendTxV2(
peer: Peer, peer: Peer,
transactions: openarray[Transaction]) {. transactions: openArray[Transaction]) {.
costQuantity(transactions.len, max = maxTransactionsFetch).} = costQuantity(transactions.len, max = maxTransactionsFetch).} =
let chain = peer.network.chain let chain = peer.network.chain
@ -409,7 +409,7 @@ p2pProtocol les(version = lesVersion,
proc getTxStatus( proc getTxStatus(
peer: Peer, peer: Peer,
transactions: openarray[Transaction]) {. transactions: openArray[Transaction]) {.
costQuantity(transactions.len, max = maxTransactionsFetch).} = costQuantity(transactions.len, max = maxTransactionsFetch).} =
let chain = peer.network.chain let chain = peer.network.chain
@ -422,7 +422,7 @@ p2pProtocol les(version = lesVersion,
proc txStatus( proc txStatus(
peer: Peer, peer: Peer,
bufValue: BufValueInt, bufValue: BufValueInt,
transactions: openarray[TransactionStatusMsg]) transactions: openArray[TransactionStatusMsg])
proc configureLes*(node: EthereumNode, proc configureLes*(node: EthereumNode,
# Client options: # Client options:

View File

@ -189,7 +189,7 @@ proc fullBloom*(): Bloom =
for i in 0..<result.len: for i in 0..<result.len:
result[i] = 0xFF result[i] = 0xFF
proc encryptAesGcm(plain: openarray[byte], key: SymKey, proc encryptAesGcm(plain: openArray[byte], key: SymKey,
iv: array[gcmIVLen, byte]): seq[byte] = iv: array[gcmIVLen, byte]): seq[byte] =
## Encrypt using AES-GCM, making sure to append tag and iv, in that order ## Encrypt using AES-GCM, making sure to append tag and iv, in that order
var gcm: GCM[aes256] var gcm: GCM[aes256]
@ -203,7 +203,7 @@ proc encryptAesGcm(plain: openarray[byte], key: SymKey,
result.add tag result.add tag
result.add iv result.add iv
proc decryptAesGcm(cipher: openarray[byte], key: SymKey): Option[seq[byte]] = proc decryptAesGcm(cipher: openArray[byte], key: SymKey): Option[seq[byte]] =
## Decrypt AES-GCM ciphertext and validate authenticity - assumes ## Decrypt AES-GCM ciphertext and validate authenticity - assumes
## cipher-tag-iv format of the buffer ## cipher-tag-iv format of the buffer
if cipher.len < gcmTagLen + gcmIVLen: if cipher.len < gcmTagLen + gcmIVLen:
@ -310,7 +310,7 @@ proc encode*(rng: var BrHmacDrbgContext, self: Payload): Option[seq[byte]] =
# No encryption! # No encryption!
return some(plain) return some(plain)
proc decode*(data: openarray[byte], dst = none[PrivateKey](), proc decode*(data: openArray[byte], dst = none[PrivateKey](),
symKey = none[SymKey]()): Option[DecodedPayload] = symKey = none[SymKey]()): Option[DecodedPayload] =
## Decode data into payload, potentially trying to decrypt if keys are ## Decode data into payload, potentially trying to decrypt if keys are
## provided ## provided

View File

@ -163,7 +163,7 @@ p2pProtocol Whisper(version = whisperVersion,
bloom: seq[byte], bloom: seq[byte],
isLightNode: bool) isLightNode: bool)
proc messages(peer: Peer, envelopes: openarray[Envelope]) = proc messages(peer: Peer, envelopes: openArray[Envelope]) =
if not peer.state.initialized: if not peer.state.initialized:
warn "Handshake not completed yet, discarding messages" warn "Handshake not completed yet, discarding messages"
return return

View File

@ -53,7 +53,7 @@ proc roundup16*(x: int): int {.inline.} =
template toa(a, b, c: untyped): untyped = template toa(a, b, c: untyped): untyped =
toOpenArray((a), (b), (b) + (c) - 1) toOpenArray((a), (b), (b) + (c) - 1)
proc sxor[T](a: var openarray[T], b: openarray[T]) {.inline.} = proc sxor[T](a: var openArray[T], b: openArray[T]) {.inline.} =
doAssert(len(a) == len(b)) doAssert(len(a) == len(b))
for i in 0 ..< len(a): for i in 0 ..< len(a):
a[i] = a[i] xor b[i] a[i] = a[i] xor b[i]
@ -80,9 +80,9 @@ template decryptedLength*(size: int): int =
## Returns size of decrypted message for body with length `size`. ## Returns size of decrypted message for body with length `size`.
roundup16(size) roundup16(size)
proc encrypt*(c: var SecretState, header: openarray[byte], proc encrypt*(c: var SecretState, header: openArray[byte],
frame: openarray[byte], frame: openArray[byte],
output: var openarray[byte]): RlpxResult[void] = output: var openArray[byte]): RlpxResult[void] =
## Encrypts `header` and `frame` using SecretState `c` context and store ## Encrypts `header` and `frame` using SecretState `c` context and store
## result into `output`. ## result into `output`.
## ##
@ -136,7 +136,7 @@ proc encrypt*(c: var SecretState, header: openarray[byte],
copyMem(addr output[frameMacPos], addr frameMac.data[0], RlpHeaderLength) copyMem(addr output[frameMacPos], addr frameMac.data[0], RlpHeaderLength)
ok() ok()
proc encryptMsg*(msg: openarray[byte], secrets: var SecretState): seq[byte] = proc encryptMsg*(msg: openArray[byte], secrets: var SecretState): seq[byte] =
var header: RlpxHeader var header: RlpxHeader
if uint32(msg.len) > maxUInt24: if uint32(msg.len) > maxUInt24:
@ -163,8 +163,8 @@ proc encryptMsg*(msg: openarray[byte], secrets: var SecretState): seq[byte] =
proc getBodySize*(a: RlpxHeader): int = proc getBodySize*(a: RlpxHeader): int =
(int(a[0]) shl 16) or (int(a[1]) shl 8) or int(a[2]) (int(a[0]) shl 16) or (int(a[1]) shl 8) or int(a[2])
proc decryptHeader*(c: var SecretState, data: openarray[byte], proc decryptHeader*(c: var SecretState, data: openArray[byte],
output: var openarray[byte]): RlpxResult[void] = output: var openArray[byte]): RlpxResult[void] =
## Decrypts header `data` using SecretState `c` context and store ## Decrypts header `data` using SecretState `c` context and store
## result into `output`. ## result into `output`.
## ##
@ -201,15 +201,15 @@ proc decryptHeader*(c: var SecretState, data: openarray[byte],
result = ok() result = ok()
proc decryptHeaderAndGetMsgSize*(c: var SecretState, proc decryptHeaderAndGetMsgSize*(c: var SecretState,
encryptedHeader: openarray[byte], encryptedHeader: openArray[byte],
outSize: var int): RlpxResult[void] = outSize: var int): RlpxResult[void] =
var decryptedHeader: RlpxHeader var decryptedHeader: RlpxHeader
result = decryptHeader(c, encryptedHeader, decryptedHeader) result = decryptHeader(c, encryptedHeader, decryptedHeader)
if result.isOk(): if result.isOk():
outSize = decryptedHeader.getBodySize outSize = decryptedHeader.getBodySize
proc decryptBody*(c: var SecretState, data: openarray[byte], bodysize: int, proc decryptBody*(c: var SecretState, data: openArray[byte], bodysize: int,
output: var openarray[byte], outlen: var int): RlpxResult[void] = output: var openArray[byte], outlen: var int): RlpxResult[void] =
## Decrypts body `data` using SecretState `c` context and store ## Decrypts body `data` using SecretState `c` context and store
## result into `output`. ## result into `output`.
## ##

View File

@ -353,7 +353,7 @@ proc readImpl[E](rlp: var Rlp, T: type seq[E]): T =
for elem in rlp: for elem in rlp:
result.add rlp.read(E) result.add rlp.read(E)
proc readImpl[E](rlp: var Rlp, T: type openarray[E]): seq[E] = proc readImpl[E](rlp: var Rlp, T: type openArray[E]): seq[E] =
result = readImpl(rlp, seq[E]) result = readImpl(rlp, seq[E])
proc readImpl(rlp: var Rlp, T: type[object|tuple], proc readImpl(rlp: var Rlp, T: type[object|tuple],
@ -406,7 +406,7 @@ proc `>>`*[T](rlp: var Rlp, location: var T) =
template readRecordType*(rlp: var Rlp, T: type, wrappedInList: bool): auto = template readRecordType*(rlp: var Rlp, T: type, wrappedInList: bool): auto =
readImpl(rlp, T, wrappedInList) readImpl(rlp, T, wrappedInList)
proc decode*(bytes: openarray[byte]): RlpNode = proc decode*(bytes: openArray[byte]): RlpNode =
var rlp = rlpFromBytes(bytes) var rlp = rlpFromBytes(bytes)
rlp.toNodes rlp.toNodes

View File

@ -130,10 +130,10 @@ proc appendBlob(self: var RlpWriter, data: openArray[byte], startMarker: byte) =
proc appendImpl(self: var RlpWriter, data: string) = proc appendImpl(self: var RlpWriter, data: string) =
appendBlob(self, data.toOpenArrayByte(0, data.high), BLOB_START_MARKER) appendBlob(self, data.toOpenArrayByte(0, data.high), BLOB_START_MARKER)
proc appendBlob(self: var RlpWriter, data: openarray[byte]) = proc appendBlob(self: var RlpWriter, data: openArray[byte]) =
appendBlob(self, data, BLOB_START_MARKER) appendBlob(self, data, BLOB_START_MARKER)
proc appendBlob(self: var RlpWriter, data: openarray[char]) = proc appendBlob(self: var RlpWriter, data: openArray[char]) =
appendBlob(self, data.toOpenArrayByte(0, data.high), BLOB_START_MARKER) appendBlob(self, data.toOpenArrayByte(0, data.high), BLOB_START_MARKER)
proc appendInt(self: var RlpWriter, i: Integer) = proc appendInt(self: var RlpWriter, i: Integer) =
@ -169,10 +169,10 @@ template appendImpl(self: var RlpWriter, e: enum) =
template appendImpl(self: var RlpWriter, b: bool) = template appendImpl(self: var RlpWriter, b: bool) =
appendImpl(self, int(b)) appendImpl(self, int(b))
proc appendImpl[T](self: var RlpWriter, listOrBlob: openarray[T]) = proc appendImpl[T](self: var RlpWriter, listOrBlob: openArray[T]) =
mixin append mixin append
# TODO: This append proc should be overloaded by `openarray[byte]` after # TODO: This append proc should be overloaded by `openArray[byte]` after
# nim bug #7416 is fixed. # nim bug #7416 is fixed.
when T is (byte or char): when T is (byte or char):
self.appendBlob(listOrBlob) self.appendBlob(listOrBlob)

View File

@ -74,13 +74,13 @@ proc hashAndSave*(self: BinaryTrie, node: openArray[byte]): TrieNodeKey =
self.db.put(result, node) self.db.put(result, node)
template saveKV(self: BinaryTrie, keyPath: TrieBitSeq | bool, child: openArray[byte]): untyped = template saveKV(self: BinaryTrie, keyPath: TrieBitSeq | bool, child: openArray[byte]): untyped =
self.hashAndsave(encodeKVNode(keyPath, child)) self.hashAndSave(encodeKVNode(keyPath, child))
template saveLeaf(self: BinaryTrie, value: openArray[byte]): untyped = template saveLeaf(self: BinaryTrie, value: openArray[byte]): untyped =
self.hashAndsave(encodeLeafNode(value)) self.hashAndSave(encodeLeafNode(value))
template saveBranch(self: BinaryTrie, L, R: openArray[byte]): untyped = template saveBranch(self: BinaryTrie, L, R: openArray[byte]): untyped =
self.hashAndsave(encodeBranchNode(L, R)) self.hashAndSave(encodeBranchNode(L, R))
proc setBranchNode(self: BinaryTrie, keyPath: TrieBitSeq, node: TrieNode, proc setBranchNode(self: BinaryTrie, keyPath: TrieBitSeq, node: TrieNode,
value: openArray[byte], deleteSubtrie = false): TrieNodeKey value: openArray[byte], deleteSubtrie = false): TrieNodeKey

View File

@ -145,7 +145,7 @@ proc getWitnessImpl*(db: DB; nodeHash: TrieNodeKey; keyPath: TrieBitSeq; output:
raise newException(InvalidKeyError, "Key too long") raise newException(InvalidKeyError, "Key too long")
of KV_TYPE: of KV_TYPE:
output.add nodeVal output.add nodeVal
if keyPath.len < node.keyPath.len and node.keyPath[0..<keyPath.len] == keypath: if keyPath.len < node.keyPath.len and node.keyPath[0..<keyPath.len] == keyPath:
if not getTrieNodesImpl(db, node.child, output): return if not getTrieNodesImpl(db, node.child, output): return
elif keyPath[0..<node.keyPath.len] == node.keyPath: elif keyPath[0..<node.keyPath.len] == node.keyPath:
getWitnessImpl(db, node.child, keyPath.sliceToEnd(node.keyPath.len), output) getWitnessImpl(db, node.child, keyPath.sliceToEnd(node.keyPath.len), output)

View File

@ -15,17 +15,17 @@ type
deleted: HashSet[seq[byte]] deleted: HashSet[seq[byte]]
# XXX: poor's man vtref types # XXX: poor's man vtref types
PutProc = proc (db: RootRef, key, val: openarray[byte]) {. PutProc = proc (db: RootRef, key, val: openArray[byte]) {.
gcsafe, raises: [Defect].} gcsafe, raises: [Defect].}
GetProc = proc (db: RootRef, key: openarray[byte]): seq[byte] {. GetProc = proc (db: RootRef, key: openArray[byte]): seq[byte] {.
gcsafe, raises: [Defect].} gcsafe, raises: [Defect].}
## The result will be empty seq if not found ## The result will be empty seq if not found
DelProc = proc (db: RootRef, key: openarray[byte]) {. DelProc = proc (db: RootRef, key: openArray[byte]) {.
gcsafe, raises: [Defect].} gcsafe, raises: [Defect].}
ContainsProc = proc (db: RootRef, key: openarray[byte]): bool {. ContainsProc = proc (db: RootRef, key: openArray[byte]): bool {.
gcsafe, raises: [Defect].} gcsafe, raises: [Defect].}
TrieDatabaseRef* = ref object TrieDatabaseRef* = ref object
@ -49,19 +49,19 @@ type
TransactionID* = distinct DbTransaction TransactionID* = distinct DbTransaction
proc put*(db: TrieDatabaseRef, key, val: openarray[byte]) {.gcsafe.} proc put*(db: TrieDatabaseRef, key, val: openArray[byte]) {.gcsafe.}
proc get*(db: TrieDatabaseRef, key: openarray[byte]): seq[byte] {.gcsafe.} proc get*(db: TrieDatabaseRef, key: openArray[byte]): seq[byte] {.gcsafe.}
proc del*(db: TrieDatabaseRef, key: openarray[byte]) {.gcsafe.} proc del*(db: TrieDatabaseRef, key: openArray[byte]) {.gcsafe.}
proc beginTransaction*(db: TrieDatabaseRef): DbTransaction {.gcsafe.} proc beginTransaction*(db: TrieDatabaseRef): DbTransaction {.gcsafe.}
proc keccak*(r: openArray[byte]): KeccakHash = proc keccak*(r: openArray[byte]): KeccakHash =
keccak256.digest r keccak256.digest r
proc get*(db: MemoryLayer, key: openarray[byte]): seq[byte] = proc get*(db: MemoryLayer, key: openArray[byte]): seq[byte] =
result = db.records.getOrDefault(@key).value result = db.records.getOrDefault(@key).value
traceGet key, result traceGet key, result
proc del*(db: MemoryLayer, key: openarray[byte]) = proc del*(db: MemoryLayer, key: openArray[byte]) =
traceDel key traceDel key
# The database should ensure that the empty key is always active: # The database should ensure that the empty key is always active:
@ -76,10 +76,10 @@ proc del*(db: MemoryLayer, key: openarray[byte]) =
db.records.del(key) db.records.del(key)
db.deleted.incl(key) db.deleted.incl(key)
proc contains*(db: MemoryLayer, key: openarray[byte]): bool = proc contains*(db: MemoryLayer, key: openArray[byte]): bool =
db.records.hasKey(@key) db.records.hasKey(@key)
proc put*(db: MemoryLayer, key, val: openarray[byte]) = proc put*(db: MemoryLayer, key, val: openArray[byte]) =
tracePut key, val tracePut key, val
# TODO: This is quite inefficient and it won't be necessary once # TODO: This is quite inefficient and it won't be necessary once
@ -168,19 +168,19 @@ proc safeDispose*(t: DbTransaction) {.inline.} =
if (not isNil(t)) and (t.state == Pending): if (not isNil(t)) and (t.state == Pending):
t.rollback() t.rollback()
proc putImpl[T](db: RootRef, key, val: openarray[byte]) = proc putImpl[T](db: RootRef, key, val: openArray[byte]) =
mixin put mixin put
put(T(db), key, val) put(T(db), key, val)
proc getImpl[T](db: RootRef, key: openarray[byte]): seq[byte] = proc getImpl[T](db: RootRef, key: openArray[byte]): seq[byte] =
mixin get mixin get
return get(T(db), key) return get(T(db), key)
proc delImpl[T](db: RootRef, key: openarray[byte]) = proc delImpl[T](db: RootRef, key: openArray[byte]) =
mixin del mixin del
del(T(db), key) del(T(db), key)
proc containsImpl[T](db: RootRef, key: openarray[byte]): bool = proc containsImpl[T](db: RootRef, key: openArray[byte]): bool =
mixin contains mixin contains
return contains(T(db), key) return contains(T(db), key)
@ -194,14 +194,14 @@ proc trieDB*[T: RootRef](x: T): TrieDatabaseRef =
result.delProc = delImpl[T] result.delProc = delImpl[T]
result.containsProc = containsImpl[T] result.containsProc = containsImpl[T]
proc put*(db: TrieDatabaseRef, key, val: openarray[byte]) = proc put*(db: TrieDatabaseRef, key, val: openArray[byte]) =
var t = db.mostInnerTransaction var t = db.mostInnerTransaction
if t != nil: if t != nil:
t.modifications.put(key, val) t.modifications.put(key, val)
else: else:
db.putProc(db.obj, key, val) db.putProc(db.obj, key, val)
proc get*(db: TrieDatabaseRef, key: openarray[byte]): seq[byte] = proc get*(db: TrieDatabaseRef, key: openArray[byte]): seq[byte] =
# TODO: This is quite inefficient and it won't be necessary once # TODO: This is quite inefficient and it won't be necessary once
# https://github.com/nim-lang/Nim/issues/7457 is developed. # https://github.com/nim-lang/Nim/issues/7457 is developed.
let key = @key let key = @key
@ -216,14 +216,14 @@ proc get*(db: TrieDatabaseRef, key: openarray[byte]): seq[byte] =
if db.getProc != nil: if db.getProc != nil:
result = db.getProc(db.obj, key) result = db.getProc(db.obj, key)
proc del*(db: TrieDatabaseRef, key: openarray[byte]) = proc del*(db: TrieDatabaseRef, key: openArray[byte]) =
var t = db.mostInnerTransaction var t = db.mostInnerTransaction
if t != nil: if t != nil:
t.modifications.del(key) t.modifications.del(key)
else: else:
db.delProc(db.obj, key) db.delProc(db.obj, key)
proc contains*(db: TrieDatabaseRef, key: openarray[byte]): bool = proc contains*(db: TrieDatabaseRef, key: openArray[byte]): bool =
# TODO: This is quite inefficient and it won't be necessary once # TODO: This is quite inefficient and it won't be necessary once
# https://github.com/nim-lang/Nim/issues/7457 is developed. # https://github.com/nim-lang/Nim/issues/7457 is developed.
let key = @key let key = @key

View File

@ -45,7 +45,7 @@ proc addSample*(h: var DelayHistogram, sample: uint32, currentTime: Moment) =
h.delayBaseHistory[h.delayBaseIdx] = sample h.delayBaseHistory[h.delayBaseIdx] = sample
if wrapCompareLess(sample, h.delayBase): if wrapCompareLess(sample, h.delayBase):
h.delay_base = sample h.delayBase = sample
let delay = sample - h.delayBase let delay = sample - h.delayBase

View File

@ -255,7 +255,6 @@ proc ackPacket*(
ackNr: ackNr ackNr: ackNr
) )
Packet(header: h, eack: extensionData, payload: @[]) Packet(header: h, eack: extensionData, payload: @[])
proc dataPacket*( proc dataPacket*(

View File

@ -830,7 +830,7 @@ proc ackPacket(socket: UtpSocket, seqNr: uint16, currentTime: Moment): AckResult
proc ackPackets(socket: UtpSocket, nrPacketsToAck: uint16, currentTime: Moment) = proc ackPackets(socket: UtpSocket, nrPacketsToAck: uint16, currentTime: Moment) =
## Ack packets in outgoing buffer based on ack number in the received packet ## Ack packets in outgoing buffer based on ack number in the received packet
var i = 0 var i = 0
while i < int(nrPacketsToack): while i < int(nrPacketsToAck):
let result = socket.ackPacket(socket.seqNr - socket.curWindowPackets, currentTime) let result = socket.ackPacket(socket.seqNr - socket.curWindowPackets, currentTime)
case result case result
of PacketAcked: of PacketAcked:
@ -847,7 +847,7 @@ proc calculateAckedbytes(socket: UtpSocket, nrPacketsToAck: uint16, now: Moment)
var i: uint16 = 0 var i: uint16 = 0
var ackedBytes: uint32 = 0 var ackedBytes: uint32 = 0
var minRtt: Duration = InfiniteDuration var minRtt: Duration = InfiniteDuration
while i < nrPacketsToack: while i < nrPacketsToAck:
let seqNr = socket.seqNr - socket.curWindowPackets + i let seqNr = socket.seqNr - socket.curWindowPackets + i
let packetOpt = socket.outBuffer.get(seqNr) let packetOpt = socket.outBuffer.get(seqNr)
if (packetOpt.isSome() and packetOpt.unsafeGet().transmissions > 0): if (packetOpt.isSome() and packetOpt.unsafeGet().transmissions > 0):
@ -1237,7 +1237,7 @@ proc numOfBytesInFlight*(socket: UtpSocket): uint32 = socket.sendBufferTracker.c
# It throws assertion error when number of elements in buffer do not equal kept counter # It throws assertion error when number of elements in buffer do not equal kept counter
proc numPacketsInReordedBuffer*(socket: UtpSocket): int = proc numPacketsInReordedBuffer*(socket: UtpSocket): int =
var num = 0 var num = 0
for e in socket.inBUffer.items(): for e in socket.inBuffer.items():
if e.isSome(): if e.isSome():
inc num inc num
doAssert(num == int(socket.reorderCount)) doAssert(num == int(socket.reorderCount))

View File

@ -29,7 +29,7 @@ procSuite "SqStoreRef":
let insertStmt = db.prepareStmt( let insertStmt = db.prepareStmt(
"INSERT INTO records(value) VALUES (?);", "INSERT INTO records(value) VALUES (?);",
openarray[byte], void).get openArray[byte], void).get
let insert1Res = insertStmt.exec [byte 1, 2, 3, 4] let insert1Res = insertStmt.exec [byte 1, 2, 3, 4]
let insert2Res = insertStmt.exec @[] let insert2Res = insertStmt.exec @[]
@ -63,11 +63,11 @@ procSuite "SqStoreRef":
let selectRangeStmt = db.prepareStmt( let selectRangeStmt = db.prepareStmt(
"SELECT value FROM records WHERE key >= ? and key < ?;", "SELECT value FROM records WHERE key >= ? and key < ?;",
(int64, int64), openarray[byte]).get (int64, int64), openArray[byte]).get
block: block:
var allBytes = newSeq[byte]() var allBytes = newSeq[byte]()
let selectRangeRes = selectRangeStmt.exec((0'i64, 5'i64)) do (bytes: openarray[byte]) {.gcsafe.}: let selectRangeRes = selectRangeStmt.exec((0'i64, 5'i64)) do (bytes: openArray[byte]) {.gcsafe.}:
allBytes.add byte(bytes.len) allBytes.add byte(bytes.len)
allBytes.add bytes allBytes.add bytes
@ -80,7 +80,7 @@ procSuite "SqStoreRef":
0, 0,
1, 5] 1, 5]
block: block:
let selectRangeRes = selectRangeStmt.exec((10'i64, 20'i64)) do (bytes: openarray[byte]): let selectRangeRes = selectRangeStmt.exec((10'i64, 20'i64)) do (bytes: openArray[byte]):
echo "Got unexpected bytes: ", bytes echo "Got unexpected bytes: ", bytes
check: check:
@ -106,7 +106,7 @@ procSuite "SqStoreRef":
] ]
test "Tuple with byte arrays support": test "Tuple with byte arrays support":
# openarray[byte] requires either Nim 1.4 # openArray[byte] requires either Nim 1.4
# or hardcoding the seq[byte] and array[N, byte] paths # or hardcoding the seq[byte] and array[N, byte] paths
let db = SqStoreRef.init("", "test", inMemory = true)[] let db = SqStoreRef.init("", "test", inMemory = true)[]
defer: db.close() defer: db.close()

View File

@ -6,14 +6,14 @@ test:
block: block:
let decoded = decodeMessage(payload) let decoded = decodeMessage(payload)
if decoded.isOK(): if decoded.isOk():
let message = decoded.get() let message = decoded.get()
var encoded: seq[byte] var encoded: seq[byte]
case message.kind case message.kind
of unused: break of unused: break
of ping: encoded = encodeMessage(message.ping, message.reqId) of ping: encoded = encodeMessage(message.ping, message.reqId)
of pong: encoded = encodeMessage(message.pong, message.reqId) of pong: encoded = encodeMessage(message.pong, message.reqId)
of findNode: encoded = encodeMessage(message.findNode, message.reqId) of findnode: encoded = encodeMessage(message.findNode, message.reqId)
of nodes: encoded = encodeMessage(message.nodes, message.reqId) of nodes: encoded = encodeMessage(message.nodes, message.reqId)
of talkreq: encoded = encodeMessage(message.talkreq, message.reqId) of talkreq: encoded = encodeMessage(message.talkreq, message.reqId)
of talkresp: encoded = encodeMessage(message.talkresp, message.reqId) of talkresp: encoded = encodeMessage(message.talkresp, message.reqId)

View File

@ -10,7 +10,7 @@ type
test1: uint32 test1: uint32
test2: string test2: string
template testDecode(payload: openarray, T: type) = template testDecode(payload: openArray, T: type) =
try: try:
discard rlp.decode(payload, T) discard rlp.decode(payload, T)
except RlpError as e: except RlpError as e:

View File

@ -245,7 +245,7 @@ suite "KeyFile test suite":
jobject = createKeyFileJson(seckey1, "miawmiawcat", 3, AES128CTR, SCRYPT)[] jobject = createKeyFileJson(seckey1, "miawmiawcat", 3, AES128CTR, SCRYPT)[]
privKey = decodeKeyFileJson(jobject, "miawmiawcat")[] privKey = decodeKeyFileJson(jobject, "miawmiawcat")[]
check privKey.toRaw == secKey1.toRaw check privKey.toRaw == seckey1.toRaw
test "Load non-existent pathname test": test "Load non-existent pathname test":
check: check:

View File

@ -16,7 +16,7 @@ import
from strutils import toLowerAscii from strutils import toLowerAscii
proc compare(x: openarray[byte], y: openarray[byte]): bool = proc compare(x: openArray[byte], y: openArray[byte]): bool =
result = len(x) == len(y) result = len(x) == len(y)
if result: if result:
for i in 0..(len(x) - 1): for i in 0..(len(x) - 1):

View File

@ -11,8 +11,8 @@ proc localAddress*(port: int): Address =
proc initDiscoveryNode*(rng: ref BrHmacDrbgContext, privKey: PrivateKey, proc initDiscoveryNode*(rng: ref BrHmacDrbgContext, privKey: PrivateKey,
address: Address, address: Address,
bootstrapRecords: openarray[Record] = [], bootstrapRecords: openArray[Record] = [],
localEnrFields: openarray[(string, seq[byte])] = [], localEnrFields: openArray[(string, seq[byte])] = [],
previousRecord = none[enr.Record]()): previousRecord = none[enr.Record]()):
discv5_protocol.Protocol = discv5_protocol.Protocol =
# set bucketIpLimit to allow bucket split # set bucketIpLimit to allow bucket split
@ -30,13 +30,13 @@ proc initDiscoveryNode*(rng: ref BrHmacDrbgContext, privKey: PrivateKey,
result.open() result.open()
proc nodeIdInNodes*(id: NodeId, nodes: openarray[Node]): bool = proc nodeIdInNodes*(id: NodeId, nodes: openArray[Node]): bool =
for n in nodes: for n in nodes:
if id == n.id: return true if id == n.id: return true
proc generateNode*(privKey: PrivateKey, port: int = 20302, proc generateNode*(privKey: PrivateKey, port: int = 20302,
ip: ValidIpAddress = ValidIpAddress.init("127.0.0.1"), ip: ValidIpAddress = ValidIpAddress.init("127.0.0.1"),
localEnrFields: openarray[FieldPair] = []): Node = localEnrFields: openArray[FieldPair] = []): Node =
let port = Port(port) let port = Port(port)
let enr = enr.Record.init(1, privKey, some(ip), let enr = enr.Record.init(1, privKey, some(ip),
some(port), some(port), localEnrFields).expect("Properly intialized private key") some(port), some(port), localEnrFields).expect("Properly intialized private key")

View File

@ -33,7 +33,7 @@ proc packData(payload: openArray[byte], pk: PrivateKey): seq[byte] =
msgHash = keccak256.digest(signature & payloadSeq) msgHash = keccak256.digest(signature & payloadSeq)
result = @(msgHash.data) & signature & payloadSeq result = @(msgHash.data) & signature & payloadSeq
proc nodeIdInNodes(id: NodeId, nodes: openarray[Node]): bool = proc nodeIdInNodes(id: NodeId, nodes: openArray[Node]): bool =
for n in nodes: for n in nodes:
if id == n.id: return true if id == n.id: return true

View File

@ -676,7 +676,7 @@ suite "Discovery v5 Tests":
# Check handshake duplicates # Check handshake duplicates
check receiveNode.codec.handshakes.len == 1 check receiveNode.codec.handshakes.len == 1
# Check if it is for the first packet that a handshake is stored # Check if it is for the first packet that a handshake is stored
let key = HandShakeKey(nodeId: sendNode.id, address: a) let key = HandshakeKey(nodeId: sendNode.id, address: a)
check receiveNode.codec.handshakes[key].whoareyouData.requestNonce == check receiveNode.codec.handshakes[key].whoareyouData.requestNonce ==
firstRequestNonce firstRequestNonce

View File

@ -292,7 +292,7 @@ suite "Discovery v5.1 Packet Encodings Test Vectors":
let decoded = codecB.decodePacket(nodeA.address.get(), let decoded = codecB.decodePacket(nodeA.address.get(),
hexToSeqByte(encodedPacket)) hexToSeqByte(encodedPacket))
check: check:
decoded.isOK() decoded.isOk()
decoded.get().messageOpt.isSome() decoded.get().messageOpt.isSome()
decoded.get().messageOpt.get().reqId.id == hexToSeqByte(pingReqId) decoded.get().messageOpt.get().reqId.id == hexToSeqByte(pingReqId)
decoded.get().messageOpt.get().kind == ping decoded.get().messageOpt.get().kind == ping
@ -313,7 +313,7 @@ suite "Discovery v5.1 Packet Encodings Test Vectors":
hexToSeqByte(encodedPacket)) hexToSeqByte(encodedPacket))
check: check:
decoded.isOK() decoded.isOk()
decoded.get().flag == Flag.Whoareyou decoded.get().flag == Flag.Whoareyou
decoded.get().whoareyou.requestNonce == hexToByteArray[gcmNonceSize](whoareyouRequestNonce) decoded.get().whoareyou.requestNonce == hexToByteArray[gcmNonceSize](whoareyouRequestNonce)
decoded.get().whoareyou.idNonce == hexToByteArray[idNonceSize](whoareyouIdNonce) decoded.get().whoareyou.idNonce == hexToByteArray[idNonceSize](whoareyouIdNonce)
@ -352,7 +352,7 @@ suite "Discovery v5.1 Packet Encodings Test Vectors":
challengeData: hexToSeqByte(whoareyouChallengeData)) challengeData: hexToSeqByte(whoareyouChallengeData))
pubkey = some(privKeyA.toPublicKey()) pubkey = some(privKeyA.toPublicKey())
challenge = Challenge(whoareyouData: whoareyouData, pubkey: pubkey) challenge = Challenge(whoareyouData: whoareyouData, pubkey: pubkey)
key = HandShakeKey(nodeId: nodeA.id, address: nodeA.address.get()) key = HandshakeKey(nodeId: nodeA.id, address: nodeA.address.get())
check: not codecB.handshakes.hasKeyOrPut(key, challenge) check: not codecB.handshakes.hasKeyOrPut(key, challenge)
@ -402,7 +402,7 @@ suite "Discovery v5.1 Packet Encodings Test Vectors":
challengeData: hexToSeqByte(whoareyouChallengeData)) challengeData: hexToSeqByte(whoareyouChallengeData))
pubkey = none(PublicKey) pubkey = none(PublicKey)
challenge = Challenge(whoareyouData: whoareyouData, pubkey: pubkey) challenge = Challenge(whoareyouData: whoareyouData, pubkey: pubkey)
key = HandShakeKey(nodeId: nodeA.id, address: nodeA.address.get()) key = HandshakeKey(nodeId: nodeA.id, address: nodeA.address.get())
check: not codecB.handshakes.hasKeyOrPut(key, challenge) check: not codecB.handshakes.hasKeyOrPut(key, challenge)
@ -520,7 +520,7 @@ suite "Discovery v5.1 Additional Encode/Decode":
let decoded = codecB.decodePacket(nodeA.address.get(), data) let decoded = codecB.decodePacket(nodeA.address.get(), data)
let key = HandShakeKey(nodeId: nodeB.id, address: nodeB.address.get()) let key = HandshakeKey(nodeId: nodeB.id, address: nodeB.address.get())
var challenge: Challenge var challenge: Challenge
check: check:

View File

@ -14,7 +14,7 @@ import
nimcrypto/[utils, sha2, hmac, rijndael], nimcrypto/[utils, sha2, hmac, rijndael],
../../eth/keys, ../../eth/p2p/ecies ../../eth/keys, ../../eth/p2p/ecies
proc compare[A, B](x: openarray[A], y: openarray[B], s: int = 0): bool = proc compare[A, B](x: openArray[A], y: openArray[B], s: int = 0): bool =
result = true result = true
doAssert(s >= 0) doAssert(s >= 0)
var size = if s == 0: min(len(x), len(y)) else: min(s, min(len(x), len(y))) var size = if s == 0: min(len(x), len(y)) else: min(s, min(len(x), len(y)))

View File

@ -6,7 +6,7 @@ import
../../eth/keys, ../../eth/p2p/discoveryv5/[routing_table, node, enr], ../../eth/keys, ../../eth/p2p/discoveryv5/[routing_table, node, enr],
./discv5_test_helper ./discv5_test_helper
func customDistance*(a, b: NodeId): Uint256 = func customDistance*(a, b: NodeId): UInt256 =
if a >= b: if a >= b:
a - b a - b
else: else:

View File

@ -226,11 +226,11 @@ suite "Whisper envelope":
var env = Envelope(ttl: 1, data: @[byte 0xde, 0xad, 0xbe, 0xef]) var env = Envelope(ttl: 1, data: @[byte 0xde, 0xad, 0xbe, 0xef])
# PoW calculation with no leading zeroes # PoW calculation with no leading zeroes
env.nonce = 100000 env.nonce = 100000
check hashAndPoW(env) == ("A788E02A95BFC673709E97CA81E39CA903BAD5638D3388964C51EB64952172D6", check hashAndPow(env) == ("A788E02A95BFC673709E97CA81E39CA903BAD5638D3388964C51EB64952172D6",
0.07692307692307693) 0.07692307692307693)
# PoW calculation with 8 leading zeroes # PoW calculation with 8 leading zeroes
env.nonce = 276 env.nonce = 276
check hashAndPoW(env) == ("00E2374C6353C243E4073E209A7F2ACB2506522AF318B3B78CF9A88310A2A11C", check hashAndPow(env) == ("00E2374C6353C243E4073E209A7F2ACB2506522AF318B3B78CF9A88310A2A11C",
19.692307692307693) 19.692307692307693)
suite "Whisper queue": suite "Whisper queue":

View File

@ -105,7 +105,7 @@ suite "test api usage":
rlp.listElem(2).toString == "Donec ligula tortor, egestas eu est vitae" rlp.listElem(2).toString == "Donec ligula tortor, egestas eu est vitae"
# test creating RLPs from other RLPs # test creating RLPs from other RLPs
var list = rlpFromBytes encodeList(rlp.listELem(1), rlp.listELem(0)) var list = rlpFromBytes encodeList(rlp.listElem(1), rlp.listElem(0))
# test that iteration with enterList/skipElem works as expected # test that iteration with enterList/skipElem works as expected
doAssert list.enterList # We already know that we are working with a list doAssert list.enterList # We already know that we are working with a list
@ -191,7 +191,7 @@ suite "test api usage":
Inf, NegInf, NaN]: Inf, NegInf, NaN]:
template isNaN(n): bool = template isNaN(n): bool =
classify(n) == fcNaN classify(n) == fcNan
template chk(input) = template chk(input) =
let restored = decode(encode(input), float64) let restored = decode(encode(input), float64)

View File

@ -14,7 +14,7 @@ type
proc cmp(lhs, rhs: TestOp): int = cmp(lhs.idx, rhs.idx) proc cmp(lhs, rhs: TestOp): int = cmp(lhs.idx, rhs.idx)
proc `<=`(lhs, rhs: TestOp): bool = lhs.idx <= rhs.idx proc `<=`(lhs, rhs: TestOp): bool = lhs.idx <= rhs.idx
proc runSingleTest(testSequence: openarray[TestOp], proc runSingleTest(testSequence: openArray[TestOp],
secureMode: bool, secureMode: bool,
expectedRootHash: string): bool = expectedRootHash: string): bool =
var var

View File

@ -21,8 +21,8 @@ proc localAddress*(port: int): Address =
proc initDiscoveryNode*(rng: ref BrHmacDrbgContext, proc initDiscoveryNode*(rng: ref BrHmacDrbgContext,
privKey: PrivateKey, privKey: PrivateKey,
address: Address, address: Address,
bootstrapRecords: openarray[Record] = [], bootstrapRecords: openArray[Record] = [],
localEnrFields: openarray[(string, seq[byte])] = [], localEnrFields: openArray[(string, seq[byte])] = [],
previousRecord = none[enr.Record]()): discv5_protocol.Protocol = previousRecord = none[enr.Record]()): discv5_protocol.Protocol =
# set bucketIpLimit to allow bucket split # set bucketIpLimit to allow bucket split
let tableIpLimits = TableIpLimits(tableIpLimit: 1000, bucketIpLimit: 24) let tableIpLimits = TableIpLimits(tableIpLimit: 1000, bucketIpLimit: 24)

View File

@ -192,7 +192,7 @@ procSuite "Utp protocol over udp tests":
yield futSock yield futSock
check: check:
futSock.finished() and (not futsock.failed()) and (not futsock.cancelled()) futSock.finished() and (not futSock.failed()) and (not futSock.cancelled())
server2Called.isSet() server2Called.isSet()
await utpProt1.shutdownWait() await utpProt1.shutdownWait()

View File

@ -1134,7 +1134,7 @@ procSuite "Utp socket unit test":
let q = newAsyncQueue[Packet]() let q = newAsyncQueue[Packet]()
let initialRemoteSeq = 10'u16 let initialRemoteSeq = 10'u16
let someData1 = @[1'u8] let someData1 = @[1'u8]
let somedata2 = @[2'u8] let someData2 = @[2'u8]
let (outgoingSocket, initialPacket) = let (outgoingSocket, initialPacket) =
connectOutGoingSocket( connectOutGoingSocket(
initialRemoteSeq, initialRemoteSeq,
@ -1181,7 +1181,7 @@ procSuite "Utp socket unit test":
check: check:
writeFut2.finished() writeFut2.finished()
firstPacket.payload == someData1 firstPacket.payload == someData1
secondPacket.payload == somedata2 secondPacket.payload == someData2
await outgoingSocket.destroyWait() await outgoingSocket.destroyWait()