mysql

package
v0.0.0-...-3b2d471 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2023 License: MIT Imports: 34 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MysqlConfKeyReorgVersion = "reorg.version"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AddressIndexedLog

type AddressIndexedLog struct {
	ID          uint64
	ContractID  uint64 `gorm:"column:cid;size:64;not null;index:idx_cid_bn,priority:1"`
	BlockNumber uint64 `gorm:"column:bn;not null;index:idx_cid_bn,priority:2"`
	Epoch       uint64 `gorm:"not null;index"` // to support pop logs when reorg
	Topic0      string `gorm:"size:66;not null"`
	Topic1      string `gorm:"size:66"`
	Topic2      string `gorm:"size:66"`
	Topic3      string `gorm:"size:66"`
	LogIndex    uint64 `gorm:"not null"`
	Extra       []byte `gorm:"type:mediumText"` // extention json field
}

func (AddressIndexedLog) TableName

func (AddressIndexedLog) TableName() string

type AddressIndexedLogFilter

type AddressIndexedLogFilter struct {
	LogFilter

	ContractId uint64
}

AddressIndexedLogFilter is used to query event logs that indexed by contract id and block number.

func (*AddressIndexedLogFilter) Find

func (filter *AddressIndexedLogFilter) Find(db *gorm.DB) ([]*AddressIndexedLog, error)

type AddressIndexedLogStore

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

AddressIndexedLogStore is used to store address indexed event logs in N partitions, e.g. logs_1, logs_2, ..., logs_n.

func NewAddressIndexedLogStore

func NewAddressIndexedLogStore(db *gorm.DB, cs *ContractStore, partitions uint32) *AddressIndexedLogStore

func (*AddressIndexedLogStore) AddAddressIndexedLogs

func (ls *AddressIndexedLogStore) AddAddressIndexedLogs(dbTx *gorm.DB, data *store.EpochData, bigContractIds map[uint64]bool) error

AddAddressIndexedLogs adds event logs of specified epoch (with that of big contract ignored) into different partitioned tables.

func (*AddressIndexedLogStore) CreatePartitionedTables

func (ls *AddressIndexedLogStore) CreatePartitionedTables() (int, error)

CreatePartitionedTables initializes partitioned tables.

func (*AddressIndexedLogStore) DeleteAddressIndexedLogs

func (ls *AddressIndexedLogStore) DeleteAddressIndexedLogs(dbTx *gorm.DB, epochFrom, epochTo uint64) error

DeleteAddressIndexedLogs removes event logs of specified epoch number range.

Generally, this is used when pivot chain switched for confirmed blocks.

func (*AddressIndexedLogStore) GetAddressIndexedLogs

func (ls *AddressIndexedLogStore) GetAddressIndexedLogs(
	filter AddressIndexedLogFilter,
	contract string,
) ([]*AddressIndexedLog, error)

GetAddressIndexedLogs returns event logs for the specified filter.

func (*AddressIndexedLogStore) GetPartitionedTableName

func (ls *AddressIndexedLogStore) GetPartitionedTableName(contract string) string

GetPartitionedTableName returns partitioned table name with specified contract address hashed partition index

type Config

type Config struct {
	Enabled bool

	Host     string `default:"127.0.0.1:3306"`
	Username string
	Password string
	Database string

	Dsn string // to support one line config

	ConnMaxLifetime time.Duration `default:"3m"`
	MaxOpenConns    int           `default:"10"`
	MaxIdleConns    int           `default:"10"`

	AddressIndexedLogEnabled    bool   `default:"true"`
	AddressIndexedLogPartitions uint32 `default:"100"`

	MaxBnRangedArchiveLogPartitions uint32 `default:"5"`
}

Config represents the mysql configurations to open a database instance.

func MustNewConfigFromViper

func MustNewConfigFromViper() *Config

MustNewConfigFromViper creates an instance of Config from Viper or panic on error.

func MustNewEthStoreConfigFromViper

func MustNewEthStoreConfigFromViper() *Config

func (*Config) MustOpenOrCreate

func (config *Config) MustOpenOrCreate(option StoreOption) *MysqlStore

MustOpenOrCreate creates an instance of store or exits on any erorr.

type Contract

