forked from ontio/ontology
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wasm_service.go
170 lines (141 loc) · 4.86 KB
/
wasm_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
*
* The ontology 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 ontology 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 ontology. If not, see <http://www.gnu.org/licenses/>.
*/
package wasmvm
import (
"github.com/go-interpreter/wagon/exec"
"github.com/hashicorp/golang-lru"
"github.com/ontio/ontology/common"
"github.com/ontio/ontology/core/store"
"github.com/ontio/ontology/core/types"
"github.com/ontio/ontology/errors"
"github.com/ontio/ontology/smartcontract/context"
"github.com/ontio/ontology/smartcontract/event"
"github.com/ontio/ontology/smartcontract/states"
"github.com/ontio/ontology/smartcontract/storage"
)
type WasmVmService struct {
Store store.LedgerStore
CacheDB *storage.CacheDB
ContextRef context.ContextRef
Notifications []*event.NotifyEventInfo
Code []byte
Tx *types.Transaction
Time uint32
Height uint32
BlockHash common.Uint256
PreExec bool
GasPrice uint64
GasLimit *uint64
ExecStep *uint64
GasFactor uint64
IsTerminate bool
vm *exec.VM
}
var (
ERR_CHECK_STACK_SIZE = errors.NewErr("[WasmVmService] vm over max stack size!")
ERR_EXECUTE_CODE = errors.NewErr("[WasmVmService] vm execute code invalid!")
ERR_GAS_INSUFFICIENT = errors.NewErr("[WasmVmService] gas insufficient")
VM_EXEC_STEP_EXCEED = errors.NewErr("[WasmVmService] vm execute step exceed!")
CONTRACT_NOT_EXIST = errors.NewErr("[WasmVmService] Get contract code from db fail")
DEPLOYCODE_TYPE_ERROR = errors.NewErr("[WasmVmService] DeployCode type error!")
VM_EXEC_FAULT = errors.NewErr("[WasmVmService] vm execute state fault!")
VM_INIT_FAULT = errors.NewErr("[WasmVmService] vm init state fault!")
CODE_CACHE_SIZE = 100
CONTRACT_METHOD_NAME = "invoke"
//max memory size of wasm vm
WASM_MEM_LIMITATION uint64 = 10 * 1024 * 1024
VM_STEP_LIMIT = 40000000
WASM_CALLSTACK_LIMIT = 1024
CodeCache *lru.ARCCache
)
func init() {
CodeCache, _ = lru.NewARC(CODE_CACHE_SIZE)
//if err != nil{
// log.Info("NewARC block error %s", err)
//}
}
func (this *WasmVmService) Invoke() (interface{}, error) {
if len(this.Code) == 0 {
return nil, ERR_EXECUTE_CODE
}
contract := &states.WasmContractParam{}
sink := common.NewZeroCopySource(this.Code)
err := contract.Deserialization(sink)
if err != nil {
return nil, err
}
code, err := this.CacheDB.GetContract(contract.Address)
if err != nil {
return nil, err
}
if code == nil {
return nil, errors.NewErr("wasm contract does not exist")
}
wasmCode, err := code.GetWasmCode()
if err != nil {
return nil, errors.NewErr("not a wasm contract")
}
this.ContextRef.PushContext(&context.Context{ContractAddress: contract.Address, Code: wasmCode})
host := &Runtime{Service: this, Input: contract.Args}
var compiled *exec.CompiledModule
if CodeCache != nil {
cached, ok := CodeCache.Get(contract.Address.ToHexString())
if ok {
compiled = cached.(*exec.CompiledModule)
}
}
if compiled == nil {
compiled, err = ReadWasmModule(wasmCode, false)
if err != nil {
return nil, err
}
CodeCache.Add(contract.Address.ToHexString(), compiled)
}
vm, err := exec.NewVMWithCompiled(compiled, WASM_MEM_LIMITATION)
if err != nil {
return nil, VM_INIT_FAULT
}
vm.HostData = host
vm.AvaliableGas = &exec.Gas{GasLimit: this.GasLimit, LocalGasCounter: 0, GasPrice: this.GasPrice, GasFactor: this.GasFactor, ExecStep: this.ExecStep}
vm.CallStackDepth = uint32(WASM_CALLSTACK_LIMIT)
vm.RecoverPanic = true
entryName := CONTRACT_METHOD_NAME
entry, ok := compiled.RawModule.Export.Entries[entryName]
if ok == false {
return nil, errors.NewErr("[Call]Method:" + entryName + " does not exist!")
}
//get entry index
index := int64(entry.Index)
//get function index
fidx := compiled.RawModule.Function.Types[int(index)]
//get function type
ftype := compiled.RawModule.Types.Entries[int(fidx)]
//no returns of the entry function
if len(ftype.ReturnTypes) > 0 {
return nil, errors.NewErr("[Call]ExecCode error! Invoke function sig error")
}
//no args for passed in, all args in runtime input buffer
this.vm = vm
_, err = vm.ExecCode(index)
if err != nil {
return nil, errors.NewErr("[Call]ExecCode error!" + err.Error())
}
//pop the current context
this.ContextRef.PopContext()
return host.Output, nil
}