state

package
v0.0.0-...-a53bfeb Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2020 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Copyright © 2019 Annchain Authors <EMAIL ADDRESS>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

View Source
const (
	MaxTokenName   = 20
	MaxTokenSymbol = 5
)
View Source
const (
	KeyPrefix string = "tkID"
)
View Source
const TokenNotDirtied int32 = -1

Variables

View Source
var MaxTrieCacheGen = uint16(120)

MaxTrieCacheGen is trie cache generation limit after which to evict trie nodes from memory.

View Source
var (
	OGTokenID = int32(0)
)

token id

Functions

func LatestTokenIDTrieKey

func LatestTokenIDTrieKey() []byte

func TokenTrieKey

func TokenTrieKey(tokenID int32) []byte

Types

type AccountData

type AccountData struct {
	Address  ogTypes.Address
	Balances BalanceSet
	Nonce    uint64
	Root     ogTypes.Hash
	CodeHash []byte
}

func NewAccountData

func NewAccountData() AccountData

func (*AccountData) DecodeMsg

func (z *AccountData) DecodeMsg(dc *msgp.Reader) (err error)

DecodeMsg implements msgp.Decodable

func (*AccountData) EncodeMsg

func (z *AccountData) EncodeMsg(en *msgp.Writer) (err error)

EncodeMsg implements msgp.Encodable

func (*AccountData) MarshalMsg

func (a *AccountData) MarshalMsg() (b []byte, err error)

func (*AccountData) MsgSize

func (a *AccountData) MsgSize() int

func (*AccountData) UnmarshalMsg

func (a *AccountData) UnmarshalMsg(b []byte) ([]byte, error)

type BalanceSet

type BalanceSet map[int32]*math.BigInt

func NewBalanceSet

func NewBalanceSet() BalanceSet

func (*BalanceSet) Copy

func (bs *BalanceSet) Copy() BalanceSet

func (*BalanceSet) IsEmpty

func (bs *BalanceSet) IsEmpty() bool

func (*BalanceSet) MarshalMsg

func (bs *BalanceSet) MarshalMsg() (b []byte, err error)

func (*BalanceSet) MsgSize

func (bs *BalanceSet) MsgSize() int

func (*BalanceSet) PreAdd

func (bs *BalanceSet) PreAdd(tokenID int32, increment *math.BigInt) *math.BigInt

func (*BalanceSet) PreSub

func (bs *BalanceSet) PreSub(tokenID int32, decrement *math.BigInt) *math.BigInt

func (*BalanceSet) UnmarshalMsg

func (bs *BalanceSet) UnmarshalMsg(bts []byte) (b []byte, err error)

type Database

type Database interface {
	// OpenTrie opens the main account trie.
	OpenTrie(root ogTypes.Hash) (Trie, error)

	// OpenStorageTrie opens the storage trie of an account.
	OpenStorageTrie(addrHash, root ogTypes.Hash) (Trie, error)

	// CopyTrie returns an independent copy of the given trie.
	CopyTrie(Trie) Trie

	// ContractCode retrieves a particular contract's code.
	ContractCode(addrHash, codeHash ogTypes.Hash) ([]byte, error)

	// ContractCodeSize retrieves a particular contracts code's size.
	ContractCodeSize(addrHash, codeHash ogTypes.Hash) (int, error)

	// TrieDB retrieves the low level trie database used for data storage.
	TrieDB() *trie.Database
}

Database wraps access to tries and contract code.

func NewDatabase

func NewDatabase(db ogdb.Database) Database

NewDatabase creates a backing store for state. The returned database is safe for concurrent use and retains cached trie nodes in memory. The pool is an optional intermediate trie-node memory pool between the low level storage layer and the high level trie abstraction.

type JournalEntry

type JournalEntry interface {
	// revert undoes the changes introduced by this journal entry.
	Revert(db *StateDB)

	// dirtied returns the address modified by this journal entry.
	Dirtied() ogTypes.Address

	// TokenDirtied returns the Token ID modified by this journal entry.
	TokenDirtied() int32
}

journalEntry is a modification entry in the state change journal that can be reverted on demand.

type StateDB

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

StateDB stores account's data. Account's data include address, balance, nonce, code and its contract db if it is an contract address. An account is stored as a StateObject, for more detail please check StateObject struct.

func NewStateDB

func NewStateDB(conf StateDBConfig, db Database, root ogTypes.Hash) (*StateDB, error)

func (*StateDB) AddBalance

func (sd *StateDB) AddBalance(addr ogTypes.Address, increment *math.BigInt)

AddBalance

func (*StateDB) AddPreimage

func (sd *StateDB) AddPreimage(h ogTypes.Hash, b []byte)

func (*StateDB) AddRefund

func (sd *StateDB) AddRefund(increment uint64)

