db

package
v0.0.0-...-c096a92 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Connect

func Connect() *gorm.DB

Connect - Connecting to postgresql database

func DeleteBlock

func DeleteBlock(dbWTx *gorm.DB, number uint64) error

DeleteBlock - Delete block entry, identified by block number, while cascading all dependent entries ( i.e. in transactions/ events table )

func DoesItMatch

func DoesItMatch(event *data.Event, topics map[uint8]string) bool

DoesItMatch - Given one event emitted by some contract & topic signature against which we're attempting to match the event it'll return boolean depending upon whether it's satisfying all conditions or not

func EventTopicsAsString

func EventTopicsAsString(topics map[uint8]string) string

EventTopicsAsString - Given event topic signatures as map, returns these topics as string, seperated by `,` i.e. comma to be used for forming DB raw query

func ExtractOutOnlyMatchingEvents

func ExtractOutOnlyMatchingEvents(events []*data.Event, topics map[uint8]string) *data.Events

ExtractOutOnlyMatchingEvents - Extract out only those events which are having full match with specified event topic signatures

func GetAllBlockNumbersInRange

func GetAllBlockNumbersInRange(db *gorm.DB, from uint64, to uint64) []uint64

GetAllBlockNumbersInRange - Returns all block numbers in given range, both inclusive

func GetBlockByHash

func GetBlockByHash(db *gorm.DB, hash common.Hash) *data.Block

GetBlockByHash - Given blockhash finds out block related information

If not found, returns nil

func GetBlockByNumber

func GetBlockByNumber(db *gorm.DB, number uint64) *data.Block

GetBlockByNumber - Fetch block using block number

If not found, returns nil

func GetBlockCount

func GetBlockCount(db *gorm.DB) uint64

GetBlockCount - Returns how many blocks currently present in database

Caution : As we're dealing with very large tables ( with row count ~ 10M & increasing 1 row every 2 seconds ) this function needs to be least frequently, otherwise due to full table scan it'll cost us a lot

Currently only using during application start up

All other block count calculation requirements can be fulfilled by using in-memory program state holder

func GetBlocksByNumberRange

func GetBlocksByNumberRange(db *gorm.DB, from uint64, to uint64) *data.Blocks

GetBlocksByNumberRange - Given block numbers as range, it'll extract out those blocks by number, while returning them in ascendically sorted form in terms of block numbers

Note : Can return at max 10 blocks in a single query

If more blocks are requested, simply to be rejected In that case, consider splitting them such that they satisfy criteria

func GetBlocksByTimeRange

func GetBlocksByTimeRange(db *gorm.DB, from uint64, to uint64) *data.Blocks

GetBlocksByTimeRange - Given time range ( of 60 sec span at max ), returns blocks mined in that time span

If asked to find out blocks in time span larger than 60 sec, simply drops query request

func GetContractCreationTransactionsFromAccountByBlockNumberRange

func GetContractCreationTransactionsFromAccountByBlockNumberRange(db *gorm.DB, account common.Address, from uint64, to uint64) *data.Transactions

GetContractCreationTransactionsFromAccountByBlockNumberRange - Fetch all contract creation tx(s) from given account with in specific block number range

func GetContractCreationTransactionsFromAccountByBlockTimeRange

func GetContractCreationTransactionsFromAccountByBlockTimeRange(db *gorm.DB, account common.Address, from uint64, to uint64) *data.Transactions

GetContractCreationTransactionsFromAccountByBlockTimeRange - Fetch all contract creation tx(s) from given account with in specific block time span range

func GetCurrentBlockNumber

func GetCurrentBlockNumber(db *gorm.DB) uint64

GetCurrentBlockNumber - Returns highest block number, which got processed by the service

func GetCurrentOldestBlockNumber

func GetCurrentOldestBlockNumber(db *gorm.DB) uint64

GetCurrentOldestBlockNumber - Fetches what's lowest block number present in database, which denotes if it's not 0, from here we can start syncing again, until we reach 0

func GetEventByBlockHashAndLogIndex

func GetEventByBlockHashAndLogIndex(db *gorm.DB, hash common.Hash, index uint) *data.Event

GetEventByBlockHashAndLogIndex - Given block hash and log index in block return respective event log, if any exists

func GetEventByBlockNumberAndLogIndex

func GetEventByBlockNumberAndLogIndex(db *gorm.DB, number uint64, index uint) *data.Event

GetEventByBlockNumberAndLogIndex - Given block number and log index in block return respective event log, if any exists

func GetEventsByBlockHash

func GetEventsByBlockHash(db *gorm.DB, blockHash common.Hash) *data.Events

