Skip to content

Commit

Permalink
Refactor: refactored some code
Browse files Browse the repository at this point in the history
  • Loading branch information
hmoog committed Apr 6, 2022
1 parent ce05f84 commit ba6b873
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 49 deletions.
4 changes: 4 additions & 0 deletions packages/ledger/branchdag/branchdag.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ import (
type BranchDAG struct {
// Events is a dictionary for BranchDAG 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 BranchDAG.
Utils *Utils

// options is a dictionary for configuration parameters of the BranchDAG.
options *options

// inclusionStateMutex is a mutex that prevents that two processes simultaneously write the InclusionState.
inclusionStateMutex sync.RWMutex
}
Expand Down
7 changes: 7 additions & 0 deletions packages/ledger/branchdag/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
type Events struct {
// BranchCreated is an event that gets triggered whenever a new Branch is created.
BranchCreated *event.Event[*BranchCreatedEvent]

// BranchConflictsUpdated is an event that gets triggered whenever the ConflictIDs of a Branch are updated.
BranchConflictsUpdated *event.Event[*BranchConflictsUpdatedEvent]

// BranchParentsUpdated is an event that gets triggered whenever the parent BranchIDs of a Branch are updated.
BranchParentsUpdated *event.Event[*BranchParentsUpdatedEvent]
}
Expand All @@ -33,8 +35,10 @@ func newEvents() *Events {
type BranchCreatedEvent struct {
// BranchID contains the identifier of the newly created Branch.
BranchID BranchID

// ParentBranchIDs contains the parent Branches of the newly created Branch.
ParentBranchIDs BranchIDs

// ConflictIDs contains the set of conflicts that this Branch is involved with.
ConflictIDs ConflictIDs
}
Expand All @@ -48,6 +52,7 @@ type BranchCreatedEvent struct {
type BranchConflictsUpdatedEvent struct {
// BranchID contains the identifier of the updated Branch.
BranchID BranchID

// NewConflictIDs contains the set of conflicts that this Branch was added to.
NewConflictIDs ConflictIDs
}
Expand All @@ -61,8 +66,10 @@ type BranchConflictsUpdatedEvent struct {
type BranchParentsUpdatedEvent struct {
// BranchID contains the identifier of the updated Branch.
BranchID BranchID

// AddedBranch contains the forked parent Branch that replaces the removed parents.
AddedBranch BranchID

// RemovedBranches contains the parent BranchIDs that were replaced by the newly forked Branch.
RemovedBranches BranchIDs
}
Expand Down
11 changes: 11 additions & 0 deletions packages/ledger/branchdag/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,25 @@ import (
type Branch struct {
// id contains the identifier of the Branch.
id BranchID

// parents contains the parent BranchIDs that this Branch depends on.
parents BranchIDs

// parentsMutex contains a mutex that is used to synchronize parallel access to the parents.
parentsMutex sync.RWMutex

// conflictIDs contains the identifiers of the conflicts that this Branch is part of.
conflictIDs ConflictIDs

// conflictIDsMutex contains a mutex that is used to synchronize parallel access to the conflictIDs.
conflictIDsMutex sync.RWMutex

// inclusionState contains the InclusionState of the Branch.
inclusionState InclusionState

// inclusionStateMutex contains a mutex that is used to synchronize parallel access to the inclusionState.
inclusionStateMutex sync.RWMutex

// StorableObjectFlags embeds the properties and methods required to manage to object storage related flags.
objectstorage.StorableObjectFlags
}
Expand Down Expand Up @@ -192,8 +199,10 @@ var _ objectstorage.StorableObject = new(Branch)
type ChildBranch struct {
// parentBranchID contains the identifier of the parent Branch.
parentBranchID BranchID

// childBranchID contains the identifier of the child Branch.
childBranchID BranchID

// StorableObjectFlags embeds the properties and methods required to manage to object storage related flags.
objectstorage.StorableObjectFlags
}
Expand Down Expand Up @@ -291,8 +300,10 @@ var childBranchKeyPartition = objectstorage.PartitionKey(BranchIDLength, BranchI
type ConflictMember struct {
// conflictID contains the identifier of the conflict.
conflictID ConflictID

// branchID contains the identifier of the Branch.
branchID BranchID

// StorableObjectFlags embeds the properties and methods required to manage to object storage related flags.
objectstorage.StorableObjectFlags
}
Expand Down
22 changes: 5 additions & 17 deletions packages/ledger/branchdag/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,6 @@ func WithChildBranchCacheTime(childBranchCacheTime time.Duration) Option {

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

// region WithConflictCacheTime ////////////////////////////////////////////////////////////////////////////////////////

// WithConflictCacheTime is an Option for the BranchDAG that allows to configure how long Conflict objects stay cached
// after they have been released.
func WithConflictCacheTime(conflictCacheTime time.Duration) Option {
return func(options *options) {
options.conflictCacheTime = conflictCacheTime
}
}

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

// region WithConflictMemberCacheTime //////////////////////////////////////////////////////////////////////////////////

// WithConflictMemberCacheTime is an Option for the BranchDAG that allows to configure how long ConflictMember objects
Expand All @@ -83,8 +71,7 @@ func WithConflictMemberCacheTime(conflictMemberCacheTime time.Duration) Option {

// region Option ///////////////////////////////////////////////////////////////////////////////////////////////////////

// Option represents the return type of optional parameters that can be handed into the constructor of the BranchDAG to
// configure its behavior.
// Option represents a configurable parameter for the BranchDAG that modifies its behavior.
type Option func(*options)

// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -95,14 +82,16 @@ type Option func(*options)
type options struct {
// store contains the KVStore that is used to persist data.
store kvstore.KVStore

// cacheTimeProvider contains the CacheTimeProvider that overrides the local cache times.
cacheTimeProvider *database.CacheTimeProvider

// branchCacheTime contains the duration that Branch objects stay cached after they have been released.
branchCacheTime time.Duration

// childBranchCacheTime contains the duration that ChildBranch objects stay cached after they have been released.
childBranchCacheTime time.Duration
// conflictCacheTime contains the duration that Conflict objects stay cached after they have been released.
conflictCacheTime time.Duration

// conflictMemberCacheTime contains the duration that ConflictMember objects stay cached after they have been
// released.
conflictMemberCacheTime time.Duration
Expand All @@ -116,7 +105,6 @@ func newOptions(option ...Option) (new *options) {
cacheTimeProvider: database.NewCacheTimeProvider(0),
branchCacheTime: 60 * time.Second,
childBranchCacheTime: 60 * time.Second,
conflictCacheTime: 60 * time.Second,
conflictMemberCacheTime: 10 * time.Second,
}).apply(option...)
}
Expand Down
21 changes: 7 additions & 14 deletions packages/ledger/branchdag/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package branchdag

import (
"sync"
"time"

"github.com/cockroachdb/errors"
"github.com/iotaledger/hive.go/byteutils"
Expand All @@ -18,10 +17,13 @@ import (
type Storage struct {
// branchStorage is an object storage used to persist Branch objects.
branchStorage *objectstorage.ObjectStorage[*Branch]

// childBranchStorage is an object storage used to persist ChildBranch objects.
childBranchStorage *objectstorage.ObjectStorage[*ChildBranch]

// conflictMemberStorage is an object storage used to persist ConflictMember objects.
conflictMemberStorage *objectstorage.ObjectStorage[*ConflictMember]

// shutdownOnce is used to ensure that the shutdown routine is executed only a single time.
shutdownOnce sync.Once
}
Expand All @@ -32,29 +34,20 @@ func newStorage(options *options) (new *Storage) {
branchStorage: objectstorage.New[*Branch](
options.store.WithRealm([]byte{database.PrefixBranchDAG, PrefixBranchStorage}),
options.cacheTimeProvider.CacheTime(options.branchCacheTime),
objectstorage.LeakDetectionEnabled(true, objectstorage.LeakDetectionOptions{
MaxConsumersPerObject: 10,
MaxConsumerHoldTime: 5 * time.Second,
}),
objectstorage.LeakDetectionEnabled(false),
),
childBranchStorage: objectstorage.New[*ChildBranch](
options.store.WithRealm([]byte{database.PrefixBranchDAG, PrefixChildBranchStorage}),
childBranchKeyPartition,
options.cacheTimeProvider.CacheTime(options.childBranchCacheTime),
objectstorage.LeakDetectionEnabled(true, objectstorage.LeakDetectionOptions{
MaxConsumersPerObject: 10,
MaxConsumerHoldTime: 5 * time.Second,
}),
objectstorage.LeakDetectionEnabled(false),
objectstorage.StoreOnCreation(true),
),
conflictMemberStorage: objectstorage.New[*ConflictMember](
options.store.WithRealm([]byte{database.PrefixBranchDAG, PrefixConflictMemberStorage}),
conflictMemberKeyPartition,
options.cacheTimeProvider.CacheTime(options.conflictCacheTime),
objectstorage.LeakDetectionEnabled(true, objectstorage.LeakDetectionOptions{
MaxConsumersPerObject: 10,
MaxConsumerHoldTime: 5 * time.Second,
}),
options.cacheTimeProvider.CacheTime(options.conflictMemberCacheTime),
objectstorage.LeakDetectionEnabled(false),
objectstorage.StoreOnCreation(true),
),
}
Expand Down
20 changes: 11 additions & 9 deletions packages/ledger/branchdag/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,6 @@ func NewConflictIDs(ids ...ConflictID) (new ConflictIDs) {
// InclusionState represents the confirmation status of branches in the BranchDAG.
type InclusionState uint8

const (
// Pending represents elements that have neither been confirmed nor rejected.
Pending InclusionState = iota
// Confirmed represents elements that have been confirmed and will stay part of the ledger state forever.
Confirmed
// Rejected represents elements that have been rejected and will not be included in the ledger state.
Rejected
)

// FromMarshalUtil un-serializes an InclusionState using a MarshalUtil.
func (i *InclusionState) FromMarshalUtil(marshalUtil *marshalutil.MarshalUtil) (err error) {
untypedInclusionState, err := marshalUtil.ReadUint8()
Expand Down Expand Up @@ -242,4 +233,15 @@ func (i InclusionState) Bytes() []byte {
return marshalutil.New(marshalutil.Uint8Size).WriteUint8(uint8(i)).Bytes()
}

const (
// Pending represents elements that have neither been confirmed nor rejected.
Pending InclusionState = iota

// Confirmed represents elements that have been confirmed and will stay part of the ledger state forever.
Confirmed

// Rejected represents elements that have been rejected and will not be included in the ledger state.
Rejected
)

// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////
1 change: 1 addition & 0 deletions packages/ledger/branchdag/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

// Utils is a BranchDAG component that bundles utility related API to simplify common interactions with the BranchDAG.
type Utils struct {
// branchDAG contains a reference to the BranchDAG instance that created these Utils.
branchDAG *BranchDAG
}

Expand Down
6 changes: 0 additions & 6 deletions packages/ledger/utxo/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ type Output interface {
ID() (id OutputID)

// SetID sets the identifier of the Output.
//
// Note: Since determining the identifier of an Output is an operation that requires at least a few hashing
// operations, we allow the ID to be set from the outside.
//
// This allows us to potentially skip the ID calculation in cases where the ID is known upfront (e.g. when loading
// Outputs from the object storage).
SetID(id OutputID)

// TransactionID returns the identifier of the Transaction that created this Output.
Expand Down
3 changes: 0 additions & 3 deletions packages/ledger/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ type VM interface {
// ExecuteTransaction executes the Transaction and determines the Outputs from the given Inputs. It returns an error
// if the execution fails.
ExecuteTransaction(transaction utxo.Transaction, inputs []utxo.Output, gasLimit ...uint64) (outputs []utxo.Output, err error)

// ParseTransaction un-serializes a Transaction from the given sequence of bytes.
ParseTransaction([]byte) (transaction utxo.Transaction, err error)

// ParseOutput un-serializes an Output from the given sequence of bytes.
ParseOutput([]byte) (output utxo.Output, err error)

// ResolveInput translates the Input into an OutputID.
ResolveInput(input utxo.Input) (outputID utxo.OutputID)
}

0 comments on commit ba6b873

Please sign in to comment.