branchmanager

package
v0.2.4-0...-2c2ceb1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2020 License: Apache-2.0, BSD-2-Clause Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const BranchIDLength = transaction.IDLength

BranchIDLength encodes the length of a branch identifier - since branches get created by transactions, it has the same length as a transaction ID.

View Source
const ConflictIDLength = transaction.OutputIDLength

ConflictIDLength encodes the length of a Conflict identifier - since Conflicts get created by transactions spending the same Output, it has the same length as an OutputID.

Variables

View Source
var (
	// UndefinedBranchID is the zero value of a BranchID and represents a branch that has not been set.
	UndefinedBranchID = BranchID{}

	// MasterBranchID is the identifier of the MasterBranch (root of the Branch DAG).
	MasterBranchID = BranchID{1}
)
View Source
var (
	// ParseConflictID is a wrapper for simplified unmarshaling of Ids from a byte stream using the marshalUtil package.
	ParseConflictID = transaction.ParseOutputID

	// ConflictIDFromBytes unmarshals a ConflictID from a sequence of bytes.
	ConflictIDFromBytes = transaction.OutputIDFromBytes
)

Functions

func BranchFromObjectStorage

func BranchFromObjectStorage(key []byte, data []byte) (result objectstorage.StorableObject, err error)

BranchFromObjectStorage is a factory method that creates a new Branch instance from the ObjectStorage.

func ChildBranchFromObjectStorage

func ChildBranchFromObjectStorage(key []byte, data []byte) (result objectstorage.StorableObject, err error)

ChildBranchFromObjectStorage is a factory method that creates a new ChildBranch instance from a storage key of the objectstorage. It is used by the objectstorage, to create new instances of this entity.

func ConflictFromObjectStorage

func ConflictFromObjectStorage(key []byte, data []byte) (result objectstorage.StorableObject, err error)

ConflictFromObjectStorage is a factory method that creates a new Conflict instance from the ObjectStorage.

func ConflictMemberFromObjectStorage

func ConflictMemberFromObjectStorage(key []byte, _ []byte) (result objectstorage.StorableObject, err error)

ConflictMemberFromObjectStorage is a factory method that creates a new ConflictMember instance from a storage key of the objectstorage. It is used by the objectstorage, to create new instances of this entity.

Types

type Branch

type Branch struct {
	objectstorage.StorableObjectFlags
	// contains filtered or unexported fields
}

Branch represents a part of the tangle, that shares the same perception of the ledger state. Every conflicting transaction forms a Branch, that contains all transactions that are spending Outputs of the conflicting transactions. Branches can also be created by merging two other Branches, which creates an aggregated Branch.

func BranchFromBytes

func BranchFromBytes(bytes []byte) (result *Branch, consumedBytes int, err error)

BranchFromBytes unmarshals a Branch from a sequence of bytes.

func NewBranch

func NewBranch(id BranchID, parentBranches []BranchID) *Branch

NewBranch is the constructor of a Branch and creates a new Branch object from the given details.

func ParseBranch

func ParseBranch(marshalUtil *marshalutil.MarshalUtil) (result *Branch, err error)

ParseBranch unmarshals a Branch using the given marshalUtil (for easier marshaling/unmarshaling).

func (*Branch) Bytes

func (branch *Branch) Bytes() []byte

Bytes returns a marshaled version of this Branch.

func (*Branch) Confirmed

func (branch *Branch) Confirmed() bool

Confirmed returns true if the branch has been accepted to be part of the ledger state.

func (*Branch) Conflicts

func (branch *Branch) Conflicts() (conflicts map[ConflictID]types.Empty)

Conflicts retrieves the Conflicts that a Branch is part of.

func (*Branch) Finalized

func (branch *Branch) Finalized() bool

Finalized returns true if the branch has been marked as finalized.

func (*Branch) ID

func (branch *Branch) ID() BranchID

ID returns the identifier of the Branch (usually the transaction.ID that created the branch - unless its an aggregated Branch).

func (*Branch) IsAggregated

