Skip to content

Commit

Permalink
Remove blocks that failed to be appended from the ticker.
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrm50 committed Jun 5, 2024
1 parent 65060ce commit 21197e8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pkg/protocol/engine/blockdag/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package blockdag
import (
"github.com/iotaledger/hive.go/runtime/event"
"github.com/iotaledger/iota-core/pkg/protocol/engine/blocks"
iotago "github.com/iotaledger/iota.go/v4"
)

// Events is a collection of Tangle related Events.
Expand All @@ -19,6 +20,9 @@ type Events struct {
// MissingBlockAppended is triggered when a previously missing Block was appended.
MissingBlockAppended *event.Event1[*blocks.Block]

// BlockNotAppended is triggered when an incoming Block could not be successfully appended.
BlockNotAppended *event.Event1[iotago.BlockID]

// BlockInvalid is triggered when a Block is found to be invalid.
BlockInvalid *event.Event2[*blocks.Block, error]

Expand All @@ -32,6 +36,7 @@ var NewEvents = event.CreateGroupConstructor(func() (newEvents *Events) {
BlockSolid: event.New1[*blocks.Block](),
BlockMissing: event.New1[*blocks.Block](),
MissingBlockAppended: event.New1[*blocks.Block](),
BlockNotAppended: event.New1[iotago.BlockID](),
BlockInvalid: event.New2[*blocks.Block, error](),
}
})
1 change: 1 addition & 0 deletions pkg/protocol/engine/blockdag/inmemoryblockdag/blockdag.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func NewProvider(opts ...options.Option[BlockDAG]) module.Provider[*engine.Engin

e.Events.PreSolidFilter.BlockPreAllowed.Hook(func(block *model.Block) {
if _, _, err := b.Append(block); err != nil {
b.events.BlockNotAppended.Trigger(block.ID())
b.LogError("failed to append block", "blockID", block.ID(), "issuer", block.ProtocolBlock().Header.IssuerID, "err", err)
}
}, event.WithWorkerPool(wp))
Expand Down
12 changes: 10 additions & 2 deletions pkg/protocol/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,15 +585,23 @@ func (e *Engine) setupEvictionState() {

func (e *Engine) setupBlockRequester() {
e.Events.BlockRequester.LinkTo(e.BlockRequester.Events)

wp := e.Workers.CreatePool("BlockRequester", workerpool.WithWorkerCount(1)) // Using just 1 worker to avoid contention
// We need to hook to make sure that the request is created before the block arrives to avoid a race condition
// where we try to delete the request again before it is created. Thus, continuing to request forever.
e.Events.BlockDAG.BlockMissing.Hook(func(block *blocks.Block) {
e.BlockRequester.StartTicker(block.ID())
})

e.Events.BlockDAG.MissingBlockAppended.Hook(func(block *blocks.Block) {
e.BlockRequester.StopTicker(block.ID())
}, event.WithWorkerPool(e.Workers.CreatePool("BlockRequester", workerpool.WithWorkerCount(1)))) // Using just 1 worker to avoid contention
}, event.WithWorkerPool(wp))

// Remove the block from the ticker if it failed to be appended.
// It's executed for all blocks to avoid locking twice:
// once to check if the block has the ticker and then again to remove it.
e.Events.BlockDAG.BlockNotAppended.Hook(func(blockID iotago.BlockID) {
e.BlockRequester.StopTicker(blockID)
}, event.WithWorkerPool(wp))
}

func (e *Engine) setupPruning() {
Expand Down

0 comments on commit 21197e8

Please sign in to comment.