store

package
v0.0.0-...-54d5f2e Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const SqliteInMemory = ":memory:"

SqliteInMemory tells sqlite to use a fully in-memory database Useful for testing and development.

Variables

View Source
var (
	ErrNotFound = errors.New("not found")
)

Repository errors

Functions

func CreateMigration

func CreateMigration(name, migrationsDir string) (string, error)

func DefaultSqliteConnOptions

func DefaultSqliteConnOptions() map[string]string

func MigrationsSource

func MigrationsSource(dbType string) (migrate.MigrationSource, error)

func ValidateConfigLive

func ValidateConfigLive(cfg config.Config) error

ValidateConfigLive assumes the config.Config is valid, and checks if the database is reachable.

Types

type MigrationStatus

type MigrationStatus struct {
	ID        string     `json:"id"`
	Applied   bool       `json:"applied"`
	AppliedAt *time.Time `json:"appliedAt,omitempty"`
}

MigrationStatus represents migration entry in the database.

type Migrator

type Migrator interface {
	MigrateUp() (int, error)
	MigrateDown() (int, error)
	MigrationStatus() ([]MigrationStatus, error)
}

Migrator abstracts schema migrations

type Packet

type Packet struct {
	ID        int64
	CreatedAt time.Time
	UpdatedAt time.Time

	Status RelayStatus

	SourceChainID      string
	DestinationChainID string
	SourceTxHash       string
	SourceTxTime       time.Time

	PacketSequenceNumber      uint64
	PacketSourceClientID      string
	PacketDestinationClientID string
	PacketTimeoutTimestamp    time.Time

	RecvTxHash           *string
	RecvTxTime           *time.Time
	RecvTxRelayerAddress *string

	WriteAckTxHash *string
	WriteAckTxTime *time.Time
	WriteAckStatus *WriteAckStatus

	AckTxHash           *string
	AckTxTime           *time.Time
	AckTxRelayerAddress *string

	TimeoutTxHash           *string
	TimeoutTxTime           *time.Time
	TimeoutTxRelayerAddress *string
}

Packet a packet tracked through its relay lifecycle.

type PacketFilter

type PacketFilter struct {
	Statuses            []RelayStatus
	SourceChainID       string
	DestinationChainID  string
	SourceClientID      string
	DestinationClientID string
	SourceTxHash        string
	SequenceNumber      uint64
}

PacketFilter narrows a ListPackets query.

type PacketKey

type PacketKey struct {
	SourceChainID  string
	SourceClientID string
	Sequence       uint64
}

PacketKey uniquely identifies a packet.

type PacketTx

type PacketTx struct {
	Hash           string
	Time           time.Time
	RelayerAddress string
}

PacketTx a relay transaction recorded on a packet.

type Page

type Page struct {
	Limit  int64
	Before int64
}

Page bounds a ListPackets result. Before is an exclusive upper bound on packet id

type PostgresDB

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

PostgresDB is a wrapper around the postgres database.

func NewPostgres

func NewPostgres(ctx context.Context, url string) (*PostgresDB, error)

NewPostgres creates a new PostgresDB instance with pgx connection pool. Context must be long-lived. URL example: "postgres://username:password@localhost:5432/database_name"

func NewPostgresWithConfig

func NewPostgresWithConfig(ctx context.Context, config *pgxpool.Config, ping bool) (*PostgresDB, error)

NewPostgresWithConfig creates a new PostgresDB instance based on pgxpool.Config Allows to modify the config before creation.

func (*PostgresDB) ClearPacketAckTx

func (db *PostgresDB) ClearPacketAckTx(ctx context.Context, key PacketKey) error

func (*PostgresDB) ClearPacketRecvTx

func (db *PostgresDB) ClearPacketRecvTx(ctx context.Context, key PacketKey) error

func (*PostgresDB) ClearPacketTimeoutTx

func (db *PostgresDB) ClearPacketTimeoutTx(ctx context.Context, key PacketKey) error

func (*PostgresDB) Close

func (db *PostgresDB) Close() error

func (*PostgresDB) ListDispatchablePackets

func (db *PostgresDB) ListDispatchablePackets(ctx context.Context) ([]Packet, error)

func (*PostgresDB) ListPackets

func (db *PostgresDB) ListPackets(
	ctx context.Context,
	filter PacketFilter,
	page Page,
) ([]Packet, error)

func (*PostgresDB) ListPacketsBySourceTx

func (db *PostgresDB) ListPacketsBySourceTx(
	ctx context.Context,
	chainID string,
	txHash string,
) ([]Packet, error)

func (*PostgresDB) MigrateDown

func (db *PostgresDB) MigrateDown() (int, error)

MigrateDown migrates only ONE migration down

func (*PostgresDB) MigrateUp

func (db *PostgresDB) MigrateUp() (int, error)

MigrateUp migrates ALL available migrations

func (*PostgresDB) MigrationStatus

func (db *PostgresDB) MigrationStatus() ([]MigrationStatus, error)

