embed

package
v0.0.0-...-6425a1c Latest Latest
Warning

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

Go to latest
Published: May 9, 2020 License: LGPL-3.0 Imports: 26 Imported by: 8

Documentation

Overview

* Copyright (C) 2018 The ZeepinChain Authors * This file is part of The ZeepinChain library. * * The ZeepinChain is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * The ZeepinChain is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with The ZeepinChain. If not, see <http://www.gnu.org/licenses/>.

Index

Constants

This section is empty.

Variables

View Source
var (
	//Gas Limit
	MIN_TRANSACTION_GAS           uint64 = 20000 // Per transaction base cost.
	BLOCKCHAIN_GETHEADER_GAS      uint64 = 100
	BLOCKCHAIN_GETBLOCK_GAS       uint64 = 200
	BLOCKCHAIN_GETTRANSACTION_GAS uint64 = 100
	BLOCKCHAIN_GETCONTRACT_GAS    uint64 = 100
	CONTRACT_CREATE_GAS           uint64 = 20000000
	CONTRACT_MIGRATE_GAS          uint64 = 20000000
	UINT_DEPLOY_CODE_LEN_GAS      uint64 = 200000
	UINT_INVOKE_CODE_LEN_GAS      uint64 = 20000
	NATIVE_INVOKE_GAS             uint64 = 1000
	STORAGE_GET_GAS               uint64 = 200
	STORAGE_PUT_GAS               uint64 = 4000
	STORAGE_DELETE_GAS            uint64 = 100
	RUNTIME_CHECKWITNESS_GAS      uint64 = 200
	APPCALL_GAS                   uint64 = 10
	TAILCALL_GAS                  uint64 = 10
	SHA1_GAS                      uint64 = 10
	SHA256_GAS                    uint64 = 10
	HASH160_GAS                   uint64 = 20
	HASH256_GAS                   uint64 = 20
	OPCODE_GAS                    uint64 = 1

	PER_UNIT_CODE_LEN   int = 1024
	METHOD_LENGTH_LIMIT int = 1024
	MAX_STACK_SIZE      int = 1024
	VM_STEP_LIMIT       int = 400000

	// API Name
	ATTRIBUTE_GETUSAGE_NAME = "ZeepinChain.Attribute.GetUsage"
	ATTRIBUTE_GETDATA_NAME  = "ZeepinChain.Attribute.GetData"

	BLOCK_GETTRANSACTIONCOUNT_NAME       = "System.Block.GetTransactionCount"
	BLOCK_GETTRANSACTIONS_NAME           = "System.Block.GetTransactions"
	BLOCK_GETTRANSACTION_NAME            = "System.Block.GetTransaction"
	BLOCKCHAIN_GETHEIGHT_NAME            = "System.Blockchain.GetHeight"
	BLOCKCHAIN_GETHEADER_NAME            = "System.Blockchain.GetHeader"
	BLOCKCHAIN_GETBLOCK_NAME             = "System.Blockchain.GetBlock"
	BLOCKCHAIN_GETTRANSACTION_NAME       = "System.Blockchain.GetTransaction"
	BLOCKCHAIN_GETCONTRACT_NAME          = "System.Blockchain.GetContract"
	BLOCKCHAIN_GETTRANSACTIONHEIGHT_NAME = "System.Blockchain.GetTransactionHeight"

	HEADER_GETINDEX_NAME         = "System.Header.GetIndex"
	HEADER_GETHASH_NAME          = "System.Header.GetHash"
	HEADER_GETVERSION_NAME       = "ZeepinChain.Header.GetVersion"
	HEADER_GETPREVHASH_NAME      = "System.Header.GetPrevHash"
	HEADER_GETTIMESTAMP_NAME     = "System.Header.GetTimestamp"
	HEADER_GETCONSENSUSDATA_NAME = "ZeepinChain.Header.GetConsensusData"
	HEADER_GETNEXTCONSENSUS_NAME = "ZeepinChain.Header.GetNextConsensus"
	HEADER_GETMERKLEROOT_NAME    = "ZeepinChain.Header.GetMerkleRoot"

	TRANSACTION_GETHASH_NAME       = "System.Transaction.GetHash"
	TRANSACTION_GETTYPE_NAME       = "ZeepinChain.Transaction.GetType"
	TRANSACTION_GETATTRIBUTES_NAME = "ZeepinChain.Transaction.GetAttributes"

	CONTRACT_CREATE_NAME            = "ZeepinChain.Contract.Create"
	CONTRACT_MIGRATE_NAME           = "ZeepinChain.Contract.Migrate"
	CONTRACT_GETSTORAGECONTEXT_NAME = "System.Contract.GetStorageContext"
	CONTRACT_DESTROY_NAME           = "System.Contract.Destroy"
	CONTRACT_GETSCRIPT_NAME         = "ZeepinChain.Contract.GetScript"

	STORAGE_GET_NAME                = "System.Storage.Get"
	STORAGE_PUT_NAME                = "System.Storage.Put"
	STORAGE_DELETE_NAME             = "System.Storage.Delete"
	STORAGE_GETCONTEXT_NAME         = "System.Storage.GetContext"
	STORAGE_GETREADONLYCONTEXT_NAME = "System.Storage.GetReadOnlyContext"

	STORAGECONTEXT_ASREADONLY_NAME = "System.StorageContext.AsReadOnly"

	RUNTIME_GETTIME_NAME      = "System.Runtime.GetTime"
	RUNTIME_CHECKWITNESS_NAME = "System.Runtime.CheckWitness"
	RUNTIME_NOTIFY_NAME       = "System.Runtime.Notify"
	RUNTIME_LOG_NAME          = "System.Runtime.Log"
	RUNTIME_GETTRIGGER_NAME   = "System.Runtime.GetTrigger"
	RUNTIME_SERIALIZE_NAME    = "System.Runtime.Serialize"
	RUNTIME_DESERIALIZE_NAME  = "System.Runtime.Deserialize"

	NATIVE_INVOKE_NAME = "ZeepinChain.Native.Invoke"

	GETSCRIPTCONTAINER_NAME     = "System.ExecutionEngine.GetScriptContainer"
	GETEXECUTINGSCRIPTHASH_NAME = "System.ExecutionEngine.GetExecutingScriptHash"
	GETCALLINGSCRIPTHASH_NAME   = "System.ExecutionEngine.GetCallingScriptHash"
	GETENTRYSCRIPTHASH_NAME     = "System.ExecutionEngine.GetEntryScriptHash"

	APPCALL_NAME              = "APPCALL"
	TAILCALL_NAME             = "TAILCALL"
	SHA1_NAME                 = "SHA1"
	SHA256_NAME               = "SHA256"
	HASH160_NAME              = "HASH160"
	HASH256_NAME              = "HASH256"
	UINT_DEPLOY_CODE_LEN_NAME = "Deploy.Code.Gas"
	UINT_INVOKE_CODE_LEN_NAME = "Invoke.Code.Gas"

	GAS_TABLE = initGAS_TABLE()

	GAS_TABLE_KEYS = []string{
		BLOCKCHAIN_GETHEADER_NAME,
		BLOCKCHAIN_GETBLOCK_NAME,
		BLOCKCHAIN_GETTRANSACTION_NAME,
		BLOCKCHAIN_GETCONTRACT_NAME,
		CONTRACT_CREATE_NAME,
		CONTRACT_MIGRATE_NAME,
		STORAGE_GET_NAME,
		STORAGE_PUT_NAME,
		STORAGE_DELETE_NAME,
		RUNTIME_CHECKWITNESS_NAME,
		NATIVE_INVOKE_NAME,
		APPCALL_NAME,
		TAILCALL_NAME,
		SHA1_NAME,
		SHA256_NAME,
		HASH160_NAME,
		HASH256_NAME,
		UINT_DEPLOY_CODE_LEN_NAME,
		UINT_INVOKE_CODE_LEN_NAME,
	}
)
View Source
var (
	ERR_CHECK_STACK_SIZE  = errors.NewErr("[EmbeddedService] vm over max stack size!")
	ERR_EXECUTE_CODE      = errors.NewErr("[EmbeddedService] vm execute code invalid!")
	ERR_GAS_INSUFFICIENT  = errors.NewErr("[EmbeddedService] gas insufficient")
	VM_EXEC_STEP_EXCEED   = errors.NewErr("[EmbeddedService] vm execute step exceed!")
	CONTRACT_NOT_EXIST    = errors.NewErr("[EmbeddedService] Get contract code from db fail")
	DEPLOYCODE_TYPE_ERROR = errors.NewErr("[EmbeddedService] DeployCode type error!")
	VM_EXEC_FAULT         = errors.NewErr("[EmbeddedService] vm execute state fault!")
)
View Source
var (
	// Register all service for smart contract execute
	ServiceMap = map[string]Service{
		ATTRIBUTE_GETUSAGE_NAME:              {Execute: AttributeGetUsage, Validator: validatorAttribute},
		ATTRIBUTE_GETDATA_NAME:               {Execute: AttributeGetData, Validator: validatorAttribute},
		BLOCK_GETTRANSACTIONCOUNT_NAME:       {Execute: BlockGetTransactionCount, Validator: validatorBlock},
		BLOCK_GETTRANSACTIONS_NAME:           {Execute: BlockGetTransactions, Validator: validatorBlock},
		BLOCK_GETTRANSACTION_NAME:            {Execute: BlockGetTransaction, Validator: validatorBlockTransaction},
		BLOCKCHAIN_GETHEIGHT_NAME:            {Execute: BlockChainGetHeight},
		BLOCKCHAIN_GETHEADER_NAME:            {Execute: BlockChainGetHeader, Validator: validatorBlockChainHeader},
		BLOCKCHAIN_GETBLOCK_NAME:             {Execute: BlockChainGetBlock, Validator: validatorBlockChainBlock},
		BLOCKCHAIN_GETTRANSACTION_NAME:       {Execute: BlockChainGetTransaction, Validator: validatorBlockChainTransaction},
		BLOCKCHAIN_GETCONTRACT_NAME:          {Execute: BlockChainGetContract, Validator: validatorBlockChainContract},
		BLOCKCHAIN_GETTRANSACTIONHEIGHT_NAME: {Execute: BlockChainGetTransactionHeight},
		HEADER_GETINDEX_NAME:                 {Execute: HeaderGetIndex, Validator: validatorHeader},
		HEADER_GETHASH_NAME:                  {Execute: HeaderGetHash, Validator: validatorHeader},
		HEADER_GETVERSION_NAME:               {Execute: HeaderGetVersion, Validator: validatorHeader},
		HEADER_GETPREVHASH_NAME:              {Execute: HeaderGetPrevHash, Validator: validatorHeader},
		HEADER_GETTIMESTAMP_NAME:             {Execute: HeaderGetTimestamp, Validator: validatorHeader},
		HEADER_GETCONSENSUSDATA_NAME:         {Execute: HeaderGetConsensusData, Validator: validatorHeader},
		HEADER_GETNEXTCONSENSUS_NAME:         {Execute: HeaderGetNextConsensus, Validator: validatorHeader},
		HEADER_GETMERKLEROOT_NAME:            {Execute: HeaderGetMerkleRoot, Validator: validatorHeader},
		TRANSACTION_GETHASH_NAME:             {Execute: TransactionGetHash, Validator: validatorTransaction},
		TRANSACTION_GETTYPE_NAME:             {Execute: TransactionGetType, Validator: validatorTransaction},
		TRANSACTION_GETATTRIBUTES_NAME:       {Execute: TransactionGetAttributes, Validator: validatorTransaction},
		CONTRACT_CREATE_NAME:                 {Execute: ContractCreate},
		CONTRACT_MIGRATE_NAME:                {Execute: ContractMigrate},
		CONTRACT_GETSTORAGECONTEXT_NAME:      {Execute: ContractGetStorageContext},
		CONTRACT_DESTROY_NAME:                {Execute: ContractDestory},
		CONTRACT_GETSCRIPT_NAME:              {Execute: ContractGetCode, Validator: validatorGetCode},
		RUNTIME_GETTIME_NAME:                 {Execute: RuntimeGetTime},
		RUNTIME_CHECKWITNESS_NAME:            {Execute: RuntimeCheckWitness, Validator: validatorCheckWitness},
		RUNTIME_NOTIFY_NAME:                  {Execute: RuntimeNotify, Validator: validatorNotify},
		RUNTIME_LOG_NAME:                     {Execute: RuntimeLog, Validator: validatorLog},
		RUNTIME_GETTRIGGER_NAME:              {Execute: RuntimeGetTrigger},
		RUNTIME_SERIALIZE_NAME:               {Execute: RuntimeSerialize, Validator: validatorSerialize},
		RUNTIME_DESERIALIZE_NAME:             {Execute: RuntimeDeserialize, Validator: validatorDeserialize},
		NATIVE_INVOKE_NAME:                   {Execute: NativeInvoke},
		STORAGE_GET_NAME:                     {Execute: StorageGet},
		STORAGE_PUT_NAME:                     {Execute: StoragePut},
		STORAGE_DELETE_NAME:                  {Execute: StorageDelete},
		STORAGE_GETCONTEXT_NAME:              {Execute: StorageGetContext},
		STORAGE_GETREADONLYCONTEXT_NAME:      {Execute: StorageGetReadOnlyContext},
		STORAGECONTEXT_ASREADONLY_NAME:       {Execute: StorageContextAsReadOnly},
		GETSCRIPTCONTAINER_NAME:              {Execute: GetCodeContainer},
		GETEXECUTINGSCRIPTHASH_NAME:          {Execute: GetExecutingAddress},
		GETCALLINGSCRIPTHASH_NAME:            {Execute: GetCallingAddress},
		GETENTRYSCRIPTHASH_NAME:              {Execute: GetEntryAddress},
	}
)

