database

package
v0.1.34 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2023 License: AGPL-3.0 Imports: 11 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"
)

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)

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

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

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

	// 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)

	// 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

	// 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 added in v0.1.28

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 added in v0.1.32

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

func (*Datastore) MaxConcurrentWrites added in v0.1.28

func (d *Datastore) MaxConcurrentWrites() int

func (*Datastore) NodeList added in v0.1.33

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

func (*Datastore) ShouldWriteBlockEvents added in v0.1.28

func (d *Datastore) ShouldWriteBlockEvents() bool

func (*Datastore) ShouldWriteBlocks added in v0.1.28

func (d *Datastore) ShouldWriteBlocks() bool

func (*Datastore) ShouldWriteTransactionEvents added in v0.1.28

func (d *Datastore) ShouldWriteTransactionEvents() bool

func (*Datastore) ShouldWriteTransactions added in v0.1.28

func (d *Datastore) ShouldWriteTransactions() bool

func (*Datastore) WriteBlock added in v0.1.28

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

WriteBlock writes the block and the block event to datastore.

func (*Datastore) WriteBlockBody added in v0.1.28

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

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 added in v0.1.28

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

WriteBlockHashes will write the block events to datastore.

func (*Datastore) WriteBlockHeaders added in v0.1.28

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

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) WriteTransactions added in v0.1.28

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

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
}

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
	Difficulty  string
	Number      string
	GasLimit    string
	GasUsed     string
	Time        time.Time
	Extra       []byte
	MixDigest   string
	Nonce       string
	BaseFee     string
}

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

type DatastoreOptions added in v0.1.28

type DatastoreOptions struct {
	ProjectID                    string
	DatabaseID                   string
	SensorID                     string
	MaxConcurrency               int
	ShouldWriteBlocks            bool
	ShouldWriteBlockEvents       bool
	ShouldWriteTransactions      bool
	ShouldWriteTransactionEvents bool
}

DatastoreOptions is used when creating a NewDatastore.

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
	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