func (*PostgresDB) Ping

func (db *PostgresDB) Ping(ctx context.Context) error

func (*PostgresDB) Transact

func (db *PostgresDB) Transact(ctx context.Context, call func(repo Repository) error) error

func (*PostgresDB) UpdatePacketAckTx

func (db *PostgresDB) UpdatePacketAckTx(ctx context.Context, key PacketKey, tx PacketTx) error

func (*PostgresDB) UpdatePacketRecvTx

func (db *PostgresDB) UpdatePacketRecvTx(ctx context.Context, key PacketKey, tx PacketTx) error

func (*PostgresDB) UpdatePacketStatus

func (db *PostgresDB) UpdatePacketStatus(ctx context.Context, key PacketKey, status RelayStatus) error

func (*PostgresDB) UpdatePacketTimeoutTx

func (db *PostgresDB) UpdatePacketTimeoutTx(ctx context.Context, key PacketKey, tx PacketTx) error

func (*PostgresDB) UpdatePacketWriteAck

func (db *PostgresDB) UpdatePacketWriteAck(ctx context.Context, key PacketKey, ack WriteAck) error

func (*PostgresDB) UpsertPacket

func (db *PostgresDB) UpsertPacket(ctx context.Context, input UpsertPacket) error

type RelayStatus

type RelayStatus string

RelayStatus the relay state of a packet.

const (
	RelayStatusNotSelected                RelayStatus = "NOT_SELECTED"
	RelayStatusPending                    RelayStatus = "PENDING"
	RelayStatusAwaitingSendFinality       RelayStatus = "AWAITING_SEND_FINALITY"
	RelayStatusCheckRecvPacketDelivery    RelayStatus = "CHECK_RECV_PACKET_DELIVERY"
	RelayStatusGetRecvPacket              RelayStatus = "GET_RECV_PACKET"
	RelayStatusDeliverRecvPacket          RelayStatus = "DELIVER_RECV_PACKET"
	RelayStatusWaitForWriteAck            RelayStatus = "WAIT_FOR_WRITE_ACK"
	RelayStatusAwaitingWriteAckFinality   RelayStatus = "AWAITING_WRITE_ACK_FINALITY"
	RelayStatusCheckAckPacketDelivery     RelayStatus = "CHECK_ACK_PACKET_DELIVERY"
	RelayStatusGetAckPacket               RelayStatus = "GET_ACK_PACKET"
	RelayStatusDeliverAckPacket           RelayStatus = "DELIVER_ACK_PACKET"
	RelayStatusAwaitingTimeoutFinality    RelayStatus = "AWAITING_TIMEOUT_FINALITY"
	RelayStatusCheckTimeoutPacketDelivery RelayStatus = "CHECK_TIMEOUT_PACKET_DELIVERY"
	RelayStatusGetTimeoutPacket           RelayStatus = "GET_TIMEOUT_PACKET"
	RelayStatusDeliverTimeoutPacket       RelayStatus = "DELIVER_TIMEOUT_PACKET"
	RelayStatusCompleteWithAck            RelayStatus = "COMPLETE_WITH_ACK"
	RelayStatusCompleteWithWriteAckError  RelayStatus = "COMPLETE_WITH_WRITE_ACK_ERROR"
	RelayStatusCompleteWithTimeout        RelayStatus = "COMPLETE_WITH_TIMEOUT"
	RelayStatusFailed                     RelayStatus = "FAILED"
)

Packet statuses

func AllRelayStatuses

func AllRelayStatuses() []RelayStatus

AllRelayStatuses enumerates possible packet states

type Repository

type Repository interface {
	// UpsertPacket records a packet. A new packet is inserted; an existing
	// NOT_SELECTED packet is refreshed with the input's metadata and status,
	// but only when the input status is NOT_SELECTED or PENDING, the latter
	// selecting it for relay. Existing packets in any other status are never
	// modified.
	UpsertPacket(ctx context.Context, input UpsertPacket) error

	ListPacketsBySourceTx(ctx context.Context, chainID string, txHash string) ([]Packet, error)

	ListPackets(ctx context.Context, filter PacketFilter, page Page) ([]Packet, error)

	// ListDispatchablePackets returns selected packets that have not reached a terminal status.
	ListDispatchablePackets(ctx context.Context) ([]Packet, error)

	UpdatePacketStatus(ctx context.Context, key PacketKey, status RelayStatus) error

	UpdatePacketRecvTx(ctx context.Context, key PacketKey, tx PacketTx) error
	ClearPacketRecvTx(ctx context.Context, key PacketKey) error

	UpdatePacketWriteAck(ctx context.Context, key PacketKey, ack WriteAck) error

	UpdatePacketAckTx(ctx context.Context, key PacketKey, tx PacketTx) error
	ClearPacketAckTx(ctx context.Context, key PacketKey) error

	UpdatePacketTimeoutTx(ctx context.Context, key PacketKey, tx PacketTx) error
	ClearPacketTimeoutTx(ctx context.Context, key PacketKey) error
}

