models

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2023 License: Apache-2.0, BSD-2-Clause Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// BlockVersion defines the Version of the block structure.
	BlockVersion uint8 = 1

	// MaxBlockSize defines the maximum size of a block in bytes.
	MaxBlockSize = 64 * 1024

	// MaxBlockWork defines the maximum work of a block.
	MaxBlockWork = 1

	// BlockIDLength defines the length of an BlockID.
	BlockIDLength = types.IdentifierLength + 8

	// MinParentsCount defines the minimum number of parents each parents block must have.
	MinParentsCount = 1

	// MaxParentsCount defines the maximum number of parents each parents block must have.
	MaxParentsCount = 8

	// MinParentsBlocksCount defines the minimum number of parents each parents block must have.
	MinParentsBlocksCount = 1

	// MaxParentsBlocksCount defines the maximum number of parents each parents block must have.
	MaxParentsBlocksCount = 4

	// MinStrongParentsCount defines the minimum number of strong parents a block must have.
	MinStrongParentsCount = 1
)
View Source
const BlockIDContextKey = "blockID"
View Source
const (
	// LastValidBlockType counts StrongParents, WeakParents, ShallowLikeParents.
	LastValidBlockType = ShallowLikeParentType
)

Variables

View Source
var (
	// ErrNoStrongParents is triggered if there no strong parents.
	ErrNoStrongParents = errors.New("missing strong blocks in first parent block")

	// ErrBlockTypeIsUnknown is triggered when the block type is unknown.
	ErrBlockTypeIsUnknown = errors.Errorf("block types must range from %d-%d", 1, LastValidBlockType)

	// ErrConflictingReferenceAcrossBlocks is triggered if there conflicting references across blocks.
	ErrConflictingReferenceAcrossBlocks = errors.New("different blocks have conflicting references")
)

Functions

func BlockIDToContext

func BlockIDToContext(ctx context.Context, blockID BlockID) context.Context

BlockIDToContext adds the BlockID to the given context.

func GenesisRootBlockProvider

func GenesisRootBlockProvider(index slot.Index) *advancedset.AdvancedSet[BlockID]

func IsEmptyBlockID

func IsEmptyBlockID(blockID BlockID) bool

func WithBlock

func WithBlock(alias string, block *Block) options.Option[TestFramework]

func WithCommitment

func WithCommitment(cm *commitment.Commitment) options.Option[Block]

func WithIssuer

func WithIssuer(issuer ed25519.PublicKey) options.Option[Block]

func WithIssuingTime

func WithIssuingTime(issuingTime time.Time) options.Option[Block]

func WithLatestConfirmedSlot

func WithLatestConfirmedSlot(index slot.Index) options.Option[Block]

func WithLikedInsteadParents

func WithLikedInsteadParents(parents BlockIDs) options.Option[Block]

func WithNonce

func WithNonce(nonce uint64) options.Option[Block]

func WithParents

func WithParents(parents ParentBlockIDs) options.Option[Block]

func WithPayload

func WithPayload(p payload.Payload) options.Option[Block]

func WithSequenceNumber

func WithSequenceNumber(sequenceNumber uint64) options.Option[Block]

func WithSignature

func WithSignature(signature ed25519.Signature) options.Option[Block]

func WithStrongParents

func WithStrongParents(parents BlockIDs) options.Option[Block]

func WithVersion

func WithVersion(version uint8) options.Option[Block]

func WithWeakParents

func WithWeakParents(parents BlockIDs) options.Option[Block]

Types

type Block

type Block struct {
	model.Storable[BlockID, Block, *Block, block] `serix:"0"`
	// contains filtered or unexported fields
}

Block represents the core block for the base layer Mesh.

func NewBlock

func NewBlock(opts ...options.Option[Block]) *Block

NewBlock creates a new block with the details provided by the issuer.

func NewEmptyBlock

func NewEmptyBlock(id BlockID, opts ...options.Option[Block]) (newBlock *Block)

func (*Block) Commitment

func (b *Block) Commitment() *commitment.Commitment

