sqlite

package
v2.1.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2025 License: MIT Imports: 36 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Backup

func Backup(ctx context.Context, srcPath, destPath string) (err error)

Backup creates a backup of the database at the specified path. The backup is created using the SQLite backup API, which is safe to use with a live database.

This function should be used if the database is not already open in the current process. If the database is already open, use Store.Backup.

func ForeignKeyCheck

func ForeignKeyCheck(ctx context.Context, fp string, log *zap.Logger) error

ForeignKeyCheck runs a PRAGMA foreign_key_check on the database and logs any foreign key constraint violations. If any violations are found, an error is returned.

func IntegrityCheck

func IntegrityCheck(ctx context.Context, fp string, log *zap.Logger) error

IntegrityCheck runs a PRAGMA integrity_check on the database and logs any integrity errors. If any errors are found, an error is returned.

func NewPeerStore

func NewPeerStore(s *Store) (syncer.PeerStore, error)

NewPeerStore creates a new peer store using the given store.

Types

type PeerStore

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

A PeerStore stores information about peers.

func (*PeerStore) AddPeer

func (ps *PeerStore) AddPeer(peer string) error

AddPeer adds the given peer to the store.

func (*PeerStore) Ban

func (ps *PeerStore) Ban(peer string, duration time.Duration, reason string) error

Ban temporarily bans the given peer.

func (*PeerStore) Banned

func (ps *PeerStore) Banned(peer string) (bool, error)

Banned returns true if the peer is banned.

func (*PeerStore) PeerInfo

func (ps *PeerStore) PeerInfo(peer string) (syncer.PeerInfo, error)

PeerInfo returns the information for the given peer.

func (*PeerStore) Peers

func (ps *PeerStore) Peers() ([]syncer.PeerInfo, error)

Peers returns the addresses of all known peers.

func (*PeerStore) UpdatePeerInfo

func (ps *PeerStore) UpdatePeerInfo(peer string, fn func(*syncer.PeerInfo)) error

UpdatePeerInfo updates the information for the given peer.

type Store

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

A Store is a persistent store that uses a SQL database as its backend.

func OpenDatabase

func OpenDatabase(fp string, log *zap.Logger) (*Store, error)

OpenDatabase creates a new SQLite store and initializes the database. If the database does not exist, it is created.

func (*Store) AccountBalance

func (s *Store) AccountBalance(accountID rhp3.Account) (balance types.Currency, err error)

AccountBalance returns the balance of the account with the given ID.

func (*Store) AccountFunding

func (s *Store) AccountFunding(account rhp3.Account) (srcs []accounts.FundingSource, err error)

AccountFunding returns all contracts that were used to fund the account.

func (*Store) Accounts

func (s *Store) Accounts(limit, offset int) (acc []accounts.Account, err error)

Accounts returns all accounts in the database paginated.

func (*Store) AddContract

func (s *Store) AddContract(revision contracts.SignedRevision, formationSet []types.Transaction, lockedCollateral types.Currency, initialUsage contracts.Usage, negotationHeight uint64) error

AddContract adds a new contract to the database.

func (*Store) AddPeer

func (s *Store) AddPeer(peer string) error

AddPeer adds the given peer to the store.

func (*Store) AddTempSector

func (s *Store) AddTempSector(root types.Hash256, expiration uint64) error

AddTempSector adds a sector to temporary storage. The sectors will be deleted after the expiration height

func (*Store) AddTemporarySectors deprecated

func (s *Store) AddTemporarySectors(sectors []storage.TempSector) error

AddTemporarySectors adds the roots of sectors that are temporarily stored on the host. The sectors will be deleted after the expiration height.

Deprecated: use AddTempSector

func (*Store) AddV2Contract

func (s *Store) AddV2Contract(contract contracts.V2Contract, formationSet rhp4.TransactionSet) error

AddV2Contract adds a new contract to the database.

func (*Store) AddVolume

