Skip to content

Commit

Permalink
Rename conflict to spend
Browse files Browse the repository at this point in the history
  • Loading branch information
jkrvivian committed Nov 9, 2023
1 parent 0c78f57 commit 64bf4f1
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 168 deletions.
6 changes: 3 additions & 3 deletions pkg/core/acceptance/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
16 changes: 8 additions & 8 deletions pkg/protocol/engine/booker/inmemorybooker/booker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion pkg/protocol/engine/mempool/spenddag/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/protocol/engine/mempool/spenddag/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
2 changes: 1 addition & 1 deletion pkg/protocol/engine/mempool/spenddag/spenddag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@ 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()

if s.isShutdown.Load() {
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
Expand Down Expand Up @@ -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
Expand Down
78 changes: 39 additions & 39 deletions pkg/protocol/engine/mempool/spenddag/spenddagv1/spend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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]) {
Expand Down Expand Up @@ -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()
Expand All @@ -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
}
Expand All @@ -230,54 +230,54 @@ 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()

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()

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()

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()

return c.likedInstead.Clone()
}

// Shutdown shuts down the Conflict.
// Shutdown shuts down the Spend.
func (c *Spend[SpendID, ResourceID, VoteRank]) Shutdown() {
c.ConflictingSpends.Shutdown()
}
Expand All @@ -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()
Expand Down Expand Up @@ -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

Expand All @@ -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()
Expand Down Expand Up @@ -417,7 +417,7 @@ func (c *Spend[SpendID, ResourceID, VoteRank]) registerChild(child *Spend[SpendI
}
}

// unregisterChild unregisters the given child Conflict.
// unregisterChild unregisters the given child Spend.
func (c *Spend[SpendID, ResourceID, VoteRank]) unregisterChild(conflict *Spend[SpendID, ResourceID, VoteRank]) {
c.structureMutex.Lock()
defer c.structureMutex.Unlock()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/protocol/engine/mempool/spenddag/spenddagv1/spenddag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestMemoryRelease(t *testing.T) {
conflictSetAlias := fmt.Sprintf("conflictSet-%d", slot)
for conflictIndex := 0; conflictIndex < conflictsInConflictSet; 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{}))
}
Expand Down
Loading

0 comments on commit 64bf4f1

Please sign in to comment.