server

package
v0.65.1 Latest Latest
Warning

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

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

Documentation

Overview

Package server runs the MidnightState gRPC service. It is a native google.golang.org/grpc server (not ConnectRPC) so that clients written against the Acropolis tonic service are byte-for-byte compatible. The UTxO-event query RPCs (GetAssetCreates, GetAssetSpends, GetRegistrations, GetDeregistrations, GetUtxoEvents) are backed by Config.Metadata (see midnight_state.go). The governance/parameters/block/epoch/stability RPCs are backed by Config.Database and Config.SlotTimer (see service.go). Any remaining RPC falls back to UnimplementedMidnightStateServer.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Logger *slog.Logger
	// Metadata is the store backing the MidnightState query RPCs
	// (GetAssetCreates, GetAssetSpends, GetRegistrations,
	// GetDeregistrations, GetUtxoEvents). Nil leaves those RPCs
	// unimplemented. Any metadata.MetadataStore implementation satisfies
	// this narrower interface structurally.
	Metadata eventStore
	// BlockNumberByHash resolves a Cardano block hash to its block number.
	// It backs GetUtxoEvents' end_block_hash boundary so that boundary is
	// honored even when the target block has no Midnight events of its
	// own (found=false for an unknown hash, not an error). Nil causes
	// GetUtxoEvents to fail requests that set end_block_hash.
	BlockNumberByHash func(hash []byte) (blockNumber uint64, found bool, err error)
	// Host and Port are the gRPC listen address. Defaults to 0.0.0.0:50051.
	Host string
	Port uint
	// TLSCertFilePath and TLSKeyFilePath enable TLS when both are set. When
	// both are empty the server listens without TLS. Setting only one is an
	// error.
	TLSCertFilePath string
	TLSKeyFilePath  string
	// ShutdownTimeout bounds GracefulStop before escalating to a hard Stop.
	// Defaults to 30s.
	ShutdownTimeout time.Duration
	// Database backs the governance/parameters/block/epoch/stability RPCs.
	// Wrap a *database.Database with NewDatabase. Required for those RPCs to
	// work; RPCs that only need Database are unaffected by a nil SlotTimer.
	Database MidnightDatabase
	// SlotTimer resolves block timestamps and, for the as-of-timestamp
	// variants of the stability RPCs, converts a wall-clock time back to a
	// slot. Required by GetBlockByHash, GetLatestBlock, GetStableBlock, and
	// GetLatestStableBlock.
	SlotTimer SlotTimer
}

Config holds the configuration for the Midnight gRPC server.

type MidnightDatabase added in v0.63.0

type MidnightDatabase interface {
	GetLatestMidnightGovernanceDatum(
		datumType string,
		blockNumber uint64,
	) (*models.MidnightGovernanceDatum, error)
	GetMidnightAriadneParamsAtOrBeforeEpoch(
		epoch uint64,
	) (*models.MidnightAriadneParams, error)
	GetMidnightEpochCandidatesByEpoch(
		epoch uint64,
	) (*models.MidnightEpochCandidates, error)
	// GetMidnightCommitteeCandidateRegistrationsByTxHashes returns the
	// on-chain provenance (block/slot/tx-index/tx-inputs) for candidate
	// UTxOs whose creating tx hash is in txHashes, for GetEpochCandidates to
	// join against the (tx_hash, output_index, datum) membership decoded
	// from a MidnightEpochCandidates snapshot.
	GetMidnightCommitteeCandidateRegistrationsByTxHashes(
		txHashes [][]byte,
	) ([]models.MidnightCommitteeCandidateRegistration, error)
	GetPoolStakeSnapshotsByEpoch(
		epoch uint64,
		snapshotType string,
	) ([]*models.PoolStakeSnapshot, error)
	GetEpoch(epochId uint64) (*models.Epoch, error)
	GetEpochBySlot(slot uint64) (*models.Epoch, error)
	BlockByHash(hash []byte) (models.Block, error)
	// BlockByNumber returns the block at the given 0-based consensus block
	// number, translating to the blob store's internal 1-based index.
	BlockByNumber(number uint64) (models.Block, error)
	BlocksRecent(count int) ([]models.Block, error)
	BlockBeforeSlot(slot uint64) (models.Block, error)
}

MidnightDatabase is the subset of *database.Database needed by the governance/parameters/block/epoch/stability RPCs. Keeping this package dependent on a narrow interface (rather than *database.Database directly) matches the composition boundary documented in ARCHITECTURE.md: domain packages depend on constructor-injected narrow interfaces, and only the composition layer (here, adapter.go's NewDatabase) wires the concrete type. All methods implicitly use a nil transaction (auto-transaction per call), mirroring api/mesh's MeshDatabase.

func NewDatabase added in v0.63.0

func NewDatabase(db *database.Database) MidnightDatabase

NewDatabase wraps a *database.Database as a MidnightDatabase for use in the Midnight gRPC server config.

type Server

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

Server runs the MidnightState gRPC service over its own listener.

func New

func New(cfg Config) (*Server, error)

New validates cfg and returns a Server. It does not bind or serve; call Start for that.

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

Start binds the listener and serves the gRPC server in a background goroutine. Binding and any TLS keypair load happen synchronously so that address-in-use and certificate errors surface before Start returns. A goroutine watches ctx and performs a bounded graceful shutdown when it is cancelled.

func (*Server) Stop

func (s *Server) Stop(ctx context.Context) error

Stop gracefully shuts the server down, bounded by ctx's deadline when set and otherwise by the configured shutdown timeout. It is safe to call repeatedly and concurrently with the context-cancellation path.

type SlotTimer added in v0.63.0

type SlotTimer interface {
	SlotToTime(slot uint64) (time.Time, error)
	TimeToSlot(t time.Time) (uint64, error)
}

SlotTimer converts between slot numbers and wall-clock time. Satisfied by *ledger.LedgerState; kept as a narrow local interface so this package does not need to import the full ledger package.

Jump to

Keyboard shortcuts

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