Functions

func AttributeGetData

func AttributeGetData(service *EmbeddedService, engine *vm.ExecutionEngine) error

AttributeGetData put attribute's data to vm stack

func AttributeGetUsage

func AttributeGetUsage(service *EmbeddedService, engine *vm.ExecutionEngine) error

AttributeGetUsage put attribute's usage to vm stack

func BlockChainGetBlock

func BlockChainGetBlock(service *EmbeddedService, engine *vm.ExecutionEngine) error

BlockChainGetBlock put blockchain's block to vm stack

func BlockChainGetContract

func BlockChainGetContract(service *EmbeddedService, engine *vm.ExecutionEngine) error

BlockChainGetContract put blockchain's contract to vm stack

func BlockChainGetHeader

func BlockChainGetHeader(service *EmbeddedService, engine *vm.ExecutionEngine) error

BlockChainGetHeader put blockchain's header to vm stack

func BlockChainGetHeight

func BlockChainGetHeight(service *EmbeddedService, engine *vm.ExecutionEngine) error

BlockChainGetHeight put blockchain's height to vm stack

func BlockChainGetTransaction

func BlockChainGetTransaction(service *EmbeddedService, engine *vm.ExecutionEngine) error

BlockChainGetTransaction put blockchain's transaction to vm stack

