Skip to content

Commit

Permalink
Feat: added Reset of TipManager and TipSelection
Browse files Browse the repository at this point in the history
  • Loading branch information
hmoog committed Nov 7, 2023
1 parent 53c7f7d commit 731be8d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
15 changes: 9 additions & 6 deletions pkg/protocol/engine/tipmanager/v1/tipmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,11 @@ type TipManager struct {
// New creates a new TipManager.
func New(blockRetriever func(blockID iotago.BlockID) (block *blocks.Block, exists bool)) *TipManager {
t := &TipManager{
retrieveBlock: blockRetriever,
tipMetadataStorage: shrinkingmap.New[iotago.SlotIndex, *shrinkingmap.ShrinkingMap[iotago.BlockID, *TipMetadata]](),
strongTipSet: randommap.New[iotago.BlockID, *TipMetadata](),
weakTipSet: randommap.New[iotago.BlockID, *TipMetadata](),
blockAdded: event.New1[tipmanager.TipMetadata](),
retrieveBlock: blockRetriever,
blockAdded: event.New1[tipmanager.TipMetadata](),
}

t.Reset()
t.TriggerConstructed()
t.TriggerInitialized()

Expand Down Expand Up @@ -105,7 +103,12 @@ func (t *TipManager) Evict(slot iotago.SlotIndex) {

// Reset resets the component to a clean state as if it was created at the last commitment.
func (t *TipManager) Reset() {
// TODO: reset tip metadata storage
t.evictionMutex.Lock()
defer t.evictionMutex.Unlock()

t.tipMetadataStorage = shrinkingmap.New[iotago.SlotIndex, *shrinkingmap.ShrinkingMap[iotago.BlockID, *TipMetadata]]()
t.strongTipSet = randommap.New[iotago.BlockID, *TipMetadata]()
t.weakTipSet = randommap.New[iotago.BlockID, *TipMetadata]()
}

// Shutdown marks the TipManager as shutdown.
Expand Down
34 changes: 33 additions & 1 deletion pkg/protocol/engine/tipselection/v1/tip_selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/hive.go/runtime/module"
"github.com/iotaledger/hive.go/runtime/options"
"github.com/iotaledger/hive.go/runtime/syncutils"
"github.com/iotaledger/hive.go/runtime/timed"
"github.com/iotaledger/iota-core/pkg/model"
"github.com/iotaledger/iota-core/pkg/protocol/engine/blocks"
Expand Down Expand Up @@ -55,6 +56,12 @@ type TipSelection struct {
// optMaxWeakReferences contains the maximum number of weak references that are allowed.
optMaxWeakReferences int

// livenessThresholdQueueMutex is used to synchronize access to the liveness threshold queue.
livenessThresholdQueueMutex syncutils.RWMutex

// acceptanceTimeMutex is used to synchronize access to the acceptance time.
acceptanceTimeMutex syncutils.RWMutex

// Module embeds the required methods of the module.Interface.
module.Module
}
Expand Down Expand Up @@ -139,12 +146,16 @@ func (t *TipSelection) SelectTips(amount int) (references model.ParentReferences

// SetAcceptanceTime updates the acceptance time of the TipSelection.
func (t *TipSelection) SetAcceptanceTime(acceptanceTime time.Time) (previousValue time.Time) {
t.acceptanceTimeMutex.RLock()
defer t.acceptanceTimeMutex.RUnlock()

return t.acceptanceTime.Set(acceptanceTime)
}

// Reset resets the component to a clean state as if it was created at the last commitment.
func (t *TipSelection) Reset() {
// TODO: reset acceptance time and liveness threshold queue
t.resetAcceptanceTime()
t.resetLivenessThresholdQueue()
}

// Shutdown triggers the shutdown of the TipSelection.
Expand Down Expand Up @@ -235,6 +246,9 @@ func (t *TipSelection) isValidWeakTip(block *blocks.Block) bool {

// triggerLivenessThreshold triggers the liveness threshold for all tips that have reached the given threshold.
func (t *TipSelection) triggerLivenessThreshold(threshold time.Time) {
t.livenessThresholdQueueMutex.RLock()
defer t.livenessThresholdQueueMutex.RUnlock()

for _, tip := range t.livenessThresholdQueue.PopUntil(threshold) {
if dynamicLivenessThreshold := tip.Block().IssuingTime().Add(t.livenessThreshold(tip)); dynamicLivenessThreshold.After(threshold) {
t.livenessThresholdQueue.Push(tip, dynamicLivenessThreshold)
Expand All @@ -244,6 +258,24 @@ func (t *TipSelection) triggerLivenessThreshold(threshold time.Time) {
}
}

func (t *TipSelection) resetLivenessThresholdQueue() {
t.livenessThresholdQueueMutex.Lock()
defer t.livenessThresholdQueueMutex.Unlock()

t.livenessThresholdQueue = timed.NewPriorityQueue[tipmanager.TipMetadata](true)
}

func (t *TipSelection) resetAcceptanceTime() {
t.acceptanceTimeMutex.Lock()
defer t.acceptanceTimeMutex.Unlock()

t.acceptanceTime = reactive.NewVariable[time.Time](monotonicallyIncreasing)

t.acceptanceTime.OnUpdate(func(_, acceptanceTime time.Time) {
t.triggerLivenessThreshold(acceptanceTime)
})
}

// WithMaxStrongParents is an option for the TipSelection that allows to configure the maximum number of strong parents.
func WithMaxStrongParents(maxStrongParents int) options.Option[TipSelection] {
return func(tipManager *TipSelection) {
Expand Down

0 comments on commit 731be8d

Please sign in to comment.