Skip to content

Commit

Permalink
Refactor: started cleaning branchdag
Browse files Browse the repository at this point in the history
  • Loading branch information
hmoog committed Apr 5, 2022
1 parent 2299ade commit 8704bd2
Show file tree
Hide file tree
Showing 14 changed files with 343 additions and 295 deletions.
12 changes: 7 additions & 5 deletions packages/ledger/booker.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ func (b *booker) bookTransaction(txMetadata *TransactionMetadata, inputsMetadata
func (b *booker) inheritBranchIDs(txID utxo.TransactionID, inputsMetadata OutputsMetadata) (inheritedBranchIDs branchdag.BranchIDs) {
conflictingInputIDs, consumersToFork := b.determineConflictDetails(txID, inputsMetadata)
if conflictingInputIDs.Size() == 0 {
return b.BranchDAG.RemoveConfirmedBranches(inputsMetadata.BranchIDs())
return b.BranchDAG.FilterPendingBranches(inputsMetadata.BranchIDs())
}

branchID := branchdag.NewBranchID(txID)
b.BranchDAG.CreateBranch(branchID, b.BranchDAG.RemoveConfirmedBranches(inputsMetadata.BranchIDs()), branchdag.NewConflictIDs(lo.Map(conflictingInputIDs.Slice(), branchdag.NewConflictID)...))
b.BranchDAG.CreateBranch(branchID, b.BranchDAG.FilterPendingBranches(inputsMetadata.BranchIDs()), branchdag.NewConflictIDs(lo.Map(conflictingInputIDs.Slice(), branchdag.NewConflictID)...))

_ = consumersToFork.ForEach(func(transactionID utxo.TransactionID) (err error) {
b.utils.WithTransactionAndMetadata(transactionID, func(tx *Transaction, txMetadata *TransactionMetadata) {
Expand Down Expand Up @@ -123,7 +123,9 @@ func (b *booker) forkTransaction(tx *Transaction, txMetadata *TransactionMetadat
previousParentBranches := txMetadata.BranchIDs()

forkedBranchID := branchdag.NewBranchID(txMetadata.ID())
if !b.BranchDAG.CreateBranch(forkedBranchID, previousParentBranches, branchdag.NewConflictIDs(lo.Map(conflictingInputs.Slice(), branchdag.NewConflictID)...)) {
conflictIDs := branchdag.NewConflictIDs(lo.Map(conflictingInputs.Slice(), branchdag.NewConflictID)...)
if !b.BranchDAG.CreateBranch(forkedBranchID, previousParentBranches, conflictIDs) {
b.BranchDAG.AddBranchToConflicts(forkedBranchID, conflictIDs)
b.mutex.Unlock(txMetadata.ID())
return
}
Expand Down Expand Up @@ -159,7 +161,7 @@ func (b *booker) propagateForkedBranchToFutureCone(outputIDs utxo.OutputIDs, for

func (b *booker) updateBranchesAfterFork(txMetadata *TransactionMetadata, forkedBranchID branchdag.BranchID, previousParents branchdag.BranchIDs) bool {
if txMetadata.IsConflicting() {
b.BranchDAG.UpdateParentsAfterFork(branchdag.NewBranchID(txMetadata.ID()), forkedBranchID, previousParents)
b.BranchDAG.UpdateBranchParents(branchdag.NewBranchID(txMetadata.ID()), forkedBranchID, previousParents)
return false
}

Expand All @@ -170,7 +172,7 @@ func (b *booker) updateBranchesAfterFork(txMetadata *TransactionMetadata, forked
newBranchIDs := txMetadata.BranchIDs().Clone()
newBranchIDs.DeleteAll(previousParents)
newBranchIDs.Add(forkedBranchID)
newBranches := b.BranchDAG.RemoveConfirmedBranches(newBranchIDs)
newBranches := b.BranchDAG.FilterPendingBranches(newBranchIDs)

b.Storage.CachedOutputsMetadata(txMetadata.OutputIDs()).Consume(func(outputMetadata *OutputMetadata) {
outputMetadata.SetBranchIDs(newBranches)
Expand Down
Loading

0 comments on commit 8704bd2

Please sign in to comment.