database

package
v0.0.0-...-cdd02db Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2024 License: ISC Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateNew

func CreateNew(dbFile, feeXPub string, log slog.Logger) error

CreateNew intializes a new bbolt database with all of the necessary vspd buckets, and inserts: - the provided extended pubkey (to be used for deriving fee addresses). - an ed25519 keypair to sign API responses. - a secret key to use for initializing a HTTP cookie store.

Types

type AltSignAddrData

type AltSignAddrData struct {
	// AltSignAddr is the new alternate signing address. It is base 58 encoded.
	AltSignAddr string
	// Req is the original request to set an alternate signing address.
	Req string
	// ReqSig is the request's signature signed by the commitment address of the
	// corresponding ticket. It is base 64 encoded.
	ReqSig string
	// Resp is the original response from the server to the alternate signing
	// address.
	Resp string
	// RespSig is the response's signature signed by the server. It is base 64
	// encoded.
	RespSig string
}

AltSignAddrData holds the information needed to prove that a client added an alternate signing address.

type FeeStatus

type FeeStatus string

FeeStatus represents the current state of a ticket fee payment.

const (
	// NoFee indicates no fee tx has been received yet.
	NoFee FeeStatus = "none"
	// FeeReceieved indicates fee tx has been received but not broadcast.
	FeeReceieved FeeStatus = "received"
	// FeeBroadcast indicates fee tx has been broadcast but not confirmed.
	FeeBroadcast FeeStatus = "broadcast"
	// FeeConfirmed indicates fee tx has been broadcast and confirmed.
	FeeConfirmed FeeStatus = "confirmed"
	// FeeError indicates fee tx could not be broadcast due to an error.
	FeeError FeeStatus = "error"
)

type Ticket

type Ticket struct {
	Hash              string
	PurchaseHeight    int64
	CommitmentAddress string
	FeeAddressIndex   uint32
	FeeAddress        string
	FeeAmount         int64
	FeeExpiration     int64

	// Confirmed will be set when the ticket has 6+ confirmations.
	Confirmed bool

	// VotingWIF is set in /payfee.
	VotingWIF string

	// VoteChoices, TSpendPolicy and TreasuryPolicy are initially set in
	// /payfee, but can be updated in /setvotechoices.
	VoteChoices    map[string]string
	TSpendPolicy   map[string]string
	TreasuryPolicy map[string]string

	// FeeTxHex and FeeTxHash will be set when the fee tx has been received.
	FeeTxHex  string
	FeeTxHash string

	// FeeTxStatus indicates the current state of the fee transaction.
	FeeTxStatus FeeStatus

	// Outcome is set once a ticket is either voted or revoked. An empty outcome
	// indicates that a ticket is still votable.
	Outcome TicketOutcome
}

func (*Ticket) FeeExpired

func (t *Ticket) FeeExpired() bool

type TicketList

type TicketList []Ticket

func (TicketList) EarliestPurchaseHeight

func (t TicketList) EarliestPurchaseHeight() int64

EarliestPurchaseHeight returns the lowest non-zero purchase height in the list of tickets. Zero will be returned if the list is empty, or if every ticket in the list has zero purchase height.

func (TicketList) SortByPurchaseHeight

func (t TicketList) SortByPurchaseHeight()

type TicketOutcome

type TicketOutcome string

TicketOutcome describes the reason a ticket is no longer votable.

const (
	// Expired indicates the ticket expired and has been revoked.
	Expired TicketOutcome = "expired"
	// Missed indicates the ticket was missed and has been revoked.
	Missed TicketOutcome = "missed"
	// Voted indicates the ticket has already voted.
	Voted TicketOutcome = "voted"

	// Revoked is a deprecated status which should no longer be used. It was
	// used before vspd was able to distinguish between expired and missed
	// tickets.
	Revoked TicketOutcome = "revoked"
)

type VoteChangeRecord

type VoteChangeRecord struct {
	Request           string `json:"req"`
	RequestSignature  string `json:"reqs"`
	Response          string `json:"rsp"`
	ResponseSignature string `json:"rsps"`
}

VoteChangeRecord is serialized to json and stored in bbolt db. The json keys are deliberately kept short because they are duplicated many times in the db.

type VspDatabase

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

VspDatabase wraps an instance of bolt.DB and provides VSP specific convenience functions.

func Open

func Open(dbFile string, log slog.Logger, maxVoteChangeRecords int) (*VspDatabase, error)

Open initializes and returns an open database. An error is returned if no database file is found at the provided path.

func (*VspDatabase) AltSignAddrData

func (vdb *VspDatabase) AltSignAddrData(ticketHash string) (*AltSignAddrData, error)

AltSignAddrData retrieves a ticket's alternate signing data. Existence of an alternate signing address can be inferred by no error and nil data return.

func (*VspDatabase) BackupDB

func (vdb *VspDatabase) BackupDB(w http.ResponseWriter) error

BackupDB streams a backup of the database over an http response writer.

func (*VspDatabase) Close

func (vdb *VspDatabase) Close(writeBackup bool)

Close will close the database and, if requested, make a copy of the database to the backup location.

func (*VspDatabase) CookieSecret

func (vdb *VspDatabase) CookieSecret() ([]byte, error)

CookieSecret retrieves the generated cookie store secret key from the database.