GetEventsByBlockHash - Given block hash retrieves all events from all tx present in that block

func GetEventsByTransactionHash

func GetEventsByTransactionHash(db *gorm.DB, txHash common.Hash) *data.Events

GetEventsByTransactionHash - Given tx hash, returns all events emitted during contract interaction ( i.e. tx execution )

func GetEventsFromContractByBlockNumberRange

func GetEventsFromContractByBlockNumberRange(db *gorm.DB, contract common.Address, from uint64, to uint64) *data.Events

GetEventsFromContractByBlockNumberRange - Given block number range & contract address, extracts out all events emitted by this contract during block span

func GetEventsFromContractByBlockTimeRange

func GetEventsFromContractByBlockTimeRange(db *gorm.DB, contract common.Address, from uint64, to uint64) *data.Events

GetEventsFromContractByBlockTimeRange - Given block time range & contract address, extracts out all events emitted by this contract during time span

func GetEventsFromContractWithTopicsByBlockNumberRange

func GetEventsFromContractWithTopicsByBlockNumberRange(db *gorm.DB, contract common.Address, from uint64, to uint64, topics map[uint8]string) *data.Events

GetEventsFromContractWithTopicsByBlockNumberRange - Given block number range, contract address & topics of event log, extracts out all events emitted by this contract during block span with topic signatures matching

func GetEventsFromContractWithTopicsByBlockTimeRange

func GetEventsFromContractWithTopicsByBlockTimeRange(db *gorm.DB, contract common.Address, from uint64, to uint64, topics map[uint8]string) *data.Events

GetEventsFromContractWithTopicsByBlockTimeRange - Given time range, contract address & topics of event log, extracts out all events emitted by this contract during block span with topic signatures matching

func GetLastXEventsFromContract

func GetLastXEventsFromContract(db *gorm.DB, contract common.Address, x int) *data.Events

GetLastXEventsFromContract - Finds out last `x` events emitted by contract

func GetTransactionByHash

func GetTransactionByHash(db *gorm.DB, hash common.Hash) *data.Transaction

GetTransactionByHash - Given tx hash, extracts out transaction related data

func GetTransactionCountBetweenAccountsByBlockNumberRange

func GetTransactionCountBetweenAccountsByBlockNumberRange(db *gorm.DB, fromAccount common.Address, toAccount common.Address, from uint64, to uint64) int64

GetTransactionCountBetweenAccountsByBlockNumberRange - Given from & to account addresses & block number range, returns #-of transactions where `from` & `to` fields are matching

func GetTransactionCountBetweenAccountsByBlockTimeRange

func GetTransactionCountBetweenAccountsByBlockTimeRange(db *gorm.DB, fromAccount common.Address, toAccount common.Address, from uint64, to uint64) int64

GetTransactionCountBetweenAccountsByBlockTimeRange - Given from & to account addresses & block mining time range, returns #-of transactions where `from` & `to` fields are matching

func GetTransactionCountByBlockHash

func GetTransactionCountByBlockHash(db *gorm.DB, hash common.Hash) int64

GetTransactionCountByBlockHash - Given block hash, finds out how many transactions are packed in that block

func GetTransactionCountByBlockNumber

func GetTransactionCountByBlockNumber(db *gorm.DB, number uint64) int64

GetTransactionCountByBlockNumber - Given block number, finds out how many transactions are packed in that block

func GetTransactionCountFromAccountByBlockNumberRange

func GetTransactionCountFromAccountByBlockNumberRange(db *gorm.DB, account common.Address, from uint64, to uint64) int64

GetTransactionCountFromAccountByBlockNumberRange - Given account address & block number range, it can find out how many tx(s) were sent from this account in specified block range

func GetTransactionCountFromAccountByBlockTimeRange

func GetTransactionCountFromAccountByBlockTimeRange(db *gorm.DB, account common.Address, from uint64, to uint64) int64

GetTransactionCountFromAccountByBlockTimeRange - Given account address & block mining time stamp range, it can find out count of all tx(s) performed by this address, with in that time span

func GetTransactionCountToAccountByBlockNumberRange

func GetTransactionCountToAccountByBlockNumberRange(db *gorm.DB, account common.Address, from uint64, to uint64) int64

GetTransactionCountToAccountByBlockNumberRange - Given account address & block number range, returns #-of transactions where `account` was in `to` field

func GetTransactionCountToAccountByBlockTimeRange

func GetTransactionCountToAccountByBlockTimeRange(db *gorm.DB, account common.Address, from uint64, to uint64) int64

GetTransactionCountToAccountByBlockTimeRange - Given account address which is present in `to` field of tx(s) held in blocks mined with in given time range, returns those tx count

