storage

package
v0.6.63 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2020 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultBlockCacheSize is 0 MB.
	DefaultBlockCacheSize = 0

	// DefaultIndexCacheSize is 2 GB.
	DefaultIndexCacheSize = 2000 << 20

	// TinyIndexCacheSize is 10 MB.
	TinyIndexCacheSize = 10 << 20

	// DefaultMaxTableSize is 256 MB. The larger
	// this value is, the larger database transactions
	// storage can handle (~15% of the max table size
	// == max commit size).
	DefaultMaxTableSize = 256 << 20

	// DefaultLogValueSize is 64 MB.
	DefaultLogValueSize = 64 << 20

	// DefaultCompressionMode is the default block
	// compression setting.
	DefaultCompressionMode = options.None
)
View Source
const (
	// BlockCounter is the number of added blocks.
	BlockCounter = "blocks"

	// OrphanCounter is the number of orphaned blocks.
	OrphanCounter = "orphans"

	// TransactionCounter is the number of processed transactions.
	TransactionCounter = "transactions"

	// OperationCounter is the number of processed operations.
	OperationCounter = "operations"

	// AddressesCreatedCounter is the number of created addresses.
	AddressesCreatedCounter = "addresses_created"

	// TransactionsCreatedCounter is the number of created transactions.
	TransactionsCreatedCounter = "transactions_created"

	// TransactionsConfirmedCounter is the number of confirmed transactions.
	TransactionsConfirmedCounter = "transactions_confirmed"

	// StaleBroadcastsCounter is the number of transaction broadcasts that
	// never appeared on-chain.
	StaleBroadcastsCounter = "stale_broadcasts"

	// FailedBroadcastsCounter is the number of transaction broadcasts that
	// never made it on-chain after retries.
	FailedBroadcastsCounter = "failed_broadcasts"

	// ActiveReconciliationCounter is the number of active
	// reconciliations performed.
	ActiveReconciliationCounter = "active_reconciliations"

	// InactiveReconciliationCounter is the number of inactive
	// reconciliations performed.
	InactiveReconciliationCounter = "inactive_reconciliations"

	// ExemptReconciliationCounter is the number of reconciliation
	// failures that were exempt.
	ExemptReconciliationCounter = "exempt_reconciliations"

	// FailedReconciliationCounter is the number of reconciliation
	// failures that were not exempt.
	FailedReconciliationCounter = "failed_reconciliations"

	// SkippedReconciliationsCounter is the number of reconciliation
	// attempts that were skipped. This typically occurs because an
	// account balance has been updated since being marked for reconciliation
	// or the block where an account was updated has been orphaned.
	SkippedReconciliationsCounter = "skipped_reconciliations"
)

Variables

View Source
var (
	ErrDatabaseOpenFailed         = errors.New("unable to open database")
	ErrCompressorLoadFailed       = errors.New("unable to load compressor")
	ErrDBCloseFailed              = errors.New("unable to close database")
	ErrCommitFailed               = errors.New("unable to commit transaction")
	ErrScanGetValueFailed         = errors.New("unable to get value for key")
	ErrScanWorkerFailed           = errors.New("worker failed")
	ErrDecompressFailed           = errors.New("unable to decompress")
	ErrDecompressSaveUnsuccessful = errors.New("unable to store decompressed file")
	ErrLoadFileUnsuccessful       = errors.New("unable to load file")
	ErrCompressNormalFailed       = errors.New("unable to compress normal")
	ErrCompressWithDictFailed     = errors.New("unable to compress with dictionary")
	ErrDecompressWithDictFailed   = errors.New("unable to decompress with dictionary")
	ErrDecompressOutputMismatch   = errors.New("decompressed dictionary output does not match")
	ErrRecompressFailed           = errors.New("unable to recompress")
	ErrCreateTempDirectoryFailed  = errors.New("unable to create temporary directory")
	ErrMaxEntries                 = errors.New("max entries reached")
	ErrScanFailed                 = errors.New("unable to scan")
	ErrNoEntriesFoundInNamespace  = errors.New("found 0 entries for namespace")
	ErrInvokeZSTDFailed           = errors.New("unable to start zstd")
	ErrTrainZSTDFailed            = errors.New("unable to train zstd")
	ErrWalkFilesFailed            = errors.New("unable to walk files")

	BadgerStorageErrs = []error{
		ErrDatabaseOpenFailed,
		ErrCompressorLoadFailed,
		ErrDBCloseFailed,
		ErrCommitFailed,
		ErrScanGetValueFailed,
		ErrScanWorkerFailed,
		ErrDecompressFailed,
		ErrDecompressSaveUnsuccessful,
		ErrLoadFileUnsuccessful,
		ErrCompressNormalFailed,
		ErrCompressWithDictFailed,
		ErrDecompressWithDictFailed,
		ErrDecompressOutputMismatch,
		ErrRecompressFailed,
		ErrCreateTempDirectoryFailed,
		ErrMaxEntries,
		ErrScanFailed,
		ErrNoEntriesFoundInNamespace,
		ErrInvokeZSTDFailed,
		ErrTrainZSTDFailed,
		ErrWalkFilesFailed,
	}
)

Badger Storage Errors

View Source
var (
	ErrBroadcastTxStale     = errors.New("unable to handle stale transaction")
	ErrBroadcastTxConfirmed = errors.New(
		"unable to handle confirmed transaction",
	)
	ErrBroadcastFindTxFailed = errors.New(
		"unable to determine if transaction was seen",
	)
	ErrBroadcastEncodeUpdateFailed        = errors.New("unable to encode updated broadcast")
	ErrBroadcastUpdateFailed              = errors.New("unable to update broadcast")
	ErrBroadcastDeleteConfirmedTxFailed   = errors.New("unable to delete confirmed broadcast")
	ErrBroadcastInvokeBlockHandlersFailed = errors.New("unable to handle block")
	ErrBroadcastFailed                    = errors.New(
		"unable to broadcast pending transactions",
	)
	ErrBroadcastDBGetFailed = errors.New(
		"unable to determine if already broadcasting transaction",
	)
	ErrBroadcastAlreadyExists      = errors.New("already broadcasting transaction")
	ErrBroadcastEncodeFailed       = errors.New("unable to encode broadcast")
	ErrBroadcastSetFailed          = errors.New("unable to set broadcast")
	ErrBroadcastScanFailed         = errors.New("unable to scan for all broadcasts")
	ErrBroadcastDecodeFailed       = errors.New("unable to decode broadcast")
	ErrBroadcastCommitUpdateFailed = errors.New("unable to commit broadcast update")
	ErrBroadcastIdentifierMismatch = errors.New(
		"unexpected transaction hash returned by broadcast",
	)
	ErrBroadcastGetCurrentBlockIdentifierFailed = errors.New(
		"unable to get current block identifier",
	)
	ErrBroadcastAtTipFailed               = errors.New("unable to determine if at tip")
	ErrBroadcastGetAllFailed              = errors.New("unable to get all broadcasts")
	ErrBroadcastDeleteFailed              = errors.New("unable to delete broadcast")
	ErrBroadcastHandleFailureUnsuccessful = errors.New("unable to handle broadcast failure")
	ErrBroadcastCommitDeleteFailed        = errors.New("unable to commit broadcast delete")
	ErrBroadcastPerformFailed             = errors.New("unable to perform broadcast")

	BroadcastStorageErrs = []error{
		ErrBroadcastTxStale,
		ErrBroadcastTxConfirmed,
		ErrBroadcastFindTxFailed,
		ErrBroadcastEncodeUpdateFailed,
		ErrBroadcastUpdateFailed,
		ErrBroadcastDeleteConfirmedTxFailed,
		ErrBroadcastInvokeBlockHandlersFailed,
		ErrBroadcastFailed,
		ErrBroadcastDBGetFailed,
		ErrBroadcastAlreadyExists,
		ErrBroadcastEncodeFailed,
		ErrBroadcastSetFailed,
		ErrBroadcastScanFailed,
		ErrBroadcastDecodeFailed,
		ErrBroadcastCommitUpdateFailed,
		ErrBroadcastIdentifierMismatch,
		ErrBroadcastGetCurrentBlockIdentifierFailed,
		ErrBroadcastAtTipFailed,
		ErrBroadcastGetAllFailed,
		ErrBroadcastDeleteFailed,
		ErrBroadcastHandleFailureUnsuccessful,
		ErrBroadcastCommitDeleteFailed,
		ErrBroadcastPerformFailed,
	}
)

