accounts

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AccountsLRUCacheSize = 1000
)

Variables

View Source
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 InitializeAccountStore(ctx context.Context, db sql.DB, logger log.Logger) (*Accounts, error)

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

func (a *Accounts) Commit() error

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

func (a *Accounts) GetBlockSpends() []*Spend

GetBlockSpends returns all the spends that occurred in the block.

func (*Accounts) NumAccounts

func (*Accounts) NumAccounts(ctx context.Context, tx sql.Executor) (int64, error)

func (*Accounts) Rollback

func (a *Accounts) Rollback()

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.

func (*Accounts) Updates

func (a *Accounts) Updates() []*types.Account

type Spend

type Spend struct {
	Account *types.AccountID
	Amount  *big.Int
	Nonce   uint64
}

func (*Spend) MarshalBinary

func (s *Spend) MarshalBinary() ([]byte, error)

func (*Spend) UnmarshalBinary

func (s *Spend) UnmarshalBinary(data []byte) (err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