type Contract struct {
	// id and address are immutable once created.
	ID      uint64 // contract id
	Address string `gorm:"size:64;not null;unique"`

	// num of persisted event logs
	LogCount int `gorm:"not null;default:0"`
	// latest updated epoch height
	LatestUpdatedEpoch uint64 `gorm:"index:index_lue;not null;default:0"`
}

func (Contract) TableName

func (Contract) TableName() string

type ContractStore

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

func NewContractStore

func NewContractStore(db *gorm.DB, cacheSize ...int) *ContractStore

func (*ContractStore) AddContract

func (cs *ContractStore) AddContract(contracts map[string]bool) (int, error)

AddContract adds absent contracts in batch and returns the number of new added contracts.

func (*ContractStore) AddContractByEpochData

func (cs *ContractStore) AddContractByEpochData(slice ...*store.EpochData) (int, error)

AddContract adds contract for the specified epoch data slice and returns the number of new added contracts.

func (*ContractStore) AddContractIfAbsent

func (cs *ContractStore) AddContractIfAbsent(address string) (uint64, bool, error)

AddContractIfAbsent add new contract if absent. Note, this method is thread unsafe. Generally, it is used by data sync thread.

func (ContractStore) Close

func (bs ContractStore) Close() error

func (*ContractStore) GetContractAddressById

func (cs *ContractStore) GetContractAddressById(id uint64) (string, bool, error)

GetContractAddressById gets contract address by id from cache or db if not found.

func (*ContractStore) GetContractById

func (cs *ContractStore) GetContractById(cid uint64) (*Contract, bool, error)

GetContractById gets contract by id from db and also loads the cache.

For contract `LogCount` and `LastUpdatedEpoch` fields, they are quite mutable while cache maybe rarely updated.

func (*ContractStore) GetContractIdByAddress

func (cs *ContractStore) GetContractIdByAddress(address string) (uint64, bool, error)

GetContractIdByAddress gets contract id by address from cache or db if not found.

func (*ContractStore) GetUpdatedContractsSinceEpoch

func (cs *ContractStore) GetUpdatedContractsSinceEpoch(epoch uint64) ([]*Contract, error)

GetContractsByEpoch gets all contracts that have been updated since the specified epoch.

func (ContractStore) IsRecordNotFound

func (ContractStore) IsRecordNotFound(err error) bool

func (*ContractStore) UpdateContractStats

func (cs *ContractStore) UpdateContractStats(
	dbTx *gorm.DB, cid uint64, countDelta int, latestUpdatedEpoch uint64,
) error

UpdateContractStats updates statistics (log count/latest updated epoch) of the specified contract.

type LogFilter

type LogFilter struct {
	TableName string

	// always indexed by block number
	BlockFrom uint64
	BlockTo   uint64

	// event hash and indexed data 1, 2, 3
	Topics []store.VariadicValue
}

logFilter is used to query event logs with specified table, block number range and topics.

func (*LogFilter) Find

func (filter *LogFilter) Find(db *gorm.DB) ([]int, error)

TODO add method FindXxx for type safety and double check the result set size <= max_limit.

type MysqlStore

type MysqlStore struct {
	*UserStore
	*RateLimitStore
	// contains filtered or unexported fields
}

MysqlStore aggregation store for chain data persistence operation.

func (MysqlStore) BlockRange

func (e2bms MysqlStore) BlockRange(epoch uint64) (citypes.RangeUint64, bool, error)

blockRange returns the spanning block range for the give epoch.

func (MysqlStore) Close

func (bs MysqlStore) Close() error

func (MysqlStore) GetBlockByBlockNumber

func (bs MysqlStore) GetBlockByBlockNumber(ctx context.Context, blockNumber uint64) (*store.Block, error)

func (MysqlStore) GetBlockByEpoch

func (bs MysqlStore) GetBlockByEpoch(ctx context.Context, epochNumber uint64) (*store.Block, error)

func (MysqlStore) GetBlockByHash

func (bs MysqlStore) GetBlockByHash(ctx context.Context, blockHash types.Hash) (*store.Block, error)

func (MysqlStore) GetBlockSummaryByBlockNumber

func (bs MysqlStore) GetBlockSummaryByBlockNumber(ctx context.Context, blockNumber uint64) (*store.BlockSummary, error)

