ledgerstate

package
v0.27.2 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2026 License: Apache-2.0 Imports: 30 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EraByron = iota
	EraShelley
	EraAllegra
	EraMary
	EraAlonzo
	EraBabbage
	EraConway
)

Era name constants for logging and error messages.

View Source
const (
	CredentialTypeKey          uint64 = 0 // Key hash credential
	CredentialTypeScript       uint64 = 1 // Script hash credential
	CredentialTypeAbstain      uint64 = 2 // AlwaysAbstain pseudo-DRep
	CredentialTypeNoConfidence uint64 = 3 // AlwaysNoConfidence pseudo-DRep
)

Credential type constants matching CBOR encoding. Types 0 and 1 are standard key/script credentials with a 28-byte hash. Types 2 and 3 are pseudo-DRep targets used only in DRep delegation (not valid as staking credentials); they carry no hash.

View Source
const MaxTelescopeDepth = 16

MaxTelescopeDepth caps recursion in the nested telescope traversal. Cardano has 7 eras (Byron through Conway) so 16 is generous while still preventing stack exhaustion from attacker-controlled CBOR.

Variables

View Source
var ErrLedgerDirNotFound = errors.New("ledger directory not found")

ErrLedgerDirNotFound is returned when the ledger directory cannot be located within an extracted snapshot.

Functions

func AggregatePoolStake

func AggregatePoolStake(
	snap *ParsedSnapShot,
	epoch uint64,
	snapshotType string,
	capturedSlot uint64,
) []*models.PoolStakeSnapshot

AggregatePoolStake aggregates per-credential stake into per-pool totals, producing PoolStakeSnapshot models suitable for database storage.

func EraName

func EraName(eraIndex int) string

EraName returns the human-readable name for an era index.

func FindLedgerStateFile

func FindLedgerStateFile(extractedDir string) (string, error)

FindLedgerStateFile searches the extracted snapshot directory for the ledger state file. It supports two formats:

  • Legacy: ledger/<slot>.lstate or ledger/<slot>
  • UTxO-HD: ledger/<slot>/state

Returns the path to the state file.

func FindUTxOTableFile

func FindUTxOTableFile(extractedDir string) string

FindUTxOTableFile searches for the UTxO table file in UTxO-HD format. Returns the path to tables/tvar if it exists, or empty string if not found (legacy format).

func ImportLedgerState

func ImportLedgerState(
	ctx context.Context,
	cfg ImportConfig,
) error

ImportLedgerState orchestrates the full import of parsed ledger state data into Dingo's metadata store. If ImportKey is set, completed phases are checkpointed so a failed import can resume from the last successful phase.

func ParseUTxOsFromFile

func ParseUTxOsFromFile(
	path string,
	callback UTxOCallback,
) (int, error)

ParseUTxOsFromFile reads and streams UTxOs from a UTxO-HD tvar file. The tvar format is array(1) containing a map of TxIn->TxOut entries. The map may use indefinite-length encoding (0xbf...0xff).

Note: The entire file is read into memory because the CBOR stream decoder (gouroboros) requires a contiguous byte slice and does not support io.Reader streaming. Mainnet tvar files can be multi-GB. A future optimization could use mmap to avoid copying the file contents into Go heap memory.

func ParseUTxOsStreaming

func ParseUTxOsStreaming(
	data cbor.RawMessage,
	callback UTxOCallback,
) (int, error)

ParseUTxOsStreaming decodes the UTxO map from raw CBOR data and calls the callback for each batch of parsed UTxOs. This avoids loading the entire UTxO set into memory. Both definite-length and indefinite-length (0xbf) CBOR maps are supported.

func UTxOToModel

func UTxOToModel(u *ParsedUTxO, slot uint64) models.Utxo

UTxOToModel converts a ParsedUTxO to a Dingo database Utxo model.

func VerifyChecksumFile

func VerifyChecksumFile(lstatePath string) error

VerifyChecksumFile verifies the CRC32 checksum of a ledger state file against its companion .checksum file.