Commitment returns the Commitment of the block.

func (*Block) ContentHash

func (b *Block) ContentHash() (contentHash types.Identifier, err error)

func (*Block) DetermineID

func (b *Block) DetermineID(slotTimeProvider *slot.TimeProvider, blockIdentifier ...types.Identifier) (err error)

DetermineID calculates and sets the block's BlockID and size.

func (*Block) ForEachParent

func (b *Block) ForEachParent(consumer func(parent Parent))

ForEachParent executes a consumer func for each parent.

func (*Block) ForEachParentByType

func (b *Block) ForEachParentByType(parentType ParentsType, consumer func(parentBlockID BlockID) bool)

ForEachParentByType executes a consumer func for each strong parent.

func (*Block) IssuerID

func (b *Block) IssuerID() (issuerID identity.ID)

func (*Block) IssuerPublicKey

func (b *Block) IssuerPublicKey() ed25519.PublicKey

IssuerPublicKey returns the public key of the block issuer.

func (*Block) IssuingTime

func (b *Block) IssuingTime() time.Time

IssuingTime returns the time when this block was created.

func (*Block) LatestConfirmedSlot

func (b *Block) LatestConfirmedSlot() slot.Index

LatestConfirmedSlot returns the LatestConfirmedSlot of the block.

func (*Block) Nonce

func (b *Block) Nonce() uint64

Nonce returns the Nonce of the block.

func (*Block) Parents

func (b *Block) Parents() (parents []BlockID)

Parents returns a copy of the parents of the block.

func (*Block) ParentsByType

func (b *Block) ParentsByType(parentType ParentsType) BlockIDs

ParentsByType returns a slice of all parents of the desired type.

func (*Block) ParentsCountByType

func (b *Block) ParentsCountByType(parentType ParentsType) uint8

ParentsCountByType returns the total parents count of this block.

func (*Block) Payload

func (b *Block) Payload() payload.Payload

Payload returns the Payload of the block.

func (*Block) SequenceNumber

func (b *Block) SequenceNumber() uint64

SequenceNumber returns the sequence number of this block.

func (*Block) SetSignature

func (b *Block) SetSignature(signature ed25519.Signature)

func (*Block) Sign

func (b *Block) Sign(pair *ed25519.KeyPair) error

func (*Block) Signature

func (b *Block) Signature() ed25519.Signature

Signature returns the Signature of the block.

func (*Block) Size

func (b *Block) Size() int

Size returns the block size in bytes.

func (*Block) String

func (b *Block) String() string

func (*Block) StrongParents

func (b *Block) StrongParents() (strongParents []BlockID)

StrongParents returns a copy of the strong parents of the block.

func (*Block) VerifySignature

func (b *Block) VerifySignature() (valid bool, err error)

VerifySignature verifies the Signature of the block.

func (*Block) Version

func (b *Block) Version() uint8

Version returns the block Version.

func (*Block) Work

func (b *Block) Work() int

Work returns the work units required to process this block. Currently to 1 for all blocks, but could be improved.

type BlockID

type BlockID struct {
	Identifier types.Identifier `serix:"0"`
	SlotIndex  slot.Index       `serix:"1"`
}

BlockID identifies a block via its BLAKE2b-256 hash of its bytes.

var EmptyBlockID BlockID

EmptyBlockID is an empty id.

func BlockIDFromContext

func BlockIDFromContext(ctx context.Context) BlockID

BlockIDFromContext returns the BlockID from the given context.

func DetermineID

func DetermineID(blkBytes []byte, slotIndex slot.Index) BlockID

DetermineID calculates the block's BlockID.

func NewBlockID

func NewBlockID(contentHash types.Identifier, signature ed25519.Signature, slotIndex slot.Index) BlockID

NewBlockID returns a new BlockID for the given data.

func (BlockID) Alias

func (b BlockID) Alias() (alias string)

Alias returns the human-readable alias of the BlockID (or the base58 encoded bytes if no alias was set).

func (BlockID) Base58

func (b BlockID) Base58() (base58Encoded string)