Broadcast Storage Errors

View Source
var (
	ErrCoinQueryFailed                  = errors.New("unable to query for coin")
	ErrCoinDecodeFailed                 = errors.New("unable to decode coin")
	ErrCoinGetFailed                    = errors.New("unable to get coin")
	ErrCoinAddFailed                    = errors.New("unable to add coin")
	ErrReconciliationUpdateCommitFailed = errors.New("unable to commit last reconciliation update")
	ErrCoinDataEncodeFailed             = errors.New("unable to encode coin data")
	ErrCoinStoreFailed                  = errors.New("unable to store coin")
	ErrAccountCoinStoreFailed           = errors.New("unable to store account coin")
	ErrAccountCoinQueryFailed           = errors.New("unable to query coins for account")
	ErrCoinDeleteFailed                 = errors.New("unable to delete coin")
	ErrOperationParseFailed             = errors.New("unable to parse operation success")
	ErrUnableToDetermineIfSkipOperation = errors.New(
		"unable to to determine if should skip operation",
	)
	ErrDuplicateCoinFound           = errors.New("duplicate coin found")
	ErrCoinRemoveFailed             = errors.New("unable to remove coin")
	ErrAccountIdentifierQueryFailed = errors.New("unable to query account identifier")
	ErrCurrentBlockGetFailed        = errors.New("unable to get current block identifier")
	ErrCoinLookupFailed             = errors.New("unable to lookup coin")
	ErrUTXOBalanceGetFailed         = errors.New("unable to get utxo balance")
	ErrCoinParseFailed              = errors.New("unable to parse amount for coin")
	ErrCoinImportFailed             = errors.New("unable to import coins")

	CoinStorageErrs = []error{
		ErrCoinQueryFailed,
		ErrCoinDecodeFailed,
		ErrCoinGetFailed,
		ErrCoinAddFailed,
		ErrReconciliationUpdateCommitFailed,
		ErrCoinDataEncodeFailed,
		ErrCoinStoreFailed,
		ErrAccountCoinStoreFailed,
		ErrAccountCoinQueryFailed,
		ErrCoinDeleteFailed,
		ErrOperationParseFailed,
		ErrUnableToDetermineIfSkipOperation,
		ErrDuplicateCoinFound,
		ErrCoinRemoveFailed,
		ErrAccountIdentifierQueryFailed,
		ErrCurrentBlockGetFailed,
		ErrCoinLookupFailed,
		ErrUTXOBalanceGetFailed,
		ErrCoinParseFailed,
		ErrCoinImportFailed,
	}
)

Coin Storage Errors

View Source
var (
	ErrLoadDictFailed      = errors.New("unable to load dictionary")
	ErrObjectEncodeFailed  = errors.New("unable to encode object")
	ErrRawCompressFailed   = errors.New("unable to compress raw bytes")
	ErrRawDecompressFailed = errors.New("unable to decompress raw bytes")
	ErrRawDecodeFailed     = errors.New("unable to decode bytes")
	ErrBufferWriteFailed   = errors.New("unable to write to buffer")
	ErrWriterCloseFailed   = errors.New("unable to close writer")
	ErrObjectDecodeFailed  = errors.New("unable to decode object")
	ErrReaderCloseFailed   = errors.New("unable to close reader")
	ErrCopyBlockFailed     = errors.New("unable to copy block")

	CompressorErrs = []error{
		ErrLoadDictFailed,
		ErrObjectEncodeFailed,
		ErrRawCompressFailed,
		ErrRawDecompressFailed,
		ErrRawDecodeFailed,
		ErrBufferWriteFailed,
		ErrWriterCloseFailed,
		ErrObjectDecodeFailed,
		ErrReaderCloseFailed,
		ErrCopyBlockFailed,
	}
)

Compressor Errors

View Source
var (
	ErrJobsGetAllFailed              = errors.New("unable to get all jobs")
	ErrJobIdentifierDecodeFailed     = errors.New("unable to decode existing identifier")
	ErrJobGetFailed                  = errors.New("unable to get job")
	ErrJobIdentifierEncodeFailed     = errors.New("unable to encode job identifier")
	ErrJobIdentifierUpdateFailed     = errors.New("unable to update job identifier")
	ErrJobIdentifiersEncodeAllFailed = errors.New("unable to encode identifiers")
	ErrJobIdentifiersSetAllFailed    = errors.New("unable to set identifiers")
	ErrJobIdentifierRemoveFailed     = errors.New("unable to remove identifier")
	ErrJobIdentifierNotFound         = errors.New("identifier not found")
	ErrJobRemoveFailed               = errors.New("unable to remove job")
	ErrJobAddFailed                  = errors.New("unable to add job")
	ErrJobIdentifierGetFailed        = errors.New("unable to get next identifier")
	ErrJobUpdateOldFailed            = errors.New("unable to update terminal job")
	ErrJobEncodeFailed               = errors.New("unable to encode job")
	ErrJobUpdateFailed               = errors.New("unable to update job")
	ErrJobMetadataUpdateFailed       = errors.New("unable to update metadata")
	ErrJobDoesNotExist               = errors.New("job does not exist")
	ErrJobDecodeFailed               = errors.New("unable to decode job")

	JobStorageErrs = []error{
		ErrJobsGetAllFailed,
		ErrJobIdentifierDecodeFailed,
		ErrJobGetFailed,
		ErrJobIdentifierEncodeFailed,
		ErrJobIdentifierUpdateFailed,
		ErrJobIdentifiersEncodeAllFailed,
		ErrJobIdentifiersSetAllFailed,
		ErrJobIdentifierRemoveFailed,
		ErrJobIdentifierNotFound,
		ErrJobRemoveFailed,
		ErrJobAddFailed,
		ErrJobIdentifierGetFailed,
		ErrJobUpdateOldFailed,
		ErrJobEncodeFailed,
		ErrJobUpdateFailed,
		ErrJobMetadataUpdateFailed,
		ErrJobDoesNotExist,
		ErrJobDecodeFailed,
	}
)

Job Storage Errors

View Source
var (
	// ErrAddrExists is returned when key storage already
	// contains an address.
	ErrAddrExists = errors.New("address already exists")

	ErrAddrCheckIfExistsFailed  = errors.New("unable to check if address exists")
	ErrSerializeKeyFailed       = errors.New("unable to serialize key")
	ErrStoreKeyFailed           = errors.New("unable to store key")
	ErrCommitKeyFailed          = errors.New("unable to commit new key to db")
	ErrAddrGetFailed            = errors.New("unable to get address")
	ErrAddrNotFound             = errors.New("address not found")
	ErrParseSavedKeyFailed      = errors.New("unable to parse saved key")
	ErrKeyScanFailed            = errors.New("database scan for keys failed")
	ErrParseKeyPairFailed       = errors.New("unable to parse key pair")
	ErrKeyGetFailed             = errors.New("unable to get key")
	ErrSignerCreateFailed       = errors.New("unable to create signer")
	ErrDetermineSigTypeFailed   = errors.New("cannot determine signature type for payload")
	ErrSignPayloadFailed        = errors.New("unable to to sign payload")
	ErrAddrsGetAllFailed        = errors.New("unable to get addresses")
	ErrNoAddrAvailable          = errors.New("no addresses available")
	ErrAddrImportFailed         = errors.New("unable to import prefunded account")
	ErrPrefundedAcctStoreFailed = errors.New("unable to store prefunded account")

	KeyStorageErrs = []error{
		ErrAddrExists,
		ErrAddrCheckIfExistsFailed,
		ErrSerializeKeyFailed,
		ErrStoreKeyFailed,
		ErrCommitKeyFailed,
		ErrAddrGetFailed,
		ErrAddrNotFound,
		ErrParseSavedKeyFailed,
		ErrKeyScanFailed,
		ErrParseKeyPairFailed,
		ErrKeyGetFailed,
		ErrSignerCreateFailed,
		ErrDetermineSigTypeFailed,
		ErrSignPayloadFailed,
		ErrAddrsGetAllFailed,
		ErrNoAddrAvailable,
		ErrAddrImportFailed,
		ErrPrefundedAcctStoreFailed,
	}
)

