database

package
v0.1.124 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: AGPL-3.0 Imports: 24 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	// DefaultClickHouseFlushInterval is how often each batcher flushes when its
	// batch is not yet full. It was 1s, which with seven sensors meant ~5 inserts a
	// second per table of 1 to a few hundred rows each; every insert is a new part,
	// and merging those parts back together was most of the ClickHouse VM's CPU.
	// Rows reach ClickHouse up to this much later; HasBlock also checks
	// pendingBlocks, so the sensor's own backfill check is unaffected.
	DefaultClickHouseFlushInterval = 5 * time.Second
)

Default batching parameters. Write* calls only enqueue rows; a background goroutine flushes them in batches, since ClickHouse prefers large, infrequent inserts. Buffers are fixed-size and drop-on-full so a slow database can never stall the sensor or exhaust memory.

Variables

This section is empty.

Functions

func ClickHouseNodeList added in v0.1.124

func ClickHouseNodeList(ctx context.Context, dsn string, limit int) ([]string, error)

ClickHouseNodeList returns the URLs of the most recently seen peers in the sensor database at dsn. Unlike NewClickHouse, which discards everything when it cannot connect so the sensor keeps running, it fails, so an unreachable database is not mistaken for an empty node list.

func Hashes added in v0.1.119

func Hashes(anns []BlockAnnouncement) []common.Hash

Hashes drops the heights, for backends that only store hashes.

Types

type BlockAnnouncement added in v0.1.119

type BlockAnnouncement struct {
	Hash   common.Hash
	Number uint64
}

BlockAnnouncement is an announced block hash with its height, as carried by eth NewBlockHashes. ClickHouse keys observations by block number and needs the height; hash-keyed backends ignore it.

p2p.NewBlockHashesPacket is a slice of this type, so a decoded packet reaches WriteBlockEvents with no copy or conversion.

type ClickHouse added in v0.1.118

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

ClickHouse implements the Database interface backed by a ClickHouse cluster. The table definitions live in clickhouse_schema.sql, in the sensor-network-tools and polygon-infrastructure repos rather than here.

The schema separates content-addressed facts from observations and this writer matches it: every row written to a fact table (blocks, block_bodies, block_txs, transactions) is complete and a pure function of its hash, so two sensors emit byte-identical rows and no write can partially overwrite another. Everything observational goes to the event streams.

func (*ClickHouse) Close added in v0.1.118

func (c *ClickHouse) Close() error

Close stops the batcher goroutines, waits for their final drain flush to complete, and closes the connection. It is safe to call on a no-op instance (connection never established).

func (*ClickHouse) HasBlock added in v0.1.118

func (c *ClickHouse) HasBlock(ctx context.Context, hash common.Hash) bool

func (*ClickHouse) MaxConcurrentWrites added in v0.1.118

func (c *ClickHouse) MaxConcurrentWrites() int

func (*ClickHouse) NodeList added in v0.1.118

func (c *ClickHouse) NodeList(ctx context.Context, limit int) ([]string, error)

NodeList returns the most recently seen peers' enode URLs, from the narrow peers_current rollup rather than grouping over the event firehose.

func (*ClickHouse) ShouldWriteBlockEvents added in v0.1.118

func (c *ClickHouse) ShouldWriteBlockEvents() bool

func (*ClickHouse) ShouldWriteBlocks added in v0.1.118

func (c *ClickHouse) ShouldWriteBlocks() bool

func (*ClickHouse) ShouldWriteFirstBlockEvent added in v0.1.118

func (c *ClickHouse) ShouldWriteFirstBlockEvent() bool

func (*ClickHouse) ShouldWriteFirstTransactionEvent added in v0.1.118

func (c *ClickHouse) ShouldWriteFirstTransactionEvent() bool

func (*ClickHouse) ShouldWritePeers added in v0.1.118

func (c *ClickHouse) ShouldWritePeers() bool

func (*ClickHouse) ShouldWriteTransactionEvents added in v0.1.118

func (c *ClickHouse) ShouldWriteTransactionEvents() bool

func (*ClickHouse) ShouldWriteTransactions added in v0.1.118

func (c *ClickHouse) ShouldWriteTransactions() bool

