ethmonitor

package
v1.24.12 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: LGPL-3.0, MIT Imports: 21 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFatal                 = errors.New("ethmonitor: fatal error, stopping")
	ErrReorg                 = errors.New("ethmonitor: block reorg")
	ErrUnexpectedParentHash  = errors.New("ethmonitor: unexpected parent hash")
	ErrUnexpectedBlockNumber = errors.New("ethmonitor: unexpected block number")
	ErrQueueFull             = errors.New("ethmonitor: publish queue is full")
	ErrMaxAttempts           = errors.New("ethmonitor: max attempts hit")
	ErrMonitorStopped        = errors.New("ethmonitor: stopped")
)
View Source
var DefaultOptions = Options{
	Logger:                           logger.NewLogger(logger.LogLevel_WARN),
	PollingInterval:                  1500 * time.Millisecond,
	ExpectedBlockInterval:            15 * time.Second,
	StreamingErrorResetInterval:      15 * time.Minute,
	StreamingRetryAfter:              20 * time.Minute,
	StreamingErrNumToSwitchToPolling: 3,
	UnsubscribeOnStop:                false,
	Timeout:                          20 * time.Second,
	StartBlockNumber:                 nil,
	TrailNumBlocksBehindHead:         0,
	BlockRetentionLimit:              200,
	WithLogs:                         false,
	LogTopics:                        []common.Hash{},
	DebugLogging:                     false,
	CacheExpiry:                      300 * time.Second,
	Alerter:                          util.NoopAlerter(),
}

Functions

func IsBlockEq added in v1.7.0

func IsBlockEq(a, b *types.Block) bool

Types

type Block

type Block struct {
	*types.Block

	// Event type where Block is Added or Removed (ie. reorged)
	Event Event

	// Logs in the block, grouped by transactions:
	// [[txnA logs, ..], [txnB logs, ..], ..]
	// Logs [][]types.Log `json:"logs"`
	Logs []types.Log

	// OK flag which represents the block is ready for broadcasting
	OK bool

	// Raw byte payloads for block and logs responses from the nodes.
	// The values are only set if RetainPayloads is set to true on monitor.
	BlockPayload []byte
	LogsPayload  []byte
}

func (*Block) MarshalJSON added in v1.17.9

func (b *Block) MarshalJSON() ([]byte, error)

func (*Block) UnmarshalJSON added in v1.17.9

func (b *Block) UnmarshalJSON(data []byte) error

type Blocks

type Blocks []*Block

func (Blocks) Copy added in v1.3.0

func (blocks Blocks) Copy() Blocks

func (Blocks) EventExists added in v1.3.0

func (blocks Blocks) EventExists(block *types.Block, event Event) bool

func (Blocks) FindBlock

func (blocks Blocks) FindBlock(blockHash common.Hash, optEvent ...Event) (*Block, bool)

func (Blocks) Head added in v1.7.0

func (b Blocks) Head() *Block

func (Blocks) IsOK added in v1.7.0

func (b Blocks) IsOK() bool

func (Blocks) LatestBlock

func (b Blocks) LatestBlock() *Block

func (Blocks) Reorg added in v1.7.0

func (b Blocks) Reorg() bool

func (Blocks) Tail added in v1.7.0

func (b Blocks) Tail() *Block

type Chain

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

func (*Chain) Blocks

func (c *Chain) Blocks() Blocks

func (*Chain) BootstrapFromBlocks added in v1.17.9

func (c *Chain) BootstrapFromBlocks(blocks []*Block) error

BootstrapFromBlocks will bootstrap the ethmonitor canonical chain from input blocks, while also verifying the chain hashes link together.

func (*Chain) BootstrapFromBlocksJSON added in v1.17.9

func (c *Chain) BootstrapFromBlocksJSON(data []byte) error

BootstrapFromBlocksJSON is convenience method which accepts json and bootstraps the ethmonitor chain. This method is here mostly for debugging purposes and recommend that you use BootstrapFromBlocks and handle constructing block events outside of ethmonitor.

func (*Chain) GetAverageBlockTime added in v1.14.2

func (c *Chain) GetAverageBlockTime() float64

func (*Chain) GetBlock

func (c *Chain) GetBlock(hash common.Hash) *Block

func (*Chain) GetBlockByNumber added in v1.3.2

func (c *Chain) GetBlockByNumber(blockNum uint64, event Event) *Block

func (*Chain) GetTransaction

func (c *Chain) GetTransaction(txnHash common.Hash) (*types.Transaction, Event)

GetTransaction searches our canonical chain of blocks (where each block points at previous), and returns the transaction. Aka, searches our chain for mined transactions. Keep in mind transactions can still be reorged, but you can check the blockNumber and compare it against the head to determine if its final.

func (*Chain) Head

func (c *Chain) Head() *Block

func (*Chain) PrintAllBlocks

func (c *Chain) PrintAllBlocks()

func (*Chain) ReadyHead added in v1.22.0

func (c *Chain) ReadyHead() *Block

func (*Chain) Snapshot added in v1.17.9

func (c *Chain) Snapshot() ([]byte, error)

func (*Chain) Tail added in v1.7.0

func (c *Chain) Tail() *Block

type Event added in v1.3.0

type Event uint32
const (
	Added Event = iota
	Removed
)

type Monitor

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

func NewMonitor

func NewMonitor(provider ethrpc.RawInterface, options ...Options) (*Monitor, error)