func GetTransactionFromAccountWithNonce

func GetTransactionFromAccountWithNonce(db *gorm.DB, account common.Address, nonce uint64) *data.Transaction

GetTransactionFromAccountWithNonce - Given tx sender address & account nonce, finds out tx, satisfying condition

func GetTransactionsBetweenAccountsByBlockNumberRange

func GetTransactionsBetweenAccountsByBlockNumberRange(db *gorm.DB, fromAccount common.Address, toAccount common.Address, from uint64, to uint64) *data.Transactions

GetTransactionsBetweenAccountsByBlockNumberRange - Given from & to account addresses & block number range, returns transactions where `from` & `to` fields are matching

func GetTransactionsBetweenAccountsByBlockTimeRange

func GetTransactionsBetweenAccountsByBlockTimeRange(db *gorm.DB, fromAccount common.Address, toAccount common.Address, from uint64, to uint64) *data.Transactions

GetTransactionsBetweenAccountsByBlockTimeRange - Given from & to account addresses & block mining time range, returns transactions where `from` & `to` fields are matching

func GetTransactionsByBlockHash

func GetTransactionsByBlockHash(db *gorm.DB, hash common.Hash) *data.Transactions

GetTransactionsByBlockHash - Given block hash, returns all transactions present in that block

func GetTransactionsByBlockNumber

func GetTransactionsByBlockNumber(db *gorm.DB, number uint64) *data.Transactions

GetTransactionsByBlockNumber - Given block number, returns all transactions present in that block

func GetTransactionsFromAccountByBlockNumberRange

func GetTransactionsFromAccountByBlockNumberRange(db *gorm.DB, account common.Address, from uint64, to uint64) *data.Transactions

GetTransactionsFromAccountByBlockNumberRange - Given account address & block number range, it can find out all transactions which are performed from this account

func GetTransactionsFromAccountByBlockTimeRange

func GetTransactionsFromAccountByBlockTimeRange(db *gorm.DB, account common.Address, from uint64, to uint64) *data.Transactions

GetTransactionsFromAccountByBlockTimeRange - Given account address & block mining time stamp range, it can find out all tx(s) performed from this account, with in that time span

func GetTransactionsToAccountByBlockNumberRange

func GetTransactionsToAccountByBlockNumberRange(db *gorm.DB, account common.Address, from uint64, to uint64) *data.Transactions

GetTransactionsToAccountByBlockNumberRange - Given account address & block number range, returns transactions where `account` was in `to` field

func GetTransactionsToAccountByBlockTimeRange

func GetTransactionsToAccountByBlockTimeRange(db *gorm.DB, account common.Address, from uint64, to uint64) *data.Transactions

GetTransactionsToAccountByBlockTimeRange - Given account address which is present in `to` field of tx(s) held in blocks mined with in given time range

func PutBlock

func PutBlock(dbWTx *gorm.DB, block *Blocks) error

PutBlock - Persisting fetched block

func RemoveEventsByBlockHash

func RemoveEventsByBlockHash(dbWTx *gorm.DB, blockHash string) error

RemoveEventsByBlockHash - All events emitted by tx(s) packed in block, to be removed from DB

func RemoveTransactionsByBlockHash

func RemoveTransactionsByBlockHash(dbWTx *gorm.DB, blockHash string) error

RemoveTransactionsByBlockHash - Remove all tx(s) packed in this block from DB

func StoreBlock

func StoreBlock(dbWOTx *gorm.DB, block *PackedBlock, status *d.StatusHolder, queue *q.BlockProcessorQueue) error

StoreBlock - Persisting block data in database, if data already not stored

Also checks equality with existing data, if mismatch found, updated with latest data

Tries to wrap db modifications inside database transaction to guarantee consistency, other read only operations being performed without protection of db transaction

👆 gives us performance improvement, also taste of atomic db operation i.e. either whole block data is written or nothing is written

func UpdateBlock

func UpdateBlock(dbWTx *gorm.DB, block *Blocks) error

UpdateBlock - Updating already existing block

func UpsertEvent

func UpsertEvent(dbWTx *gorm.DB, event *Events) error

UpsertEvent - It may be the case previously this block was processed and event log also got persisted into database, which has been updated on chain, due to chain reorganization

Now we want to persist latest entry into database, using upsert operation i.e. if entry doesn't exist yet, it'll be created, but if it does i.e. conflicting primary key found, then, all fields will be updated to latest value

func UpsertTransaction

func UpsertTransaction(dbWTx *gorm.DB, tx *Transactions) error

UpsertTransaction - It may be the case previously this block was processed and transaction also got persisted into database, which has been updated on chain, due to chain reorganization