func (s *Store) AddVolume(localPath string, readOnly bool) (volumeID int64, err error)

AddVolume initializes a new storage volume and adds it to the volume store. GrowVolume must be called afterwards to initialize the volume to its desired size.

func (*Store) Backup

func (s *Store) Backup(ctx context.Context, destPath string) error

Backup creates a backup of the database at the specified path. The backup is created using the SQLite backup API, which is safe to use with a live database.

This function should be used if the database is already open in the current process. If the database is not already open, use Backup.

func (*Store) Ban

func (s *Store) Ban(peer string, duration time.Duration, reason string) error

Ban temporarily bans one or more IPs. The addr should either be a single IP with port (e.g. 1.2.3.4:5678) or a CIDR subnet (e.g. 1.2.3.4/16).

func (*Store) Banned

func (s *Store) Banned(peer string) (banned bool, _ error)

Banned returns true if the peer is banned.

func (*Store) CheckContractAccountFunding

func (s *Store) CheckContractAccountFunding() error

CheckContractAccountFunding checks that the contract account funding table is correct.

func (*Store) Close

func (s *Store) Close() error

Close closes the underlying database.

func (*Store) Contract

func (s *Store) Contract(id types.FileContractID) (contract contracts.Contract, err error)

Contract returns the contract with the given ID.

func (*Store) ContractActions

func (s *Store) ContractActions(index types.ChainIndex, revisionBroadcastHeight uint64) (actions contracts.LifecycleActions, err error)

ContractActions returns the contract lifecycle actions for the given index.

func (*Store) ContractChainIndexElement

func (s *Store) ContractChainIndexElement(index types.ChainIndex) (element types.ChainIndexElement, err error)

ContractChainIndexElement returns the chain index element for the given height.

func (*Store) Contracts

func (s *Store) Contracts(filter contracts.ContractFilter) (contracts []contracts.Contract, count int, err error)

Contracts returns a paginated list of contracts.

func (*Store) CreditAccountWithContract

func (s *Store) CreditAccountWithContract(fund accounts.FundAccountWithContract) error

CreditAccountWithContract adds the specified amount to the account with the given ID.

func (*Store) DebitAccount

func (s *Store) DebitAccount(accountID rhp3.Account, usage accounts.Usage) error

DebitAccount subtracts the specified amount from the account with the given ID. Returns the remaining balance of the account.

func (*Store) ExpireContractSectors

func (s *Store) ExpireContractSectors(height uint64) error

ExpireContractSectors expires all sectors that are no longer covered by an active contract.

func (*Store) ExpireTempSectors

func (s *Store) ExpireTempSectors(height uint64) error

ExpireTempSectors deletes the roots of sectors that are no longer temporarily stored on the host.

func (*Store) ExpireV2ContractSectors

func (s *Store) ExpireV2ContractSectors(height uint64) error

ExpireV2ContractSectors expires all sectors that are no longer covered by an active contract.

func (*Store) GetRegistryValue

func (s *Store) GetRegistryValue(key rhp3.RegistryKey) (entry rhp3.RegistryValue, err error)

GetRegistryValue returns the registry value for the given key. If the key is not found should return ErrEntryNotFound.

func (*Store) GrowVolume

func (s *Store) GrowVolume(id int64, maxSectors uint64) error

GrowVolume grows a storage volume's metadata by n sectors.

func (*Store) HasSector

func (s *Store) HasSector(root types.Hash256) (exists bool, err error)

HasSector returns true if the sector root is stored on the host

func (*Store) HostKey

func (s *Store) HostKey() (pk types.PrivateKey)

HostKey returns the host's private key.

func (*Store) IncrementRHPDataUsage

func (s *Store) IncrementRHPDataUsage(ingress, egress uint64) error

IncrementRHPDataUsage increments the RHP3 ingress and egress metrics.

func (*Store) IncrementRegistryAccess

func (s *Store) IncrementRegistryAccess(read, write uint64) error

IncrementRegistryAccess increments the registry read and write metrics.