Key Storage Errors

View Source
var (
	// ErrNegativeBalance is returned when an account
	// balance goes negative as the result of an operation.
	ErrNegativeBalance = errors.New("negative balance")

	// ErrInvalidLiveBalance is returned when an account's
	// live balance varies in a way that is inconsistent
	// with any balance exemption.
	ErrInvalidLiveBalance = errors.New("invalid live balance")

	// ErrBalancePruned is returned when the caller attempts
	// to retrieve a pruned balance.
	ErrBalancePruned = errors.New("balance pruned")

	// ErrBlockNil is returned when the block to lookup
	// a balance at is nil.
	ErrBlockNil = errors.New("block nil")

	// ErrAccountMissing is returned when a fetched
	// account does not exist.
	ErrAccountMissing = errors.New("block nil")

	BalanceStorageErrs = []error{
		ErrNegativeBalance,
		ErrInvalidLiveBalance,
		ErrBalancePruned,
		ErrBlockNil,
		ErrAccountMissing,
	}
)

Balance Storage Errors

View Source
var (
	// ErrHeadBlockNotFound is returned when there is no
	// head block found in BlockStorage.
	ErrHeadBlockNotFound = errors.New("head block not found")

	// ErrBlockNotFound is returned when a block is not
	// found in BlockStorage.
	ErrBlockNotFound = errors.New("block not found")

	// ErrDuplicateKey is returned when a key
	// cannot be stored because it is a duplicate.
	ErrDuplicateKey = errors.New("duplicate key")

	// ErrDuplicateTransactionHash is returned when a transaction
	// hash cannot be stored because it is a duplicate.
	ErrDuplicateTransactionHash = errors.New("duplicate transaction hash")

	ErrBlockGetFailed                  = errors.New("unable to get block")
	ErrTransactionGetFailed            = errors.New("could not get transaction")
	ErrBlockEncodeFailed               = errors.New("unable to encode block")
	ErrBlockStoreFailed                = errors.New("unable to store block")
	ErrBlockIndexStoreFailed           = errors.New("unable to store block index")
	ErrBlockIdentifierUpdateFailed     = errors.New("unable to update head block identifier")
	ErrBlockCopyFailed                 = errors.New("unable to copy block")
	ErrTransactionHashStoreFailed      = errors.New("unable to store transaction hash")
	ErrBlockDeleteFailed               = errors.New("unable to delete block")
	ErrBlockIndexDeleteFailed          = errors.New("unable to delete block index")
	ErrHeadBlockIdentifierUpdateFailed = errors.New("unable to update head block identifier")
	ErrLastProcessedBlockPrecedesStart = errors.New(
		"last processed block is less than start index",
	)
	ErrTransactionHashContentsDecodeFailed = errors.New(
		"could not decode transaction hash contents",
	)
	ErrTransactionDataEncodeFailed = errors.New("unable to encode transaction data")
	ErrTransactionDeleteFailed     = errors.New("could not remove transaction")
	ErrTransactionHashNotFound     = errors.New(
		"saved blocks at transaction does not contain transaction hash",
	)
	ErrTransactionDBQueryFailed = errors.New("unable to query database for transaction")
	ErrBlockDataDecodeFailed    = errors.New(
		"unable to decode block data for transaction",
	)
	ErrTransactionNotFound            = errors.New("unable to find transaction")
	ErrTransactionDoesNotExistInBlock = errors.New("transaction does not exist in block")
	ErrHeadBlockGetFailed             = errors.New("unable to get head block")
	ErrOldestIndexUpdateFailed        = errors.New("oldest index update failed")
	ErrOldestIndexMissing             = errors.New("oldest index missing")
	ErrOldestIndexRead                = errors.New("cannot read oldest index")
	ErrCannotRemoveOldest             = errors.New("cannot remove oldest index")
	ErrCannotAccessPrunedData         = errors.New("cannot access pruned data")
	ErrNothingToPrune                 = errors.New("nothing to prune")
	ErrPruningFailed                  = errors.New("pruning failed")
	ErrCannotPruneTransaction         = errors.New("cannot prune transaction")

	BlockStorageErrs = []error{
		ErrHeadBlockNotFound,
		ErrBlockNotFound,
		ErrDuplicateKey,
		ErrDuplicateTransactionHash,
		ErrBlockGetFailed,
		ErrTransactionGetFailed,
		ErrBlockEncodeFailed,
		ErrBlockStoreFailed,
		ErrBlockIndexStoreFailed,
		ErrBlockIdentifierUpdateFailed,
		ErrBlockCopyFailed,
		ErrTransactionHashStoreFailed,
		ErrBlockDeleteFailed,
		ErrBlockIndexDeleteFailed,
		ErrHeadBlockIdentifierUpdateFailed,
		ErrLastProcessedBlockPrecedesStart,
		ErrTransactionHashContentsDecodeFailed,
		ErrTransactionDataEncodeFailed,
		ErrTransactionDeleteFailed,
		ErrTransactionHashNotFound,
		ErrTransactionDBQueryFailed,
		ErrBlockDataDecodeFailed,
		ErrTransactionNotFound,
		ErrTransactionDoesNotExistInBlock,
		ErrHeadBlockGetFailed,
		ErrOldestIndexUpdateFailed,
		ErrOldestIndexMissing,
		ErrOldestIndexRead,
		ErrCannotRemoveOldest,
		ErrCannotAccessPrunedData,
		ErrNothingToPrune,
		ErrPruningFailed,
		ErrCannotPruneTransaction,
	}
)

Block Storage Errors

View Source
var (
	// ErrCoinNotFound is returned when a coin is not found
	// in CoinStorage.
	ErrCoinNotFound = errors.New("coin not found")
)

Functions

func BadgerTrain

func BadgerTrain(
	ctx context.Context,
	namespace string,
	db string,
	output string,
	maxEntries int,
	compressorEntries []*CompressorEntry,
) (float64, float64, error)

BadgerTrain creates a zstd dictionary for a given BadgerStorage DB namespace. Optionally, you can specify the maximum number of entries to load into storage (if -1 is provided, then all possible are loaded).

func DefaultBadgerOptions

func DefaultBadgerOptions(dir string) badger.Options

DefaultBadgerOptions are the default options used to initialized a new BadgerDB. These settings override many of the default BadgerDB settings to restrict memory usage to ~6 GB. If constraining memory usage is not desired for your use case, you can provide your own BadgerDB settings with the configuration option WithCustomSettings.

There are many threads about optimizing memory usage in Badger (which can grow to many GBs if left untuned). Our own research indicates that each MB increase in MaxTableSize and/or ValueLogFileSize corresponds to a 10 MB increase in RAM usage (all other settings equal). Our primary concern is large database transaction size, so we configure MaxTableSize to be 4 times the size of ValueLogFileSize (if we skewed any further to MaxTableSize, we would quickly hit the default open file limit on many OSes).

func Err

func Err(err error) (bool, string)

Err takes an error as an argument and returns whether or not the error is one thrown by the storage along with the specific source of the error

func GetAccountKey

func GetAccountKey(account *types.AccountIdentifier, currency *types.Currency) []byte

GetAccountKey returns a deterministic hash of a types.Account + types.Currency.

func GetHistoricalBalanceKey

func GetHistoricalBalanceKey(
	account *types.AccountIdentifier,
	currency *types.Currency,
	blockIndex int64,
) []byte

GetHistoricalBalanceKey returns a deterministic hash of a types.Account + types.Currency + block index.

func GetHistoricalBalancePrefix