func VerifySnapshotDigest

func VerifySnapshotDigest(
	archivePath string,
	expectedDigest string,
) error

VerifySnapshotDigest computes the SHA-256 digest of a snapshot archive file and compares it against the expected digest from the Mithril aggregator.

Types

type Credential

type Credential struct {
	Hash []byte
	Type uint64
}

Credential pairs a credential type with its hash. For types 0 (key) and 1 (script), Hash is a 28-byte Blake2b-224. For types 2 (AlwaysAbstain) and 3 (AlwaysNoConfidence), Hash is nil.

type EpochLengthFunc

type EpochLengthFunc func(eraId uint) (
	slotLength, epochLength uint, err error,
)

EpochLengthFunc resolves the slot length (ms) and epoch length (in slots) for a given era ID using the Cardano node config. Returns (0, 0, err) if the era is unknown or the config does not contain the needed genesis parameters.

type EraBound

type EraBound struct {
	Slot  uint64
	Epoch uint64
}

EraBound represents the start boundary of an era in the HardFork telescope. Slot is the first slot of the era and Epoch is the epoch number at that slot.

type ImportConfig

type ImportConfig struct {
	Database   *database.Database
	State      *RawLedgerState
	Logger     *slog.Logger
	OnProgress func(ImportProgress)
	// EpochLength resolves era parameters from the node config.
	// If nil, epoch generation falls back to computing from
	// era bounds.
	EpochLength EpochLengthFunc
	// ImportKey identifies this import for resume tracking.
	// Format: "{digest}:{slot}". If empty, resume is disabled.
	ImportKey string
}

ImportConfig holds configuration for the ledger state import.

type ImportProgress

type ImportProgress struct {
	Stage       string
	Current     int
	Total       int
	Percent     float64
	Description string
}

ImportProgress reports progress during ledger state import.

type MapEntry

type MapEntry struct {
	KeyRaw   []byte
	ValueRaw []byte
}

MapEntry holds a raw key-value pair from a CBOR map.

type ParsedAccount

type ParsedAccount struct {
	StakingKey  Credential // staking credential (type + hash)
	PoolKeyHash []byte     // 28-byte pool key hash (delegation target)
	DRepCred    Credential // DRep credential (vote delegation)
	Reward      uint64     // reward balance in lovelace
	Deposit     uint64     // deposit in lovelace
	Active      bool
}

ParsedAccount represents a stake account with its delegation.

type ParsedAsset

type ParsedAsset struct {
	PolicyId []byte // 28 bytes
	Name     []byte
	Amount   uint64
}

ParsedAsset represents a native asset within a UTxO.

type ParsedCertState

type ParsedCertState struct {
	Accounts []ParsedAccount
	Pools    []ParsedPool
	DReps    []ParsedDRep
}

ParsedCertState holds the parsed delegation, pool, and DRep state.

func ParseCertState

func ParseCertState(data cbor.RawMessage) (*ParsedCertState, error)

ParseCertState decodes the CertState from raw CBOR. CertState = [VState, PState, DState]

type ParsedCommitteeMember

type ParsedCommitteeMember struct {
	ColdCredential Credential // cold credential (type + hash)
	ExpiresEpoch   uint64
}

ParsedCommitteeMember holds a committee member credential and expiration.

type ParsedConstitution

type ParsedConstitution struct {
	AnchorURL    string
	AnchorHash   []byte // 32 bytes
	PolicyHash   []byte // 28 bytes, nil if no guardrails script
	ParseWarning error  // non-fatal warning from decoding
}

ParsedConstitution holds decoded constitution data.

type ParsedDRep

type ParsedDRep struct {
	Credential  Credential // credential (type + hash)
	AnchorURL   string
	AnchorHash  []byte
	Deposit     uint64
	ExpiryEpoch uint64 // epoch when this DRep expires (0 = unknown)
	Active      bool
}

ParsedDRep represents a DRep registration.

