forkable

package
v0.0.2-0...-d05d5d5 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Block

type Block struct {
	BlockID         string
	BlockNum        uint64
	PreviousBlockID string
	Object          interface{}
}

func (*Block) AsRef

func (b *Block) AsRef() bstream.BlockRef

type ForkDB

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

ForkDB holds the graph of block headBlockID to previous block.

func NewForkDB

func NewForkDB(opts ...ForkDBOption) *ForkDB
func (f *ForkDB) AddLink(blockRef bstream.BlockRef, previousRefID string, obj interface{}) (exists bool, seenPrevious bool)

func (*ForkDB) BlockForID

func (f *ForkDB) BlockForID(blockID string) *Block

func (*ForkDB) BlockInCurrentChain

func (f *ForkDB) BlockInCurrentChain(startAtBlock bstream.BlockRef, blockNum uint64) bstream.BlockRef

BlockInCurrentChain finds the block_id at height `blockNum` under the requested `startAtBlockID` base block. Passing the head block id as `startAtBlockID` will tell you if the block num is part of the longest chain.

func (*ForkDB) ChainSwitchSegments

func (f *ForkDB) ChainSwitchSegments(oldHeadBlockID, newHeadsPreviousID string) (truncatedUndo []string, reversedRedo []string, reorgJunctionBlock string)

ChainSwitchSegments returns the list of block IDs that should be `undo`ne (in reverse chain order) and the list of blocks that should be `redo`ne (in chain order) for `blockID` (linking to `previousID`) to become the longest chain.

This assumes you are querying for something that *is* the longest chain (or the to-become longest chain).

func (f *ForkDB) ClonedLinks() (out map[string]string, nums map[string]uint64)

CloneLinks retrieves a snapshot of the links in the ForkDB. Used only in ForkViewerin `eosws`.

func (*ForkDB) CompleteSegment

func (f *ForkDB) CompleteSegment(startBlock bstream.BlockRef) (blocks []*Block, reachLIB bool)

CompleteSegment is like ReversibleSegment but keeps going passed lib and stops as soon no parent for a given block is present in ForkDB (there could be a hole however in which case this method returns up to the point where the hole is found).

No special handling is required for the genesis block as its parent will simply not be found in ForkDB as it cannot exist and it's just the "normal" case.

func (f *ForkDB) DeleteLink(id string)

func (*ForkDB) Deserialize

func (f *ForkDB) Deserialize(data []byte, objectFactory ObjectFactory) error

Deserialize the ForkDB from a series of bytes. The ForkDB holding objects of unknown type, the `objectFactory` is used here so you can specify which type of object is hold in the ForkDB `objects` map.

The `objectFactory` is a function that returns a new empty pointer instance of the object. It is used to deserialize the object from the serialized data be if it's a JSON object or a binary object.

If you are using protobuf objects, you can pass `nil` as the `objectFactory` as Protobuf should be able to deserialize the correct message, don't forget to pull the Golang dependencies of the Protobuf bindings your type(s) are correclty registered.

func (*ForkDB) Exists

func (f *ForkDB) Exists(blockID string) bool

func (*ForkDB) HasLIB

func (f *ForkDB) HasLIB() bool

func (*ForkDB) HasNewIrreversibleSegment

func (f *ForkDB) HasNewIrreversibleSegment(newLIB bstream.BlockRef) (hasNew bool, irreversibleSegment, staleBlocks []*Block)

HasNewIrreversibleSegment returns segments upon passing the newDposLIBID that are irreversible and stale. If there was no new segment, `hasNew` will be false. WARN: this method can only be called when `HasLIB()` is true. Otherwise, it panics.

func (*ForkDB) InitLIB

func (f *ForkDB) InitLIB(ref bstream.BlockRef)

func (*ForkDB) IsBehindLIB

func (f *ForkDB) IsBehindLIB(blockNum uint64) bool
func (f *ForkDB) IterateLinks(callback func(blockID, previousBlockID string, object interface{}) (getNext bool))

func (*ForkDB) LIBID

func (f *ForkDB) LIBID() string

Get the last irreversible block ID

func (*ForkDB) LIBNum

func (f *ForkDB) LIBNum() uint64

Get the last irreversible block num

func (*ForkDB) MoveLIB