func (*Store) IncrementSectorStats

func (s *Store) IncrementSectorStats(reads, writes, cacheHit, cacheMiss uint64) error

IncrementSectorStats increments the sector read, write and cache metrics.

func (*Store) LastAnnouncement

func (s *Store) LastAnnouncement() (ann settings.Announcement, err error)

LastAnnouncement returns the last announcement.

func (*Store) LastV2AnnouncementHash

func (s *Store) LastV2AnnouncementHash() (h types.Hash256, index types.ChainIndex, err error)

LastV2AnnouncementHash returns the hash of the last v2 announcement and the chain index it was confirmed in.

func (*Store) Metrics

func (s *Store) Metrics(timestamp time.Time) (m metrics.Metrics, err error)

Metrics returns aggregate metrics for the host as of the timestamp.

func (*Store) MigrateSectors

func (s *Store) MigrateSectors(ctx context.Context, volumeID int64, startIndex uint64, migrateFn storage.MigrateFunc) (migrated, failed int, err error)

MigrateSectors migrates each occupied sector of a volume starting at startIndex. migrateFn will be called for each sector that needs to be migrated. The sector data should be copied to the new location and synced to disk immediately. If migrateFn returns an error, that sector will be considered failed and the migration will continue. If the context is canceled, the migration will stop and the error will be returned. If the host runs out of writable storage, migration will stop and ErrNotEnoughStorage will be returned. The number of sectors migrated and failed will always be returned, even if an error occurs.

func (*Store) Peers

func (s *Store) Peers() (peers []syncer.PeerInfo, _ error)

Peers returns the addresses of all known peers.

func (*Store) PeriodMetrics

func (s *Store) PeriodMetrics(start time.Time, n int, interval metrics.Interval) ([]metrics.Metrics, error)

PeriodMetrics returns aggregate metrics for n periods starting at start

func (*Store) PinnedSettings

func (s *Store) PinnedSettings(context.Context) (pinned pin.PinnedSettings, err error)

PinnedSettings returns the host's pinned settings.

func (*Store) PruneAccounts

func (s *Store) PruneAccounts(height uint64) error

PruneAccounts removes all accounts that have expired

func (*Store) PruneSectors

func (s *Store) PruneSectors(ctx context.Context, lastAccess time.Time) error

PruneSectors removes volume references for sectors that have not been accessed since the provided timestamp and are no longer referenced by a contract or temp storage.

func (*Store) RHP4AccountBalance

func (s *Store) RHP4AccountBalance(account proto4.Account) (balance types.Currency, err error)

RHP4AccountBalance returns the balance of the account with the given ID.

func (*Store) RHP4AccountBalances

func (s *Store) RHP4AccountBalances(accounts []proto4.Account) (balances []types.Currency, err error)

RHP4AccountBalances returns the balances of the accounts with the given IDs. The balances are returned in the same order as the input accounts. If an account does not exist, the balance at that index will be types.ZeroCurrency.

func (*Store) RHP4CreditAccounts

func (s *Store) RHP4CreditAccounts(deposits []proto4.AccountDeposit, contractID types.FileContractID, revision types.V2FileContract, usage proto4.Usage) (balances []types.Currency, err error)

RHP4CreditAccounts credits the accounts with the given deposits and revises the contract.

func (*Store) RHP4DebitAccount

func (s *Store) RHP4DebitAccount(account proto4.Account, usage proto4.Usage) error

RHP4DebitAccount debits the account with the given ID.

func (*Store) RebroadcastFormationSets

func (s *Store) RebroadcastFormationSets(minNegotiationheight uint64) (rebroadcast [][]types.Transaction, err error)

RebroadcastFormationSets returns formation sets that should be rebroadcast

func (*Store) RecalcContractAccountFunding

func (s *Store) RecalcContractAccountFunding() error

RecalcContractAccountFunding recalculates the contract account funding table.

func (*Store) RegisterWebhook