type ParsedGovProposal

type ParsedGovProposal struct {
	TxHash       []byte // 32 bytes
	ActionIndex  uint32
	ActionType   uint8
	Deposit      uint64
	ReturnAddr   []byte
	AnchorURL    string
	AnchorHash   []byte
	ProposedIn   uint64
	ExpiresAfter uint64
}

ParsedGovProposal holds a decoded governance proposal.

type ParsedGovState

type ParsedGovState struct {
	Constitution *ParsedConstitution
	Committee    []ParsedCommitteeMember
	Proposals    []ParsedGovProposal
}

ParsedGovState holds all decoded governance state components.

func ParseGovState

func ParseGovState(
	data cbor.RawMessage,
	eraIndex int,
) (*ParsedGovState, error)

ParseGovState decodes governance state from raw CBOR. Returns nil, nil for pre-Conway eras (eraIndex < 6).

Conway GovState structure (7 fields):

[proposals, committee, constitution,
 cur_pparams, prev_pparams, future_pparams,
 drep_pulsing_state]

Constitution, committee, and proposals are parsed. Errors from committee and proposals parsing are collected and returned alongside partial results so the caller can log them.

type ParsedPool

type ParsedPool struct {
	PoolKeyHash   []byte // 28 bytes
	VrfKeyHash    []byte // 32 bytes
	Pledge        uint64
	Cost          uint64
	MarginNum     uint64
	MarginDen     uint64
	RewardAccount []byte
	Owners        [][]byte // list of 28-byte key hashes
	Relays        []ParsedRelay
	MetadataUrl   string
	MetadataHash  []byte // 32 bytes
	Deposit       uint64
}

ParsedPool represents a stake pool registration.

type ParsedRelay

type ParsedRelay struct {
	Type     uint8  // 0=SingleHostAddr, 1=SingleHostName, 2=MultiHostName
	Port     uint16 // 0 if not specified
	IPv4     []byte // 4 bytes, nil if not specified
	IPv6     []byte // 16 bytes, nil if not specified
	Hostname string // for SingleHostName and MultiHostName
}

ParsedRelay represents a pool relay from the stake pool registration certificate.

type ParsedSnapShot

type ParsedSnapShot struct {
	// Stake maps credential-hex to staked lovelace.
	Stake map[string]uint64
	// Delegations maps credential-hex to pool key hash.
	Delegations map[string][]byte
	// PoolParams maps pool-hex to pool parameters.
	PoolParams map[string]*ParsedPool
}

ParsedSnapShot represents a single stake distribution snapshot.

type ParsedSnapShots

type ParsedSnapShots struct {
	Mark ParsedSnapShot
	Set  ParsedSnapShot
	Go   ParsedSnapShot
	Fee  uint64
}

ParsedSnapShots holds the three stake distribution snapshots (mark, set, go) from the ledger state.

func ParseSnapShots

func ParseSnapShots(data cbor.RawMessage) (*ParsedSnapShots, error)

ParseSnapShots decodes the stake distribution snapshots (mark, set, go) from the EpochState. SnapShots = [mark, set, go, fee] Each SnapShot = [Stake, Delegations, PoolParams]

type ParsedUTxO

type ParsedUTxO struct {
	TxHash      []byte // 32 bytes
	OutputIndex uint32
	Address     []byte // raw address bytes
	PaymentKey  []byte // 28 bytes, extracted from address
	StakingKey  []byte // 28 bytes, extracted from address
	Amount      uint64 // lovelace
	Assets      []ParsedAsset
	DatumHash   []byte // optional
	Datum       []byte // optional inline datum CBOR
	ScriptRef   []byte // optional reference script CBOR
}

ParsedUTxO represents a parsed unspent transaction output ready for conversion to Dingo's database model.

type RawLedgerState