func (MysqlStore) GetBlockSummaryByEpoch

func (bs MysqlStore) GetBlockSummaryByEpoch(ctx context.Context, epochNumber uint64) (*store.BlockSummary, error)

func (MysqlStore) GetBlockSummaryByHash

func (bs MysqlStore) GetBlockSummaryByHash(ctx context.Context, blockHash types.Hash) (*store.BlockSummary, error)

func (MysqlStore) GetBlocksByEpoch

func (bs MysqlStore) GetBlocksByEpoch(ctx context.Context, epochNumber uint64) ([]types.Hash, error)

func (*MysqlStore) GetLogs

func (ms *MysqlStore) GetLogs(ctx context.Context, storeFilter store.LogFilter) ([]*store.Log, error)

func (MysqlStore) GetReceipt

func (ts MysqlStore) GetReceipt(ctx context.Context, txHash types.Hash) (*store.TransactionReceipt, error)

func (MysqlStore) GetReorgVersion

func (cs MysqlStore) GetReorgVersion() (int, error)

func (MysqlStore) GetTransaction

func (ts MysqlStore) GetTransaction(ctx context.Context, txHash types.Hash) (*store.Transaction, error)

func (MysqlStore) IsRecordNotFound

func (MysqlStore) IsRecordNotFound(err error) bool

func (MysqlStore) LoadConfig

func (cs MysqlStore) LoadConfig(confNames ...string) (map[string]interface{}, error)

func (MysqlStore) LoadRateLimitConfigs

func (cs MysqlStore) LoadRateLimitConfigs() *rate.Config

func (MysqlStore) MaxEpoch

func (e2bms MysqlStore) MaxEpoch() (uint64, bool, error)

MaxEpoch returns the max epoch within the map store.

func (MysqlStore) PivotHash

func (e2bms MysqlStore) PivotHash(epoch uint64) (string, bool, error)

pivotHash returns the pivot hash of the given epoch.

func (*MysqlStore) Popn

func (ms *MysqlStore) Popn(epochUntil uint64) error

Popn pops multiple epoch data from database.

func (*MysqlStore) Prune

func (ms *MysqlStore) Prune()

Prune prune data from db store.

func (*MysqlStore) Push

func (ms *MysqlStore) Push(data *store.EpochData) error

func (*MysqlStore) Pushn

func (ms *MysqlStore) Pushn(dataSlice []*store.EpochData) error

func (MysqlStore) StoreConfig

func (cs MysqlStore) StoreConfig(confName string, confVal interface{}) error

type RateLimit

type RateLimit struct {
	ID        uint32
	SID       uint32 // strategy ID
	LimitType int    `gorm:"default:0;not null"`       // limit type
	LimitKey  string `gorm:"unique;size:128;not null"` // limit key
	CreatedAt time.Time
	UpdatedAt time.Time
}

RateLimit rate limit keyset table

func (RateLimit) TableName

func (RateLimit) TableName() string

type RateLimitStore

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

func NewRateLimitStore

func NewRateLimitStore(db *gorm.DB) *RateLimitStore

func (RateLimitStore) Close

func (bs RateLimitStore) Close() error

func (RateLimitStore) IsRecordNotFound

func (RateLimitStore) IsRecordNotFound(err error) bool

func (*RateLimitStore) LoadRateLimitKeyset

func (rls *RateLimitStore) LoadRateLimitKeyset(filter *rate.KeysetFilter) (res []*rate.KeyInfo, err error)

type StoreOption

type StoreOption struct {
	Disabler store.StoreDisabler
}

type User

type User struct {
	ID          uint32
	Name        string `gorm:"size:256;not null;unique"`
	Description string `gorm:"size:1024"`
	ApiKey      string `gorm:"size:256;not null;unique"`
	NodeUrl     string `gorm:"size:256;not null"`
}

User represents a VIP user that provide specific archive node to query historical event logs.

func (User) TableName

func (User) TableName() string

type UserStore

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

func (UserStore) Close

func (bs UserStore) Close() error

func (*UserStore) GetUserByKey

func (us *UserStore) GetUserByKey(key string) (*User, bool, error)

func (UserStore) IsRecordNotFound

func (UserStore) IsRecordNotFound(err error) bool

Jump to

Keyboard shortcuts

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