clientdb

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2021 License: MIT Imports: 31 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DBFilename is the default filename of the client database.
	DBFilename = "pool.db"

	// DefaultPoolDBTimeout is the default maximum time we wait for the
	// Pool bbolt database to be opened. If the database is already opened
	// by another process, the unique lock cannot be obtained. With the
	// timeout we error out after the given time instead of just blocking
	// for forever.
	DefaultPoolDBTimeout = 5 * time.Second
)
View Source
const Subsystem = "CLDB"

Variables

View Source
var (
	// ErrNoOrder is the error returned if no order with the given nonce
	// exists in the store.
	ErrNoOrder = errors.New("no order found")

	// ErrOrderExists is returned if an order is submitted that is already
	// known to the store.
	ErrOrderExists = errors.New("order with this nonce already exists")
)
View Source
var (

	// ErrAccountNotFound is an error returned when we attempt to retrieve
	// information about an account but it is not found.
	ErrAccountNotFound = errors.New("account not found")
)
View Source
var (

	// ErrDBReversion is returned when detecting an attempt to revert to a
	// prior database version.
	ErrDBReversion = errors.New("cannot revert to prior version")
)
View Source
var (
	// ErrNoSidecar is the error returned if no sidecar with the given
	// multisig pubkey exists in the store.
	ErrNoSidecar = errors.New("no sidecar found")
)

Functions

func DeserializeOrder

func DeserializeOrder(nonce order.Nonce, r io.Reader) (
	order.Order, error)

DeserializeOrder deserializes an order from the binary LN wire format.

func ReadElement

func ReadElement(r io.Reader, element interface{}) error

ReadElement is a one-stop utility function to deserialize any data structure.

func ReadElements

func ReadElements(r io.Reader, elements ...interface{}) error

ReadElements deserializes a variable number of elements into the passed io.Reader, with each element being deserialized according to the ReadElement function.

func SerializeOrder

func SerializeOrder(o order.Order, w io.Writer) error

SerializeOrder binary serializes an order to a writer using the common LN wire format.

func UseLogger

func UseLogger(logger btclog.Logger)

UseLogger uses a specified Logger to output package logging info. This should be used in preference to SetLogWriter if the caller is also using btclog.

func WriteElement

func WriteElement(w io.Writer, element interface{}) error

WriteElement is a one-stop shop to write the big endian representation of any element which is to be serialized. The passed io.Writer should be backed by an appropriately sized byte slice, or be able to dynamically expand to accommodate additional data.

func WriteElements

func WriteElements(w io.Writer, elements ...interface{}) error

WriteElements is writes each element in the elements slice to the passed io.Writer using WriteElement.

Types

type CreatedEvent

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

CreatedEvent is an event implementation that tracks the creation of an order. This is distinct from the order state change to allow us to efficiently filter all events by their type to get the creation timestamps of all orders.

func NewCreatedEvent

func NewCreatedEvent(o order.Order) *CreatedEvent

NewCreatedEvent creates a new CreatedEvent from an order with the current system time as the timestamp.

func (*CreatedEvent) Deserialize

func (e *CreatedEvent) Deserialize(r io.Reader) error

Deserialize reads the event data from a binary storage format. This does not deserialize the event type as that's handled generically to allow for easy filtering.

NOTE: This is part of the event.Event interface.

func (*CreatedEvent) Nonce

func (e *CreatedEvent) Nonce() order.Nonce

Nonce returns the nonce of the order this event refers to.

NOTE: This is part of the order.OrderEvent interface.

func (*CreatedEvent) Serialize

func (e *CreatedEvent) Serialize(w io.Writer) error

Serialize writes the event data to a binary storage format. This does not serialize the event type as that's handled generically to allow for easy filtering.

NOTE: This is part of the event.Event interface.

func (*CreatedEvent) SetTimestamp

func (e *CreatedEvent) SetTimestamp(ts time.Time)

SetTimestamp updates the timestamp of the event. This is needed to adjust timestamps in case they collide to ensure the global uniqueness of all event timestamps.

NOTE: This is part of the event.Event interface.

func (*CreatedEvent) String

func (e *CreatedEvent) String() string

String returns a human readable representation of the event.

NOTE: This is part of the event.Event interface.

func (*CreatedEvent) Timestamp

func (e *CreatedEvent) Timestamp() time.Time

Timestamp is the time the event happened. This will be made unique once it is stored. To avoid collisions, the timestamp is adjusted on the nanosecond scale to reach uniqueness.

NOTE: This is part of the event.Event interface.

func (*CreatedEvent) Type

func (e *CreatedEvent) Type() event.Type

Type returns the type of the event.

NOTE: This is part of the event.Event interface.

type DB

