ngstate

package
v0.0.0-...-e87e1d8 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2024 License: GPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

Package ngstate provides a global account status manager which controls all accounts and balance besides that, the manager can do generating current sheet for fast sync

ngstate can get init from genesis sheet or remote sheet.

To use ngstate, dev should init these modules first - ngblocks - storage

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTxRegExcess = errors.New("one address can only register one accounts")
	ErrTxRegExist  = errors.New("account is already registered by others")
)
View Source
var (
	ErrPosOutOfBound = errors.New("pos out of bound")
	ErrLenExcess     = errors.New("length is excess")
	ErrLenInvalid    = errors.New("length is invalid")
)
View Source
var ErrDestroyAccountContractNotEmpty = errors.New("contract should be empty on destroy tx")
View Source
var ErrOutOfMem = errors.New("out of the allocated memory")
View Source
var ErrSnapshotNofFound = errors.New("cannot find the snapshot")
View Source
var ErrTxrBalanceInsufficient = errors.New("account's balance is not sufficient for the tx")

Functions

func CheckBlockTxs

func CheckBlockTxs(txn *bbolt.Tx, block *ngtypes.FullBlock) error

CheckBlockTxs will check all requirements for txs in block

func CheckTx

func CheckTx(txn *bbolt.Tx, tx *ngtypes.FullTx) error

CheckTx will check the requirements for one tx (except generate tx)

Types

type SnapshotManager

type SnapshotManager struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func (*SnapshotManager) GetSnapshot

func (sm *SnapshotManager) GetSnapshot(height uint64, hash []byte) *ngtypes.Sheet

GetSnapshot return the snapshot in a balance sheet at a height, and doo hash check for external use with security ensure

func (*SnapshotManager) GetSnapshotByHash

func (sm *SnapshotManager) GetSnapshotByHash(hash []byte) *ngtypes.Sheet

GetSnapshotByHash return the snapshot in a balance sheet with the hash for internal use only

func (*SnapshotManager) GetSnapshotByHeight

func (sm *SnapshotManager) GetSnapshotByHeight(height uint64) *ngtypes.Sheet

GetSnapshotByHeight return the snapshot in a balance sheet at a height, without hash check for internal use only

func (*SnapshotManager) PutSnapshot

func (sm *SnapshotManager) PutSnapshot(height uint64, hash []byte, sheet *ngtypes.Sheet)

type State

type State struct {
	Network ngtypes.Network

	*bbolt.DB
	*SnapshotManager
	// contains filtered or unexported fields
}

State is a global set of account & txs status (nil) --> B0(Prev: S0) --> B1(Prev: S1) -> B2(Prev: S2)

init (S0,S0)  -->   (S0,S1)  -->    (S1, S2)

func InitStateFromGenesis

func InitStateFromGenesis(db *bbolt.DB, network ngtypes.Network) *State

InitStateFromGenesis will initialize the state in the given db, with the default genesis sheet data

func InitStateFromSheet

func InitStateFromSheet(db *bbolt.DB, network ngtypes.Network, sheet *ngtypes.Sheet) *State

InitStateFromSheet will initialize the state in the given db, with the sheet data this func is written for snapshot sync/converging when initializing from non-genesis checkpoint

func (*State) AccountIsRegistered

func (state *State) AccountIsRegistered(num uint64) bool

AccountIsRegistered checks whether the account is registered in state

func (*State) GetAccountByAddress

func (state *State) GetAccountByAddress(address ngtypes.Address) (*ngtypes.Account, error)

GetAccountByAddress returns an ngtypes.Account obj by the account's address this is a heavy action, so dont called by any internal part like p2p and consensus

func (*State) GetAccountByNum

func (state *State) GetAccountByNum(num uint64) (account *ngtypes.Account, err error)

GetAccountByNum returns an ngtypes.Account obj by the account's number

func (*State) GetMatureBalanceByAddress

func (state *State) GetMatureBalanceByAddress(address ngtypes.Address) (*big.Int, error)

GetMatureBalanceByAddress get the locked balance of account by the account's address

func (*State) GetMatureBalanceByNum

func (state *State) GetMatureBalanceByNum(num uint64) (*big.Int, error)

GetMatureBalanceByNum get the balance of account by the account's num

func (*State) GetSnapshot

func (state *State) GetSnapshot(height uint64, hash []byte) *ngtypes.Sheet

func (*State) GetTotalBalanceByAddress

func (state *State) GetTotalBalanceByAddress(address ngtypes.Address) (*big.Int, error)

GetTotalBalanceByAddress get the total balance of account by the account's address

func (*State) GetTotalBalanceByNum

func (state *State) GetTotalBalanceByNum(num uint64) (*big.Int, error)

GetTotalBalanceByNum get the balance of account by the account's num

func (*State) HandleTxs

func (state *State) HandleTxs(txn *bbolt.Tx, txs ...*ngtypes.FullTx) (err error)

HandleTxs will apply the tx into the state if tx is VALID

func (*State) RebuildFromBlockStore

func (state *State) RebuildFromBlockStore() error

RebuildFromBlockStore works for doing converge and remove all

func (*State) RebuildFromSheet

func (state *State) RebuildFromSheet(sheet *ngtypes.Sheet) error

RebuildFromSheet will overwrite a state from the given sheet

func (*State) Upgrade

func (state *State) Upgrade(txn *bbolt.Tx, block *ngtypes.FullBlock) error

Upgrade will apply block's txs on current state

type VM

type VM struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

VM is a vm based on wasmtime, which acts as a sandbox env to exec native func

func NewVM

func NewVM(txn *bbolt.Tx, account *ngtypes.Account) (*VM, error)

NewVM creates a new Wasm call me when a assign or append tx

func (*VM) CallOnTx

func (vm *VM) CallOnTx(ins *wasman.Instance)

CallOnTx when applying new tx

func (*VM) InitBuiltInImports

func (vm *VM) InitBuiltInImports() error

InitBuiltInImports will bind go's host func with the contract module

func (*VM) Instantiate

func (vm *VM) Instantiate(tx *ngtypes.FullTx) (*wasman.Instance, error)

Instantiate will generate a runnable instance from thr module before Instantiate, the caller should run Init

Jump to

Keyboard shortcuts

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