Skip to content

Commit

Permalink
Merge pull request #867 from iotaledger/debug/acceptance-halt
Browse files Browse the repository at this point in the history
Debug: Acceptance halt
  • Loading branch information
piotrm50 authored Mar 25, 2024
2 parents acb7eeb + 8fca0d6 commit 11c39f7
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 167 deletions.
42 changes: 17 additions & 25 deletions pkg/protocol/engine/blocks/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Block struct {
payloadSpenderIDs ds.Set[iotago.TransactionID]

// BlockGadget block
preAccepted bool
preAccepted reactive.Variable[bool]
acceptanceRatifiers ds.Set[account.SeatIndex]
accepted reactive.Variable[bool]
preConfirmed bool
Expand Down Expand Up @@ -86,6 +86,7 @@ func NewBlock(modelBlock *model.Block) *Block {
solid: reactive.NewVariable[bool](),
invalid: reactive.NewVariable[bool](),
booked: reactive.NewVariable[bool](),
preAccepted: reactive.NewVariable[bool](),
accepted: reactive.NewVariable[bool](),
weightPropagated: reactive.NewVariable[bool](),
notarized: reactive.NewEvent(),
Expand All @@ -106,22 +107,17 @@ func NewRootBlock(blockID iotago.BlockID, commitmentID iotago.CommitmentID, issu
commitmentID: commitmentID,
issuingTime: issuingTime,
},
solid: reactive.NewVariable[bool](),
solid: reactive.NewVariable[bool]().Init(true),
invalid: reactive.NewVariable[bool](),
booked: reactive.NewVariable[bool](),
preAccepted: true,
accepted: reactive.NewVariable[bool](),
weightPropagated: reactive.NewVariable[bool](),
booked: reactive.NewVariable[bool]().Init(true),
preAccepted: reactive.NewVariable[bool]().Init(true),
accepted: reactive.NewVariable[bool]().Init(true),
weightPropagated: reactive.NewVariable[bool]().Init(true),
notarized: reactive.NewEvent(),
scheduled: true,
}

// This should be true since we commit and evict on acceptance.
b.solid.Set(true)
b.booked.Set(true)
b.weightPropagated.Set(true)
b.notarized.Set(true)
b.accepted.Set(true)
b.notarized.Trigger()

return b
}
Expand All @@ -138,6 +134,7 @@ func NewMissingBlock(blockID iotago.BlockID) *Block {
solid: reactive.NewVariable[bool](),
invalid: reactive.NewVariable[bool](),
booked: reactive.NewVariable[bool](),
preAccepted: reactive.NewVariable[bool](),
accepted: reactive.NewVariable[bool](),
weightPropagated: reactive.NewVariable[bool](),
notarized: reactive.NewEvent(),
Expand Down Expand Up @@ -428,24 +425,19 @@ func (b *Block) SetPayloadSpenderIDs(payloadSpenderIDs ds.Set[iotago.Transaction
b.payloadSpenderIDs = payloadSpenderIDs
}

// PreAccepted returns a reactive variable that is true if the Block was pre accepted.
func (b *Block) PreAccepted() reactive.Variable[bool] {
return b.preAccepted
}

// IsPreAccepted returns true if the Block was preAccepted.
func (b *Block) IsPreAccepted() bool {
b.mutex.RLock()
defer b.mutex.RUnlock()

return b.preAccepted
return b.preAccepted.Get()
}

// SetPreAccepted sets the Block as preAccepted.
func (b *Block) SetPreAccepted() (wasUpdated bool) {
b.mutex.Lock()
defer b.mutex.Unlock()

if wasUpdated = !b.preAccepted; wasUpdated {
b.preAccepted = true
}

return wasUpdated
return !b.preAccepted.Set(true)
}

func (b *Block) AddAcceptanceRatifier(seat account.SeatIndex) (added bool) {
Expand Down Expand Up @@ -644,7 +636,7 @@ func (b *Block) String() string {
builder.AddField(stringify.NewStructField("Invalid", b.invalid.Get()))
builder.AddField(stringify.NewStructField("Booked", b.booked.Get()))
builder.AddField(stringify.NewStructField("Witnesses", b.witnesses))
builder.AddField(stringify.NewStructField("PreAccepted", b.preAccepted))
builder.AddField(stringify.NewStructField("PreAccepted", b.preAccepted.Get()))
builder.AddField(stringify.NewStructField("AcceptanceRatifiers", b.acceptanceRatifiers.String()))
builder.AddField(stringify.NewStructField("Accepted", b.accepted.Get()))
builder.AddField(stringify.NewStructField("PreConfirmed", b.preConfirmed))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (m *Manager) ForceCommit(slot iotago.SlotIndex) (*model.Commitment, error)
}

func (m *Manager) ForceCommitUntil(commitUntilSlot iotago.SlotIndex) error {
m.LogInfof("Force commit until slot %d", commitUntilSlot)
m.LogInfo("force committing until", "slot", commitUntilSlot)

for i := m.storage.Settings().LatestCommitment().Slot() + 1; i <= commitUntilSlot; i++ {
if _, err := m.ForceCommit(i); err != nil {
Expand Down
14 changes: 14 additions & 0 deletions pkg/protocol/engine/tipmanager/tip_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,17 @@ func (t TipPool) Max(other TipPool) TipPool {

return other
}

// String returns a human-readable representation of the TipPool.
func (t TipPool) String() string {
switch t {
case StrongTipPool:
return "StrongTipPool"
case WeakTipPool:
return "WeakTipPool"
case DroppedTipPool:
return "DroppedTipPool"
default:
return "UndefinedTipPool"
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package tipmanagerv1

import (
"fmt"

"github.com/iotaledger/hive.go/ds/randommap"
"github.com/iotaledger/hive.go/ds/reactive"
"github.com/iotaledger/hive.go/ds/shrinkingmap"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/hive.go/log"
"github.com/iotaledger/hive.go/runtime/event"
"github.com/iotaledger/hive.go/runtime/module"
"github.com/iotaledger/hive.go/runtime/syncutils"
Expand Down Expand Up @@ -58,7 +57,7 @@ func New(
blockRetriever func(blockID iotago.BlockID) (block *blocks.Block, exists bool),
retrieveCommitteeInSlot func(slot iotago.SlotIndex) (*account.SeatedAccounts, bool),
) *TipManager {
return module.InitSimpleLifecycle(&TipManager{
t := &TipManager{
Module: subModule,
retrieveBlock: blockRetriever,
retrieveCommitteeInSlot: retrieveCommitteeInSlot,
Expand All @@ -68,7 +67,11 @@ func New(
strongTipSet: randommap.New[iotago.BlockID, *TipMetadata](),
weakTipSet: randommap.New[iotago.BlockID, *TipMetadata](),
blockAdded: event.New1[tipmanager.TipMetadata](),
})
}

t.initLogging()

return module.InitSimpleLifecycle(t)
}

// AddBlock adds a Block to the TipManager and returns the TipMetadata if the Block was added successfully.
Expand All @@ -78,12 +81,9 @@ func (t *TipManager) AddBlock(block *blocks.Block) tipmanager.TipMetadata {
return nil
}

tipMetadata, created := storage.GetOrCreate(block.ID(), func() *TipMetadata {
return NewBlockMetadata(block)
})

tipMetadata, created := storage.GetOrCreate(block.ID(), t.tipMetadataFactory(block))
if created {
t.setupBlockMetadata(tipMetadata)
t.trackTipMetadata(tipMetadata)
}

return tipMetadata
Expand Down Expand Up @@ -153,8 +153,26 @@ func (t *TipManager) Reset() {
lo.ForEach(t.weakTipSet.Keys(), func(id iotago.BlockID) { t.weakTipSet.Delete(id) })
}

// setupBlockMetadata sets up the behavior of the given Block.
func (t *TipManager) setupBlockMetadata(tipMetadata *TipMetadata) {
// initLogging initializes the logging of the TipManager.
func (t *TipManager) initLogging() {
logLevel := log.LevelTrace

t.blockAdded.Hook(func(metadata tipmanager.TipMetadata) {
t.Log("block added", logLevel, "blockID", metadata.ID())
})
}

// tipMetadataFactory creates a function that can be called to create a new TipMetadata instance for the given Block.
func (t *TipManager) tipMetadataFactory(block *blocks.Block) func() *TipMetadata {
return func() *TipMetadata {
return NewTipMetadata(t.NewChildLogger(block.ID().String()), block)
}
}

// trackTipMetadata sets up the tracking of the given TipMetadata in the TipManager.
func (t *TipManager) trackTipMetadata(tipMetadata *TipMetadata) {
t.blockAdded.Trigger(tipMetadata)

tipMetadata.isStrongTipPoolMember.WithNonEmptyValue(func(_ bool) func() {
return t.trackLatestValidationBlock(tipMetadata)
})
Expand Down Expand Up @@ -190,8 +208,6 @@ func (t *TipManager) setupBlockMetadata(tipMetadata *TipMetadata) {
tipMetadata.connectWeakParent(parentMetadata)
}
})

t.blockAdded.Trigger(tipMetadata)
}

// trackLatestValidationBlock tracks the latest validator block and takes care of marking the corresponding TipMetadata.
Expand Down Expand Up @@ -232,23 +248,18 @@ func (t *TipManager) trackLatestValidationBlock(tipMetadata *TipMetadata) (teard
func (t *TipManager) forEachParentByType(block *blocks.Block, consumer func(parentType iotago.ParentsType, parentMetadata *TipMetadata)) {
for _, parent := range block.ParentsWithType() {
if metadataStorage := t.metadataStorage(parent.ID.Slot()); metadataStorage != nil {
// Make sure we don't add root blocks back to the tips.
// make sure we don't add root blocks back to the tips.
parentBlock, exists := t.retrieveBlock(parent.ID)

if !exists || parentBlock.IsRootBlock() {
continue
}

if parentBlock.ModelBlock() == nil {
fmt.Printf(">> parentBlock exists, but parentBlock.ProtocolBlock() == nil\n ParentBlock: %s\n Block: %s\n", parentBlock.String(), block.String())
parentMetadata, created := metadataStorage.GetOrCreate(parent.ID, t.tipMetadataFactory(parentBlock))
if created {
t.trackTipMetadata(parentMetadata)
}

parentMetadata, created := metadataStorage.GetOrCreate(parent.ID, func() *TipMetadata { return NewBlockMetadata(parentBlock) })
consumer(parent.Type, parentMetadata)

if created {
t.setupBlockMetadata(parentMetadata)
}
}
}
}
Expand Down Expand Up @@ -285,6 +296,3 @@ func (t *TipManager) selectTips(tipSet *randommap.RandomMap[iotago.BlockID, *Tip

return lo.Map(tipSet.Values(), func(tip *TipMetadata) tipmanager.TipMetadata { return tip })
}

// code contract (make sure the type implements all required methods).
var _ tipmanager.TipManager = new(TipManager)
Loading

0 comments on commit 11c39f7

Please sign in to comment.