database

package
v0.1.63 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2024 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Kinds are the datastore equivalent of tables.
	BlocksKind            = "blocks"
	BlockEventsKind       = "block_events"
	TransactionsKind      = "transactions"
	TransactionEventsKind = "transaction_events"
	PeersKind             = "peers"
	MaxAttempts           = 3
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Database

type Database interface {
	// WriteBlock will write the both the block and block event to the database
	// if ShouldWriteBlocks and ShouldWriteBlockEvents return true, respectively.
	WriteBlock(context.Context, *enode.Node, *types.Block, *big.Int, time.Time)

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

	// WriteBlockHashes will write the block hashes if ShouldWriteBlockEvents
	// returns true.
	WriteBlockHashes(context.Context, *enode.Node, []common.Hash, time.Time)

	// WriteBlockBodies will write the block bodies if ShouldWriteBlocks returns
	// true.
	WriteBlockBody(context.Context, *eth.BlockBody, common.Hash, time.Time)

	// WriteTransactions will write the both the transaction and transaction
	// event to the database if ShouldWriteTransactions and
	// ShouldWriteTransactionEvents return true, respectively.
	WriteTransactions(context.Context, *enode.Node, []*types.Transaction, 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
	ShouldWriteTransactions() bool
	ShouldWriteTransactionEvents() bool
	ShouldWritePeers() bool

	// NodeList will return a list of enode URLs.
	NodeList(ctx context.Context, limit int) ([]string, 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 NewDatastore

func NewDatastore(ctx context.Context, opts DatastoreOptions) Database

NewDatastore connects to datastore and creates the client. This should only be called once unless trying to write to different databases.

type Datastore

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

Datastore wraps the datastore client, stores the sensorID, and other information needed when writing blocks and transactions.

func (*Datastore) HasBlock

func (d *Datastore) HasBlock(ctx context.Context, hash common.Hash) bool

func (*Datastore) MaxConcurrentWrites

func (d *Datastore) MaxConcurrentWrites() int

func (*Datastore) NodeList

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

func (*Datastore) ShouldWriteBlockEvents

func (d *Datastore) ShouldWriteBlockEvents() bool

func (*Datastore) ShouldWriteBlocks

func (d *Datastore) ShouldWriteBlocks() bool

func (*Datastore) ShouldWritePeers

func (d *Datastore) ShouldWritePeers() bool

func (*Datastore) ShouldWriteTransactionEvents

func (d *Datastore) ShouldWriteTransactionEvents() bool

func (*Datastore) ShouldWriteTransactions

func (d *Datastore) ShouldWriteTransactions() bool

func (*Datastore) WriteBlock

func (d *Datastore) WriteBlock(ctx context.Context, peer *enode.Node, block *types.Block, td *big.Int, tfs time.Time)

WriteBlock writes the block and the block event to datastore.

func (*Datastore) WriteBlockBody

func (d *Datastore) WriteBlockBody(ctx context.Context, body *eth.BlockBody, hash common.Hash, tfs time.Time)

WriteBlockHeaders will write the block bodies to datastore. It will not write block events because bodies will only be sent to the sensor when requested. The block events will be written when the hash is received instead. It will write the uncles and transactions to datastore if they don't already exist.

func (*Datastore) WriteBlockHashes

func (d *Datastore) WriteBlockHashes(ctx context.Context, peer *enode.Node, hashes []common.Hash, tfs time.Time)

WriteBlockHashes will write the block events to datastore.

func (*Datastore) WriteBlockHeaders

func (d *Datastore) WriteBlockHeaders(ctx context.Context, headers []*types.Header, tfs time.Time)

WriteBlockHeaders will write the block headers to datastore. It will not write block events because headers will only be sent to the sensor when requested. The block events will be written when the hash is received instead.

func (*Datastore) WritePeers

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

WritePeers writes the connected peers to datastore.

func (*Datastore) WriteTransactions

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

WriteTransactions will write the transactions and transaction events to datastore.

type DatastoreBlock

type DatastoreBlock struct {
	*DatastoreHeader
	TotalDifficulty string
	Transactions    []*datastore.Key
	Uncles          []*datastore.Key
}

DatastoreBlock represents a block stored in datastore.

type DatastoreEvent

type DatastoreEvent struct {
	SensorId string
	PeerId   string
	Hash     *datastore.Key
	Time     time.Time
	TTL      time.Time
}

DatastoreEvent can represent a peer sending the sensor a transaction hash or a block hash. In this implementation, the block and transactions are written to different tables by specifying a kind during key creation see writeEvents for more.

type DatastoreHeader

type DatastoreHeader struct {
	ParentHash    *datastore.Key
	UncleHash     string
	Coinbase      string
	Root          string
	TxHash        string
	ReceiptHash   string
	Bloom         []byte `datastore:",noindex"`
	Difficulty    string
	Number        string
	GasLimit      string
	GasUsed       string
	Time          time.Time
	Extra         []byte `datastore:",noindex"`
	MixDigest     string
	Nonce         string
	BaseFee       string
	TimeFirstSeen time.Time
	TTL           time.Time
}

DatastoreHeader stores the data in manner that can be easily written without loss of precision.

type DatastoreOptions

type DatastoreOptions struct {
	ProjectID                    string
	DatabaseID                   string
	SensorID                     string
	MaxConcurrency               int
	ShouldWriteBlocks            bool
	ShouldWriteBlockEvents       bool
	ShouldWriteTransactions      bool
	ShouldWriteTransactionEvents bool
	ShouldWritePeers             bool
	TTL                          time.Duration
}

DatastoreOptions is used when creating a NewDatastore.

type DatastorePeer

type DatastorePeer struct {
	Name         string
	Caps         []string
	URL          string
	LastSeenBy   string
	TimeLastSeen time.Time
	TTL          time.Time
}

type DatastoreTransaction

type DatastoreTransaction struct {
	Data          []byte `datastore:",noindex"`
	From          string
	Gas           string
	GasFeeCap     string
	GasPrice      string
	GasTipCap     string
	Nonce         string
	To            string
	Value         string
	V, R, S       string
	Time          time.Time
	TimeFirstSeen time.Time
	TTL           time.Time
	Type          int16
}

DatastoreTransaction represents a transaction stored in datastore. Data is not indexed because there is a max sized for indexed byte slices, which Data will occasionally exceed.

Jump to

Keyboard shortcuts

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