mysql

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2026 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewFromCmdlineOptions

func NewFromCmdlineOptions() plugin.Plugin

Types

type CommitTimestamp

type CommitTimestamp struct {
	ID        uint `gorm:"primarykey"`
	Timestamp int64
}

CommitTimestamp represents the MySQL record used to track the current commit timestamp

func (CommitTimestamp) TableName

func (CommitTimestamp) TableName() string

type MetadataStoreMysql

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

MetadataStoreMysql stores metadata in MySQL.

func New

func New(
	host string,
	port uint,
	user string,
	password string,
	database string,
	sslMode string,
	timeZone string,
	logger *slog.Logger,
	promRegistry prometheus.Registerer,
) (*MetadataStoreMysql, error)

New creates a new database

func NewWithOptions

func NewWithOptions(opts ...MysqlOptionFunc) (*MetadataStoreMysql, error)

NewWithOptions creates a new database with options

func (*MetadataStoreMysql) AddUtxos

func (d *MetadataStoreMysql) AddUtxos(
	utxos []models.UtxoSlot,
	txn types.Txn,
) error

AddUtxos saves a batch of UTxOs

func (*MetadataStoreMysql) AutoMigrate

func (d *MetadataStoreMysql) AutoMigrate(dst ...any) error

AutoMigrate wraps the gorm AutoMigrate

func (*MetadataStoreMysql) BeginTxn

func (d *MetadataStoreMysql) BeginTxn() (types.Txn, error)

BeginTxn starts a transaction and returns the handle with an error. Callers that prefer explicit error handling can use this instead of Transaction().

func (*MetadataStoreMysql) Close

func (d *MetadataStoreMysql) Close() error

Close gets the database handle from our MetadataStore and closes it

func (*MetadataStoreMysql) Create

func (d *MetadataStoreMysql) Create(value any) *gorm.DB

Create creates a record

func (*MetadataStoreMysql) DB

func (d *MetadataStoreMysql) DB() *gorm.DB

DB returns the database handle

func (*MetadataStoreMysql) DeleteBlockNoncesBeforeSlot

func (d *MetadataStoreMysql) DeleteBlockNoncesBeforeSlot(
	slotNumber uint64,
	txn types.Txn,
) error

DeleteBlockNoncesBeforeSlot deletes block_nonce records with slot less than the specified value

func (*MetadataStoreMysql) DeleteBlockNoncesBeforeSlotWithoutCheckpoints

func (d *MetadataStoreMysql) DeleteBlockNoncesBeforeSlotWithoutCheckpoints(
	slotNumber uint64,
	txn types.Txn,
) error

DeleteBlockNoncesBeforeSlotWithoutCheckpoints deletes block_nonce records with slot < given value AND is_checkpoint = false

func (*MetadataStoreMysql) DeleteCertificatesAfterSlot

func (d *MetadataStoreMysql) DeleteCertificatesAfterSlot(
	slot uint64,
	txn types.Txn,
) error

DeleteCertificatesAfterSlot removes all certificate records added after the given slot. This is used during chain rollbacks to undo certificate state changes. All deletions are performed atomically within a transaction to ensure consistency.

func (*MetadataStoreMysql) DeletePParamUpdatesAfterSlot

func (d *MetadataStoreMysql) DeletePParamUpdatesAfterSlot(
	slot uint64,
	txn types.Txn,
) error

DeletePParamUpdatesAfterSlot removes protocol parameter update records added after the given slot.

func (*MetadataStoreMysql) DeletePParamsAfterSlot

func (d *MetadataStoreMysql) DeletePParamsAfterSlot(
	slot uint64,
	txn types.Txn,
) error

DeletePParamsAfterSlot removes protocol parameter records added after the given slot.

func (*MetadataStoreMysql) DeleteTransactionsAfterSlot

func (d *MetadataStoreMysql) DeleteTransactionsAfterSlot(
	slot uint64,
	txn types.Txn,
) error

DeleteTransactionsAfterSlot removes transaction records added after the given slot. This also clears UTXO references (spent_at_tx_id, collateral_by_tx_id, referenced_by_tx_id) to transactions being deleted, effectively restoring UTXOs to their unspent state. UTXO hash-based foreign keys are NULLed out before deleting transactions to prevent orphaned references.

func (*MetadataStoreMysql) DeleteUtxo

func (d *MetadataStoreMysql) DeleteUtxo(
	utxoId models.UtxoId,
	txn types.Txn,
) error

