forked from SealSC/SealEVM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sealEVM.go
271 lines (226 loc) · 7.52 KB
/
sealEVM.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* Copyright 2020 The SealEVM Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package SealEVM
import (
"github.com/SealSC/SealEVM/environment"
"github.com/SealSC/SealEVM/evmErrors"
"github.com/SealSC/SealEVM/evmInt256"
"github.com/SealSC/SealEVM/instructions"
"github.com/SealSC/SealEVM/memory"
"github.com/SealSC/SealEVM/opcodes"
"github.com/SealSC/SealEVM/precompiledContracts"
"github.com/SealSC/SealEVM/stack"
"github.com/SealSC/SealEVM/storage"
)
type EVMResultCallback func(result ExecuteResult, err error)
type EVMParam struct {
MaxStackDepth int
ExternalStore storage.IExternalStorage
ResultCallback EVMResultCallback
Context *environment.Context
}
type EVM struct {
stack *stack.Stack
memory *memory.Memory
storage *storage.Storage
context *environment.Context
instructions instructions.IInstructions
resultNotify EVMResultCallback
}
type ExecuteResult struct {
ResultData []byte
GasLeft uint64
StorageCache storage.ResultCache
ExitOpCode opcodes.OpCode
}
func Load() {
instructions.Load()
}
func New(param EVMParam) *EVM {
if param.Context.Block.GasLimit.Cmp(param.Context.Transaction.GasLimit.Int) < 0 {
param.Context.Transaction.GasLimit = evmInt256.FromBigInt(param.Context.Block.GasLimit.Int)
}
evm := &EVM{
stack: stack.New(param.MaxStackDepth),
memory: memory.New(),
storage: storage.New(param.ExternalStore),
context: param.Context,
instructions: nil,
resultNotify: param.ResultCallback,
}
evm.instructions = instructions.New(evm, evm.stack, evm.memory, evm.storage, evm.context, nil, closure)
return evm
}
func NewWithCache(param EVMParam, s *storage.Storage) *EVM {
if param.Context.Block.GasLimit.Cmp(param.Context.Transaction.GasLimit.Int) < 0 {
param.Context.Transaction.GasLimit = evmInt256.FromBigInt(param.Context.Block.GasLimit.Int)
}
evm := &EVM{
stack: stack.New(param.MaxStackDepth),
memory: memory.New(),
storage: s,
context: param.Context,
instructions: nil,
resultNotify: param.ResultCallback,
}
evm.instructions = instructions.New(evm, evm.stack, evm.memory, evm.storage, evm.context, nil, closure)
return evm
}
func (e *EVM) subResult(result ExecuteResult, err error) {
if err == nil && result.ExitOpCode != opcodes.REVERT {
storage.MergeResultCache(&result.StorageCache, &e.storage.ResultCache)
}
}
func (e *EVM) executePreCompiled(addr uint64, input []byte) (ExecuteResult, error) {
contract := precompiledContracts.GetContract(addr)
gasCost := contract.GasCost(input)
gasLeft := e.instructions.GetGasLeft()
result := ExecuteResult{
ResultData: nil,
GasLeft: gasLeft,
StorageCache: e.storage.ResultCache,
}
if gasLeft < gasCost {
return result, evmErrors.OutOfGas
}
execRet, err := contract.Execute(input)
gasLeft -= gasCost
e.instructions.SetGasLimit(gasLeft)
result.ResultData = execRet
return result, err
}
func (e *EVM) ExecuteContract(doTransfer bool) (ExecuteResult, error) {
contractAddr := e.context.Contract.Namespace
gasLeft := e.instructions.GetGasLeft()
result := ExecuteResult{
ResultData: nil,
GasLeft: gasLeft,
StorageCache: e.storage.ResultCache,
}
if doTransfer {
msg := e.context.Message
//doing transfer for non-zero value
if msg.Value.Sign() != 0 {
if e.instructions.IsReadOnly() {
return result, evmErrors.WriteProtection
}
if e.storage.CanTransfer(msg.Caller, contractAddr, msg.Value) {
e.storage.BalanceModify(msg.Caller, msg.Value, true)
e.storage.BalanceModify(contractAddr, msg.Value, false)
} else {
return result, evmErrors.InsufficientBalance
}
}
}
if contractAddr != nil {
//check if is precompiled
if contractAddr.IsUint64() {
addr := contractAddr.Uint64()
if addr < precompiledContracts.PrecompiledContractCount() {
return e.executePreCompiled(addr, e.context.Message.Data)
}
}
}
execRet, gasLeft, err := e.instructions.ExecuteContract()
result.ResultData = execRet
result.ExitOpCode = e.instructions.ExitOpCode()
if e.resultNotify != nil {
e.resultNotify(result, err)
}
return result, err
}
func (e *EVM) getClosureDefaultEVM(param instructions.ClosureParam) *EVM {
newEVM := NewWithCache(EVMParam{
MaxStackDepth: 1024,
ExternalStore: e.storage.ExternalStorage,
ResultCallback: e.subResult,
Context: &environment.Context{
Block: e.context.Block,
Transaction: e.context.Transaction,
Message: environment.Message{
Data: param.CallData,
},
},
}, e.storage)
newEVM.context.Contract = environment.Contract{
Namespace: param.ContractAddress,
Code: param.ContractCode,
Hash: param.ContractHash,
}
return newEVM
}
func (e *EVM) commonCall(param instructions.ClosureParam) ([]byte, error) {
newEVM := e.getClosureDefaultEVM(param)
//set storage namespace and call value
switch param.OpCode {
case opcodes.CALL:
newEVM.context.Contract.Namespace = param.ContractAddress
newEVM.context.Message.Value = param.CallValue
newEVM.context.Message.Caller = e.context.Contract.Namespace
case opcodes.STATICCALL:
newEVM.context.Contract.Namespace = param.ContractAddress
newEVM.context.Message.Value = param.CallValue
newEVM.context.Message.Caller = e.context.Contract.Namespace
case opcodes.CALLCODE:
newEVM.context.Contract.Namespace = e.context.Contract.Namespace
newEVM.context.Message.Value = param.CallValue
newEVM.context.Message.Caller = e.context.Contract.Namespace
case opcodes.DELEGATECALL:
newEVM.context.Contract.Namespace = e.context.Contract.Namespace
newEVM.context.Message.Value = e.context.Message.Value
newEVM.context.Message.Caller = e.context.Message.Caller
}
if param.OpCode == opcodes.STATICCALL || e.instructions.IsReadOnly() {
newEVM.instructions.SetReadOnly()
}
ret, err := newEVM.ExecuteContract(opcodes.CALL == param.OpCode)
if ret.ExitOpCode == opcodes.REVERT {
err = evmErrors.RevertErr
}
e.instructions.SetGasLimit(ret.GasLeft)
return ret.ResultData, err
}
func (e *EVM) commonCreate(param instructions.ClosureParam) ([]byte, error) {
var addr *evmInt256.Int
if opcodes.CREATE == param.OpCode {
addr = e.storage.ExternalStorage.CreateAddress(e.context.Message.Caller, e.context.Transaction)
} else {
addr = e.storage.ExternalStorage.CreateFixedAddress(e.context.Message.Caller, param.CreateSalt, e.context.Transaction)
}
newEVM := e.getClosureDefaultEVM(param)
newEVM.context.Contract.Namespace = addr
newEVM.context.Message.Value = param.CallValue
newEVM.context.Message.Caller = e.context.Contract.Namespace
ret, err := newEVM.ExecuteContract(true)
if ret.ExitOpCode == opcodes.REVERT {
err = evmErrors.RevertErr
}
e.instructions.SetGasLimit(ret.GasLeft)
return ret.ResultData, err
}
func closure(param instructions.ClosureParam) ([]byte, error) {
evm, ok := param.VM.(*EVM)
if !ok {
return nil, evmErrors.InvalidEVMInstance
}
switch param.OpCode {
case opcodes.CALL, opcodes.CALLCODE, opcodes.DELEGATECALL, opcodes.STATICCALL:
return evm.commonCall(param)
case opcodes.CREATE, opcodes.CREATE2:
return evm.commonCreate(param)
}
return nil, nil
}