type DB struct {
	*bbolt.DB
}

DB is a bolt-backed persistent store.

func New

func New(dir, fileName string) (*DB, error)

New creates a new bolt database that can be found at the given directory.

func (*DB) Account

func (db *DB) Account(traderKey *btcec.PublicKey) (*account.Account, error)

Account retrieves a specific account by trader key or returns ErrAccountNotFound if it's not found.

func (*DB) Accounts

func (db *DB) Accounts() ([]*account.Account, error)

Accounts retrieves all known accounts from the database.

func (*DB) AddAccount

func (db *DB) AddAccount(account *account.Account) error

AddAccount adds a record for the account to the database.

func (*DB) AddSidecar

func (db *DB) AddSidecar(ticket *sidecar.Ticket) error

AddSidecar adds a record for the sidecar to the database.

func (*DB) AddSidecarWithBid

func (db *DB) AddSidecarWithBid(ticket *sidecar.Ticket, bid *order.Bid) error

AddSidecarWithBid is identical to the AddSidecar method, but it also inserts a bid template in a special bucket to facilitate automated negotiation of sidecar channels.

func (*DB) AllEvents

func (db *DB) AllEvents(evtType event.Type) ([]event.Event, error)

AllEvents returns all events that are of the given type. Use event.TypeAny to not filter by type.

func (*DB) DeletePendingBatch

func (db *DB) DeletePendingBatch() error

DeletePendingBatch removes all references to the current pending batch without applying its staged updates to accounts and orders. If no pending batch exists, this acts as a no-op.

func (*DB) GetEvents

func (db *DB) GetEvents(timestamps map[time.Time]struct{}) ([]event.Event,
	error)

GetEvents returns all events identified by their unique timestamps.

func (*DB) GetEventsInRange

func (db *DB) GetEventsInRange(start, end time.Time,
	evtType event.Type) ([]event.Event, error)

GetEventsInRange returns all events that have their timestamps between the given start and end time (inclusive) and are of the given type. Use event.TypeAny to not filter by type.

func (*DB) GetLocalBatchSnapshot

func (db *DB) GetLocalBatchSnapshot(id order.BatchID) (
	*LocalBatchSnapshot, error)

GetLocalBatchSnapshot fetches the batch snapshot for the given batch ID.

func (*DB) GetLocalBatchSnapshots

func (db *DB) GetLocalBatchSnapshots() ([]*LocalBatchSnapshot, error)

GetLocalBatchSnapshots returns snapshots for all batches the trader has participated in.

func (*DB) GetOrder

func (db *DB) GetOrder(nonce order.Nonce) (order.Order, error)

GetOrder returns an order by looking up the nonce. If no order with that nonce exists in the store, ErrNoOrder is returned.

NOTE: This is part of the Store interface.

func (*DB) GetOrderEvents

func (db *DB) GetOrderEvents(o order.Nonce) ([]event.Event, error)

GetOrderEvents returns all events of an order by looking up the event reference keys in the order bucket.

func (*DB) GetOrders

func (db *DB) GetOrders() ([]order.Order, error)

GetOrders returns all orders that are currently known to the store.

NOTE: This is part of the Store interface.

func (*DB) LockID

func (db *DB) LockID() (wtxmgr.LockID, error)

LockID retrieves the database's global lock ID used to lease outputs from the backing lnd node's wallet.

func (*DB) MarkBatchComplete

func (db *DB) MarkBatchComplete() error

MarkBatchComplete marks a pending batch as complete, applying any staged modifications necessary, and allowing a trader to participate in a new batch. If a pending batch is not found, account.ErrNoPendingBatch is returned.

func (*DB) PendingBatchSnapshot

func (db *DB) PendingBatchSnapshot() (*LocalBatchSnapshot, error)

PendingBatchSnapshot retrieves the snapshot of the currently pending batch. If there isn't one, account.ErrNoPendingBatch is returned.

func (*DB) Sidecar

func (db *DB) Sidecar(id [8]byte,
	offerSignPubKey *btcec.PublicKey) (*sidecar.Ticket, error)

Sidecar retrieves a specific sidecar by its ID and provider signing key (offer signature pubkey) or returns ErrNoSidecar if it's not found.

func (*DB) SidecarBidTemplate

func (db *DB) SidecarBidTemplate(ticket *sidecar.Ticket) (*order.Bid, error)

SidecarBidTemplate attempts to retrieve a bid template associated with the passed sidecar ticket.

func (*DB) Sidecars

func (db *DB) Sidecars() ([]*sidecar.Ticket, error)

Sidecars retrieves all known sidecars from the database.

func (*DB) StoreBatchEvents