func (branch *Branch) IsAggregated() bool

IsAggregated returns true if the branch is not a conflict-branch, but was created by merging multiple other branches.

func (*Branch) Liked

func (branch *Branch) Liked() bool

Liked returns if the branch is liked (it is preferred and all of its parents are liked).

func (*Branch) ObjectStorageKey

func (branch *Branch) ObjectStorageKey() []byte

ObjectStorageKey returns the bytes that are used as a key when storing the Branch in an objectstorage.

func (*Branch) ObjectStorageValue

func (branch *Branch) ObjectStorageValue() []byte

ObjectStorageValue returns the bytes that represent all remaining information (not stored in the key) of a marshaled Branch.

func (*Branch) ParentBranches

func (branch *Branch) ParentBranches() (parentBranches []BranchID)

ParentBranches returns the identifiers of the parents of this Branch.

func (*Branch) Preferred

func (branch *Branch) Preferred() bool

Preferred returns true, if the branch is the favored one among the branches in the same conflict sets.

func (*Branch) Rejected

func (branch *Branch) Rejected() bool

Rejected returns true if the branch has been rejected to be part of the ledger state.

func (*Branch) String

func (branch *Branch) String() string

String returns a human readable version of this Branch (for debug purposes).

func (*Branch) Update

func (branch *Branch) Update(other objectstorage.StorableObject)

Update is disabled but needs to be implemented to be compatible with the objectstorage.

type BranchID

type BranchID [BranchIDLength]byte

BranchID represents an identifier of a Branch.

func BranchIDFromBase58

func BranchIDFromBase58(base58String string) (branchID BranchID, err error)

BranchIDFromBase58 creates a new BranchID from a base58 encoded string.

func BranchIDFromBytes

func BranchIDFromBytes(bytes []byte) (result BranchID, consumedBytes int, err error)

BranchIDFromBytes unmarshals a BranchID from a sequence of bytes.

func NewBranchID

func NewBranchID(transactionID transaction.ID) (branchID BranchID)

NewBranchID creates a new BranchID from a transaction ID.

func ParseBranchID

func ParseBranchID(marshalUtil *marshalutil.MarshalUtil) (result BranchID, err error)

ParseBranchID unmarshals a BranchID using the given marshalUtil (for easier marshaling/unmarshaling).

func (BranchID) Bytes

func (branchId BranchID) Bytes() []byte

Bytes marshals the BranchID into a sequence of bytes.

func (BranchID) String

func (branchId BranchID) String() string

String creates a base58 encoded version of the BranchID.

type BranchIds

type BranchIds map[BranchID]types.Empty

BranchIds represents a collection of BranchIds.

func (BranchIds) ToList

func (branchIDs BranchIds) ToList() (result []BranchID)

ToList create a slice of BranchIDs from the collection.

type BranchManager

type BranchManager struct {

	// contains the Events of the BranchManager
	Events *Events
	// contains filtered or unexported fields
}

BranchManager is an entity that manages the branches of a UTXODAG. It offers methods to add, delete and modify Branches. It automatically keeps track of the "monotonicity" of liked and disliked by propagating these flags according to the structure of the Branch-DAG.

func New

func New(store kvstore.KVStore) (branchManager *BranchManager)

New is the constructor of the BranchManager.

func (*BranchManager) AggregateBranches

func (branchManager *BranchManager) AggregateBranches(branches ...BranchID) (cachedAggregatedBranch *CachedBranch, err error)

AggregateBranches takes a list of BranchIDs and tries to "aggregate" the given IDs into a new Branch. It is used to correctly "inherit" the referenced parent Branches into a new one.

func (*BranchManager) Branch

func (branchManager *BranchManager) Branch(branchID BranchID) *CachedBranch

Branch loads a Branch from the objectstorage.

func (*BranchManager) BranchesConflicting

func (branchManager *BranchManager) BranchesConflicting(branchIds ...BranchID) (branchesConflicting bool, err error)

BranchesConflicting returns true if the given Branches are part of the same Conflicts and can therefore not be merged.

