Documentation
¶
Index ¶
- Constants
- Variables
- type Accounts
- func (a *Accounts) ApplySpend(ctx context.Context, tx sql.Executor, account *types.AccountID, ...) error
- func (a *Accounts) Commit() error
- func (a *Accounts) Credit(ctx context.Context, tx sql.Executor, account *types.AccountID, amt *big.Int) error
- func (a *Accounts) GetAccount(ctx context.Context, tx sql.Executor, account *types.AccountID) (*types.Account, error)
- func (a *Accounts) GetBlockSpends() []*Spend
- func (*Accounts) NumAccounts(ctx context.Context, tx sql.Executor) (int64, error)
- func (a *Accounts) Rollback()
- func (a *Accounts) Spend(ctx context.Context, tx sql.Executor, account *types.AccountID, ...) error
- func (a *Accounts) Transfer(ctx context.Context, db sql.TxMaker, from, to *types.AccountID, amt *big.Int) error
- func (a *Accounts) Updates() []*types.Account
- type Spend
Constants ¶
const (
AccountsLRUCacheSize = 1000
)
Variables ¶
var ( ErrInsufficientFunds = errors.New("insufficient funds") ErrConvertToBigInt = errors.New("could not convert to big int") ErrInvalidNonce = errors.New("invalid nonce") ErrAccountNotFound = errors.New("account not found") ErrNegativeBalance = errors.New("negative balance not permitted") ErrNegativeTransfer = errors.New("negative transfer not permitted") )
Functions ¶
This section is empty.
Types ¶
type Accounts ¶
type Accounts struct {
// contains filtered or unexported fields
}
Accounts represents an in-memory cache of accounts stored in a PostgreSQL database. This is primarily used to optimize data reads.
func InitializeAccountStore ¶
func (*Accounts) ApplySpend ¶
func (a *Accounts) ApplySpend(ctx context.Context, tx sql.Executor, account *types.AccountID, amount *big.Int, nonce int64) error
ApplySpend spends an amount from an account. It blocks until the spend is written to the database. This is used by the new nodes during migration to replicate spends from the old network to the new network. If the account does not have enough funds to spend the amount, spend the entire balance. Nonces on the new network take precedence over the old network, so the nonces are not checked.
func (*Accounts) Commit ¶
Commit applies all the updates to the in-memory cache. This is called after the updates are written to the pg database.
func (*Accounts) Credit ¶
func (a *Accounts) Credit(ctx context.Context, tx sql.Executor, account *types.AccountID, amt *big.Int) error
Credit credits an account with the given amount. If the account does not exist, it will be created. A negative amount will be treated as a debit. Accounts cannot have negative balances, and will return an error if the amount would cause the balance to go negative. It also adds a record to the in-memory cache.
func (*Accounts) GetAccount ¶
func (a *Accounts) GetAccount(ctx context.Context, tx sql.Executor, account *types.AccountID) (*types.Account, error)
GetAccount retrieves the account with the given identifier. If the account does not exist, it will return an account with a balance of 0 and a nonce of 0.
func (*Accounts) GetBlockSpends ¶
GetBlockSpends returns all the spends that occurred in the block.
func (*Accounts) NumAccounts ¶
func (*Accounts) Spend ¶
func (a *Accounts) Spend(ctx context.Context, tx sql.Executor, account *types.AccountID, amount *big.Int, nonce int64) error
Spend spends an amount from an account and records nonces. It blocks until the spend is written to the database. The nonce passed must be exactly one greater than the account's nonce. If the nonce is not valid, the spend will fail. If the account does not have enough funds to spend the amount, an ErrInsufficientFunds error will be returned.
func (*Accounts) Transfer ¶
func (a *Accounts) Transfer(ctx context.Context, db sql.TxMaker, from, to *types.AccountID, amt *big.Int) error
Transfer transfers an amount from one account to another. If the from account does not have enough funds to transfer the amount, it will fail. If the to account does not exist, it will be created. The amount must be greater than 0.