func (*ClickHouse) WriteBlock added in v0.1.118

func (c *ClickHouse) WriteBlock(ctx context.Context, peer *enode.Node, block *types.Block, txs *bortx.Body, td *big.Int, tfs time.Time)

func (*ClickHouse) WriteBlockBody added in v0.1.118

func (c *ClickHouse) WriteBlockBody(ctx context.Context, body *eth.BlockBody, txs *bortx.Body, ann BlockAnnouncement, tfs time.Time)

func (*ClickHouse) WriteBlockBodyArrival added in v0.1.124

func (c *ClickHouse) WriteBlockBodyArrival(ctx context.Context, ann BlockAnnouncement, tfs time.Time)

WriteBlockBodyArrival records the body event. Like the header sources it carries no peer -- the sensor requested the body -- so it is excluded from the propagation rollup, but it makes "which sensor got the body, and when" answerable. It is written on arrival, whether or not the body later checks out against its header, because the arrival is an observation either way.

func (*ClickHouse) WriteBlockEvents added in v0.1.118

func (c *ClickHouse) WriteBlockEvents(ctx context.Context, peer *enode.Node, anns []BlockAnnouncement, tfs time.Time)

func (*ClickHouse) WriteBlockHashFirstSeen added in v0.1.118

func (c *ClickHouse) WriteBlockHashFirstSeen(ctx context.Context, peer *enode.Node, hash common.Hash, tfsh time.Time)

WriteBlockHashFirstSeen is a no-op: earliest first-seen is derived from block_events by the block_events_first materialized view.

func (*ClickHouse) WriteBlockHeaders added in v0.1.118

func (c *ClickHouse) WriteBlockHeaders(ctx context.Context, headers []*types.Header, tfs time.Time, isParent bool)

func (*ClickHouse) WritePeers added in v0.1.118

func (c *ClickHouse) WritePeers(ctx context.Context, peers []*p2p.Peer, tls time.Time)

func (*ClickHouse) WriteTransactionEvents added in v0.1.118

func (c *ClickHouse) WriteTransactionEvents(ctx context.Context, peer *enode.Node, hashes []common.Hash, tfs time.Time)

func (*ClickHouse) WriteTransactions added in v0.1.118

func (c *ClickHouse) WriteTransactions(ctx context.Context, peer *enode.Node, txs []*types.Transaction, tfs time.Time)

type ClickHouseOptions added in v0.1.118

type ClickHouseOptions struct {
	DSN                              string
	SensorID                         string
	ChainID                          uint64
	MaxConcurrency                   int
	ShouldWriteBlocks                bool
	ShouldWriteBlockEvents           bool
	ShouldWriteFirstBlockEvent       bool
	ShouldWriteTransactions          bool
	ShouldWriteTransactionEvents     bool
	ShouldWriteFirstTransactionEvent bool
	ShouldWritePeers                 bool
	// FlushInterval is how often batchers flush a partial batch. Zero means
	// DefaultClickHouseFlushInterval.
	FlushInterval time.Duration
}

ClickHouseOptions is used when creating a NewClickHouse.

type Database

