-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathoph_call.nim
548 lines (473 loc) · 16.4 KB
/
oph_call.nim
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# Nimbus
# Copyright (c) 2021-2024 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
# http://www.apache.org/licenses/LICENSE-2.0)
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
# http://opensource.org/licenses/MIT)
# at your option. This file may not be copied, modified, or distributed except
# according to those terms.
## EVM Opcode Handlers: Call Operations
## ====================================
##
{.push raises: [].}
import
../../../constants,
../../evm_errors,
../../../common/evmforks,
../../computation,
../../memory,
../../stack,
../../types,
../gas_costs,
../gas_meter,
../op_codes,
../utils/utils_numeric,
./oph_defs,
chronicles,
eth/common,
eth/common/eth_types,
stint
when not defined(evmc_enabled):
import
../../state,
../../../db/ledger
# ------------------------------------------------------------------------------
# Private
# ------------------------------------------------------------------------------
type
LocalParams = object
gas: UInt256
value: UInt256
codeAddress: EthAddress
sender: EthAddress
memInPos: int
memInLen: int
memOutPos: int
memOutLen: int
flags: MsgFlags
memOffset: int
memLength: int
contractAddress: EthAddress
gasCallEIP2929: GasInt
proc updateStackAndParams(q: var LocalParams; c: Computation): EvmResultVoid =
? c.stack.push(0)
let
outLen = calcMemSize(q.memOutPos, q.memOutLen)
inLen = calcMemSize(q.memInPos, q.memInLen)
# get the bigger one
if outLen < inLen:
q.memOffset = q.memInPos
q.memLength = q.memInLen
else:
q.memOffset = q.memOutPos
q.memLength = q.memOutLen
# EIP2929: This came before old gas calculator
# because it will affect `c.gasMeter.gasRemaining`
# and further `childGasLimit`
if FkBerlin <= c.fork:
when evmc_enabled:
if c.host.accessAccount(q.codeAddress) == EVMC_ACCESS_COLD:
q.gasCallEIP2929 = ColdAccountAccessCost - WarmStorageReadCost
else:
c.vmState.mutateStateDB:
if not db.inAccessList(q.codeAddress):
db.accessList(q.codeAddress)
# The WarmStorageReadCostEIP2929 (100) is already deducted in
# the form of a constant `gasCall`
q.gasCallEIP2929 = ColdAccountAccessCost - WarmStorageReadCost
ok()
proc callParams(c: Computation): EvmResult[LocalParams] =
## Helper for callOp()
var res = LocalParams(
gas : ? c.stack.popInt(),
codeAddress : ? c.stack.popAddress(),
value : ? c.stack.popInt(),
memInPos : ? c.stack.popMemRef(),
memInLen : ? c.stack.popMemRef(),
memOutPos : ? c.stack.popMemRef(),
memOutLen : ? c.stack.popMemRef(),
sender : c.msg.contractAddress,
flags : c.msg.flags,
)
res.contractAddress = res.codeAddress
? res.updateStackAndParams(c)
ok(res)
proc callCodeParams(c: Computation): EvmResult[LocalParams] =
## Helper for callCodeOp()
var res = ? c.callParams()
res.contractAddress = c.msg.contractAddress
ok(res)
proc delegateCallParams(c: Computation): EvmResult[LocalParams] =
## Helper for delegateCall()
var res = LocalParams(
gas : ? c.stack.popInt(),
codeAddress : ? c.stack.popAddress(),
memInPos : ? c.stack.popMemRef(),
memInLen : ? c.stack.popMemRef(),
memOutPos : ? c.stack.popMemRef(),
memOutLen : ? c.stack.popMemRef(),
value : c.msg.value,
sender : c.msg.sender,
flags : c.msg.flags,
contractAddress: c.msg.contractAddress,
)
? res.updateStackAndParams(c)
ok(res)
proc staticCallParams(c: Computation): EvmResult[LocalParams] =
## Helper for staticCall()
var res = LocalParams(
gas : ? c.stack.popInt(),
codeAddress : ? c.stack.popAddress(),
memInPos : ? c.stack.popMemRef(),
memInLen : ? c.stack.popMemRef(),
memOutPos : ? c.stack.popMemRef(),
memOutLen : ? c.stack.popMemRef(),
value : 0.u256,
sender : c.msg.contractAddress,
flags : {EVMC_STATIC},
)
res.contractAddress = res.codeAddress
? res.updateStackAndParams(c)
ok(res)
when evmc_enabled:
template execSubCall(c: Computation; msg: ref nimbus_message; p: LocalParams) =
c.chainTo(msg):
c.returnData = @(makeOpenArray(c.res.output_data, c.res.output_size.int))
let actualOutputSize = min(p.memOutLen, c.returnData.len)
if actualOutputSize > 0:
? c.memory.write(p.memOutPos,
c.returnData.toOpenArray(0, actualOutputSize - 1))
c.gasMeter.returnGas(c.res.gas_left)
c.gasMeter.refundGas(c.res.gas_refund)
if c.res.status_code == EVMC_SUCCESS:
? c.stack.top(1)
if not c.res.release.isNil:
c.res.release(c.res)
ok()
else:
proc execSubCall(c: Computation; childMsg: Message; memPos, memLen: int) =
## Call new VM -- helper for `Call`-like operations
# need to provide explicit <c> and <child> for capturing in chainTo proc()
# <memPos> and <memLen> are provided by value and need not be captured
var
child = newComputation(c.vmState, false, childMsg)
c.chainTo(child):
if not child.shouldBurnGas:
c.gasMeter.returnGas(child.gasMeter.gasRemaining)
if child.isSuccess:
c.gasMeter.refundGas(child.gasMeter.gasRefunded)
? c.stack.top(1)
c.returnData = child.output
let actualOutputSize = min(memLen, child.output.len)
if actualOutputSize > 0:
? c.memory.write(memPos, child.output.toOpenArray(0, actualOutputSize - 1))
ok()
# ------------------------------------------------------------------------------
# Private, op handlers implementation
# ------------------------------------------------------------------------------
proc callOp(k: var VmCtx): EvmResultVoid =
## 0xf1, Message-Call into an account
let cpt = k.cpt
if EVMC_STATIC in cpt.msg.flags:
let val = ? cpt.stack[^3, UInt256]
if val > 0.u256:
return err(opErr(StaticContext))
let
p = ? cpt.callParams
res = ? cpt.gasCosts[Call].c_handler(
p.value,
GasParams(
kind: Call,
c_isNewAccount: not cpt.accountExists(p.contractAddress),
c_gasBalance: cpt.gasMeter.gasRemaining - p.gasCallEIP2929,
c_contractGas: p.gas,
c_currentMemSize: cpt.memory.len,
c_memOffset: p.memOffset,
c_memLength: p.memLength))
var (gasCost, childGasLimit) = res
gasCost += p.gasCallEIP2929
if gasCost >= 0:
? cpt.opcodeGastCost(Call, gasCost, reason = $Call)
cpt.returnData.setLen(0)
if cpt.msg.depth >= MaxCallDepth:
debug "Computation Failure",
reason = "Stack too deep",
maximumDepth = MaxCallDepth,
depth = cpt.msg.depth
cpt.gasMeter.returnGas(childGasLimit)
return ok()
if gasCost < 0 and childGasLimit <= 0:
return err(opErr(OutOfGas))
cpt.memory.extend(p.memInPos, p.memInLen)
cpt.memory.extend(p.memOutPos, p.memOutLen)
let senderBalance = cpt.getBalance(p.sender)
if senderBalance < p.value:
cpt.gasMeter.returnGas(childGasLimit)
return ok()
when evmc_enabled:
let
msg = new(nimbus_message)
c = cpt
msg[] = nimbus_message(
kind : EVMC_CALL,
depth : (cpt.msg.depth + 1).int32,
gas : childGasLimit,
sender : p.sender,
recipient : p.contractAddress,
code_address: p.codeAddress,
input_data : cpt.memory.readPtr(p.memInPos),
input_size : p.memInLen.uint,
value : toEvmc(p.value),
flags : p.flags
)
c.execSubCall(msg, p)
else:
cpt.execSubCall(
memPos = p.memOutPos,
memLen = p.memOutLen,
childMsg = Message(
kind: EVMC_CALL,
depth: cpt.msg.depth + 1,
gas: childGasLimit,
sender: p.sender,
contractAddress: p.contractAddress,
codeAddress: p.codeAddress,
value: p.value,
data: @(cpt.memory.read(p.memInPos, p.memInLen)),
flags: p.flags))
ok()
# ---------------------
proc callCodeOp(k: var VmCtx): EvmResultVoid =
## 0xf2, Message-call into this account with an alternative account's code.
let
cpt = k.cpt
p = ? cpt.callCodeParams
res = ? cpt.gasCosts[CallCode].c_handler(
p.value,
GasParams(
kind: CallCode,
c_isNewAccount: not cpt.accountExists(p.contractAddress),
c_gasBalance: cpt.gasMeter.gasRemaining - p.gasCallEIP2929,
c_contractGas: p.gas,
c_currentMemSize: cpt.memory.len,
c_memOffset: p.memOffset,
c_memLength: p.memLength))
var (gasCost, childGasLimit) = res
gasCost += p.gasCallEIP2929
if gasCost >= 0:
? cpt.opcodeGastCost(CallCode, gasCost, reason = $CallCode)
cpt.returnData.setLen(0)
if cpt.msg.depth >= MaxCallDepth:
debug "Computation Failure",
reason = "Stack too deep",
maximumDepth = MaxCallDepth,
depth = cpt.msg.depth
cpt.gasMeter.returnGas(childGasLimit)
return ok()
if gasCost < 0 and childGasLimit <= 0:
return err(opErr(OutOfGas))
cpt.memory.extend(p.memInPos, p.memInLen)
cpt.memory.extend(p.memOutPos, p.memOutLen)
let senderBalance = cpt.getBalance(p.sender)
if senderBalance < p.value:
cpt.gasMeter.returnGas(childGasLimit)
return ok()
when evmc_enabled:
let
msg = new(nimbus_message)
c = cpt
msg[] = nimbus_message(
kind : EVMC_CALLCODE,
depth : (cpt.msg.depth + 1).int32,
gas : childGasLimit,
sender : p.sender,
recipient : p.contractAddress,
code_address: p.codeAddress,
input_data : cpt.memory.readPtr(p.memInPos),
input_size : p.memInLen.uint,
value : toEvmc(p.value),
flags : p.flags
)
c.execSubCall(msg, p)
else:
cpt.execSubCall(
memPos = p.memOutPos,
memLen = p.memOutLen,
childMsg = Message(
kind: EVMC_CALLCODE,
depth: cpt.msg.depth + 1,
gas: childGasLimit,
sender: p.sender,
contractAddress: p.contractAddress,
codeAddress: p.codeAddress,
value: p.value,
data: @(cpt.memory.read(p.memInPos, p.memInLen)),
flags: p.flags))
ok()
# ---------------------
proc delegateCallOp(k: var VmCtx): EvmResultVoid =
## 0xf4, Message-call into this account with an alternative account's
## code, but persisting the current values for sender and value.
let
cpt = k.cpt
p = ? cpt.delegateCallParams
res = ? cpt.gasCosts[DelegateCall].c_handler(
p.value,
GasParams(
kind: DelegateCall,
c_isNewAccount: not cpt.accountExists(p.contractAddress),
c_gasBalance: cpt.gasMeter.gasRemaining - p.gasCallEIP2929,
c_contractGas: p.gas,
c_currentMemSize: cpt.memory.len,
c_memOffset: p.memOffset,
c_memLength: p.memLength))
var (gasCost, childGasLimit) = res
gasCost += p.gasCallEIP2929
if gasCost >= 0:
? cpt.opcodeGastCost(DelegateCall, gasCost, reason = $DelegateCall)
cpt.returnData.setLen(0)
if cpt.msg.depth >= MaxCallDepth:
debug "Computation Failure",
reason = "Stack too deep",
maximumDepth = MaxCallDepth,
depth = cpt.msg.depth
cpt.gasMeter.returnGas(childGasLimit)
return ok()
if gasCost < 0 and childGasLimit <= 0:
return err(opErr(OutOfGas))
cpt.memory.extend(p.memInPos, p.memInLen)
cpt.memory.extend(p.memOutPos, p.memOutLen)
when evmc_enabled:
let
msg = new(nimbus_message)
c = cpt
msg[] = nimbus_message(
kind : EVMC_DELEGATECALL,
depth : (cpt.msg.depth + 1).int32,
gas : childGasLimit,
sender : p.sender,
recipient : p.contractAddress,
code_address: p.codeAddress,
input_data : cpt.memory.readPtr(p.memInPos),
input_size : p.memInLen.uint,
value : toEvmc(p.value),
flags : p.flags
)
c.execSubCall(msg, p)
else:
cpt.execSubCall(
memPos = p.memOutPos,
memLen = p.memOutLen,
childMsg = Message(
kind: EVMC_DELEGATECALL,
depth: cpt.msg.depth + 1,
gas: childGasLimit,
sender: p.sender,
contractAddress: p.contractAddress,
codeAddress: p.codeAddress,
value: p.value,
data: @(cpt.memory.read(p.memInPos, p.memInLen)),
flags: p.flags))
ok()
# ---------------------
proc staticCallOp(k: var VmCtx): EvmResultVoid =
## 0xfa, Static message-call into an account.
let
cpt = k.cpt
p = ? cpt.staticCallParams
res = ? cpt.gasCosts[StaticCall].c_handler(
p.value,
GasParams(
kind: StaticCall,
c_isNewAccount: not cpt.accountExists(p.contractAddress),
c_gasBalance: cpt.gasMeter.gasRemaining - p.gasCallEIP2929,
c_contractGas: p.gas,
c_currentMemSize: cpt.memory.len,
c_memOffset: p.memOffset,
c_memLength: p.memLength))
var (gasCost, childGasLimit) = res
gasCost += p.gasCallEIP2929
if gasCost >= 0:
? cpt.opcodeGastCost(StaticCall, gasCost, reason = $StaticCall)
cpt.returnData.setLen(0)
if cpt.msg.depth >= MaxCallDepth:
debug "Computation Failure",
reason = "Stack too deep",
maximumDepth = MaxCallDepth,
depth = cpt.msg.depth
cpt.gasMeter.returnGas(childGasLimit)
return ok()
if gasCost < 0 and childGasLimit <= 0:
return err(opErr(OutOfGas))
cpt.memory.extend(p.memInPos, p.memInLen)
cpt.memory.extend(p.memOutPos, p.memOutLen)
when evmc_enabled:
let
msg = new(nimbus_message)
c = cpt
msg[] = nimbus_message(
kind : EVMC_CALL,
depth : (cpt.msg.depth + 1).int32,
gas : childGasLimit,
sender : p.sender,
recipient : p.contractAddress,
code_address: p.codeAddress,
input_data : cpt.memory.readPtr(p.memInPos),
input_size : p.memInLen.uint,
value : toEvmc(p.value),
flags : p.flags
)
c.execSubCall(msg, p)
else:
cpt.execSubCall(
memPos = p.memOutPos,
memLen = p.memOutLen,
childMsg = Message(
kind: EVMC_CALL,
depth: cpt.msg.depth + 1,
gas: childGasLimit,
sender: p.sender,
contractAddress: p.contractAddress,
codeAddress: p.codeAddress,
value: p.value,
data: @(cpt.memory.read(p.memInPos, p.memInLen)),
flags: p.flags))
ok()
# ------------------------------------------------------------------------------
# Public, op exec table entries
# ------------------------------------------------------------------------------
const
VmOpExecCall*: seq[VmOpExec] = @[
(opCode: Call, ## 0xf1, Message-Call into an account
forks: VmOpAllForks,
name: "call",
info: "Message-Call into an account",
exec: (prep: VmOpIgnore,
run: callOp,
post: VmOpIgnore)),
(opCode: CallCode, ## 0xf2, Message-Call with alternative code
forks: VmOpAllForks,
name: "callCode",
info: "Message-call into this account with alternative account's code",
exec: (prep: VmOpIgnore,
run: callCodeOp,
post: VmOpIgnore)),
(opCode: DelegateCall, ## 0xf4, CallCode with persisting sender and value
forks: VmOpHomesteadAndLater,
name: "delegateCall",
info: "Message-call into this account with an alternative account's " &
"code but persisting the current values for sender and value.",
exec: (prep: VmOpIgnore,
run: delegateCallOp,
post: VmOpIgnore)),
(opCode: StaticCall, ## 0xfa, Static message-call into an account
forks: VmOpByzantiumAndLater,
name: "staticCall",
info: "Static message-call into an account",
exec: (prep: VmOpIgnore,
run: staticCallOp,
post: VmOpIgnore))]
# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------