Skip to content

Commit

Permalink
Feat: refactored more code
Browse files Browse the repository at this point in the history
  • Loading branch information
hmoog committed Mar 30, 2022
1 parent dac0c7c commit d469cbf
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 147 deletions.
14 changes: 5 additions & 9 deletions packages/refactored/branchdag/branch_dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,18 @@ func (b *BranchDAG) CreateBranch(branchID BranchID, parentBranchIDs BranchIDs, c
return created
}

// AddBranchParent changes the parents of a Branch (also updating the references of the ChildBranches).
func (b *BranchDAG) AddBranchParent(branchID, newParentBranchID BranchID) (err error) {
// UpdateParentsAfterFork changes the parents of a Branch (also updating the references of the ChildBranches).
func (b *BranchDAG) UpdateParentsAfterFork(branchID, newParentBranchID BranchID, previousParents BranchIDs) {
b.inclusionStateMutex.RLock()
defer b.inclusionStateMutex.RUnlock()

if !b.Branch(branchID).Consume(func(branch *Branch) {
b.Branch(branchID).Consume(func(branch *Branch) {
parentBranchIDs := branch.Parents()
if !parentBranchIDs.Add(newParentBranchID) {
return
}

parentBranchIDs.Delete(MasterBranchID)
parentBranchIDs.DeleteAll(previousParents)

if cachedChildBranch, stored := b.childBranchStorage.StoreIfAbsent(NewChildBranch(newParentBranchID, branchID)); stored {
cachedChildBranch.Release()
Expand All @@ -188,11 +188,7 @@ func (b *BranchDAG) AddBranchParent(branchID, newParentBranchID BranchID) (err e
if branch.SetParents(parentBranchIDs) {
b.Events.BranchParentsUpdated.Trigger(&BranchParentUpdate{branchID, parentBranchIDs})
}
}) {
return errors.Errorf("failed to unwrap Branch: %w", cerrors.ErrFatal)
}

return nil
})
}

// RemoveConfirmedBranches returns the BranchIDs of the pending and rejected Branches that are
Expand Down
36 changes: 18 additions & 18 deletions packages/refactored/branchdag/branch_dag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,33 @@ func TestBranchDAG_RetrieveBranch(t *testing.T) {
err := branchDAG.Prune()
require.NoError(t, err)

branchID2 := branchDAG.CreateBranch(utxo.TransactionID{2}, NewBranchIDs(MasterBranchID), NewConflictIDs(ConflictID{0}, ConflictID{1}))
cachedBranch2 := branchDAG.Branch(branchID2)
assert.True(t, branchDAG.CreateBranch(utxo.TransactionID{2}, NewBranchIDs(MasterBranchID), NewConflictIDs(ConflictID{0}, ConflictID{1})))
cachedBranch2 := branchDAG.Branch(utxo.TransactionID{2})
defer cachedBranch2.Release()
Branch2, exists := cachedBranch2.Unwrap()
require.True(t, exists)
assert.Equal(t, NewBranchIDs(MasterBranchID), Branch2.Parents())
assert.Equal(t, NewConflictIDs(ConflictID{0}, ConflictID{1}), Branch2.Conflicts())

branchID3 := branchDAG.CreateBranch(utxo.TransactionID{3}, NewBranchIDs(Branch2.ID()), NewConflictIDs(ConflictID{0}, ConflictID{1}, ConflictID{2}))
cachedBranch3 := branchDAG.Branch(branchID3)
assert.True(t, branchDAG.CreateBranch(utxo.TransactionID{3}, NewBranchIDs(Branch2.ID()), NewConflictIDs(ConflictID{0}, ConflictID{1}, ConflictID{2})))
cachedBranch3 := branchDAG.Branch(utxo.TransactionID{3})
defer cachedBranch3.Release()
Branch3, exists := cachedBranch3.Unwrap()
require.True(t, exists)

assert.Equal(t, NewBranchIDs(Branch2.ID()), Branch3.Parents())
assert.Equal(t, NewConflictIDs(ConflictID{0}, ConflictID{1}, ConflictID{2}), Branch3.Conflicts())

branchID2 = branchDAG.CreateBranch(utxo.TransactionID{2}, NewBranchIDs(MasterBranchID), NewConflictIDs(ConflictID{0}, ConflictID{1}, ConflictID{2}))
cachedBranch2 = branchDAG.Branch(branchID2)
assert.False(t, branchDAG.CreateBranch(utxo.TransactionID{2}, NewBranchIDs(MasterBranchID), NewConflictIDs(ConflictID{0}, ConflictID{1}, ConflictID{2})))
cachedBranch2 = branchDAG.Branch(utxo.TransactionID{2})
defer cachedBranch2.Release()
Branch2, exists = cachedBranch2.Unwrap()
require.True(t, exists)

assert.Equal(t, NewConflictIDs(ConflictID{0}, ConflictID{1}, ConflictID{2}), Branch2.Conflicts())

branchID4 := branchDAG.CreateBranch(utxo.TransactionID{4}, NewBranchIDs(Branch3.ID(), Branch3.ID()), NewConflictIDs(ConflictID{3}))
cachedBranch4 := branchDAG.Branch(branchID4)
assert.True(t, branchDAG.CreateBranch(utxo.TransactionID{4}, NewBranchIDs(Branch3.ID(), Branch3.ID()), NewConflictIDs(ConflictID{3})))
cachedBranch4 := branchDAG.Branch(utxo.TransactionID{4})
defer cachedBranch4.Release()
Branch4, exists := cachedBranch4.Unwrap()
require.True(t, exists)
Expand All @@ -60,14 +60,14 @@ func TestBranchDAG_ConflictMembers(t *testing.T) {
require.NoError(t, err)

// create initial branches
branchID2 := branchDAG.CreateBranch(utxo.TransactionID{2}, NewBranchIDs(MasterBranchID), NewConflictIDs(ConflictID{0}))
cachedBranch2 := branchDAG.Branch(branchID2)
assert.True(t, branchDAG.CreateBranch(utxo.TransactionID{2}, NewBranchIDs(MasterBranchID), NewConflictIDs(ConflictID{0})))
cachedBranch2 := branchDAG.Branch(utxo.TransactionID{2})
defer cachedBranch2.Release()
branch2, exists := cachedBranch2.Unwrap()
assert.True(t, exists)

branchID3 := branchDAG.CreateBranch(utxo.TransactionID{3}, NewBranchIDs(MasterBranchID), NewConflictIDs(ConflictID{0}))
cachedBranch3 := branchDAG.Branch(branchID3)
assert.True(t, branchDAG.CreateBranch(utxo.TransactionID{3}, NewBranchIDs(MasterBranchID), NewConflictIDs(ConflictID{0})))
cachedBranch3 := branchDAG.Branch(utxo.TransactionID{3})
defer cachedBranch3.Release()
branch3, exists := cachedBranch3.Unwrap()
assert.True(t, exists)
Expand All @@ -83,8 +83,8 @@ func TestBranchDAG_ConflictMembers(t *testing.T) {
assert.Equal(t, expectedConflictMembers, actualConflictMembers)

// add branch 4
branchID4 := branchDAG.CreateBranch(utxo.TransactionID{4}, NewBranchIDs(MasterBranchID), NewConflictIDs(ConflictID{0}))
cachedBranch4 := branchDAG.Branch(branchID4)
assert.True(t, branchDAG.CreateBranch(utxo.TransactionID{4}, NewBranchIDs(MasterBranchID), NewConflictIDs(ConflictID{0})))
cachedBranch4 := branchDAG.Branch(utxo.TransactionID{4})
defer cachedBranch4.Release()
branch4, exists := cachedBranch4.Unwrap()
assert.True(t, exists)
Expand Down Expand Up @@ -212,14 +212,14 @@ func createBranch(t *testing.T, branchDAG *BranchDAG, branchAlias string, parent
var randomTxID utxo.TransactionID
if err := randomTxID.FromRandomness(); err != nil {
t.Error(err)
return UndefinedBranchID
return BranchID{}
}

branchID := branchDAG.CreateBranch(randomTxID, parents, conflictIDs)
cachedBranch := branchDAG.Branch(branchID)
assert.True(t, branchDAG.CreateBranch(randomTxID, parents, conflictIDs))
cachedBranch := branchDAG.Branch(randomTxID)
cachedBranch.Release()

RegisterBranchIDAlias(branchID, branchAlias)

return branchID
return randomTxID
}
14 changes: 0 additions & 14 deletions packages/refactored/generics/filter/filters.go

This file was deleted.

16 changes: 10 additions & 6 deletions packages/refactored/ledger/booker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ func (b *Booker) bookTransactionCommand(params *params, next dataflow.Next[*para
defer cachedOutputsMetadata.Release()
params.OutputsMetadata = NewOutputsMetadata(cachedOutputsMetadata.Unwrap()...)

generics.ForEach(params.Consumers, func(consumer *Consumer) {
consumer.SetBooked()
})
params.TransactionMetadata.SetBooked(true)
b.markTransactionBooked(params.TransactionMetadata, params.Consumers)

b.TransactionBookedEvent.Trigger(params.Transaction.ID())

Expand All @@ -44,6 +41,13 @@ func (b *Booker) bookTransaction(txID TransactionID, txMetadata *TransactionMeta
return b.bookOutputs(txMetadata, outputs, inheritedBranchIDs)
}

func (b *Booker) markTransactionBooked(txMetadata *TransactionMetadata, consumers []*Consumer) {
generics.ForEach(consumers, func(consumer *Consumer) {
consumer.SetBooked()
})
txMetadata.SetBooked(true)
}

func (b *Booker) inheritBranchIDsFromInputs(txID TransactionID, inputsMetadata OutputsMetadata) (inheritedBranchIDs branchdag.BranchIDs) {
conflictingInputIDs, consumersToFork := b.determineConflictDetails(txID, inputsMetadata)
if conflictingInputIDs.Size() == 0 {
Expand Down Expand Up @@ -106,8 +110,6 @@ func (b *Booker) forkTransaction(tx *Transaction, txMetadata *TransactionMetadat
conflictingInputs := b.resolveInputs(tx.Inputs()).Intersect(outputsSpentByConflictingTx)
previousParentBranches := txMetadata.BranchIDs()

fmt.Println(txMetadata.ID(), previousParentBranches)

if !b.CreateBranch(txMetadata.ID(), previousParentBranches, conflictingInputs) || !b.updateBranchesAfterFork(txMetadata, txMetadata.ID(), previousParentBranches) {
b.Unlock(txMetadata.ID())
return
Expand Down Expand Up @@ -139,7 +141,9 @@ func (b *Booker) propagateForkedBranchToFutureCone(txMetadata *TransactionMetada

func (b *Booker) updateBranchesAfterFork(txMetadata *TransactionMetadata, forkedBranchID branchdag.BranchID, previousParents branchdag.BranchIDs) bool {
if txMetadata.IsConflicting() {
b.BranchDAG.UpdateParentsAfterFork(txMetadata.ID(), forkedBranchID, previousParents)
fmt.Println("conflicting")
return false
}

if txMetadata.BranchIDs().Has(forkedBranchID) {
Expand Down
42 changes: 42 additions & 0 deletions packages/refactored/ledger/dataflow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ledger

import (
"github.com/iotaledger/hive.go/generics/dataflow"
)

type DataFlow struct {
*Ledger
}

func NewDataFlow(ledger *Ledger) *DataFlow {
return &DataFlow{
ledger,
}
}

func (d *DataFlow) storeAndProcessTransaction() *dataflow.DataFlow[*params] {
return dataflow.New[*params](
d.storeTransactionCommand,
d.processTransaction().ChainedCommand,
)
}

func (d *DataFlow) processTransaction() *dataflow.DataFlow[*params] {
return dataflow.New[*params](
d.initConsumersCommand,
d.checkTransaction().ChainedCommand,
d.bookTransactionCommand,
).WithErrorCallback(func(err error, params *params) {
d.ErrorEvent.Trigger(err)

// TODO: mark Transaction as invalid and trigger invalid event
})
}

func (d *DataFlow) checkTransaction() *dataflow.DataFlow[*params] {
return dataflow.New[*params](
d.checkSolidityCommand,
d.checkOutputsCausallyRelatedCommand,
d.executeTransactionCommand,
)
}
18 changes: 13 additions & 5 deletions packages/refactored/ledger/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ import (
"github.com/iotaledger/hive.go/generics/dataflow"

"github.com/iotaledger/goshimmer/packages/refactored/generics"
"github.com/iotaledger/goshimmer/packages/refactored/utxo"
)

type Executor struct {
type VM struct {
*Ledger

vm utxo.VM
}

func NewExecutor(ledger *Ledger) (new *Executor) {
return &Executor{
func NewVM(ledger *Ledger, vm utxo.VM) (new *VM) {
return &VM{
Ledger: ledger,
vm: vm,
}
}

func (e *Executor) executeTransactionCommand(params *params, next dataflow.Next[*params]) (err error) {
utxoOutputs, err := e.vm.ExecuteTransaction(params.Transaction.Transaction, params.Inputs.UTXOOutputs())
func (v *VM) executeTransactionCommand(params *params, next dataflow.Next[*params]) (err error) {
utxoOutputs, err := v.vm.ExecuteTransaction(params.Transaction.Transaction, params.Inputs.UTXOOutputs())
if err != nil {
return errors.Errorf("failed to execute transaction with %s: %w", params.Transaction.ID(), ErrTransactionInvalid)
}
Expand All @@ -27,3 +31,7 @@ func (e *Executor) executeTransactionCommand(params *params, next dataflow.Next[

return next(params)
}

func (v *VM) resolveInputs(inputs []Input) (outputIDs OutputIDs) {
return NewOutputIDs(generics.Map(inputs, v.vm.ResolveInput)...)
}
Loading

0 comments on commit d469cbf

Please sign in to comment.