type Database interface {
	// WriteBlock records a block delivered whole (NewBlock). The block row is
	// written if ShouldWriteBlocks returns true; the event is written if either
	// ShouldWriteBlockEvents or ShouldWriteFirstBlockEvent returns true, because
	// a whole-block delivery is one event per block per sensor and is the only
	// place a block first seen this way is observed. Transaction facts come
	// from txs, the decoded body: the *types.Block cannot hold Bor state-sync
	// transactions.
	WriteBlock(context.Context, *enode.Node, *types.Block, *bortx.Body, *big.Int, time.Time)

	// WriteBlockHeaders will write the block headers if ShouldWriteBlocks
	// returns true.
	WriteBlockHeaders(context.Context, []*types.Header, time.Time, bool)

	// WriteBlockEvents appends an inbound block event (peer, hash, height, time)
	// for each announcement — one per peer we received the announcement from. The
	// caller decides which announcements to pass (every one for the full per-peer
	// stream, or just the first-seen ones); the backend only appends.
	WriteBlockEvents(context.Context, *enode.Node, []BlockAnnouncement, time.Time)

	// WriteBlockHashFirstSeen records the earliest event for a block hash.
	// Backends that derive first-seen from the event stream (e.g. ClickHouse)
	// treat it as a no-op.
	WriteBlockHashFirstSeen(context.Context, *enode.Node, common.Hash, time.Time)

	// WriteBlockBodyArrival records that a requested block body arrived, at the
	// time it arrived. The announcement carries the height, which the eth block
	// body does not, so backends that key observations by block number can
	// record it. It is called as soon as the body decodes, separately from
	// WriteBlockBody, which waits until the body has been checked.
	WriteBlockBodyArrival(context.Context, BlockAnnouncement, time.Time)

	// WriteBlockBody writes the facts of a block body: its transaction count and
	// ordered transactions (the block row itself comes from WriteBlock or
	// WriteBlockHeaders). It is only called with a body checked against its
	// header -- transactions, uncles and withdrawals -- so tfs, when the body
	// arrived, may be well before the call. ClickHouse writes the body facts if ShouldWriteBlocks and the
	// transactions if ShouldWriteTransactions.
	WriteBlockBody(context.Context, *eth.BlockBody, *bortx.Body, BlockAnnouncement, time.Time)

	// WriteTransactions writes the transaction bodies if ShouldWriteTransactions
	// returns true. Transaction events are recorded separately via
	// WriteTransactionEvents at announcement time.
	WriteTransactions(context.Context, *enode.Node, []*types.Transaction, time.Time)

	// WriteTransactionEvents appends an inbound transaction event (peer, hash,
	// time) for each hash — the tx mirror of WriteBlockEvents. The caller
	// decides which hashes to pass (every announcement for the full per-peer
	// stream, or just the first-seen ones); the backend only appends.
	WriteTransactionEvents(context.Context, *enode.Node, []common.Hash, time.Time)

	// WritePeers will write the connected peers to the database.
	WritePeers(context.Context, []*p2p.Peer, time.Time)

	// HasBlock will return whether the block is in the database. If the database
	// client has not been initialized this will always return true.
	HasBlock(context.Context, common.Hash) bool

	MaxConcurrentWrites() int
	ShouldWriteBlocks() bool
	ShouldWriteBlockEvents() bool
	ShouldWriteFirstBlockEvent() bool
	ShouldWriteTransactions() bool
	ShouldWriteTransactionEvents() bool
	ShouldWriteFirstTransactionEvent() bool
	ShouldWritePeers() bool

	// NodeList will return a list of enode URLs.
	NodeList(ctx context.Context, limit int) ([]string, error)

	// Close flushes any buffered writes and releases the underlying database
	// connection. It blocks until in-flight writes have drained (or their
	// shutdown flush times out). Implementations with no resources to release
	// return nil.
	Close() error
}

Database represents a database solution to write block and transaction data to. To use another database solution, just implement these methods and update the sensor to use the new connection.

func NewClickHouse added in v0.1.118

func NewClickHouse(ctx context.Context, opts ClickHouseOptions) Database

NewClickHouse connects to ClickHouse, verifies connectivity, and starts the background batch flushers. Callers should defer Close to drain buffered rows on shutdown. If the connection cannot be established the returned Database no-ops all writes so the sensor keeps running.

func NewJSONDatabase added in v0.1.88

func NewJSONDatabase(opts JSONDatabaseOptions) Database

NewJSONDatabase creates a new JSONDatabase instance.

func NoDatabase added in v0.1.88

func NoDatabase() Database

NoDatabase creates a new nodb instance.

type JSONBlock added in v0.1.88

type JSONBlock struct {
	Type            string    `json:"type"`
	SensorID        string    `json:"sensor_id"`
	Hash            string    `json:"hash"`
	ParentHash      string    `json:"parent_hash"`
	Number          uint64    `json:"number"`
	Timestamp       uint64    `json:"timestamp"`
	GasLimit        uint64    `json:"gas_limit"`
	GasUsed         uint64    `json:"gas_used"`
	Difficulty      string    `json:"difficulty,omitempty"`
	TotalDifficulty string    `json:"total_difficulty,omitempty"`
	BaseFee         string    `json:"base_fee,omitempty"`
	TxCount         int       `json:"tx_count"`
	UncleCount      int       `json:"uncle_count"`
	TimeFirstSeen   time.Time `json:"time_first_seen"`
	IsParent        bool      `json:"is_parent"`
}