Now we want to persist latest entry into database, using upsert operation i.e. if entry doesn't exist yet, it'll be created, but if it does i.e. conflicting primary key found, then, all fields will be updated to latest value

Types

type Blocks

type Blocks struct {
	Hash                string       `gorm:"column:hash;type:char(66);primaryKey"`
	Number              uint64       `gorm:"column:number;type:bigint;not null;unique;index:,sort:asc"`
	Time                uint64       `gorm:"column:time;type:bigint;not null;index:,sort:asc"`
	ParentHash          string       `gorm:"column:parenthash;type:char(66);not null"`
	Difficulty          string       `gorm:"column:difficulty;type:varchar;not null"`
	GasUsed             uint64       `gorm:"column:gasused;type:bigint;not null"`
	GasLimit            uint64       `gorm:"column:gaslimit;type:bigint;not null"`
	Nonce               string       `gorm:"column:nonce;type:varchar;not null"`
	Miner               string       `gorm:"column:miner;type:char(42);not null"`
	Size                float64      `gorm:"column:size;type:float(8);not null"`
	StateRootHash       string       `gorm:"column:stateroothash;type:char(66);not null"`
	UncleHash           string       `gorm:"column:unclehash;type:char(66);not null"`
	TransactionRootHash string       `gorm:"column:txroothash;type:char(66);not null"`
	ReceiptRootHash     string       `gorm:"column:receiptroothash;type:char(66);not null"`
	ExtraData           []byte       `gorm:"column:extradata;type:bytea"`
	Transactions        Transactions `gorm:"foreignKey:blockhash;constraint:OnDelete:CASCADE;"`
	Events              Events       `gorm:"foreignKey:blockhash;constraint:OnDelete:CASCADE;"`
}

Blocks - Mined block info holder table model

func GetBlock

func GetBlock(_db *gorm.DB, number uint64) *Blocks

GetBlock - Fetch block by number, from database

func (*Blocks) SimilarTo

func (b *Blocks) SimilarTo(_b *Blocks) bool

SimilarTo - Checking whether two blocks are exactly similar or not

func (Blocks) TableName

func (Blocks) TableName() string

TableName - Overriding default table name

type Events

type Events struct {
	BlockHash       string         `gorm:"column:blockhash;type:char(66);not null;primaryKey"`
	Index           uint           `gorm:"column:index;type:integer;not null;primaryKey"`
	Origin          string         `gorm:"column:origin;type:char(42);not null;index"`
	Topics          pq.StringArray `gorm:"column:topics;type:text[];not null;index:,type:gin"`
	Data            []byte         `gorm:"column:data;type:bytea"`
	TransactionHash string         `gorm:"column:txhash;type:char(66);not null;index"`
}

Events - Events emitted from smart contracts to be held in this table

func (Events) TableName

func (Events) TableName() string

TableName - Overriding default table name

type PackedBlock

type PackedBlock struct {
	Block        *Blocks
	Transactions []*PackedTransaction
}

PackedBlock - Whole block data to be persisted in a single database transaction to ensure data consistency, if something goes wrong in mid, whole persisting operation will get reverted

type PackedTransaction

type PackedTransaction struct {
	Tx     *Transactions
	Events []*Events
}

PackedTransaction - All data that is stored in a tx, to be passed from tx data fetcher to whole block data persist handler function

type Tabler

type Tabler interface {
	TableName() string
}

Tabler - ...

type Transactions

type Transactions struct {
	Hash      string `gorm:"column:hash;type:char(66);primaryKey"`
	From      string `gorm:"column:from;type:char(42);not null;index"`
	To        string `gorm:"column:to;type:char(42);index"`
	Contract  string `gorm:"column:contract;type:char(42);index"`
	Value     string `gorm:"column:value;type:varchar"`
	Data      []byte `gorm:"column:data;type:bytea"`
	Gas       uint64 `gorm:"column:gas;type:bigint;not null"`
	GasPrice  string `gorm:"column:gasprice;type:varchar;not null"`
	Cost      string `gorm:"column:cost;type:varchar;not null"`
	Nonce     uint64 `gorm:"column:nonce;type:bigint;not null;index"`
	State     uint64 `gorm:"column:state;type:smallint;not null"`
	BlockHash string `gorm:"column:blockhash;type:char(66);not null;index"`
	Events    Events `gorm:"foreignKey:txhash;constraint:OnDelete:CASCADE;"`
}

Transactions - Blockchain transaction holder table model

func (Transactions) TableName

func (Transactions) TableName() string

TableName - Overriding default table name

Jump to

Keyboard shortcuts

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