Skip to content

Commit

Permalink
Feat: added comment
Browse files Browse the repository at this point in the history
  • Loading branch information
hmoog committed Apr 11, 2022
1 parent d84c7fd commit 6de8e17
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
1 change: 1 addition & 0 deletions packages/ledger/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Events struct {
// TransactionBranchIDUpdated is an event that gets triggered whenever the Branch of a Transaction is updated.
TransactionBranchIDUpdated *event.Event[*TransactionBranchIDUpdatedEvent]

// TransactionInvalid is an event that gets triggered whenever a Transaction is found to be invalid.
TransactionInvalid *event.Event[*TransactionInvalidEvent]

// Error is event that gets triggered whenever an error occurs while processing a Transaction.
Expand Down
6 changes: 5 additions & 1 deletion packages/ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Ledger struct {
Events *Events

// Storage is a dictionary for storage related API endpoints.
Storage *storage
Storage *Storage

// Utils is a dictionary for utility methods that simplify the interaction with the Ledger.
Utils *Utils
Expand Down Expand Up @@ -84,6 +84,10 @@ func (l *Ledger) StoreAndProcessTransaction(tx utxo.Transaction) (err error) {
func (l *Ledger) PruneTransaction(txID utxo.TransactionID) {
// TODO: IMPLEMENT PRUNING LOGIC
}
func (l *Ledger) Shutdown() {
l.Storage.Shutdown()
l.BranchDAG.Shutdown()
}

// processTransaction tries to book a single Transaction.
func (l *Ledger) processTransaction(tx *Transaction) (err error) {
Expand Down
28 changes: 14 additions & 14 deletions packages/ledger/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (

// region storage //////////////////////////////////////////////////////////////////////////////////////////////////////

// storage is a Ledger component that bundles the storage related API.
type storage struct {
// Storage is a Ledger component that bundles the storage related API.
type Storage struct {
// transactionStorage is an object storage used to persist Transactions objects.
transactionStorage *objectstorage.ObjectStorage[*Transaction]

Expand All @@ -36,8 +36,8 @@ type storage struct {
}

// newStorage returns a new storage instance for the given Ledger.
func newStorage(ledger *Ledger) (new *storage) {
return &storage{
func newStorage(ledger *Ledger) (new *Storage) {
return &Storage{
transactionStorage: objectstorage.New[*Transaction](
ledger.options.store.WithRealm([]byte{database.PrefixLedger, PrefixTransactionStorage}),
ledger.options.cacheTimeProvider.CacheTime(ledger.options.transactionCacheTime),
Expand Down Expand Up @@ -74,7 +74,7 @@ func newStorage(ledger *Ledger) (new *storage) {

// CachedTransaction retrieves the CachedObject representing the named Transaction. The optional computeIfAbsentCallback
// can be used to dynamically initialize a non-existing Transaction.
func (s *storage) CachedTransaction(transactionID utxo.TransactionID, computeIfAbsentCallback ...func(transactionID utxo.TransactionID) *Transaction) (cachedTransaction *objectstorage.CachedObject[*Transaction]) {
func (s *Storage) CachedTransaction(transactionID utxo.TransactionID, computeIfAbsentCallback ...func(transactionID utxo.TransactionID) *Transaction) (cachedTransaction *objectstorage.CachedObject[*Transaction]) {
if len(computeIfAbsentCallback) >= 1 {
return s.transactionStorage.ComputeIfAbsent(transactionID.Bytes(), func(key []byte) *Transaction {
return computeIfAbsentCallback[0](transactionID)
Expand All @@ -86,7 +86,7 @@ func (s *storage) CachedTransaction(transactionID utxo.TransactionID, computeIfA

// CachedTransactionMetadata retrieves the CachedObject representing the named TransactionMetadata. The optional
// computeIfAbsentCallback can be used to dynamically initialize a non-existing TransactionMetadata.
func (s *storage) CachedTransactionMetadata(transactionID utxo.TransactionID, computeIfAbsentCallback ...func(transactionID utxo.TransactionID) *TransactionMetadata) (cachedTransactionMetadata *objectstorage.CachedObject[*TransactionMetadata]) {
func (s *Storage) CachedTransactionMetadata(transactionID utxo.TransactionID, computeIfAbsentCallback ...func(transactionID utxo.TransactionID) *TransactionMetadata) (cachedTransactionMetadata *objectstorage.CachedObject[*TransactionMetadata]) {
if len(computeIfAbsentCallback) >= 1 {
return s.transactionMetadataStorage.ComputeIfAbsent(transactionID.Bytes(), func(key []byte) *TransactionMetadata {
return computeIfAbsentCallback[0](transactionID)
Expand All @@ -98,7 +98,7 @@ func (s *storage) CachedTransactionMetadata(transactionID utxo.TransactionID, co

// CachedOutput retrieves the CachedObject representing the named Output. The optional computeIfAbsentCallback can be
// used to dynamically initialize a non-existing Output.
func (s *storage) CachedOutput(outputID utxo.OutputID, computeIfAbsentCallback ...func(outputID utxo.OutputID) *Output) (cachedOutput *objectstorage.CachedObject[*Output]) {
func (s *Storage) CachedOutput(outputID utxo.OutputID, computeIfAbsentCallback ...func(outputID utxo.OutputID) *Output) (cachedOutput *objectstorage.CachedObject[*Output]) {
if len(computeIfAbsentCallback) >= 1 {
return s.outputStorage.ComputeIfAbsent(outputID.Bytes(), func(key []byte) *Output {
return computeIfAbsentCallback[0](outputID)
Expand All @@ -109,7 +109,7 @@ func (s *storage) CachedOutput(outputID utxo.OutputID, computeIfAbsentCallback .
}

// CachedOutputs retrieves the CachedObjects containing the named Outputs.
func (s *storage) CachedOutputs(outputIDs utxo.OutputIDs) (cachedOutputs objectstorage.CachedObjects[*Output]) {
func (s *Storage) CachedOutputs(outputIDs utxo.OutputIDs) (cachedOutputs objectstorage.CachedObjects[*Output]) {
cachedOutputs = make(objectstorage.CachedObjects[*Output], 0)
for it := outputIDs.Iterator(); it.HasNext(); {
cachedOutputs = append(cachedOutputs, s.CachedOutput(it.Next()))
Expand All @@ -120,7 +120,7 @@ func (s *storage) CachedOutputs(outputIDs utxo.OutputIDs) (cachedOutputs objects

// CachedOutputMetadata retrieves the CachedObject representing the named OutputMetadata. The optional
// computeIfAbsentCallback can be used to dynamically initialize a non-existing OutputMetadata.
func (s *storage) CachedOutputMetadata(outputID utxo.OutputID, computeIfAbsentCallback ...func(outputID utxo.OutputID) *OutputMetadata) (cachedOutputMetadata *objectstorage.CachedObject[*OutputMetadata]) {
func (s *Storage) CachedOutputMetadata(outputID utxo.OutputID, computeIfAbsentCallback ...func(outputID utxo.OutputID) *OutputMetadata) (cachedOutputMetadata *objectstorage.CachedObject[*OutputMetadata]) {
if len(computeIfAbsentCallback) >= 1 {
return s.outputMetadataStorage.ComputeIfAbsent(outputID.Bytes(), func(key []byte) *OutputMetadata {
return computeIfAbsentCallback[0](outputID)
Expand All @@ -131,7 +131,7 @@ func (s *storage) CachedOutputMetadata(outputID utxo.OutputID, computeIfAbsentCa
}

// CachedOutputsMetadata retrieves the CachedObjects containing the named OutputMetadata.
func (s *storage) CachedOutputsMetadata(outputIDs utxo.OutputIDs) (cachedOutputsMetadata objectstorage.CachedObjects[*OutputMetadata]) {
func (s *Storage) CachedOutputsMetadata(outputIDs utxo.OutputIDs) (cachedOutputsMetadata objectstorage.CachedObjects[*OutputMetadata]) {
cachedOutputsMetadata = make(objectstorage.CachedObjects[*OutputMetadata], 0)
for it := outputIDs.Iterator(); it.HasNext(); {
cachedOutputsMetadata = append(cachedOutputsMetadata, s.CachedOutputMetadata(it.Next()))
Expand All @@ -142,7 +142,7 @@ func (s *storage) CachedOutputsMetadata(outputIDs utxo.OutputIDs) (cachedOutputs

// CachedConsumer retrieves the CachedObject representing the named Consumer. The optional computeIfAbsentCallback can
// be used to dynamically initialize a non-existing Consumer.
func (s *storage) CachedConsumer(outputID utxo.OutputID, txID utxo.TransactionID, computeIfAbsentCallback ...func(outputID utxo.OutputID, txID utxo.TransactionID) *Consumer) (cachedConsumer *objectstorage.CachedObject[*Consumer]) {
func (s *Storage) CachedConsumer(outputID utxo.OutputID, txID utxo.TransactionID, computeIfAbsentCallback ...func(outputID utxo.OutputID, txID utxo.TransactionID) *Consumer) (cachedConsumer *objectstorage.CachedObject[*Consumer]) {
if len(computeIfAbsentCallback) >= 1 {
return s.consumerStorage.ComputeIfAbsent(byteutils.ConcatBytes(outputID.Bytes(), txID.Bytes()), func(key []byte) *Consumer {
return computeIfAbsentCallback[0](outputID, txID)
Expand All @@ -153,7 +153,7 @@ func (s *storage) CachedConsumer(outputID utxo.OutputID, txID utxo.TransactionID
}

// CachedConsumers retrieves the CachedObjects containing the named Consumers.
func (s *storage) CachedConsumers(outputID utxo.OutputID) (cachedConsumers objectstorage.CachedObjects[*Consumer]) {
func (s *Storage) CachedConsumers(outputID utxo.OutputID) (cachedConsumers objectstorage.CachedObjects[*Consumer]) {
cachedConsumers = make(objectstorage.CachedObjects[*Consumer], 0)
s.consumerStorage.ForEach(func(key []byte, cachedObject *objectstorage.CachedObject[*Consumer]) bool {
cachedConsumers = append(cachedConsumers, cachedObject)
Expand All @@ -164,7 +164,7 @@ func (s *storage) CachedConsumers(outputID utxo.OutputID) (cachedConsumers objec
}

// storeTransactionCommand is a ChainedCommand that stores a Transaction.
func (s *storage) storeTransactionCommand(params *dataFlowParams, next dataflow.Next[*dataFlowParams]) (err error) {
func (s *Storage) storeTransactionCommand(params *dataFlowParams, next dataflow.Next[*dataFlowParams]) (err error) {
created := false
cachedTransactionMetadata := s.CachedTransactionMetadata(params.Transaction.ID(), func(txID utxo.TransactionID) *TransactionMetadata {
s.transactionStorage.Store(params.Transaction).Release()
Expand Down Expand Up @@ -197,7 +197,7 @@ func (s *storage) storeTransactionCommand(params *dataFlowParams, next dataflow.
}

// initConsumers creates the Consumers of a Transaction if they didn't exist before.
func (s *storage) initConsumers(outputIDs utxo.OutputIDs, txID utxo.TransactionID) (cachedConsumers objectstorage.CachedObjects[*Consumer]) {
func (s *Storage) initConsumers(outputIDs utxo.OutputIDs, txID utxo.TransactionID) (cachedConsumers objectstorage.CachedObjects[*Consumer]) {
cachedConsumers = make(objectstorage.CachedObjects[*Consumer], 0)
for it := outputIDs.Iterator(); it.HasNext(); {
cachedConsumers = append(cachedConsumers, s.CachedConsumer(it.Next(), txID, NewConsumer))
Expand Down

0 comments on commit 6de8e17

Please sign in to comment.