Skip to content

Commit

Permalink
Fix: fixed events in blockdag
Browse files Browse the repository at this point in the history
  • Loading branch information
hmoog committed Mar 12, 2024
1 parent d9aaad6 commit 139a7aa
Showing 1 changed file with 42 additions and 32 deletions.
74 changes: 42 additions & 32 deletions pkg/protocol/engine/blockdag/inmemoryblockdag/blockdag.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func NewProvider(opts ...options.Option[BlockDAG]) module.Provider[*engine.Engin
b := New(e.NewSubModule("BlockDAG"), e.Workers.CreateGroup("BlockDAG"), int(e.Storage.Settings().APIProvider().CommittedAPI().ProtocolParameters().MaxCommittableAge())*2, e.EvictionState, e.BlockCache, e.ErrorHandler("blockdag"), opts...)

e.ConstructedEvent().OnTrigger(func() {
b.Init(e.SyncManager.LatestCommitment)

wp := b.workers.CreatePool("BlockDAG.Append", workerpool.WithWorkerCount(2))

e.Events.PreSolidFilter.BlockPreAllowed.Hook(func(block *model.Block) {
Expand All @@ -56,54 +58,34 @@ func NewProvider(opts ...options.Option[BlockDAG]) module.Provider[*engine.Engin
}
}, event.WithWorkerPool(wp))

b.latestCommitmentFunc = e.SyncManager.LatestCommitment

e.Events.BlockDAG.LinkTo(b.events)
})

return b
})
}

func (b *BlockDAG) setupBlock(block *blocks.Block) {
var unsolidParentsCount atomic.Int32
unsolidParentsCount.Store(int32(len(block.Parents())))

block.ForEachParent(func(parent iotago.Parent) {
parentBlock, exists := b.blockCache.Block(parent.ID)
if !exists {
b.errorHandler(ierrors.Errorf("failed to setup block %s, parent %s is missing", block.ID(), parent.ID))

return
}

parentBlock.Solid().OnUpdateOnce(func(_ bool, _ bool) {
if unsolidParentsCount.Add(-1) == 0 {
if block.SetSolid() {
b.events.BlockSolid.Trigger(block)
}
}
})

parentBlock.Invalid().OnUpdateOnce(func(_ bool, _ bool) {
if block.SetInvalid() {
b.events.BlockInvalid.Trigger(block, ierrors.Errorf("parent block %s is marked as invalid", parent.ID))
}
})
})
}

// New is the constructor for the BlockDAG and creates a new BlockDAG instance.
func New(subModule module.Module, workers *workerpool.Group, unsolidCommitmentBufferSize int, evictionState *eviction.State, blockCache *blocks.Blocks, errorHandler func(error), opts ...options.Option[BlockDAG]) (newBlockDAG *BlockDAG) {
return module.InitSimpleLifecycle(options.Apply(&BlockDAG{
return options.Apply(&BlockDAG{
Module: subModule,
events: blockdag.NewEvents(),
evictionState: evictionState,
blockCache: blockCache,
workers: workers,
errorHandler: errorHandler,
uncommittedSlotBlocks: buffer.NewUnsolidCommitmentBuffer[*blocks.Block](unsolidCommitmentBufferSize),
}, opts), (*BlockDAG).shutdown)
}, opts, func(b *BlockDAG) {
b.ShutdownEvent().OnTrigger(b.shutdown)

b.ConstructedEvent().Trigger()
})
}

func (b *BlockDAG) Init(latestCommitmentFunc func() *model.Commitment) {
b.latestCommitmentFunc = latestCommitmentFunc

b.InitializedEvent().Trigger()
}

// Append is used to append new Blocks to the BlockDAG. It is the main function of the BlockDAG that triggers Events.
Expand Down Expand Up @@ -151,6 +133,34 @@ func (b *BlockDAG) Reset() {
b.uncommittedSlotBlocks.Reset()
}

func (b *BlockDAG) setupBlock(block *blocks.Block) {
var unsolidParentsCount atomic.Int32
unsolidParentsCount.Store(int32(len(block.Parents())))

block.ForEachParent(func(parent iotago.Parent) {
parentBlock, exists := b.blockCache.Block(parent.ID)
if !exists {
b.errorHandler(ierrors.Errorf("failed to setup block %s, parent %s is missing", block.ID(), parent.ID))

return
}

parentBlock.Solid().OnUpdateOnce(func(_ bool, _ bool) {
if unsolidParentsCount.Add(-1) == 0 {
if block.SetSolid() {
b.events.BlockSolid.Trigger(block)
}
}
})

parentBlock.Invalid().OnUpdateOnce(func(_ bool, _ bool) {
if block.SetInvalid() {
b.events.BlockInvalid.Trigger(block, ierrors.Errorf("parent block %s is marked as invalid", parent.ID))
}
})
})
}

// append tries to append the given Block to the BlockDAG.
func (b *BlockDAG) append(modelBlock *model.Block) (block *blocks.Block, wasAppended bool, err error) {
shouldAppend, err := b.shouldAppend(modelBlock)
Expand Down

0 comments on commit 139a7aa

Please sign in to comment.