Sync: Add packet tracing to `blockchain_sync` network calls

Using the same packet tracing format to match `protocol_eth65`.
There aren't many calls, and this makes them clear.

Signed-off-by: Jamie Lokier <jamie@shareable.org>
This commit is contained in:
Jamie Lokier 2021-07-22 17:36:17 +01:00
parent b28396d10d
commit 57de56bab6
No known key found for this signature in database
GPG Key ID: CBC25C68435C30A2
1 changed files with 28 additions and 2 deletions

View File

@ -192,10 +192,16 @@ proc getBestBlockNumber(p: Peer): Future[BlockNumber] {.async.} =
skip: 0, skip: 0,
reverse: true) reverse: true)
tracePacket ">> Sending eth.GetBlockHeaders (0x03)", peer=p,
startBlock=request.startBlock.hash, max=request.maxResults
let latestBlock = await p.getBlockHeaders(request) let latestBlock = await p.getBlockHeaders(request)
if latestBlock.isSome and latestBlock.get.headers.len > 0: if latestBlock.isSome:
if latestBlock.get.headers.len > 0:
result = latestBlock.get.headers[0].blockNumber result = latestBlock.get.headers[0].blockNumber
tracePacket "<< Got reply eth.BlockHeaders (0x04)", peer=p,
count=latestBlock.get.headers.len,
blockNumber=(if latestBlock.get.headers.len > 0: $result else: "missing")
proc obtainBlocksFromPeer(syncCtx: SyncContext, peer: Peer) {.async.} = proc obtainBlocksFromPeer(syncCtx: SyncContext, peer: Peer) {.async.} =
# Update our best block number # Update our best block number
@ -225,8 +231,12 @@ proc obtainBlocksFromPeer(syncCtx: SyncContext, peer: Peer) {.async.} =
var dataReceived = false var dataReceived = false
try: try:
tracePacket ">> Sending eth.GetBlockHeaders (0x03)", peer,
startBlock=request.startBlock.number, max=request.maxResults
let results = await peer.getBlockHeaders(request) let results = await peer.getBlockHeaders(request)
if results.isSome: if results.isSome:
tracePacket "<< Got reply eth.BlockHeaders (0x04)", peer,
count=results.get.headers.len
shallowCopy(workItem.headers, results.get.headers) shallowCopy(workItem.headers, results.get.headers)
var bodies = newSeq[BlockBody]() var bodies = newSeq[BlockBody]()
@ -239,16 +249,24 @@ proc obtainBlocksFromPeer(syncCtx: SyncContext, peer: Peer) {.async.} =
nextIndex = nextIndex + 1 nextIndex = nextIndex + 1
hashes.add(blockHash(i)) hashes.add(blockHash(i))
if hashes.len == maxBodiesFetch: if hashes.len == maxBodiesFetch:
tracePacket ">> Sending eth.GetBlockBodies (0x05)", peer,
count=hashes.len
let b = await peer.getBlockBodies(hashes) let b = await peer.getBlockBodies(hashes)
if b.isNone: if b.isNone:
raise newException(CatchableError, "Was not able to get the block bodies.") raise newException(CatchableError, "Was not able to get the block bodies.")
tracePacket "<< Got reply eth.BlockBodies (0x06)", peer,
count=b.get.blocks.len
hashes.setLen(0) hashes.setLen(0)
bodies.add(b.get.blocks) bodies.add(b.get.blocks)
if hashes.len != 0: if hashes.len != 0:
tracePacket ">> Sending eth.GetBlockBodies (0x05)", peer,
count=hashes.len
let b = await peer.getBlockBodies(hashes) let b = await peer.getBlockBodies(hashes)
if b.isNone: if b.isNone:
raise newException(CatchableError, "Was not able to get the block bodies.") raise newException(CatchableError, "Was not able to get the block bodies.")
tracePacket "<< Got reply eth.BlockBodies (0x06)", peer,
count=b.get.blocks.len
bodies.add(b.get.blocks) bodies.add(b.get.blocks)
if bodies.len == workItem.headers.len: if bodies.len == workItem.headers.len:
@ -301,8 +319,16 @@ proc peersAgreeOnChain(a, b: Peer): Future[bool] {.async.} =
skip: 0, skip: 0,
reverse: true) reverse: true)
tracePacket ">> Sending eth.GetBlockHeaders (0x03)", peer=a,
startBlock=request.startBlock.hash, max=request.maxResults
let latestBlock = await a.getBlockHeaders(request) let latestBlock = await a.getBlockHeaders(request)
result = latestBlock.isSome and latestBlock.get.headers.len > 0 result = latestBlock.isSome and latestBlock.get.headers.len > 0
if tracePackets and latestBlock.isSome:
let blockNumber = if result: $latestBlock.get.headers[0].blockNumber
else: "missing"
tracePacket "<< Got reply eth.BlockHeaders (0x04)", peer=a,
count=latestBlock.get.headers.len, blockNumber
proc randomTrustedPeer(ctx: SyncContext): Peer = proc randomTrustedPeer(ctx: SyncContext): Peer =
var k = rand(ctx.trustedPeers.len - 1) var k = rand(ctx.trustedPeers.len - 1)