Skip to content

Commit

Permalink
Merge branch 'feat/utxovm' of github.com:iotaledger/goshimmer into fe…
Browse files Browse the repository at this point in the history
…at/utxovm
  • Loading branch information
jonastheis committed Mar 31, 2022
2 parents 114f4e4 + 3d9e804 commit dc43c29
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 225 deletions.
17 changes: 9 additions & 8 deletions packages/refactored/ledger/booker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

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

type Booker struct {
Expand Down Expand Up @@ -60,15 +61,15 @@ func (b *Booker) bookTransaction(txMetadata *TransactionMetadata, inputsMetadata
b.TransactionBookedEvent.Trigger(txMetadata.ID())
}

func (b *Booker) inheritBranchIDs(txID TransactionID, inputsMetadata OutputsMetadata) (inheritedBranchIDs branchdag.BranchIDs) {
func (b *Booker) inheritBranchIDs(txID utxo.TransactionID, inputsMetadata OutputsMetadata) (inheritedBranchIDs branchdag.BranchIDs) {
conflictingInputIDs, consumersToFork := b.determineConflictDetails(txID, inputsMetadata)
if conflictingInputIDs.Size() == 0 {
return b.RemoveConfirmedBranches(inputsMetadata.BranchIDs())
}

b.CreateBranch(txID, b.RemoveConfirmedBranches(inputsMetadata.BranchIDs()), conflictingInputIDs)

_ = consumersToFork.ForEach(func(transactionID TransactionID) (err error) {
_ = consumersToFork.ForEach(func(transactionID utxo.TransactionID) (err error) {
b.WithTransactionAndMetadata(transactionID, func(tx *Transaction, txMetadata *TransactionMetadata) {
b.forkTransaction(tx, txMetadata, conflictingInputIDs)
})
Expand All @@ -91,17 +92,17 @@ func (b *Booker) storeOutputs(outputs Outputs, branchIDs branchdag.BranchIDs) {
})
}

func (b *Booker) determineConflictDetails(txID TransactionID, inputsMetadata OutputsMetadata) (conflictingInputIDs OutputIDs, consumersToFork TransactionIDs) {
conflictingInputIDs = NewOutputIDs()
consumersToFork = NewTransactionIDs()
func (b *Booker) determineConflictDetails(txID utxo.TransactionID, inputsMetadata OutputsMetadata) (conflictingInputIDs utxo.OutputIDs, consumersToFork utxo.TransactionIDs) {
conflictingInputIDs = utxo.NewOutputIDs()
consumersToFork = utxo.NewTransactionIDs()

_ = inputsMetadata.ForEach(func(outputMetadata *OutputMetadata) error {
isConflicting, consumerToFork := outputMetadata.RegisterProcessedConsumer(txID)
if isConflicting {
conflictingInputIDs.Add(outputMetadata.ID())
}

if consumerToFork != EmptyTransactionID {
if consumerToFork != utxo.EmptyTransactionID {
consumersToFork.Add(consumerToFork)
}

Expand All @@ -111,7 +112,7 @@ func (b *Booker) determineConflictDetails(txID TransactionID, inputsMetadata Out
return conflictingInputIDs, consumersToFork
}

func (b *Booker) forkTransaction(tx *Transaction, txMetadata *TransactionMetadata, outputsSpentByConflictingTx OutputIDs) {
func (b *Booker) forkTransaction(tx *Transaction, txMetadata *TransactionMetadata, outputsSpentByConflictingTx utxo.OutputIDs) {
b.Lock(txMetadata.ID())

conflictingInputs := b.resolveInputs(tx.Inputs()).Intersect(outputsSpentByConflictingTx)
Expand All @@ -132,7 +133,7 @@ func (b *Booker) forkTransaction(tx *Transaction, txMetadata *TransactionMetadat
}

func (b *Booker) propagateForkedBranchToFutureCone(txMetadata *TransactionMetadata, forkedBranchID branchdag.BranchID, previousParentBranches branchdag.BranchIDs) {
b.WalkConsumingTransactionMetadata(txMetadata.OutputIDs(), func(consumingTxMetadata *TransactionMetadata, walker *walker.Walker[OutputID]) {
b.WalkConsumingTransactionMetadata(txMetadata.OutputIDs(), func(consumingTxMetadata *TransactionMetadata, walker *walker.Walker[utxo.OutputID]) {
b.Lock(consumingTxMetadata.ID())
defer b.Unlock(consumingTxMetadata.ID())

Expand Down
8 changes: 5 additions & 3 deletions packages/refactored/ledger/dataflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package ledger
import (
"github.com/iotaledger/hive.go/generics/dataflow"
"github.com/iotaledger/hive.go/generics/event"

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

type DataFlow struct {
Expand All @@ -16,7 +18,7 @@ func NewDataFlow(ledger *Ledger) *DataFlow {
}

func (d *DataFlow) Setup() {
d.TransactionBookedEvent.Attach(event.NewClosure[TransactionID](func(txID TransactionID) {
d.TransactionBookedEvent.Attach(event.NewClosure[utxo.TransactionID](func(txID utxo.TransactionID) {
d.CachedTransaction(txID).Consume(func(tx *Transaction) {
_ = d.Ledger.processTransaction(tx)
})
Expand Down Expand Up @@ -46,14 +48,14 @@ func (d *DataFlow) checkTransaction() *dataflow.DataFlow[*dataFlowParams] {
return dataflow.New[*dataFlowParams](
d.checkSolidityCommand,
d.checkOutputsCausallyRelatedCommand,
d.executeTransactionCommand,
d.checkTransactionExecutionCommand,
)
}

type dataFlowParams struct {
Transaction *Transaction
TransactionMetadata *TransactionMetadata
InputIDs OutputIDs
InputIDs utxo.OutputIDs
Inputs Outputs
InputsMetadata OutputsMetadata
Consumers []*Consumer
Expand Down
52 changes: 43 additions & 9 deletions packages/refactored/ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ import (
// region Ledger ///////////////////////////////////////////////////////////////////////////////////////////////////////

type Ledger struct {
TransactionStoredEvent *event.Event[TransactionID]
TransactionBookedEvent *event.Event[TransactionID]
TransactionStoredEvent *event.Event[utxo.TransactionID]
TransactionBookedEvent *event.Event[utxo.TransactionID]
ErrorEvent *event.Event[error]

*Storage
*Solidifier
*Validator
*VM
*Booker

*DataFlow
Expand All @@ -34,8 +32,8 @@ type Ledger struct {

func New(store kvstore.KVStore, vm utxo.VM, options ...Option) (ledger *Ledger) {
ledger = &Ledger{
TransactionStoredEvent: event.New[TransactionID](),
TransactionBookedEvent: event.New[TransactionID](),
TransactionStoredEvent: event.New[utxo.TransactionID](),
TransactionBookedEvent: event.New[utxo.TransactionID](),
ErrorEvent: event.New[error](),

BranchDAG: branchdag.NewBranchDAG(store, database.NewCacheTimeProvider(0)),
Expand All @@ -46,9 +44,7 @@ func New(store kvstore.KVStore, vm utxo.VM, options ...Option) (ledger *Ledger)

ledger.DataFlow = NewDataFlow(ledger)
ledger.Storage = NewStorage(ledger)
ledger.Solidifier = NewSolidifier(ledger)
ledger.Validator = NewValidator(ledger)
ledger.VM = NewVM(ledger, vm)
ledger.Validator = NewValidator(ledger, vm)
ledger.Booker = NewBooker(ledger)
ledger.Utils = NewUtils(ledger)

Expand Down Expand Up @@ -88,3 +84,41 @@ func (l *Ledger) processTransaction(tx *Transaction) (err error) {
}

// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////

// region Options //////////////////////////////////////////////////////////////////////////////////////////////////////

// Option represents the return type of optional parameters that can be handed into the constructor of the Ledger
// to configure its behavior.
type Option func(*Options)

// Options is a container for all configurable parameters of the Ledger.
type Options struct {
Store kvstore.KVStore
CacheTimeProvider *database.CacheTimeProvider
LazyBookingEnabled bool
}

// Store is an Option for the Ledger that allows to specify which storage layer is supposed to be used to persist
// data.
func Store(store kvstore.KVStore) Option {
return func(options *Options) {
options.Store = store
}
}

// CacheTimeProvider is an Option for the Tangle that allows to override hard coded cache time.
func CacheTimeProvider(cacheTimeProvider *database.CacheTimeProvider) Option {
return func(options *Options) {
options.CacheTimeProvider = cacheTimeProvider
}
}

// LazyBookingEnabled is an Option for the Ledger that allows to specify if the ledger state should lazy book
// conflicts that look like they have been decided already.
func LazyBookingEnabled(enabled bool) Option {
return func(options *Options) {
options.LazyBookingEnabled = enabled
}
}

// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////
30 changes: 15 additions & 15 deletions packages/refactored/ledger/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type TransactionMetadata struct {
solidificationTimeMutex sync.RWMutex
lazyBooked bool
lazyBookedMutex sync.RWMutex
outputIDs OutputIDs
outputIDs utxo.OutputIDs
outputIDsMutex sync.RWMutex
gradeOfFinality gof.GradeOfFinality
gradeOfFinalityTime time.Time
Expand Down Expand Up @@ -247,15 +247,15 @@ func (t *TransactionMetadata) SetLazyBooked(lazyBooked bool) (modified bool) {
}

// OutputIDs returns the OutputIDs that this Transaction created.
func (t *TransactionMetadata) OutputIDs() OutputIDs {
func (t *TransactionMetadata) OutputIDs() utxo.OutputIDs {
t.outputIDsMutex.RLock()
defer t.outputIDsMutex.RUnlock()

return t.outputIDs
}

// SetOutputIDs sets the OutputIDs that this Transaction created.
func (t *TransactionMetadata) SetOutputIDs(outputIDs OutputIDs) {
func (t *TransactionMetadata) SetOutputIDs(outputIDs utxo.OutputIDs) {
t.outputIDsMutex.RLock()
defer t.outputIDsMutex.RUnlock()

Expand Down Expand Up @@ -385,30 +385,30 @@ var _ objectstorage.StorableObject = new(Output)
// region Outputs //////////////////////////////////////////////////////////////////////////////////////////////////////

type Outputs struct {
*orderedmap.OrderedMap[OutputID, *Output]
*orderedmap.OrderedMap[utxo.OutputID, *Output]
}

func NewOutputs(outputs ...*Output) (new Outputs) {
new = Outputs{orderedmap.New[OutputID, *Output]()}
new = Outputs{orderedmap.New[utxo.OutputID, *Output]()}
for _, output := range outputs {
new.Set(output.ID(), output)
}

return new
}

func (o Outputs) IDs() (ids OutputIDs) {
outputIDs := make([]OutputID, 0)
o.OrderedMap.ForEach(func(id OutputID, _ *Output) bool {
func (o Outputs) IDs() (ids utxo.OutputIDs) {
outputIDs := make([]utxo.OutputID, 0)
o.OrderedMap.ForEach(func(id utxo.OutputID, _ *Output) bool {
outputIDs = append(outputIDs, id)
return true
})

return NewOutputIDs(outputIDs...)
return utxo.NewOutputIDs(outputIDs...)
}

func (o Outputs) ForEach(callback func(output *Output) (err error)) (err error) {
o.OrderedMap.ForEach(func(_ OutputID, output *Output) bool {
o.OrderedMap.ForEach(func(_ utxo.OutputID, output *Output) bool {
if err = callback(output); err != nil {
return false
}
Expand Down Expand Up @@ -708,11 +708,11 @@ var _ objectstorage.StorableObject = new(OutputMetadata)
// region OutputsMetadata //////////////////////////////////////////////////////////////////////////////////////////////

type OutputsMetadata struct {
*orderedmap.OrderedMap[OutputID, *OutputMetadata]
*orderedmap.OrderedMap[utxo.OutputID, *OutputMetadata]
}

func NewOutputsMetadata(outputsMetadata ...*OutputMetadata) (new OutputsMetadata) {
new = OutputsMetadata{orderedmap.New[OutputID, *OutputMetadata]()}
new = OutputsMetadata{orderedmap.New[utxo.OutputID, *OutputMetadata]()}
for _, outputMetadata := range outputsMetadata {
new.Set(outputMetadata.ID(), outputMetadata)
}
Expand All @@ -733,8 +733,8 @@ func (o OutputsMetadata) Filter(predicate func(outputMetadata *OutputMetadata) b
return filtered
}

func (o OutputsMetadata) IDs() (ids OutputIDs) {
ids = NewOutputIDs()
func (o OutputsMetadata) IDs() (ids utxo.OutputIDs) {
ids = utxo.NewOutputIDs()
_ = o.ForEach(func(outputMetadata *OutputMetadata) (err error) {
ids.Add(outputMetadata.ID())
return nil
Expand All @@ -754,7 +754,7 @@ func (o OutputsMetadata) BranchIDs() (branchIDs branchdag.BranchIDs) {
}

func (o OutputsMetadata) ForEach(callback func(outputMetadata *OutputMetadata) (err error)) (err error) {
o.OrderedMap.ForEach(func(_ OutputID, outputMetadata *OutputMetadata) bool {
o.OrderedMap.ForEach(func(_ utxo.OutputID, outputMetadata *OutputMetadata) bool {
if err = callback(outputMetadata); err != nil {
return false
}
Expand Down
45 changes: 0 additions & 45 deletions packages/refactored/ledger/options.go

This file was deleted.

43 changes: 0 additions & 43 deletions packages/refactored/ledger/solidifier.go

This file was deleted.

Loading

0 comments on commit dc43c29

Please sign in to comment.