storage

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: May 15, 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 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 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,
	helper Helper,
) *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) 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) GetAllAccountCurrency added in v0.2.1

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

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

func (b *BlockStorage) RemoveBlock(
	ctx context.Context,
	blockIdentifier *types.BlockIdentifier,
) ([]*parser.BalanceChange, 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) SetBalance added in v0.2.0

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

func (b *BlockStorage) StoreBlock(
	ctx context.Context,
	block *types.Block,
) ([]*parser.BalanceChange, 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,
	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 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 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 Helper added in v0.2.0

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

	ExemptFunc() parser.ExemptOperation
	Asserter() *asserter.Asserter
}

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

Jump to

Keyboard shortcuts

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