func (*BranchManager) ChildBranches

func (branchManager *BranchManager) ChildBranches(branchID BranchID) CachedChildBranches

ChildBranches loads the references to the ChildBranches of the given Branch.

func (*BranchManager) Conflict

func (branchManager *BranchManager) Conflict(conflictID ConflictID) *CachedConflict

Conflict loads a Conflict from the objectstorage.

func (*BranchManager) ConflictMembers

func (branchManager *BranchManager) ConflictMembers(conflictID ConflictID) CachedConflictMembers

ConflictMembers loads the referenced members of a Conflict from the objectstorage.

func (*BranchManager) ElevateConflictBranch

func (branchManager *BranchManager) ElevateConflictBranch(branchToElevate BranchID, newParent BranchID) (isConflictBranch bool, modified bool, err error)

ElevateConflictBranch moves a branch to a new parent. This is necessary if a new conflict appears in the past cone of an already existing conflict.

func (*BranchManager) Fork

func (branchManager *BranchManager) Fork(branchID BranchID, parentBranches []BranchID, conflicts []ConflictID) (cachedBranch *CachedBranch, newBranchCreated bool)

Fork adds a new Branch to the branch-DAG and automatically creates the Conflicts and references if they don't exist. It can also be used to update an existing Branch and add it to additional conflicts.

func (*BranchManager) GenerateAggregatedBranchID

func (branchManager *BranchManager) GenerateAggregatedBranchID(branchIDs ...BranchID) BranchID

GenerateAggregatedBranchID generates an aggregated BranchID from the handed in BranchIDs.

func (*BranchManager) IsBranchConfirmed

func (branchManager *BranchManager) IsBranchConfirmed(id BranchID) (confirmed bool)

IsBranchConfirmed returns true if the Branch is marked as confirmed.

func (*BranchManager) IsBranchLiked

func (branchManager *BranchManager) IsBranchLiked(id BranchID) (liked bool)

IsBranchLiked returns true if the Branch is currently marked as liked.

func (*BranchManager) Prune

func (branchManager *BranchManager) Prune() (err error)

Prune resets the database and deletes all objects (for testing or "node resets").

func (*BranchManager) SetBranchFinalized

func (branchManager *BranchManager) SetBranchFinalized(branchID BranchID) (modified bool, err error)

SetBranchFinalized modifies the finalized flag of a branch. It automatically triggers

func (*BranchManager) SetBranchLiked

func (branchManager *BranchManager) SetBranchLiked(branchID BranchID, liked bool) (modified bool, err error)

SetBranchLiked is the method that allows us to modify the liked flag of a branch (it propagates to the parents).

func (*BranchManager) SetBranchPreferred

func (branchManager *BranchManager) SetBranchPreferred(branchID BranchID, preferred bool) (modified bool, err error)

SetBranchPreferred is the method that allows us to modify the preferred flag of a branch.

type CachedBranch

type CachedBranch struct {
	objectstorage.CachedObject
}

CachedBranch is a wrapper for the generic CachedObject returned by the objectstorage, that overrides the accessor methods, with a type-casted one.

func (*CachedBranch) Consume

func (cachedBranch *CachedBranch) Consume(consumer func(branch *Branch), forceRelease ...bool) (consumed bool)

Consume unwraps the CachedObject and passes a type-casted version to the consumer (if the object is not empty - it exists). It automatically releases the object when the consumer finishes.

func (*CachedBranch) Retain

func (cachedBranch *CachedBranch) Retain() *CachedBranch

Retain marks this CachedObject to still be in use by the program.

func (*CachedBranch) Unwrap

func (cachedBranch *CachedBranch) Unwrap() *Branch

Unwrap is the type-casted equivalent of Get. It returns nil if the object does not exist.

type CachedBranches

type CachedBranches map[BranchID]*CachedBranch

CachedBranches represents a collection of CachedBranches.

func (CachedBranches) Consume

func (cachedBranches CachedBranches) Consume(consumer func(branch *Branch)) (consumed bool)

