Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Commit a43667a

Browse files
committed
Feat: Add validator tip set
1 parent 928e923 commit a43667a

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

Diff for: pkg/protocol/engine/tipmanager/v1/tip_metadata.go

+32-8
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,22 @@ type TipMetadata struct {
4848
// that is connected to the tips.
4949
isReferencedByTips reactive.Variable[bool]
5050

51+
// isLatestValidatorBlock is true if the block is the latest block of a validator.
52+
isLatestValidatorBlock reactive.Variable[bool]
53+
54+
// referencesLatestValidatorBlock is true if the block is the latest validator block or has parents that reference
55+
// the latest validator block.
56+
referencesLatestValidatorBlock reactive.Variable[bool]
57+
5158
// isStrongTip is true if the block is a strong tip pool member and is not strongly referenced by other tips.
5259
isStrongTip reactive.Variable[bool]
5360

5461
// isWeakTip is true if the block is a weak tip pool member and is not referenced by other tips.
5562
isWeakTip reactive.Variable[bool]
5663

64+
// isValidatorTip is true if the block is a strong tip and references the latest validator block.
65+
isValidatorTip reactive.Variable[bool]
66+
5767
// isMarkedOrphaned is true if the liveness threshold has been reached and the block was not accepted.
5868
isMarkedOrphaned reactive.Variable[bool]
5969

@@ -85,21 +95,30 @@ type TipMetadata struct {
8595

8696
// weaklyOrphanedWeakParents holds the number of weak parents that are weakly orphaned.
8797
weaklyOrphanedWeakParents reactive.Counter[bool]
98+
99+
// parentsReferencingLatestValidatorBlock holds the number of parents that reference the latest validator block.
100+
parentsReferencingLatestValidatorBlock reactive.Counter[bool]
88101
}
89102

90103
// NewBlockMetadata creates a new TipMetadata instance.
91104
func NewBlockMetadata(block *blocks.Block) *TipMetadata {
92105
t := &TipMetadata{
93-
block: block,
94-
tipPool: reactive.NewVariable[tipmanager.TipPool](tipmanager.TipPool.Max),
95-
livenessThresholdReached: reactive.NewEvent(),
96-
evicted: reactive.NewEvent(),
97-
stronglyConnectedStrongChildren: reactive.NewCounter[bool](),
98-
connectedWeakChildren: reactive.NewCounter[bool](),
99-
stronglyOrphanedStrongParents: reactive.NewCounter[bool](),
100-
weaklyOrphanedWeakParents: reactive.NewCounter[bool](),
106+
block: block,
107+
tipPool: reactive.NewVariable[tipmanager.TipPool](tipmanager.TipPool.Max),
108+
livenessThresholdReached: reactive.NewEvent(),
109+
evicted: reactive.NewEvent(),
110+
isLatestValidatorBlock: reactive.NewVariable[bool](),
111+
stronglyConnectedStrongChildren: reactive.NewCounter[bool](),
112+
connectedWeakChildren: reactive.NewCounter[bool](),
113+
stronglyOrphanedStrongParents: reactive.NewCounter[bool](),
114+
weaklyOrphanedWeakParents: reactive.NewCounter[bool](),
115+
parentsReferencingLatestValidatorBlock: reactive.NewCounter[bool](),
101116
}
102117

118+
t.referencesLatestValidatorBlock = reactive.NewDerivedVariable2(func(_ bool, isLatestValidatorBlock bool, parentsReferencingLatestValidatorBlock int) bool {
119+
return isLatestValidatorBlock || parentsReferencingLatestValidatorBlock > 0
120+
}, t.isLatestValidatorBlock, t.parentsReferencingLatestValidatorBlock)
121+
103122
t.isMarkedOrphaned = reactive.NewDerivedVariable2[bool, bool](func(_ bool, isLivenessThresholdReached bool, isAccepted bool) bool {
104123
return isLivenessThresholdReached && !isAccepted
105124
}, t.livenessThresholdReached, block.Accepted())
@@ -160,6 +179,10 @@ func NewBlockMetadata(block *blocks.Block) *TipMetadata {
160179
return isWeakTipPoolMember && !isReferencedByTips
161180
}, t.isWeakTipPoolMember, t.isReferencedByTips)
162181

182+
t.isValidatorTip = reactive.NewDerivedVariable2(func(_ bool, isStrongTip bool, referencesLatestValidatorBlock bool) bool {
183+
return isStrongTip && referencesLatestValidatorBlock
184+
}, t.isStrongTip, t.referencesLatestValidatorBlock)
185+
163186
return t
164187
}
165188

@@ -206,6 +229,7 @@ func (t *TipMetadata) Evicted() reactive.Event {
206229
// connectStrongParent sets up the parent and children related properties for a strong parent.
207230
func (t *TipMetadata) connectStrongParent(strongParent *TipMetadata) {
208231
t.stronglyOrphanedStrongParents.Monitor(strongParent.isStronglyOrphaned)
232+
t.parentsReferencingLatestValidatorBlock.Monitor(strongParent.referencesLatestValidatorBlock)
209233

210234
// unsubscribe when the parent is evicted, since we otherwise continue to hold a reference to it.
211235
unsubscribe := strongParent.stronglyConnectedStrongChildren.Monitor(t.isStronglyConnectedToTips)

Diff for: pkg/protocol/engine/tipmanager/v1/tipmanager.go

+11
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ type TipManager struct {
2222
// tipMetadataStorage contains the TipMetadata of all Blocks that are managed by the TipManager.
2323
tipMetadataStorage *shrinkingmap.ShrinkingMap[iotago.SlotIndex, *shrinkingmap.ShrinkingMap[iotago.BlockID, *TipMetadata]]
2424

25+
// validatorTipSet contains the subset of blocks from the strong tip set that reference the latest validator block.
26+
validatorTipSet *randommap.RandomMap[iotago.BlockID, *TipMetadata]
27+
2528
// strongTipSet contains the blocks of the strong tip pool that have no referencing children.
2629
strongTipSet *randommap.RandomMap[iotago.BlockID, *TipMetadata]
2730

@@ -123,6 +126,14 @@ func (t *TipManager) Shutdown() {
123126

124127
// setupBlockMetadata sets up the behavior of the given Block.
125128
func (t *TipManager) setupBlockMetadata(tipMetadata *TipMetadata) {
129+
tipMetadata.isValidatorTip.OnUpdate(func(_ bool, isValidatorTip bool) {
130+
if isValidatorTip {
131+
t.validatorTipSet.Set(tipMetadata.ID(), tipMetadata)
132+
} else {
133+
t.validatorTipSet.Delete(tipMetadata.ID())
134+
}
135+
})
136+
126137
tipMetadata.isStrongTip.OnUpdate(func(_ bool, isStrongTip bool) {
127138
if isStrongTip {
128139
t.strongTipSet.Set(tipMetadata.ID(), tipMetadata)

0 commit comments

Comments
 (0)