Skip to content

Commit

Permalink
netsync: adding header validation upon header download
Browse files Browse the repository at this point in the history
Adding the functionality that the headers would be validated as soon as they are recieved from the peer
  • Loading branch information
samay-kothari committed Aug 10, 2022
1 parent 18a49dc commit 18f1b26
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions netsync/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 18f1b26

Please sign in to comment.