state

package
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2021 License: Apache-2.0 Imports: 41 Imported by: 0

README

state

状态机逻辑实现。

对外接口

NewLedgerCtx() NewState()

RegisterAclMG(t.ctx.Acl) RegisterContractMG(t.ctx.Contract)

Documentation

Overview

统一定义状态机对外暴露功能

Index

Constants

View Source
const (
	LatestBlockKey = "pointer"

	// BetaTxVersion 为当前代码支持的最高交易版本
	BetaTxVersion  = 3
	RootTxVersion  = 0
	FeePlaceholder = "$"
	// TxSizePercent max percent of txs' size in one block
	TxSizePercent = 0.8

	TxWaitTimeout = 5
)

Variables

View Source
var (
	ErrDoubleSpent          = errors.New("utxo can not be spent more than once")
	ErrAlreadyInUnconfirmed = errors.New("this transaction is in unconfirmed state")
	ErrPreBlockMissMatch    = errors.New("play block failed because pre-hash != latest_block")
	ErrUnexpected           = errors.New("this is a unexpected error")
	ErrInvalidAutogenTx     = errors.New("found invalid autogen-tx")
	ErrUTXODuplicated       = errors.New("found duplicated utxo in same tx")
	ErrRWSetInvalid         = errors.New("RWSet of transaction invalid")
	ErrACLNotEnough         = errors.New("ACL not enough")
	ErrInvalidSignature     = errors.New("the signature is invalid or not match the address")

	ErrGasNotEnough   = errors.New("Gas not enough")
	ErrVersionInvalid = errors.New("Invalid tx version")
	ErrInvalidAccount = errors.New("Invalid account")
	ErrInvalidTxExt   = errors.New("Invalid tx ext")
	ErrTxTooLarge     = errors.New("Tx size is too large")

	ErrParseContractUtxos   = errors.New("Parse contract utxos error")
	ErrContractTxAmout      = errors.New("Contract transfer amount error")
	ErrGetReservedContracts = errors.New("Get reserved contracts error")
)

Functions

func GetVersion

func GetVersion(txIn *protos.TxInputExt) string

Types

type BlockAgent

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

func NewBlockAgent

func NewBlockAgent(blk *lpb.InternalBlock) *BlockAgent

func (*BlockAgent) GetBlockid

func (t *BlockAgent) GetBlockid() []byte

func (*BlockAgent) GetConsensusStorage

func (t *BlockAgent) GetConsensusStorage() ([]byte, error)

共识记录信息,xledger账本由于历史原因需要做下转换

func (*BlockAgent) GetHeight

func (t *BlockAgent) GetHeight() int64

func (*BlockAgent) GetInTrunk

func (t *BlockAgent) GetInTrunk() bool

func (*BlockAgent) GetNextHash

func (t *BlockAgent) GetNextHash() []byte

func (*BlockAgent) GetPreHash

func (t *BlockAgent) GetPreHash() []byte

func (*BlockAgent) GetProposer

func (t *BlockAgent) GetProposer() []byte

func (*BlockAgent) GetPublicKey

func (t *BlockAgent) GetPublicKey() string

func (*BlockAgent) GetSign

func (t *BlockAgent) GetSign() []byte

func (*BlockAgent) GetTimestamp

func (t *BlockAgent) GetTimestamp() int64

func (*BlockAgent) GetTxIDs

func (t *BlockAgent) GetTxIDs() []string

func (*BlockAgent) MakeBlockId

func (t *BlockAgent) MakeBlockId() ([]byte, error)

计算BlockId

func (*BlockAgent) SetItem

func (t *BlockAgent) SetItem(item string, value interface{}) error

用于pow挖矿时需更新nonce

type BlockHeightNotifier

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

BlockHeightNotifier hold the the latest block's height information of utxovm and notify listeners when information changed

func NewBlockHeightNotifier

func NewBlockHeightNotifier() *BlockHeightNotifier

NewBlockHeightNotifier instances a new BlockHeightNotifier

func (*BlockHeightNotifier) UpdateHeight

func (b *BlockHeightNotifier) UpdateHeight(height int64)

UpdateHeight update the height information and notify all listeners

func (*BlockHeightNotifier) WaitHeight

func (b *BlockHeightNotifier) WaitHeight(target int64) int64

WaitHeight wait util the height of current block >= target

type ConsensusStorage

type ConsensusStorage struct {
	TargetBits  int32           `json:"targetBits,omitempty"`
	Justify     *lpb.QuorumCert `json:"justify,omitempty"`
	CurTerm     int64           `json:"curTerm,omitempty"`
	CurBlockNum int64           `json:"curBlockNum,omitempty"`
}

兼容xledger账本历史原因共识部分字段分开存储在区块中

type InvokeRequest

type InvokeRequest struct {
	ModuleName   string            `json:"module_name" mapstructure:"module_name"`
	ContractName string            `json:"contract_name" mapstructure:"contract_name"`
	MethodName   string            `json:"method_name" mapstructure:"method_name"`
	Args         map[string]string `json:"args" mapstructure:"args"`
}