JSONBlock represents a block in JSON format.

type JSONBlockEvent added in v0.1.88

type JSONBlockEvent struct {
	Type      string    `json:"type"`
	SensorID  string    `json:"sensor_id"`
	PeerID    string    `json:"peer_id"`
	Hash      string    `json:"hash"`
	Number    uint64    `json:"number,omitempty"`
	Timestamp time.Time `json:"timestamp"`
}

JSONBlockEvent represents a block event in JSON format.

type JSONBlockHashFirstSeen added in v0.1.97

type JSONBlockHashFirstSeen struct {
	Type          string    `json:"type"`
	SensorID      string    `json:"sensor_id"`
	Hash          string    `json:"hash"`
	TimeFirstSeen time.Time `json:"time_first_seen"`
}

JSONBlockHashFirstSeen represents a block hash announcement in JSON format.

type JSONDatabase added in v0.1.88

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

JSONDatabase outputs data as JSON to stdout. Each record is output as a single line of JSON (newline-delimited JSON).

func (*JSONDatabase) Close added in v0.1.118

func (j *JSONDatabase) Close() error

Close does nothing; records are written to stdout unbuffered.

func (*JSONDatabase) HasBlock added in v0.1.88

func (j *JSONDatabase) HasBlock(ctx context.Context, hash common.Hash) bool

HasBlock always returns true to avoid unnecessary parent block fetching for JSON output.

func (*JSONDatabase) MaxConcurrentWrites added in v0.1.88

func (j *JSONDatabase) MaxConcurrentWrites() int

MaxConcurrentWrites returns the max concurrency.

func (*JSONDatabase) NodeList added in v0.1.88

func (j *JSONDatabase) NodeList(ctx context.Context, limit int) ([]string, error)

NodeList returns an empty list as JSON database doesn't store nodes.

func (*JSONDatabase) ShouldWriteBlockEvents added in v0.1.88

func (j *JSONDatabase) ShouldWriteBlockEvents() bool

ShouldWriteBlockEvents returns the configured value.

func (*JSONDatabase) ShouldWriteBlocks added in v0.1.88

func (j *JSONDatabase) ShouldWriteBlocks() bool

ShouldWriteBlocks returns the configured value.

func (*JSONDatabase) ShouldWriteFirstBlockEvent added in v0.1.108

func (j *JSONDatabase) ShouldWriteFirstBlockEvent() bool

ShouldWriteFirstBlockEvent returns false for JSON database.

func (*JSONDatabase) ShouldWriteFirstTransactionEvent added in v0.1.112

func (j *JSONDatabase) ShouldWriteFirstTransactionEvent() bool

ShouldWriteFirstTransactionEvent returns false for JSON database.

func (*JSONDatabase) ShouldWritePeers added in v0.1.88

func (j *JSONDatabase) ShouldWritePeers() bool

ShouldWritePeers returns the configured value.

func (*JSONDatabase) ShouldWriteTransactionEvents added in v0.1.88

func (j *JSONDatabase) ShouldWriteTransactionEvents() bool

ShouldWriteTransactionEvents returns the configured value.

func (*JSONDatabase) ShouldWriteTransactions added in v0.1.88

func (j *JSONDatabase) ShouldWriteTransactions() bool

ShouldWriteTransactions returns the configured value.

func (*JSONDatabase) Write added in v0.1.88

func (j *JSONDatabase) Write(v any)

Write safely outputs JSON to stdout.

func (*JSONDatabase) WriteBlock added in v0.1.88

func (j *JSONDatabase) WriteBlock(_ context.Context, peer *enode.Node, block *types.Block, txs *bortx.Body, td *big.Int, tfs time.Time)

WriteBlock writes the block and the block event as JSON.

func (*JSONDatabase) WriteBlockBody added in v0.1.88

func (j *JSONDatabase) WriteBlockBody(ctx context.Context, body *eth.BlockBody, txs *bortx.Body, ann BlockAnnouncement, tfs time.Time)

WriteBlockBody writes the block body as JSON.

