storage

package
v0.4.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

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"
)

Variables

View Source
var (
	// ErrAccountNotFound is returned when an account
	// is not found in BalanceStorage.
	ErrAccountNotFound = errors.New("account not found")

	// ErrNegativeBalance is returned when an account
	// balance goes negative as the result of an operation.
	ErrNegativeBalance = errors.New("negative balance")
)
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")

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

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

Functions

func GetBalanceKey added in v0.2.0

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

GetBalanceKey returns a deterministic hash of an types.Account + types.Currency.

Types

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) Get

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

Get fetches the value of a key in its own transaction.

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).

func (*BadgerStorage) Scan added in v0.2.1

func (b *BadgerStorage) Scan(
	ctx context.Context,
	prefix []byte,
) ([][]byte, error)

Scan fetches all items at a given prefix. This is typically used to get all items in a namespace.

func (*BadgerStorage) Set

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

Set changes the value of the key to the value in its own transaction.

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.

func (*BadgerTransaction) Set

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

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

type BalanceStorage added in v0.4.0

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

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

func NewBalanceStorage added in v0.4.0

func NewBalanceStorage(
	db Database,
) *BalanceStorage

NewBalanceStorage returns a new BalanceStorage.

func (*BalanceStorage) AddingBlock added in v0.4.0

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 added in v0.4.0

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 added in v0.4.0

func (b *BalanceStorage) GetAllAccountCurrency(
	ctx context.Context,
) ([]*reconciler.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 added in v0.4.0

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

GetBalance returns all the balances of a types.AccountIdentifier and the types.BlockIdentifier it was last updated at.

func (*BalanceStorage) Initialize added in v0.4.0

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) RemovingBlock added in v0.4.0

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 added in v0.4.0

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. This is particularly useful for bootstrapping balances.

func (*BalanceStorage) UpdateBalance added in v0.4.0

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.

type BalanceStorageHandler added in v0.4.0

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 added in v0.4.0

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

	ExemptFunc() parser.ExemptOperation
	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 added in v0.4.0

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

AddBlock stores a block or returns an error.

func (*BlockStorage) AtTip added in v0.4.0

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

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

func (*BlockStorage) CreateBlockCache added in v0.2.0

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

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

func (*BlockStorage) FindTransaction added in v0.4.0

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.BlockIdentifier,
) (*types.Block, error)

GetBlock returns a block, if it exists.

func (*BlockStorage) GetHeadBlockIdentifier

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

GetHeadBlockIdentifier returns the head block identifier, if it exists.

func (*BlockStorage) Initialize added in v0.4.0

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) 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 added in v0.2.0

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 added in v0.4.0

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. TODO: Must be exported for use

type Broadcast added in v0.4.0

type Broadcast struct {
	Identifier    *types.TransactionIdentifier `json:"identifier"`
	Sender        string                       `json:"sender"`
	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 added in v0.4.0

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

BroadcastStorage implements storage methods for managing transaction broadcast.

func NewBroadcastStorage added in v0.4.0

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

NewBroadcastStorage returns a new BroadcastStorage.

func (*BroadcastStorage) AddingBlock added in v0.4.0

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 added in v0.4.0

func (b *BroadcastStorage) Broadcast(
	ctx context.Context,
	sender string,
	intent []*types.Operation,
	transactionIdentifier *types.TransactionIdentifier,
	payload string,
) 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 added in v0.4.0

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 added in v0.4.0

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 added in v0.4.0

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

GetAllBroadcasts returns all currently in-process broadcasts.

func (*BroadcastStorage) Initialize added in v0.4.0

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) LockedAddresses added in v0.4.0

func (b *BroadcastStorage) LockedAddresses(ctx context.Context) ([]string, error)

LockedAddresses returns all addresses currently active in transaction broadcasts. The caller SHOULD NOT broadcast a transaction from an account if it is considered locked!

func (*BroadcastStorage) RemovingBlock added in v0.4.0

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 added in v0.4.0

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,
		*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,
		*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,
		*types.TransactionIdentifier,
		[]*types.Operation,
	) error
}

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

type BroadcastStorageHelper added in v0.4.0

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,
		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 Coin added in v0.4.0

type Coin struct {
	Identifier  *types.CoinIdentifier `json:"identifier"`
	Transaction *types.Transaction    `json:"transaction"`
	Operation   *types.Operation      `json:"operation"`
}

Coin represents some spendable output (typically referred to as a UTXO).

type CoinStorage added in v0.4.0

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

CoinStorage implements storage methods for storing UTXOs.

func NewCoinStorage added in v0.4.0

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

NewCoinStorage returns a new CoinStorage.

func (*CoinStorage) AddingBlock added in v0.4.0

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) GetCoins added in v0.4.0

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

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

func (*CoinStorage) GetLargestCoin added in v0.4.0

func (c *CoinStorage) GetLargestCoin(
	ctx context.Context,
	accountIdentifier *types.AccountIdentifier,
	currency *types.Currency,
) (*big.Int, *types.CoinIdentifier, 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 added in v0.4.0

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

RemovingBlock is called by BlockStorage when removing a block.

type CommitWorker added in v0.4.0

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 CounterStorage added in v0.3.0

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

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

func NewCounterStorage added in v0.3.0

func NewCounterStorage(
	db Database,
) *CounterStorage

NewCounterStorage returns a new CounterStorage.

func (*CounterStorage) Get added in v0.3.0

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

Get returns the current value of a counter.

func (*CounterStorage) Update added in v0.3.0

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.

type Database

type Database interface {
	NewDatabaseTransaction(context.Context, bool) DatabaseTransaction
	Close(context.Context) error
	Set(context.Context, []byte, []byte) error
	Get(context.Context, []byte) (bool, []byte, error)
	Scan(ctx context.Context, prefix []byte) ([][]byte, error)
}

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

func NewBadgerStorage

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

NewBadgerStorage creates a new BadgerStorage.

type DatabaseTransaction

type DatabaseTransaction interface {
	Set(context.Context, []byte, []byte) error
	Get(context.Context, []byte) (bool, []byte, error)
	Delete(context.Context, []byte) error
	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.

type KeyStorage added in v0.4.0

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

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

func NewKeyStorage added in v0.4.0

func NewKeyStorage(
	db Database,
) *KeyStorage

NewKeyStorage returns a new KeyStorage.

func (*KeyStorage) Get added in v0.4.0

func (k *KeyStorage) Get(ctx context.Context, address string) (*keys.KeyPair, error)

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

func (*KeyStorage) GetAllAddresses added in v0.4.0

func (k *KeyStorage) GetAllAddresses(ctx context.Context) ([]string, error)

GetAllAddresses returns all addresses in key storage.

func (*KeyStorage) RandomAddress added in v0.4.0

func (k *KeyStorage) RandomAddress(ctx context.Context) (string, error)

RandomAddress returns a random address from all addresses.

func (*KeyStorage) Sign added in v0.4.0

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 added in v0.4.0

func (k *KeyStorage) Store(ctx context.Context, address string, keyPair *keys.KeyPair) error

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

Jump to

Keyboard shortcuts

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