func (*MetadataStoreMysql) DeleteUtxos

func (d *MetadataStoreMysql) DeleteUtxos(
	utxos []models.UtxoId,
	txn types.Txn,
) error

func (*MetadataStoreMysql) DeleteUtxosAfterSlot

func (d *MetadataStoreMysql) DeleteUtxosAfterSlot(
	slot uint64,
	txn types.Txn,
) error

func (*MetadataStoreMysql) First

func (d *MetadataStoreMysql) First(args any) *gorm.DB

First returns the first DB entry

func (*MetadataStoreMysql) GetAccount

func (d *MetadataStoreMysql) GetAccount(
	stakeKey []byte,
	includeInactive bool,
	txn types.Txn,
) (*models.Account, error)

GetAccount gets an account

func (*MetadataStoreMysql) GetActivePoolRelays

func (d *MetadataStoreMysql) GetActivePoolRelays(
	txn types.Txn,
) ([]models.PoolRegistrationRelay, error)

GetActivePoolRelays returns all relays from currently active pools. A pool is considered active if it has a registration and either: - No retirement, or - The retirement epoch is in the future

Implementation note: This function loads all registrations and retirements per pool and filters in Go rather than using complex SQL JOINs. This approach is necessary because:

  1. GORM's Preload with Limit(1) applies the limit globally, not per-parent
  2. Determining the "latest" registration/retirement requires comparing added_slot values which is cumbersome in a single query
  3. The filtering logic (retirement epoch vs current epoch) is clearer in Go

For networks with thousands of pools, this may use significant memory. Future optimization could use raw SQL with window functions (ROW_NUMBER) or batch processing if memory becomes a concern.

func (*MetadataStoreMysql) GetAssetByPolicyAndName

func (d *MetadataStoreMysql) GetAssetByPolicyAndName(
	policyId lcommon.Blake2b224,
	assetName []byte,
	txn types.Txn,
) (models.Asset, error)

GetAssetByPolicyAndName returns an asset by policy ID and asset name

func (*MetadataStoreMysql) GetAssetsByPolicy

func (d *MetadataStoreMysql) GetAssetsByPolicy(
	policyId lcommon.Blake2b224,
	txn types.Txn,
) ([]models.Asset, error)

GetAssetsByPolicy returns all assets for a given policy ID

func (*MetadataStoreMysql) GetAssetsByUTxO

func (d *MetadataStoreMysql) GetAssetsByUTxO(
	txId []byte,
	idx uint32,
	txn types.Txn,
) ([]models.Asset, error)

GetAssetsByUTxO returns all assets for a given UTxO using transaction ID and output index

func (*MetadataStoreMysql) GetBlockNonce

func (d *MetadataStoreMysql) GetBlockNonce(
	point ocommon.Point,
	txn types.Txn,
) ([]byte, error)

GetBlockNonce retrieves the block nonce for a specific block

func (*MetadataStoreMysql) GetCommitTimestamp

func (d *MetadataStoreMysql) GetCommitTimestamp() (int64, error)

func (*MetadataStoreMysql) GetDatum

func (d *MetadataStoreMysql) GetDatum(
	hash lcommon.Blake2b256,
	txn types.Txn,
) (*models.Datum, error)

GetDatum returns a datum by its hash

func (*MetadataStoreMysql) GetDrep

func (d *MetadataStoreMysql) GetDrep(
	cred []byte,
	includeInactive bool,
	txn types.Txn,
) (*models.Drep, error)

GetDrep gets a drep

func (*MetadataStoreMysql) GetEpochSummary

func (d *MetadataStoreMysql) GetEpochSummary(
	epoch uint64,
	txn types.Txn,
) (*models.EpochSummary, error)

GetEpochSummary retrieves an epoch summary by epoch number

func (*MetadataStoreMysql) GetEpochs

func (d *MetadataStoreMysql) GetEpochs(
	txn types.Txn,
) ([]models.Epoch, error)

GetEpochs returns the list of epochs

func (*MetadataStoreMysql) GetEpochsByEra

func (d *MetadataStoreMysql) GetEpochsByEra(
	eraId uint,
	txn types.Txn,
) ([]models.Epoch, error)

GetEpochsByEra returns the list of epochs by era

func (*MetadataStoreMysql) GetLatestEpochSummary

func (d *MetadataStoreMysql) GetLatestEpochSummary(
	txn types.Txn,
) (*models.EpochSummary, error)

GetLatestEpochSummary retrieves the most recent epoch summary

func (*MetadataStoreMysql) GetPParamUpdates

func (d *MetadataStoreMysql) GetPParamUpdates(
	epoch uint64,
	txn types.Txn,
) ([]models.PParamUpdate, error)

GetPParamUpdates returns a list of protocol parameter updates for a given epoch

func (*MetadataStoreMysql) GetPParams

func (d *MetadataStoreMysql) GetPParams(
	epoch uint64,
	txn types.Txn,
) ([]models.PParams, error)

GetPParams returns a list of protocol parameters for a given epoch. If there are no pparams for the specified epoch, it will return the most recent pparams before the specified epoch

func (*MetadataStoreMysql) GetPool

func (d *MetadataStoreMysql) GetPool(
	pkh lcommon.PoolKeyHash,
	includeInactive bool,
	txn types.Txn,
) (*models.Pool, error)

GetPool gets a pool

func (*MetadataStoreMysql) GetPoolRegistrations

func (d *MetadataStoreMysql) GetPoolRegistrations(
	pkh lcommon.PoolKeyHash,
	txn types.Txn,
) ([]lcommon.PoolRegistrationCertificate, error)

GetPoolRegistrations returns pool registration certificates

func (*MetadataStoreMysql) GetPoolStakeSnapshot

func (d *MetadataStoreMysql) GetPoolStakeSnapshot(
	epoch uint64,
	snapshotType string,
	poolKeyHash []byte,
	txn types.Txn,
) (*models.PoolStakeSnapshot, error)

GetPoolStakeSnapshot retrieves a specific pool's stake snapshot

func (*MetadataStoreMysql) GetPoolStakeSnapshotsByEpoch

func (d *MetadataStoreMysql) GetPoolStakeSnapshotsByEpoch(
	epoch uint64,
	snapshotType string,
	txn types.Txn,
) ([]*models.PoolStakeSnapshot, error)

GetPoolStakeSnapshotsByEpoch retrieves all pool stake snapshots for an epoch

func (*MetadataStoreMysql) GetScript

func (d *MetadataStoreMysql) GetScript(
	hash lcommon.ScriptHash,
	txn types.Txn,
) (*models.Script, error)

GetScript returns the script content by its hash

func (*MetadataStoreMysql) GetStakeRegistrations

func (d *MetadataStoreMysql) GetStakeRegistrations(
	stakingKey []byte,
	txn types.Txn,
) ([]lcommon.StakeRegistrationCertificate, error)

GetStakeRegistrations returns stake registration certificates

func (*MetadataStoreMysql) GetTip

func (d *MetadataStoreMysql) GetTip(
	txn types.Txn,
) (ocommon.Tip, error)

GetTip returns the current metadata Tip as ocommon.Tip

func (*MetadataStoreMysql) GetTotalActiveStake

func (d *MetadataStoreMysql) GetTotalActiveStake(
	epoch uint64,
	snapshotType string,
	txn types.Txn,
) (uint64, error)

GetTotalActiveStake returns the sum of all pool stakes for an epoch

func (*MetadataStoreMysql) GetTransactionByHash

func (d *MetadataStoreMysql) GetTransactionByHash(
	hash []byte,
	txn types.Txn,
) (*models.Transaction, error)

GetTransactionByHash returns a transaction by its hash

func (*MetadataStoreMysql) GetUtxo

func (d *MetadataStoreMysql) GetUtxo(
	txId []byte,
	idx uint32,
	txn types.Txn,
) (*models.Utxo, error)

GetUtxo returns a Utxo by reference

func (*MetadataStoreMysql) GetUtxosAddedAfterSlot

func (d *MetadataStoreMysql) GetUtxosAddedAfterSlot(
	slot uint64,
	txn types.Txn,
) ([]models.Utxo, error)

GetUtxosAddedAfterSlot returns a list of Utxos added after a given slot

func (*MetadataStoreMysql) GetUtxosByAddress

func (d *MetadataStoreMysql) GetUtxosByAddress(
	addr ledger.Address,
	txn types.Txn,
) ([]models.Utxo, error)

GetUtxosByAddress returns a list of Utxos

func (*MetadataStoreMysql) GetUtxosByAssets

func (d *MetadataStoreMysql) GetUtxosByAssets(
	policyId []byte,
	assetName []byte,
	txn types.Txn,
) ([]models.Utxo, error)

GetUtxosByAssets returns a list of Utxos that contain the specified assets policyId: the policy ID of the asset (required) assetName: the asset name (pass nil to match all assets under the policy, or empty []byte{} to match assets with empty names)

func (*MetadataStoreMysql) GetUtxosDeletedBeforeSlot

func (d *MetadataStoreMysql) GetUtxosDeletedBeforeSlot(
	slot uint64,
	limit int,
	txn types.Txn,
) ([]models.Utxo, error)

GetUtxosDeletedBeforeSlot returns a list of Utxos marked as deleted before a given slot

func (*MetadataStoreMysql) Order

func (d *MetadataStoreMysql) Order(args any) *gorm.DB

Order orders a DB query

func (*MetadataStoreMysql) RestoreAccountStateAtSlot

func (d *MetadataStoreMysql) RestoreAccountStateAtSlot(
	slot uint64,
	txn types.Txn,
) error

RestoreAccountStateAtSlot reverts account delegation state to the given slot. For accounts modified after the slot, this restores their Pool and Drep delegations to the state they had at the given slot, or marks them inactive if they were registered after that slot.

This implementation uses batch fetching to avoid N+1 query patterns: instead of querying certificates per-account, it fetches all relevant certificates for all affected accounts upfront (one query per table), then processes them in memory.

func (*MetadataStoreMysql) RestoreDrepStateAtSlot

func (d *MetadataStoreMysql) RestoreDrepStateAtSlot(
	slot uint64,
	txn types.Txn,
) error

RestoreDrepStateAtSlot reverts DRep state to the given slot. DReps that have no registrations at or before the given slot are deleted. DReps that have prior registrations have their Active status and anchor data restored based on the most recent certificate at or before the slot.

This implementation uses batch fetching to avoid N+1 query patterns: instead of querying certificates per-DRep, it fetches all relevant certificates for all affected DReps upfront (one query per table), then processes them in memory.

func (*MetadataStoreMysql) RestorePoolStateAtSlot

func (d *MetadataStoreMysql) RestorePoolStateAtSlot(
	slot uint64,
	txn types.Txn,
) error

RestorePoolStateAtSlot reverts pool state to the given slot. Pools that have no registrations at or before the given slot are deleted. Pools that have registrations at or before the given slot have their denormalized fields (pledge, cost, margin, etc.) restored from the most recent registration at or before the slot.

This implementation uses batch fetching to avoid N+1 query patterns: instead of querying certificates per-pool, it fetches all relevant registrations for all affected pools upfront in one query with a JOIN to the certs table to get cert_index for deterministic same-slot ordering.

func (*MetadataStoreMysql) SaveEpochSummary

func (d *MetadataStoreMysql) SaveEpochSummary(
	summary *models.EpochSummary,
	txn types.Txn,
) error

SaveEpochSummary saves an epoch summary

func (*MetadataStoreMysql) SavePoolStakeSnapshot

func (d *MetadataStoreMysql) SavePoolStakeSnapshot(
	snapshot *models.PoolStakeSnapshot,
	txn types.Txn,
) error

SavePoolStakeSnapshot saves a pool stake snapshot

func (*MetadataStoreMysql) SavePoolStakeSnapshots

func (d *MetadataStoreMysql) SavePoolStakeSnapshots(
	snapshots []*models.PoolStakeSnapshot,
	txn types.Txn,
) error

SavePoolStakeSnapshots saves multiple pool stake snapshots in batch

func (*MetadataStoreMysql) SetAccount

func (d *MetadataStoreMysql) SetAccount(
	stakeKey, pkh, drep []byte,
	slot uint64,
	active bool,
	txn types.Txn,
) error

SetAccount saves an account

func (*MetadataStoreMysql) SetBlockNonce

func (d *MetadataStoreMysql) SetBlockNonce(
	blockHash []byte,
	slotNumber uint64,
	nonce []byte,
	isCheckpoint bool,
	txn types.Txn,
) error

SetBlockNonce inserts a block nonce into the block_nonce table

func (*MetadataStoreMysql) SetCommitTimestamp

func (d *MetadataStoreMysql) SetCommitTimestamp(
	timestamp int64,
	txn types.Txn,
) error

func (*MetadataStoreMysql) SetDatum