func (*VspDatabase) CountTickets

func (vdb *VspDatabase) CountTickets() (int64, int64, int64, int64, error)

CountTickets returns the total number of voted, expired, missed, and currently voting tickets. This func iterates over every ticket so should be used sparingly.

func (*VspDatabase) DeleteAltSignAddr

func (vdb *VspDatabase) DeleteAltSignAddr(ticketHash string) error

DeleteAltSignAddr deletes an alternate signing address from the database. Does not error if there is no record in the database to delete.

func (*VspDatabase) DeleteTicket

func (vdb *VspDatabase) DeleteTicket(ticket Ticket) error

func (*VspDatabase) FeeXPub

func (vdb *VspDatabase) FeeXPub() (string, error)

FeeXPub retrieves the extended pubkey used for generating fee addresses from the database.

func (*VspDatabase) GetLastAddressIndex

func (vdb *VspDatabase) GetLastAddressIndex() (uint32, error)

GetLastAddressIndex retrieves the last index used to derive a new fee address from the fee xpub key.

func (*VspDatabase) GetMissedTickets

func (vdb *VspDatabase) GetMissedTickets() (TicketList, error)

GetMissedTickets returns all tickets which have outcome == missed.

func (*VspDatabase) GetMissingPurchaseHeight

func (vdb *VspDatabase) GetMissingPurchaseHeight() (TicketList, error)

GetMissingPurchaseHeight returns tickets which are confirmed but do not have a purchase height.

func (*VspDatabase) GetPendingFees

func (vdb *VspDatabase) GetPendingFees() (TicketList, error)

GetPendingFees returns tickets which are confirmed and have a fee tx which is not yet broadcast.

func (*VspDatabase) GetRevokedTickets

func (vdb *VspDatabase) GetRevokedTickets() (TicketList, error)

GetRevokedTickets returns all tickets which have outcome == revoked.

func (*VspDatabase) GetTicketByHash

func (vdb *VspDatabase) GetTicketByHash(ticketHash string) (Ticket, bool, error)

func (*VspDatabase) GetUnconfirmedFees

func (vdb *VspDatabase) GetUnconfirmedFees() (TicketList, error)

GetUnconfirmedFees returns tickets with a fee tx that is broadcast but not confirmed yet.

func (*VspDatabase) GetUnconfirmedTickets

func (vdb *VspDatabase) GetUnconfirmedTickets() (TicketList, error)

GetUnconfirmedTickets returns tickets which are not yet confirmed.

func (*VspDatabase) GetVotableTickets

func (vdb *VspDatabase) GetVotableTickets() (TicketList, error)

GetVotableTickets returns tickets with a confirmed fee tx and no outcome (ie. not expired/voted/missed).

func (*VspDatabase) GetVoteChanges

func (vdb *VspDatabase) GetVoteChanges(ticketHash string) (map[uint32]VoteChangeRecord, error)

GetVoteChanges retrieves all of the stored vote change records for the provided ticket hash.

func (*VspDatabase) GetVotedTickets

func (vdb *VspDatabase) GetVotedTickets() (TicketList, error)

GetVotedTickets returns tickets with a confirmed fee tx and outcome == voted.

func (*VspDatabase) InsertAltSignAddr

func (vdb *VspDatabase) InsertAltSignAddr(ticketHash string, data *AltSignAddrData) error

InsertAltSignAddr will insert the provided alternate signing address into the database. Returns an error if data for the ticket hash already exist.

Passed data must have no empty fields.

func (*VspDatabase) InsertNewTicket

func (vdb *VspDatabase) InsertNewTicket(ticket Ticket) error

InsertNewTicket will insert the provided ticket into the database. Returns an error if either the ticket hash or fee address already exist.

func (*VspDatabase) KeyPair

func (vdb *VspDatabase) KeyPair() (ed25519.PrivateKey, ed25519.PublicKey, error)

KeyPair retrieves the keypair used to sign API responses from the database.

func (*VspDatabase) SaveVoteChange

func (vdb *VspDatabase) SaveVoteChange(ticketHash string, record VoteChangeRecord) error

SaveVoteChange will insert the provided vote change record into the database, and if this breaches the maximum amount of allowed records, delete the oldest one which is currently stored. Records are stored using a serially increasing integer as the key.

func (*VspDatabase) SetLastAddressIndex

func (vdb *VspDatabase) SetLastAddressIndex(idx uint32) error

SetLastAddressIndex updates the last index used to derive a new fee address from the fee xpub key.

func (*VspDatabase) Size

func (vdb *VspDatabase) Size() (uint64, error)

Size returns the current size of the database in bytes. Note that this may not exactly match the size of the database file stored on disk.

func (*VspDatabase) UpdateTicket

func (vdb *VspDatabase) UpdateTicket(ticket Ticket) error

func (*VspDatabase) Upgrade

func (vdb *VspDatabase) Upgrade(currentVersion uint32) error

Upgrade will update the database to the latest known version.

func (*VspDatabase) Version

func (vdb *VspDatabase) Version() (uint32, error)

Version returns the current database version.

func (*VspDatabase) WriteHotBackupFile

func (vdb *VspDatabase) WriteHotBackupFile() error

WriteHotBackupFile writes a backup of the database file while the database is still open.

Jump to

Keyboard shortcuts

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