func (*StateDB) AddTokenBalance

func (sd *StateDB) AddTokenBalance(addr ogTypes.Address, tokenID int32, increment *math.BigInt)

func (*StateDB) AppendJournal

func (sd *StateDB) AppendJournal(entry JournalEntry)

func (*StateDB) ClearJournalAndRefund

func (sd *StateDB) ClearJournalAndRefund()

func (*StateDB) Commit

func (sd *StateDB) Commit() (ogTypes.Hash, error)

Commit tries to save dirty data to memory trie db.

func (*StateDB) CreateAccount

func (sd *StateDB) CreateAccount(addr ogTypes.Address)

CreateAccount will create a new state for input address.

func (*StateDB) Database

func (sd *StateDB) Database() Database

func (*StateDB) DeleteStateObject

func (sd *StateDB) DeleteStateObject(addr ogTypes.Address) error

DeleteStateObject remove a state from StateDB. Return error if it fails.

func (*StateDB) DestroyToken

func (sd *StateDB) DestroyToken(tokenID int32) error

func (*StateDB) Empty

func (sd *StateDB) Empty(addr ogTypes.Address) bool

func (*StateDB) Exist

func (sd *StateDB) Exist(addr ogTypes.Address) bool

func (*StateDB) ForEachStorage

func (sd *StateDB) ForEachStorage(addr ogTypes.Address, f func(key, value ogTypes.Hash) bool)

func (*StateDB) GetAllTokenBalance

func (sd *StateDB) GetAllTokenBalance(addr ogTypes.Address) BalanceSet

func (*StateDB) GetBalance

func (sd *StateDB) GetBalance(addr ogTypes.Address) *math.BigInt

func (*StateDB) GetCode

func (sd *StateDB) GetCode(addr ogTypes.Address) []byte

func (*StateDB) GetCodeHash

func (sd *StateDB) GetCodeHash(addr ogTypes.Address) ogTypes.Hash

func (*StateDB) GetCodeSize

func (sd *StateDB) GetCodeSize(addr ogTypes.Address) int

func (*StateDB) GetCommittedState

func (sd *StateDB) GetCommittedState(addr ogTypes.Address, key ogTypes.Hash) ogTypes.Hash

func (*StateDB) GetNonce

func (sd *StateDB) GetNonce(addr ogTypes.Address) uint64

func (*StateDB) GetOrCreateStateObject

func (sd *StateDB) GetOrCreateStateObject(addr ogTypes.Address) *StateObject

GetOrCreateStateObject will find a state from memory by account address. If state not exists, it will load a state from db.

func (*StateDB) GetRefund

func (sd *StateDB) GetRefund() uint64

func (*StateDB) GetState

func (sd *StateDB) GetState(addr ogTypes.Address, key ogTypes.Hash) ogTypes.Hash

func (*StateDB) GetStateObject

func (sd *StateDB) GetStateObject(addr ogTypes.Address) *StateObject

GetStateObject get a state from StateDB. If state not exist, load it from db.

func (*StateDB) GetTokenBalance

func (sd *StateDB) GetTokenBalance(addr ogTypes.Address, tokenID int32) *math.BigInt

func (*StateDB) GetTokenObject

func (sd *StateDB) GetTokenObject(tokenID int32) *TokenObject

GetTokenObject get token object from StateDB.tokens . If not exists then try to load from trie db.

func (*StateDB) HasSuicided

func (sd *StateDB) HasSuicided(addr ogTypes.Address) bool

func (*StateDB) IssueToken

func (sd *StateDB) IssueToken(issuer ogTypes.Address, name, symbol string, reIssuable bool, fstIssue *math.BigInt) (int32, error)

IssueToken creates a new token according to offered token information.

func (*StateDB) LatestTokenID

func (sd *StateDB) LatestTokenID() int32

func (*StateDB) ReIssueToken

func (sd *StateDB) ReIssueToken(tokenID int32, amount *math.BigInt) error

func (*StateDB) RevertToSnapshot

func (sd *StateDB) RevertToSnapshot(snapshotid int)

func (*StateDB) Root

func (sd *StateDB) Root() ogTypes.Hash

func (*StateDB) SetBalance

func (sd *StateDB) SetBalance(addr ogTypes.Address, balance *math.BigInt)

SetBalance set origin OG token balance. TODO should be modified to satisfy all tokens.

func (*StateDB) SetCode

func (sd *StateDB) SetCode(addr ogTypes.Address, code []byte)

func (*StateDB) SetNonce

func (sd *StateDB) SetNonce(addr ogTypes.Address, nonce uint64)

func (*StateDB) SetState

func (sd *StateDB) SetState(addr ogTypes.Address, key, value ogTypes.Hash)

func (*StateDB) SetStateObject

func (sd *StateDB) SetStateObject(addr ogTypes.Address, stobj *StateObject)

