From 18f1b26c5ee92a29d63b41208d82eafb5fdbd6e7 Mon Sep 17 00:00:00 2001 From: Samay Kothari <2001samay@gmail.com> Date: Fri, 5 Aug 2022 00:09:59 +0530 Subject: [PATCH] netsync: adding header validation upon header download Adding the functionality that the headers would be validated as soon as they are recieved from the peer --- netsync/manager.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/netsync/manager.go b/netsync/manager.go index 737cd7ec..dcc013ac 100644 --- a/netsync/manager.go +++ b/netsync/manager.go @@ -905,13 +905,13 @@ func (sm *SyncManager) fetchHeaderBlocks() { } iv := wire.NewInvVect(wire.InvTypeBlock, node.hash) - haveInv, err := sm.haveInventory(iv) + haveBlock, err := sm.chain.HaveBlock(node.hash) if err != nil { log.Warnf("Unexpected failure when checking for "+ - "existing inventory during header block "+ + "existing block during header block "+ "fetch: %v", err) } - if !haveInv { + if !haveBlock { syncPeerState := sm.peerStates[sm.syncPeer] sm.requestedBlocks[*node.hash] = struct{}{} @@ -978,6 +978,7 @@ func (sm *SyncManager) handleHeadersMsg(hmsg *headersMsg) { // previous and that checkpoints match. receivedCheckpoint := false var finalHash *chainhash.Hash + chain := sm.chain for _, blockHeader := range msg.Headers { blockHash := blockHeader.BlockHash() finalHash = &blockHash @@ -1008,7 +1009,17 @@ func (sm *SyncManager) handleHeadersMsg(hmsg *headersMsg) { peer.Disconnect() return } - + // Processing the block headers that are downloaded and if it passes + // all the checks, creating a block node that only contains header. + err := chain.ProcessBlockHeader(blockHeader) + if err != nil { + // Note that there is no need to check for an orphan header here + // because they were already verified to connect above. + log.Debugf("Failed to process block header %s from peer %s: %v -- "+ + "disconnecting", blockHeader.BlockHash(), peer, err) + peer.Disconnect() + return + } // Verify the header at the next checkpoint height matches. if node.height == sm.nextCheckpoint.Height { if node.hash.IsEqual(sm.nextCheckpoint.Hash) {