func (s *Store) RegisterWebhook(url, secret string, scopes []string) (id int64, err error)

RegisterWebhook registers a new webhook.

func (*Store) RegistryEntries

func (s *Store) RegistryEntries() (count, limit uint64, err error)

RegistryEntries returns the current number of entries as well as the maximum number of entries the registry can hold.

func (*Store) RemoveSector

func (s *Store) RemoveSector(root types.Hash256) (err error)

RemoveSector removes the metadata of a sector and returns its location in the volume.

func (*Store) RemoveVolume

func (s *Store) RemoveVolume(id int64, force bool) error

RemoveVolume removes a storage volume from the volume store. If there are used sectors in the volume, ErrVolumeNotEmpty is returned. If force is true, the volume is removed regardless of whether it is empty.

func (*Store) RemoveWebhook

func (s *Store) RemoveWebhook(id int64) error

RemoveWebhook removes a webhook.

func (*Store) RenewContract

func (s *Store) RenewContract(renewal contracts.SignedRevision, clearing contracts.SignedRevision, renewalTxnSet []types.Transaction, lockedCollateral types.Currency, clearingUsage, renewalUsage contracts.Usage, negotationHeight uint64) error

RenewContract adds a new contract to the database and sets the old contract's renewed_from field. The old contract's sector roots are copied to the new contract.

func (*Store) RenewV2Contract

func (s *Store) RenewV2Contract(renewal contracts.V2Contract, renewalSet rhp4.TransactionSet, renewedID types.FileContractID, roots []types.Hash256) error

RenewV2Contract adds a new v2 contract to the database and sets the old contract's renewed_from field. The old contract's sector roots are copied to the new contract. The status of the old contract should continue to be active until the renewal is confirmed

func (*Store) ResetChainState

func (s *Store) ResetChainState() error

ResetChainState resets the consensus state of the store. This should only occur if the user has reset their consensus database to sync from scratch.

func (*Store) RevertLastAnnouncement

func (s *Store) RevertLastAnnouncement() error

RevertLastAnnouncement reverts the last announcement.

func (*Store) ReviseContract

func (s *Store) ReviseContract(revision contracts.SignedRevision, roots []types.Hash256, usage contracts.Usage, sectorChanges []contracts.SectorChange) error

ReviseContract atomically updates a contract's revision and sectors

func (*Store) ReviseV2Contract

func (s *Store) ReviseV2Contract(id types.FileContractID, revision types.V2FileContract, oldRoots, newRoots []types.Hash256, usage proto4.Usage) error

ReviseV2Contract atomically updates a contract's revision and sectors

func (*Store) SectorLocation

func (s *Store) SectorLocation(root types.Hash256) (location storage.SectorLocation, err error)

SectorLocation returns the location of a sector or an error if the sector is not found.

func (*Store) SectorReferences

func (s *Store) SectorReferences(root types.Hash256) (refs storage.SectorReference, err error)

SectorReferences returns the references, if any of a sector root

func (*Store) SectorRoots

func (s *Store) SectorRoots() (roots map[types.FileContractID][]types.Hash256, err error)

SectorRoots returns the sector roots for all contracts.

func (*Store) SetAvailable

func (s *Store) SetAvailable(volumeID int64, available bool) error

SetAvailable sets the available flag on a volume.

func (*Store) SetReadOnly

func (s *Store) SetReadOnly(volumeID int64, readOnly bool) error

SetReadOnly sets the read-only flag on a volume.

func (*Store) SetRegistryValue

func (s *Store) SetRegistryValue(entry rhp3.RegistryEntry, expiration uint64) error

SetRegistryValue sets the registry value for the given key.

func (*Store) Settings

func (s *Store) Settings() (config settings.Settings, err error)

Settings returns the current host settings.

func (*Store) ShrinkVolume

func (s *Store) ShrinkVolume(id int64, maxSectors uint64) error