func (*StateDB) SetTokenBalance

func (sd *StateDB) SetTokenBalance(addr ogTypes.Address, tokenID int32, balance *math.BigInt)

func (*StateDB) Snapshot

func (sd *StateDB) Snapshot() int

func (*StateDB) Stop

func (sd *StateDB) Stop()

func (*StateDB) String

func (sd *StateDB) String() string

func (*StateDB) SubBalance

func (sd *StateDB) SubBalance(addr ogTypes.Address, decrement *math.BigInt)

SubBalance

func (*StateDB) SubRefund

func (sd *StateDB) SubRefund(decrement uint64)

func (*StateDB) SubTokenBalance

func (sd *StateDB) SubTokenBalance(addr ogTypes.Address, tokenID int32, decrement *math.BigInt)

func (*StateDB) Suicide

func (sd *StateDB) Suicide(addr ogTypes.Address) bool

type StateDBConfig

type StateDBConfig struct {
	PurgeTimer     time.Duration
	BeatExpireTime time.Duration
}

func DefaultStateDBConfig

func DefaultStateDBConfig() StateDBConfig

type StateDBInterface

type StateDBInterface interface {
	CreateAccount(ogTypes.Address)

	SubBalance(ogTypes.Address, *math.BigInt)
	SubTokenBalance(ogTypes.Address, int32, *math.BigInt)
	AddBalance(ogTypes.Address, *math.BigInt)
	AddTokenBalance(ogTypes.Address, int32, *math.BigInt)
	SetTokenBalance(ogTypes.Address, int32, *math.BigInt)
	// Retrieve the balance from the given address or 0 if object not found
	GetBalance(ogTypes.Address) *math.BigInt
	GetTokenBalance(ogTypes.Address, int32) *math.BigInt

	GetNonce(ogTypes.Address) uint64
	SetNonce(ogTypes.Address, uint64)

	GetCodeHash(ogTypes.Address) ogTypes.Hash
	GetCode(ogTypes.Address) []byte
	SetCode(ogTypes.Address, []byte)
	GetCodeSize(ogTypes.Address) int

	// AddRefund adds gas to the refund counter
	AddRefund(uint64)
	// SubRefund removes gas from the refund counter.
	// This method will panic if the refund counter goes below zero
	SubRefund(uint64)
	// GetRefund returns the current value of the refund counter.
	GetRefund() uint64

	GetCommittedState(ogTypes.Address, ogTypes.Hash) ogTypes.Hash
	// GetState retrieves a value from the given account's storage trie.
	GetState(ogTypes.Address, ogTypes.Hash) ogTypes.Hash
	SetState(ogTypes.Address, ogTypes.Hash, ogTypes.Hash)

	AppendJournal(JournalEntry)

	// Suicide marks the given account as suicided.
	// This clears the account balance.
	//
	// The account's state object is still available until the state is committed,
	// getStateObject will return a non-nil account after Suicide.
	Suicide(ogTypes.Address) bool
	HasSuicided(ogTypes.Address) bool

	// Exist reports whether the given account exists in state.
	// Notably this should also return true for suicided accounts.
	Exist(ogTypes.Address) bool
	// Empty returns whether the given account is empty. Empty
	// is defined according to EIP161 (balance = nonce = code = 0).
	Empty(ogTypes.Address) bool

	// RevertToSnapshot reverts all state changes made since the given revision.
	RevertToSnapshot(int)
	// Snapshot creates a new revision
	Snapshot() int

	//AddLog(*Log)
	AddPreimage(ogTypes.Hash, []byte)

	ForEachStorage(ogTypes.Address, func(ogTypes.Hash, ogTypes.Hash) bool)
	// for debug.
	String() string
}

StateDB is an OVM database for full state querying.

type StateObject

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

func NewStateObject

func NewStateObject(addr ogTypes.Address, db StateDBInterface) *StateObject

func (*StateObject) AddBalance

func (s *StateObject) AddBalance(tokenID int32, increment *math.BigInt)

func (*StateObject) CommitStorage

func (s *StateObject) CommitStorage(db Database, preCommit bool) error

func (*StateObject) Copy

func (s *StateObject) Copy(src *StateObject)

func (*StateObject) Decode

func (s *StateObject) Decode(b []byte, db *StateDB) error

func (*StateObject) DecodeMsg

func (z *StateObject) DecodeMsg(dc *msgp.Reader) (err error)

DecodeMsg implements msgp.Decodable

func (*StateObject) Encode

func (s *StateObject) Encode() ([]byte, error)

func (StateObject) EncodeMsg

func (z StateObject) EncodeMsg(en *msgp.Writer) (err error)

EncodeMsg implements msgp.Encodable

func (*StateObject) GetAllBalance

func (s *StateObject) GetAllBalance() BalanceSet