func GetHistoricalBalancePrefix(account *types.AccountIdentifier, currency *types.Currency) []byte

GetHistoricalBalancePrefix returns a deterministic hash of a types.Account + types.Currency to limit scan results.

func PerformanceBadgerOptions

func PerformanceBadgerOptions(dir string) badger.Options

PerformanceBadgerOptions are performance geared BadgerDB options that use much more RAM than the default settings.

Types

type AccountCoin

type AccountCoin struct {
	Account *types.AccountIdentifier `json:"account"`
	Coin    *types.Coin              `json:"coin"`
}

AccountCoin contains an AccountIdentifier and a Coin that it owns

type BadgerOption

type BadgerOption func(b *BadgerStorage)

BadgerOption is used to overwrite default values in BadgerStorage construction. Any Option not provided falls back to the default value.

func WithCompressorEntries

func WithCompressorEntries(entries []*CompressorEntry) BadgerOption

WithCompressorEntries provides zstd dictionaries for given namespaces.

func WithCustomSettings

func WithCustomSettings(settings badger.Options) BadgerOption

WithCustomSettings allows for overriding all default BadgerDB options with custom settings.

func WithIndexCacheSize

func WithIndexCacheSize(size int64) BadgerOption

WithIndexCacheSize override the DefaultIndexCacheSize setting for the BadgerDB. The size here is in bytes. If you provide custom BadgerDB settings, do not use this config as it will be overridden by your custom settings.

func WithoutCompression

func WithoutCompression() BadgerOption

WithoutCompression disables zstd compression.

type BadgerStorage

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

BadgerStorage is a wrapper around Badger DB that implements the Database interface.

func (*BadgerStorage) Close

func (b *BadgerStorage) Close(ctx context.Context) error

Close closes the database to prevent corruption. The caller should defer this in main.

func (*BadgerStorage) Encoder

func (b *BadgerStorage) Encoder() *Encoder

Encoder returns the BadgerStorage encoder.

func (*BadgerStorage) NewDatabaseTransaction

func (b *BadgerStorage) NewDatabaseTransaction(
	ctx context.Context,
	write bool,
) DatabaseTransaction

NewDatabaseTransaction creates a new BadgerTransaction. If the transaction will not modify any values, pass in false for the write parameter (this allows for optimization within the Badger DB).

type BadgerTransaction

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

BadgerTransaction is a wrapper around a Badger DB transaction that implements the DatabaseTransaction interface.

func (*BadgerTransaction) Commit

Commit attempts to commit and discard the transaction.

func (*BadgerTransaction) Delete

func (b *BadgerTransaction) Delete(ctx context.Context, key []byte) error

Delete removes the key and its value within the transaction.

func (*BadgerTransaction) Discard

func (b *BadgerTransaction) Discard(context.Context)

Discard discards an open transaction. All transactions must be either discarded or committed.

func (*BadgerTransaction) Get

func (b *BadgerTransaction) Get(
	ctx context.Context,
	key []byte,
) (bool, []byte, error)

Get accesses the value of the key within a transaction. It is up to the caller to reclaim any memory returned.

func (*BadgerTransaction) Scan

func (b *BadgerTransaction) Scan(
	ctx context.Context,
	prefix []byte,
	seekStart []byte,
	worker func([]byte, []byte) error,
	logEntries bool,
	reverse bool,
) (int, error)

Scan calls a worker for each item in a scan instead of reading all items into memory.

func (*BadgerTransaction) ScanMulti added in v0.6.24

func (b *BadgerTransaction) ScanMulti(
	ctx context.Context,
	changes []*parser.BalanceChange,
	logEntries bool,
	reverse bool,
	GetHistoricalBalancePrefix func(account *types.AccountIdentifier, currency *types.Currency) []byte,
	GetHistoricalBalanceKey func(account *types.AccountIdentifier, currency *types.Currency, blockIndex int64) []byte,
) []string

Scan calls a worker for each item in a scan instead of reading all items into memory.

func (*BadgerTransaction) Set

func (b *BadgerTransaction) Set(
	ctx context.Context,
	key []byte,
	value []byte,
	reclaimValue bool,
) error

Set changes the value of the key to the value within a transaction.

type BalanceStorage

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

BalanceStorage implements block specific storage methods on top of a Database and DatabaseTransaction interface.

func NewBalanceStorage

func NewBalanceStorage(
	db Database,
) *BalanceStorage

NewBalanceStorage returns a new BalanceStorage.

func (*BalanceStorage) AddingBlock

func (b *BalanceStorage) AddingBlock(
	ctx context.Context,
	block *types.Block,
	transaction DatabaseTransaction,
) (CommitWorker, error)

AddingBlock is called by BlockStorage when adding a block to storage.

func (*BalanceStorage) BootstrapBalances

func (b *BalanceStorage) BootstrapBalances(
	ctx context.Context,
	bootstrapBalancesFile string,
	genesisBlockIdentifier *types.BlockIdentifier,
) error

BootstrapBalances is utilized to set the balance of any number of AccountIdentifiers at the genesis blocks. This is particularly useful for setting the value of accounts that received an allocation in the genesis block.

func (*BalanceStorage) GetAllAccountCurrency

func (b *BalanceStorage) GetAllAccountCurrency(
	ctx context.Context,
) ([]*types.AccountCurrency, error)

GetAllAccountCurrency scans the db for all balances and returns a slice of reconciler.AccountCurrency. This is useful for bootstrapping the reconciler after restart.

func (*BalanceStorage) GetBalance

func (b *BalanceStorage) GetBalance(
	ctx context.Context,
	account *types.AccountIdentifier,
	currency *types.Currency,
	index int64,
) (*types.Amount, error)

GetBalance returns the balance of a types.AccountIdentifier at the canonical block of a certain index.

func (*BalanceStorage) GetBalanceTransactional

func (b *BalanceStorage) GetBalanceTransactional(
	ctx context.Context,
	dbTx DatabaseTransaction,
	account *types.AccountIdentifier,
	currency *types.Currency,
	index int64,
) (*types.Amount, error)

GetBalanceTransactional returns all the balances of a types.AccountIdentifier and the types.BlockIdentifier it was last updated at in a database transaction.

func (*BalanceStorage) GetOrSetBalance

func (b *BalanceStorage) GetOrSetBalance(
	ctx context.Context,
	account *types.AccountIdentifier,
	currency *types.Currency,
	block *types.BlockIdentifier,
) (*types.Amount, error)

GetOrSetBalance returns the balance of a types.AccountIdentifier at the canonical block of a certain index, setting it if it doesn't exist.

func (*BalanceStorage) GetOrSetBalanceTransactional

func (b *BalanceStorage) GetOrSetBalanceTransactional(
	ctx context.Context,
	dbTx DatabaseTransaction,
	account *types.AccountIdentifier,
	currency *types.Currency,
	block *types.BlockIdentifier,
) (*types.Amount, error)

GetOrSetBalanceTransactional returns the balance of a types.AccountIdentifier at the canonical block of a certain index, setting it if it doesn't exist.

func (*BalanceStorage) Initialize

func (b *BalanceStorage) Initialize(
	helper BalanceStorageHelper,
	handler BalanceStorageHandler,
)

Initialize adds a BalanceStorageHelper and BalanceStorageHandler to BalanceStorage. This must be called prior to syncing!

func (*BalanceStorage) OrphanBalance

func (b *BalanceStorage) OrphanBalance(
	ctx context.Context,
	dbTransaction DatabaseTransaction,
	account *types.AccountIdentifier,
	currency *types.Currency,
	block *types.BlockIdentifier,
) error

OrphanBalance removes all saved states for a *types.Account and *types.Currency at blocks >= the provided block.

func (*BalanceStorage) PruneBalances

func (b *BalanceStorage) PruneBalances(
	ctx context.Context,
	account *types.AccountIdentifier,
	currency *types.Currency,
	index int64,
) error

PruneBalances removes all historical balance states <= some index. This can significantly reduce storage usage in scenarios where historical balances are only retrieved once (like reconciliation).

func (*BalanceStorage) Reconciled