func (*JSONDatabase) WriteBlockBodyArrival added in v0.1.124

func (j *JSONDatabase) WriteBlockBodyArrival(ctx context.Context, ann BlockAnnouncement, tfs time.Time)

WriteBlockBodyArrival does nothing. The JSON backend records a body only once it has been checked against its header, in WriteBlockBody, which carries the time the body arrived; a body that fails the check or whose header never arrives is not recorded.

func (*JSONDatabase) WriteBlockEvents added in v0.1.118

func (j *JSONDatabase) WriteBlockEvents(ctx context.Context, peer *enode.Node, anns []BlockAnnouncement, tfs time.Time)

WriteBlockEvents writes the block events as JSON.

func (*JSONDatabase) WriteBlockHashFirstSeen added in v0.1.97

func (j *JSONDatabase) WriteBlockHashFirstSeen(ctx context.Context, peer *enode.Node, hash common.Hash, tfsh time.Time)

WriteBlockHashFirstSeen writes a partial block entry with just the hash first seen time. For JSON output, this writes a separate record type.

func (*JSONDatabase) WriteBlockHeaders added in v0.1.88

func (j *JSONDatabase) WriteBlockHeaders(ctx context.Context, headers []*types.Header, tfs time.Time, isParent bool)

WriteBlockHeaders writes the block headers as JSON. The isParent parameter indicates if these headers were fetched as parent blocks.

func (*JSONDatabase) WritePeers added in v0.1.88

func (j *JSONDatabase) WritePeers(ctx context.Context, peers []*p2p.Peer, tls time.Time)

WritePeers writes the connected peers as JSON.

func (*JSONDatabase) WriteTransactionEvents added in v0.1.118

func (j *JSONDatabase) WriteTransactionEvents(ctx context.Context, peer *enode.Node, hashes []common.Hash, tfs time.Time)

func (*JSONDatabase) WriteTransactions added in v0.1.88

func (j *JSONDatabase) WriteTransactions(_ context.Context, peer *enode.Node, txs []*types.Transaction, tfs time.Time)

WriteTransactions writes the transactions and transaction events as JSON.

type JSONDatabaseOptions added in v0.1.88

type JSONDatabaseOptions struct {
	SensorID                     string
	ChainID                      uint64
	MaxConcurrency               int
	ShouldWriteBlocks            bool
	ShouldWriteBlockEvents       bool
	ShouldWriteTransactions      bool
	ShouldWriteTransactionEvents bool
	ShouldWritePeers             bool
}

JSONDatabaseOptions is used when creating a NewJSONDatabase.

type JSONPeer added in v0.1.88

type JSONPeer struct {
	Type         string    `json:"type"`
	SensorID     string    `json:"sensor_id"`
	ID           string    `json:"id"`
	Name         string    `json:"name"`
	URL          string    `json:"url"`
	Caps         []string  `json:"caps"`
	TimeLastSeen time.Time `json:"time_last_seen"`
}

JSONPeer represents a peer in JSON format.

type JSONTransaction added in v0.1.88

type JSONTransaction struct {
	Type          string    `json:"type"`
	SensorID      string    `json:"sensor_id"`
	Hash          string    `json:"hash"`
	From          string    `json:"from,omitempty"`
	To            string    `json:"to,omitempty"`
	Value         string    `json:"value"`
	Gas           uint64    `json:"gas"`
	GasPrice      string    `json:"gas_price"`
	GasFeeCap     string    `json:"gas_fee_cap,omitempty"`
	GasTipCap     string    `json:"gas_tip_cap,omitempty"`
	Nonce         uint64    `json:"nonce"`
	TxType        uint8     `json:"tx_type"`
	TimeFirstSeen time.Time `json:"time_first_seen"`
}

JSONTransaction represents a transaction in JSON format.

type JSONTransactionEvent added in v0.1.88

type JSONTransactionEvent struct {
	Type      string    `json:"type"`
	SensorID  string    `json:"sensor_id"`
	PeerID    string    `json:"peer_id"`
	Hash      string    `json:"hash"`
	Timestamp time.Time `json:"timestamp"`
}

JSONTransactionEvent represents a transaction event in JSON format.

Jump to

Keyboard shortcuts

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