func BlockChainGetTransactionHeight

func BlockChainGetTransactionHeight(service *EmbeddedService, engine *vm.ExecutionEngine) error

BlockChainGetTransactionHeight put transaction in block height to vm stack

func BlockGetTransaction

func BlockGetTransaction(service *EmbeddedService, engine *vm.ExecutionEngine) error

BlockGetTransaction put block's transaction to vm stack

func BlockGetTransactionCount

func BlockGetTransactionCount(service *EmbeddedService, engine *vm.ExecutionEngine) error

BlockGetTransactionCount put block's transactions count to vm stack

func BlockGetTransactions

func BlockGetTransactions(service *EmbeddedService, engine *vm.ExecutionEngine) error

BlockGetTransactions put block's transactions to vm stack

func BuildParamToNative

func BuildParamToNative(bf *bytes.Buffer, item types.StackItems) error

func CircularRefAndDepthDetection

func CircularRefAndDepthDetection(value vmtypes.StackItems) bool

func ContractCreate

func ContractCreate(service *EmbeddedService, engine *vm.ExecutionEngine) error

ContractCreate create a new smart contract on blockchain, and put it to vm stack

func ContractDestory

func ContractDestory(service *EmbeddedService, engine *vm.ExecutionEngine) error

ContractDestory destroy a contract

