poll

package
v0.7.4 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Adapter

type Adapter[T any] interface {

	// GetFinalizedBlockNumber returns the finalized block number, which will no longer be reverted.
	GetFinalizedBlockNumber(ctx context.Context) (uint64, error)

	// GetLatestBlockNumber returns the latest block number, which may be reverted later.
	//
	// E.g. "safe", "latest" or "latest - N" block that not finalized yet.
	GetLatestBlockNumber(ctx context.Context) (uint64, error)

	// GetBlockData returns the whole blockchain data of the given block number. Typically, it includes
	// block, transactions, receipts and traces.
	//
	// The implementation should be responsible for chain reorg detection if the given block number is
	// not finalized yet.
	GetBlockData(ctx context.Context, blockNumber uint64) (T, error)

	// GetBlockHash returns the block hash of given blockchain data.
	GetBlockHash(data T) string
	// GetParentBlockHash returns the parent block hash of given blockchain data.
	GetParentBlockHash(data T) string
}

Adapter adapts any data source to fetch blockchain data. Typically, a PRC adapter is used to fetch blockchain data from fullnode.

Generic type T is used to support different kinds of blockchain data, e.g. Conflux core space and eSpace.

type CatchUpOption

type CatchUpOption struct {
	Parallel ParallelOption

	Buffer struct {
		Capacity int `default:"1024"`
		MaxBytes int `default:"256000000"` // 256M
	}
}

type CatchUpPoller

type CatchUpPoller[T channel.Sizable] struct {
	// contains filtered or unexported fields
}

CatchUpPoller is used to poll blockchain data concurrently in catch up phase.

func NewCatchUpPoller

func NewCatchUpPoller[T channel.Sizable](adapter Adapter[T], nextBlockNumber uint64, option ...CatchUpOption) *CatchUpPoller[T]

func (*CatchUpPoller[T]) DataCh

func (poller *CatchUpPoller[T]) DataCh() <-chan T

DataCh returns a read-only channel to consume data.

Note, the channel wil be closed once caught up to the latest finalized block.

func (*CatchUpPoller[T]) NextBlockNumber

func (poller *CatchUpPoller[T]) NextBlockNumber() uint64

NextBlockNumber returns the next block number to poll data.

func (*CatchUpPoller[T]) Poll

func (poller *CatchUpPoller[T]) Poll(ctx context.Context, wg *sync.WaitGroup)

Poll polls blockchain data in parallel until the latest finalized block number is polled.

type FinalizedPoller

type FinalizedPoller[T any] struct {
	// contains filtered or unexported fields
}

FinalizedPoller is used to poll the finalized blockchain data block by block.

func NewFinalizedPoller

func NewFinalizedPoller[T any](adapter Adapter[T], nextBlockNumber uint64, option ...Option) *FinalizedPoller[T]

func (*FinalizedPoller[T]) DataCh

func (poller *FinalizedPoller[T]) DataCh() <-chan T

DataCh returns a read-only channel to consume data. The channel will not be closed until poll goroutine terminated.

func (*FinalizedPoller[T]) Poll

func (poller *FinalizedPoller[T]) Poll(ctx context.Context, wg *sync.WaitGroup)

Poll polls the finalized blockchain data block by block.

type LatestPoller

type LatestPoller[T any] struct {
	// contains filtered or unexported fields
}

LatestPoller is used to poll the latest blockchain data block by block.

func NewLatestPoller

func NewLatestPoller[T any](adapter Adapter[T], nextBlockNumber uint64, reorgParams ReorgWindowParams, option ...Option) *LatestPoller[T]

func (*LatestPoller[T]) DataCh

func (poller *LatestPoller[T]) DataCh() <-chan Revertable[T]

DataCh returns a read-only channel to consume data. The channel will not be closed until poll goroutine terminated.

func (*LatestPoller[T]) Poll

func (poller *LatestPoller[T]) Poll(ctx context.Context, wg *sync.WaitGroup)

Poll polls the latest blockchain data block by block.

type Option

type Option struct {
	IdleInterval  time.Duration `default:"1s"`
	RetryInterval time.Duration `default:"5s"`
	BufferSize    int           `default:"32"`
	Health        health.TimedCounterConfig
}

type ParallelOption

type ParallelOption struct {
	parallel.SerialOption `mapstructure:",squash"`

	RetryInterval time.Duration `default:"1s"`

	Health health.TimedCounterConfig
}

type ParallelWorker

type ParallelWorker[T any] struct {
	// contains filtered or unexported fields
}

ParallelWorker is used to poll blockchain data in parallel.

func NewParallelWorker

func NewParallelWorker[T any](adapter Adapter[T], offset uint64, dataCh chan<- T, option ...ParallelOption) *ParallelWorker[T]

func (*ParallelWorker[T]) ParallelCollect

func (worker *ParallelWorker[T]) ParallelCollect(ctx context.Context, result *parallel.Result[T]) error

ParallelCollect implements the parallel.Interface[T] interface.

func (*ParallelWorker[T]) ParallelDo

func (worker *ParallelWorker[T]) ParallelDo(ctx context.Context, routine, task int) (data T, err error)

ParallelDo implements the parallel.Interface[T] interface.

func (*ParallelWorker[T]) Polled

func (worker *ParallelWorker[T]) Polled() uint64

Polled returns the number of actual polled data.

type ReorgWindow

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

ReorgWindow is used to detect chain reorg.

func NewReorgWindow

func NewReorgWindow() *ReorgWindow

func NewReorgWindowWithLatestBlocks

func NewReorgWindowWithLatestBlocks(params ReorgWindowParams) *ReorgWindow

NewReorgWindowWithLatestBlocks initializes with given latest blocks and the last finalized block number.

When service restarted, user should load recent blocks and the last finalized block from database, and initialize the reorg window so as to correctly handle chain reorg during the service down time.

func (*ReorgWindow) Evict

func (window *ReorgWindow) Evict(blockNumber uint64)

func (*ReorgWindow) Push

func (window *ReorgWindow) Push(blockNumber uint64, blockHash, parentBlockHash string) (appended, popped bool)

type ReorgWindowParams

type ReorgWindowParams struct {
	FinalizedBlockNumber uint64
	FinalizedBlockHash   string
	LatestBlocks         map[uint64]string
}

type Revertable

type Revertable[T any] struct {
	Data     T
	Reverted bool // indicates whether chain reorg happened
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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