script

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: May 30, 2023 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

---------------------------------------------------------------------------------------------

  • Copyright (c) IBAX. All rights reserved.
  • See LICENSE in the project root for license information. *--------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------

  • Copyright (c) IBAX. All rights reserved.
  • See LICENSE in the project root for license information. *--------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------

  • Copyright (c) IBAX. All rights reserved.
  • See LICENSE in the project root for license information. *--------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------

  • Copyright (c) IBAX. All rights reserved.
  • See LICENSE in the project root for license information. *--------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------

  • Copyright (c) IBAX. All rights reserved.
  • See LICENSE in the project root for license information. *--------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------

  • Copyright (c) IBAX. All rights reserved.
  • See LICENSE in the project root for license information. *--------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------

  • Copyright (c) IBAX. All rights reserved.
  • See LICENSE in the project root for license information. *--------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------

  • Copyright (c) IBAX. All rights reserved.
  • See LICENSE in the project root for license information. *--------------------------------------------------------------------------------------------

Index

Constants

View Source
const (
	Extend_type                = `type`
	Extend_time                = `time`
	Extend_ecosystem_id        = `ecosystem_id`
	Extend_node_position       = `node_position`
	Extend_block               = `block`
	Extend_key_id              = `key_id`
	Extend_account_id          = `account_id`
	Extend_block_key_id        = `block_key_id`
	Extend_parent              = `parent`
	Extend_txcost              = `txcost`
	Extend_txhash              = `txhash`
	Extend_result              = `result`
	Extend_sc                  = `sc`
	Extend_contract            = `contract`
	Extend_block_time          = `block_time`
	Extend_original_contract   = `original_contract`
	Extend_this_contract       = `this_contract`
	Extend_guest_key           = `guest_key`
	Extend_guest_account       = `guest_account`
	Extend_black_hole_key      = `black_hole_key`
	Extend_black_hole_account  = `black_hole_account`
	Extend_white_hole_key      = `white_hole_key`
	Extend_white_hole_account  = `white_hole_account`
	Extend_pre_block_data_hash = `pre_block_data_hash`
	Extend_gen_block           = `gen_block`
	Extend_time_limit          = `time_limit`

	Extend_rt_state = `rt_state`
	Extend_rt       = `rt`
	Extend_stack    = `stack`
	Extend_loop     = `loop_`
)
View Source
const (
	DtBool uint32 = iota + 1
	DtBytes
	DtInt
	DtAddress
	DtArray
	DtMap
	DtMoney
	DtFloat
	DtString
	DtFile
)
View Source
const (

	// Decimal is the constant string for decimal type
	Decimal = `decimal.Decimal`
	// Interface is the constant string for interface type
	Interface = `interface`
	File      = `*types.Map`

	MaxErrLen = 150
)
View Source
const (
	// CostCall is the cost of the function calling
	CostCall = 50
	// CostContract is the cost of the contract calling
	CostContract = 100
	// CostExtend is the cost of the extend function calling
	CostExtend = 10

	TagFile      = "file"
	TagAddress   = "address"
	TagSignature = "signature"
	TagOptional  = "optional"
)

Variables

View Source
var (
	ErrMemoryLimit = errors.New("Memory limit exceeded")
	//ErrVMTimeLimit returns when the time limit exceeded
	ErrVMTimeLimit = errors.New(`time limit exceeded`)
)
View Source
var ObjectType_name = map[int32]string{
	0: "Unknown",
	1: "Contract",
	2: "Func",
	3: "ExtFunc",
	4: "Var",
	5: "ExtVar",
}
View Source
var ObjectType_value = map[string]int32{
	"Unknown":  0,
	"Contract": 1,
	"Func":     2,
	"ExtFunc":  3,
	"Var":      4,
	"ExtVar":   5,
}
View Source
var VMType_name = map[int32]string{
	0: "INVALID",
	1: "Smart",
	2: "CLB",
	3: "CLBMaster",
}
View Source
var VMType_value = map[string]int32{
	"INVALID":   0,
	"Smart":     1,
	"CLB":       2,
	"CLBMaster": 3,
}
View Source
var VarRegexp = `^[a-zA-Z][a-zA-Z0-9_]*$`

VarRegexp letter { letter | unicode_digit }

Functions

func ContractsList

