2018-10-15 14:36:43 +00:00
|
|
|
import
|
|
|
|
sets, options, random, hashes,
|
|
|
|
asyncdispatch2, chronicles, eth_common/eth_types,
|
|
|
|
private/types, rlpx, peer_pool, rlpx_protocols/eth_protocol,
|
|
|
|
../eth_p2p.nim
|
|
|
|
|
|
|
|
const
|
|
|
|
minPeersToStartSync* = 2 # Wait for consensus of at least this
|
|
|
|
# number of peers before syncing
|
|
|
|
|
2018-07-23 21:39:41 +00:00
|
|
|
type
|
|
|
|
SyncStatus* = enum
|
|
|
|
syncSuccess
|
|
|
|
syncNotEnoughPeers
|
|
|
|
syncTimeOut
|
|
|
|
|
|
|
|
WantedBlocksState = enum
|
|
|
|
Initial,
|
|
|
|
Requested,
|
2018-10-02 11:40:08 +00:00
|
|
|
Received,
|
|
|
|
Persisted
|
2018-07-23 21:39:41 +00:00
|
|
|
|
|
|
|
WantedBlocks = object
|
2018-08-03 11:27:37 +00:00
|
|
|
startIndex: BlockNumber
|
|
|
|
numBlocks: uint
|
2018-07-23 21:39:41 +00:00
|
|
|
state: WantedBlocksState
|
2018-08-29 08:44:17 +00:00
|
|
|
headers: seq[BlockHeader]
|
|
|
|
bodies: seq[BlockBody]
|
2018-07-23 21:39:41 +00:00
|
|
|
|
|
|
|
SyncContext = ref object
|
|
|
|
workQueue: seq[WantedBlocks]
|
2018-08-03 11:27:37 +00:00
|
|
|
endBlockNumber: BlockNumber
|
|
|
|
finalizedBlock: BlockNumber # Block which was downloaded and verified
|
2018-08-29 08:44:17 +00:00
|
|
|
chain: AbstractChainDB
|
2018-10-01 11:33:59 +00:00
|
|
|
peerPool: PeerPool
|
|
|
|
trustedPeers: HashSet[Peer]
|
2018-10-02 11:40:08 +00:00
|
|
|
hasOutOfOrderBlocks: bool
|
2018-08-03 11:27:37 +00:00
|
|
|
|
2018-10-15 14:36:43 +00:00
|
|
|
proc hash*(p: Peer): Hash {.inline.} = hash(cast[pointer](p))
|
|
|
|
|
2018-08-03 11:27:37 +00:00
|
|
|
proc endIndex(b: WantedBlocks): BlockNumber =
|
|
|
|
result = b.startIndex
|
2018-09-06 16:53:45 +00:00
|
|
|
result += (b.numBlocks - 1).u256
|
2018-08-03 11:27:37 +00:00
|
|
|
|
|
|
|
proc availableWorkItem(ctx: SyncContext): int =
|
|
|
|
var maxPendingBlock = ctx.finalizedBlock
|
2018-10-02 11:40:08 +00:00
|
|
|
echo "queue len: ", ctx.workQueue.len
|
2018-08-03 11:27:37 +00:00
|
|
|
result = -1
|
|
|
|
for i in 0 .. ctx.workQueue.high:
|
|
|
|
case ctx.workQueue[i].state
|
|
|
|
of Initial:
|
|
|
|
return i
|
2018-10-02 11:40:08 +00:00
|
|
|
of Persisted:
|
2018-08-03 11:27:37 +00:00
|
|
|
result = i
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
|
|
|
|
let eb = ctx.workQueue[i].endIndex
|
|
|
|
if eb > maxPendingBlock: maxPendingBlock = eb
|
2018-07-23 21:39:41 +00:00
|
|
|
|
2018-08-03 11:27:37 +00:00
|
|
|
let nextRequestedBlock = maxPendingBlock + 1
|
|
|
|
if nextRequestedBlock >= ctx.endBlockNumber:
|
|
|
|
return -1
|
|
|
|
|
|
|
|
if result == -1:
|
|
|
|
result = ctx.workQueue.len
|
|
|
|
ctx.workQueue.setLen(result + 1)
|
|
|
|
|
|
|
|
var numBlocks = (ctx.endBlockNumber - nextRequestedBlock).toInt
|
|
|
|
if numBlocks > maxHeadersFetch:
|
|
|
|
numBlocks = maxHeadersFetch
|
|
|
|
ctx.workQueue[result] = WantedBlocks(startIndex: nextRequestedBlock, numBlocks: numBlocks.uint, state: Initial)
|
2018-07-23 21:39:41 +00:00
|
|
|
|
2018-10-02 11:40:08 +00:00
|
|
|
proc persistWorkItem(ctx: SyncContext, wi: var WantedBlocks) =
|
|
|
|
ctx.chain.persistBlocks(wi.headers, wi.bodies)
|
|
|
|
wi.headers.setLen(0)
|
|
|
|
wi.bodies.setLen(0)
|
|
|
|
ctx.finalizedBlock = wi.endIndex
|
|
|
|
wi.state = Persisted
|
|
|
|
|
|
|
|
proc persistPendingWorkItems(ctx: SyncContext) =
|
|
|
|
var nextStartIndex = ctx.finalizedBlock + 1
|
|
|
|
var keepRunning = true
|
|
|
|
var hasOutOfOrderBlocks = false
|
|
|
|
debug "Looking for out of order blocks"
|
|
|
|
while keepRunning:
|
|
|
|
keepRunning = false
|
|
|
|
hasOutOfOrderBlocks = false
|
|
|
|
for i in 0 ..< ctx.workQueue.len:
|
|
|
|
let start = ctx.workQueue[i].startIndex
|
|
|
|
if ctx.workQueue[i].state == Received:
|
|
|
|
if start == nextStartIndex:
|
|
|
|
debug "Persisting pending work item", start
|
|
|
|
ctx.persistWorkItem(ctx.workQueue[i])
|
|
|
|
nextStartIndex = ctx.finalizedBlock + 1
|
|
|
|
keepRunning = true
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
hasOutOfOrderBlocks = true
|
|
|
|
|
|
|
|
ctx.hasOutOfOrderBlocks = hasOutOfOrderBlocks
|
|
|
|
|
2018-07-23 21:39:41 +00:00
|
|
|
proc returnWorkItem(ctx: SyncContext, workItem: int) =
|
2018-08-29 08:44:17 +00:00
|
|
|
let wi = addr ctx.workQueue[workItem]
|
|
|
|
let askedBlocks = wi.numBlocks.int
|
|
|
|
let receivedBlocks = wi.headers.len
|
2018-10-02 11:40:08 +00:00
|
|
|
let start = wi.startIndex
|
2018-08-29 08:44:17 +00:00
|
|
|
|
|
|
|
if askedBlocks == receivedBlocks:
|
2018-10-02 11:40:08 +00:00
|
|
|
debug "Work item complete", start,
|
2018-08-29 08:44:17 +00:00
|
|
|
askedBlocks,
|
|
|
|
receivedBlocks
|
|
|
|
else:
|
2018-10-02 11:40:08 +00:00
|
|
|
warn "Work item complete", start,
|
2018-08-29 08:44:17 +00:00
|
|
|
askedBlocks,
|
|
|
|
receivedBlocks
|
|
|
|
|
2018-10-02 11:40:08 +00:00
|
|
|
if wi.startIndex != ctx.finalizedBlock + 1:
|
|
|
|
info "Blocks out of order", start, final = ctx.finalizedBlock
|
|
|
|
ctx.hasOutOfOrderBlocks = true
|
|
|
|
else:
|
|
|
|
info "Persisting blocks", start
|
|
|
|
ctx.persistWorkItem(wi[])
|
|
|
|
if ctx.hasOutOfOrderBlocks:
|
|
|
|
ctx.persistPendingWorkItems()
|
2018-08-29 08:44:17 +00:00
|
|
|
|
2018-10-01 11:33:59 +00:00
|
|
|
proc newSyncContext(chain: AbstractChainDB, peerPool: PeerPool): SyncContext =
|
2018-08-03 11:27:37 +00:00
|
|
|
new result
|
2018-08-29 08:44:17 +00:00
|
|
|
result.chain = chain
|
2018-10-01 11:33:59 +00:00
|
|
|
result.peerPool = peerPool
|
|
|
|
result.trustedPeers = initSet[Peer]()
|
|
|
|
result.finalizedBlock = chain.getBestBlockHeader().blockNumber
|
2018-07-23 21:39:41 +00:00
|
|
|
|
|
|
|
proc handleLostPeer(ctx: SyncContext) =
|
|
|
|
# TODO: ask the PeerPool for new connections and then call
|
2018-08-03 11:27:37 +00:00
|
|
|
# `obtainBlocksFromPeer`
|
2018-07-23 21:39:41 +00:00
|
|
|
discard
|
|
|
|
|
2018-10-01 11:33:59 +00:00
|
|
|
proc getBestBlockNumber(p: Peer): Future[BlockNumber] {.async.} =
|
|
|
|
let request = BlocksRequest(
|
|
|
|
startBlock: HashOrNum(isHash: true,
|
|
|
|
hash: p.state(eth).bestBlockHash),
|
|
|
|
maxResults: 1,
|
|
|
|
skip: 0,
|
|
|
|
reverse: true)
|
2018-07-23 21:39:41 +00:00
|
|
|
|
2018-10-01 11:33:59 +00:00
|
|
|
let latestBlock = await p.getBlockHeaders(request)
|
|
|
|
|
|
|
|
if latestBlock.isSome and latestBlock.get.headers.len > 0:
|
|
|
|
result = latestBlock.get.headers[0].blockNumber
|
|
|
|
|
|
|
|
proc obtainBlocksFromPeer(syncCtx: SyncContext, peer: Peer) {.async.} =
|
|
|
|
# Update our best block number
|
|
|
|
let bestBlockNumber = await peer.getBestBlockNumber()
|
|
|
|
if bestBlockNumber > syncCtx.endBlockNumber:
|
|
|
|
info "New sync end block number", number = bestBlockNumber
|
|
|
|
syncCtx.endBlockNumber = bestBlockNumber
|
2018-07-23 21:39:41 +00:00
|
|
|
|
2018-08-03 11:27:37 +00:00
|
|
|
while (let workItemIdx = syncCtx.availableWorkItem(); workItemIdx != -1):
|
2018-07-23 21:39:41 +00:00
|
|
|
template workItem: auto = syncCtx.workQueue[workItemIdx]
|
|
|
|
workItem.state = Requested
|
2018-10-01 11:33:59 +00:00
|
|
|
debug "Requesting block headers", start = workItem.startIndex, count = workItem.numBlocks, peer
|
2018-07-23 21:39:41 +00:00
|
|
|
let request = BlocksRequest(
|
|
|
|
startBlock: HashOrNum(isHash: false,
|
2018-08-03 11:27:37 +00:00
|
|
|
number: workItem.startIndex),
|
|
|
|
maxResults: workItem.numBlocks,
|
2018-07-23 21:39:41 +00:00
|
|
|
skip: 0,
|
|
|
|
reverse: false)
|
|
|
|
|
2018-09-06 16:53:45 +00:00
|
|
|
var dataReceived = false
|
2018-07-23 21:39:41 +00:00
|
|
|
try:
|
|
|
|
let results = await peer.getBlockHeaders(request)
|
|
|
|
if results.isSome:
|
2018-08-29 08:44:17 +00:00
|
|
|
shallowCopy(workItem.headers, results.get.headers)
|
|
|
|
|
|
|
|
var bodies = newSeq[BlockBody]()
|
|
|
|
var hashes = newSeq[KeccakHash]()
|
|
|
|
for i in workItem.headers:
|
|
|
|
hashes.add(blockHash(i))
|
|
|
|
if hashes.len == maxBodiesFetch:
|
|
|
|
let b = await peer.getBlockBodies(hashes)
|
|
|
|
hashes.setLen(0)
|
|
|
|
bodies.add(b.get.blocks)
|
|
|
|
|
|
|
|
if hashes.len != 0:
|
|
|
|
let b = await peer.getBlockBodies(hashes)
|
|
|
|
bodies.add(b.get.blocks)
|
|
|
|
|
2018-10-02 11:40:08 +00:00
|
|
|
if bodies.len == workItem.headers.len:
|
|
|
|
shallowCopy(workItem.bodies, bodies)
|
|
|
|
dataReceived = true
|
|
|
|
else:
|
|
|
|
warn "Bodies len != headers.len", bodies = bodies.len, headers = workItem.headers.len
|
2018-07-23 21:39:41 +00:00
|
|
|
except:
|
|
|
|
# the success case uses `continue`, so we can just fall back to the
|
|
|
|
# failure path below. If we signal time-outs with exceptions such
|
|
|
|
# failures will be easier to handle.
|
|
|
|
discard
|
|
|
|
|
2018-09-06 16:53:45 +00:00
|
|
|
if dataReceived:
|
2018-10-02 11:40:08 +00:00
|
|
|
workItem.state = Received
|
2018-09-06 16:53:45 +00:00
|
|
|
syncCtx.returnWorkItem workItemIdx
|
|
|
|
else:
|
2018-10-02 11:40:08 +00:00
|
|
|
workItem.state = Initial
|
2018-09-06 16:53:45 +00:00
|
|
|
try:
|
|
|
|
await peer.disconnect(SubprotocolReason)
|
|
|
|
except:
|
|
|
|
discard
|
|
|
|
syncCtx.handleLostPeer()
|
|
|
|
break
|
2018-07-23 21:39:41 +00:00
|
|
|
|
2018-10-02 11:40:08 +00:00
|
|
|
debug "Fininshed otaining blocks", peer
|
2018-08-03 11:27:37 +00:00
|
|
|
|
2018-10-01 11:33:59 +00:00
|
|
|
proc peersAgreeOnChain(a, b: Peer): Future[bool] {.async.} =
|
|
|
|
# Returns true if one of the peers acknowledges existense of the best block
|
|
|
|
# of another peer.
|
|
|
|
var
|
|
|
|
a = a
|
|
|
|
b = b
|
|
|
|
|
|
|
|
if a.state(eth).bestDifficulty < b.state(eth).bestDifficulty:
|
|
|
|
swap(a, b)
|
|
|
|
|
|
|
|
let request = BlocksRequest(
|
|
|
|
startBlock: HashOrNum(isHash: true,
|
|
|
|
hash: b.state(eth).bestBlockHash),
|
|
|
|
maxResults: 1,
|
|
|
|
skip: 0,
|
|
|
|
reverse: true)
|
|
|
|
|
|
|
|
let latestBlock = await a.getBlockHeaders(request)
|
|
|
|
result = latestBlock.isSome and latestBlock.get.headers.len > 0
|
|
|
|
|
|
|
|
proc randomTrustedPeer(ctx: SyncContext): Peer =
|
|
|
|
var k = rand(ctx.trustedPeers.len - 1)
|
|
|
|
var i = 0
|
|
|
|
for p in ctx.trustedPeers:
|
|
|
|
result = p
|
|
|
|
if i == k: return
|
|
|
|
inc i
|
|
|
|
|
|
|
|
proc startSyncWithPeer(ctx: SyncContext, peer: Peer) {.async.} =
|
2018-10-15 14:36:43 +00:00
|
|
|
debug "start sync ", peer, trustedPeers = ctx.trustedPeers.len
|
2018-10-01 11:33:59 +00:00
|
|
|
if ctx.trustedPeers.len >= minPeersToStartSync:
|
|
|
|
# We have enough trusted peers. Validate new peer against trusted
|
|
|
|
if await peersAgreeOnChain(peer, ctx.randomTrustedPeer()):
|
|
|
|
ctx.trustedPeers.incl(peer)
|
|
|
|
asyncCheck ctx.obtainBlocksFromPeer(peer)
|
|
|
|
elif ctx.trustedPeers.len == 0:
|
|
|
|
# Assume the peer is trusted, but don't start sync until we reevaluate
|
|
|
|
# it with more peers
|
|
|
|
debug "Assume trusted peer", peer
|
|
|
|
ctx.trustedPeers.incl(peer)
|
|
|
|
else:
|
|
|
|
# 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
|
|
|
|
disagreedPeer: Peer
|
|
|
|
|
|
|
|
for tp in ctx.trustedPeers:
|
|
|
|
if await peersAgreeOnChain(peer, tp):
|
|
|
|
inc agreeScore
|
|
|
|
else:
|
|
|
|
disagreedPeer = tp
|
|
|
|
|
|
|
|
let disagreeScore = ctx.trustedPeers.len - agreeScore
|
|
|
|
|
|
|
|
if agreeScore == ctx.trustedPeers.len:
|
|
|
|
ctx.trustedPeers.incl(peer) # The best possible outsome
|
|
|
|
elif disagreeScore == 1:
|
|
|
|
info "Peer is no more trusted for sync", peer
|
|
|
|
ctx.trustedPeers.excl(disagreedPeer)
|
|
|
|
ctx.trustedPeers.incl(peer)
|
|
|
|
else:
|
|
|
|
info "Peer not trusted for sync", peer
|
|
|
|
|
|
|
|
if ctx.trustedPeers.len == minPeersToStartSync:
|
|
|
|
for p in ctx.trustedPeers:
|
|
|
|
asyncCheck ctx.obtainBlocksFromPeer(p)
|
|
|
|
|
|
|
|
|
|
|
|
proc onPeerConnected(ctx: SyncContext, peer: Peer) =
|
|
|
|
debug "New candidate for sync", peer
|
|
|
|
discard
|
|
|
|
let f = ctx.startSyncWithPeer(peer)
|
2018-10-21 18:00:15 +00:00
|
|
|
f.callback = proc(data: pointer) {.gcsafe.} =
|
2018-10-01 11:33:59 +00:00
|
|
|
if f.failed:
|
|
|
|
error "startSyncWithPeer failed", msg = f.readError.msg, peer
|
|
|
|
|
|
|
|
proc onPeerDisconnected(ctx: SyncContext, p: Peer) =
|
2018-10-15 14:36:43 +00:00
|
|
|
debug "peer disconnected ", peer = p
|
2018-10-01 11:33:59 +00:00
|
|
|
ctx.trustedPeers.excl(p)
|
|
|
|
|
|
|
|
proc startSync(ctx: SyncContext) =
|
|
|
|
var po: PeerObserver
|
|
|
|
po.onPeerConnected = proc(p: Peer) =
|
|
|
|
ctx.onPeerConnected(p)
|
|
|
|
|
|
|
|
po.onPeerDisconnected = proc(p: Peer) =
|
|
|
|
ctx.onPeerDisconnected(p)
|
|
|
|
|
|
|
|
ctx.peerPool.addObserver(ctx, po)
|
|
|
|
|
2018-08-03 11:27:37 +00:00
|
|
|
proc findBestPeer(node: EthereumNode): (Peer, DifficultyInt) =
|
2018-07-23 21:39:41 +00:00
|
|
|
var
|
|
|
|
bestBlockDifficulty: DifficultyInt = 0.stuint(256)
|
|
|
|
bestPeer: Peer = nil
|
|
|
|
|
|
|
|
for peer in node.peers(eth):
|
|
|
|
let peerEthState = peer.state(eth)
|
|
|
|
if peerEthState.initialized:
|
|
|
|
if peerEthState.bestDifficulty > bestBlockDifficulty:
|
|
|
|
bestBlockDifficulty = peerEthState.bestDifficulty
|
|
|
|
bestPeer = peer
|
|
|
|
|
2018-08-03 11:27:37 +00:00
|
|
|
result = (bestPeer, bestBlockDifficulty)
|
|
|
|
|
|
|
|
proc fastBlockchainSync*(node: EthereumNode): Future[SyncStatus] {.async.} =
|
|
|
|
## Code for the fast blockchain sync procedure:
|
|
|
|
## https://github.com/ethereum/wiki/wiki/Parallel-Block-Downloads
|
|
|
|
## https://github.com/ethereum/go-ethereum/pull/1889
|
2018-10-01 11:33:59 +00:00
|
|
|
# TODO: This needs a better interface. Consider removing this function and
|
|
|
|
# exposing SyncCtx
|
|
|
|
var syncCtx = newSyncContext(node.chain, node.peerPool)
|
|
|
|
syncCtx.startSync()
|
2018-07-08 22:26:14 +00:00
|
|
|
|