func (b *BalanceStorage) Reconciled(
	ctx context.Context,
	account *types.AccountIdentifier,
	currency *types.Currency,
	block *types.BlockIdentifier,
) error

Reconciled updates the LastReconciled field on a particular balance. Tracking reconciliation coverage is an important end condition.

func (*BalanceStorage) ReconciliationCoverage

func (b *BalanceStorage) ReconciliationCoverage(
	ctx context.Context,
	minimumIndex int64,
) (float64, error)

ReconciliationCoverage returns the proportion of accounts [0.0, 1.0] that have been reconciled at an index >= to a minimumIndex.

func (*BalanceStorage) RemovingBlock

func (b *BalanceStorage) RemovingBlock(
	ctx context.Context,
	block *types.Block,
	transaction DatabaseTransaction,
) (CommitWorker, error)

RemovingBlock is called by BlockStorage when removing a block from storage.

func (*BalanceStorage) SetBalance

func (b *BalanceStorage) SetBalance(
	ctx context.Context,
	dbTransaction DatabaseTransaction,
	account *types.AccountIdentifier,
	amount *types.Amount,
	block *types.BlockIdentifier,
) error

SetBalance allows a client to set the balance of an account in a database transaction (removing all historical states). This is particularly useful for bootstrapping balances.

func (*BalanceStorage) SetBalanceImported

func (b *BalanceStorage) SetBalanceImported(
	ctx context.Context,
	helper BalanceStorageHelper,
	accountBalances []*utils.AccountBalance,
) error

SetBalanceImported sets the balances of a set of addresses by getting their balances from the tip block, and populating the database. This is used when importing prefunded addresses.

func (*BalanceStorage) UpdateBalance

func (b *BalanceStorage) UpdateBalance(
	ctx context.Context,
	dbTransaction DatabaseTransaction,
	change *parser.BalanceChange,
	parentBlock *types.BlockIdentifier,
) error

UpdateBalance updates a types.AccountIdentifer by a types.Amount and sets the account's most recent accessed block.

func (*BalanceStorage) UpdateBalances added in v0.6.24

func (b *BalanceStorage) UpdateBalances(
	ctx context.Context,
	dbTransaction DatabaseTransaction,
	changes []*parser.BalanceChange,
	parentBlock *types.BlockIdentifier,
) error

type BalanceStorageHandler

type BalanceStorageHandler interface {
	BlockAdded(ctx context.Context, block *types.Block, changes []*parser.BalanceChange) error
	BlockRemoved(ctx context.Context, block *types.Block, changes []*parser.BalanceChange) error
}

BalanceStorageHandler is invoked after balance changes are committed to the database.

type BalanceStorageHelper

type BalanceStorageHelper interface {
	AccountBalance(
		ctx context.Context,
		account *types.AccountIdentifier,
		currency *types.Currency,
		block *types.BlockIdentifier,
	) (*types.Amount, error)

	ExemptFunc() parser.ExemptOperation
	BalanceExemptions() []*types.BalanceExemption
	Asserter() *asserter.Asserter
}

BalanceStorageHelper functions are used by BalanceStorage to process balances. Defining an interface allows the client to determine if they wish to query the node for certain information or use another datastore.

type BlockStorage

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

BlockStorage implements block specific storage methods on top of a Database and DatabaseTransaction interface.

func NewBlockStorage

func NewBlockStorage(
	db Database,
) *BlockStorage

NewBlockStorage returns a new BlockStorage.

func (*BlockStorage) AddBlock

func (b *BlockStorage) AddBlock(
	ctx context.Context,
	block *types.Block,
) error

AddBlock stores a block or returns an error.

func (*BlockStorage) AtTip

func (b *BlockStorage) AtTip(
	ctx context.Context,
	tipDelay int64,
) (bool, *types.BlockIdentifier, error)

AtTip returns a boolean indicating if we are at tip (provided some acceptable tip delay).

func (*BlockStorage) AtTipTransactional

func (b *BlockStorage) AtTipTransactional(
	ctx context.Context,
	tipDelay int64,
	txn DatabaseTransaction,
) (bool, *types.BlockIdentifier, error)

AtTipTransactional returns a boolean indicating if we are at tip (provided some acceptable tip delay) in a database transaction.

func (*BlockStorage) CanonicalBlock

func (b *BlockStorage) CanonicalBlock(
	ctx context.Context,
	blockIdentifier *types.BlockIdentifier,
) (bool, error)

CanonicalBlock returns a boolean indicating if a block with the provided *types.BlockIdentifier is in the canonical chain (regardless if it has been pruned).

func (*BlockStorage) CanonicalBlockTransactional

func (b *BlockStorage) CanonicalBlockTransactional(
	ctx context.Context,
	blockIdentifier *types.BlockIdentifier,
	dbTx DatabaseTransaction,
) (bool, error)

CanonicalBlockTransactional returns a boolean indicating if a block with the provided *types.BlockIdentifier is in the canonical chain (regardless if it has been pruned) in a single storage.DatabaseTransaction.

func (*BlockStorage) CreateBlockCache

func (b *BlockStorage) CreateBlockCache(ctx context.Context, blocks int) []*types.BlockIdentifier

CreateBlockCache populates a slice of blocks with the most recent ones in storage.

func (*BlockStorage) FindTransaction

func (b *BlockStorage) FindTransaction(
	ctx context.Context,
	transactionIdentifier *types.TransactionIdentifier,
	txn DatabaseTransaction,
) (*types.BlockIdentifier, *types.Transaction, error)

FindTransaction returns the most recent *types.BlockIdentifier containing the transaction and the transaction.

func (*BlockStorage) GetBlock

func (b *BlockStorage) GetBlock(
	ctx context.Context,
	blockIdentifier *types.PartialBlockIdentifier,
) (*types.Block, error)

GetBlock returns a block, if it exists. GetBlock will fetch all transactions contained in a block automatically. If you don't wish to do this for performance reasons, use GetBlockLazy.

func (*BlockStorage) GetBlockLazy

func (b *BlockStorage) GetBlockLazy(
	ctx context.Context,
	blockIdentifier *types.PartialBlockIdentifier,
) (*types.BlockResponse, error)

GetBlockLazy returns a *types.BlockResponse with populated OtherTransactions array containing all the transactions the caller must retrieve. This is typically used to serve /block queries.

func (*BlockStorage) GetBlockLazyTransactional

func (b *BlockStorage) GetBlockLazyTransactional(
	ctx context.Context,
	blockIdentifier *types.PartialBlockIdentifier,
	transaction DatabaseTransaction,
) (*types.BlockResponse, error)

GetBlockLazyTransactional returns a *types.BlockResponse with populated OtherTransactions array containing all the transactions the caller must retrieve in a provided database transaction.

func (*BlockStorage) GetBlockTransaction

func (b *BlockStorage) GetBlockTransaction(
	ctx context.Context,
	blockIdentifier *types.BlockIdentifier,
	transactionIdentifier *types.TransactionIdentifier,
) (*types.Transaction, error)

GetBlockTransaction retrieves a transaction belonging to a certain block in a database transaction. This is usually used to implement /block/transaction.

func (*BlockStorage) GetBlockTransactional

func (b *BlockStorage) GetBlockTransactional(
	ctx context.Context,
	dbTx DatabaseTransaction,
	blockIdentifier *types.PartialBlockIdentifier,
) (*types.Block, error)

GetBlockTransactional gets a block in the context of a database transaction.

func (*BlockStorage) GetHeadBlockIdentifier

func (b *BlockStorage) GetHeadBlockIdentifier(
	ctx context.Context,
) (*types.BlockIdentifier, error)

GetHeadBlockIdentifier returns the head block identifier, if it exists.

func (*BlockStorage) GetHeadBlockIdentifierTransactional

func (b *BlockStorage) GetHeadBlockIdentifierTransactional(
	ctx context.Context,
	transaction DatabaseTransaction,
) (*types.BlockIdentifier, error)

GetHeadBlockIdentifierTransactional returns the head block identifier, if it exists, in the context of a DatabaseTransaction.