func ContractsList(value string) ([]string, error)

ContractsList returns list of contracts names from source of code

func ExContract

func ExContract(rt *RunTime, state uint32, name string, params *types.Map) (any, error)

ExContract executes the name contract in the state with specified parameters

func ExecContract

func ExecContract(rt *RunTime, name, txs string, params ...any) (any, error)

ExecContract runs the name contract where txs contains the list of parameters and params are the values of parameters

func GetFieldDefaultValue

func GetFieldDefaultValue(fieldType uint32) any

func GetSettings

func GetSettings(rt *RunTime, cntname, name string) (any, error)

GetSettings returns the value of the parameter

func LoadSysFuncs

func LoadSysFuncs(vm *VM, state int) error

func MemoryUsage

func MemoryUsage(rt *RunTime) int64

func OriginalToString

func OriginalToString(original uint32) string

func ReleaseSmartVMObjects

func ReleaseSmartVMObjects()

func RollbackSmartVMObjects

func RollbackSmartVMObjects()

func RunContractById

func RunContractById(vm *VM, id int32, methods []string, extend map[string]any, txHash []byte) error

func RunContractByName

func RunContractByName(vm *VM, name string, methods []string, extend map[string]any, txHash []byte) error

func SavepointSmartVMObjects

func SavepointSmartVMObjects()

func SetVMError

func SetVMError(eType string, eText any) error

SetVMError sets error of VM

func StateName

func StateName(state uint32, name string) string

StateName checks the name of the contract and modifies it to @[state]name if it is necessary.

func VMCompileEval

func VMCompileEval(vm *VM, src string, prefix uint32) error

func VMObjectExists

func VMObjectExists(vm *VM, name string, state uint32) bool

func VMRun

func VMRun(vm *VM, block *CodeBlock, params []any, extend map[string]any, hash []byte) (ret []any, err error)

VMRun executes CodeBlock in vm

func ValueToDecimal

func ValueToDecimal(v any) (ret decimal.Decimal, err error)

ValueToDecimal converts interface (string, float64, Decimal or int64) to Decimal

func ValueToFloat

func ValueToFloat(v any) (ret float64)

ValueToFloat converts interface (string, float64 or int64) to float64

Types

type ByteCode

type ByteCode struct {
	Cmd   uint16
	Line  uint16
	Value any
}

ByteCode stores a command and an additional parameter.

type ByteCodes

type ByteCodes []*ByteCode

ByteCodes is the slice of ByteCode items

type CodeBlock

type CodeBlock struct {
	Objects map[string]*ObjInfo
	Type    ObjectType
	Owner   *OwnerInfo
	// Types that are valid to be assigned to Info:
	//	*FuncInfo
	//	*ContractInfo
	Info     isCodeBlockInfo
	Parent   *CodeBlock
	Vars     []reflect.Type
	Code     ByteCodes
	Children CodeBlocks
}

CodeBlock contains all information about compiled block {...} and its children

func NewCodeBlock

func NewCodeBlock() *CodeBlock

func (*CodeBlock) Extend

func (b *CodeBlock) Extend(ext *ExtendData)

Extend sets the extended variables and functions

func (*CodeBlock) GetContractInfo

func (m *CodeBlock) GetContractInfo() *ContractInfo

func (*CodeBlock) GetFuncInfo

func (m *CodeBlock) GetFuncInfo() *FuncInfo

func (*CodeBlock) GetInfo

func (m *CodeBlock) GetInfo() isCodeBlockInfo

type CodeBlocks

type CodeBlocks []*CodeBlock

CodeBlocks is a slice of blocks

type ContractInfo

type ContractInfo struct {
	ID       uint32
	Name     string
	Owner    *OwnerInfo
	Used     map[string]bool // Called contracts
	Tx       *[]*FieldInfo
	Settings map[string]any
	CanWrite bool // If the function can update DB
}

ContractInfo contains the contract information

func VMGetContractByID

func VMGetContractByID(vm *VM, id int32) *ContractInfo

func (*ContractInfo) TxMap

func (c *ContractInfo) TxMap() map[string]*FieldInfo

type ErrInfo

type ErrInfo struct {
	Name string
	Line uint16
}

ErrInfo stores info about current contract or function

type ExtFuncInfo

