2022-09-16 07:24:12 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at
|
|
|
|
# https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at
|
|
|
|
# https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed
|
|
|
|
# except according to those terms.
|
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
## Negotiate a pivot header base on the *best header* values. Buddies that
|
|
|
|
## cannot provide a minimal block number will be disconnected.
|
|
|
|
##
|
|
|
|
## Ackn: nim-eth/eth/p2p/blockchain_sync.nim: `startSyncWithPeer()`
|
2022-09-16 07:24:12 +00:00
|
|
|
|
|
|
|
import
|
|
|
|
std/[hashes, options, sets],
|
|
|
|
chronicles,
|
|
|
|
chronos,
|
2022-10-20 16:59:54 +00:00
|
|
|
eth/[common, p2p],
|
2022-09-16 07:24:12 +00:00
|
|
|
stew/byteutils,
|
2022-10-10 02:31:28 +00:00
|
|
|
".."/[protocol, sync_desc, types]
|
2022-09-16 07:24:12 +00:00
|
|
|
|
2023-01-30 22:10:23 +00:00
|
|
|
{.push raises:[].}
|
2022-09-16 07:24:12 +00:00
|
|
|
|
|
|
|
logScope:
|
2022-09-30 08:22:14 +00:00
|
|
|
topics = "best-pivot"
|
2022-09-16 07:24:12 +00:00
|
|
|
|
|
|
|
const
|
2022-09-30 08:22:14 +00:00
|
|
|
extraTraceMessages = false or true
|
2022-09-16 07:24:12 +00:00
|
|
|
## Additional trace commands
|
|
|
|
|
2023-01-16 14:51:32 +00:00
|
|
|
minPeersToStartSync = 2
|
2022-09-16 07:24:12 +00:00
|
|
|
## Wait for consensus of at least this number of peers before syncing.
|
|
|
|
|
2023-01-16 14:51:32 +00:00
|
|
|
failCountMax = 3
|
2022-11-25 14:56:42 +00:00
|
|
|
## Stop after a peer fails too often while negotiating. This happens if
|
|
|
|
## a peer responses repeatedly with useless data.
|
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
type
|
|
|
|
BestPivotCtxRef* = ref object of RootRef
|
|
|
|
## Data shared by all peers.
|
|
|
|
rng: ref HmacDrbgContext ## Random generator
|
2023-01-16 14:51:32 +00:00
|
|
|
untrusted: HashSet[Peer] ## Clean up list
|
2022-09-30 08:22:14 +00:00
|
|
|
trusted: HashSet[Peer] ## Peers ready for delivery
|
2023-01-16 14:51:32 +00:00
|
|
|
relaxed: HashSet[Peer] ## Peers accepted in relaxed mode
|
2022-11-01 15:07:44 +00:00
|
|
|
relaxedMode: bool ## Not using strictly `trusted` set
|
2023-01-16 14:51:32 +00:00
|
|
|
minPeers: int ## Minimum peers needed in non-relaxed mode
|
|
|
|
comFailMax: int ## Stop peer after too many communication errors
|
2022-09-30 08:22:14 +00:00
|
|
|
|
|
|
|
BestPivotWorkerRef* = ref object of RootRef
|
|
|
|
## Data for this peer only
|
|
|
|
global: BestPivotCtxRef ## Common data
|
|
|
|
header: Option[BlockHeader] ## Pivot header (if any)
|
|
|
|
ctrl: BuddyCtrlRef ## Control and state settings
|
|
|
|
peer: Peer ## network peer
|
2022-11-25 14:56:42 +00:00
|
|
|
comFailCount: int ## Beware of repeated network errors
|
2022-09-30 08:22:14 +00:00
|
|
|
|
2022-09-16 07:24:12 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-01-30 22:10:23 +00:00
|
|
|
#proc hash(peer: Peer): Hash =
|
|
|
|
# ## Mixin `HashSet[Peer]` handler
|
|
|
|
# hash(cast[pointer](peer))
|
2022-09-16 07:24:12 +00:00
|
|
|
|
|
|
|
template safeTransport(
|
2022-09-30 08:22:14 +00:00
|
|
|
bp: BestPivotWorkerRef;
|
2022-09-16 07:24:12 +00:00
|
|
|
info: static[string];
|
|
|
|
code: untyped) =
|
|
|
|
try:
|
|
|
|
code
|
|
|
|
except TransportError as e:
|
2022-09-30 08:22:14 +00:00
|
|
|
error info & ", stop", peer=bp.peer, error=($e.name), msg=e.msg
|
|
|
|
bp.ctrl.stopped = true
|
2022-09-16 07:24:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
proc rand(r: ref HmacDrbgContext; maxVal: uint64): uint64 =
|
|
|
|
# github.com/nim-lang/Nim/tree/version-1-6/lib/pure/random.nim#L216
|
|
|
|
const
|
|
|
|
randMax = high(uint64)
|
|
|
|
if 0 < maxVal:
|
|
|
|
if maxVal == randMax:
|
|
|
|
var x: uint64
|
|
|
|
r[].generate(x)
|
|
|
|
return x
|
|
|
|
while true:
|
|
|
|
var x: uint64
|
|
|
|
r[].generate(x)
|
|
|
|
# avoid `mod` bias, so `x <= n*maxVal <= randMax` for some integer `n`
|
|
|
|
if x <= randMax - (randMax mod maxVal):
|
|
|
|
# uint -> int
|
|
|
|
return x mod (maxVal + 1)
|
|
|
|
|
|
|
|
proc rand(r: ref HmacDrbgContext; maxVal: int): int =
|
|
|
|
if 0 < maxVal: # somehow making sense of `maxVal = -1`
|
|
|
|
return cast[int](r.rand(maxVal.uint64))
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
proc getRandomTrustedPeer(bp: BestPivotWorkerRef): Result[Peer,void] =
|
2022-09-16 07:24:12 +00:00
|
|
|
## Return random entry from `trusted` peer different from this peer set if
|
|
|
|
## there are enough
|
|
|
|
##
|
|
|
|
## Ackn: nim-eth/eth/p2p/blockchain_sync.nim: `randomTrustedPeer()`
|
|
|
|
let
|
2022-09-30 08:22:14 +00:00
|
|
|
nPeers = bp.global.trusted.len
|
|
|
|
offInx = if bp.peer in bp.global.trusted: 2 else: 1
|
2022-09-16 07:24:12 +00:00
|
|
|
if 0 < nPeers:
|
2022-09-30 08:22:14 +00:00
|
|
|
var (walkInx, stopInx) = (0, bp.global.rng.rand(nPeers - offInx))
|
|
|
|
for p in bp.global.trusted:
|
|
|
|
if p == bp.peer:
|
2022-09-16 07:24:12 +00:00
|
|
|
continue
|
|
|
|
if walkInx == stopInx:
|
|
|
|
return ok(p)
|
|
|
|
walkInx.inc
|
|
|
|
err()
|
|
|
|
|
|
|
|
proc getBestHeader(
|
2022-09-30 08:22:14 +00:00
|
|
|
bp: BestPivotWorkerRef;
|
|
|
|
): Future[Result[BlockHeader,void]]
|
|
|
|
{.async.} =
|
2022-09-16 07:24:12 +00:00
|
|
|
## Get best block number from best block hash.
|
|
|
|
##
|
|
|
|
## Ackn: nim-eth/eth/p2p/blockchain_sync.nim: `getBestBlockNumber()`
|
|
|
|
let
|
2022-09-30 08:22:14 +00:00
|
|
|
peer = bp.peer
|
2022-09-16 07:24:12 +00:00
|
|
|
startHash = peer.state(eth).bestBlockHash
|
|
|
|
reqLen = 1u
|
|
|
|
hdrReq = BlocksRequest(
|
|
|
|
startBlock: HashOrNum(
|
|
|
|
isHash: true,
|
|
|
|
hash: startHash),
|
|
|
|
maxResults: reqLen,
|
|
|
|
skip: 0,
|
|
|
|
reverse: true)
|
|
|
|
|
|
|
|
trace trEthSendSendingGetBlockHeaders, peer,
|
|
|
|
startBlock=startHash.data.toHex, reqLen
|
|
|
|
|
|
|
|
var hdrResp: Option[blockHeadersObj]
|
2022-09-30 08:22:14 +00:00
|
|
|
bp.safeTransport("Error fetching block header"):
|
2022-09-16 07:24:12 +00:00
|
|
|
hdrResp = await peer.getBlockHeaders(hdrReq)
|
2022-09-30 08:22:14 +00:00
|
|
|
if bp.ctrl.stopped:
|
2022-09-16 07:24:12 +00:00
|
|
|
return err()
|
|
|
|
|
|
|
|
if hdrResp.isNone:
|
2022-11-25 14:56:42 +00:00
|
|
|
bp.comFailCount.inc
|
|
|
|
trace trEthRecvReceivedBlockHeaders, peer, reqLen,
|
2023-01-16 14:51:32 +00:00
|
|
|
hdrRespLen="n/a", comFailCount=bp.comFailCount
|
|
|
|
if bp.global.comFailMax < bp.comFailCount:
|
2022-11-25 14:56:42 +00:00
|
|
|
bp.ctrl.zombie = true
|
2022-09-16 07:24:12 +00:00
|
|
|
return err()
|
|
|
|
|
|
|
|
let hdrRespLen = hdrResp.get.headers.len
|
|
|
|
if hdrRespLen == 1:
|
|
|
|
let
|
|
|
|
header = hdrResp.get.headers[0]
|
2023-01-30 22:10:23 +00:00
|
|
|
blockNumber {.used.} = header.blockNumber
|
2022-09-16 07:24:12 +00:00
|
|
|
trace trEthRecvReceivedBlockHeaders, peer, hdrRespLen, blockNumber
|
2022-11-25 14:56:42 +00:00
|
|
|
bp.comFailCount = 0 # reset fail count
|
2022-09-16 07:24:12 +00:00
|
|
|
return ok(header)
|
|
|
|
|
2023-01-16 14:51:32 +00:00
|
|
|
bp.comFailCount.inc
|
|
|
|
trace trEthRecvReceivedBlockHeaders, peer, reqLen,
|
|
|
|
hdrRespLen, comFailCount=bp.comFailCount
|
|
|
|
if bp.global.comFailMax < bp.comFailCount:
|
|
|
|
bp.ctrl.zombie = true
|
2022-09-16 07:24:12 +00:00
|
|
|
return err()
|
|
|
|
|
|
|
|
proc agreesOnChain(
|
2022-09-30 08:22:14 +00:00
|
|
|
bp: BestPivotWorkerRef;
|
2022-09-16 07:24:12 +00:00
|
|
|
other: Peer
|
2022-09-30 08:22:14 +00:00
|
|
|
): Future[Result[void,bool]]
|
|
|
|
{.async.} =
|
|
|
|
## Returns `true` if one of the peers `bp.peer` or `other` acknowledges
|
2022-09-16 07:24:12 +00:00
|
|
|
## existence of the best block of the other peer. The values returned mean
|
|
|
|
## * ok() -- `peer` is trusted
|
|
|
|
## * err(true) -- `peer` is untrusted
|
|
|
|
## * err(false) -- `other` is dead
|
|
|
|
##
|
|
|
|
## Ackn: nim-eth/eth/p2p/blockchain_sync.nim: `peersAgreeOnChain()`
|
|
|
|
let
|
2022-09-30 08:22:14 +00:00
|
|
|
peer = bp.peer
|
2022-09-16 07:24:12 +00:00
|
|
|
var
|
|
|
|
start = peer
|
|
|
|
fetch = other
|
|
|
|
swapped = false
|
|
|
|
# Make sure that `fetch` has not the smaller difficulty.
|
|
|
|
if fetch.state(eth).bestDifficulty < start.state(eth).bestDifficulty:
|
|
|
|
swap(fetch, start)
|
|
|
|
swapped = true
|
|
|
|
|
|
|
|
let
|
|
|
|
startHash = start.state(eth).bestBlockHash
|
|
|
|
hdrReq = BlocksRequest(
|
|
|
|
startBlock: HashOrNum(
|
|
|
|
isHash: true,
|
|
|
|
hash: startHash),
|
|
|
|
maxResults: 1,
|
|
|
|
skip: 0,
|
|
|
|
reverse: true)
|
|
|
|
|
|
|
|
trace trEthSendSendingGetBlockHeaders, peer, start, fetch,
|
|
|
|
startBlock=startHash.data.toHex, hdrReqLen=1, swapped
|
|
|
|
|
|
|
|
var hdrResp: Option[blockHeadersObj]
|
2022-09-30 08:22:14 +00:00
|
|
|
bp.safeTransport("Error fetching block header"):
|
2022-09-16 07:24:12 +00:00
|
|
|
hdrResp = await fetch.getBlockHeaders(hdrReq)
|
2022-09-30 08:22:14 +00:00
|
|
|
if bp.ctrl.stopped:
|
2022-09-16 07:24:12 +00:00
|
|
|
if swapped:
|
|
|
|
return err(true)
|
|
|
|
# No need to terminate `peer` if it was the `other`, failing nevertheless
|
2022-09-30 08:22:14 +00:00
|
|
|
bp.ctrl.stopped = false
|
2022-09-16 07:24:12 +00:00
|
|
|
return err(false)
|
|
|
|
|
|
|
|
if hdrResp.isSome:
|
|
|
|
let hdrRespLen = hdrResp.get.headers.len
|
|
|
|
if 0 < hdrRespLen:
|
2023-01-30 22:10:23 +00:00
|
|
|
let blockNumber {.used.} = hdrResp.get.headers[0].blockNumber
|
2022-09-16 07:24:12 +00:00
|
|
|
trace trEthRecvReceivedBlockHeaders, peer, start, fetch,
|
|
|
|
hdrRespLen, blockNumber
|
|
|
|
return ok()
|
|
|
|
|
|
|
|
trace trEthRecvReceivedBlockHeaders, peer, start, fetch,
|
|
|
|
blockNumber="n/a", swapped
|
|
|
|
return err(true)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2022-09-30 08:22:14 +00:00
|
|
|
# Public functions, constructor
|
2022-09-16 07:24:12 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
proc init*(
|
|
|
|
T: type BestPivotCtxRef; ## Global data descriptor type
|
|
|
|
rng: ref HmacDrbgContext; ## Random generator
|
2023-01-16 14:51:32 +00:00
|
|
|
minPeers = minPeersToStartSync; ## Consensus of at least this #of peers
|
|
|
|
failMax = failCountMax; ## Stop peer after too many com. errors
|
2022-09-30 08:22:14 +00:00
|
|
|
): T =
|
2023-01-16 14:51:32 +00:00
|
|
|
## Global constructor, shared data. If `minPeers` is smaller that `2`,
|
|
|
|
## relaxed mode will be enabled (see also `pivotRelaxedMode()`.)
|
|
|
|
result = T(rng: rng,
|
|
|
|
minPeers: minPeers,
|
|
|
|
comFailMax: failCountMax)
|
|
|
|
if minPeers < 2:
|
|
|
|
result.minPeers = minPeersToStartSync
|
|
|
|
result.relaxedMode = true
|
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
|
|
|
|
proc init*(
|
|
|
|
T: type BestPivotWorkerRef; ## Global data descriptor type
|
|
|
|
ctx: BestPivotCtxRef; ## Global data descriptor
|
|
|
|
ctrl: BuddyCtrlRef; ## Control and state settings
|
|
|
|
peer: Peer; ## For fetching data from network
|
|
|
|
): T =
|
|
|
|
## Buddy/local constructor
|
|
|
|
T(global: ctx,
|
|
|
|
header: none(BlockHeader),
|
|
|
|
ctrl: ctrl,
|
|
|
|
peer: peer)
|
|
|
|
|
|
|
|
proc clear*(bp: BestPivotWorkerRef) =
|
|
|
|
## Reset descriptor
|
2023-01-16 14:51:32 +00:00
|
|
|
bp.global.untrusted.incl bp.peer
|
2022-09-30 08:22:14 +00:00
|
|
|
bp.header = none(BlockHeader)
|
2022-09-16 07:24:12 +00:00
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
2022-09-16 07:24:12 +00:00
|
|
|
|
2023-01-16 14:51:32 +00:00
|
|
|
proc nPivotApproved*(ctx: BestPivotCtxRef): int =
|
|
|
|
## Number of trusted or relax mode approved pivots
|
|
|
|
(ctx.trusted + ctx.relaxed - ctx.untrusted).len
|
|
|
|
|
2022-11-01 15:07:44 +00:00
|
|
|
proc pivotRelaxedMode*(ctx: BestPivotCtxRef; enable = false) =
|
|
|
|
## Controls relaxed mode. In relaxed mode, the *best header* is fetched
|
|
|
|
## from the network and used as pivot if its block number is large enough.
|
|
|
|
## Otherwise, the default is to find at least `pivotMinPeersToStartSync`
|
|
|
|
## peers (this one included) that agree on a minimum pivot.
|
|
|
|
ctx.relaxedMode = enable
|
|
|
|
|
2022-10-08 17:20:50 +00:00
|
|
|
proc pivotHeader*(bp: BestPivotWorkerRef): Result[BlockHeader,void] =
|
2022-09-30 08:22:14 +00:00
|
|
|
## Returns cached block header if available and the buddy `peer` is trusted.
|
2022-11-01 15:07:44 +00:00
|
|
|
## In relaxed mode (see `pivotRelaxedMode()`), also lesser trusted pivots
|
|
|
|
## are returned.
|
2022-09-30 08:22:14 +00:00
|
|
|
if bp.header.isSome and
|
2022-11-01 15:07:44 +00:00
|
|
|
bp.peer notin bp.global.untrusted:
|
|
|
|
|
2023-01-16 14:51:32 +00:00
|
|
|
if bp.global.minPeers <= bp.global.trusted.len and
|
2022-11-01 15:07:44 +00:00
|
|
|
bp.peer in bp.global.trusted:
|
|
|
|
return ok(bp.header.unsafeGet)
|
|
|
|
|
|
|
|
if bp.global.relaxedMode:
|
|
|
|
when extraTraceMessages:
|
|
|
|
trace "Returning not fully trusted pivot", peer=bp.peer,
|
|
|
|
trusted=bp.global.trusted.len, untrusted=bp.global.untrusted.len
|
|
|
|
return ok(bp.header.unsafeGet)
|
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
err()
|
2022-09-16 07:24:12 +00:00
|
|
|
|
2022-12-18 16:06:43 +00:00
|
|
|
proc pivotHeader*(
|
|
|
|
bp: BestPivotWorkerRef; ## Worker peer
|
|
|
|
relaxedMode: bool; ## One time relaxed mode flag
|
|
|
|
): Result[BlockHeader,void] =
|
|
|
|
## Variant of `pivotHeader()` with `relaxedMode` flag as function argument.
|
|
|
|
if bp.header.isSome and
|
|
|
|
bp.peer notin bp.global.untrusted:
|
|
|
|
|
2023-01-16 14:51:32 +00:00
|
|
|
if bp.global.minPeers <= bp.global.trusted.len and
|
2022-12-18 16:06:43 +00:00
|
|
|
bp.peer in bp.global.trusted:
|
|
|
|
return ok(bp.header.unsafeGet)
|
|
|
|
|
|
|
|
if relaxedMode:
|
|
|
|
when extraTraceMessages:
|
|
|
|
trace "Returning not fully trusted pivot", peer=bp.peer,
|
|
|
|
trusted=bp.global.trusted.len, untrusted=bp.global.untrusted.len
|
|
|
|
return ok(bp.header.unsafeGet)
|
|
|
|
|
|
|
|
err()
|
|
|
|
|
2022-10-08 17:20:50 +00:00
|
|
|
proc pivotNegotiate*(
|
|
|
|
bp: BestPivotWorkerRef; ## Worker peer
|
2022-09-30 08:22:14 +00:00
|
|
|
minBlockNumber: Option[BlockNumber]; ## Minimum block number to expect
|
2022-10-08 17:20:50 +00:00
|
|
|
): Future[bool]
|
|
|
|
{.async.} =
|
2022-09-30 08:22:14 +00:00
|
|
|
## Negotiate best header pivot. This function must be run in *single mode* at
|
|
|
|
## the beginning of a running worker peer. If the function returns `true`,
|
|
|
|
## the current `buddy` can be used for syncing and the function
|
|
|
|
## `bestPivotHeader()` will succeed returning a `BlockHeader`.
|
2022-09-16 07:24:12 +00:00
|
|
|
##
|
2022-11-01 15:07:44 +00:00
|
|
|
## In relaxed mode (see `pivotRelaxedMode()`), negotiation stopps when there
|
|
|
|
## is a *best header*. It caches the best header and returns `true` it the
|
|
|
|
## block number is large enough.
|
|
|
|
##
|
2022-09-16 07:24:12 +00:00
|
|
|
## Ackn: nim-eth/eth/p2p/blockchain_sync.nim: `startSyncWithPeer()`
|
|
|
|
##
|
2022-09-30 08:22:14 +00:00
|
|
|
let peer = bp.peer
|
2022-09-16 07:24:12 +00:00
|
|
|
|
|
|
|
# Delayed clean up batch list
|
2022-09-30 08:22:14 +00:00
|
|
|
if 0 < bp.global.untrusted.len:
|
2022-09-16 07:24:12 +00:00
|
|
|
when extraTraceMessages:
|
2022-09-30 08:22:14 +00:00
|
|
|
trace "Removing untrusted peers", peer, trusted=bp.global.trusted.len,
|
|
|
|
untrusted=bp.global.untrusted.len, runState=bp.ctrl.state
|
2023-01-16 14:51:32 +00:00
|
|
|
bp.global.trusted = bp.global.trusted - bp.global.untrusted
|
|
|
|
bp.global.relaxed = bp.global.relaxed - bp.global.untrusted
|
|
|
|
bp.global.untrusted.clear()
|
2022-09-16 07:24:12 +00:00
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
if bp.header.isNone:
|
2022-09-16 07:24:12 +00:00
|
|
|
when extraTraceMessages:
|
|
|
|
# Only log for the first time (if any)
|
|
|
|
trace "Pivot initialisation", peer,
|
2022-09-30 08:22:14 +00:00
|
|
|
trusted=bp.global.trusted.len, runState=bp.ctrl.state
|
2022-09-16 07:24:12 +00:00
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
let rc = await bp.getBestHeader()
|
2022-09-16 07:24:12 +00:00
|
|
|
# Beware of peer terminating the session right after communicating
|
2022-09-30 08:22:14 +00:00
|
|
|
if rc.isErr or bp.ctrl.stopped:
|
2022-09-16 07:24:12 +00:00
|
|
|
return false
|
|
|
|
let
|
|
|
|
bestNumber = rc.value.blockNumber
|
2022-09-30 08:22:14 +00:00
|
|
|
minNumber = minBlockNumber.get(otherwise = 0.toBlockNumber)
|
2022-09-16 07:24:12 +00:00
|
|
|
if bestNumber < minNumber:
|
2022-09-30 08:22:14 +00:00
|
|
|
bp.ctrl.zombie = true
|
2022-09-16 07:24:12 +00:00
|
|
|
trace "Useless peer, best number too low", peer,
|
2022-09-30 08:22:14 +00:00
|
|
|
trusted=bp.global.trusted.len, runState=bp.ctrl.state,
|
2022-09-16 07:24:12 +00:00
|
|
|
minNumber, bestNumber
|
2022-11-01 15:07:44 +00:00
|
|
|
return false
|
2022-09-30 08:22:14 +00:00
|
|
|
bp.header = some(rc.value)
|
2022-09-16 07:24:12 +00:00
|
|
|
|
2022-11-01 15:07:44 +00:00
|
|
|
# No further negotiation if in relaxed mode
|
|
|
|
if bp.global.relaxedMode:
|
2023-01-16 14:51:32 +00:00
|
|
|
bp.global.relaxed.incl bp.peer
|
2022-11-01 15:07:44 +00:00
|
|
|
return true
|
|
|
|
|
2023-01-16 14:51:32 +00:00
|
|
|
if bp.global.minPeers <= bp.global.trusted.len:
|
2022-09-16 07:24:12 +00:00
|
|
|
# We have enough trusted peers. Validate new peer against trusted
|
2022-09-30 08:22:14 +00:00
|
|
|
let rc = bp.getRandomTrustedPeer()
|
2022-09-16 07:24:12 +00:00
|
|
|
if rc.isOK:
|
2022-09-30 08:22:14 +00:00
|
|
|
let rx = await bp.agreesOnChain(rc.value)
|
2022-09-16 07:24:12 +00:00
|
|
|
if rx.isOk:
|
2022-09-30 08:22:14 +00:00
|
|
|
bp.global.trusted.incl peer
|
|
|
|
when extraTraceMessages:
|
2023-01-30 22:10:23 +00:00
|
|
|
let bestHeader {.used.} =
|
2022-09-30 08:22:14 +00:00
|
|
|
if bp.header.isSome: "#" & $bp.header.get.blockNumber
|
|
|
|
else: "nil"
|
|
|
|
trace "Accepting peer", peer, trusted=bp.global.trusted.len,
|
|
|
|
untrusted=bp.global.untrusted.len, runState=bp.ctrl.state,
|
|
|
|
bestHeader
|
2022-09-16 07:24:12 +00:00
|
|
|
return true
|
|
|
|
if not rx.error:
|
|
|
|
# Other peer is dead
|
2022-09-30 08:22:14 +00:00
|
|
|
bp.global.trusted.excl rc.value
|
2022-11-01 15:07:44 +00:00
|
|
|
return false
|
2022-09-16 07:24:12 +00:00
|
|
|
|
|
|
|
# If there are no trusted peers yet, assume this very peer is trusted,
|
|
|
|
# but do not finish initialisation until there are more peers.
|
2022-11-01 15:07:44 +00:00
|
|
|
if bp.global.trusted.len == 0:
|
2022-09-30 08:22:14 +00:00
|
|
|
bp.global.trusted.incl peer
|
2022-09-16 07:24:12 +00:00
|
|
|
when extraTraceMessages:
|
2023-01-30 22:10:23 +00:00
|
|
|
let bestHeader {.used.} =
|
2022-09-30 08:22:14 +00:00
|
|
|
if bp.header.isSome: "#" & $bp.header.get.blockNumber
|
|
|
|
else: "nil"
|
2022-09-16 07:24:12 +00:00
|
|
|
trace "Assume initial trusted peer", peer,
|
2022-09-30 08:22:14 +00:00
|
|
|
trusted=bp.global.trusted.len, runState=bp.ctrl.state, bestHeader
|
2022-11-01 15:07:44 +00:00
|
|
|
return false
|
2022-09-16 07:24:12 +00:00
|
|
|
|
2022-11-01 15:07:44 +00:00
|
|
|
if bp.global.trusted.len == 1 and bp.peer in bp.global.trusted:
|
2022-09-16 07:24:12 +00:00
|
|
|
# Ignore degenerate case, note that `trusted.len < minPeersToStartSync`
|
2022-11-01 15:07:44 +00:00
|
|
|
return false
|
2022-09-16 07:24:12 +00:00
|
|
|
|
2022-11-01 15:07:44 +00:00
|
|
|
# At this point we have some "trusted" candidates, but they are not
|
|
|
|
# "trusted" enough. We evaluate `peer` against all other candidates. If
|
|
|
|
# one of the candidates disagrees, we swap it for `peer`. If all candidates
|
|
|
|
# agree, we add `peer` to trusted set. The peers in the set will become
|
|
|
|
# "fully trusted" (and sync will start) when the set is big enough
|
|
|
|
var
|
|
|
|
agreeScore = 0
|
|
|
|
otherPeer: Peer
|
|
|
|
deadPeers: HashSet[Peer]
|
|
|
|
when extraTraceMessages:
|
|
|
|
trace "Trust scoring peer", peer,
|
|
|
|
trusted=bp.global.trusted.len, runState=bp.ctrl.state
|
|
|
|
for p in bp.global.trusted:
|
|
|
|
if peer == p:
|
|
|
|
inc agreeScore
|
|
|
|
else:
|
|
|
|
let rc = await bp.agreesOnChain(p)
|
|
|
|
if rc.isOk:
|
2022-09-16 07:24:12 +00:00
|
|
|
inc agreeScore
|
2022-11-01 15:07:44 +00:00
|
|
|
elif bp.ctrl.stopped:
|
|
|
|
# Beware of terminated session
|
2022-09-16 07:24:12 +00:00
|
|
|
return false
|
2022-11-01 15:07:44 +00:00
|
|
|
elif rc.error:
|
|
|
|
otherPeer = p
|
|
|
|
else:
|
|
|
|
# `Other` peer is dead
|
|
|
|
deadPeers.incl p
|
|
|
|
|
|
|
|
# Normalise
|
|
|
|
if 0 < deadPeers.len:
|
|
|
|
bp.global.trusted = bp.global.trusted - deadPeers
|
|
|
|
if bp.global.trusted.len == 0 or
|
|
|
|
bp.global.trusted.len == 1 and bp.peer in bp.global.trusted:
|
|
|
|
return false
|
2022-09-16 07:24:12 +00:00
|
|
|
|
2022-11-01 15:07:44 +00:00
|
|
|
# Check for the number of peers that disagree
|
|
|
|
case bp.global.trusted.len - agreeScore:
|
|
|
|
of 0:
|
|
|
|
bp.global.trusted.incl peer # best possible outcome
|
|
|
|
when extraTraceMessages:
|
|
|
|
trace "Agreeable trust score for peer", peer,
|
|
|
|
trusted=bp.global.trusted.len, runState=bp.ctrl.state
|
|
|
|
of 1:
|
|
|
|
bp.global.trusted.excl otherPeer
|
|
|
|
bp.global.trusted.incl peer
|
|
|
|
when extraTraceMessages:
|
|
|
|
trace "Other peer no longer trusted", peer,
|
|
|
|
otherPeer, trusted=bp.global.trusted.len, runState=bp.ctrl.state
|
|
|
|
else:
|
|
|
|
when extraTraceMessages:
|
|
|
|
trace "Peer not trusted", peer,
|
|
|
|
trusted=bp.global.trusted.len, runState=bp.ctrl.state
|
|
|
|
discard
|
2022-09-16 07:24:12 +00:00
|
|
|
|
2022-11-01 15:07:44 +00:00
|
|
|
# Evaluate status, finally
|
2023-01-16 14:51:32 +00:00
|
|
|
if bp.global.minPeers <= bp.global.trusted.len:
|
2022-11-01 15:07:44 +00:00
|
|
|
when extraTraceMessages:
|
2023-01-30 22:10:23 +00:00
|
|
|
let bestHeader {.used.} =
|
2022-11-01 15:07:44 +00:00
|
|
|
if bp.header.isSome: "#" & $bp.header.get.blockNumber
|
|
|
|
else: "nil"
|
|
|
|
trace "Peer trusted now", peer,
|
|
|
|
trusted=bp.global.trusted.len, runState=bp.ctrl.state, bestHeader
|
|
|
|
return true
|
2022-09-16 07:24:12 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|