Skip to content

Commit

Permalink
Rename variable spenddag to spendDAG
Browse files Browse the repository at this point in the history
  • Loading branch information
jkrvivian committed Nov 9, 2023
1 parent 2500e79 commit 0c78f57
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 58 deletions.
16 changes: 8 additions & 8 deletions pkg/protocol/engine/mempool/spenddag/tests/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import (
iotago "github.com/iotaledger/iota.go/v4"
)

// Framework is a test framework for the spenddag that allows to easily create and manipulate the DAG and its
// Framework is a test framework for the SpendDAG that allows to easily create and manipulate the DAG and its
// validators using human-readable aliases instead of actual IDs.
type Framework struct {
// Instance is the spenddag instance that is used in the tests.
// Instance is the SpendDAG instance that is used in the tests.
Instance spenddag.SpendDAG[iotago.TransactionID, iotago.OutputID, vote.MockedRank]

// Accounts is the AccountsTestFramework that is used in the tests.
Accounts *AccountsTestFramework

// Assert provides a set of assertions that can be used to verify the state of the spenddag.
// Assert provides a set of assertions that can be used to verify the state of the SpendDAG.
Assert *Assertions

// SpendID is a function that is used to translate a string alias into a (deterministic) iotago.TransactionID.
Expand All @@ -35,13 +35,13 @@ type Framework struct {
// NewFramework creates a new instance of the Framework.
func NewFramework(
t *testing.T,
spenddag spenddag.SpendDAG[iotago.TransactionID, iotago.OutputID, vote.MockedRank],
spendDAG spenddag.SpendDAG[iotago.TransactionID, iotago.OutputID, vote.MockedRank],
validators *AccountsTestFramework,
spendID func(string) iotago.TransactionID,
resourceID func(string) iotago.OutputID,
) *Framework {
f := &Framework{
Instance: spenddag,
Instance: spendDAG,
Accounts: validators,
SpendID: spendID,
ResourceID: resourceID,
Expand All @@ -67,8 +67,8 @@ func (f *Framework) UpdateSpendParents(conflictAlias string, addedParentIDs []st
// LikedInstead returns the set of conflicts that are liked instead of the given conflicts.
func (f *Framework) LikedInstead(conflictAliases ...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...))
_ = f.Instance.ReadConsistent(func(spendDAG spenddag.ReadLockedSpendDAG[iotago.TransactionID, iotago.OutputID, vote.MockedRank]) error {
result = spendDAG.LikedInstead(f.SpendIDs(conflictAliases...))

return nil
})
Expand All @@ -86,7 +86,7 @@ func (f *Framework) CastVotes(nodeAlias string, voteRank int, conflictAliases ..
return f.Instance.CastVotes(vote.NewVote[vote.MockedRank](seat, vote.MockedRank(voteRank)), f.SpendIDs(conflictAliases...))
}

// EvictConflict evicts given conflict from the spenddag.
// EvictConflict evicts given conflict from the SpendDAG.
func (f *Framework) EvictSpend(conflictAlias string) {
f.Instance.EvictSpend(f.SpendID(conflictAlias))
}
Expand Down
34 changes: 17 additions & 17 deletions pkg/protocol/engine/mempool/spenddag/tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ import (

func TestAll(t *testing.T, frameworkProvider func(*testing.T) *Framework) {
for testName, testCase := range map[string]func(*testing.T, *Framework){
"CreateConflict": CreateSpend,
"ExistingConflictJoinsConflictSets": ExistingConflictJoinsSpendSets,
"JoinConflictSetTwice": JoinConflictSetTwice,
"UpdateConflictParents": UpdateSpendParents,
"LikedInstead": LikedInstead,
"CreateConflictWithoutMembers": CreateConflictWithoutMembers,
"ConflictAcceptance": ConflictAcceptance,
"CastVotes": CastVotes,
"CastVotes_VoteRank": CastVotesVoteRank,
"CastVotesAcceptance": CastVotesAcceptance,
"EvictAcceptedConflict": EvictAcceptedConflict,
"EvictRejectedConflict": EvictRejectedConflict,
"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,
} {
t.Run(testName, func(t *testing.T) { testCase(t, frameworkProvider(t)) })
}
}

func ExistingConflictJoinsSpendSets(t *testing.T, tf *Framework) {
func ExistingSpendJoinsConflictSets(t *testing.T, tf *Framework) {
require.NoError(t, tf.CreateOrUpdateConflict("conflict1", []string{"resource1"}))
require.NoError(t, tf.CreateOrUpdateConflict("conflict2", []string{"resource1"}))
tf.Assert.ConflictSetMembers("resource1", "conflict1", "conflict2")
Expand Down Expand Up @@ -91,7 +91,7 @@ func CreateSpend(t *testing.T, tf *Framework) {
tf.Assert.Parents("conflict4", "conflict1")
}

func CreateConflictWithoutMembers(t *testing.T, tf *Framework) {
func CreateSpendWithoutMembers(t *testing.T, tf *Framework) {
tf.Accounts.CreateID("nodeID1")
tf.Accounts.CreateID("nodeID2")
tf.Accounts.CreateID("nodeID3")
Expand Down Expand Up @@ -151,7 +151,7 @@ func LikedInstead(t *testing.T, tf *Framework) {
tf.Assert.LikedInstead([]string{"conflict1", "conflict2", "conflict3", "conflict4"}, "conflict1", "conflict4")
}

func ConflictAcceptance(t *testing.T, tf *Framework) {
func SpendAcceptance(t *testing.T, tf *Framework) {
tf.Accounts.CreateID("nodeID1")
tf.Accounts.CreateID("nodeID2")
tf.Accounts.CreateID("nodeID3")
Expand Down Expand Up @@ -353,7 +353,7 @@ func JoinConflictSetTwice(t *testing.T, tf *Framework) {
tf.Assert.ConflictSets("conflict1", "resource1", "resource2", "resource3", "resource4")
}

func EvictAcceptedConflict(t *testing.T, tf *Framework) {
func EvictAcceptedSpend(t *testing.T, tf *Framework) {
tf.Accounts.CreateID("nodeID1")
tf.Accounts.CreateID("nodeID2")
tf.Accounts.CreateID("nodeID3")
Expand Down Expand Up @@ -414,7 +414,7 @@ func EvictAcceptedConflict(t *testing.T, tf *Framework) {
tf.Assert.Parents("conflict6")
}

func EvictRejectedConflict(t *testing.T, tf *Framework) {
func EvictRejectedSpend(t *testing.T, tf *Framework) {
conflictEvictedEventCount := 0
tf.Instance.Events().SpendEvicted.Hook(func(id iotago.TransactionID) {
conflictEvictedEventCount++
Expand Down
6 changes: 3 additions & 3 deletions pkg/protocol/engine/mempool/tests/testframework.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

type TestFramework struct {
Instance mempool.MemPool[vote.MockedRank]
spenddag spenddag.SpendDAG[iotago.TransactionID, mempool.StateID, vote.MockedRank]
SpendDAG spenddag.SpendDAG[iotago.TransactionID, mempool.StateID, vote.MockedRank]

referencesByAlias map[string]mempool.StateReference
stateIDByAlias map[string]mempool.StateID
Expand All @@ -35,10 +35,10 @@ type TestFramework struct {
mutex syncutils.RWMutex
}

func NewTestFramework(test *testing.T, instance mempool.MemPool[vote.MockedRank], spenddag spenddag.SpendDAG[iotago.TransactionID, mempool.StateID, vote.MockedRank], ledgerState *ledgertests.MockStateResolver, workers *workerpool.Group) *TestFramework {
func NewTestFramework(test *testing.T, instance mempool.MemPool[vote.MockedRank], spendDAG spenddag.SpendDAG[iotago.TransactionID, mempool.StateID, vote.MockedRank], ledgerState *ledgertests.MockStateResolver, workers *workerpool.Group) *TestFramework {
t := &TestFramework{
Instance: instance,
spenddag: spenddag,
SpendDAG: spendDAG,
referencesByAlias: make(map[string]mempool.StateReference),
stateIDByAlias: make(map[string]mempool.StateID),
signedTransactionByAlias: make(map[string]mempool.SignedTransaction),
Expand Down
16 changes: 8 additions & 8 deletions pkg/protocol/engine/mempool/tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ func TestProcessTransactionWithReadOnlyInputs(t *testing.T, tf *TestFramework) {
return nil
})

conflictSetsTx1, exists := tf.spenddag.ConflictSets(tf.TransactionID("tx1"))
conflictSetsTx1, exists := tf.SpendDAG.ConflictSets(tf.TransactionID("tx1"))
require.True(t, exists)
require.Equal(t, 1, conflictSetsTx1.Size())
require.True(t, conflictSetsTx1.Has(tf.StateID("genesis")))

conflictSetsTx2, exists := tf.spenddag.ConflictSets(tf.TransactionID("tx2"))
conflictSetsTx2, exists := tf.SpendDAG.ConflictSets(tf.TransactionID("tx2"))
require.True(t, exists)
require.Equal(t, 1, conflictSetsTx2.Size())
require.True(t, conflictSetsTx2.Has(tf.StateID("tx1:0")))
Expand Down Expand Up @@ -300,9 +300,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.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"))))

tf.Instance.Evict(2)

Expand All @@ -311,9 +311,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.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"))))

tf.RequireTransactionsEvicted(map[string]bool{"tx1": false, "tx2": false, "tx3": false})

Expand Down
18 changes: 9 additions & 9 deletions pkg/protocol/engine/mempool/v1/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ type MemPool[VoteRank spenddag.VoteRankType[VoteRank]] struct {
// invalidate blocks that try to spend them.
delayedOutputStateEviction *shrinkingmap.ShrinkingMap[iotago.SlotIndex, *shrinkingmap.ShrinkingMap[iotago.Identifier, *StateMetadata]]

// spenddag is the DAG that is used to keep track of the conflicts between transactions.
spenddag spenddag.SpendDAG[iotago.TransactionID, mempool.StateID, VoteRank]
// spendDAG is the DAG that is used to keep track of the conflicts between transactions.
spendDAG spenddag.SpendDAG[iotago.TransactionID, mempool.StateID, VoteRank]

apiProvider iotago.APIProvider

Expand All @@ -80,7 +80,7 @@ func New[VoteRank spenddag.VoteRankType[VoteRank]](
stateResolver mempool.StateResolver,
mutationsFunc func(iotago.SlotIndex) (kvstore.KVStore, error),
workers *workerpool.Group,
spenddag spenddag.SpendDAG[iotago.TransactionID, mempool.StateID, VoteRank],
spendDAG spenddag.SpendDAG[iotago.TransactionID, mempool.StateID, VoteRank],
apiProvider iotago.APIProvider,
errorHandler func(error),
opts ...options.Option[MemPool[VoteRank]],
Expand All @@ -97,7 +97,7 @@ func New[VoteRank spenddag.VoteRankType[VoteRank]](
executionWorkers: workers.CreatePool("executionWorkers", workerpool.WithWorkerCount(1)),
delayedTransactionEviction: shrinkingmap.New[iotago.SlotIndex, ds.Set[iotago.TransactionID]](),
delayedOutputStateEviction: shrinkingmap.New[iotago.SlotIndex, *shrinkingmap.ShrinkingMap[iotago.Identifier, *StateMetadata]](),
spenddag: spenddag,
spendDAG: spendDAG,
apiProvider: apiProvider,
errorHandler: errorHandler,
signedTransactionAttached: event.New1[mempool.SignedTransactionMetadata](),
Expand Down Expand Up @@ -334,7 +334,7 @@ func (m *MemPool[VoteRank]) bookTransaction(transaction *TransactionMetadata) {
func (m *MemPool[VoteRank]) forkTransaction(transactionMetadata *TransactionMetadata, resourceIDs ds.Set[mempool.StateID]) {
transactionMetadata.conflicting.Trigger()

if err := m.spenddag.UpdateConflictingResources(transactionMetadata.ID(), resourceIDs); err != nil {
if err := m.spendDAG.UpdateConflictingResources(transactionMetadata.ID(), resourceIDs); err != nil {
// this is a hack, as with a reactive.Variable we cannot set it to 0 and still check if it was orphaned.
transactionMetadata.orphanedSlot.Set(1)

Expand Down Expand Up @@ -426,7 +426,7 @@ func (m *MemPool[VoteRank]) updateStateDiffs(transaction *TransactionMetadata, p
}

func (m *MemPool[VoteRank]) setup() {
m.spenddag.Events().SpendAccepted.Hook(func(id iotago.TransactionID) {
m.spendDAG.Events().SpendAccepted.Hook(func(id iotago.TransactionID) {
if transaction, exists := m.cachedTransactions.Get(id); exists {
transaction.setConflictAccepted()
}
Expand All @@ -449,18 +449,18 @@ func (m *MemPool[VoteRank]) setupTransaction(transaction *TransactionMetadata) {
})

transaction.OnConflicting(func() {
m.spenddag.CreateSpend(transaction.ID())
m.spendDAG.CreateSpend(transaction.ID())

unsubscribe := transaction.parentSpendIDs.OnUpdate(func(appliedMutations ds.SetMutations[iotago.TransactionID]) {
if err := m.spenddag.UpdateSpendParents(transaction.ID(), appliedMutations.AddedElements(), appliedMutations.DeletedElements()); err != nil {
if err := m.spendDAG.UpdateSpendParents(transaction.ID(), appliedMutations.AddedElements(), appliedMutations.DeletedElements()); err != nil {
panic(err)
}
})

transaction.OnEvicted(func() {
unsubscribe()

m.spenddag.EvictSpend(transaction.ID())
m.spendDAG.EvictSpend(transaction.ID())
})
})

Expand Down
10 changes: 5 additions & 5 deletions pkg/protocol/engine/mempool/v1/mempool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ func TestMempoolV1_ResourceCleanup(t *testing.T) {
}

ledgerState := ledgertests.New(ledgertests.NewMockedState(iotago.EmptyTransactionID, 0))
spenddag := spenddagv1.New[iotago.TransactionID, mempool.StateID, vote.MockedRank](func() int { return 0 })
spendDAG := spenddagv1.New[iotago.TransactionID, mempool.StateID, vote.MockedRank](func() int { return 0 })
memPoolInstance := New[vote.MockedRank](new(mempooltests.VM), func(reference mempool.StateReference) *promise.Promise[mempool.State] {
return ledgerState.ResolveOutputState(reference)
}, mutationsFunc, workers, spenddag, api.SingleVersionProvider(tpkg.TestAPI), func(error) {})
}, mutationsFunc, workers, spendDAG, api.SingleVersionProvider(tpkg.TestAPI), func(error) {})

tf := mempooltests.NewTestFramework(t, memPoolInstance, spenddag, ledgerState, workers)
tf := mempooltests.NewTestFramework(t, memPoolInstance, spendDAG, ledgerState, workers)

issueTransactions := func(startIndex, transactionCount int, prevStateAlias string) (int, string) {
index := startIndex
Expand Down Expand Up @@ -141,13 +141,13 @@ func newTestFramework(t *testing.T) *mempooltests.TestFramework {
workers := workerpool.NewGroup(t.Name())

ledgerState := ledgertests.New(ledgertests.NewMockedState(iotago.EmptyTransactionID, 0))
spenddag := spenddagv1.New[iotago.TransactionID, mempool.StateID, vote.MockedRank](account.NewAccounts().SelectCommittee().SeatCount)
spendDAG := spenddagv1.New[iotago.TransactionID, mempool.StateID, vote.MockedRank](account.NewAccounts().SelectCommittee().SeatCount)

mutationsFunc := func(index iotago.SlotIndex) (kvstore.KVStore, error) {
return mapdb.NewMapDB(), nil
}

return mempooltests.NewTestFramework(t, New[vote.MockedRank](new(mempooltests.VM), func(reference mempool.StateReference) *promise.Promise[mempool.State] {
return ledgerState.ResolveOutputState(reference)
}, mutationsFunc, workers, spenddag, api.SingleVersionProvider(tpkg.TestAPI), func(error) {}), spenddag, ledgerState, workers)
}, mutationsFunc, workers, spendDAG, api.SingleVersionProvider(tpkg.TestAPI), func(error) {}), spendDAG, ledgerState, workers)
}
16 changes: 8 additions & 8 deletions pkg/protocol/engine/tipselection/v1/tip_selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ type TipSelection struct {
// tipManager is the TipManager that is used to access the tip related metadata.
tipManager tipmanager.TipManager

// conflictDAG is the ConflictDAG that is used to track conflicts.
conflictDAG spenddag.SpendDAG[iotago.TransactionID, mempool.StateID, ledger.BlockVoteRank]
// spendDAG is the SpendDAG that is used to track spends.
spendDAG spenddag.SpendDAG[iotago.TransactionID, mempool.StateID, ledger.BlockVoteRank]

// rootBlocks is a function that returns the current root blocks.
rootBlocks func() iotago.BlockIDs
Expand Down Expand Up @@ -78,9 +78,9 @@ func New(opts ...options.Option[TipSelection]) *TipSelection {
//
// This method is separated from the constructor so the TipSelection can be initialized lazily after all dependencies
// are available.
func (t *TipSelection) Construct(tipManager tipmanager.TipManager, conflictDAG spenddag.SpendDAG[iotago.TransactionID, mempool.StateID, ledger.BlockVoteRank], transactionMetadataRetriever func(iotago.TransactionID) (mempool.TransactionMetadata, bool), rootBlocksRetriever func() iotago.BlockIDs, livenessThresholdFunc func(tipmanager.TipMetadata) time.Duration) *TipSelection {
func (t *TipSelection) Construct(tipManager tipmanager.TipManager, spendDAG spenddag.SpendDAG[iotago.TransactionID, mempool.StateID, ledger.BlockVoteRank], transactionMetadataRetriever func(iotago.TransactionID) (mempool.TransactionMetadata, bool), rootBlocksRetriever func() iotago.BlockIDs, livenessThresholdFunc func(tipmanager.TipMetadata) time.Duration) *TipSelection {
t.tipManager = tipManager
t.conflictDAG = conflictDAG
t.spendDAG = spendDAG
t.transactionMetadata = transactionMetadataRetriever
t.rootBlocks = rootBlocksRetriever
t.livenessThreshold = livenessThresholdFunc
Expand All @@ -103,7 +103,7 @@ func (t *TipSelection) SelectTips(amount int) (references model.ParentReferences
references = make(model.ParentReferences)
strongParents := ds.NewSet[iotago.BlockID]()
shallowLikesParents := ds.NewSet[iotago.BlockID]()
_ = t.conflictDAG.ReadConsistent(func(_ spenddag.ReadLockedSpendDAG[iotago.TransactionID, mempool.StateID, ledger.BlockVoteRank]) error {
_ = t.spendDAG.ReadConsistent(func(_ spenddag.ReadLockedSpendDAG[iotago.TransactionID, mempool.StateID, ledger.BlockVoteRank]) error {
previousLikedInsteadConflicts := ds.NewSet[iotago.TransactionID]()

if t.collectReferences(references, iotago.StrongParentType, t.tipManager.StrongTips, func(tip tipmanager.TipMetadata) {
Expand Down Expand Up @@ -177,7 +177,7 @@ func (t *TipSelection) classifyTip(tipMetadata tipmanager.TipMetadata) {
// likedInsteadReferences returns the liked instead references that are required to be able to reference the given tip.
func (t *TipSelection) likedInsteadReferences(likedConflicts ds.Set[iotago.TransactionID], tipMetadata tipmanager.TipMetadata) (references []iotago.BlockID, updatedLikedConflicts ds.Set[iotago.TransactionID], err error) {
necessaryReferences := make(map[iotago.TransactionID]iotago.BlockID)
if err = t.conflictDAG.LikedInstead(tipMetadata.Block().SpendIDs()).ForEach(func(likedSpendID iotago.TransactionID) error {
if err = t.spendDAG.LikedInstead(tipMetadata.Block().SpendIDs()).ForEach(func(likedSpendID iotago.TransactionID) error {
transactionMetadata, exists := t.transactionMetadata(likedSpendID)
if !exists {
return ierrors.Errorf("transaction required for liked instead reference (%s) not found in mem-pool", likedSpendID)
Expand Down Expand Up @@ -233,12 +233,12 @@ func (t *TipSelection) collectReferences(references model.ParentReferences, pare

// isValidStrongTip checks if the given block is a valid strong tip.
func (t *TipSelection) isValidStrongTip(block *blocks.Block) bool {
return !t.conflictDAG.AcceptanceState(block.SpendIDs()).IsRejected()
return !t.spendDAG.AcceptanceState(block.SpendIDs()).IsRejected()
}

// isValidWeakTip checks if the given block is a valid weak tip.
func (t *TipSelection) isValidWeakTip(block *blocks.Block) bool {
return t.conflictDAG.LikedInstead(block.PayloadSpendIDs()).Size() == 0
return t.spendDAG.LikedInstead(block.PayloadSpendIDs()).Size() == 0
}

// triggerLivenessThreshold triggers the liveness threshold for all tips that have reached the given threshold.
Expand Down

0 comments on commit 0c78f57

Please sign in to comment.