mirror of https://github.com/status-im/nim-eth.git
Make discv5 timeouts configurable (#523)
This commit is contained in:
parent
a5ea6a9a90
commit
883825aad7
|
@ -119,15 +119,17 @@ const
|
||||||
ipMajorityInterval = 5.minutes ## Interval for checking the latest IP:Port
|
ipMajorityInterval = 5.minutes ## Interval for checking the latest IP:Port
|
||||||
## majority and updating this when ENR auto update is set.
|
## majority and updating this when ENR auto update is set.
|
||||||
initialLookups = 1 ## Amount of lookups done when populating the routing table
|
initialLookups = 1 ## Amount of lookups done when populating the routing table
|
||||||
handshakeTimeout* = 2.seconds ## timeout for the reply on the
|
defaultHandshakeTimeout* = 2.seconds ## timeout for the reply on the
|
||||||
## whoareyou message
|
## whoareyou message
|
||||||
responseTimeout* = 4.seconds ## timeout for the response of a request-response
|
defaultResponseTimeout* = 4.seconds ## timeout for the response of a request-response
|
||||||
## call
|
## call
|
||||||
|
|
||||||
type
|
type
|
||||||
DiscoveryConfig* = object
|
DiscoveryConfig* = object
|
||||||
tableIpLimits*: TableIpLimits
|
tableIpLimits*: TableIpLimits
|
||||||
bitsPerHop*: int
|
bitsPerHop*: int
|
||||||
|
handshakeTimeout: Duration
|
||||||
|
responseTimeout: Duration
|
||||||
|
|
||||||
Protocol* = ref object
|
Protocol* = ref object
|
||||||
transp: DatagramTransport
|
transp: DatagramTransport
|
||||||
|
@ -147,6 +149,8 @@ type
|
||||||
enrAutoUpdate: bool
|
enrAutoUpdate: bool
|
||||||
talkProtocols*: Table[seq[byte], TalkProtocol] # TODO: Table is a bit of
|
talkProtocols*: Table[seq[byte], TalkProtocol] # TODO: Table is a bit of
|
||||||
# overkill here, use sequence
|
# overkill here, use sequence
|
||||||
|
handshakeTimeout: Duration
|
||||||
|
responseTimeout: Duration
|
||||||
rng*: ref HmacDrbgContext
|
rng*: ref HmacDrbgContext
|
||||||
|
|
||||||
PendingRequest = object
|
PendingRequest = object
|
||||||
|
@ -166,7 +170,10 @@ type
|
||||||
const
|
const
|
||||||
defaultDiscoveryConfig* = DiscoveryConfig(
|
defaultDiscoveryConfig* = DiscoveryConfig(
|
||||||
tableIpLimits: DefaultTableIpLimits,
|
tableIpLimits: DefaultTableIpLimits,
|
||||||
bitsPerHop: DefaultBitsPerHop)
|
bitsPerHop: DefaultBitsPerHop,
|
||||||
|
handshakeTimeout: defaultHandshakeTimeout,
|
||||||
|
responseTimeout: defaultResponseTimeout
|
||||||
|
)
|
||||||
|
|
||||||
proc addNode*(d: Protocol, node: Node): bool =
|
proc addNode*(d: Protocol, node: Node): bool =
|
||||||
## Add `Node` to discovery routing table.
|
## Add `Node` to discovery routing table.
|
||||||
|
@ -392,7 +399,7 @@ proc sendWhoareyou(d: Protocol, toId: NodeId, a: Address,
|
||||||
|
|
||||||
let data = encodeWhoareyouPacket(d.rng[], d.codec, toId, a, requestNonce,
|
let data = encodeWhoareyouPacket(d.rng[], d.codec, toId, a, requestNonce,
|
||||||
recordSeq, pubkey)
|
recordSeq, pubkey)
|
||||||
sleepAsync(handshakeTimeout).addCallback() do(data: pointer):
|
sleepAsync(d.handshakeTimeout).addCallback() do(data: pointer):
|
||||||
# TODO: should we still provide cancellation in case handshake completes
|
# TODO: should we still provide cancellation in case handshake completes
|
||||||
# correctly?
|
# correctly?
|
||||||
d.codec.handshakes.del(key)
|
d.codec.handshakes.del(key)
|
||||||
|
@ -487,7 +494,7 @@ proc registerRequest(d: Protocol, n: Node, message: seq[byte],
|
||||||
nonce: AESGCMNonce) =
|
nonce: AESGCMNonce) =
|
||||||
let request = PendingRequest(node: n, message: message)
|
let request = PendingRequest(node: n, message: message)
|
||||||
if not d.pendingRequests.hasKeyOrPut(nonce, request):
|
if not d.pendingRequests.hasKeyOrPut(nonce, request):
|
||||||
sleepAsync(responseTimeout).addCallback() do(data: pointer):
|
sleepAsync(d.responseTimeout).addCallback() do(data: pointer):
|
||||||
d.pendingRequests.del(nonce)
|
d.pendingRequests.del(nonce)
|
||||||
|
|
||||||
proc waitMessage(d: Protocol, fromNode: Node, reqId: RequestId):
|
proc waitMessage(d: Protocol, fromNode: Node, reqId: RequestId):
|
||||||
|
@ -495,7 +502,7 @@ proc waitMessage(d: Protocol, fromNode: Node, reqId: RequestId):
|
||||||
result = newFuture[Option[Message]]("waitMessage")
|
result = newFuture[Option[Message]]("waitMessage")
|
||||||
let res = result
|
let res = result
|
||||||
let key = (fromNode.id, reqId)
|
let key = (fromNode.id, reqId)
|
||||||
sleepAsync(responseTimeout).addCallback() do(data: pointer):
|
sleepAsync(d.responseTimeout).addCallback() do(data: pointer):
|
||||||
d.awaitedMessages.del(key)
|
d.awaitedMessages.del(key)
|
||||||
if not res.finished:
|
if not res.finished:
|
||||||
res.complete(none(Message))
|
res.complete(none(Message))
|
||||||
|
@ -914,13 +921,32 @@ func init*(
|
||||||
T: type DiscoveryConfig,
|
T: type DiscoveryConfig,
|
||||||
tableIpLimit: uint,
|
tableIpLimit: uint,
|
||||||
bucketIpLimit: uint,
|
bucketIpLimit: uint,
|
||||||
bitsPerHop: int): T =
|
bitsPerHop: int,
|
||||||
|
handshakeTimeout: Duration,
|
||||||
|
responseTimeout: Duration
|
||||||
|
): T =
|
||||||
|
|
||||||
DiscoveryConfig(
|
DiscoveryConfig(
|
||||||
tableIpLimits: TableIpLimits(
|
tableIpLimits: TableIpLimits(
|
||||||
tableIpLimit: tableIpLimit,
|
tableIpLimit: tableIpLimit,
|
||||||
bucketIpLimit: bucketIpLimit),
|
bucketIpLimit: bucketIpLimit),
|
||||||
bitsPerHop: bitsPerHop
|
bitsPerHop: bitsPerHop,
|
||||||
|
handshakeTimeout: handshakeTimeout,
|
||||||
|
responseTimeout: responseTimeout
|
||||||
|
)
|
||||||
|
|
||||||
|
func init*(
|
||||||
|
T: type DiscoveryConfig,
|
||||||
|
tableIpLimit: uint,
|
||||||
|
bucketIpLimit: uint,
|
||||||
|
bitsPerHop: int): T =
|
||||||
|
|
||||||
|
DiscoveryConfig.init(
|
||||||
|
tableIpLimit,
|
||||||
|
bucketIpLimit,
|
||||||
|
bitsPerHop,
|
||||||
|
defaultHandshakeTimeout,
|
||||||
|
defaultResponseTimeout
|
||||||
)
|
)
|
||||||
|
|
||||||
proc newProtocol*(
|
proc newProtocol*(
|
||||||
|
@ -984,6 +1010,8 @@ proc newProtocol*(
|
||||||
enrAutoUpdate: enrAutoUpdate,
|
enrAutoUpdate: enrAutoUpdate,
|
||||||
routingTable: RoutingTable.init(
|
routingTable: RoutingTable.init(
|
||||||
node, config.bitsPerHop, config.tableIpLimits, rng),
|
node, config.bitsPerHop, config.tableIpLimits, rng),
|
||||||
|
handshakeTimeout: config.handshakeTimeout,
|
||||||
|
responseTimeout: config.responseTimeout,
|
||||||
rng: rng)
|
rng: rng)
|
||||||
|
|
||||||
template listeningAddress*(p: Protocol): Address =
|
template listeningAddress*(p: Protocol): Address =
|
||||||
|
|
|
@ -614,7 +614,7 @@ suite "Discovery v5 Tests":
|
||||||
check receiveNode.codec.handshakes.len == 5
|
check receiveNode.codec.handshakes.len == 5
|
||||||
# TODO: Could get rid of the sleep by storing the timeout future of the
|
# TODO: Could get rid of the sleep by storing the timeout future of the
|
||||||
# handshake
|
# handshake
|
||||||
await sleepAsync(handshakeTimeout)
|
await sleepAsync(defaultHandshakeTimeout)
|
||||||
# Checking handshake cleanup
|
# Checking handshake cleanup
|
||||||
check receiveNode.codec.handshakes.len == 0
|
check receiveNode.codec.handshakes.len == 0
|
||||||
|
|
||||||
|
@ -644,7 +644,7 @@ suite "Discovery v5 Tests":
|
||||||
check receiveNode.codec.handshakes.len == 5
|
check receiveNode.codec.handshakes.len == 5
|
||||||
# TODO: Could get rid of the sleep by storing the timeout future of the
|
# TODO: Could get rid of the sleep by storing the timeout future of the
|
||||||
# handshake
|
# handshake
|
||||||
await sleepAsync(handshakeTimeout)
|
await sleepAsync(defaultHandshakeTimeout)
|
||||||
# Checking handshake cleanup
|
# Checking handshake cleanup
|
||||||
check receiveNode.codec.handshakes.len == 0
|
check receiveNode.codec.handshakes.len == 0
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue