mysql

package
v0.22.1 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: Apache-2.0 Imports: 28 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) ClearSyncState added in v0.22.0

func (d *MetadataStoreMysql) ClearSyncState(
	txn types.Txn,
) error

ClearSyncState removes all sync state entries.

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) DeleteAddressTransactionsAfterSlot added in v0.22.0

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

DeleteAddressTransactionsAfterSlot removes address-transaction mapping records added after the given slot.

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) DeleteCommitteeMembersAfterSlot added in v0.22.0

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

DeleteCommitteeMembersAfterSlot removes committee members added after the given slot and clears deleted_slot for any that were soft-deleted after that slot.

func (*MetadataStoreMysql) DeleteConstitutionsAfterSlot added in v0.22.0

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

DeleteConstitutionsAfterSlot removes constitutions added after the given slot and clears deleted_slot for any that were soft-deleted after that slot. This is used during chain rollbacks.

func (*MetadataStoreMysql) DeleteEpochSummariesAfterEpoch added in v0.22.0

func (d *MetadataStoreMysql) DeleteEpochSummariesAfterEpoch(
	epoch uint64,
	txn types.Txn,
) error

DeleteEpochSummariesAfterEpoch deletes all epoch summaries after a given epoch. This is used during chain rollbacks.

func (*MetadataStoreMysql) DeleteEpochSummariesBeforeEpoch added in v0.22.0

func (d *MetadataStoreMysql) DeleteEpochSummariesBeforeEpoch(
	epoch uint64,
	txn types.Txn,
) error

DeleteEpochSummariesBeforeEpoch deletes all epoch summaries before a given epoch.

func (*MetadataStoreMysql) DeleteEpochsAfterSlot added in v0.22.0

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

DeleteEpochsAfterSlot removes all epoch entries whose start slot is after the given slot.

func (*MetadataStoreMysql) DeleteGovernanceProposalsAfterSlot added in v0.22.0

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

DeleteGovernanceProposalsAfterSlot removes proposals added after the given slot, clears deleted_slot for any that were soft-deleted after that slot, and reverts status changes (ratified/enacted) that occurred after the rollback slot. This is used during chain rollbacks.

func (*MetadataStoreMysql) DeleteGovernanceVotesAfterSlot added in v0.22.0

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

DeleteGovernanceVotesAfterSlot removes votes added after the given slot and clears deleted_slot for any that were soft-deleted after that slot. This is used during chain rollbacks.

NOTE: Vote changes (when a voter changes their vote) are tracked via vote_updated_slot. If a vote was changed after the rollback slot but originally cast before it, we cannot restore the previous vote value with the current upsert model. For full rollback support of vote changes, votes should be append-only. Currently, we delete any vote whose update occurred after the rollback slot to avoid having stale vote values persist.

func (*MetadataStoreMysql) DeleteNetworkStateAfterSlot added in v0.22.0

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

DeleteNetworkStateAfterSlot removes network state records added after the given slot.

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) DeletePoolStakeSnapshotsAfterEpoch added in v0.22.0

func (d *MetadataStoreMysql) DeletePoolStakeSnapshotsAfterEpoch(
	epoch uint64,
	txn types.Txn,
) error

DeletePoolStakeSnapshotsAfterEpoch deletes all pool stake snapshots after a given epoch. This is used during chain rollbacks.

func (*MetadataStoreMysql) DeletePoolStakeSnapshotsBeforeEpoch added in v0.22.0

func (d *MetadataStoreMysql) DeletePoolStakeSnapshotsBeforeEpoch(
	epoch uint64,
	txn types.Txn,
) error

DeletePoolStakeSnapshotsBeforeEpoch deletes all pool stake snapshots before a given epoch.

func (*MetadataStoreMysql) DeletePoolStakeSnapshotsForEpoch added in v0.22.0

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

DeletePoolStakeSnapshotsForEpoch deletes all pool stake snapshots for a specific epoch

func (*MetadataStoreMysql) DeleteSyncState added in v0.22.0

func (d *MetadataStoreMysql) DeleteSyncState(
	key string,
	txn types.Txn,
) error

DeleteSyncState removes a sync state key.

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) GetActiveCommitteeMembers added in v0.22.0

func (d *MetadataStoreMysql) GetActiveCommitteeMembers(
	txn types.Txn,
) ([]*models.AuthCommitteeHot, error)

GetActiveCommitteeMembers retrieves all active committee members. Returns only the latest authorization per cold key, and excludes members whose latest resignation is after their latest authorization.

func (*MetadataStoreMysql) GetActiveDreps added in v0.22.0

func (d *MetadataStoreMysql) GetActiveDreps(
	txn types.Txn,
) ([]*models.Drep, error)

GetActiveDreps retrieves all active DReps.

func (*MetadataStoreMysql) GetActiveGovernanceProposals added in v0.22.0

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

GetActiveGovernanceProposals retrieves all governance proposals that haven't expired.

func (*MetadataStoreMysql) GetActivePoolKeyHashes added in v0.22.0

func (d *MetadataStoreMysql) GetActivePoolKeyHashes(
	txn types.Txn,
) ([][]byte, error)

GetActivePoolKeyHashes retrieves the key hashes of all currently active pools. A pool is active if it has a registration and either no retirement or the retirement epoch is in the future.

This delegates to GetActivePoolKeyHashesAtSlot using the current tip's slot, ensuring consistent same-slot certificate handling via block_index and cert_index ordering (ORDER BY added_slot DESC, block_index DESC, cert_index DESC).

func (*MetadataStoreMysql) GetActivePoolKeyHashesAtSlot added in v0.22.0

func (d *MetadataStoreMysql) GetActivePoolKeyHashesAtSlot(
	slot uint64,
	txn types.Txn,
) ([][]byte, error)

GetActivePoolKeyHashesAtSlot retrieves the key hashes of pools that were active at the given slot. A pool was active at a slot if:

  1. It had a registration with added_slot <= slot
  2. Either no retirement with added_slot <= slot, OR the retirement was for an epoch that hadn't started by the given slot

This implementation uses window functions (ROW_NUMBER) to fetch only the latest registration and retirement per pool directly in the database, avoiding loading all pools and their certificates into memory. Properly handles same-slot certificates by comparing block_index and cert_index.

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

Same-slot certificates are disambiguated by fetching cert_index from the certs table when needed.

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) GetAddressesByStakingKey added in v0.22.0

func (d *MetadataStoreMysql) GetAddressesByStakingKey(
	stakingKey []byte,
	limit int,
	offset int,
	txn types.Txn,
) ([]models.AddressTransaction, error)

GetAddressesByStakingKey returns distinct addresses mapped to a staking key.

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) GetBackfillCheckpoint added in v0.22.0

func (d *MetadataStoreMysql) GetBackfillCheckpoint(
	phase string,
	txn types.Txn,
) (*models.BackfillCheckpoint, error)

GetBackfillCheckpoint retrieves a backfill checkpoint by phase. Returns nil (not error) if no checkpoint exists for the phase.

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) GetBlockNoncesInSlotRange added in v0.22.0

func (d *MetadataStoreMysql) GetBlockNoncesInSlotRange(
	startSlot uint64,
	endSlot uint64,
	txn types.Txn,
) ([]models.BlockNonce, error)

GetBlockNoncesInSlotRange retrieves all block nonces where slot >= startSlot and slot < endSlot.

func (*MetadataStoreMysql) GetCommitTimestamp

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

func (*MetadataStoreMysql) GetCommitteeActiveCount added in v0.22.0

func (d *MetadataStoreMysql) GetCommitteeActiveCount(
	txn types.Txn,
) (int, error)

GetCommitteeActiveCount returns the number of active (non-resigned) committee members.

func (*MetadataStoreMysql) GetCommitteeMember added in v0.22.0

func (d *MetadataStoreMysql) GetCommitteeMember(
	coldKey []byte,
	txn types.Txn,
) (*models.AuthCommitteeHot, error)

GetCommitteeMember retrieves a committee member by cold key. Returns the latest authorization (ordered by added_slot DESC).

func (*MetadataStoreMysql) GetCommitteeMembers added in v0.22.0

func (d *MetadataStoreMysql) GetCommitteeMembers(
	txn types.Txn,
) ([]*models.CommitteeMember, error)

GetCommitteeMembers retrieves all active (non-deleted) snapshot-imported committee members.

func (*MetadataStoreMysql) GetConstitution added in v0.22.0

func (d *MetadataStoreMysql) GetConstitution(
	txn types.Txn,
) (*models.Constitution, error)

GetConstitution retrieves the current constitution. Returns nil if the constitution has been soft-deleted.

func (*MetadataStoreMysql) GetDRepVotingPower added in v0.22.0

func (d *MetadataStoreMysql) GetDRepVotingPower(
	drepCredential []byte,
	txn types.Txn,
) (uint64, error)

GetDRepVotingPower calculates the voting power for a DRep by summing the stake of all accounts delegated to it. Uses the current live UTxO set (deleted_slot = 0) for the calculation.

TODO: This implementation uses the current live UTxO set as an approximation. A future implementation should accept an epoch parameter and use epoch-based stake snapshots for accurate voting power at a specific point in time.

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) GetEpoch added in v0.22.0

func (d *MetadataStoreMysql) GetEpoch(
	epochId uint64,
	txn types.Txn,
) (*models.Epoch, error)

GetEpoch returns a single epoch by its ID, or nil if not found.

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) GetExpiredDReps added in v0.22.0

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

GetExpiredDReps retrieves all active DReps whose expiry epoch is at or before the given epoch.

func (*MetadataStoreMysql) GetGovernanceProposal added in v0.22.0

func (d *MetadataStoreMysql) GetGovernanceProposal(
	txHash []byte,
	actionIndex uint32,
	txn types.Txn,
) (*models.GovernanceProposal, error)

GetGovernanceProposal retrieves a governance proposal by transaction hash and action index. Returns nil if the proposal has been soft-deleted.

func (*MetadataStoreMysql) GetGovernanceVotes added in v0.22.0

func (d *MetadataStoreMysql) GetGovernanceVotes(
	proposalID uint,
	txn types.Txn,
) ([]*models.GovernanceVote, error)

GetGovernanceVotes retrieves all votes for a governance proposal.

func (*MetadataStoreMysql) GetImportCheckpoint added in v0.22.0

func (d *MetadataStoreMysql) GetImportCheckpoint(
	importKey string,
	txn types.Txn,
) (*models.ImportCheckpoint, error)

GetImportCheckpoint retrieves the checkpoint for a given import key. Returns nil if no checkpoint exists.

func (*MetadataStoreMysql) GetLatestEpochSummary

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

GetLatestEpochSummary retrieves the most recent epoch summary

func (*MetadataStoreMysql) GetNetworkState added in v0.22.0

func (d *MetadataStoreMysql) GetNetworkState(
	txn types.Txn,
) (*models.NetworkState, error)

GetNetworkState retrieves the most recent network state.

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) GetPoolByVrfKeyHash added in v0.22.0

func (d *MetadataStoreMysql) GetPoolByVrfKeyHash(
	vrfKeyHash []byte,
	txn types.Txn,
) (*models.Pool, error)

GetPoolByVrfKeyHash retrieves an active pool by its VRF key hash. Returns nil if no active pool uses this VRF key.

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) GetStakeByPool added in v0.22.0

func (d *MetadataStoreMysql) GetStakeByPool(
	poolKeyHash []byte,
	txn types.Txn,
) (uint64, uint64, error)

GetStakeByPool returns the total delegated stake and delegator count for a pool.

func (*MetadataStoreMysql) GetStakeByPools added in v0.22.0

func (d *MetadataStoreMysql) GetStakeByPools(
	poolKeyHashes [][]byte,
	txn types.Txn,
) (map[string]uint64, map[string]uint64, error)

GetStakeByPools returns delegated stake for multiple pools in a single query. Stake is computed by joining active accounts with their live UTxOs (deleted_slot = 0) and summing the UTxO amounts per pool.

func (*MetadataStoreMysql) GetStakeRegistrations

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

GetStakeRegistrations returns stake registration certificates

func (*MetadataStoreMysql) GetSyncState added in v0.22.0

func (d *MetadataStoreMysql) GetSyncState(
	key string,
	txn types.Txn,
) (string, error)

GetSyncState retrieves a sync state value by key.

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 total active stake for an epoch. It uses the pre-calculated total from EpochSummary when available, falling back to summing individual pool snapshots.

func (*MetadataStoreMysql) GetTransactionByHash

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

GetTransactionByHash returns a transaction by its hash

func (*MetadataStoreMysql) GetTransactionHashesAfterSlot added in v0.22.0

func (d *MetadataStoreMysql) GetTransactionHashesAfterSlot(
	slot uint64,
	txn types.Txn,
) ([][]byte, error)

GetTransactionHashesAfterSlot returns transaction hashes for transactions added after the given slot. This is used for blob cleanup during rollback/truncation.

func (*MetadataStoreMysql) GetTransactionsByAddress added in v0.22.0

func (d *MetadataStoreMysql) GetTransactionsByAddress(
	paymentKey []byte,
	stakingKey []byte,
	limit int,
	offset int,
	order string,
	txn types.Txn,
) ([]models.Transaction, error)

GetTransactionsByAddress returns transactions that involve the given payment/staking key with pagination support.

func (*MetadataStoreMysql) GetTransactionsByBlockHash added in v0.22.0

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

GetTransactionsByBlockHash returns all transactions in a block, ordered by index

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) GetUtxoIncludingSpent added in v0.22.0

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

GetUtxoIncludingSpent returns a Utxo by reference, including spent UTxOs

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) GetUtxosByAddressAtSlot added in v0.22.0

func (d *MetadataStoreMysql) GetUtxosByAddressAtSlot(
	addr lcommon.Address,
	slot uint64,
	txn types.Txn,
) ([]models.Utxo, error)

GetUtxosByAddressAtSlot returns UTxOs for an address that existed at a specific slot.

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) ImportAccount added in v0.22.0

func (d *MetadataStoreMysql) ImportAccount(
	account *models.Account,
	txn types.Txn,
) error

ImportAccount upserts an account (insert or update delegation fields on conflict).

func (*MetadataStoreMysql) ImportDrep added in v0.22.0

func (d *MetadataStoreMysql) ImportDrep(
	drep *models.Drep,
	reg *models.RegistrationDrep,
	txn types.Txn,
) error

ImportDrep upserts a DRep and creates a registration record.

func (*MetadataStoreMysql) ImportPool added in v0.22.0

func (d *MetadataStoreMysql) ImportPool(
	pool *models.Pool,
	reg *models.PoolRegistration,
	txn types.Txn,
) error

ImportPool upserts a pool and creates a registration record.

func (*MetadataStoreMysql) ImportUtxos added in v0.22.0

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

ImportUtxos inserts UTxOs in bulk, ignoring duplicates. Assets are inserted in a second pass to avoid cascading the associated Assets into the same bulk INSERT, which can push the packet size over MySQL limits.

func (*MetadataStoreMysql) IsCommitteeMemberResigned added in v0.22.0

func (d *MetadataStoreMysql) IsCommitteeMemberResigned(
	coldKey []byte,
	txn types.Txn,
) (bool, error)

IsCommitteeMemberResigned checks if a committee member has resigned. Returns true only if the latest resignation is after the latest authorization (handles resign-then-rejoin scenarios). Uses (added_slot, certificate_id) for deterministic ordering based on global certificate order.

func (*MetadataStoreMysql) Order

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

Order orders a DB query

func (*MetadataStoreMysql) ReadTransaction added in v0.22.0

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

ReadTransaction creates a read-only transaction.

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) RestoreNormalPragmas added in v0.22.0

func (d *MetadataStoreMysql) RestoreNormalPragmas() error

RestoreNormalPragmas restores MySQL session settings to defaults and restores the connection pool size.

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) SetBackfillCheckpoint added in v0.22.0

func (d *MetadataStoreMysql) SetBackfillCheckpoint(
	checkpoint *models.BackfillCheckpoint,
	txn types.Txn,
) error

SetBackfillCheckpoint creates or updates a backfill checkpoint, upserting on the Phase column.

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) SetBulkLoadPragmas added in v0.22.0

func (d *MetadataStoreMysql) SetBulkLoadPragmas() error

SetBulkLoadPragmas configures MySQL session settings for high-throughput bulk inserts. It pins the connection pool to a single connection so SET statements apply to all queries.