Base58 returns a base58 encoded version of the BlockID.

func (BlockID) Bytes

func (b BlockID) Bytes() (serialized []byte, err error)

Bytes returns a serialized version of the BlockID.

func (BlockID) CompareTo

func (b BlockID) CompareTo(other BlockID) int

CompareTo does a lexicographical comparison to another blockID. Returns 0 if equal, -1 if smaller, or 1 if larger than other. Passing nil as other will result in a panic.

func (*BlockID) DecodeJSON

func (b *BlockID) DecodeJSON(val any) error

func (BlockID) EncodeJSON

func (b BlockID) EncodeJSON() (any, error)

func (*BlockID) FromBase58

func (b *BlockID) FromBase58(base58EncodedString string) (err error)

FromBase58 un-serializes a BlockID from a base58 encoded string.

func (*BlockID) FromBytes

func (b *BlockID) FromBytes(serialized []byte) (consumedBytes int, err error)

FromBytes deserializes a BlockID from a byte slice.

func (*BlockID) FromRandomness

func (b *BlockID) FromRandomness(optionalSlot ...slot.Index) (err error)

FromRandomness generates a random BlockID.

func (BlockID) Index

func (b BlockID) Index() slot.Index

func (BlockID) Length

func (b BlockID) Length() int

Length returns the byte length of a serialized BlockID.

func (BlockID) RegisterAlias

func (b BlockID) RegisterAlias(alias string)

RegisterAlias allows to register a human-readable alias for the BlockID which will be used as a replacement for the String method.

func (BlockID) String

func (b BlockID) String() (humanReadable string)

String returns a human-readable version of the BlockID.

func (BlockID) UnregisterAlias

func (b BlockID) UnregisterAlias()

UnregisterAlias allows to unregister a previously registered alias.

type BlockIDs

type BlockIDs map[BlockID]types.Empty

BlockIDs is a set of BlockIDs where every BlockID is stored only once.

func NewBlockIDs

func NewBlockIDs(blkIDs ...BlockID) BlockIDs

NewBlockIDs construct a new BlockID collection from the optional BlockIDs.

func (BlockIDs) Add

func (m BlockIDs) Add(blockID BlockID) BlockIDs

Add adds a BlockID to the collection and returns the collection to enable chaining.

func (BlockIDs) AddAll

func (m BlockIDs) AddAll(blockIDs BlockIDs) BlockIDs

AddAll adds all BlockIDs to the collection and returns the collection to enable chaining.

func (BlockIDs) Base58

func (m BlockIDs) Base58() (result []string)

Base58 returns a string slice of base58 BlockID.

func (BlockIDs) Bytes

func (m BlockIDs) Bytes() (serialized []byte, err error)

Bytes returns a serialized version of the BlockIDs.

func (BlockIDs) Clone

func (m BlockIDs) Clone() (clonedBlockIDs BlockIDs)

Clone creates a copy of the BlockIDs.

func (BlockIDs) Contains

func (m BlockIDs) Contains(target BlockID) (contains bool)

Contains checks if the given target BlockID is part of the BlockIDs.

func (BlockIDs) Empty

func (m BlockIDs) Empty() (empty bool)

Empty checks if BlockIDs is empty.

func (BlockIDs) First

func (m BlockIDs) First() BlockID

First returns the first element in BlockIDs (not ordered). This method only makes sense if there is exactly one element in the collection.

func (BlockIDs) FromBytes

func (m BlockIDs) FromBytes(serialized []byte) (consumedBytes int, err error)

FromBytes deserializes a BlockIDs from a byte slice.

func (BlockIDs) Remove

func (m BlockIDs) Remove(blockID BlockID) BlockIDs

Remove removes a BlockID from the collection and returns the collection to enable chaining.

func (BlockIDs) RemoveAll

func (m BlockIDs) RemoveAll(blockIDs BlockIDs) BlockIDs

RemoveAll removes the BlockIDs from the collection and returns the collection to enable chaining.

func (BlockIDs) Slice

func (m BlockIDs) Slice() []BlockID