type ExtFuncInfo struct {
	Name     string
	Params   []reflect.Type
	Results  []reflect.Type
	Auto     []string
	Variadic bool
	Func     any
	CanWrite bool // If the function can update DB
}

ExtFuncInfo is the structure for the extended function

type ExtendData

type ExtendData struct {
	Objects    map[string]any
	AutoPars   map[string]string
	WriteFuncs map[string]struct{}
}

ExtendData is used for the definition of the extended functions and variables

func NewExtendData

func NewExtendData() *ExtendData

type FieldInfo

type FieldInfo struct {
	Name     string
	Type     reflect.Type
	Original uint32
	Tags     string
}

FieldInfo describes the field of the data structure

func (*FieldInfo) ContainsTag

func (fi *FieldInfo) ContainsTag(tag string) bool

ContainsTag returns whether the tag is contained in this field

type FuncInfo

type FuncInfo struct {
	Name    string
	Params  []reflect.Type
	Results []reflect.Type
	//tail function
	Names    *map[string]FuncName
	Variadic bool
	ID       uint32
	CanWrite bool // If the function can update DB
}

FuncInfo contains the function information

type FuncName

type FuncName struct {
	Params   []reflect.Type
	Offset   []int
	Variadic bool
}

FuncName is storing param of FuncName

type FuncNameCmd

type FuncNameCmd struct {
	Name  string
	Count int
}

FuncNameCmd for cmdFuncName

type GlobalVm

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

type IndexInfo

type IndexInfo struct {
	VarOffset int
	Owner     *CodeBlock
	Extend    string
}

IndexInfo contains the information for SetIndex

type Lexeme

type Lexeme struct {
	Type   uint32 // Type of the lexeme
	Ext    uint32
	Value  any    // Value of lexeme
	Line   uint16 // Line of the lexeme
	Column uint32 // Position inside the line
}

Lexeme contains information about language item

func NewLexeme

func NewLexeme(t uint32, ext uint32, value any, line uint16, column uint32) *Lexeme

func (*Lexeme) GetLogger

func (l *Lexeme) GetLogger() *log.Entry

GetLogger returns logger

type Lexemes

type Lexemes []*Lexeme

Lexemes is a slice of lexemes

type ObjInfo

type ObjInfo struct {
	Type ObjectType
	// Types that are valid to be assigned to Value:
	//	*CodeBlock
	//	*ExtFuncInfo
	//	*ObjInfo_Variable
	//	*ObjInfo_ExtendVariable
	Value isObjInfoValue
}

ObjInfo is the common object type

func (*ObjInfo) GetCodeBlock

func (m *ObjInfo) GetCodeBlock() *CodeBlock

func (*ObjInfo) GetExtFuncInfo

func (m *ObjInfo) GetExtFuncInfo() *ExtFuncInfo

func (*ObjInfo) GetExtendVariable

func (m *ObjInfo) GetExtendVariable() *ObjInfo_ExtendVariable

func (*ObjInfo) GetValue

func (m *ObjInfo) GetValue() isObjInfoValue

func (*ObjInfo) GetVariable

func (m *ObjInfo) GetVariable() *ObjInfo_Variable

type ObjInfo_ExtendVariable

type ObjInfo_ExtendVariable struct {
	//object extend variable name
	Name string
}

type ObjInfo_Variable

type ObjInfo_Variable struct {
	Name  string
	Index int
}

type ObjectType

type ObjectType int32

ObjectType Types of the compiled objects

const (
	// ObjUnknown is an unknown object.
	ObjectType_Unknown ObjectType = 0
	// ObjectType_Contract is a contract object.
	ObjectType_Contract ObjectType = 1
	// ObjectType_Func is a function object. myfunc()
	ObjectType_Func ObjectType = 2
	// ObjectType_ExtFunc is an extended build in function object. $myfunc()
	ObjectType_ExtFunc ObjectType = 3
	// ObjectType_Var is a variable. myvar
	ObjectType_Var ObjectType = 4
	// ObjectType_ExtVar is an extended build in variable. $myvar
	ObjectType_ExtVar ObjectType = 5
)

func (ObjectType) EnumDescriptor

func (ObjectType) EnumDescriptor() ([]byte, []int)

func (ObjectType) String

func (x ObjectType) String() string