type RawLedgerState struct {
	// EraIndex identifies the current era (0=Byron … 6=Conway).
	EraIndex int
	// Epoch is the epoch number at the time of the snapshot.
	Epoch uint64
	// Tip is the chain tip recorded in the snapshot.
	Tip *SnapshotTip
	// Treasury is the treasury balance in lovelace.
	Treasury uint64
	// Reserves is the reserves balance in lovelace.
	Reserves uint64
	// UTxOData is the deferred CBOR for the UTxO map.
	UTxOData cbor.RawMessage
	// CertStateData is the deferred CBOR for [VState, PState, DState].
	CertStateData cbor.RawMessage
	// GovStateData is the deferred CBOR for governance state.
	GovStateData cbor.RawMessage
	// PParamsData is the deferred CBOR for protocol parameters.
	PParamsData cbor.RawMessage
	// SnapShotsData is the deferred CBOR for mark/set/go stake snapshots.
	SnapShotsData cbor.RawMessage
	// EraBounds holds the start boundaries of all eras extracted
	// from the telescope. Each entry gives the (Slot, Epoch) at
	// which that era began. Used to generate the full epoch
	// history needed for SlotToTime/TimeToSlot.
	EraBounds []EraBound
	// EraBoundSlot is the slot number at which the current era
	// started, extracted from the telescope Bound.
	EraBoundSlot uint64
	// EraBoundEpoch is the epoch number at which the current era
	// started, extracted from the telescope Bound.
	EraBoundEpoch uint64
	// EpochNonce is the epoch nonce from the consensus state,
	// used for VRF leader election. It is a 32-byte Blake2b hash,
	// or nil if the nonce is neutral.
	EpochNonce []byte
	// EvolvingNonce is the rolling nonce (eta_v) from the consensus
	// state, updated with each block's VRF output. Stored as the
	// tip block nonce so that subsequent block processing computes
	// correct rolling nonces after a mithril snapshot restore.
	EvolvingNonce []byte
	// CandidateNonce is the Praos candidate nonce as of the imported
	// tip. This is required to correctly continue nonce accumulation
	// from snapshot tip to epoch boundary.
	CandidateNonce []byte
	// LastEpochBlockNonce is the lagged lab nonce from consensus
	// state (used in epoch nonce calculation).
	LastEpochBlockNonce []byte
	// EraBoundsWarning holds a non-fatal error from era bounds
	// extraction. When set, epoch generation falls back to
	// the single-epoch path.
	EraBoundsWarning error
	// UTxOTablePath is the path to the UTxO table file (UTxO-HD
	// format). When set, UTxOs are streamed from this file instead
	// of from UTxOData.
	UTxOTablePath string
}

RawLedgerState holds partially-decoded ledger state data from a Cardano node snapshot file. Large sections are kept as cbor.RawMessage for deferred, streaming decoding.

func ParseSnapshot

func ParseSnapshot(path string) (*RawLedgerState, error)

ParseSnapshot reads and partially decodes a Cardano node ledger state snapshot file. The UTxO map, cert state, and stake snapshots are kept as raw CBOR for streaming decode later.

Note: The entire file is read into memory because the CBOR parsing pipeline (decodeRawArray, cbor.Decode) requires a contiguous byte slice and does not support io.Reader streaming. For legacy-format mainnet snapshots the embedded UTxO map can be hundreds of MB. A future optimization could use mmap (syscall.Mmap) to avoid copying the file contents into Go heap memory, which would keep the OS page cache as the backing store rather than allocating a separate heap buffer.

type SnapshotTip

type SnapshotTip struct {
	Slot      uint64
	BlockHash []byte
}

SnapshotTip represents the chain tip recorded in the ledger state snapshot.

type UTxOCallback

type UTxOCallback func(batch []ParsedUTxO) error

UTxOCallback is called for each batch of parsed UTxOs during streaming decode.

type UTxOParseProgress added in v0.24.0

type UTxOParseProgress struct {
	EntriesProcessed int
	TotalEntries     int
	BytesProcessed   int
	TotalBytes       int
	Percent          float64
}

Jump to

Keyboard shortcuts

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