func ContractGetCode

func ContractGetCode(service *EmbeddedService, engine *vm.ExecutionEngine) error

ContractGetCode put contract to vm stack

func ContractGetStorageContext

func ContractGetStorageContext(service *EmbeddedService, engine *vm.ExecutionEngine) error

ContractGetStorageContext put contract storage context to vm stack

func ContractMigrate

func ContractMigrate(service *EmbeddedService, engine *vm.ExecutionEngine) error

ContractMigrate migrate old smart contract to a new contract, and destroy old contract

func DeserializeStackItem

func DeserializeStackItem(r io.Reader) (items vmtypes.StackItems, err error)

func GasPrice

func GasPrice(engine *vm.ExecutionEngine, name string) (uint64, error)

func GetCallingAddress

func GetCallingAddress(service *EmbeddedService, engine *vm.ExecutionEngine) error

GetExecutingAddress push previous context to vm stack

func GetCodeContainer

func GetCodeContainer(service *EmbeddedService, engine *vm.ExecutionEngine) error

GetCodeContainer push current transaction to vm stack

func GetEntryAddress

func GetEntryAddress(service *EmbeddedService, engine *vm.ExecutionEngine) error

GetExecutingAddress push entry call context to vm stack

func GetExecutingAddress

func GetExecutingAddress(service *EmbeddedService, engine *vm.ExecutionEngine) error