Consume iterates over the CachedObjects, unwraps them and passes a type-casted version to the consumer (if the object is not empty - it exists). It automatically releases the object when the consumer finishes. It returns true, if at least one object was consumed.

func (CachedBranches) Release

func (cachedBranches CachedBranches) Release(force ...bool)

Release is a utility function that allows us to release all CachedObjects in the collection.

type CachedChildBranch

type CachedChildBranch struct {
	objectstorage.CachedObject
}

CachedChildBranch is a wrapper for the generic CachedObject returned by the objectstorage that overrides the accessor methods, with a type-casted one.

func (*CachedChildBranch) Consume

func (cachedChildBranch *CachedChildBranch) Consume(consumer func(childBranch *ChildBranch), forceRelease ...bool) (consumed bool)

Consume unwraps the CachedObject and passes a type-casted version to the consumer (if the object is not empty - it exists). It automatically releases the object when the consumer finishes.

func (*CachedChildBranch) Retain

func (cachedChildBranch *CachedChildBranch) Retain() *CachedChildBranch

Retain marks this CachedObject to still be in use by the program.

func (*CachedChildBranch) Unwrap

func (cachedChildBranch *CachedChildBranch) Unwrap() *ChildBranch

Unwrap is the type-casted equivalent of Get. It returns nil if the object does not exist.

type CachedChildBranches

type CachedChildBranches []*CachedChildBranch

CachedChildBranches represents a collection of CachedChildBranches.

func (CachedChildBranches) Consume

func (cachedChildBranches CachedChildBranches) Consume(consumer func(childBranch *ChildBranch)) (consumed bool)

Consume iterates over the CachedObjects, unwraps them and passes a type-casted version to the consumer (if the object is not empty - it exists). It automatically releases the object when the consumer finishes. It returns true, if at least one object was consumed.

func (CachedChildBranches) Release

func (cachedChildBranches CachedChildBranches) Release(force ...bool)

Release is a utility function that allows us to release all CachedObjects in the collection.

type CachedConflict

type CachedConflict struct {
	objectstorage.CachedObject
}

CachedConflict is a wrapper for the generic CachedObject returned by the objectstorage, that overrides the accessor methods, with a type-casted one.

func (*CachedConflict) Consume

func (cachedConflict *CachedConflict) Consume(consumer func(conflict *Conflict), forceRelease ...bool) (consumed bool)

Consume unwraps the CachedObject and passes a type-casted version to the consumer (if the object is not empty - it exists). It automatically releases the object when the consumer finishes.

func (*CachedConflict) Retain

func (cachedConflict *CachedConflict) Retain() *CachedConflict

Retain marks this CachedObject to still be in use by the program.

func (*CachedConflict) Unwrap

func (cachedConflict *CachedConflict) Unwrap() *Conflict

Unwrap is the type-casted equivalent of Get. It returns nil if the object does not exist.

type CachedConflictMember

type CachedConflictMember struct {
	objectstorage.CachedObject
}

CachedConflictMember is a wrapper for the generic CachedObject returned by the objectstorage that overrides the accessor methods, with a type-casted one.

func (*CachedConflictMember) Consume

func (cachedConflictMember *CachedConflictMember) Consume(consumer func(conflictMember *ConflictMember), forceRelease ...bool) (consumed bool)

Consume unwraps the CachedObject and passes a type-casted version to the consumer (if the object is not empty - it exists). It automatically releases the object when the consumer finishes.

func (*CachedConflictMember) Retain

func (cachedConflictMember *CachedConflictMember) Retain() *CachedConflictMember

Retain marks this CachedObject to still be in use by the program.

func (*CachedConflictMember) Unwrap

func (cachedConflictMember *CachedConflictMember) Unwrap() *ConflictMember

Unwrap is the type-casted equivalent of Get. It returns nil if the object does not exist.

type CachedConflictMembers

type CachedConflictMembers []*CachedConflictMember

CachedConflictMembers represents a collection of CachedConflictMembers.

func (CachedConflictMembers) Consume