func (*Monitor) Chain

func (m *Monitor) Chain() *Chain

func (*Monitor) GetAverageBlockTime added in v1.14.2

func (m *Monitor) GetAverageBlockTime() float64

GetAverageBlockTime returns the average block time in seconds (including fractions)

func (*Monitor) GetBlock

func (m *Monitor) GetBlock(blockHash common.Hash) *Block

GetBlock will search the retained blocks for the hash

func (*Monitor) GetTransaction

func (m *Monitor) GetTransaction(txnHash common.Hash) (*types.Transaction, Event)

GetBlock will search within the retained canonical chain for the txn hash. Passing `optMined true` will only return transaction which have not been removed from the chain via a reorg.

func (*Monitor) IsRunning

func (m *Monitor) IsRunning() bool

func (*Monitor) LatestBlock

func (m *Monitor) LatestBlock() *Block

LatestBlock will return the head block of the canonical chain

func (*Monitor) LatestBlockNum added in v1.17.0

func (m *Monitor) LatestBlockNum() *big.Int

LatestBlockNum returns the latest block number in the canonical chain

func (*Monitor) LatestFinalBlock added in v1.17.2

func (m *Monitor) LatestFinalBlock(numBlocksToFinality int) *Block

LatestFinalBlock returns the latest block which has reached finality. The argument `numBlocksToFinality` should be a constant value of the number of blocks a particular chain needs to reach finality. Ie. on Polygon this value would be 120 and on Ethereum it would be 20. As the pubsub system publishes new blocks, this value will change, as the chain will progress forward. It's recommend / safe to call this method each time in a <-sub.Blocks() code block.

func (*Monitor) LatestReadyBlock added in v1.22.0

func (m *Monitor) LatestReadyBlock() *Block

LatestReadyBlock returns the latest block in the canonical chain which has block.OK state to true, as in all details are available for the block.

func (*Monitor) LatestReadyBlockNum added in v1.22.0

func (m *Monitor) LatestReadyBlockNum() *big.Int

LatestReadyBlockNum returns the latest block number in the canonical chain which has block.OK state to true, as in all details are available for the block.

func (*Monitor) NumSubscribers added in v1.19.4

func (m *Monitor) NumSubscribers() int

func (*Monitor) OldestBlockNum added in v1.17.0

func (m *Monitor) OldestBlockNum() *big.Int

func (*Monitor) Options

func (m *Monitor) Options() Options

func (*Monitor) Provider added in v1.1.10

func (m *Monitor) Provider() ethrpc.Interface

func (*Monitor) PurgeHistory added in v1.17.0

func (m *Monitor) PurgeHistory()

PurgeHistory clears all but the head of the chain. Useful for tests, but should almost never be used in a normal application.

func (*Monitor) Run added in v1.6.1

func (m *Monitor) Run(ctx context.Context) error

func (*Monitor) Stop

func (m *Monitor) Stop()

func (*Monitor) Subscribe

func (m *Monitor) Subscribe(optLabel ...string) Subscription

func (*Monitor) UnsubscribeAll added in v1.21.6

func (m *Monitor) UnsubscribeAll(err error)

type Options

type Options struct {
	// Logger used by ethmonitor to log warnings and debug info
	Logger logger.Logger

	// PollingInterval to query the chain for new blocks
	PollingInterval time.Duration

	// ExpectedBlockInterval is the expected time between blocks
	ExpectedBlockInterval time.Duration

	// StreamingErrorResetInterval is the time to reset the streaming error count
	StreamingErrorResetInterval time.Duration

	// StreamingRetryAfter is the time to wait before retrying the streaming again
	StreamingRetryAfter time.Duration

	// StreamingErrNumToSwitchToPolling is the number of errors before switching to polling
	StreamingErrNumToSwitchToPolling int

	// Auto-unsubscribe on monitor stop or error
	UnsubscribeOnStop bool

	// Timeout duration used by the rpc client when fetching data from the remote node.
	Timeout time.Duration

	// StartBlockNumber to begin the monitor from.
	StartBlockNumber *big.Int

	// Bootstrap flag which indicates the monitor will expect the monitor's
	// events to be bootstrapped, and will continue from that point. This also
	// takes precedence over StartBlockNumber when set to true.
	Bootstrap bool

	// TrailNumBlocksBehindHead is the number of blocks we trail behind
	// the head of the chain before broadcasting new events to the subscribers.
	TrailNumBlocksBehindHead int

	// BlockRetentionLimit is the number of blocks we keep on the canonical chain
	// cache.
	BlockRetentionLimit int

	// Retain block and logs payloads
	RetainPayloads bool

	// WithLogs will include logs with the blocks if specified true.
	WithLogs bool

	// LogTopics will filter only specific log topics to include.
	LogTopics []common.Hash

	// CacheBackend to use for caching block data
	// NOTE: do not use this unless you know what you're doing.
	// In most cases leave this nil.
	CacheBackend cachestore.Backend

	// CacheExpiry is how long to keep each record in cache
	CacheExpiry time.Duration

	// Alerter config via github.com/goware/alerter
	Alerter util.Alerter

	// DebugLogging toggle
	DebugLogging bool
}

type Subscription

type Subscription interface {
	Blocks() <-chan Blocks
	Done() <-chan struct{}
	Err() error
	Unsubscribe()
}

Jump to

Keyboard shortcuts

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