GetExecutingAddress push current context to vm stack

func HeaderGetConsensusData

func HeaderGetConsensusData(service *EmbeddedService, engine *vm.ExecutionEngine) error

HeaderGetConsensusData put header's consensus data to vm stack

func HeaderGetHash

func HeaderGetHash(service *EmbeddedService, engine *vm.ExecutionEngine) error

HeaderGetHash put header's hash to vm stack

func HeaderGetIndex

func HeaderGetIndex(service *EmbeddedService, engine *vm.ExecutionEngine) error

HeaderGetIndex put header's height to vm stack

func HeaderGetMerkleRoot

func HeaderGetMerkleRoot(service *EmbeddedService, engine *vm.ExecutionEngine) error

HeaderGetMerkleRoot put header's merkleroot to vm stack

func HeaderGetNextConsensus

func HeaderGetNextConsensus(service *EmbeddedService, engine *vm.ExecutionEngine) error

HeaderGetNextConsensus put header's consensus to vm stack

func HeaderGetPrevHash

func HeaderGetPrevHash(service *EmbeddedService, engine *vm.ExecutionEngine) error

HeaderGetPrevHash put header's prevblockhash to vm stack

func HeaderGetTimestamp

func HeaderGetTimestamp(service *EmbeddedService, engine *vm.ExecutionEngine) error

HeaderGetTimestamp put header's timestamp to vm stack

func HeaderGetVersion

func HeaderGetVersion(service *EmbeddedService, engine *vm.ExecutionEngine) error

HeaderGetVersion put header's version to vm stack

func NativeInvoke

func NativeInvoke(service *EmbeddedService, engine *vm.ExecutionEngine) error

func RuntimeCheckWitness

func RuntimeCheckWitness(service *EmbeddedService, engine *vm.ExecutionEngine) error