func (d *MetadataStoreMysql) SetDatum(
	hash lcommon.Blake2b256,
	rawDatum []byte,
	addedSlot uint64,
	txn types.Txn,
) error

SetDatum saves a datum

func (*MetadataStoreMysql) SetDrep

func (d *MetadataStoreMysql) SetDrep(
	cred []byte,
	slot uint64,
	url string,
	hash []byte,
	active bool,
	txn types.Txn,
) error

SetDrep saves a drep

func (*MetadataStoreMysql) SetEpoch

func (d *MetadataStoreMysql) SetEpoch(
	slot, epoch uint64,
	nonce []byte,
	era, slotLength, lengthInSlots uint,
	txn types.Txn,
) error

SetEpoch saves an epoch

func (*MetadataStoreMysql) SetPParamUpdate

func (d *MetadataStoreMysql) SetPParamUpdate(
	genesis, update []byte,
	slot, epoch uint64,
	txn types.Txn,
) error

SetPParamUpdate saves a protocol parameter update

func (*MetadataStoreMysql) SetPParams

func (d *MetadataStoreMysql) SetPParams(
	params []byte,
	slot, epoch uint64,
	eraId uint,
	txn types.Txn,
) error

SetPParams saves protocol parameters

func (*MetadataStoreMysql) SetTip

func (d *MetadataStoreMysql) SetTip(
	tip ochainsync.Tip,
	txn types.Txn,
) error

SetTip saves a tip

func (*MetadataStoreMysql) SetTransaction

func (d *MetadataStoreMysql) SetTransaction(
	tx lcommon.Transaction,
	point ocommon.Point,
	idx uint32,
	certDeposits map[int]uint64,
	txn types.Txn,
) error

SetTransaction adds a new transaction to the database and processes all certificates

func (*MetadataStoreMysql) SetUtxoDeletedAtSlot

func (d *MetadataStoreMysql) SetUtxoDeletedAtSlot(
	utxoId ledger.TransactionInput,
	slot uint64,
	txn types.Txn,
) error

SetUtxoDeletedAtSlot marks a UTxO as deleted at a given slot

func (*MetadataStoreMysql) SetUtxosNotDeletedAfterSlot

func (d *MetadataStoreMysql) SetUtxosNotDeletedAfterSlot(
	slot uint64,
	txn types.Txn,
) error

SetUtxosNotDeletedAfterSlot marks a list of Utxos as not deleted after a given slot

func (*MetadataStoreMysql) Start

func (d *MetadataStoreMysql) Start() error

Start implements the plugin.Plugin interface

func (*MetadataStoreMysql) Stop

func (d *MetadataStoreMysql) Stop() error

Stop implements the plugin.Plugin interface

func (*MetadataStoreMysql) Transaction

func (d *MetadataStoreMysql) Transaction() types.Txn

Transaction creates a gorm transaction

func (*MetadataStoreMysql) Where

func (d *MetadataStoreMysql) Where(
	query any,
	args ...any,
) *gorm.DB

Where constrains a DB query

type MysqlOptionFunc

type MysqlOptionFunc func(*MetadataStoreMysql)

func WithDSN

func WithDSN(dsn string) MysqlOptionFunc

WithDSN specifies a full MySQL DSN string and takes precedence over individual connection options.

func WithDatabase

func WithDatabase(database string) MysqlOptionFunc

WithDatabase specifies the MySQL database name

func WithHost

func WithHost(host string) MysqlOptionFunc

WithHost specifies the MySQL host

func WithLogger

func WithLogger(logger *slog.Logger) MysqlOptionFunc

WithLogger specifies the logger object to use for logging messages

func WithPassword

func WithPassword(password string) MysqlOptionFunc

WithPassword specifies the MySQL password

func WithPort

func WithPort(port uint) MysqlOptionFunc

WithPort specifies the MySQL port

func WithPromRegistry

func WithPromRegistry(
	registry prometheus.Registerer,
) MysqlOptionFunc

WithPromRegistry specifies the prometheus registry to use for metrics

func WithSSLMode

func WithSSLMode(sslMode string) MysqlOptionFunc

WithSSLMode specifies the MySQL TLS option (mapped to tls= in the DSN)

func WithTimeZone

func WithTimeZone(timeZone string) MysqlOptionFunc

WithTimeZone specifies the MySQL time zone location

func WithUser

func WithUser(user string) MysqlOptionFunc

WithUser specifies the MySQL user

Jump to

Keyboard shortcuts

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