state

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2018 License: Apache-2.0 Imports: 18 Imported by: 41

Documentation

Index

Constants

View Source
const (
	// CurrentHeightKey indicates the key of current factory height in underlying database
	CurrentHeightKey = "currentHeight"
	// AccountTrieRootKey indicates the key of accountTrie root hash in underlying database
	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")

	// ErrAccountNotExist is the error that the account does not exist
	ErrAccountNotExist = errors.New("account does not exist")

	// ErrAccountCollision is the error that the account already exists
	ErrAccountCollision = errors.New("account already exists")

	// ErrFailedToMarshalState is the error that the state marshaling is failed
	ErrFailedToMarshalState = errors.New("failed to marshal state")

	// ErrFailedToUnmarshalState is the error that the state un-marshaling is failed
	ErrFailedToUnmarshalState = errors.New("failed to unmarshal state")
)

Functions

func CandidatesToMap added in v0.3.0

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

CandidatesToMap converts a candidate list to map of cachedCandidates

func Serialize added in v0.3.0

func Serialize(candidates CandidateList) ([]byte, error)

Serialize serializes a list of candidates to bytes

Types

type Candidate

type Candidate struct {
	Address          string
	Votes            *big.Int
	PubKey           []byte
	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 Deserialize added in v0.3.0

func Deserialize(buf []byte) (CandidateList, error)

Deserialize deserializes bytes to list of candidates

func MapToCandidates added in v0.3.0

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

MapToCandidates converts a map of cachedCandidates to candidate list

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) 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() *State
	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
	LoadOrCreateState(string, uint64) (*State, error)
	Balance(string) (*big.Int, error)
	Nonce(string) (uint64, error) // Note that nonce starts with 1.
	State(string) (*State, error)
	CachedState(string) (*State, error)
	RootHash() hash.Hash32B
	Height() (uint64, error)
	CommitStateChanges(uint64, []*action.Transfer, []*action.Vote, []*action.Execution) error
	// Contracts
	GetCodeHash(hash.AddrHash) (hash.Hash32B, error)
	GetCode(hash.AddrHash) ([]byte, error)
	SetCode(hash.AddrHash, []byte) error
	GetContractState(hash.AddrHash, hash.Hash32B) (hash.Hash32B, error)
	SetContractState(hash.AddrHash, hash.Hash32B, hash.Hash32B) error
	// Candidate pool
	Candidates() (uint64, []*Candidate)
	CandidatesByHeight(uint64) ([]*Candidate, error)
}

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

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

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 PrecreatedTrieOption added in v0.3.0

func PrecreatedTrieOption(accountTrie trie.Trie) FactoryOption

PrecreatedTrieOption uses pre-created tries for state factory

type State

type State 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
	Voters       map[string]*big.Int
}

State is the canonical representation of an account.

func (*State) AddBalance

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

AddBalance adds balance for state

func (*State) SubBalance

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

SubBalance subtracts balance for state

Jump to

Keyboard shortcuts

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