RuntimeCheckWitness provide check permissions service If param address isn't exist in authorization list, check fail

func RuntimeDeserialize

func RuntimeDeserialize(service *EmbeddedService, engine *vm.ExecutionEngine) error

func RuntimeGetTime

func RuntimeGetTime(service *EmbeddedService, engine *vm.ExecutionEngine) error

HeaderGetNextConsensus put current block time to vm stack

func RuntimeGetTrigger

func RuntimeGetTrigger(service *EmbeddedService, engine *vm.ExecutionEngine) error

func RuntimeLog

func RuntimeLog(service *EmbeddedService, engine *vm.ExecutionEngine) error

RuntimeLog push smart contract execute event log to client

func RuntimeNotify

func RuntimeNotify(service *EmbeddedService, engine *vm.ExecutionEngine) error

RuntimeNotify put smart contract execute event notify to notifications

func RuntimeSerialize

func RuntimeSerialize(service *EmbeddedService, engine *vm.ExecutionEngine) error

func SerializeStackItem

func SerializeStackItem(item vmtypes.StackItems) ([]byte, error)

func StorageContextAsReadOnly

func StorageContextAsReadOnly(service *EmbeddedService, engine *vm.ExecutionEngine) error

func StorageDelete

func StorageDelete(service *EmbeddedService, engine *vm.ExecutionEngine) error

StorageDelete delete smart contract storage item from cache

func StorageGet

func StorageGet(service *EmbeddedService, engine *vm.ExecutionEngine) error

StorageGet push smart contract storage item from cache to vm stack

func StorageGetContext

func StorageGetContext(service *EmbeddedService, engine *vm.ExecutionEngine) error

StorageGetContext push smart contract storage context to vm stack

func StorageGetReadOnlyContext

func StorageGetReadOnlyContext(service *EmbeddedService, engine *vm.ExecutionEngine) error

func StoragePut

func StoragePut(service *EmbeddedService, engine *vm.ExecutionEngine) error

StoragePut put smart contract storage item to cache

func StoreGasCost

func StoreGasCost(engine *vm.ExecutionEngine) (uint64, error)

func TransactionGetAttributes

func TransactionGetAttributes(service *EmbeddedService, engine *vm.ExecutionEngine) error

TransactionGetAttributes push transaction's attributes to vm stack

func TransactionGetHash

func TransactionGetHash(service *EmbeddedService, engine *vm.ExecutionEngine) error

GetExecutingAddress push transaction's hash to vm stack

func TransactionGetType

func TransactionGetType(service *EmbeddedService, engine *vm.ExecutionEngine) error

TransactionGetType push transaction's type to vm stack

Types

type EmbeddedService

type EmbeddedService struct {
	Store         store.LedgerStore
	CloneCache    *storage.CloneCache
	ContextRef    context.ContextRef
	Notifications []*event.NotifyEventInfo
	Code          []byte
	Tx            *types.Transaction
	Time          uint32
	Height        uint32
	Engine        *vm.ExecutionEngine
}

EmbeddedService is a struct for smart contract provide interop service

func (*EmbeddedService) Invoke

func (this *EmbeddedService) Invoke() (interface{}, error)

Invoke a smart contract

func (*EmbeddedService) SystemCall

func (this *EmbeddedService) SystemCall(engine *vm.ExecutionEngine) error

SystemCall provide register service for smart contract to interaction with blockchain

type Execute

type Execute func(service *EmbeddedService, engine *vm.ExecutionEngine) error

type Service

type Service struct {
	Execute   Execute
	Validator Validator
}

type StorageContext

type StorageContext struct {
	Address    common.Address
	IsReadOnly bool
}

StorageContext store smart contract address

func NewStorageContext

func NewStorageContext(address common.Address) *StorageContext

NewStorageContext return a new smart contract storage context

func (*StorageContext) ToArray

func (this *StorageContext) ToArray() []byte

ToArray return address byte array

type Validator

type Validator func(engine *vm.ExecutionEngine) error

Jump to

Keyboard shortcuts

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