func (cachedConflictMembers CachedConflictMembers) Consume(consumer func(conflictMember *ConflictMember)) (consumed bool)

Consume iterates over the CachedObjects, unwraps them and passes a type-casted version to the consumer (if the object is not empty - it exists). It automatically releases the object when the consumer finishes. It returns true, if at least one object was consumed.

func (CachedConflictMembers) Release

func (cachedConflictMembers CachedConflictMembers) Release(force ...bool)

Release is a utility function that allows us to release all CachedObjects in the collection.

type ChildBranch

type ChildBranch struct {
	objectstorage.StorableObjectFlags
	// contains filtered or unexported fields
}

ChildBranch represents the relationship between a Branch and its children. Since a Branch can have a potentially unbounded amount of child Branches, we store this as a separate k/v pair instead of a marshaled list of children inside the Branch.

func ChildBranchFromBytes

func ChildBranchFromBytes(bytes []byte) (result *ChildBranch, consumedBytes int, err error)

ChildBranchFromBytes unmarshals a ChildBranch from a sequence of bytes.

func NewChildBranch

func NewChildBranch(parentID BranchID, childID BranchID) *ChildBranch

NewChildBranch is the constructor of the ChildBranch reference.

func ParseChildBranch

func ParseChildBranch(marshalUtil *marshalutil.MarshalUtil) (result *ChildBranch, err error)

ParseChildBranch unmarshals a ChildBranch using the given marshalUtil (for easier marshaling/unmarshaling).

func (*ChildBranch) Bytes

func (childBranch *ChildBranch) Bytes() []byte

Bytes returns a marshaled version of this ChildBranch.

func (*ChildBranch) ChildID

func (childBranch *ChildBranch) ChildID() BranchID

ChildID returns the ID of the Branch that plays the role of the child in this relationship.

func (ChildBranch) ObjectStorageKey

func (childBranch ChildBranch) ObjectStorageKey() []byte

ObjectStorageKey returns the bytes that are used a key when storing the Branch in an objectstorage.

func (ChildBranch) ObjectStorageValue

func (childBranch ChildBranch) ObjectStorageValue() []byte

ObjectStorageValue returns the bytes that represent all remaining information (not stored in the key) of a marshaled ChildBranch.

func (*ChildBranch) ParentID

func (childBranch *ChildBranch) ParentID() BranchID

ParentID returns the ID of the Branch that plays the role of the parent in this relationship.

func (ChildBranch) Update

func (childBranch ChildBranch) Update(objectstorage.StorableObject)

Update is disabled but needs to be implemented to be compatible with the objectstorage.

type Conflict

type Conflict struct {
	objectstorage.StorableObjectFlags
	// contains filtered or unexported fields
}

Conflict represents a

func ConflictFromBytes

func ConflictFromBytes(bytes []byte) (result *Conflict, consumedBytes int, err error)

ConflictFromBytes unmarshals a Conflict from a sequence of bytes.

func NewConflict

func NewConflict(id ConflictID) *Conflict

NewConflict is the constructor for new Conflicts.

func ParseConflict

func ParseConflict(marshalUtil *marshalutil.MarshalUtil) (result *Conflict, err error)

ParseConflict unmarshals a Conflict using the given marshalUtil (for easier marshaling/unmarshaling).

func (*Conflict) Bytes

func (conflict *Conflict) Bytes() []byte

Bytes returns a marshaled version of this Conflict.

func (*Conflict) DecreaseMemberCount

func (conflict *Conflict) DecreaseMemberCount(optionalDelta ...int) (newMemberCount int)

DecreaseMemberCount offers a thread safe way to decrease the MemberCount property.

func (*Conflict) ID

func (conflict *Conflict) ID() ConflictID

ID returns the identifier of this Conflict.

func (*Conflict) IncreaseMemberCount

func (conflict *Conflict) IncreaseMemberCount(optionalDelta ...int) int

IncreaseMemberCount offers a thread safe way to increase the MemberCount property.

func (*Conflict) MemberCount