func (f *ForkDB) MoveLIB(blockRef bstream.BlockRef)

func (*ForkDB) PurgeBeforeLIB

func (f *ForkDB) PurgeBeforeLIB(keptBlocks int) (purgedBlocks []*Block)

func (*ForkDB) ReversibleSegment

func (f *ForkDB) ReversibleSegment(startBlock bstream.BlockRef) (blocks []*Block, reachLIB bool)

ReversibleSegment returns the blocks between the previous irreversible Block ID and the given block ID. The LIB is excluded and the given block ID is included in the results.

Do not call this function if the `.HasLIB()` is false, as the result would make no sense.

WARN: if the segment is broken by some unlinkable blocks, the return value is `nil`.

func (*ForkDB) Serialize

func (f *ForkDB) Serialize() ([]byte, error)

func (*ForkDB) SetLIB

func (f *ForkDB) SetLIB(headRef bstream.BlockRef, libNum uint64)

Set a new lib without cleaning up blocks older then new lib (NO PURGE)

func (*ForkDB) SetLogger

func (f *ForkDB) SetLogger(logger *zap.Logger)

type ForkDBOption

type ForkDBOption func(db *ForkDB)

func ForkDBWithLogger

func ForkDBWithLogger(logger *zap.Logger) ForkDBOption

type Forkable

type Forkable struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func New

func New(h bstream.Handler, opts ...Option) *Forkable

func (*Forkable) AllBlocksAt

func (p *Forkable) AllBlocksAt(num uint64) (out []*pbbstream.Block)

func (*Forkable) AllIDs

func (p *Forkable) AllIDs() (out []string)

func (*Forkable) CallWithBlocksFromCursor

func (p *Forkable) CallWithBlocksFromCursor(cursor *bstream.Cursor, callback func([]*bstream.PreprocessedBlock)) error

func (*Forkable) CallWithBlocksFromNum

func (p *Forkable) CallWithBlocksFromNum(num uint64, callback func([]*bstream.PreprocessedBlock), withForks bool) (err error)

func (*Forkable) CallWithBlocksThroughCursor

func (p *Forkable) CallWithBlocksThroughCursor(startBlock uint64, cursor *bstream.Cursor, callback func([]*bstream.PreprocessedBlock)) error

func (*Forkable) CanonicalBlockAt

func (p *Forkable) CanonicalBlockAt(num uint64) *pbbstream.Block

func (*Forkable) GetBlockByHash

func (p *Forkable) GetBlockByHash(id string) (out *pbbstream.Block)

func (*Forkable) HeadInfo

func (p *Forkable) HeadInfo() (headNum uint64, headID string, headTime time.Time, libNum uint64, err error)

func (*Forkable) HeadNum

func (p *Forkable) HeadNum() uint64

func (*Forkable) Linkable

func (p *Forkable) Linkable(blk *pbbstream.Block) bool

func (*Forkable) LowestBlockNum

func (p *Forkable) LowestBlockNum() uint64

func (*Forkable) ProcessBlock

func (p *Forkable) ProcessBlock(blk *pbbstream.Block, obj interface{}) error

type ForkableBlock

type ForkableBlock struct {
	Block *pbbstream.Block
	Obj   interface{}
	// contains filtered or unexported fields
}

type ForkableObject

type ForkableObject struct {

	// The three following fields are filled when handling multi-block steps, like when passing Irreversibile segments, the whole segment is represented in here.
	StepCount  int                          // Total number of steps in multi-block steps.
	StepIndex  int                          // Index for the current block
	StepBlocks []*bstream.PreprocessedBlock // You can decide to process them when StepCount == StepIndex +1 or when StepIndex == 0 only.

	// Object that was returned by PreprocessBlock(). Could be nil
	Obj interface{}
	// contains filtered or unexported fields
}

func (*ForkableObject) Cursor

func (fobj *ForkableObject) Cursor() *bstream.Cursor

func (*ForkableObject) FinalBlockHeight

func (fobj *ForkableObject) FinalBlockHeight() uint64

func (*ForkableObject) ReorgJunctionBlock

func (fobj *ForkableObject) ReorgJunctionBlock() bstream.BlockRef

func (*ForkableObject) Step

func (fobj *ForkableObject) Step() bstream.StepType