func (*BlockStorage) GetOldestBlockIndex

func (b *BlockStorage) GetOldestBlockIndex(
	ctx context.Context,
) (int64, error)

GetOldestBlockIndex returns the oldest block index available in BlockStorage.

func (*BlockStorage) GetOldestBlockIndexTransactional

func (b *BlockStorage) GetOldestBlockIndexTransactional(
	ctx context.Context,
	dbTx DatabaseTransaction,
) (int64, error)

GetOldestBlockIndexTransactional returns the oldest block index available in BlockStorage in a single database transaction.

func (*BlockStorage) IndexAtTip

func (b *BlockStorage) IndexAtTip(
	ctx context.Context,
	tipDelay int64,
	index int64,
) (bool, error)

IndexAtTip returns a boolean indicating if a block index is at tip (provided some acceptable tip delay). If the index is ahead of the head block and the head block is at tip, we consider the index at tip.

func (*BlockStorage) Initialize

func (b *BlockStorage) Initialize(workers []BlockWorker)

Initialize adds a []BlockWorker to BlockStorage. Usually all block workers are not created by the time block storage is constructed.

This must be called prior to syncing!

func (*BlockStorage) Prune

func (b *BlockStorage) Prune(
	ctx context.Context,
	index int64,
	minDepth int64,
) (int64, int64, error)

Prune removes block and transaction data from all blocks with index <= index. Pruning leaves all keys associated with pruned data but overwrites their data to be empty. If pruning is successful, we return the range of pruned blocks.

Prune is not invoked automatically because some applications prefer not to prune any block data.

func (*BlockStorage) RemoveBlock

func (b *BlockStorage) RemoveBlock(
	ctx context.Context,
	blockIdentifier *types.BlockIdentifier,
) error

RemoveBlock removes a block or returns an error. RemoveBlock also removes the block hash and all its transaction hashes to not break duplicate detection. This is called within a re-org.

func (*BlockStorage) SetNewStartIndex

func (b *BlockStorage) SetNewStartIndex(
	ctx context.Context,
	startIndex int64,
) error

SetNewStartIndex attempts to remove all blocks greater than or equal to the startIndex.

func (*BlockStorage) StoreHeadBlockIdentifier

func (b *BlockStorage) StoreHeadBlockIdentifier(
	ctx context.Context,
	transaction DatabaseTransaction,
	blockIdentifier *types.BlockIdentifier,
) error

StoreHeadBlockIdentifier stores a block identifier or returns an error.

type BlockWorker

type BlockWorker interface {
	AddingBlock(context.Context, *types.Block, DatabaseTransaction) (CommitWorker, error)
	RemovingBlock(context.Context, *types.Block, DatabaseTransaction) (CommitWorker, error)
}

BlockWorker is an interface that allows for work to be done while a block is added/removed from storage in the same database transaction as the change.

type BootstrapBalance

type BootstrapBalance struct {
	Account  *types.AccountIdentifier `json:"account_identifier,omitempty"`
	Currency *types.Currency          `json:"currency,omitempty"`
	Value    string                   `json:"value,omitempty"`
}

BootstrapBalance represents a balance of a *types.AccountIdentifier and a *types.Currency in the genesis block.

type Broadcast

type Broadcast struct {
	Identifier            string                       `json:"identifier"`
	NetworkIdentifier     *types.NetworkIdentifier     `json:"network_identifier"`
	TransactionIdentifier *types.TransactionIdentifier `json:"transaction_identifier"`
	ConfirmationDepth     int64                        `json:"confirmation_depth"`
	Intent                []*types.Operation           `json:"intent"`
	Payload               string                       `json:"payload"`
	LastBroadcast         *types.BlockIdentifier       `json:"broadcast_at"`
	Broadcasts            int                          `json:"broadcasts"`
}

Broadcast is persisted to the db to track transaction broadcast.

type BroadcastStorage

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

BroadcastStorage implements storage methods for managing transaction broadcast.

func NewBroadcastStorage

func NewBroadcastStorage(
	db Database,
	staleDepth int64,
	broadcastLimit int,
	tipDelay int64,
	broadcastBehindTip bool,
	blockBroadcastLimit int,
) *BroadcastStorage

NewBroadcastStorage returns a new BroadcastStorage.

func (*BroadcastStorage) AddingBlock

func (b *BroadcastStorage) AddingBlock(
	ctx context.Context,
	block *types.Block,
	transaction DatabaseTransaction,
) (CommitWorker, error)

AddingBlock is called by BlockStorage when adding a block.

func (*BroadcastStorage) Broadcast

func (b *BroadcastStorage) Broadcast(
	ctx context.Context,
	dbTx DatabaseTransaction,
	identifier string,
	network *types.NetworkIdentifier,
	intent []*types.Operation,
	transactionIdentifier *types.TransactionIdentifier,
	payload string,
	confirmationDepth int64,
) error

Broadcast is called when a caller wants a transaction to be broadcast and tracked. The caller SHOULD NOT broadcast the transaction before calling this function.

func (*BroadcastStorage) BroadcastAll

func (b *BroadcastStorage) BroadcastAll(ctx context.Context, onlyEligible bool) error

BroadcastAll broadcasts all transactions in BroadcastStorage. If onlyEligible is set to true, then only transactions that should be broadcast again are actually broadcast.

func (*BroadcastStorage) ClearBroadcasts

func (b *BroadcastStorage) ClearBroadcasts(ctx context.Context) ([]*Broadcast, error)

ClearBroadcasts deletes all in-progress broadcasts from BroadcastStorage. This is useful when there is some construction error and all pending broadcasts will fail and should be cleared instead of re-attempting.

func (*BroadcastStorage) GetAllBroadcasts

func (b *BroadcastStorage) GetAllBroadcasts(ctx context.Context) ([]*Broadcast, error)

GetAllBroadcasts returns all currently in-process broadcasts.

func (*BroadcastStorage) Initialize

func (b *BroadcastStorage) Initialize(
	helper BroadcastStorageHelper,
	handler BroadcastStorageHandler,
)

Initialize adds a BroadcastStorageHelper and BroadcastStorageHandler to BroadcastStorage. This must be called prior to syncing!

func (*BroadcastStorage) LockedAccounts

func (b *BroadcastStorage) LockedAccounts(
	ctx context.Context,
	dbTx DatabaseTransaction,
) ([]*types.AccountIdentifier, error)

LockedAccounts returns all *types.AccountIdentifier currently active in transaction broadcasts. The caller SHOULD NOT broadcast a transaction from an account if it is considered locked!

func (*BroadcastStorage) RemovingBlock

func (b *BroadcastStorage) RemovingBlock(
	ctx context.Context,
	block *types.Block,
	transaction DatabaseTransaction,
) (CommitWorker, error)

RemovingBlock is called by BlockStorage when removing a block. TODO: error if transaction removed after confirmed (means confirmation depth not deep enough)

type BroadcastStorageHandler

type BroadcastStorageHandler interface {
	// TransactionConfirmed is called when a transaction is observed on-chain for the
	// last time at a block height < current block height - confirmationDepth.
	TransactionConfirmed(
		context.Context,
		DatabaseTransaction,
		string,
		*types.BlockIdentifier,
		*types.Transaction,
		[]*types.Operation,
	) error // can use locked account again + confirm matches intent + update logger

	// TransactionStale is called when a transaction has not yet been
	// seen on-chain and is considered stale. This occurs when
	// current block height - last broadcast > staleDepth.
	TransactionStale(
		context.Context,
		DatabaseTransaction,
		string,
		*types.TransactionIdentifier,
	) error // log in counter (rebroadcast should occur here)

	// BroadcastFailed is called when another transaction broadcast would
	// put it over the provided broadcast limit.
	BroadcastFailed(
		context.Context,
		DatabaseTransaction,
		string,
		*types.TransactionIdentifier,
		[]*types.Operation,
	) error
}

BroadcastStorageHandler is invoked when a transaction is confirmed on-chain or when a transaction is considered stale.

type BroadcastStorageHelper