func (conflict *Conflict) MemberCount() int

MemberCount returns the amount of Branches that are part of this Conflict.

func (*Conflict) ObjectStorageKey

func (conflict *Conflict) ObjectStorageKey() []byte

ObjectStorageKey returns the bytes that are used a key when storing the Branch in an objectstorage.

func (*Conflict) ObjectStorageValue

func (conflict *Conflict) ObjectStorageValue() []byte

ObjectStorageValue returns the bytes that represent all remaining information (not stored in the key) of a marshaled Branch.

func (*Conflict) String

func (conflict *Conflict) String() string

String returns a human readable version of this Conflict (for debug purposes).

func (*Conflict) Update

func (conflict *Conflict) Update(other objectstorage.StorableObject)

Update is disabled but needs to be implemented to be compatible with the objectstorage.

type ConflictID

type ConflictID = transaction.OutputID

ConflictID represents an identifier of a Conflict. Since conflicts, are created by multiple transactions spending the same Output, the ConflictID is simply an alias for the conflicting OutputID.

type ConflictMember

type ConflictMember struct {
	objectstorage.StorableObjectFlags
	// contains filtered or unexported fields
}

ConflictMember represents the relationship between a Conflict and its Branches. Since a Conflict can have a potentially unbounded amount of conflicting Consumers, we store this as a separate k/v pair instead of a marshaled ist of members inside the Branch.

func ConflictMemberFromBytes

func ConflictMemberFromBytes(bytes []byte) (result *ConflictMember, consumedBytes int, err error)

ConflictMemberFromBytes unmarshals a ConflictMember from a sequence of bytes.

func NewConflictMember

func NewConflictMember(conflictID ConflictID, branchID BranchID) *ConflictMember

NewConflictMember is the constructor of the ConflictMember reference.

func ParseConflictMember

func ParseConflictMember(marshalUtil *marshalutil.MarshalUtil) (result *ConflictMember, err error)

ParseConflictMember unmarshals a ConflictMember using the given marshalUtil (for easier marshaling/unmarshaling).

func (*ConflictMember) BranchID

func (conflictMember *ConflictMember) BranchID() BranchID

BranchID returns the identifier of the Branch that this conflictMember references.

func (*ConflictMember) ConflictID

func (conflictMember *ConflictMember) ConflictID() ConflictID

ConflictID returns the identifier of the Conflict that this conflictMember belongs to.

func (ConflictMember) ObjectStorageKey

func (conflictMember ConflictMember) ObjectStorageKey() []byte

ObjectStorageKey returns the bytes that are used a key when storing the Branch in an objectstorage.

func (ConflictMember) ObjectStorageValue

func (conflictMember ConflictMember) ObjectStorageValue() []byte

ObjectStorageValue returns the bytes that represent all remaining information (not stored in the key) of a marshaled ConflictMember.

func (ConflictMember) Update

func (conflictMember ConflictMember) Update(other objectstorage.StorableObject)

Update is disabled but needs to be implemented to be compatible with the objectstorage.

type Events

type Events struct {
	// BranchPreferred gets triggered whenever a Branch becomes preferred that was not preferred before.
	BranchPreferred *events.Event

	// BranchUnpreferred gets triggered whenever a Branch becomes unpreferred that was preferred before.
	BranchUnpreferred *events.Event

	// BranchLiked gets triggered whenever a Branch becomes liked that was not liked before.
	BranchLiked *events.Event

	// BranchLiked gets triggered whenever a Branch becomes preferred that was not preferred before.
	BranchDisliked *events.Event

	// BranchFinalized gets triggered when a decision on a Branch is finalized and there will be no further state
	// changes regarding its preferred state.
	BranchFinalized *events.Event

	// BranchConfirmed gets triggered whenever a Branch becomes confirmed that was not confirmed before.
	BranchConfirmed *events.Event

	// BranchRejected gets triggered whenever a Branch becomes rejected that was not rejected before.
	BranchRejected *events.Event
}

Events is a container for the different kind of events of the BranchManager.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL