@@ -125,6 +125,8 @@ func (t *TipSelection) SelectTips(amount int) (references model.ParentReferences
125
125
126
126
previousLikedInsteadConflicts = updatedLikedInsteadConflicts
127
127
}
128
+ }, func () int {
129
+ return len (references [iotago .StrongParentType ])
128
130
},
129
131
// We select one validation tip as a strong parent. This is a security step to ensure that the tangle maintains
130
132
// acceptance by stitching together validation blocks.
@@ -140,6 +142,8 @@ func (t *TipSelection) SelectTips(amount int) (references model.ParentReferences
140
142
} else if ! shallowLikesParents .Has (tip .ID ()) {
141
143
references [iotago .WeakParentType ] = append (references [iotago .WeakParentType ], tip .ID ())
142
144
}
145
+ }, func () int {
146
+ return len (references [iotago .WeakParentType ])
143
147
}, types .NewTuple [func (optAmount ... int ) []tipmanager.TipMetadata , int ](t .tipManager .WeakTips , t .optMaxWeakReferences ))
144
148
145
149
return nil
@@ -216,17 +220,17 @@ func (t *TipSelection) likedInsteadReferences(likedConflicts ds.Set[iotago.Trans
216
220
217
221
// collectReferences collects tips from a tip selector (and calls the callback for each tip) until the number of
218
222
// references of the given type is reached.
219
- func (t * TipSelection ) collectReferences (callback func (tipmanager.TipMetadata ), tipSelectorsAmount ... * types.Tuple [func (optAmount ... int ) []tipmanager.TipMetadata , int ]) {
223
+ func (t * TipSelection ) collectReferences (callback func (tipmanager.TipMetadata ), referencesCountCallback func () int , tipSelectorsAmount ... * types.Tuple [func (optAmount ... int ) []tipmanager.TipMetadata , int ]) {
220
224
seenTips := ds .NewSet [iotago.BlockID ]()
221
225
222
226
// selectUniqueTips selects 'amount' unique tips from the given tip selector.
223
- selectUniqueTips := func (tipSelector func (optAmount ... int ) []tipmanager.TipMetadata , amount int ) (uniqueTips []tipmanager.TipMetadata ) {
224
- if amount > 0 {
225
- for _ , tip := range tipSelector (amount + seenTips .Size ()) {
227
+ selectUniqueTips := func (tipSelector func (optAmount ... int ) []tipmanager.TipMetadata , currentReferencesCount int , targetAmount int ) (uniqueTips []tipmanager.TipMetadata ) {
228
+ if targetAmount > 0 {
229
+ for _ , tip := range tipSelector (targetAmount + seenTips .Size ()) {
226
230
if seenTips .Add (tip .ID ()) {
227
231
uniqueTips = append (uniqueTips , tip )
228
232
229
- if len (uniqueTips ) == amount {
233
+ if currentReferencesCount + len (uniqueTips ) == targetAmount {
230
234
break
231
235
}
232
236
}
@@ -238,15 +242,27 @@ func (t *TipSelection) collectReferences(callback func(tipmanager.TipMetadata),
238
242
239
243
accumulatedTipAmount := 0
240
244
// We select the desired number of tips from all given tip selectors, respectively.
241
- for _ , tipSelectorAmount := range tipSelectorsAmount {
245
+ for i , tipSelectorAmount := range tipSelectorsAmount {
242
246
// Make sure we select the total number of unique tips and not just the number of tips from the given tip pool,
243
247
// because of how selectUniqueTips works.
244
248
accumulatedTipAmount += tipSelectorAmount .B
245
249
246
- for tipCandidates := selectUniqueTips (tipSelectorAmount .A , accumulatedTipAmount ); len (tipCandidates ) != 0 ; tipCandidates = selectUniqueTips (tipSelectorAmount .A , accumulatedTipAmount ) {
250
+ tipCandidates := selectUniqueTips (tipSelectorAmount .A , referencesCountCallback (), accumulatedTipAmount )
251
+
252
+ // We exit the loop in two cases:
253
+ // 1. When we've seen all the tips and there are no more unique tips to process (len(tipCandidates) != 0).
254
+ // 2. When we've successfully selected the desired number of tips and added them to the references (referencesCountCallback() >= accumulatedTipAmount).
255
+ for len (tipCandidates ) != 0 {
247
256
for _ , tip := range tipCandidates {
248
257
callback (tip )
249
258
}
259
+
260
+ referencesCount := referencesCountCallback ()
261
+ if referencesCount >= accumulatedTipAmount {
262
+ break
263
+ }
264
+
265
+ tipCandidates = selectUniqueTips (tipSelectorAmount .A , referencesCount , accumulatedTipAmount )
250
266
}
251
267
}
252
268
}
0 commit comments