Slice converts the set of BlockIDs into a slice of BlockIDs.

func (BlockIDs) String

func (m BlockIDs) String() string

String returns a human-readable Version of the BlockIDs.

func (BlockIDs) Subtract

func (m BlockIDs) Subtract(other BlockIDs) BlockIDs

Subtract removes all other from the collection and returns the collection to enable chaining.

type Parent

type Parent struct {
	ID   BlockID
	Type ParentsType
}

Parent is a parent that can be either strong or weak.

type ParentBlockIDs

type ParentBlockIDs map[ParentsType]BlockIDs

ParentBlockIDs is a map of ParentType to BlockIDs.

func NewParentBlockIDs

func NewParentBlockIDs() ParentBlockIDs

NewParentBlockIDs constructs a new ParentBlockIDs.

func (ParentBlockIDs) Add

func (p ParentBlockIDs) Add(parentType ParentsType, blockID BlockID) ParentBlockIDs

Add adds a parent to the map.

func (ParentBlockIDs) AddAll

func (p ParentBlockIDs) AddAll(parentType ParentsType, blockIDs BlockIDs) ParentBlockIDs

AddAll adds a collection of parents to the map.

func (ParentBlockIDs) AddStrong

func (p ParentBlockIDs) AddStrong(blockID BlockID) ParentBlockIDs

AddStrong adds a strong parent to the map.

func (ParentBlockIDs) CleanupReferences

func (p ParentBlockIDs) CleanupReferences()

func (ParentBlockIDs) Clone

func (p ParentBlockIDs) Clone() ParentBlockIDs

Clone returns a copy of map.

func (ParentBlockIDs) ForEach

func (p ParentBlockIDs) ForEach(callback func(parent Parent))

ForEach executes a consumer func for each parent.

func (ParentBlockIDs) IsEmpty

func (p ParentBlockIDs) IsEmpty() bool

IsEmpty returns true if the ParentBlockIDs are empty.

type ParentsType

type ParentsType uint8

ParentsType is a type that defines the type of the parent.

const (
	// UndefinedParentType is the undefined parent.
	UndefinedParentType ParentsType = iota
	// StrongParentType is the ParentsType for a strong parent.
	StrongParentType
	// WeakParentType is the ParentsType for a weak parent.
	WeakParentType
	// ShallowLikeParentType is the ParentsType for the shallow like parent.
	ShallowLikeParentType
)

func (ParentsType) String

func (bp ParentsType) String() string

String returns string representation of ParentsType.

type TestFramework

type TestFramework struct {
	// contains filtered or unexported fields
}

func NewTestFramework

func NewTestFramework(slotTimeProviderFunc func() *slot.TimeProvider, opts ...options.Option[TestFramework]) *TestFramework

NewTestFramework is the constructor of the TestFramework.

func (*TestFramework) Block

func (t *TestFramework) Block(alias string) (block *Block)

Block retrieves the Blocks that is associated with the given alias.

func (*TestFramework) BlockIDs

func (t *TestFramework) BlockIDs(aliases ...string) (blockIDs BlockIDs)

BlockIDs retrieves the Blocks that are associated with the given aliases.

func (*TestFramework) Blocks

func (t *TestFramework) Blocks(aliases ...string) (blocks []*Block)

Blocks retrieves the Blocks that are associated with the give aliases.

func (*TestFramework) CreateAndSignBlock

func (t *TestFramework) CreateAndSignBlock(alias string, keyPair *ed25519.KeyPair, opts ...options.Option[Block]) (block *Block)

CreateAndSignBlock creates a Block with the given alias and BlockTestFrameworkBlockOptions and signs it with the given keyPair.

func (*TestFramework) CreateBlock

func (t *TestFramework) CreateBlock(alias string, opts ...options.Option[Block]) (block *Block)

CreateBlock creates a Block with the given alias and BlockTestFrameworkBlockOptions.

func (*TestFramework) SetBlock

func (t *TestFramework) SetBlock(alias string, block *Block)

SetBlock set a Block with the given alias.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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