func (db *DB) StoreBatchEvents(batch *order.Batch, state order.MatchState,
	rejectReason poolrpc.MatchRejectReason) error

StoreBatchEvents creates a match event of the given match state for each of our orders involved in a batch and stores it to the main event store. In case of a batch reject the RPC reason enum value can optionally be specified.

func (*DB) StoreBatchPartialRejectEvents

func (db *DB) StoreBatchPartialRejectEvents(batch *order.Batch,
	partialRejects map[order.Nonce]*auctioneerrpc.OrderReject) error

StoreBatchPartialRejectEvents creates a reject match event for each of our orders involved in a batch and stores it to the main event store, including the reason for the reject.

func (*DB) StoreOrderEvents

func (db *DB) StoreOrderEvents(events []OrderEvent) error

StoreOrderEvents stores a list of individual order events in a single database transaction. The events' timestamps are adjusted on the nanosecond scale to ensure they're unique.

func (*DB) StorePendingBatch

func (db *DB) StorePendingBatch(batch *order.Batch, orders []order.Nonce,
	orderModifiers [][]order.Modifier, accounts []*account.Account,
	accountModifiers [][]account.Modifier) error

StorePendingBatch atomically stages all modified orders/accounts as a result of a pending batch. If any single operation fails, the whole set of changes is rolled back. Once the batch has been finalized/confirmed on-chain, then the stage modifications will be applied atomically as a result of MarkBatchComplete.

func (*DB) SubmitOrder

func (db *DB) SubmitOrder(newOrder order.Order) error

SubmitOrder stores an order by using the orders's nonce as an identifier. If an order with the given nonce already exists in the store, ErrOrderExists is returned.

NOTE: This is part of the Store interface.

func (*DB) UpdateAccount

func (db *DB) UpdateAccount(acct *account.Account,
	modifiers ...account.Modifier) error

UpdateAccount updates an account in the database according to the given modifiers.

func (*DB) UpdateOrder

func (db *DB) UpdateOrder(nonce order.Nonce, modifiers ...order.Modifier) error

UpdateOrder updates an order in the database according to the given modifiers.

NOTE: This is part of the Store interface.

func (*DB) UpdateOrders

func (db *DB) UpdateOrders(nonces []order.Nonce,
	modifiers [][]order.Modifier) error

UpdateOrders atomically updates a list of orders in the database according to the given modifiers.

NOTE: This is part of the Store interface.

func (*DB) UpdateSidecar

func (db *DB) UpdateSidecar(ticket *sidecar.Ticket) error

UpdateSidecar updates a sidecar in the database.

type LocalBatchSnapshot

type LocalBatchSnapshot struct {
	// Version is the version of the batch verification protocol.
	Version order.BatchVersion

	// BatchID is the batch's unique ID.
	BatchID order.BatchID

	// ClearingPrices is a map of the lease duration markets and the fixed
	// rate the orders were cleared at within that market.
	ClearingPrices map[uint32]order.FixedRatePremium

	// ExecutionFee is the FeeSchedule that was used by the server to
	// calculate the execution fee.
	ExecutionFee terms.LinearFeeSchedule

	// BatchTX is the complete batch transaction with all non-witness data
	// fully populated.
	BatchTX *wire.MsgTx

	// BatchTxFeeRate is the miner fee rate in sat/kW that was chosen for
	// the batch transaction.
	BatchTxFeeRate chainfee.SatPerKWeight

	// Account holds snapshots of the ending state of the local accounts
	// that participated in this batch.
	Accounts map[[33]byte]*account.Account

	// Orders holds snapshots of the ending state of local orders that were
	// part of matches in this batch.
	Orders map[order.Nonce]order.Order

	// MatchedOrders is a map between all trader's orders and the other
	// orders that were matched to them in the batch.
	MatchedOrders map[order.Nonce][]*order.MatchedOrder
}

LocalBatchSnapshot holds key information about our participation in a batch.

func NewSnapshot

func NewSnapshot(batch *order.Batch, ourOrders []order.Order,
	accounts []*account.Account) (*LocalBatchSnapshot, error)

NewSnapshots creates a new LocalBatchSnapshot from the passed order batched.

type MatchEvent

type MatchEvent struct {

	// MatchState is the state of the order matching process the event was
	// created in.
	MatchState order.MatchState

	// UnitsFilled is the number of units that was or would have been filled
	// for the current match attempt the order was in when this event was
	// created.
	UnitsFilled order.SupplyUnit

	// MatchedOrder is the order counterpart that our order was matched to
	// in the current match attempt this event was created for.
	MatchedOrder order.Nonce

	// RejectReason is set to the reason we rejected the match in case the
	// MatchState is set to MatchStateRejected.
	RejectReason uint32
	// contains filtered or unexported fields
}

