state

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2019 License: AGPL-3.0 Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrInconsistentTransactions is returned when the transactions to
	// roll back are inconsistent with the saved nonces.
	ErrInconsistentTransactions = errors.New("transactions are inconsistent")

	// ErrInvalidBlock is returned if balances or nonces in a block transactions
	// are incorrect.
	ErrInvalidBlock = errors.New("invalid block")
)
View Source
var (
	// ErrInvalidTxNonce is returned when the transaction nonce isn't greater
	// than the last nonce used by the sender's account.
	ErrInvalidTxNonce = errors.New("invalid tx nonce")

	// ErrInsufficientBalance is returned when the sender tries to send more coins than he has.
	ErrInsufficientBalance = errors.New("tx sender does not have enough coins to send")
)
View Source
var (
	// ErrInvalidTxKey is returned when the transaction key cannot
	// be unmarshalled
	ErrInvalidTxKey = errors.New("txkey byte representation is invalid")
)
View Source
var OptPrefix = func(prefix []byte) Opt {

	l := len(prefix)

	return func(s *stateDB) {
		s.prefix = prefix[:l:l]
	}
}

OptPrefix sets a prefix for all the database keys.

Functions

func ValidateBalance

func ValidateBalance(s Reader, tx *pb.Transaction) error

ValidateBalance validates transaction nonce and balance against a given state.

func ValidateBalances

func ValidateBalances(s Reader, transactions []*pb.Transaction) error

ValidateBalances validates transactions nonce and balance of a block against a given state.

Types

type GreedyInMemoryTxPool

type GreedyInMemoryTxPool struct {
	// contains filtered or unexported fields
}

GreedyInMemoryTxPool is a very simple txpool that stores queued transactions in memory.

func (*GreedyInMemoryTxPool) AddTransaction

func (m *GreedyInMemoryTxPool) AddTransaction(tx *pb.Transaction) error

AddTransaction adds a transaction in memory.

func (*GreedyInMemoryTxPool) Peek

func (m *GreedyInMemoryTxPool) Peek(n uint32) []*pb.Transaction

Peek returns the n oldest transactions from the pool.

func (*GreedyInMemoryTxPool) Pending

func (m *GreedyInMemoryTxPool) Pending() uint64

Pending returns the number of transactions.

func (*GreedyInMemoryTxPool) PopTransaction

func (m *GreedyInMemoryTxPool) PopTransaction() *pb.Transaction

PopTransaction pops the transaction with the highest fee from the pool.

type Opt

type Opt func(*stateDB)

Opt is an option for state.

type Reader

type Reader interface {
	// MerkleRoot returns the Merkle Root of the current state.
	MerkleRoot() (multihash.Multihash, error)

	// GetAccount gets the account details of a user identified
	// by his public key. It returns &pb.Account{} if the account is not
	// found.
	GetAccount(pubKey []byte) (*pb.Account, error)
}

Reader gives read access to users' account balances.

type State

type State interface {
	Reader
	TxReader
	Writer
}

State stores users' account balances. It doesn't handle validation.

func NewState

func NewState(database db.ReadWriteBatcher, opts ...Opt) State

NewState creates a new state from a DB instance.

Prefix is used to prefix keys in the database.

VERY IMPORTANT NOTE: ALL THE DIFFERENT MODULES THAT SHARE THE SAME INSTANCE OF THE DATABASE MUST USE A UNIQUE PREFIX OF THE SAME BYTESIZE.

type TxKey

type TxKey struct {
	TxIdx   uint64
	BlkHash []byte
}

TxKey is used to save a user transactions.

func (*TxKey) Marshal

func (t *TxKey) Marshal() []byte

Marshal converts the struct into a byte slice.

func (*TxKey) Unmarshal

func (t *TxKey) Unmarshal(data []byte) error

Unmarshal creates a TxKey from a byte slice.

type TxPool

type TxPool interface {
	// AddTransaction adds a transaction to the pool.
	// It assumes that the transaction has been validated.
	AddTransaction(tx *pb.Transaction) error

	// PopTransaction pops the transaction with the highest score
	// from the txpool.
	// The score can be computed from various sources: transaction
	// fees, time in the txpool, priority, etc.
	// The txpool implementations can chose to prioritize fairness,
	// miner rewards, or anything else they come up with.
	PopTransaction() *pb.Transaction

	// Peek peeks at n transactions from the txpool
	// without modifying it.
	Peek(n uint32) []*pb.Transaction

	// Pending returns the number of transactions
	// waiting to be processed.
	Pending() uint64
}

TxPool stores transactions that need to be processed.

type TxReader

type TxReader interface {
	// GetAccountTxHashes gets the transaction history of a user identified
	// by his public key.
	GetAccountTxKeys(pubKey []byte) ([]*TxKey, error)
}

TxReader gives read access to a user's transactions.

type Writer

type Writer interface {
	// UpdateAccount sets or updates the account of a user identified by
	// his public key. It should only be used for testing as it cannot be
	// rolled back.
	UpdateAccount(pubKey []byte, account *pb.Account) error

	// ProcessBlock processes all the transactions of the given
	// block and updates the state accordingly.
	ProcessBlock(blk *pb.Block) error

	// RollbackBlock rolls back a block.
	// You should only rollback the last block.
	RollbackBlock(blk *pb.Block) error
}

Writer gives write access to users' account balances.

Directories

Path Synopsis
Package mockstate is a generated GoMock package.
Package mockstate is a generated GoMock package.

Jump to

Keyboard shortcuts

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