Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
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") )
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") )
var ( // ErrInvalidTxKey is returned when the transaction key cannot // be unmarshalled ErrInvalidTxKey = errors.New("txkey byte representation is invalid") )
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 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 ¶
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 ¶
TxKey is used to save a user transactions.
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.