contract

package
v0.0.0-...-d9e9996 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2019 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const ConsensusModueName = "consensus"

ConsensusModueName is the name of consensus contract

View Source
const (
	// ContractPluginName is the name of contract plugin
	ContractPluginName = "contract"
)
View Source
const KernelModuleName = "kernel"

KernelModuleName is the name of kernel contract

Variables

View Source
var AutoGenWhiteList = map[string]bool{
	"consensus.update_consensus": true,
	"kernel.UpdateMaxBlockSize":  true,
}

AutoGenWhiteList 为必须通过提案机制才能触发调用的智能合约名单

View Source
var (
	// ErrVMNotExist is returned when found vm not exist
	ErrVMNotExist = errors.New("Vm not exist in vm manager")
)
View Source
var MaxLimits = Limits{
	Cpu:    maxResourceLimit,
	Memory: maxResourceLimit,
	Disk:   maxResourceLimit,
	XFee:   maxResourceLimit,
}

MaxLimits describes the maximum limit of resources

Functions

func ToPbLimits

func ToPbLimits(limits Limits) []*pb.ResourceLimit

FromPbLimits converts Limits to []*pb.ResourceLimit

Types

type Context

type Context interface {
	Invoke(method string, args map[string][]byte) ([]byte, error)
	ResourceUsed() Limits
	Release() error
}

Context define context interface

type ContextConfig

type ContextConfig struct {
	XMCache        *xmodel.XMCache
	Initiator      string
	AuthRequire    []string
	ContractName   string
	ResourceLimits Limits
}

ContextConfig define the config of context

type ContractExtInterface

type ContractExtInterface interface {
	// 使用额外的参数初始化
	Init(extParams map[string]interface{}) error
}

ContractExtInterface is used to initialize contract plugin

type ContractInterface

type ContractInterface interface {
	//TX界别的接口
	Run(desc *TxDesc) error
	Rollback(desc *TxDesc) error
	//获取执行合约的结果
	ReadOutput(desc *TxDesc) (ContractOutputInterface, error)

	//block级别的接口
	//区块生成之后,用来更新各个合约的状态
	Finalize(blockid []byte) error
	//用于被设置上下文
	SetContext(context *TxContext) error
	Stop()
}

ContractInterface is the interface to implement a contract driver

func CreateContractInstance

func CreateContractInstance(subtype string, extParams map[string]interface{}) (ContractInterface, error)

CreateContractInstance create contract driver from plugin manager

type ContractOutputInterface

type ContractOutputInterface interface {
	Decode(data []byte) error
	Encode() ([]byte, error)
	GetGasUsed() uint64
	Digest() ([]byte, error)
}

ContractOutputInterface used to read output of a contract

type Limits

type Limits struct {
	Cpu    int64
	Memory int64
	Disk   int64
	XFee   int64
}

Limits describes the usage or limit of resources

func FromPbLimits

func FromPbLimits(rlimits []*pb.ResourceLimit) Limits

FromPbLimits converts []*pb.ResourceLimit to Limits

func (*Limits) TotalGas

func (l *Limits) TotalGas() int64

TotalGas converts resource to gas

type SmartContract

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

SmartContract manage smart contracts

func NewSmartContract

func NewSmartContract() *SmartContract

NewSmartContract instances a new SmartContract instance

func (*SmartContract) Finalize

func (s *SmartContract) Finalize(blockid []byte) error

Finalize 在一个块的合约执行完毕之后调用。这里必须在run 之后调用,这里有可能提交之前没有提交过的合约结果

func (*SmartContract) Get

func (s *SmartContract) Get(name string) (ContractInterface, bool)

Get returns ContractInterface from contract driver name

func (*SmartContract) GetAll

func (s *SmartContract) GetAll() map[string]ContractInterface

GetAll returns all the contract drivers

func (*SmartContract) RegisterHandler

func (s *SmartContract) RegisterHandler(moduleName string, handler ContractInterface, priv int) bool

RegisterHandler 注册module对应的handler

func (*SmartContract) Remove

func (s *SmartContract) Remove(name string, priv int)

Remove remove contract driver

func (*SmartContract) Rollback

func (s *SmartContract) Rollback(desc *TxDesc) error

Rollback 回滚合约

func (*SmartContract) Run

func (s *SmartContract) Run(desc *TxDesc) error

Run 执行合约

func (*SmartContract) SetContext

func (s *SmartContract) SetContext(ctx *TxContext)

SetContext 设置所有注册合约的上下文。这里必须在run之前设置,后设置会覆盖前面设置的

func (*SmartContract) Stop

func (s *SmartContract) Stop()

Stop stops all the contract drivers

type TriggerDesc

type TriggerDesc struct {
	TxDesc
	Height  int64  `json:"height"`
	RefTxid []byte `json:"refTxid"` //创建trigger的txid,系统自动回填的
}

TriggerDesc is the description to trigger a event used by proposal

type TxContext

type TxContext struct {
	UtxoBatch kvdb.Batch //如果合约机和UtxoVM共用DB, 可以将修改打包到这个batch确保原子性
	//... 其他的需要UtxoVM与合约机共享的也可以放到这里
	Block     *pb.InternalBlock
	LedgerObj *ledger.Ledger
	IsUndo    bool
}

TxContext 合约的上下文,通常生命周期是Block范围

type TxDesc

type TxDesc struct {
	Module string                 `json:"module"`
	Method string                 `json:"method"`
	Args   map[string]interface{} `json:"args"`
	//纯文本注释
	Tag []byte `json:"tag"`
	//表示当前合约执行到期时间, 只有大于0的时候才有效, 否则应该被认为是不限制到期时间
	Deadline int64           `json:"deadline"`
	Tx       *pb.Transaction `json:"tx"`
	Trigger  *TriggerDesc    `json:"trigger"`
}

TxDesc is the description to running a contract

func Parse

func Parse(desc string) (*TxDesc, error)

Parse 解析智能合约json

type VMManager

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

VMManager define VMManager type

func NewVMManager

func NewVMManager(logger log.Logger) (*VMManager, error)

NewVMManager new an instance of VMManager

func (*VMManager) GetVM

func (vmMgr *VMManager) GetVM(module string) (VirtualMachine, error)

GetVM return specific virtual machine instance

func (*VMManager) RegisterVM

func (vmMgr *VMManager) RegisterVM(module string, vm VirtualMachine) error

RegisterVM register an instance of VM into VMManager

type VirtualMachine

type VirtualMachine interface {
	GetName() string
	NewContext(*ContextConfig) (Context, error)
}

VirtualMachine define virtual machine interface

Directories

Path Synopsis
vm

Jump to

Keyboard shortcuts

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