func (*StateObject) GetBalance

func (s *StateObject) GetBalance(tokenID int32) *math.BigInt

func (*StateObject) GetCode

func (s *StateObject) GetCode(db Database) []byte

func (*StateObject) GetCodeHash

func (s *StateObject) GetCodeHash() ogTypes.Hash

func (*StateObject) GetCodeSize

func (s *StateObject) GetCodeSize(db Database) (int, error)

func (*StateObject) GetCommittedState

func (s *StateObject) GetCommittedState(db Database, key ogTypes.Hash) ogTypes.Hash

func (*StateObject) GetNonce

func (s *StateObject) GetNonce() uint64

func (*StateObject) GetState

func (s *StateObject) GetState(db Database, key ogTypes.Hash) ogTypes.Hash

func (*StateObject) Map

func (s *StateObject) Map() map[string]interface{}

func (StateObject) MarshalMsg

func (z StateObject) MarshalMsg(b []byte) (o []byte, err error)

MarshalMsg implements msgp.Marshaler

func (StateObject) Msgsize

func (z StateObject) Msgsize() (s int)

Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message

func (*StateObject) SetBalance

func (s *StateObject) SetBalance(tokenID int32, balance *math.BigInt)

func (*StateObject) SetCode

func (s *StateObject) SetCode(codehash ogTypes.Hash, code []byte)

func (*StateObject) SetNonce

func (s *StateObject) SetNonce(nonce uint64)

func (*StateObject) SetState

func (s *StateObject) SetState(db Database, key, value ogTypes.Hash)

func (*StateObject) String

func (s *StateObject) String() string

func (*StateObject) SubBalance

func (s *StateObject) SubBalance(tokenID int32, decrement *math.BigInt)

func (*StateObject) Uncache

func (s *StateObject) Uncache()

Uncache clears dirtyStorage and committedStorage. This is aimed to check if state is committed into db.

Note that this function is for test debug only, should not be called by other functions.

func (*StateObject) UnmarshalMsg

func (z *StateObject) UnmarshalMsg(bts []byte) (o []byte, err error)

UnmarshalMsg implements msgp.Unmarshaler

type TokenObject

type TokenObject struct {
	TokenID    int32
	Name       string
	Symbol     string
	Issuer     ogtypes.Address
	ReIssuable bool

	Issues    []*math.BigInt
	Destroyed bool
	// contains filtered or unexported fields
}

func NewTokenObject

func NewTokenObject(tokenID int32, issuer ogtypes.Address, name, symbol string, reIssuable bool, fstIssue *math.BigInt, db StateDBInterface) *TokenObject

func (*TokenObject) AllIssues

func (t *TokenObject) AllIssues() []*math.BigInt

func (*TokenObject) CanReIssue

func (t *TokenObject) CanReIssue() bool

func (*TokenObject) CopyRaw

func (t *TokenObject) CopyRaw(tObj *TokenObject)

func (*TokenObject) Decode

func (t *TokenObject) Decode(b []byte) error

func (*TokenObject) Destroy

func (t *TokenObject) Destroy()

func (*TokenObject) Encode

func (t *TokenObject) Encode() ([]byte, error)

func (*TokenObject) GetID

func (t *TokenObject) GetID() int32

func (*TokenObject) GetName

func (t *TokenObject) GetName() string

func (*TokenObject) GetSymbol

func (t *TokenObject) GetSymbol() string

func (*TokenObject) MarshalMsg

func (t *TokenObject) MarshalMsg() ([]byte, error)

func (*TokenObject) MsgSize

func (t *TokenObject) MsgSize() int

func (*TokenObject) OneIssue

func (t *TokenObject) OneIssue(term int) *math.BigInt

func (*TokenObject) ReIssue

func (t *TokenObject) ReIssue(amount *math.BigInt) error

func (*TokenObject) UnmarshalMsg

func (t *TokenObject) UnmarshalMsg(b []byte) ([]byte, error)

type Trie

type Trie interface {
	TryGet(key []byte) ([]byte, error)
	TryUpdate(key, value []byte) error
	TryDelete(key []byte) error
	// preCommit is a flag for pre confirming sequencer. Because pre confirm uses the
	// same trie db in real sequencer confirm and the db will be modified during db commit.
	// To avoid this, add a flag to let pre confirm process not modify trie db anymore.
	Commit(onleaf trie.LeafCallback, preCommit bool) (ogTypes.Hash, error)
	Hash() ogTypes.Hash
	NodeIterator(startKey []byte) trie.NodeIterator
	GetKey([]byte) []byte // TODO(fjl): remove this when SecureTrie is removed
	Prove(key []byte, fromLevel uint, proofDb ogdb.Putter) error
}

Trie is a Ethereum Merkle Trie.

Jump to

Keyboard shortcuts

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