func (*MetadataStoreMysql) SetCommitTimestamp

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

func (*MetadataStoreMysql) SetCommitteeMembers added in v0.22.0

func (d *MetadataStoreMysql) SetCommitteeMembers(
	members []*models.CommitteeMember,
	txn types.Txn,
) error

SetCommitteeMembers upserts committee members imported from a Mithril snapshot.

func (*MetadataStoreMysql) SetConstitution added in v0.22.0

func (d *MetadataStoreMysql) SetConstitution(
	constitution *models.Constitution,
	txn types.Txn,
) error

SetConstitution sets the constitution.

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, evolvingNonce, candidateNonce, lastEpochBlockNonce []byte,
	era, slotLength, lengthInSlots uint,
	txn types.Txn,
) error

SetEpoch saves an epoch

func (*MetadataStoreMysql) SetGapBlockTransaction added in v0.22.0

func (d *MetadataStoreMysql) SetGapBlockTransaction(
	tx lcommon.Transaction,
	point ocommon.Point,
	idx uint32,
	txn types.Txn,
) error

SetGapBlockTransaction stores a transaction record and its produced outputs without looking up or consuming input UTxOs. Gap blocks from mithril sync have their UTxO state already reflected in the snapshot, so input processing must be skipped entirely.

func (*MetadataStoreMysql) SetGenesisStaking added in v0.22.0

func (d *MetadataStoreMysql) SetGenesisStaking(
	_ map[string]lcommon.PoolRegistrationCertificate,
	_ map[string]string,
	_ []byte,
	_ types.Txn,
) error

SetGenesisStaking is not implemented for the MySQL metadata plugin.

func (*MetadataStoreMysql) SetGenesisTransaction added in v0.22.0

func (d *MetadataStoreMysql) SetGenesisTransaction(
	hash []byte,
	blockHash []byte,
	outputs []models.Utxo,
	txn types.Txn,
) error

SetGenesisTransaction stores a genesis transaction record. Genesis transactions have no inputs, witnesses, or fees - just outputs.

func (*MetadataStoreMysql) SetGovernanceProposal added in v0.22.0

func (d *MetadataStoreMysql) SetGovernanceProposal(
	proposal *models.GovernanceProposal,
	txn types.Txn,
) error

SetGovernanceProposal creates or updates a governance proposal.

func (*MetadataStoreMysql) SetGovernanceVote added in v0.22.0

func (d *MetadataStoreMysql) SetGovernanceVote(
	vote *models.GovernanceVote,
	txn types.Txn,
) error

SetGovernanceVote records a vote on a governance proposal.

func (*MetadataStoreMysql) SetImportCheckpoint added in v0.22.0

func (d *MetadataStoreMysql) SetImportCheckpoint(
	checkpoint *models.ImportCheckpoint,
	txn types.Txn,
) error

SetImportCheckpoint creates or updates a checkpoint.

func (*MetadataStoreMysql) SetNetworkState added in v0.22.0

func (d *MetadataStoreMysql) SetNetworkState(
	treasury, reserves uint64,
	slot uint64,
	txn types.Txn,
) error

SetNetworkState stores the treasury and reserves balances.

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) SetSyncState added in v0.22.0

func (d *MetadataStoreMysql) SetSyncState(
	key, value string,
	txn types.Txn,
) error

SetSyncState stores or updates a sync state value.

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) UpdateDRepActivity added in v0.22.0

func (d *MetadataStoreMysql) UpdateDRepActivity(
	drepCredential []byte,
	activityEpoch uint64,
	inactivityPeriod uint64,
	txn types.Txn,
) error

UpdateDRepActivity updates the DRep's last activity epoch and recalculates the expiry epoch. Returns ErrDrepActivityNotUpdated if no matching DRep record was found.

MySQL's RowsAffected returns changed rows (not matched rows) by default. If a DRep votes twice in the same epoch, the second update sets identical values and RowsAffected returns 0. To distinguish "not found" from "no change", we add a WHERE clause that checks the current values differ, then fall back to an existence check when RowsAffected is 0.

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

func WithStorageMode(mode string) MysqlOptionFunc

WithStorageMode specifies the storage tier. "core" = consensus only, "api" = full tx metadata.

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