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(sync): Add badBlocks support #3214

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions dot/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,11 @@ func (nodeBuilder) newSyncService(cfg *Config, st *state.Service, fg BlockJustif
return nil, err
}

genesisData, err := st.Base.LoadGenesisData()
if err != nil {
return nil, err
}

syncCfg := &sync.Config{
LogLvl: cfg.Log.SyncLvl,
Network: net,
Expand All @@ -455,6 +460,7 @@ func (nodeBuilder) newSyncService(cfg *Config, st *state.Service, fg BlockJustif
MaxPeers: cfg.Network.MaxPeers,
SlotDuration: slotDuration,
Telemetry: telemetryMailer,
BadBlocks: genesisData.BadBlocks,
}

return sync.NewService(syncCfg)
Expand Down
14 changes: 14 additions & 0 deletions dot/sync/chain_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ type chainSync struct {
finalityGadget FinalityGadget
blockImportHandler BlockImportHandler
telemetry Telemetry
badBlocks []string
}

type chainSyncConfig struct {
Expand All @@ -189,6 +190,7 @@ type chainSyncConfig struct {
finalityGadget FinalityGadget
blockImportHandler BlockImportHandler
telemetry Telemetry
badBlocks []string
}

func newChainSync(cfg chainSyncConfig) *chainSync {
Expand Down Expand Up @@ -226,6 +228,7 @@ func newChainSync(cfg chainSyncConfig) *chainSync {
logSyncTickerC: logSyncTicker.C,
logSyncDone: make(chan struct{}),
workerPool: newSyncWorkerPool(cfg.net),
badBlocks: cfg.badBlocks,
}
}

Expand Down Expand Up @@ -632,6 +635,10 @@ loop:

err := cs.validateResponse(request, response, who)
switch {
case errors.Is(err, errBadBlock):
logger.Criticalf("Rejecting known bad block: $s", err)
cs.workerPool.shutdownWorker(taskResult.who, true)
cs.workerPool.submitRequest(taskResult.request, workersResults)
case errors.Is(err, errResponseIsNotChain):
logger.Criticalf("response invalid: %s", err)
cs.workerPool.shutdownWorker(taskResult.who, true)
Expand Down Expand Up @@ -1014,6 +1021,13 @@ func (cs *chainSync) validateBlockData(req *network.BlockRequestMessage, bd *typ

requestedData := req.RequestedData

for _, badBlockHash := range cs.badBlocks {
if bd.Hash.String() == badBlockHash {
logger.Errorf("Rejecting known bad block Number: %d Hash: %s", bd.Number(), bd.Hash)
return errBadBlock
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for _, badBlockHash := range cs.badBlocks {
if bd.Hash.String() == badBlockHash {
logger.Errorf("Rejecting known bad block Number: %d Hash: %s", bd.Number(), bd.Hash)
return errBadBlock
}
}
if slices.Contains(cs.badBlocks, bd.Hash.String) {
logger.Errorf("Rejecting known bad block Number: %d Hash: %s", bd.Number(), bd.Hash)
return errBadBlock
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Thks for the suggestion

if (requestedData&network.RequestedDataHeader) == 1 && bd.Header == nil {
cs.network.ReportPeer(peerset.ReputationChange{
Value: peerset.IncompleteHeaderValue,
Expand Down
1 change: 1 addition & 0 deletions dot/sync/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ var (
errFailedToGetParent = errors.New("failed to get parent header")
errStartAndEndMismatch = errors.New("request start and end hash are not on the same chain")
errFailedToGetDescendant = errors.New("failed to find descendant block")
errBadBlock = errors.New("known bad block")
)
1 change: 0 additions & 1 deletion dot/sync/sync_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func (s *syncWorker) update(bestHash common.Hash, bestNumber uint) {
}

var errBadPeerWorker = errors.New("bad peer worker")
var errBadBlock = errors.New("bad block")

func (s *syncWorker) Start(tasks <-chan *syncTask, wg *sync.WaitGroup) {
wg.Add(1)
Expand Down
2 changes: 2 additions & 0 deletions dot/sync/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Config struct {
MinPeers, MaxPeers int
SlotDuration time.Duration
Telemetry Telemetry
BadBlocks []string
}

// NewService returns a new *sync.Service
Expand All @@ -58,6 +59,7 @@ func NewService(cfg *Config) (*Service, error) {
finalityGadget: cfg.FinalityGadget,
blockImportHandler: cfg.BlockImportHandler,
telemetry: cfg.Telemetry,
badBlocks: cfg.BadBlocks,
}
chainSync := newChainSync(csCfg)

Expand Down