InvokeRequest define genesis reserved_contracts configure

type State

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

func NewState

func NewState(sctx *context.StateCtx) (*State, error)

func (*State) BucketCacheDelete

func (t *State) BucketCacheDelete(bucket, version string)

func (*State) ClearCache

func (t *State) ClearCache()

func (*State) Close

func (t *State) Close()

func (*State) CreateSnapshot

func (t *State) CreateSnapshot(blkId []byte) (kledger.XMReader, error)

根据指定blockid创建快照(Select方法不可用)

func (*State) CreateUtxoReader

func (t *State) CreateUtxoReader() contract.UtxoReader

func (*State) CreateXMReader

func (t *State) CreateXMReader() kledger.XMReader

创建获取最新状态数据XMReader

func (*State) CreateXMSnapshotReader

func (t *State) CreateXMSnapshotReader(blkId []byte) (kledger.XMSnapshotReader, error)

根据指定blockid创建快照(相比XMReader,只有Get方法,直接返回[]byte)

func (*State) DoTx

func (t *State) DoTx(tx *pb.Transaction) error

执行交易

func (*State) GenRWSetFromTx

func (t *State) GenRWSetFromTx(tx *pb.Transaction) ([]*kledger.VersionedData, []*kledger.PureData, error)

func (*State) GetAccountAddresses

func (t *State) GetAccountAddresses(accountName string) ([]string, error)

func (*State) GetAccountContracts

func (t *State) GetAccountContracts(account string) ([]string, error)

func (*State) GetBalance

func (t *State) GetBalance(addr string) (*big.Int, error)

查询账余额

func (*State) GetBalanceDetail

func (t *State) GetBalanceDetail(addr string) ([]*pb.BalanceDetailInfo, error)

GetFrozenBalance 查询Address的被冻结的余额 / 未冻结的余额

func (*State) GetContractStatus

func (t *State) GetContractStatus(contractName string) (*protos.ContractStatus, error)

查询合约状态

func (*State) GetFrozenBalance

func (t *State) GetFrozenBalance(addr string) (*big.Int, error)

func (*State) GetLDB

func (t *State) GetLDB() kvdb.Database

func (*State) GetLatestBlockid

func (t *State) GetLatestBlockid() []byte

func (*State) GetMaxBlockSize

func (t *State) GetMaxBlockSize() int64

func (*State) GetMeta

func (t *State) GetMeta() *pb.UtxoMeta

查找状态机meta信息

func (*State) GetReservedContractRequests

func (t *State) GetReservedContractRequests(req []*protos.InvokeRequest, isPreExec bool) ([]*protos.InvokeRequest, error)

geReservedContractRequest get reserved contract requests from system params, it doesn't consume gas.

func (*State) GetTimerTx

func (t *State) GetTimerTx(blockHeight int64) (*pb.Transaction, error)

func (*State) GetTipSnapshot

func (t *State) GetTipSnapshot() (kledger.XMReader, error)

获取最新确认高度快照(Select方法不可用)

func (*State) GetTipXMSnapshotReader

func (t *State) GetTipXMSnapshotReader() (kledger.XMSnapshotReader, error)

获取状态机最新确认高度快照(相比XMReader,只有Get方法,直接返回[]byte)

func (*State) GetTotal

func (t *State) GetTotal() *big.Int

GetTotal 返回当前总资产

func (*State) GetUnconfirmedTx

func (t *State) GetUnconfirmedTx(dedup bool) ([]*pb.Transaction, error)

获取一批未确认交易(用于矿工打包区块)

func (*State) HasTx

func (t *State) HasTx(txid []byte) (bool, error)

HasTx 查询一笔交易是否在unconfirm表 这些可能是放在tx对外提供

func (*State) ImmediateVerifyAutoTx

func (t *State) ImmediateVerifyAutoTx(blockHeight int64, tx *pb.Transaction, isRootTx bool) (bool, error)

ImmediateVerifyTx verify auto tx Immediately Transaction verification workflow:

  1. 其实可以直接判断二者的txid,相同,则包括读写集在内的内容都相同
  2. verify transaction ID is the same with data hash
  3. run contract requests and verify if the RWSet result is the same with preExed RWSet (heavy operation, keep it at last)

func (*State) ImmediateVerifyTx

func (t *State) ImmediateVerifyTx(tx *pb.Transaction, isRootTx bool) (bool, error)

ImmediateVerifyTx verify tx Immediately Transaction verification workflow:

  1. verify transaction ID is the same with data hash
  2. verify all signatures of initiator and auth requires
  3. verify the utxo input, there are three kinds of input validation 1). PKI technology for transferring from address 2). Account ACL for transferring from account 3). Contract logic transferring from contract
  4. verify the contract requests' permission
  5. verify the permission of contract RWSet (WriteSet could including unauthorized data change)
  6. run contract requests and verify if the RWSet result is the same with preExed RWSet (heavy operation, keep it at last)

func (*State) MaxTxSizePerBlock

