storage

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2020 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

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

	// ErrAccountNotFound is returned when an account
	// is not found in BlockStorage.
	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")

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

	// ErrAlreadyStartedSyncing is returned when trying to bootstrap
	// balances after syncing has started.
	ErrAlreadyStartedSyncing = errors.New("cannot bootstrap accounts, already started syncing")
)

Functions

func AddStringValues

func AddStringValues(
	a string,
	b string,
) (string, error)

AddStringValues adds string amounts using big.Int.

func CreateTempDir

func CreateTempDir() (*string, error)

CreateTempDir creates a directory in /tmp for usage within testing.

func GetAccountKey

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

GetAccountKey returns a byte slice representing a *types.AccountIdentifier. This byte slice automatically handles the existence of *types.SubAccount detail.

func GetCurrencyKey

func GetCurrencyKey(currency *types.Currency) string

GetCurrencyKey is used to identify a *types.Currency in an account's map of currencies. It is not feasible to create a map of types.Currency*types.Amount because types.Currency contains a metadata pointer that would prevent any equality.

func RemoveTempDir

func RemoveTempDir(dir string)

RemoveTempDir deletes a directory at a provided path for usage within testing.

func SubtractStringValues

func SubtractStringValues(
	a string,
	b string,
) (string, error)

SubtractStringValues subtracts a-b using big.Int.

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

type BalanceChange struct {
	Account    *types.AccountIdentifier `json:"account_identifier,omitempty"`
	Currency   *types.Currency          `json:"currency,omitempty"`
	Block      *types.BlockIdentifier   `json:"block_identifier,omitempty"`
	Difference string                   `json:"difference,omitempty"`
}

BalanceChange represents a balance change that affected a *types.AccountIdentifier and a *types.Currency.

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(ctx context.Context, db Database) *BlockStorage

NewBlockStorage returns a new BlockStorage.

func (*BlockStorage) BootstrapBalances

func (b *BlockStorage) 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 (*BlockStorage) GetBalance

func (b *BlockStorage) GetBalance(
	ctx context.Context,
	transaction DatabaseTransaction,
	account *types.AccountIdentifier,
) (map[string]*types.Amount, *types.BlockIdentifier, error)

GetBalance returns all the balances of a types.AccountIdentifier and the types.BlockIdentifier it was last updated at. TODO: change to fetch by account and currency

func (*BlockStorage) GetBlock

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

GetBlock returns a block, if it exists.

func (*BlockStorage) GetHeadBlockIdentifier

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

GetHeadBlockIdentifier returns the head block identifier, if it exists.

func (*BlockStorage) NewDatabaseTransaction

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

NewDatabaseTransaction returns a DatabaseTransaction from the Database that is backing BlockStorage.

func (*BlockStorage) RemoveBlock

func (b *BlockStorage) RemoveBlock(
	ctx context.Context,
	transaction DatabaseTransaction,
	block *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) StoreBlock

func (b *BlockStorage) StoreBlock(
	ctx context.Context,
	transaction DatabaseTransaction,
	block *types.Block,
) error

StoreBlock stores a block or returns an error. StoreBlock also stores the block hash and all its transaction hashes for duplicate detection.

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.

func (*BlockStorage) UpdateBalance

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

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

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

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.

Jump to

Keyboard shortcuts

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