func (*ForkableObject) WrappedObject

func (fobj *ForkableObject) WrappedObject() interface{}

type IrreversibleBlockIDGate

type IrreversibleBlockIDGate struct {
	MaxHoldOff int
	// contains filtered or unexported fields
}

This gate lets all blocks through once the target block ID has passed AS IRREVERSIBLE

func NewIrreversibleBlockIDGate

func NewIrreversibleBlockIDGate(blockID string, gateType bstream.GateType, h bstream.Handler, opts ...bstream.GateOption) *IrreversibleBlockIDGate

func (*IrreversibleBlockIDGate) ProcessBlock

func (g *IrreversibleBlockIDGate) ProcessBlock(blk *pbbstream.Block, obj interface{}) error

func (*IrreversibleBlockIDGate) SetLogger

func (g *IrreversibleBlockIDGate) SetLogger(logger *zap.Logger)

type IrreversibleBlockNumGate

type IrreversibleBlockNumGate struct {
	MaxHoldOff int
	// contains filtered or unexported fields
}

IrreversibleBlockNumGate This gate lets all blocks through once the target blocknum has passed AS IRREVERSIBLE

func NewIrreversibleBlockNumGate

func NewIrreversibleBlockNumGate(blockNum uint64, gateType bstream.GateType, h bstream.Handler, opts ...bstream.GateOption) *IrreversibleBlockNumGate

func (*IrreversibleBlockNumGate) ProcessBlock

func (g *IrreversibleBlockNumGate) ProcessBlock(blk *pbbstream.Block, obj interface{}) error

func (*IrreversibleBlockNumGate) SetLogger

func (g *IrreversibleBlockNumGate) SetLogger(logger *zap.Logger)

type ObjectBinaryMarshaler

type ObjectBinaryMarshaler interface {
	MarshalBinary() ([]byte, error)
	UnmarshalBinary([]byte) error
}

ObjectBinaryMarshaler is an interface that tells the ForkDB that the object can be serialized as binary via a call to `MarshalBinary` and deserialized the same way via `UnmarshalBinary` assuming you correctly provide the `objectFactory` when calling `ForkDB#Deserialize`.

type ObjectFactory

type ObjectFactory func() any

ObjectFactory is an interface that tells the ForkDB how to create a new object for deserialization. It is used when deserializing the ForkDB's object so that the correct type is instantiated.

type ObjectJSONMarshallable

type ObjectJSONMarshallable interface {
	JSONMarshallable()
}

ObjectJSONMarshallable is a marker interface that tells the ForkDB that the object can be serialized as JSON via a call to `json.Marshal` and deserialized the same way via `json.Unmarshal` assuming you correctly provide the `objectFactory` when calling `ForkDB#Deserialize`.

type Option

type Option func(f *Forkable)

func EnsureAllBlocksTriggerLongestChain

func EnsureAllBlocksTriggerLongestChain() Option

EnsureAllBlocksTriggerLongestChain will force every block to be considered as the longest chain, therefore making it appear as New at least once. The only edge case is if there is a hole between a block and LIB when it is received, and it is forked out: in this case, that block would never appear. It is extremely unlikely to happen, because incoming blocks should be linkable, and blocks that are not forked out will eventually be processed anyway.

func EnsureBlockFlows

func EnsureBlockFlows(blockRef bstream.BlockRef) Option

func HoldBlocksUntilLIB

func HoldBlocksUntilLIB() Option

func WithExclusiveLIB

func WithExclusiveLIB(irreversibleBlock bstream.BlockRef) Option

func WithFailOnUnlinkableBlocks

func WithFailOnUnlinkableBlocks(count int, gracePeriod time.Duration) Option

func WithFilters

func WithFilters(steps bstream.StepType) Option

WithFilters choses the steps we want to pass through the sub handler. It defaults to StepsAll upon creation.

func WithInclusiveLIB

func WithInclusiveLIB(irreversibleBlock bstream.BlockRef) Option

func WithKeptFinalBlocks

func WithKeptFinalBlocks(count int) Option

func WithLogger

func WithLogger(logger *zap.Logger) Option

func WithWarnOnUnlinkableBlocks

func WithWarnOnUnlinkableBlocks(count int) Option

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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