func (t *State) MaxTxSizePerBlock() (int, error)

func (*State) NewBatch

func (t *State) NewBatch() kvdb.Batch

func (*State) Play

func (t *State) Play(blockid []byte) error

执行区块

func (*State) PlayAndRepost

func (t *State) PlayAndRepost(blockid []byte, needRepost bool, isRootTx bool) error

执行和发送区块 PlayAndRepost 执行一个新收到的block,要求block的pre_hash必须是当前vm的latest_block 执行后会更新latestBlockid

func (*State) PlayForMiner

func (t *State) PlayForMiner(blockid []byte) error

func (*State) QueryAccountACL

func (t *State) QueryAccountACL(accountName string) (*protos.Acl, error)

func (*State) QueryAccountContainAK

func (t *State) QueryAccountContainAK(address string) ([]string, error)

func (*State) QueryAccountGovernTokenBalance

func (t *State) QueryAccountGovernTokenBalance(accountName string) (*protos.GovernTokenBalance, error)

func (*State) QueryBlock

func (t *State) QueryBlock(blockid []byte) (kledger.BlockHandle, error)

func (*State) QueryContractMethodACL

func (t *State) QueryContractMethodACL(contractName string, methodName string) (*protos.Acl, error)

func (*State) QueryContractStatData

func (t *State) QueryContractStatData() (*protos.ContractStatData, error)

func (*State) QueryTransaction

func (t *State) QueryTransaction(txid []byte) (*pb2.Transaction, error)

func (*State) QueryTx

func (t *State) QueryTx(txid []byte) (*pb.Transaction, bool, error)

查询交易

func (*State) QueryUtxoRecord

func (t *State) QueryUtxoRecord(accountName string, displayCount int64) (*pb.UtxoRecordDetail, error)

func (*State) ReverseTx

func (t *State) ReverseTx(FromAddr string, batch kvdb.Batch, Amount string) (*pb.Transaction, error)

(目前是凭空产生的,这笔产生的资源不加入系统的总资源)

func (*State) RollBackUnconfirmedTx

func (t *State) RollBackUnconfirmedTx() (map[string]bool, []*pb.Transaction, error)

回滚全部未确认交易

func (*State) SelectUtxos

func (t *State) SelectUtxos(fromAddr string, totalNeed *big.Int, needLock, excludeUnconfirmed bool) ([]*protos.TxInput, [][]byte, *big.Int, error)

选择足够金额的utxo

func (*State) SelectUtxosBySize

func (t *State) SelectUtxosBySize(fromAddr string, needLock, excludeUnconfirmed bool) ([]*protos.TxInput, [][]byte, *big.Int, error)

func (*State) SetAclMG

func (t *State) SetAclMG(aclMgr aclBase.AclManager)

func (*State) SetContractMG

func (t *State) SetContractMG(contractMgr contract.Manager)

func (*State) SetGovernTokenMG

func (t *State) SetGovernTokenMG(governTokenMgr governToken.GovManager)

func (*State) SetProposalMG

func (t *State) SetProposalMG(proposalMgr propose.ProposeManager)

func (*State) SetTimerTaskMG

func (t *State) SetTimerTaskMG(timerTaskMgr timerTask.TimerManager)

func (*State) VerifyContractOwnerPermission

func (t *State) VerifyContractOwnerPermission(contractName string, authRequire []string) error

VerifyContractOwnerPermission implement Contract ChainCore, used to verify contract ownership permisson

func (*State) VerifyContractPermission

func (t *State) VerifyContractPermission(initiator string, authRequire []string, contractName, methodName string) (bool, error)

VerifyContractPermission implement Contract ChainCore, used to verify contract permission while contract running

func (*State) VerifyReservedContractRequests

func (t *State) VerifyReservedContractRequests(reservedReqs, txReqs []*protos.InvokeRequest) bool

func (*State) VerifyReservedWhitelist

func (t *State) VerifyReservedWhitelist(tx *pb.Transaction) bool

It will check whether the transaction in reserved whitelist if the config of chain contains reserved contracts but the transaction does not contains reserved requests.

func (*State) VerifyTx

func (t *State) VerifyTx(tx *pb.Transaction) (bool, error)

校验交易 VerifyTx check the tx signature and permission

func (*State) VerifyTxFee

func (t *State) VerifyTxFee(tx *pb.Transaction) bool

添加校验转账手续费的函数

func (*State) WaitBlockHeight

func (t *State) WaitBlockHeight(target int64) int64

WaitBlockHeight wait util the height of current block >= target

func (*State) Walk

func (t *State) Walk(blockid []byte, ledgerPrune bool) error

同步账本和状态机

type TxDesc

type TxDesc struct {
	Args map[string]interface{} `json:"args"`
}

TxDesc is the description to running a contract

Directories

Path Synopsis
Package utxo is the key part of XuperChain, this module keeps all Unspent Transaction Outputs.
Package utxo is the key part of XuperChain, this module keeps all Unspent Transaction Outputs.

Jump to

Keyboard shortcuts

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