ShrinkVolume shrinks a storage volume's metadata to maxSectors. If there are used sectors outside of the new maximum, ErrVolumeNotEmpty is returned.

func (*Store) StorageUsage

func (s *Store) StorageUsage() (usedSectors, totalSectors uint64, err error)

StorageUsage returns the number of sectors stored and the total number of sectors available in the storage pool.

func (*Store) StoreSector

func (s *Store) StoreSector(root types.Hash256, fn storage.StoreFunc) error

StoreSector calls fn with an empty location in a writable volume. If the sector root already exists, nil is returned. The sector should be written to disk within fn. If fn returns an error, the metadata is rolled back and the error is returned. If no space is available, ErrNotEnoughStorage is returned.

func (*Store) Tip

func (s *Store) Tip() (index types.ChainIndex, err error)

Tip returns the last scanned chain index.

func (*Store) UnspentSiacoinElements

func (s *Store) UnspentSiacoinElements() (utxos []types.SiacoinElement, err error)

UnspentSiacoinElements returns the spendable siacoin outputs in the wallet.

func (*Store) UpdateChainState

func (s *Store) UpdateChainState(fn func(index.UpdateTx) error) error

UpdateChainState updates the chain state with the given updates.

func (*Store) UpdateLastAnnouncement

func (s *Store) UpdateLastAnnouncement(ann settings.Announcement) error

UpdateLastAnnouncement updates the last announcement.

func (*Store) UpdatePinnedSettings

func (s *Store) UpdatePinnedSettings(_ context.Context, p pin.PinnedSettings) error

UpdatePinnedSettings updates the host's pinned settings.

func (*Store) UpdateSettings

func (s *Store) UpdateSettings(settings settings.Settings) error

UpdateSettings updates the host's stored settings.

func (*Store) UpdateWebhook

func (s *Store) UpdateWebhook(id int64, url string, scopes []string) error

UpdateWebhook updates a webhook.

func (*Store) V2Contract

func (s *Store) V2Contract(id types.FileContractID) (contract contracts.V2Contract, err error)

V2Contract returns the contract with the given ID.

func (*Store) V2ContractElement

func (s *Store) V2ContractElement(contractID types.FileContractID) (basis types.ChainIndex, ele types.V2FileContractElement, err error)

V2ContractElement returns the latest v2 state element with the given ID.

func (*Store) V2Contracts

func (s *Store) V2Contracts(filter contracts.V2ContractFilter) (contracts []contracts.V2Contract, count int, err error)

V2Contracts returns a paginated list of v2 contracts.

func (*Store) V2SectorRoots

func (s *Store) V2SectorRoots() (roots map[types.FileContractID][]types.Hash256, err error)

V2SectorRoots returns the sector roots for all v2 contracts.

func (*Store) Vacuum

func (s *Store) Vacuum() error

Vacuum runs the VACUUM command on the database.

func (*Store) VerifyWalletKey

func (s *Store) VerifyWalletKey(seedHash types.Hash256) error

VerifyWalletKey checks that the wallet seed matches the seed hash. This detects if the user's recovery phrase has changed and the wallet needs to rescan.

func (*Store) Volume

func (s *Store) Volume(id int64) (vol storage.Volume, err error)

Volume returns a volume by its ID.

func (*Store) Volumes

func (s *Store) Volumes() (volumes []storage.Volume, err error)

Volumes returns a list of all volumes.

func (*Store) WalletEventCount

func (s *Store) WalletEventCount() (count uint64, err error)

WalletEventCount returns the total number of events relevant to the wallet.

func (*Store) WalletEvents

func (s *Store) WalletEvents(offset, limit int) (events []wallet.Event, err error)

WalletEvents returns a paginated list of transactions ordered by maturity height, descending. If no more transactions are available, (nil, nil) should be returned.

func (*Store) Webhooks

func (s *Store) Webhooks() (hooks []webhooks.Webhook, err error)

Webhooks returns all webhooks.

Jump to

Keyboard shortcuts

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