type BroadcastStorageHelper interface {
	// CurrentBlockIdentifier is called before transaction broadcast and is used
	// to determine if a transaction broadcast is stale.
	CurrentBlockIdentifier(
		context.Context,
	) (*types.BlockIdentifier, error) // used to determine if should rebroadcast

	// AtTip is called before transaction broadcast to determine if we are at tip.
	AtTip(
		context.Context,
		int64,
	) (bool, error)

	// FindTransaction looks for the provided TransactionIdentifier in processed
	// blocks and returns the block identifier containing the most recent sighting
	// and the transaction seen in that block.
	FindTransaction(
		context.Context,
		*types.TransactionIdentifier,
		DatabaseTransaction,
	) (*types.BlockIdentifier, *types.Transaction, error) // used to confirm

	// BroadcastTransaction broadcasts a transaction to a Rosetta implementation
	// and returns the *types.TransactionIdentifier returned by the implementation.
	BroadcastTransaction(
		context.Context,
		*types.NetworkIdentifier,
		string,
	) (*types.TransactionIdentifier, error) // handle initial broadcast + confirm matches provided + rebroadcast if stale
}

BroadcastStorageHelper is used by BroadcastStorage to submit transactions and find said transaction in blocks on-chain.

type BufferPool

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

BufferPool contains a sync.Pool of *bytes.Buffer.

func NewBufferPool

func NewBufferPool() *BufferPool

NewBufferPool returns a new *BufferPool.

func (*BufferPool) Get

func (p *BufferPool) Get() *bytes.Buffer

Get returns a new or reused *bytes.Buffer.

func (*BufferPool) Put

func (p *BufferPool) Put(buffer *bytes.Buffer)

Put resets the provided *bytes.Buffer and stores it in the pool for reuse.

func (*BufferPool) PutByteSlice

func (p *BufferPool) PutByteSlice(buffer []byte)

PutByteSlice creates a *bytes.Buffer from the provided []byte and stores it in the pool for reuse.

type CoinStorage

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

CoinStorage implements storage methods for storing UTXOs.

func NewCoinStorage

func NewCoinStorage(
	db Database,
	helper CoinStorageHelper,
	asserter *asserter.Asserter,
) *CoinStorage

NewCoinStorage returns a new CoinStorage.

func (*CoinStorage) AddCoins

func (c *CoinStorage) AddCoins(
	ctx context.Context,
	accountCoins []*AccountCoin,
) error

AddCoins takes an array of AccountCoins and saves them to the database. It returns an error if the transaction fails.

func (*CoinStorage) AddingBlock

func (c *CoinStorage) AddingBlock(
	ctx context.Context,
	block *types.Block,
	transaction DatabaseTransaction,
) (CommitWorker, error)

AddingBlock is called by BlockStorage when adding a block.

func (*CoinStorage) GetCoin

func (c *CoinStorage) GetCoin(
	ctx context.Context,
	coinIdentifier *types.CoinIdentifier,
) (*types.Coin, *types.AccountIdentifier, error)

GetCoin returns a *types.Coin by its identifier.

func (*CoinStorage) GetCoinTransactional

func (c *CoinStorage) GetCoinTransactional(
	ctx context.Context,
	dbTx DatabaseTransaction,
	coinIdentifier *types.CoinIdentifier,
) (*types.Coin, *types.AccountIdentifier, error)

GetCoinTransactional returns a *types.Coin by its identifier in a database transaction.

func (*CoinStorage) GetCoins

func (c *CoinStorage) GetCoins(
	ctx context.Context,
	accountIdentifier *types.AccountIdentifier,
) ([]*types.Coin, *types.BlockIdentifier, error)

GetCoins returns all unspent coins for a provided *types.AccountIdentifier.

func (*CoinStorage) GetCoinsTransactional

func (c *CoinStorage) GetCoinsTransactional(
	ctx context.Context,
	dbTx DatabaseTransaction,
	accountIdentifier *types.AccountIdentifier,
) ([]*types.Coin, *types.BlockIdentifier, error)

GetCoinsTransactional returns all unspent coins for a provided *types.AccountIdentifier.

func (*CoinStorage) GetLargestCoin

func (c *CoinStorage) GetLargestCoin(
	ctx context.Context,
	accountIdentifier *types.AccountIdentifier,
	currency *types.Currency,
) (*big.Int, *types.CoinIdentifier, *types.BlockIdentifier, error)

GetLargestCoin returns the largest Coin for a *types.AccountIdentifier and *types.Currency. If no Coins are available, a 0 balance is returned.

func (*CoinStorage) RemovingBlock

func (c *CoinStorage) RemovingBlock(
	ctx context.Context,
	block *types.Block,
	transaction DatabaseTransaction,
) (CommitWorker, error)

RemovingBlock is called by BlockStorage when removing a block.

func (*CoinStorage) SetCoinsImported

func (c *CoinStorage) SetCoinsImported(
	ctx context.Context,
	accountBalances []*utils.AccountBalance,
) error

SetCoinsImported sets coins of a set of addresses by getting their coins from the tip block, and populating the database. This is used when importing prefunded addresses.

type CoinStorageHelper

type CoinStorageHelper interface {
	// CurrentBlockIdentifier is called while fetching coins in a single
	// database transaction to return the *types.BlockIdentifier where
	// the Coin set is valid.
	CurrentBlockIdentifier(
		context.Context,
		DatabaseTransaction,
	) (*types.BlockIdentifier, error)
}

CoinStorageHelper is used by CoinStorage to determine at which block a Coin set is valid.

type CommitWorker

type CommitWorker func(context.Context) error

