Documentation
¶
Index ¶
- Variables
- func AddStringValues(a string, b string) (string, error)
- func CreateTempDir() (*string, error)
- func GetAccountKey(account *types.AccountIdentifier) []byte
- func GetCurrencyKey(currency *types.Currency) string
- func RemoveTempDir(dir string)
- func SubtractStringValues(a string, b string) (string, error)
- type BadgerStorage
- func (b *BadgerStorage) Close(ctx context.Context) error
- func (b *BadgerStorage) Get(ctx context.Context, key []byte) (bool, []byte, error)
- func (b *BadgerStorage) NewDatabaseTransaction(ctx context.Context, write bool) DatabaseTransaction
- func (b *BadgerStorage) Set(ctx context.Context, key []byte, value []byte) error
- type BadgerTransaction
- func (b *BadgerTransaction) Commit(context.Context) error
- func (b *BadgerTransaction) Delete(ctx context.Context, key []byte) error
- func (b *BadgerTransaction) Discard(context.Context)
- func (b *BadgerTransaction) Get(ctx context.Context, key []byte) (bool, []byte, error)
- func (b *BadgerTransaction) Set(ctx context.Context, key []byte, value []byte) error
- type BalanceChange
- type BlockStorage
- func (b *BlockStorage) BootstrapBalances(ctx context.Context, bootstrapBalancesFile string, ...) error
- func (b *BlockStorage) GetBalance(ctx context.Context, transaction DatabaseTransaction, ...) (map[string]*types.Amount, *types.BlockIdentifier, error)
- func (b *BlockStorage) GetBlock(ctx context.Context, transaction DatabaseTransaction, ...) (*types.Block, error)
- func (b *BlockStorage) GetHeadBlockIdentifier(ctx context.Context, transaction DatabaseTransaction) (*types.BlockIdentifier, error)
- func (b *BlockStorage) NewDatabaseTransaction(ctx context.Context, write bool) DatabaseTransaction
- func (b *BlockStorage) RemoveBlock(ctx context.Context, transaction DatabaseTransaction, ...) error
- func (b *BlockStorage) StoreBlock(ctx context.Context, transaction DatabaseTransaction, block *types.Block) error
- func (b *BlockStorage) StoreHeadBlockIdentifier(ctx context.Context, transaction DatabaseTransaction, ...) error
- func (b *BlockStorage) UpdateBalance(ctx context.Context, dbTransaction DatabaseTransaction, ...) (*BalanceChange, error)
- type BootstrapBalance
- type Database
- type DatabaseTransaction
Constants ¶
This section is empty.
Variables ¶
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 ¶
AddStringValues adds string amounts using big.Int.
func CreateTempDir ¶
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 ¶
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.
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) 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 ¶
func (b *BadgerTransaction) Commit(context.Context) error
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.
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.
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.