state

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2018 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
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

View Source
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")
)
View Source
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")
)
View Source
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

func CandidatesToMap(candidates CandidateList) (map[hash.PKHash]*Candidate, error)

CandidatesToMap converts a candidate list to map of cachedCandidates

func GobBasedDeserialize

func GobBasedDeserialize(state State, data []byte) error

GobBasedDeserialize deserialize a state from bytes via gob

func GobBasedSerialize

func GobBasedSerialize(state State) ([]byte, error)

GobBasedSerialize serializes a state into bytes via gob

func WithRunActionsCtx

func WithRunActionsCtx(ctx context.Context, ra RunActionsCtx) context.Context

WithRunActionsCtx add RunActionsCtx into context.

Types

type Account

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

func (st *Account) AddBalance(amount *big.Int) error

AddBalance adds balance for account state

func (*Account) Deserialize

func (st *Account) Deserialize(buf []byte) error

Deserialize deserializes bytes into account state

func (*Account) FromProto

func (st *Account) FromProto(acPb *iproto.AccountPb)

FromProto converts from protobuf's AccountPb

func (*Account) Serialize

func (st *Account) Serialize() ([]byte, error)

Serialize serializes account state into bytes

func (*Account) SubBalance

func (st *Account) SubBalance(amount *big.Int) error

SubBalance subtracts balance for account state

func (*Account) ToProto

func (st *Account) ToProto() *iproto.AccountPb

ToProto converts to protobuf's AccountPb

type ActionHandler

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

type CandidateList []*Candidate

CandidateList indicates the list of Candidates which is sortable

func MapToCandidates

func MapToCandidates(candidateMap map[hash.PKHash]*Candidate) (CandidateList, error)

MapToCandidates converts a map of cachedCandidates to candidate list

func (*CandidateList) Deserialize

func (l *CandidateList) Deserialize(buf []byte) error

Deserialize deserializes bytes to list of Candidates

func (CandidateList) Len

func (l CandidateList) Len() int

func (CandidateList) Less

func (l CandidateList) Less(i, j int) bool

func (*CandidateList) Serialize

func (l *CandidateList) Serialize() ([]byte, error)

Serialize serializes a list of Candidates to bytes

func (CandidateList) Swap

func (l CandidateList) Swap(i, j int)

type Contract

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

type FactoryOption func(*factory, *config.Config) error

FactoryOption sets Factory construction parameter

func DefaultTrieOption

func DefaultTrieOption() FactoryOption

DefaultTrieOption creates trie from config for state factory

func InMemTrieOption

func InMemTrieOption() FactoryOption

InMemTrieOption creates in memory trie for state factory

func PrecreatedTrieDBOption

func PrecreatedTrieDBOption(kv db.KVStore) FactoryOption

PrecreatedTrieDBOption uses pre-created trie DB for state factory

type RunActionsCtx

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

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

func (slice SortedSlice) Append(e interface{}, f func(interface{}, interface{}) int) SortedSlice

Append appends a state into the state slice

func (SortedSlice) Delete

func (slice SortedSlice) Delete(e interface{}, f func(interface{}, interface{}) int) (SortedSlice, int)

Delete deletes a state from the state slice

func (*SortedSlice) Deserialize

func (slice *SortedSlice) Deserialize(data []byte) error

Deserialize deserializes bytes into the state slice

func (SortedSlice) Get

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

func (slice *SortedSlice) Serialize() ([]byte, error)

Serialize serializes the state slice into bytes

type State

type State interface {
	Serialize() ([]byte, error)
	Deserialize(data []byte) error
}

State is the interface, which defines the common methods for state struct to be handled by state factory

type WorkingSet

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

func NewWorkingSet(
	version uint64,
	kv db.KVStore,
	root hash.Hash32B,
	actionHandlers []ActionHandler,
) (WorkingSet, error)

NewWorkingSet creates a new working set

Jump to

Keyboard shortcuts

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