From 8dcf6a37a02c4518a1ffd9763d577ecefb2f1ef6 Mon Sep 17 00:00:00 2001 From: danield9tqh Date: Tue, 5 Sep 2023 14:47:02 -0700 Subject: [PATCH] Track originaing peer for block gossip --- ironfish/src/network/blockFetcher.ts | 19 ++++++++++++++++++- ironfish/src/network/peerNetwork.ts | 4 +++- ironfish/src/telemetry/telemetry.ts | 7 ++++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/ironfish/src/network/blockFetcher.ts b/ironfish/src/network/blockFetcher.ts index 392f20193b..81e2327580 100644 --- a/ironfish/src/network/blockFetcher.ts +++ b/ironfish/src/network/blockFetcher.ts @@ -214,11 +214,28 @@ export class BlockFetcher { action: 'PROCESSING_COMPACT_BLOCK', peer: peer.state.identity, compactBlock, - sources: currentState ? currentState.sources : new Set(), + sources: currentState ? currentState.sources : new Set([peer.state.identity]), }) return true } + /** + * Return the first peer that notified us of this block + */ + firstSeenBy(hash: BlockHash): Identity | undefined { + const currentState = this.pending.get(hash) + + if (!currentState || currentState.action === 'PROCESSING_FULL_BLOCK') { + return undefined + } + + // According to docs, values are returned in insertion order + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/values + for (const v of currentState.sources.values()) { + return v + } + } + requestBlockTransactions( peer: Peer, header: BlockHeader, diff --git a/ironfish/src/network/peerNetwork.ts b/ironfish/src/network/peerNetwork.ts index f106ce5b63..39b52858bd 100644 --- a/ironfish/src/network/peerNetwork.ts +++ b/ironfish/src/network/peerNetwork.ts @@ -1198,13 +1198,15 @@ export class PeerNetwork { return } + const firstPeer = this.blockFetcher.firstSeenBy(block.header.hash) + // Mark that we've assembled a full block in the block fetcher this.blockFetcher.receivedFullBlock(block) this.broadcastBlock(block) // log that we've validated the block enough to gossip it - this.telemetry.submitNewBlockSeen(block, new Date()) + this.telemetry.submitNewBlockSeen(block, new Date(), firstPeer) // verify the full block const verified = await this.chain.verifier.verifyBlockAdd(block, prevHeader) diff --git a/ironfish/src/telemetry/telemetry.ts b/ironfish/src/telemetry/telemetry.ts index d67dafecea..5407c522a5 100644 --- a/ironfish/src/telemetry/telemetry.ts +++ b/ironfish/src/telemetry/telemetry.ts @@ -403,7 +403,7 @@ export class Telemetry { }) } - submitNewBlockSeen(block: Block, seenAt: Date): void { + submitNewBlockSeen(block: Block, seenAt: Date, peerId?: Identity): void { this.submit({ measurement: 'block_propagation', timestamp: seenAt, @@ -419,6 +419,11 @@ export class Telemetry { type: 'integer', value: block.header.timestamp.valueOf(), }, + { + name: 'firstSeenBy', + type: 'string', + value: peerId || '', + }, { name: 'sequence', type: 'integer',