Skip to content

Commit

Permalink
feat(BUX-712): refactor handleHeadersMsg function
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba-4chain committed Apr 4, 2024
1 parent da56d7e commit 7a04812
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions internal/transports/p2p/peer/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,29 @@ func (p *Peer) handleHeadersMsg(msg *wire.MsgHeaders) {
headersReceived, p, lastHeight,
)

p.requestNextHeadersBatch(receivedCheckpoint, lastHeight)
}

func (p *Peer) verifyCheckpointReached(h *domains.BlockHeader, receivedCheckpoint bool) (bool, error) {
if p.nextCheckpoint != nil && p.nextCheckpoint.Hash != nil && h.Height == p.nextCheckpoint.Height {
if h.Hash == *p.nextCheckpoint.Hash {
receivedCheckpoint = true
p.log.Info().Msgf(
"verified downloaded block header against checkpoint at height %d / hash %s",
h.Height, h.Hash,
)
} else {
p.log.Error().Msgf(
"block header at height %d/hash %s from peer %s does NOT match expected checkpoint hash of %s -- disconnecting",
h.Height, h.Hash, p, p.nextCheckpoint.Hash,
)
return false, fmt.Errorf("corresponding checkpoint height does not match got: %v, exp: %v", h.Height, p.nextCheckpoint.Height)
}
}
return receivedCheckpoint, nil
}

func (p *Peer) requestNextHeadersBatch(receivedCheckpoint bool, lastHeight int32) {
if receivedCheckpoint {
p.nextCheckpoint = findNextHeaderCheckpoint(p.checkpoints, p.nextCheckpoint.Height)
if p.nextCheckpoint == nil {
Expand All @@ -518,31 +541,13 @@ func (p *Peer) handleHeadersMsg(msg *wire.MsgHeaders) {
"checkpoints synced, requesting headers from height %d up to end of chain (zero hash) from peer %s",
lastHeight, p,
)

err := p.writeGetHeadersMsg(&zeroHash)
if err != nil {
p.log.Error().Msgf("error requesting headers from peer %s, reason: %v", p, err)
}
}

func (p *Peer) verifyCheckpointReached(h *domains.BlockHeader, receivedCheckpoint bool) (bool, error) {
if p.nextCheckpoint != nil && p.nextCheckpoint.Hash != nil && h.Height == p.nextCheckpoint.Height {
if h.Hash == *p.nextCheckpoint.Hash {
receivedCheckpoint = true
p.log.Info().Msgf(
"verified downloaded block header against checkpoint at height %d / hash %s",
h.Height, h.Hash,
)
} else {
p.log.Error().Msgf(
"block header at height %d/hash %s from peer %s does NOT match expected checkpoint hash of %s -- disconnecting",
h.Height, h.Hash, p, p.nextCheckpoint.Hash,
)
return false, fmt.Errorf("corresponding checkpoint height does not match got: %v, exp: %v", h.Height, p.nextCheckpoint.Height)
}
}
return receivedCheckpoint, nil
}

func (p *Peer) String() string {
return p.addr.String()
}

0 comments on commit 7a04812

Please sign in to comment.