Documentation ¶
Index ¶
- Constants
- Variables
- func CandidatesToMap(candidates CandidateList) (map[hash.PKHash]*Candidate, error)
- func GobBasedDeserialize(state State, data []byte) error
- func GobBasedSerialize(state State) ([]byte, error)
- func WithRunActionsCtx(ctx context.Context, ra RunActionsCtx) context.Context
- type Account
- func (st *Account) AddBalance(amount *big.Int) error
- func (st *Account) Deserialize(buf []byte) error
- func (st *Account) FromProto(acPb *iproto.AccountPb)
- func (st *Account) Serialize() ([]byte, error)
- func (st *Account) SubBalance(amount *big.Int) error
- func (st *Account) ToProto() *iproto.AccountPb
- type ActionHandler
- type Candidate
- type CandidateList
- type Contract
- type Factory
- type FactoryOption
- type RunActionsCtx
- type SortedSlice
- func (slice SortedSlice) Append(e interface{}, f func(interface{}, interface{}) int) SortedSlice
- func (slice SortedSlice) Delete(e interface{}, f func(interface{}, interface{}) int) (SortedSlice, int)
- func (slice *SortedSlice) Deserialize(data []byte) error
- func (slice SortedSlice) Get(e interface{}, f func(interface{}, interface{}) int) (interface{}, bool)
- func (slice *SortedSlice) Serialize() ([]byte, error)
- type State
- type WorkingSet
Constants ¶
const ( // CurrentHeightKey indicates the key of current factory height in underlying DB CurrentHeightKey = "currentHeight" // AccountTrieRootKey indicates the key of accountTrie root hash in underlying DB AccountTrieRootKey = "accountTrieRoot" )
Variables ¶
var ( // ErrCandidate indicates the error of candidate ErrCandidate = errors.New("invalid candidate") // ErrCandidatePb indicates the error of protobuf's candidate message ErrCandidatePb = errors.New("invalid protobuf's candidate message") // ErrCandidateMap indicates the error of candidate map ErrCandidateMap = errors.New("invalid candidate map") // ErrCandidateList indicates the error of candidate list ErrCandidateList = errors.New("invalid candidate list") )
var ( // ErrNotEnoughBalance is the error that the balance is not enough ErrNotEnoughBalance = errors.New("not enough balance") // ErrStateNotExist is the error that the stat does not exist ErrStateNotExist = errors.New("state does not exist") // ErrAccountCollision is the error that the account already exists ErrAccountCollision = errors.New("account already exists") // EmptyAccount indicates an empty account // This is a read-only variable for comparison purpose. Caller should not modify it. EmptyAccount = &Account{ Balance: big.NewInt(0), VotingWeight: big.NewInt(0), } )
var ( // ErrStateSerialization is the error that the state marshaling is failed ErrStateSerialization = errors.New("failed to marshal state") // ErrStateDeserialization is the error that the state un-marshaling is failed ErrStateDeserialization = errors.New("failed to unmarshal state") )
Functions ¶
func CandidatesToMap ¶ added in v0.3.0
func CandidatesToMap(candidates CandidateList) (map[hash.PKHash]*Candidate, error)
CandidatesToMap converts a candidate list to map of cachedCandidates
func GobBasedDeserialize ¶ added in v0.4.0
GobBasedDeserialize deserialize a state from bytes via gob
func GobBasedSerialize ¶ added in v0.4.0
GobBasedSerialize serializes a state into bytes via gob
func WithRunActionsCtx ¶ added in v0.4.0
func WithRunActionsCtx(ctx context.Context, ra RunActionsCtx) context.Context
WithRunActionsCtx add RunActionsCtx into context.
Types ¶
type Account ¶ added in v0.4.0
type Account struct { // 0 is reserved from actions in genesis block and coinbase transfers nonces // other actions' nonces start from 1 Nonce uint64 Balance *big.Int Root hash.Hash32B // storage trie root for contract account CodeHash []byte // hash of the smart contract byte-code for contract account IsCandidate bool VotingWeight *big.Int Votee string }
Account is the canonical representation of an account.
func (*Account) AddBalance ¶ added in v0.4.0
AddBalance adds balance for account state
func (*Account) Deserialize ¶ added in v0.4.0
Deserialize deserializes bytes into account state
func (*Account) FromProto ¶ added in v0.4.0
func (st *Account) FromProto(acPb *iproto.AccountPb)
FromProto converts from protobuf's AccountPb
func (*Account) SubBalance ¶ added in v0.4.0
SubBalance subtracts balance for account state
type ActionHandler ¶ added in v0.4.0
type ActionHandler interface {
Handle(context.Context, action.Action, WorkingSet) (*action.Receipt, error)
}
ActionHandler is the interface for the action handlers. For each incoming action, the assembled actions will be called one by one to process it. ActionHandler implementation is supposed to parse the sub-type of the action to decide if it wants to handle this action or not.
type Candidate ¶
type Candidate struct { Address string Votes *big.Int PublicKey keypair.PublicKey CreationHeight uint64 LastUpdateHeight uint64 }
Candidate indicates the structure of a candidate
type CandidateList ¶ added in v0.3.0
type CandidateList []*Candidate
CandidateList indicates the list of Candidates which is sortable
func MapToCandidates ¶ added in v0.3.0
func MapToCandidates(candidateMap map[hash.PKHash]*Candidate) (CandidateList, error)
MapToCandidates converts a map of cachedCandidates to candidate list
func (*CandidateList) Deserialize ¶ added in v0.4.0
func (l *CandidateList) Deserialize(buf []byte) error
Deserialize deserializes bytes to list of Candidates
func (CandidateList) Len ¶ added in v0.3.0
func (l CandidateList) Len() int
func (CandidateList) Less ¶ added in v0.3.0
func (l CandidateList) Less(i, j int) bool
func (*CandidateList) Serialize ¶ added in v0.4.0
func (l *CandidateList) Serialize() ([]byte, error)
Serialize serializes a list of Candidates to bytes
func (CandidateList) Swap ¶ added in v0.3.0
func (l CandidateList) Swap(i, j int)
type Contract ¶ added in v0.3.0
type Contract interface { GetState(hash.Hash32B) ([]byte, error) SetState(hash.Hash32B, []byte) error GetCode() ([]byte, error) SetCode(hash.Hash32B, []byte) SelfState() *Account Commit() error RootHash() hash.Hash32B }
Contract is a special type of account with code and storage trie.
type Factory ¶
type Factory interface { lifecycle.StartStopper // Accounts Balance(string) (*big.Int, error) Nonce(string) (uint64, error) // Note that Nonce starts with 1. AccountState(string) (*Account, error) RootHash() hash.Hash32B Height() (uint64, error) NewWorkingSet() (WorkingSet, error) Commit(WorkingSet) error // Candidate pool CandidatesByHeight(uint64) ([]*Candidate, error) State(hash.PKHash, State) (State, error) AddActionHandlers(...ActionHandler) }
Factory defines an interface for managing states
func NewFactory ¶
func NewFactory(cfg *config.Config, opts ...FactoryOption) (Factory, error)
NewFactory creates a new state factory
type FactoryOption ¶ added in v0.3.0
FactoryOption sets Factory construction parameter
func DefaultTrieOption ¶ added in v0.3.0
func DefaultTrieOption() FactoryOption
DefaultTrieOption creates trie from config for state factory
func InMemTrieOption ¶ added in v0.3.0
func InMemTrieOption() FactoryOption
InMemTrieOption creates in memory trie for state factory
func PrecreatedTrieDBOption ¶ added in v0.4.0
func PrecreatedTrieDBOption(kv db.KVStore) FactoryOption
PrecreatedTrieDBOption uses pre-created trie DB for state factory
type RunActionsCtx ¶ added in v0.4.0
type RunActionsCtx struct { // producer who compose those actions ProducerAddr string // gas Limit for perform those actions GasLimit *uint64 // whether disable gas charge EnableGasCharge bool }
RunActionsCtx provides the runactions with auxiliary information.
type SortedSlice ¶ added in v0.4.0
type SortedSlice []interface{}
SortedSlice represents the state slice in the state factory, which is sorted by the function:
func(i interface{}, j interface{}) int
The function is expected to output 3 type of values. 0 means i and j are equal; negative integer means i is smaller i; and positive integer means i is bigger than j.
SortedSlice will be ser/des as a whole.
func (SortedSlice) Append ¶ added in v0.4.0
func (slice SortedSlice) Append(e interface{}, f func(interface{}, interface{}) int) SortedSlice
Append appends a state into the state slice
func (SortedSlice) Delete ¶ added in v0.4.0
func (slice SortedSlice) Delete(e interface{}, f func(interface{}, interface{}) int) (SortedSlice, int)
Delete deletes a state from the state slice
func (*SortedSlice) Deserialize ¶ added in v0.4.0
func (slice *SortedSlice) Deserialize(data []byte) error
Deserialize deserializes bytes into the state slice
func (SortedSlice) Get ¶ added in v0.4.0
func (slice SortedSlice) Get(e interface{}, f func(interface{}, interface{}) int) (interface{}, bool)
Get check if a state exists in the slice
func (*SortedSlice) Serialize ¶ added in v0.4.0
func (slice *SortedSlice) Serialize() ([]byte, error)
Serialize serializes the state slice into bytes
type State ¶
State is the interface, which defines the common methods for state struct to be handled by state factory
type WorkingSet ¶ added in v0.4.0
type WorkingSet interface { // states and actions LoadOrCreateAccountState(string, *big.Int) (*Account, error) Nonce(string) (uint64, error) // Note that Nonce starts with 1. CachedAccountState(string) (*Account, error) RunActions(context.Context, uint64, []action.Action) (hash.Hash32B, map[hash.Hash32B]*action.Receipt, error) Commit() error // contracts GetCodeHash(hash.PKHash) (hash.Hash32B, error) GetCode(hash.PKHash) ([]byte, error) SetCode(hash.PKHash, []byte) error GetContractState(hash.PKHash, hash.Hash32B) (hash.Hash32B, error) SetContractState(hash.PKHash, hash.Hash32B, hash.Hash32B) error // Accounts RootHash() hash.Hash32B Version() uint64 Height() uint64 // General state State(hash.PKHash, State) (State, error) CachedState(hash.PKHash, State) (State, error) PutState(hash.PKHash, State) error UpdateCachedStates(hash.PKHash, *Account) }
WorkingSet defines an interface for working set of states changes
func NewWorkingSet ¶ added in v0.4.0
func NewWorkingSet( version uint64, kv db.KVStore, root hash.Hash32B, actionHandlers []ActionHandler, ) (WorkingSet, error)
NewWorkingSet creates a new working set