MatchEvent is an event implementation that tracks the match making process of an order.

func NewMatchEvent

func NewMatchEvent(ts time.Time, nonce order.Nonce, state order.MatchState,
	unitsFilled order.SupplyUnit, matchedOrder order.Nonce,
	rejectReason uint32) *MatchEvent

NewMatchEvent creates a new MatchEvent from an order and its matched counterpart with the given time as the timestamp.

func (*MatchEvent) Deserialize

func (e *MatchEvent) Deserialize(r io.Reader) error

Deserialize reads the event data from a binary storage format. This does not deserialize the event type as that's handled generically to allow for easy filtering.

NOTE: This is part of the event.Event interface.

func (*MatchEvent) Nonce

func (e *MatchEvent) Nonce() order.Nonce

Nonce returns the nonce of the order this event refers to.

NOTE: This is part of the order.OrderEvent interface.

func (*MatchEvent) Serialize

func (e *MatchEvent) Serialize(w io.Writer) error

Serialize writes the event data to a binary storage format. This does not serialize the event type as that's handled generically to allow for easy filtering.

NOTE: This is part of the event.Event interface.

func (*MatchEvent) SetTimestamp

func (e *MatchEvent) SetTimestamp(ts time.Time)

SetTimestamp updates the timestamp of the event. This is needed to adjust timestamps in case they collide to ensure the global uniqueness of all event timestamps.

NOTE: This is part of the event.Event interface.

func (*MatchEvent) String

func (e *MatchEvent) String() string

String returns a human readable representation of the event.

NOTE: This is part of the event.Event interface.

func (*MatchEvent) Timestamp

func (e *MatchEvent) Timestamp() time.Time

Timestamp is the time the event happened. This will be made unique once it is stored. To avoid collisions, the timestamp is adjusted on the nanosecond scale to reach uniqueness.

NOTE: This is part of the event.Event interface.

func (*MatchEvent) Type

func (e *MatchEvent) Type() event.Type

Type returns the type of the event.

NOTE: This is part of the event.Event interface.

type OrderEvent

type OrderEvent interface {
	event.Event

	// Nonce returns the nonce of the order this event refers to.
	Nonce() order.Nonce
}

OrderEvent is the main interface for order specific events.

type UpdatedEvent

type UpdatedEvent struct {

	// PrevState is the state the order had previous to the state change.
	PrevState order.State

	// NewState is the state the order had after the state change.
	NewState order.State

	// UnitsFilled is the number of units that was filled at the moment of
	// the update.
	UnitsFilled order.SupplyUnit
	// contains filtered or unexported fields
}

UpdatedEvent is an event implementation that tracks the updates of an order. This event is only meant for updates that we also persist in the database. Temporary state changes and other updates that occur during match making are tracked by MatchEvent.

func NewUpdatedEvent

func NewUpdatedEvent(prevState order.State,
	o order.Order) *UpdatedEvent

NewUpdatedEvent creates a new UpdatedEvent from an order and its previous state with the current system time as the timestamp.

func (*UpdatedEvent) Deserialize

func (e *UpdatedEvent) Deserialize(r io.Reader) error

Deserialize reads the event data from a binary storage format. This does not deserialize the event type as that's handled generically to allow for easy filtering.

NOTE: This is part of the event.Event interface.

func (*UpdatedEvent) Nonce

func (e *UpdatedEvent) Nonce() order.Nonce

Nonce returns the nonce of the order this event refers to.

NOTE: This is part of the order.OrderEvent interface.

func (*UpdatedEvent) Serialize

func (e *UpdatedEvent) Serialize(w io.Writer) error

Serialize writes the event data to a binary storage format. This does not serialize the event type as that's handled generically to allow for easy filtering.

NOTE: This is part of the event.Event interface.

func (*UpdatedEvent) SetTimestamp

func (e *UpdatedEvent) SetTimestamp(ts time.Time)

SetTimestamp updates the timestamp of the event. This is needed to adjust timestamps in case they collide to ensure the global uniqueness of all event timestamps.

NOTE: This is part of the event.Event interface.

func (*UpdatedEvent) String

func (e *UpdatedEvent) String() string

String returns a human readable representation of the event.

NOTE: This is part of the event.Event interface.

func (*UpdatedEvent) Timestamp

func (e *UpdatedEvent) Timestamp() time.Time

Timestamp is the time the event happened. This will be made unique once it is stored. To avoid collisions, the timestamp is adjusted on the nanosecond scale to reach uniqueness.

NOTE: This is part of the event.Event interface.

func (*UpdatedEvent) Type

func (e *UpdatedEvent) Type() event.Type

Type returns the type of the event.

NOTE: This is part of the event.Event interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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