Repository represents database CRUD operations.

type SqliteDB

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

SqliteDB is a wrapper around the sqlite database.

func NewSqlite

func NewSqlite(path string) (*SqliteDB, error)

func NewSqliteInMemory

func NewSqliteInMemory() (*SqliteDB, error)

func NewSqliteWithOptions

func NewSqliteWithOptions(path string, connectionOpts map[string]string) (*SqliteDB, error)

func (*SqliteDB) ClearPacketAckTx

func (db *SqliteDB) ClearPacketAckTx(ctx context.Context, key PacketKey) error

func (*SqliteDB) ClearPacketRecvTx

func (db *SqliteDB) ClearPacketRecvTx(ctx context.Context, key PacketKey) error

func (*SqliteDB) ClearPacketTimeoutTx

func (db *SqliteDB) ClearPacketTimeoutTx(ctx context.Context, key PacketKey) error

func (*SqliteDB) Close

func (db *SqliteDB) Close() error

func (*SqliteDB) ListDispatchablePackets

func (db *SqliteDB) ListDispatchablePackets(ctx context.Context) ([]Packet, error)

func (*SqliteDB) ListPackets

func (db *SqliteDB) ListPackets(
	ctx context.Context,
	filter PacketFilter,
	page Page,
) ([]Packet, error)

func (*SqliteDB) ListPacketsBySourceTx

func (db *SqliteDB) ListPacketsBySourceTx(
	ctx context.Context,
	chainID string,
	txHash string,
) ([]Packet, error)

func (*SqliteDB) MigrateDown

func (db *SqliteDB) MigrateDown() (int, error)

MigrateDown migrates only ONE migration down

func (*SqliteDB) MigrateUp

func (db *SqliteDB) MigrateUp() (int, error)

MigrateUp migrates ALL available migrations

func (*SqliteDB) MigrationStatus

func (db *SqliteDB) MigrationStatus() ([]MigrationStatus, error)

func (*SqliteDB) Ping

func (db *SqliteDB) Ping(ctx context.Context) error

func (*SqliteDB) Transact

func (db *SqliteDB) Transact(ctx context.Context, call func(repo Repository) error) error

func (*SqliteDB) UpdatePacketAckTx

func (db *SqliteDB) UpdatePacketAckTx(ctx context.Context, key PacketKey, tx PacketTx) error

func (*SqliteDB) UpdatePacketRecvTx

func (db *SqliteDB) UpdatePacketRecvTx(ctx context.Context, key PacketKey, tx PacketTx) error

func (*SqliteDB) UpdatePacketStatus

func (db *SqliteDB) UpdatePacketStatus(ctx context.Context, key PacketKey, status RelayStatus) error

func (*SqliteDB) UpdatePacketTimeoutTx

func (db *SqliteDB) UpdatePacketTimeoutTx(ctx context.Context, key PacketKey, tx PacketTx) error

func (*SqliteDB) UpdatePacketWriteAck

func (db *SqliteDB) UpdatePacketWriteAck(ctx context.Context, key PacketKey, ack WriteAck) error

func (*SqliteDB) UpsertPacket

func (db *SqliteDB) UpsertPacket(ctx context.Context, input UpsertPacket) error

type Store

type Store interface {
	Repository
	Migrator

	// Transact runs call in a transaction; call's Repository is bound to it and rolled back on error.
	Transact(ctx context.Context, call func(repo Repository) error) error

	Ping(ctx context.Context) error
	Close() error
}

Store a unified, database-agnostic API for persistence.

func NewStore

func NewStore(ctx context.Context, cfg config.Config) (Store, error)

NewStore creates a new Store instance based on the database type.

type UpsertPacket

type UpsertPacket struct {
	Status                    RelayStatus
	SourceChainID             string
	DestinationChainID        string
	SourceTxHash              string
	SourceTxTime              time.Time
	PacketSequenceNumber      uint64
	PacketSourceClientID      string
	PacketDestinationClientID string
	PacketTimeoutTimestamp    time.Time
}

UpsertPacket the fields callers provide when recording a packet; the remaining Packet fields are database-assigned or set later in the lifecycle.

func (UpsertPacket) Validate

func (t UpsertPacket) Validate() error

type WriteAck

type WriteAck struct {
	TxHash string
	TxTime time.Time
	Status WriteAckStatus
}

WriteAck the write acknowledgement observed for a packet.

type WriteAckStatus

type WriteAckStatus string

WriteAckStatus the execution result carried by a write ack.

const (
	WriteAckStatusSuccess WriteAckStatus = "SUCCESS"
	WriteAckStatusError   WriteAckStatus = "ERROR"
	WriteAckStatusUnknown WriteAckStatus = "UNKNOWN"
)

Write ack statuses

Directories

Path Synopsis
repository

Jump to

Keyboard shortcuts

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