CommitWorker is returned by a BlockWorker to be called after changes have been committed. It is common to put logging activities in here (that shouldn't be printed until the block is committed).

type CompressorEntry

type CompressorEntry struct {
	Namespace      string
	DictionaryPath string
}

CompressorEntry is used to initialize a dictionary compression. All DictionaryPaths are loaded from disk at initialization.

type CounterStorage

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

CounterStorage implements counter-specific storage methods on top of a Database and DatabaseTransaction interface.

func NewCounterStorage

func NewCounterStorage(
	db Database,
) *CounterStorage

NewCounterStorage returns a new CounterStorage.

func (*CounterStorage) Get

func (c *CounterStorage) Get(ctx context.Context, counter string) (*big.Int, error)

Get returns the current value of a counter.

func (*CounterStorage) Update

func (c *CounterStorage) Update(
	ctx context.Context,
	counter string,
	amount *big.Int,
) (*big.Int, error)

Update updates the value of a counter by amount and returns the new value.

func (*CounterStorage) UpdateTransactional

func (c *CounterStorage) UpdateTransactional(
	ctx context.Context,
	dbTx DatabaseTransaction,
	counter string,
	amount *big.Int,
) (*big.Int, error)

UpdateTransactional updates the value of a counter by amount and returns the new value in a transaction.

type Database

type Database interface {
	NewDatabaseTransaction(context.Context, bool) DatabaseTransaction
	Close(context.Context) error
	Encoder() *Encoder
}

Database is an interface that provides transactional access to a KV store.

func NewBadgerStorage

func NewBadgerStorage(
	ctx context.Context,
	dir string,
	storageOptions ...BadgerOption,
) (Database, error)

NewBadgerStorage creates a new BadgerStorage.

type DatabaseTransaction

type DatabaseTransaction interface {
	Set(context.Context, []byte, []byte, bool) error
	Get(context.Context, []byte) (bool, []byte, error)
	Delete(context.Context, []byte) error

	Scan(
		context.Context,
		[]byte,
		[]byte,
		func([]byte, []byte) error,
		bool,
		bool,
	) (int, error)
	ScanMulti(
		context.Context,
		[]*parser.BalanceChange,
		bool,
		bool,
		func(*types.AccountIdentifier, *types.Currency) []byte,
		func(*types.AccountIdentifier, *types.Currency, int64) []byte,
	) []string
	Commit(context.Context) error
	Discard(context.Context)
}

DatabaseTransaction is an interface that provides access to a KV store within some transaction context provided by a Database.

When a DatabaseTransaction is committed or discarded, all memory utilized is reclaimed. If you want to persist any data retrieved, make sure to make a copy!

type Encoder

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

Encoder handles the encoding/decoding of structs and the compression/decompression of data using zstd. Optionally, the caller can provide a map of dicts on initialization that can be used by zstd. You can read more about these "dicts" here: https://github.com/facebook/zstd#the-case-for-small-data-compression.

NOTE: If you change these dicts, you will not be able to decode previously encoded data. For many users, providing no dicts is sufficient!

func NewEncoder

func NewEncoder(
	entries []*CompressorEntry,
	pool *BufferPool,
	compress bool,
) (*Encoder, error)

NewEncoder returns a new *Encoder. The dicts provided should contain k:v of namespace:zstd dict.

func (*Encoder) Decode

func (e *Encoder) Decode(
	namespace string,
	input []byte,
	object interface{},
	reclaimInput bool,
) error

Decode attempts to decompress the object and will use a dict if one exists for the namespace.

func (*Encoder) DecodeAccountCoin

func (e *Encoder) DecodeAccountCoin(
	b []byte,
	accountCoin *AccountCoin,
	reclaimInput bool,
) error

DecodeAccountCoin decodes an AccountCoin and optionally reclaims the memory associated with the input.

func (*Encoder) DecodeRaw

func (e *Encoder) DecodeRaw(namespace string, input []byte) ([]byte, error)

DecodeRaw only decompresses an input, leaving decoding to the caller. This is particularly useful for training a compressor.

func (*Encoder) Encode

func (e *Encoder) Encode(namespace string, object interface{}) ([]byte, error)

Encode attempts to compress the object and will use a dict if one exists for the namespace.

func (*Encoder) EncodeAccountCoin

func (e *Encoder) EncodeAccountCoin(
	accountCoin *AccountCoin,
) ([]byte, error)

EncodeAccountCoin is used to encode an *AccountCoin using the scheme (on the happy path): accountAddress|coinIdentifier|amountValue|amountCurrencySymbol| amountCurrencyDecimals

And the following scheme on the unhappy path: accountAddress|coinIdentifier|amountValue|amountCurrencySymbol| amountCurrencyDecimals|accountMetadata|subAccountAddress| subAccountMetadata|amountMetadata|currencyMetadata

In both cases, the | character is represented by the unicodeRecordSeparator rune.

func (*Encoder) EncodeRaw

func (e *Encoder) EncodeRaw(namespace string, input []byte) ([]byte, error)

EncodeRaw only compresses an input, leaving encoding to the caller. This is particularly useful for training a compressor.

type JobStorage

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

JobStorage implements storage methods for managing jobs.

func NewJobStorage

func NewJobStorage(db Database) *JobStorage

NewJobStorage returns a new instance of *JobStorage.

func (*JobStorage) AllCompleted

func (j *JobStorage) AllCompleted(ctx context.Context) ([]*job.Job, error)

AllCompleted gets all successfully completed *job.Jobs.

func (*JobStorage) AllFailed

func (j *JobStorage) AllFailed(ctx context.Context) ([]*job.Job, error)

AllFailed returns all failed *job.Jobs.

func (*JobStorage) AllProcessing

func (j *JobStorage) AllProcessing(ctx context.Context) ([]*job.Job, error)

AllProcessing gets all processing *job.Jobs.

func (*JobStorage) Broadcasting

func (j *JobStorage) Broadcasting(
	ctx context.Context,
	dbTx DatabaseTransaction,
) ([]*job.Job, error)

Broadcasting returns all broadcasting *job.Job.

func (*JobStorage) Completed

func (j *JobStorage) Completed(ctx context.Context, workflow string) ([]*job.Job, error)

Completed gets all successfully completed *job.Job of a certain workflow.

func (*JobStorage) Failed

func (j *JobStorage) Failed(ctx context.Context, workflow string) ([]*job.Job, error)

Failed returns all failed *job.Job of a certain workflow.

func (*JobStorage) Get

func (j *JobStorage) Get(
	ctx context.Context,
	dbTx DatabaseTransaction,
	identifier string,
) (*job.Job, error)

Get returns a *job.Job by its identifier.

func (*JobStorage) Processing

func (j *JobStorage) Processing(
	ctx context.Context,
	dbTx DatabaseTransaction,
	workflow string,
) ([]*job.Job, error)

Processing gets all processing *job.Job of a certain workflow.

func (*JobStorage) Ready

func (j *JobStorage) Ready(ctx context.Context, dbTx DatabaseTransaction) ([]*job.Job, error)

Ready returns all ready *job.Job.

func (*JobStorage) Update

func (j *JobStorage) Update(
	ctx context.Context,
	dbTx DatabaseTransaction,
	v *job.Job,
) (string, error)

Update overwrites an existing *job.Job or creates a new one (and assigns an identifier).

type Key

type Key struct {
	Account *types.AccountIdentifier `json:"account"`
	KeyPair *keys.KeyPair            `json:"keypair"`
}

Key is the struct stored in key storage. This is public so that accounts can be loaded from a configuration file.

type KeyStorage

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

KeyStorage implements key storage methods on top of a Database and DatabaseTransaction interface.

func NewKeyStorage

func NewKeyStorage(
	db Database,
) *KeyStorage

NewKeyStorage returns a new KeyStorage.

func (*KeyStorage) Get

func (k *KeyStorage) Get(
	ctx context.Context,
	account *types.AccountIdentifier,
) (*keys.KeyPair, error)

Get returns a *keys.KeyPair for an AccountIdentifier, if it exists.

func (*KeyStorage) GetAllAccounts

func (k *KeyStorage) GetAllAccounts(ctx context.Context) ([]*types.AccountIdentifier, error)

GetAllAccounts returns all AccountIdentifiers in key storage.

func (*KeyStorage) GetAllAccountsTransactional

func (k *KeyStorage) GetAllAccountsTransactional(
	ctx context.Context,
	dbTx DatabaseTransaction,
) ([]*types.AccountIdentifier, error)

GetAllAccountsTransactional returns all AccountIdentifiers in key storage.

func (*KeyStorage) GetTransactional

func (k *KeyStorage) GetTransactional(
	ctx context.Context,
	dbTx DatabaseTransaction,
	account *types.AccountIdentifier,
) (*keys.KeyPair, error)

GetTransactional returns a *keys.KeyPair for an AccountIdentifier in a DatabaseTransaction, if it exists.

func (*KeyStorage) ImportAccounts

func (k *KeyStorage) ImportAccounts(ctx context.Context, accounts []*PrefundedAccount) error

ImportAccounts loads a set of prefunded accounts into key storage.

func (*KeyStorage) RandomAccount

func (k *KeyStorage) RandomAccount(ctx context.Context) (*types.AccountIdentifier, error)

RandomAccount returns a random account from all accounts.

func (*KeyStorage) Sign

func (k *KeyStorage) Sign(
	ctx context.Context,
	payloads []*types.SigningPayload,
) ([]*types.Signature, error)

Sign attempts to sign a slice of *types.SigningPayload with the keys in KeyStorage.

func (*KeyStorage) Store

func (k *KeyStorage) Store(
	ctx context.Context,
	account *types.AccountIdentifier,
	keyPair *keys.KeyPair,
) error

Store saves a keys.KeyPair for a given address. If the address already exists, an error is returned.

func (*KeyStorage) StoreTransactional

func (k *KeyStorage) StoreTransactional(
	ctx context.Context,
	account *types.AccountIdentifier,
	keyPair *keys.KeyPair,
	dbTx DatabaseTransaction,
) error

StoreTransactional stores a key in a database transaction.

type PrefundedAccount

type PrefundedAccount struct {
	PrivateKeyHex     string                   `json:"privkey"`
	AccountIdentifier *types.AccountIdentifier `json:"account_identifier"`
	CurveType         types.CurveType          `json:"curve_type"`
	Currency          *types.Currency          `json:"currency"`
}

PrefundedAccount is used to load prefunded addresses into key storage.

Jump to

Keyboard shortcuts

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