Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(BUX-631): prevent unneeded header sync #220

Merged
merged 11 commits into from
Mar 12, 2024
15 changes: 10 additions & 5 deletions service/chain_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type BlockHasher interface {

// Notification is "port" through which chain service can notify clients about important events.
type Notification interface {
//Notify notifies about new header stored.
// Notify notifies about new header stored.
Notify(any)
}

Expand Down Expand Up @@ -97,6 +97,11 @@ func (cs *chainService) Add(bs domains.BlockHeaderSource) (*domains.BlockHeader,
return nil, err
}

currentTip, errTip := cs.Headers.GetTip()
dorzepowski marked this conversation as resolved.
Show resolved Hide resolved
if errTip == nil && h.IsLongestChain() && h.Height < currentTip.Height {
return h, err
}

metrics.SetLatestBlock(h.Height, h.Timestamp, h.State.String())
cs.notification.Notify(domains.HeaderAdded(h))
return h, err
Expand Down Expand Up @@ -236,16 +241,16 @@ type AddBlockError struct {
type AddBlockErrorCode string

const (
//BlockRejected error code representing situation when block is on the blacklist.
// BlockRejected error code representing situation when block is on the blacklist.
BlockRejected AddBlockErrorCode = "BlockRejected"

//HeaderCreationFail error code representing situation when block cannot be created from source.
// HeaderCreationFail error code representing situation when block cannot be created from source.
HeaderCreationFail AddBlockErrorCode = "HeaderCreationFail"

//ChainUpdateFail error code representing situation when STALE chain should become Longest chain but the update of chains failed.
// ChainUpdateFail error code representing situation when STALE chain should become Longest chain but the update of chains failed.
ChainUpdateFail AddBlockErrorCode = "ChainUpdateFail"

//HeaderSaveFail error code representing situation when saving header in the repository failed.
// HeaderSaveFail error code representing situation when saving header in the repository failed.
HeaderSaveFail AddBlockErrorCode = "HeaderSaveFail"
)

Expand Down
28 changes: 19 additions & 9 deletions transports/p2p/p2psync/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,6 @@ func (sm *SyncManager) handleCheckSyncPeer() {

// topBlock returns the best chains top block height.
func (sm *SyncManager) topBlock() int32 {

if sm.syncPeer.LastBlock() > sm.syncPeer.StartingHeight() {
return sm.syncPeer.LastBlock()
}
Expand Down Expand Up @@ -528,6 +527,7 @@ func (sm *SyncManager) handleHeadersMsg(hmsg *headersMsg) {
// previous and that checkpoints match.
receivedCheckpoint := false
var finalHash *chainhash.Hash
var finalHeight int32 = 0
for _, blockHeader := range msg.Headers {
h, addErr := sm.Services.Chains.Add(domains.BlockHeaderSource(*blockHeader))

Expand Down Expand Up @@ -564,6 +564,7 @@ func (sm *SyncManager) handleHeadersMsg(hmsg *headersMsg) {

if h.IsLongestChain() {
finalHash = &h.Hash
finalHeight = h.Height
}
if sm.startHeader == nil {
sm.startHeader = h
Expand Down Expand Up @@ -602,11 +603,16 @@ func (sm *SyncManager) handleHeadersMsg(hmsg *headersMsg) {
return
}

// If we received only blocks that we already have, or are not from the longest chain,
dorzepowski marked this conversation as resolved.
Show resolved Hide resolved
// don't send another getHeaders message - do nothing.
if finalHeight < sm.Services.Headers.GetTipHeight() {
dorzepowski marked this conversation as resolved.
Show resolved Hide resolved
return
}

// This header is not a checkpoint, so request the next batch of
// headers starting from the latest known header and ending with the
// next checkpoint.
sm.sendGetHeadersWithPassedParams([]*chainhash.Hash{finalHash}, sm.nextCheckpoint.Hash, peer)
dorzepowski marked this conversation as resolved.
Show resolved Hide resolved

}

func (sm *SyncManager) requestForNextHeaderBatch(prevHash *chainhash.Hash, peer *peerpkg.Peer, prevHeight int32) {
Expand Down Expand Up @@ -699,17 +705,21 @@ func (sm *SyncManager) handleInvMsg(imsg *invMsg) {
return
}

// If our chain is current and a peer announces a block we already
// know of, then update their current block height.
if lastBlock != -1 && sm.current() {
if lastBlock != -1 {
lastHeader := sm.Services.Headers.GetTip()

blkHeight, err := sm.Services.Headers.GetHeightByHash(&invVects[lastBlock].Hash)
if err == nil {
peer.UpdateLastBlockHeight(blkHeight)
// If our chain is current and a peer announces a block we already
// know of, then update their current block height.
if sm.current() {
peer.UpdateLastBlockHeight(blkHeight)
}
if blkHeight < lastHeader.Height {
dorzepowski marked this conversation as resolved.
Show resolved Hide resolved
return
}
}
}

if lastBlock != -1 {
lastHeader := sm.Services.Headers.GetTip()
sm.log.Info().Msgf("[Manager] handleInvMsg lastConfirmedHeaderNode.hash : %s", lastHeader.Hash)
sm.log.Info().Msgf("[Manager] handleInvMsg lastConfirmedHeaderNode.height : %d", lastHeader.Height)
sm.log.Info().Msgf("[Manager] handleInvMsg &invVects[lastBlock].Hash : %v", &invVects[lastBlock].Hash)
Expand Down
Loading