mysql

package
v1.1.4-mainnet Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2023 License: MIT Imports: 36 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MysqlConfKeyReorgVersion = "reorg.version"

	// pre-defined ratelimit strategy config key prefix
	RateLimitStrategyConfKeyPrefix = "ratelimit.strategy."

	// pre-defined access control config key prefix
	AclAllowListConfKeyPrefix = "acl.allowlist."

	// pre-defined node route group config key prefix
	NodeRouteGroupConfKeyPrefix = "noderoute.group."
)

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
	*VirtualFilterLogStore
	*NodeRouteStore
	// 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) DelNodeRouteGroup

func (cs MysqlStore) DelNodeRouteGroup(group string) error

func (MysqlStore) DeleteConfig

func (cs MysqlStore) DeleteConfig(confName string) (bool, 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) LoadAclAllowList

func (cs MysqlStore) LoadAclAllowList(name string) (*acl.AllowList, error)

access control config

func (MysqlStore) LoadAclAllowListById

func (cs MysqlStore) LoadAclAllowListById(aclID uint32) (*acl.AllowList, error)

func (MysqlStore) LoadAclAllowListConfigs

func (cs MysqlStore) LoadAclAllowListConfigs() (map[uint32]*acl.AllowList, map[uint32][md5.Size]byte, error)

func (MysqlStore) LoadConfig

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

func (MysqlStore) LoadNodeRouteGroups

func (cs MysqlStore) LoadNodeRouteGroups(inclusiveGroups ...string) (res map[string]*NodeRouteGroup, err error)

func (MysqlStore) LoadRateLimitConfigs

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

func (MysqlStore) LoadRateLimitStrategy

func (cs MysqlStore) LoadRateLimitStrategy(name string) (*rate.Strategy, error)

func (MysqlStore) LoadRateLimitStrategyConfigs

func (cs MysqlStore) LoadRateLimitStrategyConfigs() (map[uint32]*rate.Strategy, map[uint32][md5.Size]byte, error)

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

func (MysqlStore) StoreNodeRouteGroup

func (cs MysqlStore) StoreNodeRouteGroup(routeGrp *NodeRouteGroup) error

type NodeRoute

type NodeRoute struct {
	ID uint32
	// route key such as access token
	RouteKey string `gorm:"unique;size:128;not null"`
	// desingated route group such as `cfxvip`
	Group string `gorm:"size:64;not null"`

	CreatedAt time.Time
	UpdatedAt time.Time
}

NodeRoute node route configurations

func (NodeRoute) TableName

func (NodeRoute) TableName() string

type NodeRouteFilter

type NodeRouteFilter struct {
	KeySet []string // route key set
	Limit  int      // result limit size (<= 0 means none)
}

type NodeRouteGroup

type NodeRouteGroup struct {
	ID    uint32   `json:"-"`     // group ID
	Name  string   `json:"-"`     // group name
	Nodes []string `json:"nodes"` // node urls
}

type NodeRouteStore

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

func NewNodeRouteStore

func NewNodeRouteStore(db *gorm.DB) *NodeRouteStore

func (*NodeRouteStore) AddNodeRoute

func (nrs *NodeRouteStore) AddNodeRoute(routeKey, routeGroup string) error

func (NodeRouteStore) Close

func (bs NodeRouteStore) Close() error

func (*NodeRouteStore) DeleteNodeRoute

func (nrs *NodeRouteStore) DeleteNodeRoute(routeKey string) (bool, error)

func (*NodeRouteStore) FindNodeRoute

func (nrs *NodeRouteStore) FindNodeRoute(routeKey string) (*NodeRoute, error)

func (NodeRouteStore) IsRecordNotFound

func (NodeRouteStore) IsRecordNotFound(err error) bool

func (*NodeRouteStore) LoadNodeRoutes

func (nrs *NodeRouteStore) LoadNodeRoutes(filter NodeRouteFilter) (res []*NodeRoute, err error)

type RateLimit

type RateLimit struct {
	ID        uint32
	SID       uint32 `gorm:"index"`                    // strategy ID
	AclID     uint32 `gorm:"index"`                    // allow list ID
	LimitType int    `gorm:"default:0;not null"`       // limit type
	LimitKey  string `gorm:"unique;size:128;not null"` // limit key
	Memo      string `gorm:"size:128"`                 // memo

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

func (rls *RateLimitStore) AddRateLimit(
	sid uint32,
	aclId uint32,
	limitType rate.LimitType,
	limitKey string,
	memo string,
) error

func (RateLimitStore) Close

func (bs RateLimitStore) Close() error

func (*RateLimitStore) DeleteRateLimit

func (rls *RateLimitStore) DeleteRateLimit(limitKey string) (bool, error)

func (RateLimitStore) IsRecordNotFound

func (RateLimitStore) IsRecordNotFound(err error) bool

func (*RateLimitStore) LoadRateLimitKeyInfos

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

func (*RateLimitStore) LoadRateLimitKeyset

func (rls *RateLimitStore) LoadRateLimitKeyset(filter *rate.KeysetFilter) (res []*RateLimit, 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

type VirtualFilterLog

type VirtualFilterLog struct {
	ID              uint64
	BlockNumber     uint64 `gorm:"column:bn;not null;index:idx_bn"`
	BlockHash       string `gorm:"column:bh;size:66;not null"`
	ContractAddress string `gorm:"size:66;not null"`
	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"`
	JsonRepr        []byte `gorm:"type:mediumText"` // marshalled json representation
	IsDel           bool   `gorm:"default:false"`   // soft delete flag
	// contains filtered or unexported fields
}

VirtualFilterLog virtual filter event logs

func (VirtualFilterLog) TableName

func (l VirtualFilterLog) TableName() string

type VirtualFilterLogStore

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

VirtualFilterLogStore partitioned store for virtual filter which polls filter changed data instantly.

func NewVirtualFilterLogStore

func NewVirtualFilterLogStore(db *gorm.DB) *VirtualFilterLogStore

func (*VirtualFilterLogStore) Append

func (vfls *VirtualFilterLogStore) Append(fid string, logs []VirtualFilterLog, partition bnPartition) error

Append appends incoming filter changed event logs, if some old ones of same block hashes already existed in the db, they will be soft deleted at first then the new ones will be inserted to the specified partition table.

func (*VirtualFilterLogStore) DeletePartitions

func (vfls *VirtualFilterLogStore) DeletePartitions(fid string) error

DeletePartitions deletes all table partitions for the virtual filter with specified id.

func (*VirtualFilterLogStore) GC

func (vfls *VirtualFilterLogStore) GC(fid string) error

GC garbage collects archive partitions exceeded the max archive partition limit starting from the oldest partiton.

func (*VirtualFilterLogStore) GetLogs

func (vfls *VirtualFilterLogStore) GetLogs(
	ctx context.Context, fid string, sfilter store.LogFilter, blockHashes ...string,
) ([]VirtualFilterLog, error)

GetLogs gets event logs for the specified virtual filter.

func (*VirtualFilterLogStore) PreparePartition

func (vfls *VirtualFilterLogStore) PreparePartition(fid string) (bnPartition, bool, error)

PreparePartitions createa new log partition for the virtual filter if necessary.

Jump to

Keyboard shortcuts

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