From c754a30ce739561a1dabca1a9b54433b221178c6 Mon Sep 17 00:00:00 2001 From: Hans Moog <3293976+hmoog@users.noreply.github.com> Date: Thu, 7 Apr 2022 17:27:16 +0200 Subject: [PATCH] Refactor: refactored code --- packages/ledger/branchdag/branchdag.go | 2 +- packages/ledger/dataflow.go | 2 +- packages/ledger/ledger.go | 36 +++++++++++++++++++++----- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/packages/ledger/branchdag/branchdag.go b/packages/ledger/branchdag/branchdag.go index e46c5f46de..7a5a7e0efe 100644 --- a/packages/ledger/branchdag/branchdag.go +++ b/packages/ledger/branchdag/branchdag.go @@ -7,7 +7,7 @@ import ( "github.com/iotaledger/hive.go/generics/walker" ) -// BranchDAG is an entity that manages conflicting versions of a quadruple-entry-accounting ledger and their causal +// BranchDAG is an entity that manages conflicting versions of a quadruple-entry accounting ledger and their causal // relationships. type BranchDAG struct { // Events is a dictionary for BranchDAG related events. diff --git a/packages/ledger/dataflow.go b/packages/ledger/dataflow.go index 258ee8fd7c..5db7d05a34 100644 --- a/packages/ledger/dataflow.go +++ b/packages/ledger/dataflow.go @@ -8,7 +8,7 @@ import ( // region dataFlow ///////////////////////////////////////////////////////////////////////////////////////////////////// -// dataFlow is a Ledger component that defines the data flow (h)ow the different commands are chained together). +// dataFlow is a Ledger component that defines the data flow (how the different commands are chained together). type dataFlow struct { // ledger contains a reference to the Ledger that created the Utils. ledger *Ledger diff --git a/packages/ledger/ledger.go b/packages/ledger/ledger.go index da79289ff3..78de94f66f 100644 --- a/packages/ledger/ledger.go +++ b/packages/ledger/ledger.go @@ -13,18 +13,35 @@ import ( // Ledger is an implementation of a "realities-ledger" that manages Outputs according to the principles of the // quadruple-entry accounting. It acts as a wrapper for the underlying components and exposes the public facing API. type Ledger struct { - Events *Events - Storage *storage - Utils *Utils + // Events is a dictionary for Ledger related events. + Events *Events + + // Storage is a dictionary for storage related API endpoints. + Storage *storage + + // Utils is a dictionary for utility methods that simplify the interaction with the Ledger. + Utils *Utils + + // BranchDAG is a reference to the BranchDAG that is used by this Ledger. BranchDAG *branchdag.BranchDAG - dataFlow *dataFlow + // dataFlow is a Ledger component that defines the data flow (how the different commands are chained together) + dataFlow *dataFlow + + // validator is a Ledger component that bundles the API that is used to check the validity of a Transaction validator *validator - booker *booker - options *options - mutex *syncutils.DAGMutex[utxo.TransactionID] + + // booker is a Ledger component that bundles the booking related API. + booker *booker + + // options is a dictionary for configuration parameters of the Ledger. + options *options + + // mutex is a DAGMutex that is used to make the Ledger thread safe. + mutex *syncutils.DAGMutex[utxo.TransactionID] } +// New returns a new Ledger from the given options. func New(options ...Option) (ledger *Ledger) { ledger = &Ledger{ Events: newEvents(), @@ -46,10 +63,12 @@ func New(options ...Option) (ledger *Ledger) { return ledger } +// CheckTransaction checks the validity of a Transaction. func (l *Ledger) CheckTransaction(tx utxo.Transaction) (err error) { return l.dataFlow.checkTransaction().Run(newDataFlowParams(NewTransaction(tx))) } +// StoreAndProcessTransaction stores and processes the given Transaction. func (l *Ledger) StoreAndProcessTransaction(tx utxo.Transaction) (err error) { l.mutex.Lock(tx.ID()) defer l.mutex.Unlock(tx.ID()) @@ -57,6 +76,7 @@ func (l *Ledger) StoreAndProcessTransaction(tx utxo.Transaction) (err error) { return l.dataFlow.storeAndProcessTransaction().Run(newDataFlowParams(NewTransaction(tx))) } +// processTransaction tries to book a single Transaction. func (l *Ledger) processTransaction(tx *Transaction) (err error) { l.mutex.Lock(tx.ID()) defer l.mutex.Unlock(tx.ID()) @@ -64,6 +84,8 @@ func (l *Ledger) processTransaction(tx *Transaction) (err error) { return l.dataFlow.processTransaction().Run(newDataFlowParams(tx)) } +// processConsumingTransactions tries to book the transactions approving the given OutputIDs (it is used to propagate +// solidity). func (l *Ledger) processConsumingTransactions(outputIDs utxo.OutputIDs) { for it := l.Utils.UnprocessedConsumingTransactions(outputIDs).Iterator(); it.HasNext(); { consumingTransactionID := it.Next()