Skip to content

Commit

Permalink
Rename ConflictSet to SpendSet
Browse files Browse the repository at this point in the history
  • Loading branch information
jkrvivian committed Nov 20, 2023
1 parent c81bc25 commit f63865c
Show file tree
Hide file tree
Showing 15 changed files with 271 additions and 270 deletions.
8 changes: 4 additions & 4 deletions pkg/protocol/engine/mempool/spenddag/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
2 changes: 1 addition & 1 deletion pkg/protocol/engine/mempool/spenddag/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions pkg/protocol/engine/mempool/spenddag/spenddag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
72 changes: 0 additions & 72 deletions pkg/protocol/engine/mempool/spenddag/spenddagv1/conflict_set.go

This file was deleted.

This file was deleted.

30 changes: 15 additions & 15 deletions pkg/protocol/engine/mempool/spenddag/spenddagv1/spend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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](),
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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 {
Expand Down
73 changes: 73 additions & 0 deletions pkg/protocol/engine/mempool/spenddag/spenddagv1/spend_set.go
Original file line number Diff line number Diff line change
@@ -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)
}
10 changes: 10 additions & 0 deletions pkg/protocol/engine/mempool/spenddag/spenddagv1/spend_set_test.go
Original file line number Diff line number Diff line change
@@ -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]
Loading

0 comments on commit f63865c

Please sign in to comment.