type OwnerInfo

type OwnerInfo struct {
	StateID  uint32 `json:"state"`
	Active   bool   `json:"active"`
	TableID  int64  `json:"tableid"`
	WalletID int64  `json:"walletid"`
	TokenID  int64  `json:"tokenid"`
}

OwnerInfo storing info about owner

type RunTime

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

RunTime is needed for the execution of the byte-code

func NewRunTime

func NewRunTime(vm *VM, cost int64) *RunTime

NewRunTime creates a new RunTime for the virtual machine

func (*RunTime) Cost

func (rt *RunTime) Cost() int64

Cost return the remain cost of the execution.

func (*RunTime) Run

func (rt *RunTime) Run(block *CodeBlock, params []any, extend map[string]any) (ret []any, err error)

Run executes CodeBlock with the specified parameters and extended variables and functions

func (*RunTime) RunCode

func (rt *RunTime) RunCode(block *CodeBlock) (status int, err error)

RunCode executes CodeBlock

func (*RunTime) SetCost

func (rt *RunTime) SetCost(cost int64)

SetCost sets the max cost of the execution.

func (*RunTime) Stack

func (rt *RunTime) Stack() []any

func (*RunTime) SubCost

func (rt *RunTime) SubCost(cost int64) error

type Stacker

type Stacker interface {
	AppendStack(fn string) error
	PopStack(fn string)
}

Stacker represents interface for working with call stack

type VM

type VM struct {
	*CodeBlock
	ExtCost       func(string) int64
	FuncCallsDB   map[string]struct{}
	Extern        bool  // extern mode of compilation
	ShiftContract int64 // id of the first contract
	// contains filtered or unexported fields
}

VM is the main type of the virtual machine

func GetVM

func GetVM() *VM

GetVM is returning smart vm

func NewVM

func NewVM() *VM

NewVM creates a new virtual machine

func (*VM) Call

func (vm *VM) Call(name string, params []any, extend map[string]any) (ret []any, err error)

Call executes the name object with the specified params and extended variables and functions

func (*VM) Compile

func (vm *VM) Compile(input []rune, owner *OwnerInfo) error

Compile compiles a source code and loads the byte-code into the virtual machine

func (*VM) CompileBlock

func (vm *VM) CompileBlock(input []rune, owner *OwnerInfo) (*CodeBlock, error)

CompileBlock compile the source code into the CodeBlock structure with a byte-code

func (*VM) CompileEval

func (vm *VM) CompileEval(input string, state uint32) error

CompileEval compiles conditional expression

func (*VM) EvalIf

func (vm *VM) EvalIf(input string, state uint32, vars map[string]any) (bool, error)

EvalIf runs the conditional expression. It compiles the source code before that if that's necessary.

func (*VM) FlushBlock

func (vm *VM) FlushBlock(root *CodeBlock)

FlushBlock loads the compiled CodeBlock into the virtual machine

func (*VM) FlushExtern

func (vm *VM) FlushExtern()

FlushExtern switches off the extern mode of the compilation

func (*VM) SetExtendCost

func (vm *VM) SetExtendCost(ext func(string) int64)

SetExtendCost sets the cost of calling extended obj in vm

func (*VM) SetFuncCallsDB

func (vm *VM) SetFuncCallsDB(funcCallsDB map[string]struct{})

SetFuncCallsDB Set up functions that can edit the database in vm

type VMError

type VMError struct {
	Type  string `json:"type"`
	Error string `json:"error"`
}

VMError represents error of VM

type VMType

type VMType int32

VMType is virtual machine type

const (
	// VMType_INVALID is invalid type
	VMType_INVALID VMType = 0
	// VMType_Smart is smart vm type
	VMType_Smart VMType = 1
	// VMType_CLB is clb vm type
	VMType_CLB VMType = 2
	// VMType_CLBMaster is CLBMaster type
	VMType_CLBMaster VMType = 3
)

func (VMType) EnumDescriptor

func (VMType) EnumDescriptor() ([]byte, []int)

func (VMType) String

func (x VMType) String() string

type VarInfo

type VarInfo struct {
	Obj   *ObjInfo
	Owner *CodeBlock
}

VarInfo contains the variable information

Directories

Path Synopsis
---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------

Jump to

Keyboard shortcuts

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