diff --git a/pkg/core/acceptance/state.go b/pkg/core/acceptance/state.go index f87fd3381..9bb7d0c2f 100644 --- a/pkg/core/acceptance/state.go +++ b/pkg/core/acceptance/state.go @@ -5,13 +5,13 @@ import ( ) const ( - // Pending is the state of pending conflicts. + // Pending is the state of pending spends. Pending State = iota - // Accepted is the state of accepted conflicts. + // Accepted is the state of accepted spends. Accepted - // Rejected is the state of rejected conflicts. + // Rejected is the state of rejected spends. Rejected ) diff --git a/pkg/protocol/engine/booker/inmemorybooker/booker.go b/pkg/protocol/engine/booker/inmemorybooker/booker.go index 562f50e59..1086e5815 100644 --- a/pkg/protocol/engine/booker/inmemorybooker/booker.go +++ b/pkg/protocol/engine/booker/inmemorybooker/booker.go @@ -157,10 +157,10 @@ func (b *Booker) setRetainBlockFailureFunc(retainBlockFailure func(iotago.BlockI func (b *Booker) book(block *blocks.Block) error { spendsToInherit, err := b.inheritSpends(block) if err != nil { - return ierrors.Wrapf(err, "failed to inherit conflicts for block %s", block.ID()) + return ierrors.Wrapf(err, "failed to inherit spends for block %s", block.ID()) } - // The block does not inherit conflicts that have been orphaned with respect to its commitment. + // The block does not inherit spends that have been orphaned with respect to its commitment. for it := spendsToInherit.Iterator(); it.HasNext(); { spendID := it.Next() @@ -170,7 +170,7 @@ func (b *Booker) book(block *blocks.Block) error { } if orphanedSlot, orphaned := txMetadata.OrphanedSlot(); orphaned && orphanedSlot <= block.SlotCommitmentID().Slot() { - // Merge-to-master orphaned conflicts. + // Merge-to-master orphaned spends. spendsToInherit.Delete(spendID) } } @@ -206,19 +206,19 @@ func (b *Booker) inheritSpends(block *blocks.Block) (spendIDs ds.Set[iotago.Tran } spendIDsToInherit.AddAll(parentBlock.PayloadSpendIDs()) - // remove all conflicting conflicts from spendIDsToInherit + // remove all conflicting spends from spendIDsToInherit for _, spendID := range parentBlock.PayloadSpendIDs().ToSlice() { - if conflictingConflicts, exists := b.spendDAG.ConflictingSpends(spendID); exists { - spendIDsToInherit.DeleteAll(b.spendDAG.FutureCone(conflictingConflicts)) + if conflictingSpends, exists := b.spendDAG.ConflictingSpends(spendID); exists { + spendIDsToInherit.DeleteAll(b.spendDAG.FutureCone(conflictingSpends)) } } } } - // Add all conflicts from the block's payload itself. + // Add all spends from the block's payload itself. // Forking on booking: we determine the block's PayloadSpendIDs by treating each TX as a conflict. spendIDsToInherit.AddAll(block.PayloadSpendIDs()) - // Only inherit conflicts that are not yet accepted (aka merge to master). + // Only inherit spends that are not yet accepted (aka merge to master). return b.spendDAG.UnacceptedSpends(spendIDsToInherit), nil } diff --git a/pkg/protocol/engine/mempool/spenddag/constraints.go b/pkg/protocol/engine/mempool/spenddag/constraints.go index 2c5503c79..f1543f636 100644 --- a/pkg/protocol/engine/mempool/spenddag/constraints.go +++ b/pkg/protocol/engine/mempool/spenddag/constraints.go @@ -4,7 +4,7 @@ import ( "github.com/iotaledger/hive.go/constraints" ) -// IDType is the constraint for the identifier of a conflict or a resource. +// IDType is the constraint for the identifier of a spend or a resource. type IDType interface { // comparable is a built-in constraint that ensures that the type can be used as a map key. comparable diff --git a/pkg/protocol/engine/mempool/spenddag/errors.go b/pkg/protocol/engine/mempool/spenddag/errors.go index d2574864a..beb901f92 100644 --- a/pkg/protocol/engine/mempool/spenddag/errors.go +++ b/pkg/protocol/engine/mempool/spenddag/errors.go @@ -4,7 +4,7 @@ import "github.com/iotaledger/hive.go/ierrors" var ( ErrExpected = ierrors.New("expected error") - ErrAlreadyPartOfConflictSet = ierrors.New("conflict already part of ConflictSet") + ErrAlreadyPartOfConflictSet = ierrors.New("spend already part of ConflictSet") ErrEntityEvicted = ierrors.New("tried to operate on evicted entity") ErrFatal = ierrors.New("fatal error") ) diff --git a/pkg/protocol/engine/mempool/spenddag/spenddag.go b/pkg/protocol/engine/mempool/spenddag/spenddag.go index a0b697801..74d0eda1a 100644 --- a/pkg/protocol/engine/mempool/spenddag/spenddag.go +++ b/pkg/protocol/engine/mempool/spenddag/spenddag.go @@ -37,7 +37,7 @@ type SpendDAG[SpendID, ResourceID IDType, VoteRank VoteRankType[VoteRank]] inter type ReadLockedSpendDAG[SpendID, ResourceID IDType, VoteRank VoteRankType[VoteRank]] interface { LikedInstead(spendIDs ds.Set[SpendID]) ds.Set[SpendID] FutureCone(spendIDs ds.Set[SpendID]) (futureCone ds.Set[SpendID]) - ConflictingSpends(spendID SpendID) (conflictingConflicts ds.Set[SpendID], exists bool) + ConflictingSpends(spendID SpendID) (conflictingSpends ds.Set[SpendID], exists bool) AcceptanceState(spendIDs ds.Set[SpendID]) acceptance.State UnacceptedSpends(spendIDs ds.Set[SpendID]) ds.Set[SpendID] } diff --git a/pkg/protocol/engine/mempool/spenddag/spenddagv1/sorted_spends.go b/pkg/protocol/engine/mempool/spenddag/spenddagv1/sorted_spends.go index ed1b74983..359c25be8 100644 --- a/pkg/protocol/engine/mempool/spenddag/spenddagv1/sorted_spends.go +++ b/pkg/protocol/engine/mempool/spenddag/spenddagv1/sorted_spends.go @@ -80,7 +80,7 @@ func NewSortedSpends[SpendID, ResourceID spenddag.IDType, VoteRank spenddag.Vote } // Add adds the given Spend to the SortedSpends. -func (s *SortedSpends[SpendID, ResourceID, VoteRank]) Add(conflict *Spend[SpendID, ResourceID, VoteRank]) bool { +func (s *SortedSpends[SpendID, ResourceID, VoteRank]) Add(spend *Spend[SpendID, ResourceID, VoteRank]) bool { s.mutex.Lock() defer s.mutex.Unlock() @@ -88,8 +88,8 @@ func (s *SortedSpends[SpendID, ResourceID, VoteRank]) Add(conflict *Spend[SpendI return false } - newMember, isNew := s.members.GetOrCreate(conflict.ID, func() *sortedSpend[SpendID, ResourceID, VoteRank] { - return newSortedSpend(s, conflict) + newMember, isNew := s.members.GetOrCreate(spend.ID, func() *sortedSpend[SpendID, ResourceID, VoteRank] { + return newSortedSpend(s, spend) }) if !isNew { return false @@ -128,7 +128,7 @@ func (s *SortedSpends[SpendID, ResourceID, VoteRank]) Add(conflict *Spend[SpendI if newMember.IsPreferred() && newMember.Compare(s.heaviestPreferredMember) == weight.Heavier { s.heaviestPreferredMember = newMember - s.owner.setPreferredInstead(conflict) + s.owner.setPreferredInstead(spend) } return true diff --git a/pkg/protocol/engine/mempool/spenddag/spenddagv1/sorted_spends_test.go b/pkg/protocol/engine/mempool/spenddag/spenddagv1/sorted_spends_test.go index a762d422b..1bdaef281 100644 --- a/pkg/protocol/engine/mempool/spenddag/spenddagv1/sorted_spends_test.go +++ b/pkg/protocol/engine/mempool/spenddag/spenddagv1/sorted_spends_test.go @@ -18,11 +18,11 @@ import ( iotago "github.com/iotaledger/iota.go/v4" ) -type SortedConflictSet = *SortedSpends[iotago.TransactionID, iotago.OutputID, vote.MockedRank] +type SortedSpendSet = *SortedSpends[iotago.TransactionID, iotago.OutputID, vote.MockedRank] -var NewSortedConflictSet = NewSortedSpends[iotago.TransactionID, iotago.OutputID, vote.MockedRank] +var NewSortedSpendSet = NewSortedSpends[iotago.TransactionID, iotago.OutputID, vote.MockedRank] -func TestSortedConflict(t *testing.T) { +func TestSortedSpend(t *testing.T) { weights := account.NewSeatedAccounts(account.NewAccounts()) pendingTasks := syncutils.NewCounter() @@ -30,54 +30,54 @@ func TestSortedConflict(t *testing.T) { return int64(weights.SeatCount()) }) - conflict1 := NewTestSpend(transactionID("conflict1"), nil, nil, weight.New().AddCumulativeWeight(12), pendingTasks, thresholdProvider) - conflict1.setAcceptanceState(acceptance.Rejected) - conflict2 := NewTestSpend(transactionID("conflict2"), nil, nil, weight.New().AddCumulativeWeight(10), pendingTasks, thresholdProvider) - conflict3 := NewTestSpend(transactionID("conflict3"), nil, nil, weight.New().AddCumulativeWeight(1), pendingTasks, thresholdProvider) - conflict3.setAcceptanceState(acceptance.Accepted) - conflict4 := NewTestSpend(transactionID("conflict4"), nil, nil, weight.New().AddCumulativeWeight(11), pendingTasks, thresholdProvider) - conflict4.setAcceptanceState(acceptance.Rejected) - conflict5 := NewTestSpend(transactionID("conflict5"), nil, nil, weight.New().AddCumulativeWeight(11), pendingTasks, thresholdProvider) - conflict6 := NewTestSpend(transactionID("conflict6"), nil, nil, weight.New().AddCumulativeWeight(2), pendingTasks, thresholdProvider) - conflict6.setAcceptanceState(acceptance.Accepted) - - sortedConflicts := NewSortedConflictSet(conflict1, pendingTasks) + spend1 := NewTestSpend(transactionID("spend1"), nil, nil, weight.New().AddCumulativeWeight(12), pendingTasks, thresholdProvider) + spend1.setAcceptanceState(acceptance.Rejected) + spend2 := NewTestSpend(transactionID("spend2"), nil, nil, weight.New().AddCumulativeWeight(10), pendingTasks, thresholdProvider) + spend3 := NewTestSpend(transactionID("spend3"), nil, nil, weight.New().AddCumulativeWeight(1), pendingTasks, thresholdProvider) + spend3.setAcceptanceState(acceptance.Accepted) + spend4 := NewTestSpend(transactionID("spend4"), nil, nil, weight.New().AddCumulativeWeight(11), pendingTasks, thresholdProvider) + spend4.setAcceptanceState(acceptance.Rejected) + spend5 := NewTestSpend(transactionID("spend5"), nil, nil, weight.New().AddCumulativeWeight(11), pendingTasks, thresholdProvider) + spend6 := NewTestSpend(transactionID("spend6"), nil, nil, weight.New().AddCumulativeWeight(2), pendingTasks, thresholdProvider) + spend6.setAcceptanceState(acceptance.Accepted) + + sortedSpends := NewSortedSpendSet(spend1, pendingTasks) pendingTasks.WaitIsZero() - assertSortedConflictsOrder(t, sortedConflicts, "conflict1:0") + assertSortedSpendsOrder(t, sortedSpends, "spend1:0") - sortedConflicts.Add(conflict2) + sortedSpends.Add(spend2) pendingTasks.WaitIsZero() - assertSortedConflictsOrder(t, sortedConflicts, "conflict2:0", "conflict1:0") + assertSortedSpendsOrder(t, sortedSpends, "spend2:0", "spend1:0") - sortedConflicts.Add(conflict3) + sortedSpends.Add(spend3) pendingTasks.WaitIsZero() - assertSortedConflictsOrder(t, sortedConflicts, "conflict3:0", "conflict2:0", "conflict1:0") + assertSortedSpendsOrder(t, sortedSpends, "spend3:0", "spend2:0", "spend1:0") - sortedConflicts.Add(conflict4) + sortedSpends.Add(spend4) pendingTasks.WaitIsZero() - assertSortedConflictsOrder(t, sortedConflicts, "conflict3:0", "conflict2:0", "conflict1:0", "conflict4:0") + assertSortedSpendsOrder(t, sortedSpends, "spend3:0", "spend2:0", "spend1:0", "spend4:0") - sortedConflicts.Add(conflict5) + sortedSpends.Add(spend5) pendingTasks.WaitIsZero() - assertSortedConflictsOrder(t, sortedConflicts, "conflict3:0", "conflict5:0", "conflict2:0", "conflict1:0", "conflict4:0") + assertSortedSpendsOrder(t, sortedSpends, "spend3:0", "spend5:0", "spend2:0", "spend1:0", "spend4:0") - sortedConflicts.Add(conflict6) + sortedSpends.Add(spend6) pendingTasks.WaitIsZero() - assertSortedConflictsOrder(t, sortedConflicts, "conflict6:0", "conflict3:0", "conflict5:0", "conflict2:0", "conflict1:0", "conflict4:0") + assertSortedSpendsOrder(t, sortedSpends, "spend6:0", "spend3:0", "spend5:0", "spend2:0", "spend1:0", "spend4:0") - conflict2.Weight.AddCumulativeWeight(3) - require.Equal(t, int64(13), conflict2.Weight.Value().CumulativeWeight()) + spend2.Weight.AddCumulativeWeight(3) + require.Equal(t, int64(13), spend2.Weight.Value().CumulativeWeight()) pendingTasks.WaitIsZero() - assertSortedConflictsOrder(t, sortedConflicts, "conflict6:0", "conflict3:0", "conflict2:0", "conflict5:0", "conflict1:0", "conflict4:0") + assertSortedSpendsOrder(t, sortedSpends, "spend6:0", "spend3:0", "spend2:0", "spend5:0", "spend1:0", "spend4:0") - conflict2.Weight.RemoveCumulativeWeight(3) - require.Equal(t, int64(10), conflict2.Weight.Value().CumulativeWeight()) + spend2.Weight.RemoveCumulativeWeight(3) + require.Equal(t, int64(10), spend2.Weight.Value().CumulativeWeight()) pendingTasks.WaitIsZero() - assertSortedConflictsOrder(t, sortedConflicts, "conflict6:0", "conflict3:0", "conflict5:0", "conflict2:0", "conflict1:0", "conflict4:0") + assertSortedSpendsOrder(t, sortedSpends, "spend6:0", "spend3:0", "spend5:0", "spend2:0", "spend1:0", "spend4:0") - conflict5.Weight.SetAcceptanceState(acceptance.Accepted) + spend5.Weight.SetAcceptanceState(acceptance.Accepted) pendingTasks.WaitIsZero() - assertSortedConflictsOrder(t, sortedConflicts, "conflict5:0", "conflict6:0", "conflict3:0", "conflict2:0", "conflict1:0", "conflict4:0") + assertSortedSpendsOrder(t, sortedSpends, "spend5:0", "spend6:0", "spend3:0", "spend2:0", "spend1:0", "spend4:0") } func TestSortedDecreaseHeaviest(t *testing.T) { @@ -88,26 +88,26 @@ func TestSortedDecreaseHeaviest(t *testing.T) { return int64(weights.SeatCount()) }) - conflict1 := NewTestSpend(transactionID("conflict1"), nil, nil, weight.New().AddCumulativeWeight(1), pendingTasks, thresholdProvider) - conflict1.setAcceptanceState(acceptance.Accepted) - conflict2 := NewTestSpend(transactionID("conflict2"), nil, nil, weight.New().AddCumulativeWeight(2), pendingTasks, thresholdProvider) + spend1 := NewTestSpend(transactionID("spend1"), nil, nil, weight.New().AddCumulativeWeight(1), pendingTasks, thresholdProvider) + spend1.setAcceptanceState(acceptance.Accepted) + spend2 := NewTestSpend(transactionID("spend2"), nil, nil, weight.New().AddCumulativeWeight(2), pendingTasks, thresholdProvider) - sortedConflicts := NewSortedConflictSet(conflict1, pendingTasks) + sortedSpends := NewSortedSpendSet(spend1, pendingTasks) - sortedConflicts.Add(conflict1) + sortedSpends.Add(spend1) pendingTasks.WaitIsZero() - assertSortedConflictsOrder(t, sortedConflicts, "conflict1:0") + assertSortedSpendsOrder(t, sortedSpends, "spend1:0") - sortedConflicts.Add(conflict2) + sortedSpends.Add(spend2) pendingTasks.WaitIsZero() - assertSortedConflictsOrder(t, sortedConflicts, "conflict1:0", "conflict2:0") + assertSortedSpendsOrder(t, sortedSpends, "spend1:0", "spend2:0") - conflict1.Weight.SetAcceptanceState(acceptance.Pending) + spend1.Weight.SetAcceptanceState(acceptance.Pending) pendingTasks.WaitIsZero() - assertSortedConflictsOrder(t, sortedConflicts, "conflict2:0", "conflict1:0") + assertSortedSpendsOrder(t, sortedSpends, "spend2:0", "spend1:0") } -func TestSortedConflictParallel(t *testing.T) { +func TestSortedSpendParallel(t *testing.T) { weights := account.NewSeatedAccounts(account.NewAccounts()) pendingTasks := syncutils.NewCounter() @@ -115,48 +115,48 @@ func TestSortedConflictParallel(t *testing.T) { return int64(weights.SeatCount()) }) - const conflictCount = 1000 + const spendCount = 1000 const updateCount = 100000 - conflicts := make(map[string]TestSpend) - parallelConflicts := make(map[string]TestSpend) - for i := 0; i < conflictCount; i++ { - alias := "conflict" + strconv.Itoa(i) + spends := make(map[string]TestSpend) + parallelSpends := make(map[string]TestSpend) + for i := 0; i < spendCount; i++ { + alias := "spend" + strconv.Itoa(i) - conflicts[alias] = NewTestSpend(transactionID(alias), nil, nil, weight.New(), pendingTasks, thresholdProvider) - parallelConflicts[alias] = NewTestSpend(transactionID(alias), nil, nil, weight.New(), pendingTasks, thresholdProvider) + spends[alias] = NewTestSpend(transactionID(alias), nil, nil, weight.New(), pendingTasks, thresholdProvider) + parallelSpends[alias] = NewTestSpend(transactionID(alias), nil, nil, weight.New(), pendingTasks, thresholdProvider) } - sortedConflicts := NewSortedConflictSet(conflicts["conflict0"], pendingTasks) - sortedParallelConflicts := NewSortedConflictSet(parallelConflicts["conflict0"], pendingTasks) - sortedParallelConflicts1 := NewSortedConflictSet(parallelConflicts["conflict0"], pendingTasks) + sortedSpends := NewSortedSpendSet(spends["spend0"], pendingTasks) + sortedParallelSpends := NewSortedSpendSet(parallelSpends["spend0"], pendingTasks) + sortedParallelSpends1 := NewSortedSpendSet(parallelSpends["spend0"], pendingTasks) - for i := 0; i < conflictCount; i++ { - alias := "conflict" + strconv.Itoa(i) + for i := 0; i < spendCount; i++ { + alias := "spend" + strconv.Itoa(i) - sortedConflicts.Add(conflicts[alias]) - sortedParallelConflicts.Add(parallelConflicts[alias]) - sortedParallelConflicts1.Add(parallelConflicts[alias]) + sortedSpends.Add(spends[alias]) + sortedParallelSpends.Add(parallelSpends[alias]) + sortedParallelSpends1.Add(parallelSpends[alias]) } - originalSortingBefore := sortedConflicts.String() - parallelSortingBefore := sortedParallelConflicts.String() + originalSortingBefore := sortedSpends.String() + parallelSortingBefore := sortedParallelSpends.String() require.Equal(t, originalSortingBefore, parallelSortingBefore) - permutations := make([]func(conflict TestSpend), 0) + permutations := make([]func(spend TestSpend), 0) for i := 0; i < updateCount; i++ { permutations = append(permutations, generateRandomWeightPermutation()) } var wg sync.WaitGroup for i, permutation := range permutations { - targetAlias := "conflict" + strconv.Itoa(i%conflictCount) + targetAlias := "spend" + strconv.Itoa(i%spendCount) - permutation(conflicts[targetAlias]) + permutation(spends[targetAlias]) wg.Add(1) - go func(permutation func(conflict TestSpend)) { - permutation(parallelConflicts[targetAlias]) + go func(permutation func(spend TestSpend)) { + permutation(parallelSpends[targetAlias]) wg.Done() }(permutation) @@ -166,45 +166,45 @@ func TestSortedConflictParallel(t *testing.T) { wg.Wait() pendingTasks.WaitIsZero() - originalSortingAfter := sortedConflicts.String() - parallelSortingAfter := sortedParallelConflicts.String() + originalSortingAfter := sortedSpends.String() + parallelSortingAfter := sortedParallelSpends.String() require.Equal(t, originalSortingAfter, parallelSortingAfter) require.NotEqualf(t, originalSortingBefore, originalSortingAfter, "original sorting should have changed") pendingTasks.WaitIsZero() - parallelSortingAfter = sortedParallelConflicts1.String() + parallelSortingAfter = sortedParallelSpends1.String() require.Equal(t, originalSortingAfter, parallelSortingAfter) require.NotEqualf(t, originalSortingBefore, originalSortingAfter, "original sorting should have changed") } -func generateRandomWeightPermutation() func(conflict TestSpend) { +func generateRandomWeightPermutation() func(spend TestSpend) { switch rand.Intn(2) { case 0: return generateRandomCumulativeWeightPermutation(int64(rand.Intn(100))) default: // return generateRandomConfirmationStatePermutation() - return func(conflict TestSpend) { + return func(spend TestSpend) { } } } -func generateRandomCumulativeWeightPermutation(delta int64) func(conflict TestSpend) { +func generateRandomCumulativeWeightPermutation(delta int64) func(spend TestSpend) { updateType := rand.Intn(100) - return func(conflict TestSpend) { + return func(spend TestSpend) { if updateType%2 == 0 { - conflict.Weight.AddCumulativeWeight(delta) + spend.Weight.AddCumulativeWeight(delta) } else { - conflict.Weight.RemoveCumulativeWeight(delta) + spend.Weight.RemoveCumulativeWeight(delta) } - conflict.Weight.AddCumulativeWeight(delta) + spend.Weight.AddCumulativeWeight(delta) } } -func assertSortedConflictsOrder(t *testing.T, sortedConflicts SortedConflictSet, aliases ...string) { - require.NoError(t, sortedConflicts.ForEach(func(c TestSpend) error { +func assertSortedSpendsOrder(t *testing.T, sortedSpends SortedSpendSet, aliases ...string) { + require.NoError(t, sortedSpends.ForEach(func(c TestSpend) error { currentAlias := aliases[0] aliases = aliases[1:] diff --git a/pkg/protocol/engine/mempool/spenddag/spenddagv1/spend.go b/pkg/protocol/engine/mempool/spenddag/spenddagv1/spend.go index 637a587b8..b14ed0d8b 100644 --- a/pkg/protocol/engine/mempool/spenddag/spenddagv1/spend.go +++ b/pkg/protocol/engine/mempool/spenddag/spenddagv1/spend.go @@ -19,67 +19,67 @@ import ( "github.com/iotaledger/iota-core/pkg/protocol/engine/mempool/spenddag" ) -// Spend is a conflict that is part of a Spend DAG. +// Spend is a spend that is part of a Spend DAG. type Spend[SpendID, ResourceID spenddag.IDType, VoteRank spenddag.VoteRankType[VoteRank]] struct { - // ID is the identifier of the Conflict. + // ID is the identifier of the Spend. ID SpendID - // Parents is the set of parents of the Conflict. + // Parents is the set of parents of the Spend. Parents ds.Set[*Spend[SpendID, ResourceID, VoteRank]] - // Children is the set of children of the Conflict. + // Children is the set of children of the Spend. Children ds.Set[*Spend[SpendID, ResourceID, VoteRank]] - // ConflictSets is the set of ConflictSets that the Conflict is part of. + // ConflictSets is the set of ConflictSets that the Spend is part of. ConflictSets ds.Set[*ConflictSet[SpendID, ResourceID, VoteRank]] - // ConflictingSpends is the set of conflicts that directly conflict with the Conflict. + // ConflictingSpends is the set of spends that directly conflict with the Spend. ConflictingSpends *SortedSpends[SpendID, ResourceID, VoteRank] - // Weight is the Weight of the Conflict. + // Weight is the Weight of the Spend. Weight *weight.Weight - // LatestVotes is the set of the latest votes of the Conflict. + // LatestVotes is the set of the latest votes of the Spend. LatestVotes *shrinkingmap.ShrinkingMap[account.SeatIndex, *vote.Vote[VoteRank]] - // AcceptanceStateUpdated is triggered when the AcceptanceState of the Conflict is updated. + // AcceptanceStateUpdated is triggered when the AcceptanceState of the Spend is updated. AcceptanceStateUpdated *event.Event2[acceptance.State, acceptance.State] - // PreferredInsteadUpdated is triggered when the preferred instead value of the Conflict is updated. + // PreferredInsteadUpdated is triggered when the preferred instead value of the Spend is updated. PreferredInsteadUpdated *event.Event1[*Spend[SpendID, ResourceID, VoteRank]] - // LikedInsteadAdded is triggered when a liked instead reference is added to the Conflict. + // LikedInsteadAdded is triggered when a liked instead reference is added to the Spend. LikedInsteadAdded *event.Event1[*Spend[SpendID, ResourceID, VoteRank]] - // LikedInsteadRemoved is triggered when a liked instead reference is removed from the Conflict. + // LikedInsteadRemoved is triggered when a liked instead reference is removed from the Spend. LikedInsteadRemoved *event.Event1[*Spend[SpendID, ResourceID, VoteRank]] // childUnhookMethods is a mapping of children to their unhook functions. childUnhookMethods *shrinkingmap.ShrinkingMap[SpendID, func()] - // preferredInstead is the preferred instead value of the Conflict. + // preferredInstead is the preferred instead value of the Spend. preferredInstead *Spend[SpendID, ResourceID, VoteRank] // evicted evicted atomic.Bool - // preferredInsteadMutex is used to synchronize access to the preferred instead value of the Conflict. + // preferredInsteadMutex is used to synchronize access to the preferred instead value of the Spend. preferredInsteadMutex syncutils.RWMutex - // likedInstead is the set of liked instead Conflicts. + // likedInstead is the set of liked instead Spends. likedInstead ds.Set[*Spend[SpendID, ResourceID, VoteRank]] - // likedInsteadSources is a mapping of liked instead Conflicts to the set of parents that inherited them. + // likedInsteadSources is a mapping of liked instead Spends to the set of parents that inherited them. likedInsteadSources *shrinkingmap.ShrinkingMap[SpendID, ds.Set[*Spend[SpendID, ResourceID, VoteRank]]] // likedInsteadMutex and structureMutex are sometimes locked in different order by different goroutines, which could result in a deadlock // however, it's impossible to deadlock if we fork all transactions upon booking // deadlock happens when the likedInstead conflict changes and parents are updated at the same time, which is impossible in the current setup // because we won't process votes on a conflict we're just creating. - // likedInsteadMutex is used to synchronize access to the liked instead value of the Conflict. + // likedInsteadMutex is used to synchronize access to the liked instead value of the Spend. likedInsteadMutex syncutils.RWMutex - // structureMutex is used to synchronize access to the structure of the Conflict. + // structureMutex is used to synchronize access to the structure of the Spend. structureMutex syncutils.RWMutex // acceptanceThreshold is the function that is used to retrieve the acceptance threshold of the committee. @@ -127,14 +127,14 @@ func NewSpend[SpendID, ResourceID spenddag.IDType, VoteRank spenddag.VoteRankTyp return c } -// JoinConflictSets registers the Conflict with the given ConflictSets. +// JoinConflictSets registers the Spend with the given ConflictSets. func (c *Spend[SpendID, ResourceID, VoteRank]) JoinSpendSets(conflictSets ds.Set[*ConflictSet[SpendID, ResourceID, VoteRank]]) (joinedConflictSets ds.Set[ResourceID], err error) { if conflictSets == nil { return ds.NewSet[ResourceID](), nil } if c.evicted.Load() { - return nil, ierrors.Errorf("tried to join conflict sets of evicted conflict: %w", spenddag.ErrEntityEvicted) + return nil, ierrors.Errorf("tried to join conflict sets of evicted spend: %w", spenddag.ErrEntityEvicted) } registerConflictingSpend := func(c *Spend[SpendID, ResourceID, VoteRank], spend *Spend[SpendID, ResourceID, VoteRank]) { @@ -179,7 +179,7 @@ func (c *Spend[SpendID, ResourceID, VoteRank]) removeParent(parent *Spend[SpendI return removed } -// UpdateParents updates the parents of the Conflict. +// UpdateParents updates the parents of the Spend. func (c *Spend[SpendID, ResourceID, VoteRank]) UpdateParents(addedParents ds.Set[*Spend[SpendID, ResourceID, VoteRank]], removedParents ds.Set[*Spend[SpendID, ResourceID, VoteRank]]) (updated bool) { c.structureMutex.Lock() defer c.structureMutex.Unlock() @@ -204,7 +204,7 @@ func (c *Spend[SpendID, ResourceID, VoteRank]) UpdateParents(addedParents ds.Set } func (c *Spend[SpendID, ResourceID, VoteRank]) ApplyVote(vote *vote.Vote[VoteRank]) { - // abort if the conflict has already been accepted or rejected + // abort if the spend has already been accepted or rejected if !c.Weight.AcceptanceState().IsPending() { return } @@ -230,22 +230,22 @@ func (c *Spend[SpendID, ResourceID, VoteRank]) ApplyVote(vote *vote.Vote[VoteRan } } -// IsPending returns true if the Conflict is pending. +// IsPending returns true if the Spend is pending. func (c *Spend[SpendID, ResourceID, VoteRank]) IsPending() bool { return c.Weight.Value().AcceptanceState().IsPending() } -// IsAccepted returns true if the Conflict is accepted. +// IsAccepted returns true if the Spend is accepted. func (c *Spend[SpendID, ResourceID, VoteRank]) IsAccepted() bool { return c.Weight.Value().AcceptanceState().IsAccepted() } -// IsRejected returns true if the Conflict is rejected. +// IsRejected returns true if the Spend is rejected. func (c *Spend[SpendID, ResourceID, VoteRank]) IsRejected() bool { return c.Weight.Value().AcceptanceState().IsRejected() } -// IsPreferred returns true if the Conflict is preferred instead of its conflicting Conflicts. +// IsPreferred returns true if the Spend is preferred instead of its conflicting Spend. func (c *Spend[SpendID, ResourceID, VoteRank]) IsPreferred() bool { c.preferredInsteadMutex.RLock() defer c.preferredInsteadMutex.RUnlock() @@ -253,7 +253,7 @@ func (c *Spend[SpendID, ResourceID, VoteRank]) IsPreferred() bool { return c.preferredInstead == c } -// PreferredInstead returns the preferred instead value of the Conflict. +// PreferredInstead returns the preferred instead value of the Spend. func (c *Spend[SpendID, ResourceID, VoteRank]) PreferredInstead() *Spend[SpendID, ResourceID, VoteRank] { c.preferredInsteadMutex.RLock() defer c.preferredInsteadMutex.RUnlock() @@ -261,7 +261,7 @@ func (c *Spend[SpendID, ResourceID, VoteRank]) PreferredInstead() *Spend[SpendID return c.preferredInstead } -// IsLiked returns true if the Conflict is liked instead of other conflicting Conflicts. +// IsLiked returns true if the Spend is liked instead of other conflicting Spends. func (c *Spend[SpendID, ResourceID, VoteRank]) IsLiked() bool { c.likedInsteadMutex.RLock() defer c.likedInsteadMutex.RUnlock() @@ -269,7 +269,7 @@ func (c *Spend[SpendID, ResourceID, VoteRank]) IsLiked() bool { return c.IsPreferred() && c.likedInstead.IsEmpty() } -// LikedInstead returns the set of liked instead Conflicts. +// LikedInstead returns the set of liked instead Spends. func (c *Spend[SpendID, ResourceID, VoteRank]) LikedInstead() ds.Set[*Spend[SpendID, ResourceID, VoteRank]] { c.likedInsteadMutex.RLock() defer c.likedInsteadMutex.RUnlock() @@ -277,7 +277,7 @@ func (c *Spend[SpendID, ResourceID, VoteRank]) LikedInstead() ds.Set[*Spend[Spen return c.likedInstead.Clone() } -// Shutdown shuts down the Conflict. +// Shutdown shuts down the Spend. func (c *Spend[SpendID, ResourceID, VoteRank]) Shutdown() { c.ConflictingSpends.Shutdown() } @@ -292,12 +292,12 @@ func (c *Spend[SpendID, ResourceID, VoteRank]) Evict() (evictedConflicts []Spend switch c.Weight.AcceptanceState() { case acceptance.Rejected: - // evict the entire future cone of rejected conflicts + // evict the entire future cone of rejected spends c.Children.Range(func(childConflict *Spend[SpendID, ResourceID, VoteRank]) { evictedConflicts = append(evictedConflicts, childConflict.Evict()...) }) default: - // remove evicted conflict from parents of children (merge to master) + // remove evicted spend from parents of children (merge to master) c.Children.Range(func(childConflict *Spend[SpendID, ResourceID, VoteRank]) { childConflict.structureMutex.Lock() defer childConflict.structureMutex.Unlock() @@ -345,7 +345,7 @@ func (c *Spend[SpendID, ResourceID, VoteRank]) Evict() (evictedConflicts []Spend return evictedConflicts } -// Compare compares the Conflict to the given other Conflict. +// Compare compares the Spend to the given other Spend. func (c *Spend[SpendID, ResourceID, VoteRank]) Compare(other *Spend[SpendID, ResourceID, VoteRank]) int { // no need to lock a mutex here, because the Weight is already thread-safe @@ -368,17 +368,17 @@ func (c *Spend[SpendID, ResourceID, VoteRank]) Compare(other *Spend[SpendID, Res return bytes.Compare(lo.PanicOnErr(c.ID.Bytes()), lo.PanicOnErr(other.ID.Bytes())) } -// String returns a human-readable representation of the Conflict. +// String returns a human-readable representation of the Spend. func (c *Spend[SpendID, ResourceID, VoteRank]) String() string { // no need to lock a mutex here, because the Weight is already thread-safe - return stringify.Struct("Conflict", + return stringify.Struct("Spend", stringify.NewStructField("id", c.ID), stringify.NewStructField("weight", c.Weight), ) } -// registerChild registers the given child Conflict. +// registerChild registers the given child Spend. func (c *Spend[SpendID, ResourceID, VoteRank]) registerChild(child *Spend[SpendID, ResourceID, VoteRank]) { c.structureMutex.Lock() defer c.structureMutex.Unlock() @@ -417,14 +417,14 @@ func (c *Spend[SpendID, ResourceID, VoteRank]) registerChild(child *Spend[SpendI } } -// unregisterChild unregisters the given child Conflict. -func (c *Spend[SpendID, ResourceID, VoteRank]) unregisterChild(conflict *Spend[SpendID, ResourceID, VoteRank]) { +// unregisterChild unregisters the given child Spend. +func (c *Spend[SpendID, ResourceID, VoteRank]) unregisterChild(spend *Spend[SpendID, ResourceID, VoteRank]) { c.structureMutex.Lock() defer c.structureMutex.Unlock() - if c.Children.Delete(conflict) { - if unhookFunc, exists := c.childUnhookMethods.Get(conflict.ID); exists { - c.childUnhookMethods.Delete(conflict.ID) + if c.Children.Delete(spend) { + if unhookFunc, exists := c.childUnhookMethods.Get(spend.ID); exists { + c.childUnhookMethods.Delete(spend.ID) unhookFunc() } @@ -472,7 +472,7 @@ func (c *Spend[SpendID, ResourceID, VoteRank]) removeInheritedLikedInsteadRefere } } -// setPreferredInstead sets the preferred instead value of the Conflict. +// setPreferredInstead sets the preferred instead value of the Spend. func (c *Spend[SpendID, ResourceID, VoteRank]) setPreferredInstead(preferredInstead *Spend[SpendID, ResourceID, VoteRank]) (previousPreferredInstead *Spend[SpendID, ResourceID, VoteRank]) { c.likedInsteadMutex.Lock() defer c.likedInsteadMutex.Unlock() @@ -504,7 +504,7 @@ func (c *Spend[SpendID, ResourceID, VoteRank]) setPreferredInstead(preferredInst return previousPreferredInstead } -// setAcceptanceState sets the acceptance state of the Conflict and returns the previous acceptance state (it triggers +// setAcceptanceState sets the acceptance state of the Spend and returns the previous acceptance state (it triggers // an AcceptanceStateUpdated event if the acceptance state was updated). func (c *Spend[SpendID, ResourceID, VoteRank]) setAcceptanceState(newState acceptance.State) (previousState acceptance.State) { if previousState = c.Weight.SetAcceptanceState(newState); previousState == newState { diff --git a/pkg/protocol/engine/mempool/spenddag/spenddagv1/spend_test.go b/pkg/protocol/engine/mempool/spenddag/spenddagv1/spend_test.go index b6cc4b716..deffa1faa 100644 --- a/pkg/protocol/engine/mempool/spenddag/spenddagv1/spend_test.go +++ b/pkg/protocol/engine/mempool/spenddag/spenddagv1/spend_test.go @@ -24,16 +24,16 @@ type TestSpend = *Spend[iotago.TransactionID, iotago.OutputID, vote.MockedRank] //var NewTestSpend = NewSpend[iotago.TransactionID, iotago.OutputID, vote.MockedRank] -func NewTestSpend(id iotago.TransactionID, parentConflicts ds.Set[*Spend[iotago.TransactionID, iotago.OutputID, vote.MockedRank]], conflictSets ds.Set[*ConflictSet[iotago.TransactionID, iotago.OutputID, vote.MockedRank]], initialWeight *weight.Weight, pendingTasksCounter *syncutils.Counter, acceptanceThresholdProvider func() int64) *Spend[iotago.TransactionID, iotago.OutputID, vote.MockedRank] { - conflict := NewSpend[iotago.TransactionID, iotago.OutputID, vote.MockedRank](id, initialWeight, pendingTasksCounter, acceptanceThresholdProvider) - _, err := conflict.JoinSpendSets(conflictSets) +func NewTestSpend(id iotago.TransactionID, parentConflicts ds.Set[*Spend[iotago.TransactionID, iotago.OutputID, vote.MockedRank]], SpendSets ds.Set[*ConflictSet[iotago.TransactionID, iotago.OutputID, vote.MockedRank]], initialWeight *weight.Weight, pendingTasksCounter *syncutils.Counter, acceptanceThresholdProvider func() int64) *Spend[iotago.TransactionID, iotago.OutputID, vote.MockedRank] { + spend := NewSpend[iotago.TransactionID, iotago.OutputID, vote.MockedRank](id, initialWeight, pendingTasksCounter, acceptanceThresholdProvider) + _, err := spend.JoinSpendSets(SpendSets) if err != nil { // TODO: change this panic(err) } - conflict.UpdateParents(parentConflicts, ds.NewSet[*Spend[iotago.TransactionID, iotago.OutputID, vote.MockedRank]]()) + spend.UpdateParents(parentConflicts, ds.NewSet[*Spend[iotago.TransactionID, iotago.OutputID, vote.MockedRank]]()) - return conflict + return spend } func TestConflict_SetRejected(t *testing.T) { @@ -44,17 +44,17 @@ func TestConflict_SetRejected(t *testing.T) { return int64(weights.SeatCount()) }) - conflict1 := NewTestSpend(transactionID("Conflict1"), nil, nil, weight.New(), pendingTasks, thresholdProvider) - conflict2 := NewTestSpend(transactionID("Conflict2"), ds.NewSet(conflict1), nil, weight.New(), pendingTasks, thresholdProvider) - conflict3 := NewTestSpend(transactionID("Conflict3"), ds.NewSet(conflict2), nil, weight.New(), pendingTasks, thresholdProvider) + Spend1 := NewTestSpend(transactionID("Conflict1"), nil, nil, weight.New(), pendingTasks, thresholdProvider) + Spend2 := NewTestSpend(transactionID("Conflict2"), ds.NewSet(Spend1), nil, weight.New(), pendingTasks, thresholdProvider) + Spend3 := NewTestSpend(transactionID("Conflict3"), ds.NewSet(Spend2), nil, weight.New(), pendingTasks, thresholdProvider) - conflict1.setAcceptanceState(acceptance.Rejected) - require.True(t, conflict1.IsRejected()) - require.True(t, conflict2.IsRejected()) - require.True(t, conflict3.IsRejected()) + Spend1.setAcceptanceState(acceptance.Rejected) + require.True(t, Spend1.IsRejected()) + require.True(t, Spend2.IsRejected()) + require.True(t, Spend3.IsRejected()) - conflict4 := NewTestSpend(transactionID("Conflict4"), ds.NewSet(conflict1), nil, weight.New(), pendingTasks, thresholdProvider) - require.True(t, conflict4.IsRejected()) + Spend4 := NewTestSpend(transactionID("Conflict4"), ds.NewSet(Spend1), nil, weight.New(), pendingTasks, thresholdProvider) + require.True(t, Spend4.IsRejected()) } func TestConflict_UpdateParents(t *testing.T) { @@ -65,12 +65,12 @@ func TestConflict_UpdateParents(t *testing.T) { return int64(weights.SeatCount()) }) - conflict1 := NewTestSpend(transactionID("Conflict1"), nil, nil, weight.New(), pendingTasks, thresholdProvider) - conflict2 := NewTestSpend(transactionID("Conflict2"), nil, nil, weight.New(), pendingTasks, thresholdProvider) - conflict3 := NewTestSpend(transactionID("Conflict3"), ds.NewSet(conflict1, conflict2), nil, weight.New(), pendingTasks, thresholdProvider) + Spend1 := NewTestSpend(transactionID("Conflict1"), nil, nil, weight.New(), pendingTasks, thresholdProvider) + Spend2 := NewTestSpend(transactionID("Conflict2"), nil, nil, weight.New(), pendingTasks, thresholdProvider) + Spend3 := NewTestSpend(transactionID("Conflict3"), ds.NewSet(Spend1, Spend2), nil, weight.New(), pendingTasks, thresholdProvider) - require.True(t, conflict3.Parents.Has(conflict1)) - require.True(t, conflict3.Parents.Has(conflict2)) + require.True(t, Spend3.Parents.Has(Spend1)) + require.True(t, Spend3.Parents.Has(Spend2)) } func TestConflict_SetAccepted(t *testing.T) { @@ -82,38 +82,38 @@ func TestConflict_SetAccepted(t *testing.T) { }) { - conflictSet1 := NewTestConflictSet(id("ConflictSet1")) - conflictSet2 := NewTestConflictSet(id("ConflictSet2")) + SpendSet1 := NewTestConflictSet(id("ConflictSet1")) + SpendSet2 := NewTestConflictSet(id("ConflictSet2")) - conflict1 := NewTestSpend(transactionID("Conflict1"), nil, ds.NewSet(conflictSet1), weight.New(), pendingTasks, thresholdProvider) - conflict2 := NewTestSpend(transactionID("Conflict2"), nil, ds.NewSet(conflictSet1, conflictSet2), weight.New(), pendingTasks, thresholdProvider) - conflict3 := NewTestSpend(transactionID("Conflict3"), nil, ds.NewSet(conflictSet2), weight.New(), pendingTasks, thresholdProvider) + Spend1 := NewTestSpend(transactionID("Conflict1"), nil, ds.NewSet(SpendSet1), weight.New(), pendingTasks, thresholdProvider) + Spend2 := NewTestSpend(transactionID("Conflict2"), nil, ds.NewSet(SpendSet1, SpendSet2), weight.New(), pendingTasks, thresholdProvider) + Spend3 := NewTestSpend(transactionID("Conflict3"), nil, ds.NewSet(SpendSet2), weight.New(), pendingTasks, thresholdProvider) - require.Equal(t, acceptance.Pending, conflict1.setAcceptanceState(acceptance.Accepted)) - require.True(t, conflict1.IsAccepted()) - require.True(t, conflict2.IsRejected()) - require.True(t, conflict3.IsPending()) + require.Equal(t, acceptance.Pending, Spend1.setAcceptanceState(acceptance.Accepted)) + require.True(t, Spend1.IsAccepted()) + require.True(t, Spend2.IsRejected()) + require.True(t, Spend3.IsPending()) // set acceptance twice to make sure that the event is not triggered twice // TODO: attach to the event and make sure that it's not triggered - require.Equal(t, acceptance.Accepted, conflict1.setAcceptanceState(acceptance.Accepted)) - require.True(t, conflict1.IsAccepted()) - require.True(t, conflict2.IsRejected()) - require.True(t, conflict3.IsPending()) + require.Equal(t, acceptance.Accepted, Spend1.setAcceptanceState(acceptance.Accepted)) + require.True(t, Spend1.IsAccepted()) + require.True(t, Spend2.IsRejected()) + require.True(t, Spend3.IsPending()) } { - conflictSet1 := NewTestConflictSet(id("ConflictSet1")) - conflictSet2 := NewTestConflictSet(id("ConflictSet2")) + SpendSet1 := NewTestConflictSet(id("ConflictSet1")) + SpendSet2 := NewTestConflictSet(id("ConflictSet2")) - conflict1 := NewTestSpend(transactionID("Conflict1"), nil, ds.NewSet(conflictSet1), weight.New(), pendingTasks, thresholdProvider) - conflict2 := NewTestSpend(transactionID("Conflict2"), nil, ds.NewSet(conflictSet1, conflictSet2), weight.New(), pendingTasks, thresholdProvider) - conflict3 := NewTestSpend(transactionID("Conflict3"), nil, ds.NewSet(conflictSet2), weight.New(), pendingTasks, thresholdProvider) + Spend1 := NewTestSpend(transactionID("Conflict1"), nil, ds.NewSet(SpendSet1), weight.New(), pendingTasks, thresholdProvider) + Spend2 := NewTestSpend(transactionID("Conflict2"), nil, ds.NewSet(SpendSet1, SpendSet2), weight.New(), pendingTasks, thresholdProvider) + Spend3 := NewTestSpend(transactionID("Conflict3"), nil, ds.NewSet(SpendSet2), weight.New(), pendingTasks, thresholdProvider) - conflict2.setAcceptanceState(acceptance.Accepted) - require.True(t, conflict1.IsRejected()) - require.True(t, conflict2.IsAccepted()) - require.True(t, conflict3.IsRejected()) + Spend2.setAcceptanceState(acceptance.Accepted) + require.True(t, Spend1.IsRejected()) + require.True(t, Spend2.IsAccepted()) + require.True(t, Spend3.IsRejected()) } } @@ -130,85 +130,85 @@ func TestConflict_SpendSets(t *testing.T) { green := NewTestConflictSet(id("green")) yellow := NewTestConflictSet(id("yellow")) - conflictA := NewTestSpend(transactionID("A"), nil, ds.NewSet(red), weight.New().AddCumulativeWeight(7), pendingTasks, thresholdProvider) - conflictB := NewTestSpend(transactionID("B"), nil, ds.NewSet(red, blue), weight.New().AddCumulativeWeight(3), pendingTasks, thresholdProvider) - conflictC := NewTestSpend(transactionID("C"), nil, ds.NewSet(blue, green), weight.New().AddCumulativeWeight(5), pendingTasks, thresholdProvider) - conflictD := NewTestSpend(transactionID("D"), nil, ds.NewSet(green, yellow), weight.New().AddCumulativeWeight(7), pendingTasks, thresholdProvider) - conflictE := NewTestSpend(transactionID("E"), nil, ds.NewSet(yellow), weight.New().AddCumulativeWeight(9), pendingTasks, thresholdProvider) + SpendA := NewTestSpend(transactionID("A"), nil, ds.NewSet(red), weight.New().AddCumulativeWeight(7), pendingTasks, thresholdProvider) + SpendB := NewTestSpend(transactionID("B"), nil, ds.NewSet(red, blue), weight.New().AddCumulativeWeight(3), pendingTasks, thresholdProvider) + SpendC := NewTestSpend(transactionID("C"), nil, ds.NewSet(blue, green), weight.New().AddCumulativeWeight(5), pendingTasks, thresholdProvider) + SpendD := NewTestSpend(transactionID("D"), nil, ds.NewSet(green, yellow), weight.New().AddCumulativeWeight(7), pendingTasks, thresholdProvider) + SpendE := NewTestSpend(transactionID("E"), nil, ds.NewSet(yellow), weight.New().AddCumulativeWeight(9), pendingTasks, thresholdProvider) preferredInsteadMap := map[TestSpend]TestSpend{ - conflictA: conflictA, - conflictB: conflictA, - conflictC: conflictC, - conflictD: conflictE, - conflictE: conflictE, + SpendA: SpendA, + SpendB: SpendA, + SpendC: SpendC, + SpendD: SpendE, + SpendE: SpendE, } pendingTasks.WaitIsZero() assertPreferredInstead(t, preferredInsteadMap) - conflictD.Weight.SetCumulativeWeight(10) + SpendD.Weight.SetCumulativeWeight(10) pendingTasks.WaitIsZero() assertPreferredInstead(t, lo.MergeMaps(preferredInsteadMap, map[TestSpend]TestSpend{ - conflictC: conflictD, - conflictD: conflictD, - conflictE: conflictD, + SpendC: SpendD, + SpendD: SpendD, + SpendE: SpendD, })) - conflictD.Weight.SetCumulativeWeight(0) + SpendD.Weight.SetCumulativeWeight(0) pendingTasks.WaitIsZero() assertPreferredInstead(t, lo.MergeMaps(preferredInsteadMap, map[TestSpend]TestSpend{ - conflictC: conflictC, - conflictD: conflictE, - conflictE: conflictE, + SpendC: SpendC, + SpendD: SpendE, + SpendE: SpendE, })) - conflictC.Weight.SetCumulativeWeight(8) + SpendC.Weight.SetCumulativeWeight(8) pendingTasks.WaitIsZero() assertPreferredInstead(t, lo.MergeMaps(preferredInsteadMap, map[TestSpend]TestSpend{ - conflictB: conflictC, + SpendB: SpendC, })) - conflictC.Weight.SetCumulativeWeight(8) + SpendC.Weight.SetCumulativeWeight(8) pendingTasks.WaitIsZero() assertPreferredInstead(t, lo.MergeMaps(preferredInsteadMap, map[TestSpend]TestSpend{ - conflictB: conflictC, + SpendB: SpendC, })) - conflictD.Weight.SetCumulativeWeight(3) + SpendD.Weight.SetCumulativeWeight(3) pendingTasks.WaitIsZero() assertPreferredInstead(t, preferredInsteadMap) - conflictE.Weight.SetCumulativeWeight(1) + SpendE.Weight.SetCumulativeWeight(1) pendingTasks.WaitIsZero() assertPreferredInstead(t, lo.MergeMaps(preferredInsteadMap, map[TestSpend]TestSpend{ - conflictD: conflictC, + SpendD: SpendC, })) - conflictE.Weight.SetCumulativeWeight(9) + SpendE.Weight.SetCumulativeWeight(9) pendingTasks.WaitIsZero() assertPreferredInstead(t, lo.MergeMaps(preferredInsteadMap, map[TestSpend]TestSpend{ - conflictD: conflictE, + SpendD: SpendE, })) - conflictF := NewTestSpend(transactionID("F"), nil, ds.NewSet(yellow), weight.New().AddCumulativeWeight(19), pendingTasks, thresholdProvider) + SpendF := NewTestSpend(transactionID("F"), nil, ds.NewSet(yellow), weight.New().AddCumulativeWeight(19), pendingTasks, thresholdProvider) pendingTasks.WaitIsZero() assertPreferredInstead(t, lo.MergeMaps(preferredInsteadMap, map[TestSpend]TestSpend{ - conflictD: conflictF, - conflictE: conflictF, - conflictF: conflictF, + SpendD: SpendF, + SpendE: SpendF, + SpendF: SpendF, })) - assertCorrectOrder(t, conflictA, conflictB, conflictC, conflictD, conflictE, conflictF) + assertCorrectOrder(t, SpendA, SpendB, SpendC, SpendD, SpendE, SpendF) } func TestConflictParallel(t *testing.T) { @@ -223,7 +223,7 @@ func TestConflictParallel(t *testing.T) { const updateCount = 100000 - permutations := make([]func(conflict TestSpend), 0) + permutations := make([]func(Spend TestSpend), 0) for i := 0; i < updateCount; i++ { permutations = append(permutations, generateRandomConflictPermutation()) } @@ -235,7 +235,7 @@ func TestConflictParallel(t *testing.T) { permutation(sequentialConflicts[targetAlias]) wg.Add(1) - go func(permutation func(conflict TestSpend)) { + go func(permutation func(Spend TestSpend)) { permutation(parallelConflicts[targetAlias]) wg.Done() @@ -248,8 +248,8 @@ func TestConflictParallel(t *testing.T) { parallelPendingTasks.WaitIsZero() - lo.ForEach(lo.Keys(parallelConflicts), func(conflictAlias string) { - assert.EqualValuesf(t, sequentialConflicts[conflictAlias].PreferredInstead().ID, parallelConflicts[conflictAlias].PreferredInstead().ID, "parallel conflict %s prefers %s, but sequential conflict prefers %s", conflictAlias, parallelConflicts[conflictAlias].PreferredInstead().ID, sequentialConflicts[conflictAlias].PreferredInstead().ID) + lo.ForEach(lo.Keys(parallelConflicts), func(SpendAlias string) { + assert.EqualValuesf(t, sequentialConflicts[SpendAlias].PreferredInstead().ID, parallelConflicts[SpendAlias].PreferredInstead().ID, "parallel Spend %s prefers %s, but sequential Spend prefers %s", SpendAlias, parallelConflicts[SpendAlias].PreferredInstead().ID, sequentialConflicts[SpendAlias].PreferredInstead().ID) }) assertCorrectOrder(t, lo.Values(sequentialConflicts)...) @@ -268,19 +268,19 @@ func TestLikedInstead1(t *testing.T) { require.True(t, masterBranch.IsLiked()) require.True(t, masterBranch.LikedInstead().IsEmpty()) - conflictSet1 := NewTestConflictSet(id("O1")) + SpendSet1 := NewTestConflictSet(id("O1")) - conflict1 := NewTestSpend(transactionID("TxA"), ds.NewSet(masterBranch), ds.NewSet(conflictSet1), weight.New().SetCumulativeWeight(6), pendingTasks, thresholdProvider) - conflict2 := NewTestSpend(transactionID("TxB"), ds.NewSet(masterBranch), ds.NewSet(conflictSet1), weight.New().SetCumulativeWeight(3), pendingTasks, thresholdProvider) + Spend1 := NewTestSpend(transactionID("TxA"), ds.NewSet(masterBranch), ds.NewSet(SpendSet1), weight.New().SetCumulativeWeight(6), pendingTasks, thresholdProvider) + Spend2 := NewTestSpend(transactionID("TxB"), ds.NewSet(masterBranch), ds.NewSet(SpendSet1), weight.New().SetCumulativeWeight(3), pendingTasks, thresholdProvider) - require.True(t, conflict1.IsPreferred()) - require.True(t, conflict1.IsLiked()) - require.Equal(t, 0, conflict1.LikedInstead().Size()) + require.True(t, Spend1.IsPreferred()) + require.True(t, Spend1.IsLiked()) + require.Equal(t, 0, Spend1.LikedInstead().Size()) - require.False(t, conflict2.IsPreferred()) - require.False(t, conflict2.IsLiked()) - require.Equal(t, 1, conflict2.LikedInstead().Size()) - require.True(t, conflict2.LikedInstead().Has(conflict1)) + require.False(t, Spend2.IsPreferred()) + require.False(t, Spend2.IsLiked()) + require.Equal(t, 1, Spend2.LikedInstead().Size()) + require.True(t, Spend2.LikedInstead().Has(Spend1)) } func TestLikedInsteadFromPreferredInstead(t *testing.T) { @@ -295,69 +295,69 @@ func TestLikedInsteadFromPreferredInstead(t *testing.T) { require.True(t, masterBranch.IsLiked()) require.True(t, masterBranch.LikedInstead().IsEmpty()) - conflictSet1 := NewTestConflictSet(id("O1")) - conflictA := NewTestSpend(transactionID("TxA"), ds.NewSet(masterBranch), ds.NewSet(conflictSet1), weight.New().SetCumulativeWeight(200), pendingTasks, thresholdProvider) - conflictB := NewTestSpend(transactionID("TxB"), ds.NewSet(masterBranch), ds.NewSet(conflictSet1), weight.New().SetCumulativeWeight(100), pendingTasks, thresholdProvider) + SpendSet1 := NewTestConflictSet(id("O1")) + SpendA := NewTestSpend(transactionID("TxA"), ds.NewSet(masterBranch), ds.NewSet(SpendSet1), weight.New().SetCumulativeWeight(200), pendingTasks, thresholdProvider) + SpendB := NewTestSpend(transactionID("TxB"), ds.NewSet(masterBranch), ds.NewSet(SpendSet1), weight.New().SetCumulativeWeight(100), pendingTasks, thresholdProvider) - require.True(t, conflictA.IsPreferred()) - require.True(t, conflictA.IsLiked()) - require.Equal(t, 0, conflictA.LikedInstead().Size()) + require.True(t, SpendA.IsPreferred()) + require.True(t, SpendA.IsLiked()) + require.Equal(t, 0, SpendA.LikedInstead().Size()) - require.False(t, conflictB.IsPreferred()) - require.False(t, conflictB.IsLiked()) - require.Equal(t, 1, conflictB.LikedInstead().Size()) - require.True(t, conflictB.LikedInstead().Has(conflictA)) + require.False(t, SpendB.IsPreferred()) + require.False(t, SpendB.IsLiked()) + require.Equal(t, 1, SpendB.LikedInstead().Size()) + require.True(t, SpendB.LikedInstead().Has(SpendA)) - conflictSet2 := NewTestConflictSet(id("O2")) - conflictC := NewTestSpend(transactionID("TxC"), ds.NewSet(conflictA), ds.NewSet(conflictSet2), weight.New().SetCumulativeWeight(200), pendingTasks, thresholdProvider) - conflictD := NewTestSpend(transactionID("TxD"), ds.NewSet(conflictA), ds.NewSet(conflictSet2), weight.New().SetCumulativeWeight(100), pendingTasks, thresholdProvider) + SpendSet2 := NewTestConflictSet(id("O2")) + SpendC := NewTestSpend(transactionID("TxC"), ds.NewSet(SpendA), ds.NewSet(SpendSet2), weight.New().SetCumulativeWeight(200), pendingTasks, thresholdProvider) + SpendD := NewTestSpend(transactionID("TxD"), ds.NewSet(SpendA), ds.NewSet(SpendSet2), weight.New().SetCumulativeWeight(100), pendingTasks, thresholdProvider) - require.True(t, conflictC.IsPreferred()) - require.True(t, conflictC.IsLiked()) - require.Equal(t, 0, conflictC.LikedInstead().Size()) + require.True(t, SpendC.IsPreferred()) + require.True(t, SpendC.IsLiked()) + require.Equal(t, 0, SpendC.LikedInstead().Size()) - require.False(t, conflictD.IsPreferred()) - require.False(t, conflictD.IsLiked()) - require.Equal(t, 1, conflictD.LikedInstead().Size()) - require.True(t, conflictD.LikedInstead().Has(conflictC)) + require.False(t, SpendD.IsPreferred()) + require.False(t, SpendD.IsLiked()) + require.Equal(t, 1, SpendD.LikedInstead().Size()) + require.True(t, SpendD.LikedInstead().Has(SpendC)) - conflictB.Weight.SetCumulativeWeight(300) + SpendB.Weight.SetCumulativeWeight(300) pendingTasks.WaitIsZero() - require.True(t, conflictB.IsPreferred()) - require.True(t, conflictB.IsLiked()) - require.Equal(t, 0, conflictB.LikedInstead().Size()) + require.True(t, SpendB.IsPreferred()) + require.True(t, SpendB.IsLiked()) + require.Equal(t, 0, SpendB.LikedInstead().Size()) - require.False(t, conflictA.IsPreferred()) - require.False(t, conflictA.IsLiked()) - require.Equal(t, 1, conflictA.LikedInstead().Size()) - require.True(t, conflictA.LikedInstead().Has(conflictB)) + require.False(t, SpendA.IsPreferred()) + require.False(t, SpendA.IsLiked()) + require.Equal(t, 1, SpendA.LikedInstead().Size()) + require.True(t, SpendA.LikedInstead().Has(SpendB)) - require.False(t, conflictD.IsPreferred()) - require.False(t, conflictD.IsLiked()) - require.Equal(t, 1, conflictD.LikedInstead().Size()) - require.True(t, conflictD.LikedInstead().Has(conflictB)) + require.False(t, SpendD.IsPreferred()) + require.False(t, SpendD.IsLiked()) + require.Equal(t, 1, SpendD.LikedInstead().Size()) + require.True(t, SpendD.LikedInstead().Has(SpendB)) - conflictB.Weight.SetCumulativeWeight(100) + SpendB.Weight.SetCumulativeWeight(100) pendingTasks.WaitIsZero() - require.True(t, conflictA.IsPreferred()) - require.True(t, conflictA.IsLiked()) - require.Equal(t, 0, conflictA.LikedInstead().Size()) + require.True(t, SpendA.IsPreferred()) + require.True(t, SpendA.IsLiked()) + require.Equal(t, 0, SpendA.LikedInstead().Size()) - require.False(t, conflictB.IsPreferred()) - require.False(t, conflictB.IsLiked()) - require.Equal(t, 1, conflictB.LikedInstead().Size()) - require.True(t, conflictB.LikedInstead().Has(conflictA)) + require.False(t, SpendB.IsPreferred()) + require.False(t, SpendB.IsLiked()) + require.Equal(t, 1, SpendB.LikedInstead().Size()) + require.True(t, SpendB.LikedInstead().Has(SpendA)) - require.True(t, conflictC.IsPreferred()) - require.True(t, conflictC.IsLiked()) - require.Equal(t, 0, conflictC.LikedInstead().Size()) + require.True(t, SpendC.IsPreferred()) + require.True(t, SpendC.IsLiked()) + require.Equal(t, 0, SpendC.LikedInstead().Size()) - require.False(t, conflictD.IsPreferred()) - require.False(t, conflictD.IsLiked()) - require.Equal(t, 1, conflictD.LikedInstead().Size()) - require.True(t, conflictD.LikedInstead().Has(conflictC)) + require.False(t, SpendD.IsPreferred()) + require.False(t, SpendD.IsLiked()) + require.Equal(t, 1, SpendD.LikedInstead().Size()) + require.True(t, SpendD.LikedInstead().Has(SpendC)) } func TestLikedInstead21(t *testing.T) { @@ -372,80 +372,80 @@ func TestLikedInstead21(t *testing.T) { require.True(t, masterBranch.IsLiked()) require.True(t, masterBranch.LikedInstead().IsEmpty()) - conflictSet1 := NewTestConflictSet(id("O1")) - conflictA := NewTestSpend(transactionID("TxA"), ds.NewSet(masterBranch), ds.NewSet(conflictSet1), weight.New().SetCumulativeWeight(200), pendingTasks, thresholdProvider) - conflictB := NewTestSpend(transactionID("TxB"), ds.NewSet(masterBranch), ds.NewSet(conflictSet1), weight.New().SetCumulativeWeight(100), pendingTasks, thresholdProvider) + SpendSet1 := NewTestConflictSet(id("O1")) + SpendA := NewTestSpend(transactionID("TxA"), ds.NewSet(masterBranch), ds.NewSet(SpendSet1), weight.New().SetCumulativeWeight(200), pendingTasks, thresholdProvider) + SpendB := NewTestSpend(transactionID("TxB"), ds.NewSet(masterBranch), ds.NewSet(SpendSet1), weight.New().SetCumulativeWeight(100), pendingTasks, thresholdProvider) - require.True(t, conflictA.IsPreferred()) - require.True(t, conflictA.IsLiked()) - require.Equal(t, 0, conflictA.LikedInstead().Size()) + require.True(t, SpendA.IsPreferred()) + require.True(t, SpendA.IsLiked()) + require.Equal(t, 0, SpendA.LikedInstead().Size()) - require.False(t, conflictB.IsPreferred()) - require.False(t, conflictB.IsLiked()) - require.Equal(t, 1, conflictB.LikedInstead().Size()) - require.True(t, conflictB.LikedInstead().Has(conflictA)) + require.False(t, SpendB.IsPreferred()) + require.False(t, SpendB.IsLiked()) + require.Equal(t, 1, SpendB.LikedInstead().Size()) + require.True(t, SpendB.LikedInstead().Has(SpendA)) - conflictSet4 := NewTestConflictSet(id("O4")) - conflictF := NewTestSpend(transactionID("TxF"), ds.NewSet(conflictA), ds.NewSet(conflictSet4), weight.New().SetCumulativeWeight(20), pendingTasks, thresholdProvider) - conflictG := NewTestSpend(transactionID("TxG"), ds.NewSet(conflictA), ds.NewSet(conflictSet4), weight.New().SetCumulativeWeight(10), pendingTasks, thresholdProvider) + SpendSet4 := NewTestConflictSet(id("O4")) + SpendF := NewTestSpend(transactionID("TxF"), ds.NewSet(SpendA), ds.NewSet(SpendSet4), weight.New().SetCumulativeWeight(20), pendingTasks, thresholdProvider) + SpendG := NewTestSpend(transactionID("TxG"), ds.NewSet(SpendA), ds.NewSet(SpendSet4), weight.New().SetCumulativeWeight(10), pendingTasks, thresholdProvider) - require.True(t, conflictF.IsPreferred()) - require.True(t, conflictF.IsLiked()) - require.Equal(t, 0, conflictF.LikedInstead().Size()) + require.True(t, SpendF.IsPreferred()) + require.True(t, SpendF.IsLiked()) + require.Equal(t, 0, SpendF.LikedInstead().Size()) - require.False(t, conflictG.IsPreferred()) - require.False(t, conflictG.IsLiked()) - require.Equal(t, 1, conflictG.LikedInstead().Size()) - require.True(t, conflictG.LikedInstead().Has(conflictF)) + require.False(t, SpendG.IsPreferred()) + require.False(t, SpendG.IsLiked()) + require.Equal(t, 1, SpendG.LikedInstead().Size()) + require.True(t, SpendG.LikedInstead().Has(SpendF)) - conflictSet2 := NewTestConflictSet(id("O2")) - conflictC := NewTestSpend(transactionID("TxC"), ds.NewSet(masterBranch), ds.NewSet(conflictSet2), weight.New().SetCumulativeWeight(200), pendingTasks, thresholdProvider) - conflictH := NewTestSpend(transactionID("TxH"), ds.NewSet(masterBranch, conflictA), ds.NewSet(conflictSet2, conflictSet4), weight.New().SetCumulativeWeight(150), pendingTasks, thresholdProvider) + SpendSet2 := NewTestConflictSet(id("O2")) + SpendC := NewTestSpend(transactionID("TxC"), ds.NewSet(masterBranch), ds.NewSet(SpendSet2), weight.New().SetCumulativeWeight(200), pendingTasks, thresholdProvider) + SpendH := NewTestSpend(transactionID("TxH"), ds.NewSet(masterBranch, SpendA), ds.NewSet(SpendSet2, SpendSet4), weight.New().SetCumulativeWeight(150), pendingTasks, thresholdProvider) - require.True(t, conflictC.IsPreferred()) - require.True(t, conflictC.IsLiked()) - require.Equal(t, 0, conflictC.LikedInstead().Size()) + require.True(t, SpendC.IsPreferred()) + require.True(t, SpendC.IsLiked()) + require.Equal(t, 0, SpendC.LikedInstead().Size()) - require.False(t, conflictH.IsPreferred()) - require.False(t, conflictH.IsLiked()) - require.Equal(t, 1, conflictH.LikedInstead().Size()) - require.True(t, conflictH.LikedInstead().Has(conflictC)) + require.False(t, SpendH.IsPreferred()) + require.False(t, SpendH.IsLiked()) + require.Equal(t, 1, SpendH.LikedInstead().Size()) + require.True(t, SpendH.LikedInstead().Has(SpendC)) - conflictSet3 := NewTestConflictSet(id("O12")) - conflictI := NewTestSpend(transactionID("TxI"), ds.NewSet(conflictF), ds.NewSet(conflictSet3), weight.New().SetCumulativeWeight(5), pendingTasks, thresholdProvider) - conflictJ := NewTestSpend(transactionID("TxJ"), ds.NewSet(conflictF), ds.NewSet(conflictSet3), weight.New().SetCumulativeWeight(15), pendingTasks, thresholdProvider) + SpendSet3 := NewTestConflictSet(id("O12")) + SpendI := NewTestSpend(transactionID("TxI"), ds.NewSet(SpendF), ds.NewSet(SpendSet3), weight.New().SetCumulativeWeight(5), pendingTasks, thresholdProvider) + SpendJ := NewTestSpend(transactionID("TxJ"), ds.NewSet(SpendF), ds.NewSet(SpendSet3), weight.New().SetCumulativeWeight(15), pendingTasks, thresholdProvider) - require.True(t, conflictJ.IsPreferred()) - require.True(t, conflictJ.IsLiked()) - require.Equal(t, 0, conflictJ.LikedInstead().Size()) + require.True(t, SpendJ.IsPreferred()) + require.True(t, SpendJ.IsLiked()) + require.Equal(t, 0, SpendJ.LikedInstead().Size()) - require.False(t, conflictI.IsPreferred()) - require.False(t, conflictI.IsLiked()) - require.Equal(t, 1, conflictI.LikedInstead().Size()) - require.True(t, conflictI.LikedInstead().Has(conflictJ)) + require.False(t, SpendI.IsPreferred()) + require.False(t, SpendI.IsLiked()) + require.Equal(t, 1, SpendI.LikedInstead().Size()) + require.True(t, SpendI.LikedInstead().Has(SpendJ)) - conflictH.Weight.SetCumulativeWeight(250) + SpendH.Weight.SetCumulativeWeight(250) pendingTasks.WaitIsZero() - require.True(t, conflictH.IsPreferred()) - require.True(t, conflictH.IsLiked()) - require.Equal(t, 0, conflictH.LikedInstead().Size()) + require.True(t, SpendH.IsPreferred()) + require.True(t, SpendH.IsLiked()) + require.Equal(t, 0, SpendH.LikedInstead().Size()) - require.False(t, conflictF.IsPreferred()) - require.False(t, conflictF.IsLiked()) - require.Equal(t, 1, conflictF.LikedInstead().Size()) - require.True(t, conflictF.LikedInstead().Has(conflictH)) + require.False(t, SpendF.IsPreferred()) + require.False(t, SpendF.IsLiked()) + require.Equal(t, 1, SpendF.LikedInstead().Size()) + require.True(t, SpendF.LikedInstead().Has(SpendH)) - require.False(t, conflictG.IsPreferred()) - require.False(t, conflictG.IsLiked()) - require.Equal(t, 1, conflictG.LikedInstead().Size()) - require.True(t, conflictG.LikedInstead().Has(conflictH)) + require.False(t, SpendG.IsPreferred()) + require.False(t, SpendG.IsLiked()) + require.Equal(t, 1, SpendG.LikedInstead().Size()) + require.True(t, SpendG.LikedInstead().Has(SpendH)) - require.True(t, conflictJ.IsPreferred()) - require.False(t, conflictJ.IsLiked()) - require.Equal(t, 1, conflictJ.LikedInstead().Size()) - require.True(t, conflictJ.LikedInstead().Has(conflictH)) + require.True(t, SpendJ.IsPreferred()) + require.False(t, SpendJ.IsLiked()) + require.Equal(t, 1, SpendJ.LikedInstead().Size()) + require.True(t, SpendJ.LikedInstead().Has(SpendH)) } func TestConflictSet_AllMembersEvicted(t *testing.T) { @@ -459,22 +459,22 @@ func TestConflictSet_AllMembersEvicted(t *testing.T) { return int64(weights.SeatCount()) }) - conflict1 := NewTestSpend(transactionID("conflict1"), nil, ds.NewSet(yellow), weight.New(), pendingTasks, thresholdProvider) - evictedConflicts := conflict1.Evict() + Spend1 := NewTestSpend(transactionID("Spend1"), nil, ds.NewSet(yellow), weight.New(), pendingTasks, thresholdProvider) + evictedConflicts := Spend1.Evict() require.Len(t, evictedConflicts, 1) - require.Contains(t, evictedConflicts, conflict1.ID) + require.Contains(t, evictedConflicts, Spend1.ID) - // evict the conflict another time and make sure that none conflicts were evicted - evictedConflicts = conflict1.Evict() + // evict the Spend another time and make sure that none Spends were evicted + evictedConflicts = Spend1.Evict() require.Len(t, evictedConflicts, 0) - // conflict tries to join conflictset who's all members were evicted - conflict2 := NewSpend[iotago.TransactionID, iotago.OutputID, vote.MockedRank](transactionID("conflict1"), weight.New(), pendingTasks, thresholdProvider) - _, err := conflict2.JoinSpendSets(ds.NewSet(yellow)) + // Spend tries to join Spendset who's all members were evicted + Spend2 := NewSpend[iotago.TransactionID, iotago.OutputID, vote.MockedRank](transactionID("Spend1"), weight.New(), pendingTasks, thresholdProvider) + _, err := Spend2.JoinSpendSets(ds.NewSet(yellow)) require.Error(t, err) - // evicted conflict tries to join conflictset - _, err = conflict1.JoinSpendSets(ds.NewSet(green)) + // evicted Spend tries to join Spendset + _, err = Spend1.JoinSpendSets(ds.NewSet(green)) require.Error(t, err) } @@ -486,13 +486,13 @@ func TestConflict_Compare(t *testing.T) { return int64(weights.SeatCount()) }) - var conflict1, conflict2 TestSpend + var Spend1, Spend2 TestSpend - conflict1 = NewTestSpend(transactionID("M"), nil, nil, weight.New(), pendingTasks, thresholdProvider) + Spend1 = NewTestSpend(transactionID("M"), nil, nil, weight.New(), pendingTasks, thresholdProvider) - require.Equal(t, weight.Heavier, conflict1.Compare(nil)) - require.Equal(t, weight.Lighter, conflict2.Compare(conflict1)) - require.Equal(t, weight.Equal, conflict2.Compare(nil)) + require.Equal(t, weight.Heavier, Spend1.Compare(nil)) + require.Equal(t, weight.Lighter, Spend2.Compare(Spend1)) + require.Equal(t, weight.Equal, Spend2.Compare(nil)) } func TestConflict_Inheritance(t *testing.T) { @@ -506,66 +506,66 @@ func TestConflict_Inheritance(t *testing.T) { return int64(weights.SeatCount()) }) - conflict1 := NewTestSpend(transactionID("conflict1"), nil, ds.NewSet(yellow), weight.New().SetCumulativeWeight(1), pendingTasks, thresholdProvider) - conflict2 := NewTestSpend(transactionID("conflict2"), nil, ds.NewSet(green), weight.New().SetCumulativeWeight(1), pendingTasks, thresholdProvider) - conflict3 := NewTestSpend(transactionID("conflict3"), ds.NewSet(conflict1, conflict2), nil, weight.New(), pendingTasks, thresholdProvider) - conflict4 := NewTestSpend(transactionID("conflict4"), nil, ds.NewSet(yellow, green), weight.New(), pendingTasks, thresholdProvider) + Spend1 := NewTestSpend(transactionID("Spend1"), nil, ds.NewSet(yellow), weight.New().SetCumulativeWeight(1), pendingTasks, thresholdProvider) + Spend2 := NewTestSpend(transactionID("Spend2"), nil, ds.NewSet(green), weight.New().SetCumulativeWeight(1), pendingTasks, thresholdProvider) + Spend3 := NewTestSpend(transactionID("Spend3"), ds.NewSet(Spend1, Spend2), nil, weight.New(), pendingTasks, thresholdProvider) + Spend4 := NewTestSpend(transactionID("Spend4"), nil, ds.NewSet(yellow, green), weight.New(), pendingTasks, thresholdProvider) pendingTasks.WaitIsZero() - require.True(t, conflict3.LikedInstead().IsEmpty()) + require.True(t, Spend3.LikedInstead().IsEmpty()) - conflict4.Weight.SetCumulativeWeight(10) + Spend4.Weight.SetCumulativeWeight(10) pendingTasks.WaitIsZero() - require.True(t, conflict3.LikedInstead().Has(conflict4)) + require.True(t, Spend3.LikedInstead().Has(Spend4)) // set it manually again, to make sure that it's idempotent - conflict2.setPreferredInstead(conflict4) + Spend2.setPreferredInstead(Spend4) pendingTasks.WaitIsZero() - require.True(t, conflict3.LikedInstead().Has(conflict4)) + require.True(t, Spend3.LikedInstead().Has(Spend4)) - // make sure that inheritance of LikedInstead works correctly for newly created conflicts - conflict5 := NewTestSpend(transactionID("conflict5"), ds.NewSet(conflict3), nil, weight.New(), pendingTasks, thresholdProvider) + // make sure that inheritance of LikedInstead works correctly for newly created Spends + Spend5 := NewTestSpend(transactionID("Spend5"), ds.NewSet(Spend3), nil, weight.New(), pendingTasks, thresholdProvider) pendingTasks.WaitIsZero() - require.True(t, conflict5.LikedInstead().Has(conflict4)) + require.True(t, Spend5.LikedInstead().Has(Spend4)) - conflict1.Weight.SetCumulativeWeight(15) + Spend1.Weight.SetCumulativeWeight(15) pendingTasks.WaitIsZero() - require.True(t, conflict3.LikedInstead().IsEmpty()) + require.True(t, Spend3.LikedInstead().IsEmpty()) } -func assertCorrectOrder(t *testing.T, conflicts ...TestSpend) { - sort.Slice(conflicts, func(i, j int) bool { - return conflicts[i].Compare(conflicts[j]) == weight.Heavier +func assertCorrectOrder(t *testing.T, spends ...TestSpend) { + sort.Slice(spends, func(i, j int) bool { + return spends[i].Compare(spends[j]) == weight.Heavier }) preferredConflicts := ds.NewSet[TestSpend]() unPreferredConflicts := ds.NewSet[TestSpend]() - for _, conflict := range conflicts { - if !unPreferredConflicts.Has(conflict) { - preferredConflicts.Add(conflict) - conflict.ConflictingSpends.Range(func(conflictingConflict *Spend[iotago.TransactionID, iotago.OutputID, vote.MockedRank]) { - if conflict != conflictingConflict { - unPreferredConflicts.Add(conflictingConflict) + for _, spend := range spends { + if !unPreferredConflicts.Has(spend) { + preferredConflicts.Add(spend) + spend.ConflictingSpends.Range(func(conflictingSpend *Spend[iotago.TransactionID, iotago.OutputID, vote.MockedRank]) { + if spend != conflictingSpend { + unPreferredConflicts.Add(conflictingSpend) } }, true) } } - for _, conflict := range conflicts { - if preferredConflicts.Has(conflict) { - require.True(t, conflict.IsPreferred(), "conflict %s should be preferred", conflict.ID) + for _, Spend := range spends { + if preferredConflicts.Has(Spend) { + require.True(t, Spend.IsPreferred(), "Spend %s should be preferred", Spend.ID) } - if unPreferredConflicts.Has(conflict) { - require.False(t, conflict.IsPreferred(), "conflict %s should be unPreferred", conflict.ID) + if unPreferredConflicts.Has(Spend) { + require.False(t, Spend.IsPreferred(), "Spend %s should be unPreferred", Spend.ID) } } _ = unPreferredConflicts.ForEach(func(unPreferredConflict TestSpend) (err error) { - // iterating in descending order, so the first preferred conflict - return unPreferredConflict.ConflictingSpends.ForEach(func(conflictingConflict TestSpend) error { - if conflictingConflict != unPreferredConflict && conflictingConflict.IsPreferred() { - require.Equal(t, conflictingConflict, unPreferredConflict.PreferredInstead()) + // iterating in descending order, so the first preferred Spend + return unPreferredConflict.ConflictingSpends.ForEach(func(SpendingConflict TestSpend) error { + if SpendingConflict != unPreferredConflict && SpendingConflict.IsPreferred() { + require.Equal(t, SpendingConflict, unPreferredConflict.PreferredInstead()) return ierrors.New("break the loop") } @@ -575,15 +575,15 @@ func assertCorrectOrder(t *testing.T, conflicts ...TestSpend) { }) } -func generateRandomConflictPermutation() func(conflict TestSpend) { +func generateRandomConflictPermutation() func(Spend TestSpend) { updateType := rand.Intn(100) delta := rand.Intn(100) - return func(conflict TestSpend) { + return func(Spend TestSpend) { if updateType%2 == 0 { - conflict.Weight.AddCumulativeWeight(int64(delta)) + Spend.Weight.AddCumulativeWeight(int64(delta)) } else { - conflict.Weight.RemoveCumulativeWeight(int64(delta)) + Spend.Weight.RemoveCumulativeWeight(int64(delta)) } } } @@ -600,23 +600,23 @@ func createConflicts(pendingTasks *syncutils.Counter) map[string]TestSpend { green := NewTestConflictSet(id("green")) yellow := NewTestConflictSet(id("yellow")) - conflictA := NewTestSpend(transactionID("A"), nil, ds.NewSet(red), weight.New(), pendingTasks, thresholdProvider) - conflictB := NewTestSpend(transactionID("B"), nil, ds.NewSet(red, blue), weight.New(), pendingTasks, thresholdProvider) - conflictC := NewTestSpend(transactionID("C"), nil, ds.NewSet(green, blue), weight.New(), pendingTasks, thresholdProvider) - conflictD := NewTestSpend(transactionID("D"), nil, ds.NewSet(green, yellow), weight.New(), pendingTasks, thresholdProvider) - conflictE := NewTestSpend(transactionID("E"), nil, ds.NewSet(yellow), weight.New(), pendingTasks, thresholdProvider) + SpendA := NewTestSpend(transactionID("A"), nil, ds.NewSet(red), weight.New(), pendingTasks, thresholdProvider) + SpendB := NewTestSpend(transactionID("B"), nil, ds.NewSet(red, blue), weight.New(), pendingTasks, thresholdProvider) + SpendC := NewTestSpend(transactionID("C"), nil, ds.NewSet(green, blue), weight.New(), pendingTasks, thresholdProvider) + SpendD := NewTestSpend(transactionID("D"), nil, ds.NewSet(green, yellow), weight.New(), pendingTasks, thresholdProvider) + SpendE := NewTestSpend(transactionID("E"), nil, ds.NewSet(yellow), weight.New(), pendingTasks, thresholdProvider) return map[string]TestSpend{ - "conflictA": conflictA, - "conflictB": conflictB, - "conflictC": conflictC, - "conflictD": conflictD, - "conflictE": conflictE, + "SpendA": SpendA, + "SpendB": SpendB, + "SpendC": SpendC, + "SpendD": SpendD, + "SpendE": SpendE, } } func assertPreferredInstead(t *testing.T, preferredInsteadMap map[TestSpend]TestSpend) { - for conflict, preferredInsteadConflict := range preferredInsteadMap { - assert.Equalf(t, preferredInsteadConflict.ID, conflict.PreferredInstead().ID, "conflict %s should prefer %s instead of %s", conflict.ID, preferredInsteadConflict.ID, conflict.PreferredInstead().ID) + for Spend, preferredInsteadConflict := range preferredInsteadMap { + assert.Equalf(t, preferredInsteadConflict.ID, Spend.PreferredInstead().ID, "Spend %s should prefer %s instead of %s", Spend.ID, preferredInsteadConflict.ID, Spend.PreferredInstead().ID) } } diff --git a/pkg/protocol/engine/mempool/spenddag/spenddagv1/spenddag.go b/pkg/protocol/engine/mempool/spenddag/spenddagv1/spenddag.go index 3bbfe3326..434ff89b3 100644 --- a/pkg/protocol/engine/mempool/spenddag/spenddagv1/spenddag.go +++ b/pkg/protocol/engine/mempool/spenddag/spenddagv1/spenddag.go @@ -223,8 +223,8 @@ func (c *SpendDAG[SpendID, ResourceID, VoteRank]) ConflictingSpends(spendID Spen } conflictingSpends = ds.NewSet[SpendID]() - spend.ConflictingSpends.Range(func(conflictingConflict *Spend[SpendID, ResourceID, VoteRank]) { - conflictingSpends.Add(conflictingConflict.ID) + spend.ConflictingSpends.Range(func(conflictingSpend *Spend[SpendID, ResourceID, VoteRank]) { + conflictingSpends.Add(conflictingSpend.ID) }) return conflictingSpends, true diff --git a/pkg/protocol/engine/mempool/spenddag/spenddagv1/spenddag_test.go b/pkg/protocol/engine/mempool/spenddag/spenddagv1/spenddag_test.go index 51aee6d30..1dc2775f3 100644 --- a/pkg/protocol/engine/mempool/spenddag/spenddagv1/spenddag_test.go +++ b/pkg/protocol/engine/mempool/spenddag/spenddagv1/spenddag_test.go @@ -53,13 +53,13 @@ func TestMemoryRelease(t *testing.T) { //t.Skip("skip memory test as for some reason it's failing") tf := newTestFramework(t) - createSpendSets := func(startSlot, conflictSetCount, evictionDelay, conflictsInConflictSet int, prevConflictSetAlias string) (int, string) { + createSpendSets := func(startSlot, conflictSetCount, evictionDelay, spendsInConflictSet int, prevConflictSetAlias string) (int, string) { slot := startSlot for ; slot < startSlot+conflictSetCount; slot++ { conflictSetAlias := fmt.Sprintf("conflictSet-%d", slot) - for conflictIndex := 0; conflictIndex < conflictsInConflictSet; conflictIndex++ { + for conflictIndex := 0; conflictIndex < spendsInConflictSet; conflictIndex++ { conflictAlias := fmt.Sprintf("conflictSet-%d:%d", slot, conflictIndex) - require.NoError(t, tf.CreateOrUpdateConflict(conflictAlias, []string{conflictSetAlias})) + require.NoError(t, tf.CreateOrUpdateSpend(conflictAlias, []string{conflictSetAlias})) if prevConflictSetAlias != "" { require.NoError(t, tf.UpdateSpendParents(conflictAlias, []string{fmt.Sprintf("%s:%d", prevConflictSetAlias, 0)}, []string{})) } @@ -67,7 +67,7 @@ func TestMemoryRelease(t *testing.T) { prevConflictSetAlias = conflictSetAlias if slotToEvict := slot - evictionDelay; slotToEvict >= 0 { - for conflictIndex := 0; conflictIndex < conflictsInConflictSet; conflictIndex++ { + for conflictIndex := 0; conflictIndex < spendsInConflictSet; conflictIndex++ { conflictAlias := fmt.Sprintf("conflictSet-%d:%d", slotToEvict, conflictIndex) tf.EvictSpend(conflictAlias) } diff --git a/pkg/protocol/engine/mempool/spenddag/spenddagv1/utils.go b/pkg/protocol/engine/mempool/spenddag/spenddagv1/utils.go index 9359454fa..093b5f687 100644 --- a/pkg/protocol/engine/mempool/spenddag/spenddagv1/utils.go +++ b/pkg/protocol/engine/mempool/spenddag/spenddagv1/utils.go @@ -6,7 +6,7 @@ import ( "github.com/iotaledger/iota-core/pkg/protocol/engine/mempool/spenddag" ) -// heaviestConflict returns the largest Conflict from the given Conflicts. +// heaviestConflict returns the largest Conflict from the given Spends. func heaviestConflict[SpendID, ResourceID spenddag.IDType, VoterPower spenddag.VoteRankType[VoterPower]](conflicts ds.Set[*Spend[SpendID, ResourceID, VoterPower]]) *Spend[SpendID, ResourceID, VoterPower] { var result *Spend[SpendID, ResourceID, VoterPower] conflicts.Range(func(conflict *Spend[SpendID, ResourceID, VoterPower]) { diff --git a/pkg/protocol/engine/mempool/spenddag/tests/assertions.go b/pkg/protocol/engine/mempool/spenddag/tests/assertions.go index a878b6e2d..23910df2f 100644 --- a/pkg/protocol/engine/mempool/spenddag/tests/assertions.go +++ b/pkg/protocol/engine/mempool/spenddag/tests/assertions.go @@ -9,79 +9,79 @@ type Assertions struct { f *Framework } -// Children asserts that the given conflict has the given children. -func (a *Assertions) Children(conflictAlias string, childAliases ...string) { - childIDs, exists := a.f.Instance.SpendChildren(a.f.SpendID(conflictAlias)) - require.True(a.f.test, exists, "Conflict %s does not exist", conflictAlias) +// Children asserts that the given spend has the given children. +func (a *Assertions) Children(spendAlias string, childAliases ...string) { + childIDs, exists := a.f.Instance.SpendChildren(a.f.SpendID(spendAlias)) + require.True(a.f.test, exists, "Conflict %s does not exist", spendAlias) - require.Equal(a.f.test, len(childAliases), childIDs.Size(), "Conflict %s has wrong number of children", conflictAlias) + require.Equal(a.f.test, len(childAliases), childIDs.Size(), "Conflict %s has wrong number of children", spendAlias) for _, childAlias := range childAliases { - require.True(a.f.test, childIDs.Has(a.f.SpendID(childAlias)), "Conflict %s does not have child %s", conflictAlias, childAlias) + require.True(a.f.test, childIDs.Has(a.f.SpendID(childAlias)), "Conflict %s does not have child %s", spendAlias, childAlias) } } -// Parents asserts that the given conflict has the given parents. -func (a *Assertions) Parents(conflictAlias string, parentAliases ...string) { - parents, exists := a.f.Instance.SpendParents(a.f.SpendID(conflictAlias)) - require.True(a.f.test, exists, "Conflict %s does not exist", conflictAlias) +// Parents asserts that the given spend has the given parents. +func (a *Assertions) Parents(spendAlias string, parentAliases ...string) { + parents, exists := a.f.Instance.SpendParents(a.f.SpendID(spendAlias)) + require.True(a.f.test, exists, "Conflict %s does not exist", spendAlias) - require.Equal(a.f.test, len(parentAliases), parents.Size(), "Conflict %s has wrong number of parents", conflictAlias) + require.Equal(a.f.test, len(parentAliases), parents.Size(), "Conflict %s has wrong number of parents", spendAlias) for _, parentAlias := range parentAliases { - require.True(a.f.test, parents.Has(a.f.SpendID(parentAlias)), "Conflict %s does not have parent %s", conflictAlias, parentAlias) + require.True(a.f.test, parents.Has(a.f.SpendID(parentAlias)), "Conflict %s does not have parent %s", spendAlias, parentAlias) } } -// LikedInstead asserts that the given conflicts return the given LikedInstead conflicts. -func (a *Assertions) LikedInstead(conflictAliases []string, likedInsteadAliases ...string) { - likedInsteadConflicts := a.f.LikedInstead(conflictAliases...) +// LikedInstead asserts that the given spends return the given LikedInstead spends. +func (a *Assertions) LikedInstead(spendAliases []string, likedInsteadAliases ...string) { + likedInsteadConflicts := a.f.LikedInstead(spendAliases...) - require.Equal(a.f.test, len(likedInsteadAliases), likedInsteadConflicts.Size(), "LikedInstead returns wrong number of conflicts %d instead of %d", likedInsteadConflicts.Size(), len(likedInsteadAliases)) + require.Equal(a.f.test, len(likedInsteadAliases), likedInsteadConflicts.Size(), "LikedInstead returns wrong number of spends %d instead of %d", likedInsteadConflicts.Size(), len(likedInsteadAliases)) } -// ConflictSetMembers asserts that the given resource has the given conflict set members. -func (a *Assertions) ConflictSetMembers(resourceAlias string, conflictAliases ...string) { +// ConflictSetMembers asserts that the given resource has the given spend set members. +func (a *Assertions) ConflictSetMembers(resourceAlias string, spendAliases ...string) { conflictSetMembers, exists := a.f.Instance.ConflictSetMembers(a.f.ResourceID(resourceAlias)) require.True(a.f.test, exists, "Resource %s does not exist", resourceAlias) - require.Equal(a.f.test, len(conflictAliases), conflictSetMembers.Size(), "Resource %s has wrong number of parents", resourceAlias) - for _, conflictAlias := range conflictAliases { - require.True(a.f.test, conflictSetMembers.Has(a.f.SpendID(conflictAlias)), "Resource %s does not have parent %s", resourceAlias, conflictAlias) + require.Equal(a.f.test, len(spendAliases), conflictSetMembers.Size(), "Resource %s has wrong number of parents", resourceAlias) + for _, spendAlias := range spendAliases { + require.True(a.f.test, conflictSetMembers.Has(a.f.SpendID(spendAlias)), "Resource %s does not have parent %s", resourceAlias, spendAlias) } } -// ConflictSets asserts that the given conflict has the given conflict sets. -func (a *Assertions) ConflictSets(conflictAlias string, resourceAliases ...string) { - conflictSets, exists := a.f.Instance.ConflictSets(a.f.SpendID(conflictAlias)) - require.True(a.f.test, exists, "Conflict %s does not exist", conflictAlias) +// ConflictSets asserts that the given spend has the given conflict sets. +func (a *Assertions) ConflictSets(spendAlias string, resourceAliases ...string) { + conflictSets, exists := a.f.Instance.ConflictSets(a.f.SpendID(spendAlias)) + require.True(a.f.test, exists, "Conflict %s does not exist", spendAlias) - require.Equal(a.f.test, len(resourceAliases), conflictSets.Size(), "Conflict %s has wrong number of conflict sets", conflictAlias) + require.Equal(a.f.test, len(resourceAliases), conflictSets.Size(), "Conflict %s has wrong number of conflict sets", spendAlias) for _, resourceAlias := range resourceAliases { - require.True(a.f.test, conflictSets.Has(a.f.ResourceID(resourceAlias)), "Conflict %s does not have conflict set %s", conflictAlias, resourceAlias) + require.True(a.f.test, conflictSets.Has(a.f.ResourceID(resourceAlias)), "Conflict %s does not have conflict set %s", spendAlias, resourceAlias) } } -// Pending asserts that the given conflicts are pending. +// Pending asserts that the given spends are pending. func (a *Assertions) Pending(aliases ...string) { for _, alias := range aliases { require.True(a.f.test, a.f.Instance.AcceptanceState(a.f.SpendIDs(alias)).IsPending(), "Conflict %s is not pending", alias) } } -// Accepted asserts that the given conflicts are accepted. +// Accepted asserts that the given spends are accepted. func (a *Assertions) Accepted(aliases ...string) { for _, alias := range aliases { require.True(a.f.test, a.f.Instance.AcceptanceState(a.f.SpendIDs(alias)).IsAccepted(), "Conflict %s is not accepted", alias) } } -// Rejected asserts that the given conflicts are rejected. +// Rejected asserts that the given spends are rejected. func (a *Assertions) Rejected(aliases ...string) { for _, alias := range aliases { require.True(a.f.test, a.f.Instance.AcceptanceState(a.f.SpendIDs(alias)).IsRejected(), "Conflict %s is not rejected", alias) } } -// ValidatorWeight asserts that the given conflict has the given validator weight. -func (a *Assertions) ValidatorWeight(conflictAlias string, weight int64) { - require.Equal(a.f.test, weight, a.f.Instance.SpendWeight(a.f.SpendID(conflictAlias)), "ValidatorWeight is %s instead of % for conflict %s", a.f.Instance.SpendWeight(a.f.SpendID(conflictAlias)), weight, conflictAlias) +// ValidatorWeight asserts that the given spend has the given validator weight. +func (a *Assertions) ValidatorWeight(spendAlias string, weight int64) { + require.Equal(a.f.test, weight, a.f.Instance.SpendWeight(a.f.SpendID(spendAlias)), "ValidatorWeight is %s instead of % for spend %s", a.f.Instance.SpendWeight(a.f.SpendID(spendAlias)), weight, spendAlias) } diff --git a/pkg/protocol/engine/mempool/spenddag/tests/framework.go b/pkg/protocol/engine/mempool/spenddag/tests/framework.go index 58bfd21e6..35cf20aa7 100644 --- a/pkg/protocol/engine/mempool/spenddag/tests/framework.go +++ b/pkg/protocol/engine/mempool/spenddag/tests/framework.go @@ -52,23 +52,23 @@ func NewFramework( return f } -// CreateOrUpdateConflict creates a new conflict or adds it to the given ConflictSets. -func (f *Framework) CreateOrUpdateConflict(alias string, resourceAliases []string) error { +// CreateOrUpdateSpend creates a new spend or adds it to the given ConflictSets. +func (f *Framework) CreateOrUpdateSpend(alias string, resourceAliases []string) error { f.Instance.CreateSpend(f.SpendID(alias)) return f.Instance.UpdateConflictingResources(f.SpendID(alias), f.ConflictSetIDs(resourceAliases...)) } -// UpdateConflictParents updates the parents of the conflict with the given alias. -func (f *Framework) UpdateSpendParents(conflictAlias string, addedParentIDs []string, removedParentIDs []string) error { - return f.Instance.UpdateSpendParents(f.SpendID(conflictAlias), f.SpendIDs(addedParentIDs...), f.SpendIDs(removedParentIDs...)) +// UpdateConflictParents updates the parents of the spend with the given alias. +func (f *Framework) UpdateSpendParents(spendAlias string, addedParentIDs []string, removedParentIDs []string) error { + return f.Instance.UpdateSpendParents(f.SpendID(spendAlias), f.SpendIDs(addedParentIDs...), f.SpendIDs(removedParentIDs...)) } -// LikedInstead returns the set of conflicts that are liked instead of the given conflicts. -func (f *Framework) LikedInstead(conflictAliases ...string) ds.Set[iotago.TransactionID] { +// LikedInstead returns the set of spends that are liked instead of the given spends. +func (f *Framework) LikedInstead(spendAliases ...string) ds.Set[iotago.TransactionID] { var result ds.Set[iotago.TransactionID] _ = f.Instance.ReadConsistent(func(spendDAG spenddag.ReadLockedSpendDAG[iotago.TransactionID, iotago.OutputID, vote.MockedRank]) error { - result = spendDAG.LikedInstead(f.SpendIDs(conflictAliases...)) + result = spendDAG.LikedInstead(f.SpendIDs(spendAliases...)) return nil }) @@ -76,19 +76,19 @@ func (f *Framework) LikedInstead(conflictAliases ...string) ds.Set[iotago.Transa return result } -// CastVotes casts the given votes for the given conflicts. -func (f *Framework) CastVotes(nodeAlias string, voteRank int, conflictAliases ...string) error { +// CastVotes casts the given votes for the given spends. +func (f *Framework) CastVotes(nodeAlias string, voteRank int, spendAliases ...string) error { seat, exists := f.Accounts.Get(nodeAlias) if !exists { return ierrors.Errorf("node with alias '%s' does not have a seat in the committee", nodeAlias) } - return f.Instance.CastVotes(vote.NewVote[vote.MockedRank](seat, vote.MockedRank(voteRank)), f.SpendIDs(conflictAliases...)) + return f.Instance.CastVotes(vote.NewVote[vote.MockedRank](seat, vote.MockedRank(voteRank)), f.SpendIDs(spendAliases...)) } -// EvictConflict evicts given conflict from the SpendDAG. -func (f *Framework) EvictSpend(conflictAlias string) { - f.Instance.EvictSpend(f.SpendID(conflictAlias)) +// EvictSpend evicts given spend from the SpendDAG. +func (f *Framework) EvictSpend(spendAlias string) { + f.Instance.EvictSpend(f.SpendID(spendAlias)) } // SpendIDs translates the given aliases into an AdvancedSet of iotago.TransactionIDs. diff --git a/pkg/protocol/engine/mempool/spenddag/tests/tests.go b/pkg/protocol/engine/mempool/spenddag/tests/tests.go index 59e97adfc..d819492ef 100644 --- a/pkg/protocol/engine/mempool/spenddag/tests/tests.go +++ b/pkg/protocol/engine/mempool/spenddag/tests/tests.go @@ -32,17 +32,17 @@ func TestAll(t *testing.T, frameworkProvider func(*testing.T) *Framework) { } func ExistingSpendJoinsConflictSets(t *testing.T, tf *Framework) { - require.NoError(t, tf.CreateOrUpdateConflict("conflict1", []string{"resource1"})) - require.NoError(t, tf.CreateOrUpdateConflict("conflict2", []string{"resource1"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict1", []string{"resource1"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict2", []string{"resource1"})) tf.Assert.ConflictSetMembers("resource1", "conflict1", "conflict2") - require.NoError(t, tf.CreateOrUpdateConflict("conflict3", []string{"resource2"})) - require.NoError(t, tf.CreateOrUpdateConflict("conflict1", []string{"resource2"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict3", []string{"resource2"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict1", []string{"resource2"})) tf.Assert.ConflictSetMembers("resource2", "conflict1", "conflict3") tf.Assert.ConflictSetMembers("resource1", "conflict1", "conflict2") - require.NoError(t, tf.CreateOrUpdateConflict("conflict2", []string{"resource2"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict2", []string{"resource2"})) tf.Assert.ConflictSetMembers("resource2", "conflict1", "conflict2", "conflict3") tf.Assert.ConflictSetMembers("resource1", "conflict1", "conflict2") @@ -51,15 +51,15 @@ func ExistingSpendJoinsConflictSets(t *testing.T, tf *Framework) { } func UpdateSpendParents(t *testing.T, tf *Framework) { - require.NoError(t, tf.CreateOrUpdateConflict("conflict1", []string{"resource1"})) - require.NoError(t, tf.CreateOrUpdateConflict("conflict2", []string{"resource2"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict1", []string{"resource1"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict2", []string{"resource2"})) - require.NoError(t, tf.CreateOrUpdateConflict("conflict3", []string{"resource1", "resource2"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict3", []string{"resource1", "resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict3", []string{"conflict1", "conflict2"}, []string{})) tf.Assert.Children("conflict1", "conflict3") tf.Assert.Parents("conflict3", "conflict1", "conflict2") - require.NoError(t, tf.CreateOrUpdateConflict("conflict2.5", []string{"conflict2.5"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict2.5", []string{"conflict2.5"})) require.NoError(t, tf.UpdateSpendParents("conflict2.5", []string{"conflict1", "conflict2"}, []string{})) tf.Assert.Children("conflict1", "conflict2.5", "conflict3") tf.Assert.Children("conflict2", "conflict2.5", "conflict3") @@ -75,14 +75,14 @@ func UpdateSpendParents(t *testing.T, tf *Framework) { } func CreateSpend(t *testing.T, tf *Framework) { - require.NoError(t, tf.CreateOrUpdateConflict("conflict1", []string{"resource1"})) - require.NoError(t, tf.CreateOrUpdateConflict("conflict2", []string{"resource1"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict1", []string{"resource1"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict2", []string{"resource1"})) tf.Assert.ConflictSetMembers("resource1", "conflict1", "conflict2") - require.NoError(t, tf.CreateOrUpdateConflict("conflict3", []string{"resource2"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict3", []string{"resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict3", []string{"conflict1"}, []string{})) - require.NoError(t, tf.CreateOrUpdateConflict("conflict4", []string{"resource2"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict4", []string{"resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict4", []string{"conflict1"}, []string{})) tf.Assert.ConflictSetMembers("resource2", "conflict3", "conflict4") @@ -99,8 +99,8 @@ func CreateSpendWithoutMembers(t *testing.T, tf *Framework) { // Non-conflicting conflicts { - require.NoError(t, tf.CreateOrUpdateConflict("conflict1", []string{"resource1"})) - require.NoError(t, tf.CreateOrUpdateConflict("conflict2", []string{"resource2"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict1", []string{"resource1"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict2", []string{"resource2"})) tf.Assert.ConflictSetMembers("resource1", "conflict1") tf.Assert.ConflictSetMembers("resource2", "conflict2") @@ -118,8 +118,8 @@ func CreateSpendWithoutMembers(t *testing.T, tf *Framework) { // Regular conflict { - require.NoError(t, tf.CreateOrUpdateConflict("conflict3", []string{"resource3"})) - require.NoError(t, tf.CreateOrUpdateConflict("conflict4", []string{"resource3"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict3", []string{"resource3"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict4", []string{"resource3"})) tf.Assert.ConflictSetMembers("resource3", "conflict3", "conflict4") @@ -135,16 +135,16 @@ func CreateSpendWithoutMembers(t *testing.T, tf *Framework) { func LikedInstead(t *testing.T, tf *Framework) { tf.Accounts.CreateID("zero-weight") - require.NoError(t, tf.CreateOrUpdateConflict("conflict1", []string{"resource1"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict1", []string{"resource1"})) require.NoError(t, tf.CastVotes("zero-weight", 1, "conflict1")) - require.NoError(t, tf.CreateOrUpdateConflict("conflict2", []string{"resource1"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict2", []string{"resource1"})) tf.Assert.ConflictSetMembers("resource1", "conflict1", "conflict2") tf.Assert.LikedInstead([]string{"conflict1", "conflict2"}, "conflict1") - require.NoError(t, tf.CreateOrUpdateConflict("conflict3", []string{"resource2"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict3", []string{"resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict3", []string{"conflict1"}, []string{})) - require.NoError(t, tf.CreateOrUpdateConflict("conflict4", []string{"resource2"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict4", []string{"resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict4", []string{"conflict1"}, []string{})) require.NoError(t, tf.CastVotes("zero-weight", 1, "conflict4")) @@ -157,16 +157,16 @@ func SpendAcceptance(t *testing.T, tf *Framework) { tf.Accounts.CreateID("nodeID3") tf.Accounts.CreateID("nodeID4") - require.NoError(t, tf.CreateOrUpdateConflict("conflict1", []string{"resource1"})) - require.NoError(t, tf.CreateOrUpdateConflict("conflict2", []string{"resource1"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict1", []string{"resource1"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict2", []string{"resource1"})) tf.Assert.ConflictSetMembers("resource1", "conflict1", "conflict2") tf.Assert.ConflictSets("conflict1", "resource1") tf.Assert.ConflictSets("conflict2", "resource1") - require.NoError(t, tf.CreateOrUpdateConflict("conflict3", []string{"resource2"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict3", []string{"resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict3", []string{"conflict1"}, []string{})) - require.NoError(t, tf.CreateOrUpdateConflict("conflict4", []string{"resource2"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict4", []string{"resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict4", []string{"conflict1"}, []string{})) tf.Assert.ConflictSetMembers("resource2", "conflict3", "conflict4") @@ -192,16 +192,16 @@ func CastVotes(t *testing.T, tf *Framework) { tf.Accounts.CreateID("nodeID3") tf.Accounts.CreateID("nodeID4") - require.NoError(t, tf.CreateOrUpdateConflict("conflict1", []string{"resource1"})) - require.NoError(t, tf.CreateOrUpdateConflict("conflict2", []string{"resource1"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict1", []string{"resource1"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict2", []string{"resource1"})) tf.Assert.ConflictSetMembers("resource1", "conflict1", "conflict2") tf.Assert.ConflictSets("conflict1", "resource1") tf.Assert.ConflictSets("conflict2", "resource1") - require.NoError(t, tf.CreateOrUpdateConflict("conflict3", []string{"resource2"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict3", []string{"resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict3", []string{"conflict1"}, []string{})) - require.NoError(t, tf.CreateOrUpdateConflict("conflict4", []string{"resource2"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict4", []string{"resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict4", []string{"conflict1"}, []string{})) tf.Assert.ConflictSetMembers("resource2", "conflict3", "conflict4") @@ -227,17 +227,17 @@ func CastVotesVoteRank(t *testing.T, tf *Framework) { tf.Accounts.CreateID("nodeID2") tf.Accounts.CreateID("nodeID3") - require.NoError(t, tf.CreateOrUpdateConflict("conflict1", []string{"resource1"})) - require.NoError(t, tf.CreateOrUpdateConflict("conflict2", []string{"resource1"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict1", []string{"resource1"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict2", []string{"resource1"})) tf.Assert.ConflictSetMembers("resource1", "conflict1", "conflict2") tf.Assert.ConflictSets("conflict1", "resource1") tf.Assert.ConflictSets("conflict2", "resource1") // create nested conflicts - require.NoError(t, tf.CreateOrUpdateConflict("conflict3", []string{"resource2"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict3", []string{"resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict3", []string{"conflict1"}, []string{})) - require.NoError(t, tf.CreateOrUpdateConflict("conflict4", []string{"resource2"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict4", []string{"resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict4", []string{"conflict1"}, []string{})) tf.Assert.ConflictSetMembers("resource2", "conflict3", "conflict4") @@ -281,16 +281,16 @@ func CastVotesAcceptance(t *testing.T, tf *Framework) { tf.Accounts.CreateID("nodeID3") tf.Accounts.CreateID("nodeID4") - require.NoError(t, tf.CreateOrUpdateConflict("conflict1", []string{"resource1"})) - require.NoError(t, tf.CreateOrUpdateConflict("conflict2", []string{"resource1"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict1", []string{"resource1"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict2", []string{"resource1"})) tf.Assert.ConflictSetMembers("resource1", "conflict1", "conflict2") tf.Assert.ConflictSets("conflict1", "resource1") tf.Assert.ConflictSets("conflict2", "resource1") - require.NoError(t, tf.CreateOrUpdateConflict("conflict3", []string{"resource2"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict3", []string{"resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict3", []string{"conflict1"}, []string{})) - require.NoError(t, tf.CreateOrUpdateConflict("conflict4", []string{"resource2"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict4", []string{"resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict4", []string{"conflict1"}, []string{})) tf.Assert.ConflictSetMembers("resource2", "conflict3", "conflict4") @@ -307,7 +307,7 @@ func CastVotesAcceptance(t *testing.T, tf *Framework) { tf.Assert.Accepted("conflict3") tf.Assert.Rejected("conflict4") - require.NoError(t, tf.CreateOrUpdateConflict("conflict5", []string{"resource1"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict5", []string{"resource1"})) tf.Assert.Rejected("conflict5") // Evict conflict and try to add non-existing parent to a rejected conflict - update is ignored because the parent is evicted. @@ -332,22 +332,22 @@ func JoinConflictSetTwice(t *testing.T, tf *Framework) { resourceAddedEventCount++ }) - require.NoError(t, tf.CreateOrUpdateConflict("conflict1", []string{"resource1"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict1", []string{"resource1"})) require.Equal(t, 1, conflictCreatedEventCount) require.Equal(t, 1, resourceAddedEventCount) tf.Assert.ConflictSets("conflict1", "resource1") - require.NoError(t, tf.CreateOrUpdateConflict("conflict1", []string{"resource2"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict1", []string{"resource2"})) require.Equal(t, 1, conflictCreatedEventCount) require.Equal(t, 2, resourceAddedEventCount) tf.Assert.ConflictSets("conflict1", "resource1", "resource2") - require.NoError(t, tf.CreateOrUpdateConflict("conflict1", []string{"resource1", "resource2"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict1", []string{"resource1", "resource2"})) require.Equal(t, 1, conflictCreatedEventCount) require.Equal(t, 2, resourceAddedEventCount) tf.Assert.ConflictSets("conflict1", "resource1", "resource2") - require.NoError(t, tf.CreateOrUpdateConflict("conflict1", []string{"resource1", "resource2", "resource3", "resource4"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict1", []string{"resource1", "resource2", "resource3", "resource4"})) require.Equal(t, 1, conflictCreatedEventCount) require.Equal(t, 3, resourceAddedEventCount) tf.Assert.ConflictSets("conflict1", "resource1", "resource2", "resource3", "resource4") @@ -359,16 +359,16 @@ func EvictAcceptedSpend(t *testing.T, tf *Framework) { tf.Accounts.CreateID("nodeID3") tf.Accounts.CreateID("nodeID4") - require.NoError(t, tf.CreateOrUpdateConflict("conflict1", []string{"resource1"})) - require.NoError(t, tf.CreateOrUpdateConflict("conflict2", []string{"resource1"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict1", []string{"resource1"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict2", []string{"resource1"})) tf.Assert.ConflictSetMembers("resource1", "conflict1", "conflict2") tf.Assert.ConflictSets("conflict1", "resource1") tf.Assert.ConflictSets("conflict2", "resource1") - require.NoError(t, tf.CreateOrUpdateConflict("conflict3", []string{"resource2"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict3", []string{"resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict3", []string{"conflict1"}, []string{})) - require.NoError(t, tf.CreateOrUpdateConflict("conflict4", []string{"resource2"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict4", []string{"resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict4", []string{"conflict1"}, []string{})) tf.Assert.ConflictSetMembers("resource2", "conflict3", "conflict4") @@ -376,10 +376,10 @@ func EvictAcceptedSpend(t *testing.T, tf *Framework) { tf.Assert.Parents("conflict3", "conflict1") tf.Assert.Parents("conflict4", "conflict1") - require.NoError(t, tf.CreateOrUpdateConflict("conflict5", []string{"resource3"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict5", []string{"resource3"})) require.NoError(t, tf.UpdateSpendParents("conflict5", []string{"conflict2"}, []string{})) - require.NoError(t, tf.CreateOrUpdateConflict("conflict6", []string{"resource3"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict6", []string{"resource3"})) require.NoError(t, tf.UpdateSpendParents("conflict6", []string{"conflict2"}, []string{})) tf.Assert.ConflictSetMembers("resource3", "conflict5", "conflict6") @@ -425,16 +425,16 @@ func EvictRejectedSpend(t *testing.T, tf *Framework) { tf.Accounts.CreateID("nodeID3") tf.Accounts.CreateID("nodeID4") - require.NoError(t, tf.CreateOrUpdateConflict("conflict1", []string{"resource1"})) - require.NoError(t, tf.CreateOrUpdateConflict("conflict2", []string{"resource1"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict1", []string{"resource1"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict2", []string{"resource1"})) tf.Assert.ConflictSetMembers("resource1", "conflict1", "conflict2") tf.Assert.ConflictSets("conflict1", "resource1") tf.Assert.ConflictSets("conflict2", "resource1") - require.NoError(t, tf.CreateOrUpdateConflict("conflict3", []string{"resource2"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict3", []string{"resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict3", []string{"conflict1"}, []string{})) - require.NoError(t, tf.CreateOrUpdateConflict("conflict4", []string{"resource2"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict4", []string{"resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict4", []string{"conflict1"}, []string{})) tf.Assert.ConflictSetMembers("resource2", "conflict3", "conflict4") @@ -442,10 +442,10 @@ func EvictRejectedSpend(t *testing.T, tf *Framework) { tf.Assert.Parents("conflict3", "conflict1") tf.Assert.Parents("conflict4", "conflict1") - require.NoError(t, tf.CreateOrUpdateConflict("conflict5", []string{"resource3"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict5", []string{"resource3"})) require.NoError(t, tf.UpdateSpendParents("conflict5", []string{"conflict2"}, []string{})) - require.NoError(t, tf.CreateOrUpdateConflict("conflict6", []string{"resource3"})) + require.NoError(t, tf.CreateOrUpdateSpend("conflict6", []string{"resource3"})) require.NoError(t, tf.UpdateSpendParents("conflict6", []string{"conflict2"}, []string{})) tf.Assert.ConflictSetMembers("resource3", "conflict5", "conflict6") diff --git a/pkg/protocol/engine/mempool/tests/testframework.go b/pkg/protocol/engine/mempool/tests/testframework.go index d8e5de00a..b8225b90f 100644 --- a/pkg/protocol/engine/mempool/tests/testframework.go +++ b/pkg/protocol/engine/mempool/tests/testframework.go @@ -243,16 +243,16 @@ func (t *TestFramework) RequireTransactionsEvicted(transactionAliases map[string } } -func (t *TestFramework) RequireSpendIDs(conflictMapping map[string][]string) { - for transactionAlias, conflictAliases := range conflictMapping { +func (t *TestFramework) RequireSpendIDs(spendMapping map[string][]string) { + for transactionAlias, spendAliases := range spendMapping { transactionMetadata, exists := t.Instance.TransactionMetadata(t.TransactionID(transactionAlias)) require.True(t.test, exists, "transaction %s does not exist", transactionAlias) spendIDs := transactionMetadata.SpendIDs() - require.Equal(t.test, len(conflictAliases), spendIDs.Size(), "%s has wrong number of SpendIDs", transactionAlias) + require.Equal(t.test, len(spendAliases), spendIDs.Size(), "%s has wrong number of SpendIDs", transactionAlias) - for _, conflictAlias := range conflictAliases { - require.True(t.test, spendIDs.Has(t.TransactionID(conflictAlias)), "transaction %s should have conflict %s, instead had %s", transactionAlias, conflictAlias, spendIDs) + for _, spendAlias := range spendAliases { + require.True(t.test, spendIDs.Has(t.TransactionID(spendAlias)), "transaction %s should have spend %s, instead had %s", transactionAlias, spendAlias, spendIDs) } } } diff --git a/pkg/protocol/engine/mempool/v1/inclusion_flags.go b/pkg/protocol/engine/mempool/v1/inclusion_flags.go index f97680af0..9e59a3653 100644 --- a/pkg/protocol/engine/mempool/v1/inclusion_flags.go +++ b/pkg/protocol/engine/mempool/v1/inclusion_flags.go @@ -27,7 +27,7 @@ func newInclusionFlags() *inclusionFlags { accepted: reactive.NewVariable[bool](), committedSlot: reactive.NewVariable[iotago.SlotIndex](), rejected: promise.NewEvent(), - // Make sure the oldest orphaned index doesn't get overridden by newer TX spending the orphaned conflict further. + // Make sure the oldest orphaned index doesn't get overridden by newer TX spending the orphaned spend further. orphanedSlot: reactive.NewVariable[iotago.SlotIndex](func(currentValue iotago.SlotIndex, newValue iotago.SlotIndex) iotago.SlotIndex { if currentValue != 0 { return currentValue