From f63865cc9b611ea2e05090a8d7087348403ecbbb Mon Sep 17 00:00:00 2001 From: jkrvivian Date: Mon, 20 Nov 2023 20:37:35 +0800 Subject: [PATCH] Rename ConflictSet to SpendSet --- .../engine/mempool/spenddag/errors.go | 8 +- .../engine/mempool/spenddag/events.go | 2 +- .../engine/mempool/spenddag/spenddag.go | 4 +- .../spenddag/spenddagv1/conflict_set.go | 72 ---------- .../spenddag/spenddagv1/conflict_set_test.go | 10 -- .../mempool/spenddag/spenddagv1/spend.go | 30 ++-- .../mempool/spenddag/spenddagv1/spend_set.go | 73 ++++++++++ .../spenddag/spenddagv1/spend_set_test.go | 10 ++ .../mempool/spenddag/spenddagv1/spend_test.go | 58 ++++---- .../mempool/spenddag/spenddagv1/spenddag.go | 60 ++++---- .../spenddag/spenddagv1/spenddag_test.go | 26 ++-- .../mempool/spenddag/tests/assertions.go | 20 +-- .../mempool/spenddag/tests/framework.go | 14 +- .../engine/mempool/spenddag/tests/tests.go | 130 +++++++++--------- pkg/protocol/engine/mempool/tests/tests.go | 24 ++-- 15 files changed, 271 insertions(+), 270 deletions(-) delete mode 100644 pkg/protocol/engine/mempool/spenddag/spenddagv1/conflict_set.go delete mode 100644 pkg/protocol/engine/mempool/spenddag/spenddagv1/conflict_set_test.go create mode 100644 pkg/protocol/engine/mempool/spenddag/spenddagv1/spend_set.go create mode 100644 pkg/protocol/engine/mempool/spenddag/spenddagv1/spend_set_test.go diff --git a/pkg/protocol/engine/mempool/spenddag/errors.go b/pkg/protocol/engine/mempool/spenddag/errors.go index beb901f92..852d78bcf 100644 --- a/pkg/protocol/engine/mempool/spenddag/errors.go +++ b/pkg/protocol/engine/mempool/spenddag/errors.go @@ -3,8 +3,8 @@ package spenddag import "github.com/iotaledger/hive.go/ierrors" var ( - ErrExpected = ierrors.New("expected error") - ErrAlreadyPartOfConflictSet = ierrors.New("spend already part of ConflictSet") - ErrEntityEvicted = ierrors.New("tried to operate on evicted entity") - ErrFatal = ierrors.New("fatal error") + ErrExpected = ierrors.New("expected error") + ErrAlreadyPartOfSpendSet = ierrors.New("spend already part of SpendSet") + ErrEntityEvicted = ierrors.New("tried to operate on evicted entity") + ErrFatal = ierrors.New("fatal error") ) diff --git a/pkg/protocol/engine/mempool/spenddag/events.go b/pkg/protocol/engine/mempool/spenddag/events.go index 1165b51e0..a66400081 100644 --- a/pkg/protocol/engine/mempool/spenddag/events.go +++ b/pkg/protocol/engine/mempool/spenddag/events.go @@ -15,7 +15,7 @@ type Events[SpendID, ResourceID comparable] struct { // SpendEvicted is triggered when a Spend is evicted from the SpendDAG. SpendEvicted *event.Event1[SpendID] - // ConflictingResourcesAdded is triggered when the Spend is added to a new ConflictSet. + // ConflictingResourcesAdded is triggered when the Spend is added to a new SpendSet. ConflictingResourcesAdded *event.Event2[SpendID, ds.Set[ResourceID]] // SpendParentsUpdated is triggered when the parents of a Spend are updated. diff --git a/pkg/protocol/engine/mempool/spenddag/spenddag.go b/pkg/protocol/engine/mempool/spenddag/spenddag.go index 74d0eda1a..7682773b1 100644 --- a/pkg/protocol/engine/mempool/spenddag/spenddag.go +++ b/pkg/protocol/engine/mempool/spenddag/spenddag.go @@ -25,9 +25,9 @@ type SpendDAG[SpendID, ResourceID IDType, VoteRank VoteRankType[VoteRank]] inter AllSpendsSupported(seat account.SeatIndex, spendIDs ds.Set[SpendID]) bool EvictSpend(spendID SpendID) - ConflictSets(spendID SpendID) (conflictSetIDs ds.Set[ResourceID], exists bool) + SpendSets(spendID SpendID) (spendSetIDs ds.Set[ResourceID], exists bool) SpendParents(spendID SpendID) (spendIDs ds.Set[SpendID], exists bool) - ConflictSetMembers(conflictSetID ResourceID) (spendIDs ds.Set[SpendID], exists bool) + SpendSetMembers(spendSetID ResourceID) (spendIDs ds.Set[SpendID], exists bool) SpendWeight(spendID SpendID) int64 SpendChildren(spendID SpendID) (spendIDs ds.Set[SpendID], exists bool) SpendVoters(spendID SpendID) (voters ds.Set[account.SeatIndex]) diff --git a/pkg/protocol/engine/mempool/spenddag/spenddagv1/conflict_set.go b/pkg/protocol/engine/mempool/spenddag/spenddagv1/conflict_set.go deleted file mode 100644 index a891f7d5d..000000000 --- a/pkg/protocol/engine/mempool/spenddag/spenddagv1/conflict_set.go +++ /dev/null @@ -1,72 +0,0 @@ -package spenddagv1 - -import ( - "github.com/iotaledger/hive.go/ds" - "github.com/iotaledger/hive.go/ds/reactive" - "github.com/iotaledger/hive.go/ierrors" - "github.com/iotaledger/hive.go/runtime/syncutils" - "github.com/iotaledger/iota-core/pkg/protocol/engine/mempool/spenddag" -) - -// ConflictSet represents a set of Spends that are conflicting with each other over a common Resource. -type ConflictSet[SpendID, ResourceID spenddag.IDType, VoteRank spenddag.VoteRankType[VoteRank]] struct { - // ID is the ID of the Resource that the Spends in this ConflictSet are conflicting over. - ID ResourceID - - // members is the set of Spends that are conflicting over the shared resource. - members ds.Set[*Spend[SpendID, ResourceID, VoteRank]] - - allMembersEvicted reactive.Variable[bool] - - mutex syncutils.RWMutex -} - -// NewConflictSet creates a new ConflictSet of Spends that are conflicting with each other over the given Resource. -func NewConflictSet[SpendID, ResourceID spenddag.IDType, VoteRank spenddag.VoteRankType[VoteRank]](id ResourceID) *ConflictSet[SpendID, ResourceID, VoteRank] { - return &ConflictSet[SpendID, ResourceID, VoteRank]{ - ID: id, - allMembersEvicted: reactive.NewVariable[bool](), - members: ds.NewSet[*Spend[SpendID, ResourceID, VoteRank]](), - } -} - -// Add adds a Spend to the ConflictSet and returns all other members of the set. -func (c *ConflictSet[SpendID, ResourceID, VoteRank]) Add(addedConflict *Spend[SpendID, ResourceID, VoteRank]) (otherMembers ds.Set[*Spend[SpendID, ResourceID, VoteRank]], err error) { - c.mutex.Lock() - defer c.mutex.Unlock() - - if c.allMembersEvicted.Get() { - return nil, ierrors.New("cannot join a ConflictSet whose all members are evicted") - } - - if otherMembers = c.members.Clone(); !c.members.Add(addedConflict) { - return nil, spenddag.ErrAlreadyPartOfConflictSet - } - - return otherMembers, nil - -} - -// Remove removes a Spend from the ConflictSet and returns all remaining members of the set. -func (c *ConflictSet[SpendID, ResourceID, VoteRank]) Remove(removedConflict *Spend[SpendID, ResourceID, VoteRank]) (removed bool) { - c.mutex.Lock() - defer c.mutex.Unlock() - - if removed = c.members.Delete(removedConflict); removed && c.members.IsEmpty() { - c.allMembersEvicted.Set(true) - } - - return removed -} - -func (c *ConflictSet[SpendID, ResourceID, VoteRank]) ForEach(callback func(parent *Spend[SpendID, ResourceID, VoteRank]) error) error { - c.mutex.RLock() - defer c.mutex.RUnlock() - - return c.members.ForEach(callback) -} - -// OnAllMembersEvicted executes a callback when all members of the ConflictSet are evicted and the ConflictSet itself can be evicted. -func (c *ConflictSet[SpendID, ResourceID, VoteRank]) OnAllMembersEvicted(callback func(prevValue, newValue bool)) { - c.allMembersEvicted.OnUpdate(callback) -} diff --git a/pkg/protocol/engine/mempool/spenddag/spenddagv1/conflict_set_test.go b/pkg/protocol/engine/mempool/spenddag/spenddagv1/conflict_set_test.go deleted file mode 100644 index 8d2bf2a40..000000000 --- a/pkg/protocol/engine/mempool/spenddag/spenddagv1/conflict_set_test.go +++ /dev/null @@ -1,10 +0,0 @@ -package spenddagv1 - -import ( - "github.com/iotaledger/iota-core/pkg/core/vote" - iotago "github.com/iotaledger/iota.go/v4" -) - -type TestConflictSet = *ConflictSet[iotago.TransactionID, iotago.OutputID, vote.MockedRank] - -var NewTestConflictSet = NewConflictSet[iotago.TransactionID, iotago.OutputID, vote.MockedRank] diff --git a/pkg/protocol/engine/mempool/spenddag/spenddagv1/spend.go b/pkg/protocol/engine/mempool/spenddag/spenddagv1/spend.go index 72728314d..054b1c840 100644 --- a/pkg/protocol/engine/mempool/spenddag/spenddagv1/spend.go +++ b/pkg/protocol/engine/mempool/spenddag/spenddagv1/spend.go @@ -30,8 +30,8 @@ type Spend[SpendID, ResourceID spenddag.IDType, VoteRank spenddag.VoteRankType[V // Children is the set of children of the Spend. Children ds.Set[*Spend[SpendID, ResourceID, VoteRank]] - // ConflictSets is the set of ConflictSets that the Spend is part of. - ConflictSets ds.Set[*ConflictSet[SpendID, ResourceID, VoteRank]] + // SpendSets is the set of SpendSets that the Spend is part of. + SpendSets ds.Set[*SpendSet[SpendID, ResourceID, VoteRank]] // ConflictingSpends is the set of spends that directly conflict with the Spend. ConflictingSpends *SortedSpends[SpendID, ResourceID, VoteRank] @@ -95,7 +95,7 @@ func NewSpend[SpendID, ResourceID spenddag.IDType, VoteRank spenddag.VoteRankTyp ID: id, Parents: ds.NewSet[*Spend[SpendID, ResourceID, VoteRank]](), Children: ds.NewSet[*Spend[SpendID, ResourceID, VoteRank]](), - ConflictSets: ds.NewSet[*ConflictSet[SpendID, ResourceID, VoteRank]](), + SpendSets: ds.NewSet[*SpendSet[SpendID, ResourceID, VoteRank]](), Weight: initialWeight, LatestVotes: shrinkingmap.New[account.SeatIndex, *vote.Vote[VoteRank]](), AcceptanceStateUpdated: event.New2[acceptance.State, acceptance.State](), @@ -127,9 +127,9 @@ func NewSpend[SpendID, ResourceID spenddag.IDType, VoteRank spenddag.VoteRankTyp return c } -// JoinSpendSets 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 { +// JoinSpendSets registers the Spend with the given SpendSets. +func (c *Spend[SpendID, ResourceID, VoteRank]) JoinSpendSets(spendSets ds.Set[*SpendSet[SpendID, ResourceID, VoteRank]]) (joinedSpendSets ds.Set[ResourceID], err error) { + if spendSets == nil { return ds.NewSet[ResourceID](), nil } @@ -148,22 +148,22 @@ func (c *Spend[SpendID, ResourceID, VoteRank]) JoinSpendSets(conflictSets ds.Set } } - joinedConflictSets = ds.NewSet[ResourceID]() + joinedSpendSets = ds.NewSet[ResourceID]() - return joinedConflictSets, conflictSets.ForEach(func(conflictSet *ConflictSet[SpendID, ResourceID, VoteRank]) error { - otherConflicts, err := conflictSet.Add(c) - if err != nil && !ierrors.Is(err, spenddag.ErrAlreadyPartOfConflictSet) { + return joinedSpendSets, spendSets.ForEach(func(spendSet *SpendSet[SpendID, ResourceID, VoteRank]) error { + otherConflicts, err := spendSet.Add(c) + if err != nil && !ierrors.Is(err, spenddag.ErrAlreadyPartOfSpendSet) { return err } - if c.ConflictSets.Add(conflictSet) { + if c.SpendSets.Add(spendSet) { if otherConflicts != nil { otherConflicts.Range(func(otherConflict *Spend[SpendID, ResourceID, VoteRank]) { registerConflictingSpend(c, otherConflict) registerConflictingSpend(otherConflict, c) }) - joinedConflictSets.Add(conflictSet.ID) + joinedSpendSets.Add(spendSet.ID) } } @@ -314,10 +314,10 @@ func (c *Spend[SpendID, ResourceID, VoteRank]) Evict() (evictedSpends []SpendID) }) c.Parents.Clear() - c.ConflictSets.Range(func(conflictSet *ConflictSet[SpendID, ResourceID, VoteRank]) { - conflictSet.Remove(c) + c.SpendSets.Range(func(spendSet *SpendSet[SpendID, ResourceID, VoteRank]) { + spendSet.Remove(c) }) - c.ConflictSets.Clear() + c.SpendSets.Clear() for _, spend := range c.ConflictingSpends.Shutdown() { if spend != c { diff --git a/pkg/protocol/engine/mempool/spenddag/spenddagv1/spend_set.go b/pkg/protocol/engine/mempool/spenddag/spenddagv1/spend_set.go new file mode 100644 index 000000000..2768fa9f0 --- /dev/null +++ b/pkg/protocol/engine/mempool/spenddag/spenddagv1/spend_set.go @@ -0,0 +1,73 @@ +package spenddagv1 + +import ( + "github.com/iotaledger/hive.go/ds" + "github.com/iotaledger/hive.go/ds/reactive" + "github.com/iotaledger/hive.go/ierrors" + "github.com/iotaledger/hive.go/runtime/syncutils" + "github.com/iotaledger/iota-core/pkg/protocol/engine/mempool/spenddag" +) + +// SpendSet represents a set of Spends of a Resource. +// If there's more than 1 Spend in a SpendSet, they are conflicting with each other over the shared Resource. +type SpendSet[SpendID, ResourceID spenddag.IDType, VoteRank spenddag.VoteRankType[VoteRank]] struct { + // ID is the ID of the Resource that the Spends in this SpendSet are conflicting over. + ID ResourceID + + // members is the set of Spends that are conflicting over the shared resource. + members ds.Set[*Spend[SpendID, ResourceID, VoteRank]] + + allMembersEvicted reactive.Variable[bool] + + mutex syncutils.RWMutex +} + +// NewSpendSet creates a new SpendSet of Spends that are conflicting with each other over the given Resource. +func NewSpendSet[SpendID, ResourceID spenddag.IDType, VoteRank spenddag.VoteRankType[VoteRank]](id ResourceID) *SpendSet[SpendID, ResourceID, VoteRank] { + return &SpendSet[SpendID, ResourceID, VoteRank]{ + ID: id, + allMembersEvicted: reactive.NewVariable[bool](), + members: ds.NewSet[*Spend[SpendID, ResourceID, VoteRank]](), + } +} + +// Add adds a Spend to the SpendSet and returns all other members of the set. +func (c *SpendSet[SpendID, ResourceID, VoteRank]) Add(addedConflict *Spend[SpendID, ResourceID, VoteRank]) (otherMembers ds.Set[*Spend[SpendID, ResourceID, VoteRank]], err error) { + c.mutex.Lock() + defer c.mutex.Unlock() + + if c.allMembersEvicted.Get() { + return nil, ierrors.New("cannot join a SpendSet whose all members are evicted") + } + + if otherMembers = c.members.Clone(); !c.members.Add(addedConflict) { + return nil, spenddag.ErrAlreadyPartOfSpendSet + } + + return otherMembers, nil + +} + +// Remove removes a Spend from the SpendSet and returns all remaining members of the set. +func (c *SpendSet[SpendID, ResourceID, VoteRank]) Remove(removedConflict *Spend[SpendID, ResourceID, VoteRank]) (removed bool) { + c.mutex.Lock() + defer c.mutex.Unlock() + + if removed = c.members.Delete(removedConflict); removed && c.members.IsEmpty() { + c.allMembersEvicted.Set(true) + } + + return removed +} + +func (c *SpendSet[SpendID, ResourceID, VoteRank]) ForEach(callback func(parent *Spend[SpendID, ResourceID, VoteRank]) error) error { + c.mutex.RLock() + defer c.mutex.RUnlock() + + return c.members.ForEach(callback) +} + +// OnAllMembersEvicted executes a callback when all members of the SpendSet are evicted and the SpendSet itself can be evicted. +func (c *SpendSet[SpendID, ResourceID, VoteRank]) OnAllMembersEvicted(callback func(prevValue, newValue bool)) { + c.allMembersEvicted.OnUpdate(callback) +} diff --git a/pkg/protocol/engine/mempool/spenddag/spenddagv1/spend_set_test.go b/pkg/protocol/engine/mempool/spenddag/spenddagv1/spend_set_test.go new file mode 100644 index 000000000..07fb376b8 --- /dev/null +++ b/pkg/protocol/engine/mempool/spenddag/spenddagv1/spend_set_test.go @@ -0,0 +1,10 @@ +package spenddagv1 + +import ( + "github.com/iotaledger/iota-core/pkg/core/vote" + iotago "github.com/iotaledger/iota.go/v4" +) + +type TestSpendSet = *SpendSet[iotago.TransactionID, iotago.OutputID, vote.MockedRank] + +var NewTestSpendSet = NewSpendSet[iotago.TransactionID, iotago.OutputID, vote.MockedRank] diff --git a/pkg/protocol/engine/mempool/spenddag/spenddagv1/spend_test.go b/pkg/protocol/engine/mempool/spenddag/spenddagv1/spend_test.go index 8aa17e264..a05ca872e 100644 --- a/pkg/protocol/engine/mempool/spenddag/spenddagv1/spend_test.go +++ b/pkg/protocol/engine/mempool/spenddag/spenddagv1/spend_test.go @@ -24,7 +24,7 @@ type TestSpend = *Spend[iotago.TransactionID, iotago.OutputID, vote.MockedRank] //var NewTestSpend = NewSpend[iotago.TransactionID, iotago.OutputID, vote.MockedRank] -func NewTestSpend(id iotago.TransactionID, parentSpends 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] { +func NewTestSpend(id iotago.TransactionID, parentSpends ds.Set[*Spend[iotago.TransactionID, iotago.OutputID, vote.MockedRank]], SpendSets ds.Set[*SpendSet[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 { @@ -82,12 +82,12 @@ func TestSpend_SetAccepted(t *testing.T) { }) { - ConflictSet1 := NewTestConflictSet(id("ConflictSet1")) - ConflictSet2 := NewTestConflictSet(id("ConflictSet2")) + SpendSet1 := NewTestSpendSet(id("SpendSet1")) + SpendSet2 := NewTestSpendSet(id("SpendSet2")) - Spend1 := NewTestSpend(transactionID("Spend1"), nil, ds.NewSet(ConflictSet1), weight.New(), pendingTasks, thresholdProvider) - Spend2 := NewTestSpend(transactionID("Spend2"), nil, ds.NewSet(ConflictSet1, ConflictSet2), weight.New(), pendingTasks, thresholdProvider) - Spend3 := NewTestSpend(transactionID("Spend3"), nil, ds.NewSet(ConflictSet2), weight.New(), pendingTasks, thresholdProvider) + Spend1 := NewTestSpend(transactionID("Spend1"), nil, ds.NewSet(SpendSet1), weight.New(), pendingTasks, thresholdProvider) + Spend2 := NewTestSpend(transactionID("Spend2"), nil, ds.NewSet(SpendSet1, SpendSet2), weight.New(), pendingTasks, thresholdProvider) + Spend3 := NewTestSpend(transactionID("Spend3"), nil, ds.NewSet(SpendSet2), weight.New(), pendingTasks, thresholdProvider) require.Equal(t, acceptance.Pending, Spend1.setAcceptanceState(acceptance.Accepted)) require.True(t, Spend1.IsAccepted()) @@ -103,8 +103,8 @@ func TestSpend_SetAccepted(t *testing.T) { } { - SpendSet1 := NewTestConflictSet(id("ConflictSet1")) - SpendSet2 := NewTestConflictSet(id("ConflictSet2")) + SpendSet1 := NewTestSpendSet(id("SpendSet1")) + SpendSet2 := NewTestSpendSet(id("SpendSet2")) Spend1 := NewTestSpend(transactionID("Spend1"), nil, ds.NewSet(SpendSet1), weight.New(), pendingTasks, thresholdProvider) Spend2 := NewTestSpend(transactionID("Spend2"), nil, ds.NewSet(SpendSet1, SpendSet2), weight.New(), pendingTasks, thresholdProvider) @@ -117,7 +117,7 @@ func TestSpend_SetAccepted(t *testing.T) { } } -func TestSpend_ConflictSets(t *testing.T) { +func TestSpend_SpendSets(t *testing.T) { weights := account.NewSeatedAccounts(account.NewAccounts()) pendingTasks := syncutils.NewCounter() @@ -125,10 +125,10 @@ func TestSpend_ConflictSets(t *testing.T) { return int64(weights.SeatCount()) }) - red := NewTestConflictSet(id("red")) - blue := NewTestConflictSet(id("blue")) - green := NewTestConflictSet(id("green")) - yellow := NewTestConflictSet(id("yellow")) + red := NewTestSpendSet(id("red")) + blue := NewTestSpendSet(id("blue")) + green := NewTestSpendSet(id("green")) + yellow := NewTestSpendSet(id("yellow")) 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) @@ -268,7 +268,7 @@ func TestLikedInstead1(t *testing.T) { require.True(t, masterBranch.IsLiked()) require.True(t, masterBranch.LikedInstead().IsEmpty()) - SpendSet1 := NewTestConflictSet(id("O1")) + SpendSet1 := NewTestSpendSet(id("O1")) 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) @@ -295,7 +295,7 @@ func TestLikedInsteadFromPreferredInstead(t *testing.T) { require.True(t, masterBranch.IsLiked()) require.True(t, masterBranch.LikedInstead().IsEmpty()) - SpendSet1 := NewTestConflictSet(id("O1")) + SpendSet1 := NewTestSpendSet(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) @@ -308,7 +308,7 @@ func TestLikedInsteadFromPreferredInstead(t *testing.T) { require.Equal(t, 1, SpendB.LikedInstead().Size()) require.True(t, SpendB.LikedInstead().Has(SpendA)) - SpendSet2 := NewTestConflictSet(id("O2")) + SpendSet2 := NewTestSpendSet(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) @@ -372,7 +372,7 @@ func TestLikedInstead21(t *testing.T) { require.True(t, masterBranch.IsLiked()) require.True(t, masterBranch.LikedInstead().IsEmpty()) - SpendSet1 := NewTestConflictSet(id("O1")) + SpendSet1 := NewTestSpendSet(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) @@ -385,7 +385,7 @@ func TestLikedInstead21(t *testing.T) { require.Equal(t, 1, SpendB.LikedInstead().Size()) require.True(t, SpendB.LikedInstead().Has(SpendA)) - SpendSet4 := NewTestConflictSet(id("O4")) + SpendSet4 := NewTestSpendSet(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) @@ -398,7 +398,7 @@ func TestLikedInstead21(t *testing.T) { require.Equal(t, 1, SpendG.LikedInstead().Size()) require.True(t, SpendG.LikedInstead().Has(SpendF)) - SpendSet2 := NewTestConflictSet(id("O2")) + SpendSet2 := NewTestSpendSet(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) @@ -411,7 +411,7 @@ func TestLikedInstead21(t *testing.T) { require.Equal(t, 1, SpendH.LikedInstead().Size()) require.True(t, SpendH.LikedInstead().Has(SpendC)) - SpendSet3 := NewTestConflictSet(id("O12")) + SpendSet3 := NewTestSpendSet(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) @@ -448,12 +448,12 @@ func TestLikedInstead21(t *testing.T) { require.True(t, SpendJ.LikedInstead().Has(SpendH)) } -func TestConflictSet_AllMembersEvicted(t *testing.T) { +func TestSpendSet_AllMembersEvicted(t *testing.T) { weights := account.NewSeatedAccounts(account.NewAccounts()) pendingTasks := syncutils.NewCounter() - yellow := NewTestConflictSet(id("yellow")) - green := NewTestConflictSet(id("green")) + yellow := NewTestSpendSet(id("yellow")) + green := NewTestSpendSet(id("green")) thresholdProvider := acceptance.ThresholdProvider(func() int64 { return int64(weights.SeatCount()) @@ -499,8 +499,8 @@ func TestSpend_Inheritance(t *testing.T) { weights := account.NewSeatedAccounts(account.NewAccounts()) pendingTasks := syncutils.NewCounter() - yellow := NewTestConflictSet(id("yellow")) - green := NewTestConflictSet(id("green")) + yellow := NewTestSpendSet(id("yellow")) + green := NewTestSpendSet(id("green")) thresholdProvider := acceptance.ThresholdProvider(func() int64 { return int64(weights.SeatCount()) @@ -595,10 +595,10 @@ func createSpends(pendingTasks *syncutils.Counter) map[string]TestSpend { return int64(weights.SeatCount()) }) - red := NewTestConflictSet(id("red")) - blue := NewTestConflictSet(id("blue")) - green := NewTestConflictSet(id("green")) - yellow := NewTestConflictSet(id("yellow")) + red := NewTestSpendSet(id("red")) + blue := NewTestSpendSet(id("blue")) + green := NewTestSpendSet(id("green")) + yellow := NewTestSpendSet(id("yellow")) 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) diff --git a/pkg/protocol/engine/mempool/spenddag/spenddagv1/spenddag.go b/pkg/protocol/engine/mempool/spenddag/spenddagv1/spenddag.go index f49bdd42b..ed2924eec 100644 --- a/pkg/protocol/engine/mempool/spenddag/spenddagv1/spenddag.go +++ b/pkg/protocol/engine/mempool/spenddag/spenddagv1/spenddag.go @@ -29,8 +29,8 @@ type SpendDAG[SpendID, ResourceID spenddag.IDType, VoteRank spenddag.VoteRankTyp spendUnhooks *shrinkingmap.ShrinkingMap[SpendID, func()] - // conflictSetsByID is a mapping of ResourceIDs to ConflictSets. - conflictSetsByID *shrinkingmap.ShrinkingMap[ResourceID, *ConflictSet[SpendID, ResourceID, VoteRank]] + // spendSetsByID is a mapping of ResourceIDs to SpendSets. + spendSetsByID *shrinkingmap.ShrinkingMap[ResourceID, *SpendSet[SpendID, ResourceID, VoteRank]] // pendingTasks is a counter that keeps track of the number of pending tasks. pendingTasks *syncutils.Counter @@ -47,12 +47,12 @@ func New[SpendID, ResourceID spenddag.IDType, VoteRank spenddag.VoteRankType[Vot return &SpendDAG[SpendID, ResourceID, VoteRank]{ events: spenddag.NewEvents[SpendID, ResourceID](), - seatCount: seatCount, - spendsByID: shrinkingmap.New[SpendID, *Spend[SpendID, ResourceID, VoteRank]](), - spendUnhooks: shrinkingmap.New[SpendID, func()](), - conflictSetsByID: shrinkingmap.New[ResourceID, *ConflictSet[SpendID, ResourceID, VoteRank]](), - pendingTasks: syncutils.NewCounter(), - votingMutex: syncutils.NewDAGMutex[account.SeatIndex](), + seatCount: seatCount, + spendsByID: shrinkingmap.New[SpendID, *Spend[SpendID, ResourceID, VoteRank]](), + spendUnhooks: shrinkingmap.New[SpendID, func()](), + spendSetsByID: shrinkingmap.New[ResourceID, *SpendSet[SpendID, ResourceID, VoteRank]](), + pendingTasks: syncutils.NewCounter(), + votingMutex: syncutils.NewDAGMutex[account.SeatIndex](), } } @@ -106,7 +106,7 @@ func (c *SpendDAG[SpendID, ResourceID, VoteRank]) CreateSpend(id SpendID) { } func (c *SpendDAG[SpendID, ResourceID, VoteRank]) UpdateConflictingResources(id SpendID, resourceIDs ds.Set[ResourceID]) error { - joinedConflictSets, err := func() (ds.Set[ResourceID], error) { + joinedSpendSets, err := func() (ds.Set[ResourceID], error) { c.mutex.RLock() defer c.mutex.RUnlock() @@ -115,15 +115,15 @@ func (c *SpendDAG[SpendID, ResourceID, VoteRank]) UpdateConflictingResources(id return nil, ierrors.Errorf("spend already evicted: %w", spenddag.ErrEntityEvicted) } - return spend.JoinSpendSets(c.conflictSets(resourceIDs)) + return spend.JoinSpendSets(c.spendSets(resourceIDs)) }() if err != nil { return ierrors.Errorf("spend %s failed to join spend sets: %w", id, err) } - if !joinedConflictSets.IsEmpty() { - c.events.ConflictingResourcesAdded.Trigger(id, joinedConflictSets) + if !joinedSpendSets.IsEmpty() { + c.events.ConflictingResourcesAdded.Trigger(id, joinedSpendSets) } return nil @@ -246,15 +246,15 @@ func (c *SpendDAG[SpendID, ResourceID, VoteRank]) SpendVoters(spendID SpendID) ( return ds.NewSet[account.SeatIndex]() } -func (c *SpendDAG[SpendID, ResourceID, VoteRank]) ConflictSets(spendID SpendID) (spendSets ds.Set[ResourceID], exists bool) { +func (c *SpendDAG[SpendID, ResourceID, VoteRank]) SpendSets(spendID SpendID) (spendSets ds.Set[ResourceID], exists bool) { spend, exists := c.spendsByID.Get(spendID) if !exists { return nil, false } spendSets = ds.NewSet[ResourceID]() - _ = spend.ConflictSets.ForEach(func(conflictSet *ConflictSet[SpendID, ResourceID, VoteRank]) error { - spendSets.Add(conflictSet.ID) + _ = spend.SpendSets.ForEach(func(spendSet *SpendSet[SpendID, ResourceID, VoteRank]) error { + spendSets.Add(spendSet.ID) return nil }) @@ -291,14 +291,14 @@ func (c *SpendDAG[SpendID, ResourceID, VoteRank]) SpendChildren(spendID SpendID) return spendChildren, true } -func (c *SpendDAG[SpendID, ResourceID, VoteRank]) ConflictSetMembers(conflictSetID ResourceID) (spends ds.Set[SpendID], exists bool) { - conflictSet, exists := c.conflictSetsByID.Get(conflictSetID) +func (c *SpendDAG[SpendID, ResourceID, VoteRank]) SpendSetMembers(spendSetID ResourceID) (spends ds.Set[SpendID], exists bool) { + spendSet, exists := c.spendSetsByID.Get(spendSetID) if !exists { return nil, false } spends = ds.NewSet[SpendID]() - _ = conflictSet.ForEach(func(parent *Spend[SpendID, ResourceID, VoteRank]) error { + _ = spendSet.ForEach(func(parent *Spend[SpendID, ResourceID, VoteRank]) error { spends.Add(parent.ID) return nil }) @@ -436,16 +436,16 @@ func (c *SpendDAG[SpendID, ResourceID, VoteRank]) spends(ids ds.Set[SpendID], ig }) } -// conflictSets returns the ConflictSets that are associated with the given ResourceIDs. If createMissing is set to -// true, it will create an empty ConflictSets for each missing ResourceID. -func (c *SpendDAG[SpendID, ResourceID, VoteRank]) conflictSets(resourceIDs ds.Set[ResourceID]) ds.Set[*ConflictSet[SpendID, ResourceID, VoteRank]] { - conflictSets := ds.NewSet[*ConflictSet[SpendID, ResourceID, VoteRank]]() +// spendSets returns the SpendSets that are associated with the given ResourceIDs. If createMissing is set to +// true, it will create an empty SpendSets for each missing ResourceID. +func (c *SpendDAG[SpendID, ResourceID, VoteRank]) spendSets(resourceIDs ds.Set[ResourceID]) ds.Set[*SpendSet[SpendID, ResourceID, VoteRank]] { + spendSets := ds.NewSet[*SpendSet[SpendID, ResourceID, VoteRank]]() resourceIDs.Range(func(resourceID ResourceID) { - conflictSets.Add(lo.Return1(c.conflictSetsByID.GetOrCreate(resourceID, c.conflictSetFactory(resourceID)))) + spendSets.Add(lo.Return1(c.spendSetsByID.GetOrCreate(resourceID, c.spendSetFactory(resourceID)))) }) - return conflictSets + return spendSets } // determineVotes determines the Spends that are supported and revoked by the given SpendIDs. @@ -494,16 +494,16 @@ func (c *SpendDAG[SpendID, ResourceID, VoteRank]) determineVotes(spendIDs ds.Set return supportedSpends, revokedSpends, nil } -func (c *SpendDAG[SpendID, ResourceID, VoteRank]) conflictSetFactory(resourceID ResourceID) func() *ConflictSet[SpendID, ResourceID, VoteRank] { - return func() *ConflictSet[SpendID, ResourceID, VoteRank] { - conflictSet := NewConflictSet[SpendID, ResourceID, VoteRank](resourceID) +func (c *SpendDAG[SpendID, ResourceID, VoteRank]) spendSetFactory(resourceID ResourceID) func() *SpendSet[SpendID, ResourceID, VoteRank] { + return func() *SpendSet[SpendID, ResourceID, VoteRank] { + spendSet := NewSpendSet[SpendID, ResourceID, VoteRank](resourceID) - conflictSet.OnAllMembersEvicted(func(prevValue bool, newValue bool) { + spendSet.OnAllMembersEvicted(func(prevValue bool, newValue bool) { if newValue && !prevValue { - c.conflictSetsByID.Delete(conflictSet.ID) + c.spendSetsByID.Delete(spendSet.ID) } }) - return conflictSet + return spendSet } } diff --git a/pkg/protocol/engine/mempool/spenddag/spenddagv1/spenddag_test.go b/pkg/protocol/engine/mempool/spenddag/spenddagv1/spenddag_test.go index 1dc2775f3..ba9fba66b 100644 --- a/pkg/protocol/engine/mempool/spenddag/spenddagv1/spenddag_test.go +++ b/pkg/protocol/engine/mempool/spenddag/spenddagv1/spenddag_test.go @@ -53,28 +53,28 @@ 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, spendsInConflictSet int, prevConflictSetAlias string) (int, string) { + createSpendSets := func(startSlot, spendSetCount, evictionDelay, spendsInSpendSet int, prevSpendSetAlias string) (int, string) { slot := startSlot - for ; slot < startSlot+conflictSetCount; slot++ { - conflictSetAlias := fmt.Sprintf("conflictSet-%d", slot) - for conflictIndex := 0; conflictIndex < spendsInConflictSet; conflictIndex++ { - conflictAlias := fmt.Sprintf("conflictSet-%d:%d", slot, conflictIndex) - require.NoError(t, tf.CreateOrUpdateSpend(conflictAlias, []string{conflictSetAlias})) - if prevConflictSetAlias != "" { - require.NoError(t, tf.UpdateSpendParents(conflictAlias, []string{fmt.Sprintf("%s:%d", prevConflictSetAlias, 0)}, []string{})) + for ; slot < startSlot+spendSetCount; slot++ { + spendSetAlias := fmt.Sprintf("spendSet-%d", slot) + for conflictIndex := 0; conflictIndex < spendsInSpendSet; conflictIndex++ { + conflictAlias := fmt.Sprintf("spendSet-%d:%d", slot, conflictIndex) + require.NoError(t, tf.CreateOrUpdateSpend(conflictAlias, []string{spendSetAlias})) + if prevSpendSetAlias != "" { + require.NoError(t, tf.UpdateSpendParents(conflictAlias, []string{fmt.Sprintf("%s:%d", prevSpendSetAlias, 0)}, []string{})) } } - prevConflictSetAlias = conflictSetAlias + prevSpendSetAlias = spendSetAlias if slotToEvict := slot - evictionDelay; slotToEvict >= 0 { - for conflictIndex := 0; conflictIndex < spendsInConflictSet; conflictIndex++ { - conflictAlias := fmt.Sprintf("conflictSet-%d:%d", slotToEvict, conflictIndex) + for conflictIndex := 0; conflictIndex < spendsInSpendSet; conflictIndex++ { + conflictAlias := fmt.Sprintf("spendSet-%d:%d", slotToEvict, conflictIndex) tf.EvictSpend(conflictAlias) } } } - return slot, prevConflictSetAlias + return slot, prevSpendSetAlias } _, prevAlias := createSpendSets(0, 30000, 1, 2, "") @@ -97,7 +97,7 @@ func TestMemoryRelease(t *testing.T) { time.Sleep(time.Second) - require.Equal(t, 0, tf.Instance.(*SpendDAG[iotago.TransactionID, iotago.OutputID, vote.MockedRank]).conflictSetsByID.Size()) + require.Equal(t, 0, tf.Instance.(*SpendDAG[iotago.TransactionID, iotago.OutputID, vote.MockedRank]).spendSetsByID.Size()) require.Equal(t, 0, tf.Instance.(*SpendDAG[iotago.TransactionID, iotago.OutputID, vote.MockedRank]).spendsByID.Size()) require.Equal(t, 0, tf.Instance.(*SpendDAG[iotago.TransactionID, iotago.OutputID, vote.MockedRank]).spendUnhooks.Size()) memStatsEnd := memanalyzer.MemSize(tf) diff --git a/pkg/protocol/engine/mempool/spenddag/tests/assertions.go b/pkg/protocol/engine/mempool/spenddag/tests/assertions.go index d76dde9db..18893d671 100644 --- a/pkg/protocol/engine/mempool/spenddag/tests/assertions.go +++ b/pkg/protocol/engine/mempool/spenddag/tests/assertions.go @@ -38,25 +38,25 @@ func (a *Assertions) LikedInstead(spendAliases []string, likedInsteadAliases ... require.Equal(a.f.test, len(likedInsteadAliases), likedInsteadSpends.Size(), "LikedInstead returns wrong number of spends %d instead of %d", likedInsteadSpends.Size(), len(likedInsteadAliases)) } -// 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)) +// SpendSetMembers asserts that the given resource has the given spend set members. +func (a *Assertions) SpendSetMembers(resourceAlias string, spendAliases ...string) { + spendSetMembers, exists := a.f.Instance.SpendSetMembers(a.f.ResourceID(resourceAlias)) require.True(a.f.test, exists, "Resource %s does not exist", resourceAlias) - require.Equal(a.f.test, len(spendAliases), conflictSetMembers.Size(), "Resource %s has wrong number of parents", resourceAlias) + require.Equal(a.f.test, len(spendAliases), spendSetMembers.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) + require.True(a.f.test, spendSetMembers.Has(a.f.SpendID(spendAlias)), "Resource %s does not have parent %s", resourceAlias, spendAlias) } } -// 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)) +// SpendSets asserts that the given spend has the given conflict sets. +func (a *Assertions) SpendSets(spendAlias string, resourceAliases ...string) { + spendSets, exists := a.f.Instance.SpendSets(a.f.SpendID(spendAlias)) require.True(a.f.test, exists, "Spend %s does not exist", spendAlias) - require.Equal(a.f.test, len(resourceAliases), conflictSets.Size(), "Spend %s has wrong number of conflict sets", spendAlias) + require.Equal(a.f.test, len(resourceAliases), spendSets.Size(), "Spend %s has wrong number of conflict sets", spendAlias) for _, resourceAlias := range resourceAliases { - require.True(a.f.test, conflictSets.Has(a.f.ResourceID(resourceAlias)), "Spend %s does not have conflict set %s", spendAlias, resourceAlias) + require.True(a.f.test, spendSets.Has(a.f.ResourceID(resourceAlias)), "Spend %s does not have conflict set %s", spendAlias, resourceAlias) } } diff --git a/pkg/protocol/engine/mempool/spenddag/tests/framework.go b/pkg/protocol/engine/mempool/spenddag/tests/framework.go index 35cf20aa7..2a59a2887 100644 --- a/pkg/protocol/engine/mempool/spenddag/tests/framework.go +++ b/pkg/protocol/engine/mempool/spenddag/tests/framework.go @@ -52,10 +52,10 @@ func NewFramework( return f } -// CreateOrUpdateSpend creates a new spend or adds it to the given ConflictSets. +// CreateOrUpdateSpend creates a new spend or adds it to the given SpendSets. 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...)) + return f.Instance.UpdateConflictingResources(f.SpendID(alias), f.SpendSetIDs(resourceAliases...)) } @@ -101,12 +101,12 @@ func (f *Framework) SpendIDs(aliases ...string) ds.Set[iotago.TransactionID] { return spendIDs } -// ConflictSetIDs translates the given aliases into an AdvancedSet of iotago.OutputIDs. -func (f *Framework) ConflictSetIDs(aliases ...string) ds.Set[iotago.OutputID] { - conflictSetIDs := ds.NewSet[iotago.OutputID]() +// SpendSetIDs translates the given aliases into an AdvancedSet of iotago.OutputIDs. +func (f *Framework) SpendSetIDs(aliases ...string) ds.Set[iotago.OutputID] { + spendSetIDs := ds.NewSet[iotago.OutputID]() for _, alias := range aliases { - conflictSetIDs.Add(f.ResourceID(alias)) + spendSetIDs.Add(f.ResourceID(alias)) } - return conflictSetIDs + return spendSetIDs } diff --git a/pkg/protocol/engine/mempool/spenddag/tests/tests.go b/pkg/protocol/engine/mempool/spenddag/tests/tests.go index d819492ef..258f0c565 100644 --- a/pkg/protocol/engine/mempool/spenddag/tests/tests.go +++ b/pkg/protocol/engine/mempool/spenddag/tests/tests.go @@ -14,38 +14,38 @@ import ( func TestAll(t *testing.T, frameworkProvider func(*testing.T) *Framework) { for testName, testCase := range map[string]func(*testing.T, *Framework){ - "CreateSpend": CreateSpend, - "ExistingSpendJoinsConflictSets": ExistingSpendJoinsConflictSets, - "JoinConflictSetTwice": JoinConflictSetTwice, - "UpdateSpendParents": UpdateSpendParents, - "LikedInstead": LikedInstead, - "CreateSpendWithoutMembers": CreateSpendWithoutMembers, - "SpendAcceptance": SpendAcceptance, - "CastVotes": CastVotes, - "CastVotes_VoteRank": CastVotesVoteRank, - "CastVotesAcceptance": CastVotesAcceptance, - "EvictAcceptedSpend": EvictAcceptedSpend, - "EvictRejectedSpend": EvictRejectedSpend, + "CreateSpend": CreateSpend, + "ExistingSpendJoinsSpendSets": ExistingSpendJoinsSpendSets, + "JoinSpendSetTwice": JoinSpendSetTwice, + "UpdateSpendParents": UpdateSpendParents, + "LikedInstead": LikedInstead, + "CreateSpendWithoutMembers": CreateSpendWithoutMembers, + "SpendAcceptance": SpendAcceptance, + "CastVotes": CastVotes, + "CastVotes_VoteRank": CastVotesVoteRank, + "CastVotesAcceptance": CastVotesAcceptance, + "EvictAcceptedSpend": EvictAcceptedSpend, + "EvictRejectedSpend": EvictRejectedSpend, } { t.Run(testName, func(t *testing.T) { testCase(t, frameworkProvider(t)) }) } } -func ExistingSpendJoinsConflictSets(t *testing.T, tf *Framework) { +func ExistingSpendJoinsSpendSets(t *testing.T, tf *Framework) { require.NoError(t, tf.CreateOrUpdateSpend("conflict1", []string{"resource1"})) require.NoError(t, tf.CreateOrUpdateSpend("conflict2", []string{"resource1"})) - tf.Assert.ConflictSetMembers("resource1", "conflict1", "conflict2") + tf.Assert.SpendSetMembers("resource1", "conflict1", "conflict2") 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") + tf.Assert.SpendSetMembers("resource2", "conflict1", "conflict3") + tf.Assert.SpendSetMembers("resource1", "conflict1", "conflict2") require.NoError(t, tf.CreateOrUpdateSpend("conflict2", []string{"resource2"})) - tf.Assert.ConflictSetMembers("resource2", "conflict1", "conflict2", "conflict3") - tf.Assert.ConflictSetMembers("resource1", "conflict1", "conflict2") + tf.Assert.SpendSetMembers("resource2", "conflict1", "conflict2", "conflict3") + tf.Assert.SpendSetMembers("resource1", "conflict1", "conflict2") tf.Assert.LikedInstead([]string{"conflict3"}, "conflict1") } @@ -77,7 +77,7 @@ func UpdateSpendParents(t *testing.T, tf *Framework) { func CreateSpend(t *testing.T, tf *Framework) { require.NoError(t, tf.CreateOrUpdateSpend("conflict1", []string{"resource1"})) require.NoError(t, tf.CreateOrUpdateSpend("conflict2", []string{"resource1"})) - tf.Assert.ConflictSetMembers("resource1", "conflict1", "conflict2") + tf.Assert.SpendSetMembers("resource1", "conflict1", "conflict2") require.NoError(t, tf.CreateOrUpdateSpend("conflict3", []string{"resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict3", []string{"conflict1"}, []string{})) @@ -85,7 +85,7 @@ func CreateSpend(t *testing.T, tf *Framework) { require.NoError(t, tf.CreateOrUpdateSpend("conflict4", []string{"resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict4", []string{"conflict1"}, []string{})) - tf.Assert.ConflictSetMembers("resource2", "conflict3", "conflict4") + tf.Assert.SpendSetMembers("resource2", "conflict3", "conflict4") tf.Assert.Children("conflict1", "conflict3", "conflict4") tf.Assert.Parents("conflict3", "conflict1") tf.Assert.Parents("conflict4", "conflict1") @@ -102,8 +102,8 @@ func CreateSpendWithoutMembers(t *testing.T, tf *Framework) { 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") + tf.Assert.SpendSetMembers("resource1", "conflict1") + tf.Assert.SpendSetMembers("resource2", "conflict2") tf.Assert.LikedInstead([]string{"conflict1"}) tf.Assert.LikedInstead([]string{"conflict2"}) @@ -121,7 +121,7 @@ func CreateSpendWithoutMembers(t *testing.T, tf *Framework) { require.NoError(t, tf.CreateOrUpdateSpend("conflict3", []string{"resource3"})) require.NoError(t, tf.CreateOrUpdateSpend("conflict4", []string{"resource3"})) - tf.Assert.ConflictSetMembers("resource3", "conflict3", "conflict4") + tf.Assert.SpendSetMembers("resource3", "conflict3", "conflict4") require.NoError(t, tf.CastVotes("nodeID3", 1, "conflict3")) @@ -138,7 +138,7 @@ func LikedInstead(t *testing.T, tf *Framework) { require.NoError(t, tf.CreateOrUpdateSpend("conflict1", []string{"resource1"})) require.NoError(t, tf.CastVotes("zero-weight", 1, "conflict1")) require.NoError(t, tf.CreateOrUpdateSpend("conflict2", []string{"resource1"})) - tf.Assert.ConflictSetMembers("resource1", "conflict1", "conflict2") + tf.Assert.SpendSetMembers("resource1", "conflict1", "conflict2") tf.Assert.LikedInstead([]string{"conflict1", "conflict2"}, "conflict1") require.NoError(t, tf.CreateOrUpdateSpend("conflict3", []string{"resource2"})) @@ -159,9 +159,9 @@ func SpendAcceptance(t *testing.T, tf *Framework) { 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") + tf.Assert.SpendSetMembers("resource1", "conflict1", "conflict2") + tf.Assert.SpendSets("conflict1", "resource1") + tf.Assert.SpendSets("conflict2", "resource1") require.NoError(t, tf.CreateOrUpdateSpend("conflict3", []string{"resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict3", []string{"conflict1"}, []string{})) @@ -169,7 +169,7 @@ func SpendAcceptance(t *testing.T, tf *Framework) { require.NoError(t, tf.CreateOrUpdateSpend("conflict4", []string{"resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict4", []string{"conflict1"}, []string{})) - tf.Assert.ConflictSetMembers("resource2", "conflict3", "conflict4") + tf.Assert.SpendSetMembers("resource2", "conflict3", "conflict4") tf.Assert.Children("conflict1", "conflict3", "conflict4") tf.Assert.Parents("conflict3", "conflict1") tf.Assert.Parents("conflict4", "conflict1") @@ -194,9 +194,9 @@ func CastVotes(t *testing.T, tf *Framework) { 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") + tf.Assert.SpendSetMembers("resource1", "conflict1", "conflict2") + tf.Assert.SpendSets("conflict1", "resource1") + tf.Assert.SpendSets("conflict2", "resource1") require.NoError(t, tf.CreateOrUpdateSpend("conflict3", []string{"resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict3", []string{"conflict1"}, []string{})) @@ -204,7 +204,7 @@ func CastVotes(t *testing.T, tf *Framework) { require.NoError(t, tf.CreateOrUpdateSpend("conflict4", []string{"resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict4", []string{"conflict1"}, []string{})) - tf.Assert.ConflictSetMembers("resource2", "conflict3", "conflict4") + tf.Assert.SpendSetMembers("resource2", "conflict3", "conflict4") tf.Assert.Children("conflict1", "conflict3", "conflict4") tf.Assert.Parents("conflict3", "conflict1") tf.Assert.Parents("conflict4", "conflict1") @@ -229,9 +229,9 @@ func CastVotesVoteRank(t *testing.T, tf *Framework) { 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") + tf.Assert.SpendSetMembers("resource1", "conflict1", "conflict2") + tf.Assert.SpendSets("conflict1", "resource1") + tf.Assert.SpendSets("conflict2", "resource1") // create nested conflicts require.NoError(t, tf.CreateOrUpdateSpend("conflict3", []string{"resource2"})) @@ -240,7 +240,7 @@ func CastVotesVoteRank(t *testing.T, tf *Framework) { require.NoError(t, tf.CreateOrUpdateSpend("conflict4", []string{"resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict4", []string{"conflict1"}, []string{})) - tf.Assert.ConflictSetMembers("resource2", "conflict3", "conflict4") + tf.Assert.SpendSetMembers("resource2", "conflict3", "conflict4") tf.Assert.Children("conflict1", "conflict3", "conflict4") tf.Assert.Parents("conflict3", "conflict1") tf.Assert.Parents("conflict4", "conflict1") @@ -283,9 +283,9 @@ func CastVotesAcceptance(t *testing.T, tf *Framework) { 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") + tf.Assert.SpendSetMembers("resource1", "conflict1", "conflict2") + tf.Assert.SpendSets("conflict1", "resource1") + tf.Assert.SpendSets("conflict2", "resource1") require.NoError(t, tf.CreateOrUpdateSpend("conflict3", []string{"resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict3", []string{"conflict1"}, []string{})) @@ -293,7 +293,7 @@ func CastVotesAcceptance(t *testing.T, tf *Framework) { require.NoError(t, tf.CreateOrUpdateSpend("conflict4", []string{"resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict4", []string{"conflict1"}, []string{})) - tf.Assert.ConflictSetMembers("resource2", "conflict3", "conflict4") + tf.Assert.SpendSetMembers("resource2", "conflict3", "conflict4") tf.Assert.Children("conflict1", "conflict3", "conflict4") tf.Assert.Parents("conflict3", "conflict1") tf.Assert.Parents("conflict4", "conflict1") @@ -321,36 +321,36 @@ func CastVotesAcceptance(t *testing.T, tf *Framework) { require.ErrorIs(t, tf.UpdateSpendParents("conflict2", []string{"conflict1"}, []string{}), spenddag.ErrEntityEvicted) } -func JoinConflictSetTwice(t *testing.T, tf *Framework) { +func JoinSpendSetTwice(t *testing.T, tf *Framework) { var conflictCreatedEventCount, resourceAddedEventCount int tf.Instance.Events().SpendCreated.Hook(func(id iotago.TransactionID) { conflictCreatedEventCount++ }) tf.Instance.Events().ConflictingResourcesAdded.Hook(func(id iotago.TransactionID, resourceID ds.Set[iotago.OutputID]) { - fmt.Println("conflict joins conflictset", id, resourceID) + fmt.Println("conflict joins spendset", id, resourceID) resourceAddedEventCount++ }) require.NoError(t, tf.CreateOrUpdateSpend("conflict1", []string{"resource1"})) require.Equal(t, 1, conflictCreatedEventCount) require.Equal(t, 1, resourceAddedEventCount) - tf.Assert.ConflictSets("conflict1", "resource1") + tf.Assert.SpendSets("conflict1", "resource1") require.NoError(t, tf.CreateOrUpdateSpend("conflict1", []string{"resource2"})) require.Equal(t, 1, conflictCreatedEventCount) require.Equal(t, 2, resourceAddedEventCount) - tf.Assert.ConflictSets("conflict1", "resource1", "resource2") + tf.Assert.SpendSets("conflict1", "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") + tf.Assert.SpendSets("conflict1", "resource1", "resource2") 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") + tf.Assert.SpendSets("conflict1", "resource1", "resource2", "resource3", "resource4") } func EvictAcceptedSpend(t *testing.T, tf *Framework) { @@ -361,9 +361,9 @@ func EvictAcceptedSpend(t *testing.T, tf *Framework) { 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") + tf.Assert.SpendSetMembers("resource1", "conflict1", "conflict2") + tf.Assert.SpendSets("conflict1", "resource1") + tf.Assert.SpendSets("conflict2", "resource1") require.NoError(t, tf.CreateOrUpdateSpend("conflict3", []string{"resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict3", []string{"conflict1"}, []string{})) @@ -371,7 +371,7 @@ func EvictAcceptedSpend(t *testing.T, tf *Framework) { require.NoError(t, tf.CreateOrUpdateSpend("conflict4", []string{"resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict4", []string{"conflict1"}, []string{})) - tf.Assert.ConflictSetMembers("resource2", "conflict3", "conflict4") + tf.Assert.SpendSetMembers("resource2", "conflict3", "conflict4") tf.Assert.Children("conflict1", "conflict3", "conflict4") tf.Assert.Parents("conflict3", "conflict1") tf.Assert.Parents("conflict4", "conflict1") @@ -382,7 +382,7 @@ func EvictAcceptedSpend(t *testing.T, tf *Framework) { require.NoError(t, tf.CreateOrUpdateSpend("conflict6", []string{"resource3"})) require.NoError(t, tf.UpdateSpendParents("conflict6", []string{"conflict2"}, []string{})) - tf.Assert.ConflictSetMembers("resource3", "conflict5", "conflict6") + tf.Assert.SpendSetMembers("resource3", "conflict5", "conflict6") tf.Assert.Children("conflict2", "conflict5", "conflict6") tf.Assert.Parents("conflict5", "conflict2") tf.Assert.Parents("conflict6", "conflict2") @@ -406,9 +406,9 @@ func EvictAcceptedSpend(t *testing.T, tf *Framework) { require.True(t, lo.Return2(tf.Instance.ConflictingSpends(tf.SpendID("conflict5")))) require.True(t, lo.Return2(tf.Instance.ConflictingSpends(tf.SpendID("conflict6")))) - require.False(t, lo.Return2(tf.Instance.ConflictSetMembers(tf.ResourceID("resource1")))) - require.False(t, lo.Return2(tf.Instance.ConflictSetMembers(tf.ResourceID("resource2")))) - tf.Assert.ConflictSetMembers("resource3", "conflict5", "conflict6") + require.False(t, lo.Return2(tf.Instance.SpendSetMembers(tf.ResourceID("resource1")))) + require.False(t, lo.Return2(tf.Instance.SpendSetMembers(tf.ResourceID("resource2")))) + tf.Assert.SpendSetMembers("resource3", "conflict5", "conflict6") tf.Assert.Parents("conflict5") tf.Assert.Parents("conflict6") @@ -427,9 +427,9 @@ func EvictRejectedSpend(t *testing.T, tf *Framework) { 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") + tf.Assert.SpendSetMembers("resource1", "conflict1", "conflict2") + tf.Assert.SpendSets("conflict1", "resource1") + tf.Assert.SpendSets("conflict2", "resource1") require.NoError(t, tf.CreateOrUpdateSpend("conflict3", []string{"resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict3", []string{"conflict1"}, []string{})) @@ -437,7 +437,7 @@ func EvictRejectedSpend(t *testing.T, tf *Framework) { require.NoError(t, tf.CreateOrUpdateSpend("conflict4", []string{"resource2"})) require.NoError(t, tf.UpdateSpendParents("conflict4", []string{"conflict1"}, []string{})) - tf.Assert.ConflictSetMembers("resource2", "conflict3", "conflict4") + tf.Assert.SpendSetMembers("resource2", "conflict3", "conflict4") tf.Assert.Children("conflict1", "conflict3", "conflict4") tf.Assert.Parents("conflict3", "conflict1") tf.Assert.Parents("conflict4", "conflict1") @@ -448,7 +448,7 @@ func EvictRejectedSpend(t *testing.T, tf *Framework) { require.NoError(t, tf.CreateOrUpdateSpend("conflict6", []string{"resource3"})) require.NoError(t, tf.UpdateSpendParents("conflict6", []string{"conflict2"}, []string{})) - tf.Assert.ConflictSetMembers("resource3", "conflict5", "conflict6") + tf.Assert.SpendSetMembers("resource3", "conflict5", "conflict6") tf.Assert.Children("conflict2", "conflict5", "conflict6") tf.Assert.Parents("conflict5", "conflict2") tf.Assert.Parents("conflict6", "conflict2") @@ -476,9 +476,9 @@ func EvictRejectedSpend(t *testing.T, tf *Framework) { tf.EvictSpend("conflict1") require.Equal(t, 3, conflictEvictedEventCount) - tf.Assert.ConflictSetMembers("resource1", "conflict2") - require.False(t, lo.Return2(tf.Instance.ConflictSetMembers(tf.ResourceID("resource2")))) - tf.Assert.ConflictSetMembers("resource3", "conflict5", "conflict6") + tf.Assert.SpendSetMembers("resource1", "conflict2") + require.False(t, lo.Return2(tf.Instance.SpendSetMembers(tf.ResourceID("resource2")))) + tf.Assert.SpendSetMembers("resource3", "conflict5", "conflict6") tf.Assert.Parents("conflict5", "conflict2") tf.Assert.Parents("conflict6", "conflict2") @@ -491,9 +491,9 @@ func EvictRejectedSpend(t *testing.T, tf *Framework) { require.False(t, lo.Return2(tf.Instance.ConflictingSpends(tf.SpendID("conflict6")))) require.Equal(t, 4, conflictEvictedEventCount) - tf.Assert.ConflictSetMembers("resource1", "conflict2") - require.False(t, lo.Return2(tf.Instance.ConflictSetMembers(tf.ResourceID("resource2")))) - tf.Assert.ConflictSetMembers("resource3", "conflict5") + tf.Assert.SpendSetMembers("resource1", "conflict2") + require.False(t, lo.Return2(tf.Instance.SpendSetMembers(tf.ResourceID("resource2")))) + tf.Assert.SpendSetMembers("resource3", "conflict5") tf.Assert.Parents("conflict5", "conflict2") tf.Assert.Children("conflict2", "conflict5") diff --git a/pkg/protocol/engine/mempool/tests/tests.go b/pkg/protocol/engine/mempool/tests/tests.go index 8ed8bc941..41557a794 100644 --- a/pkg/protocol/engine/mempool/tests/tests.go +++ b/pkg/protocol/engine/mempool/tests/tests.go @@ -110,15 +110,15 @@ func TestProcessTransactionWithReadOnlyInputs(t *testing.T, tf *TestFramework) { return nil }) - conflictSetsTx1, exists := tf.SpendDAG.ConflictSets(tf.TransactionID("tx1")) + spendSetsTx1, exists := tf.SpendDAG.SpendSets(tf.TransactionID("tx1")) require.True(t, exists) - require.Equal(t, 1, conflictSetsTx1.Size()) - require.True(t, conflictSetsTx1.Has(tf.StateID("genesis"))) + require.Equal(t, 1, spendSetsTx1.Size()) + require.True(t, spendSetsTx1.Has(tf.StateID("genesis"))) - conflictSetsTx2, exists := tf.SpendDAG.ConflictSets(tf.TransactionID("tx2")) + spendSetsTx2, exists := tf.SpendDAG.SpendSets(tf.TransactionID("tx2")) require.True(t, exists) - require.Equal(t, 1, conflictSetsTx2.Size()) - require.True(t, conflictSetsTx2.Has(tf.StateID("tx1:0"))) + require.Equal(t, 1, spendSetsTx2.Size()) + require.True(t, spendSetsTx2.Has(tf.StateID("tx1:0"))) } func TestProcessTransactionsOutOfOrder(t *testing.T, tf *TestFramework) { @@ -237,9 +237,9 @@ func TestSetTxOrphanageMultipleAttachments(t *testing.T, tf *TestFramework) { require.False(t, lo.Return2(tx2Metadata.OrphanedSlot())) require.False(t, lo.Return2(tx3Metadata.OrphanedSlot())) - require.True(t, lo.Return2(tf.SpendDAG.ConflictSets(tf.TransactionID("tx1")))) - require.True(t, lo.Return2(tf.SpendDAG.ConflictSets(tf.TransactionID("tx2")))) - require.True(t, lo.Return2(tf.SpendDAG.ConflictSets(tf.TransactionID("tx3")))) + require.True(t, lo.Return2(tf.SpendDAG.SpendSets(tf.TransactionID("tx1")))) + require.True(t, lo.Return2(tf.SpendDAG.SpendSets(tf.TransactionID("tx2")))) + require.True(t, lo.Return2(tf.SpendDAG.SpendSets(tf.TransactionID("tx3")))) tf.Instance.Evict(2) @@ -248,9 +248,9 @@ func TestSetTxOrphanageMultipleAttachments(t *testing.T, tf *TestFramework) { require.True(t, lo.Return2(tx3Metadata.OrphanedSlot())) // All conflicts still exist, as they are kept around until MCA - require.True(t, lo.Return2(tf.SpendDAG.ConflictSets(tf.TransactionID("tx1")))) - require.True(t, lo.Return2(tf.SpendDAG.ConflictSets(tf.TransactionID("tx2")))) - require.True(t, lo.Return2(tf.SpendDAG.ConflictSets(tf.TransactionID("tx3")))) + require.True(t, lo.Return2(tf.SpendDAG.SpendSets(tf.TransactionID("tx1")))) + require.True(t, lo.Return2(tf.SpendDAG.SpendSets(tf.TransactionID("tx2")))) + require.True(t, lo.Return2(tf.SpendDAG.SpendSets(tf.TransactionID("tx3")))) tf.RequireTransactionsEvicted(map[string]bool{"tx1": false, "tx2": false, "tx3": false})