-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
ExecutionManager.sol
1015 lines (904 loc) · 41.4 KB
/
ExecutionManager.sol
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
pragma solidity ^0.5.0;
pragma experimental ABIEncoderV2;
/* Internal Imports */
import {DataTypes as dt} from "./DataTypes.sol";
import {FullStateManager} from "./FullStateManager.sol";
import {ContractAddressGenerator} from "./ContractAddressGenerator.sol";
import {PurityChecker} from "./PurityChecker.sol";
import {RLPEncode} from "./RLPEncode.sol";
import {L2ToL1MessagePasser} from "./precompiles/L2ToL1MessagePasser.sol";
import {L1MessageSender} from "./precompiles/L1MessageSender.sol";
/**
* @title ExecutionManager
* @notice The execution manager ensures that the execution of each transaction is sandboxed in a distinct environment as defined
* by the supplied backend. Only state / contracts from that backend will be accessed.
*/
contract ExecutionManager {
FullStateManager stateManager;
address ZERO_ADDRESS = 0x0000000000000000000000000000000000000000;
// expected queue origin for calls from L1
uint constant L1_QUEUE_ORIGIN = 1;
// bitwise right shift 28 * 8 bits so the 4 method ID bytes are in the right-most bytes
bytes32 constant ovmCallMethodId = keccak256("ovmCALL()") >> 224;
bytes32 constant ovmCreateMethodId = keccak256("ovmCREATE()") >> 224;
// Precompile addresses
address constant l2ToL1MessagePasserOvmAddress = 0x4200000000000000000000000000000000000000;
address constant l1MsgSenderAddress = 0x4200000000000000000000000000000000000001;
// Execution storage
dt.ExecutionContext executionContext;
// Add Contract Address Generation contract
ContractAddressGenerator contractAddressGenerator;
// Add Purity Checker contract
PurityChecker purityChecker;
RLPEncode rlp;
// for testing: if true, then do not perform purity checking on init code or deployed bytecode
bool overridePurityChecker;
// Events
event ActiveContract(address _activeContract);
event CreatedContract(
address _ovmContractAddress,
address _codeContractAddress,
bytes32 _codeContractHash
);
event CallingWithEOA(
address _ovmFromAddress,
address _ovmToAddress
);
event EOACreatedContract(
address _ovmContractAddress
);
event SetStorage(
address _ovmContractAddress,
bytes32 _slot,
bytes32 _value
);
event EOACallRevert(
bytes _revertMessage
);
/**
* @notice Construct a new ExecutionManager with a specified purity checker & owner.
* @param _opcodeWhitelistMask A bit mask representing which opcodes are whitelisted or not for our purity checker
* @param _owner The owner of this contract.
* @param _blockGasLimit The block gas limit for OVM blocks
* @param _overridePurityChecker Set to true to disable purity checking (WARNING: Only do this in test environments)
*/
constructor(uint256 _opcodeWhitelistMask, address _owner, uint _blockGasLimit, bool _overridePurityChecker) public {
stateManager = new FullStateManager();
rlp = new RLPEncode();
// Set override purity checker flag
overridePurityChecker = _overridePurityChecker;
// Set the purity checker address
purityChecker = new PurityChecker(_opcodeWhitelistMask, address(this));
// Initialize new contract address generator
contractAddressGenerator = new ContractAddressGenerator();
// Associate all Ethereum precompiles
for (uint160 i = 1; i < 20; i++) {
stateManager.associateCodeContract(address(i), address(i));
}
// Deploy custom precompiles
L2ToL1MessagePasser l1ToL2MessagePasser = new L2ToL1MessagePasser(address(this));
stateManager.associateCodeContract(l2ToL1MessagePasserOvmAddress, address(l1ToL2MessagePasser));
L1MessageSender l1MessageSender = new L1MessageSender(address(this));
stateManager.associateCodeContract(l1MsgSenderAddress, address(l1MessageSender));
executionContext.gasLimit = _blockGasLimit;
executionContext.chainId = 108;
// Set our owner
// TODO
}
/**
* @notice Increments the provided address's nonce.
* This is only used by the sequencer to correct nonces when transactions fail.
* @param addr The address of the nonce to increment.
*/
function incrementNonce(address addr) external {
stateManager.incrementOvmContractNonce(addr);
}
/********************
* Execute EOA Calls *
********************/
/**
* @notice Execute an Externally Owned Account (EOA) call. This will accept all information required
* for an OVM transaction as well as a signature from an EOA. First we will calculate the
* sender address (EOA address) and then we will perform the call.
* @param _timestamp The timestamp which should be used for this call's context.
* @param _queueOrigin The parent-chain queue from which this call originated.
* @param _nonce The current nonce of the EOA.
* @param _ovmEntrypoint The contract which this transaction should be executed against.
* @param _callBytes The calldata for this ovm transaction.
* @param _v The v value of the ECDSA signature + CHAIN_ID.
* @param _r The r value of the ECDSA signature.
* @param _s The s value of the ECDSA signature.
*/
function executeEOACall(
uint _timestamp,
uint _queueOrigin,
uint _nonce,
address _ovmEntrypoint,
bytes memory _callBytes,
uint8 _v,
bytes32 _r,
bytes32 _s
) public {
// Get EOA address
address eoaAddress = recoverEOAAddress(_nonce, _ovmEntrypoint, _callBytes, _v, _r, _s);
// Require that the EOA signature isn't zero (invalid signature)
require(eoaAddress != ZERO_ADDRESS, "Failed to recover signature");
// Require nonce to be correct
require(_nonce == stateManager.getOvmContractNonce(eoaAddress), "Incorrect nonce!");
emit CallingWithEOA(
eoaAddress,
_ovmEntrypoint
);
// Make the EOA call for the account
executeTransaction(_timestamp, _queueOrigin, _ovmEntrypoint, _callBytes, eoaAddress, ZERO_ADDRESS, false);
}
/**
* @notice Execute an unsigned EOA transaction. Note that unsigned EOA calls are unauthenticated.
* This means that they should not be allowed for normal execution.
* @param _timestamp The timestamp which should be used for this call's context.
* @param _queueOrigin The parent-chain queue from which this call originated.
* @param _ovmEntrypoint The contract which this transaction should be executed against.
* @param _callBytes The calldata for this ovm transaction.
* @param _fromAddress The address which this call should originate from--the msg.sender.
* @param _allowRevert Flag which controls whether or not to revert in the case of failure.
*/
function executeTransaction(
uint _timestamp,
uint _queueOrigin,
address _ovmEntrypoint,
bytes memory _callBytes,
address _fromAddress,
address _l1MsgSenderAddress,
bool _allowRevert
) public {
require(_timestamp > 0, "Timestamp must be greater than 0");
uint _nonce = stateManager.getOvmContractNonce(_fromAddress);
// Initialize our context
initializeContext(_timestamp, _queueOrigin, _fromAddress, _l1MsgSenderAddress);
// Set the active contract to be our EOA address
switchActiveContract(_fromAddress);
// Set methodId based on whether we're creating a contract
bytes32 methodId;
uint256 callSize;
bool isCreate = _ovmEntrypoint == ZERO_ADDRESS;
// Check if we're creating -- ovmEntrypoint == ZERO_ADDRESS
if (isCreate) {
methodId = ovmCreateMethodId;
callSize = _callBytes.length + 4;
// Emit event that we are creating a contract with an EOA
address _newOvmContractAddress = contractAddressGenerator.getAddressFromCREATE(_fromAddress, _nonce);
emit EOACreatedContract(_newOvmContractAddress);
} else {
methodId = ovmCallMethodId;
callSize = _callBytes.length + 32 + 4;
// Creates will get incremented, but calls need to be as well!
stateManager.incrementOvmContractNonce(_fromAddress);
}
assembly {
if eq(isCreate, 0) {
_callBytes := sub(_callBytes, 4)
// And now set the ovmEntrypoint
mstore(add(_callBytes, 4), _ovmEntrypoint)
}
if eq(isCreate, 1) {
_callBytes := add(_callBytes, 28)
}
mstore8(_callBytes, shr(24, methodId))
mstore8(add(_callBytes, 1), shr(16, methodId))
mstore8(add(_callBytes, 2), shr(8, methodId))
mstore8(add(_callBytes, 3), methodId)
}
bool success = false;
address addr = address(this);
bytes memory result;
assembly {
success := call(gas, addr, 0, _callBytes, callSize, 0, 0)
result := mload(0x40)
let resultData := add(result, 0x20)
returndatacopy(resultData, 0, returndatasize)
if eq(success, 1) {
return(resultData, returndatasize)
}
if eq(_allowRevert, 1) {
revert(resultData, returndatasize)
}
mstore(result, returndatasize)
mstore(0x40, add(resultData, returndatasize))
}
if (!success) {
// We need the tx to succeed even on failure so logs, nonce, etc. are preserved.
// This is how we indicate that the tx "failed."
emit EOACallRevert(result);
assembly {
return(0,0)
}
}
}
/**
* @notice Recover the EOA of an ECDSA-signed Ethereum transaction. Note some values will be set to zero by default.
* Additionally, the `to=ZERO_ADDRESS` is reserved for contract creation transactions.
* @param _nonce The nonce of the transaction.
* @param _to The entrypoint / recipient of the transaction.
* @param _callData The calldata which will be applied to the entrypoint contract.
* @param _v The v value of the ECDSA signature + CHAIN_ID.
* @param _r The r value of the ECDSA signature.
* @param _s The s value of the ECDSA signature.
*/
function recoverEOAAddress(
uint _nonce,
address _to,
bytes memory _callData,
uint8 _v,
bytes32 _r,
bytes32 _s
) public view returns (address) {
bytes[] memory message = new bytes[](9);
message[0] = rlp.encodeUint(_nonce); // Nonce
message[1] = rlp.encodeUint(0); // Gas price
message[2] = rlp.encodeUint(executionContext.gasLimit); // Gas limit
// To -- Special rlp encoding handling if _to is the ZERO_ADDRESS
if (_to == ZERO_ADDRESS) {
message[3] = rlp.encodeUint(0);
} else {
message[3] = rlp.encodeAddress(_to);
}
message[4] = rlp.encodeUint(0); // Value
message[5] = rlp.encodeBytes(_callData); // Data
message[6] = rlp.encodeUint(executionContext.chainId); // ChainID
message[7] = rlp.encodeUint(0); // Zeros for R
message[8] = rlp.encodeUint(0); // Zeros for S
bytes memory encodedMessage = rlp.encodeList(message);
bytes32 hash = keccak256(abi.encodePacked(encodedMessage));
/*
* Replay protection is used to prevent signatures on one chain from
* being used on other chains. To support replay protection ethereum
* modifies the value of v in the signature to be different for each
* chainID. This was implemented based on the following EIP:
* https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md#specification
*/
return ecrecover(hash, (_v - uint8(executionContext.chainId) * 2) - 8, _r, _s);
}
/**********************
* OVM Context Opcodes *
**********************/
/**
* @notice CALLER opcode (msg.sender) -- this gets the caller of the currently-running contract.
* Note: Calling this requires a CALL, which changes the CALLER, which is why we use executionContext.
*
* This is a raw function, so there are no listed (ABI-encoded) inputs / outputs.
* Below format of the bytes expected as input and written as output:
* calldata: 4 bytes: [methodID (bytes4)]
* returndata: 32-byte CALLER address containing the left-padded, big-endian encoding of the address.
*/
function ovmCALLER() public view {
// First make sure the ovmMsgSender was set
require(executionContext.ovmMsgSender != ZERO_ADDRESS, "Error: attempting to access non-existent msgSender.");
// This is returned as left-padded, big-endian, so pad it left!
bytes32 addressBytes = bytes32(bytes20(executionContext.ovmMsgSender)) >> 96;
assembly {
let addressMemory := mload(0x40)
mstore(addressMemory, addressBytes)
return(addressMemory, 32)
}
}
/**
* @notice ADDRESS opcode -- Gets the address of the currently-running contract.
* Note: Calling this requires a CALL, which changes the ADDRESS, which is why we use executionContext.
*
* This is a raw function, so there are no listed (ABI-encoded) inputs / outputs.
* Below format of the bytes expected as input and written as output:
* calldata: 4 bytes: [methodID (bytes4)]
* returndata: 32-byte ADDRESS containing the left-padded, big-endian encoding of the address.
*/
function ovmADDRESS() public view {
// First make sure the ovmMsgSender was set
require(executionContext.ovmActiveContract != ZERO_ADDRESS, "Error: attempting to access non-existent ovmActiveContract.");
// This is returned as left-padded, big-endian, so pad it left!
bytes32 addressBytes = bytes32(bytes20(executionContext.ovmActiveContract)) >> 96;
assembly {
let addressMemory := mload(0x40)
mstore(addressMemory, addressBytes)
return(addressMemory, 32)
}
}
/**
* @notice TIMESTAMP opcode -- this gets the current timestamp. Since the L2 value for this
* will necessarily be different than L1, this needs to be overridden for the OVM.
* Note: This is a raw function, so there are no listed (ABI-encoded) inputs / outputs.
* Below format of the bytes expected as input and written as output:
* calldata: 4 bytes: [methodID (bytes4)]
* returndata: uint256 representing the current timestamp.
*/
function ovmTIMESTAMP() public view {
uint t = executionContext.timestamp;
assembly {
let timestampMemory := mload(0x40)
mstore(timestampMemory, t)
return(timestampMemory, 32)
}
}
/**
* @notice GASLIMIT opcode -- this gets the gas limit for the current transaction. Since the L2 value for this
* may be different than L1, this needs to be overridden for the OVM.
* Note: This is a raw function, so there are no listed (ABI-encoded) inputs / outputs.
* Below format of the bytes expected as input and written as output:
* calldata: 4 bytes: [methodID (bytes4)]
* returndata: uint256 representing the current gas limit.
*/
function ovmGASLIMIT() public view {
uint g = executionContext.gasLimit;
assembly {
let gasLimitMemory := mload(0x40)
mstore(gasLimitMemory, g)
return(gasLimitMemory, 32)
}
}
/**
* @notice Gets the gas limit for fraud proofs. This value exists to make sure that fraud proofs
* don't require an excessive amount of gas that is not feasible on L1.
* Note: This is a raw function, so there are no listed (ABI-encoded) inputs / outputs.
* Below format of the bytes expected as input and written as output:
* calldata: 4 bytes: [methodID (bytes4)]
* returndata: uint256 representing the fraud proof gas limit.
*/
function ovmBlockGasLimit() public view {
uint g = executionContext.gasLimit;
assembly {
let gasLimitMemory := mload(0x40)
mstore(gasLimitMemory, g)
return(gasLimitMemory, 32)
}
}
/**
* @notice Gets the queue origin in the current Execution Context.
* Note: This is a raw function, so there are no listed (ABI-encoded) inputs / outputs.
* Below format of the bytes expected as input and written as output:
* calldata: 4 bytes: [methodID (bytes4)]
* returndata: uint256 representing the current queue origin.
*/
function ovmQueueOrigin() public view {
uint q = executionContext.queueOrigin;
assembly {
let queueOriginMemory := mload(0x40)
mstore(queueOriginMemory, q)
return(queueOriginMemory, 32)
}
}
/**
* @notice This gets whether or not this contract is currently in a static call context.
* Note: This is a raw function, so there are no listed (ABI-encoded) inputs / outputs.
* Below format of the bytes expected as input and written as output:
* calldata: 4 bytes: [methodID (bytes4)]
* returndata: uint256 of 1 if in a static context and 0 if not.
*/
function isStaticContext() public view {
uint staticContext = executionContext.inStaticContext ? 1 : 0;
assembly {
let contextMemory := mload(0x40)
mstore(contextMemory, staticContext)
return(contextMemory, 32)
}
}
/**
* @notice ORIGIN opcode (tx.origin) -- this gets the origin address of the
* externally owned account that initiated this transaction.
* Note: If we are in a transaction that wasn't initiated by an externally
* owned account this function will revert.
*
* This is a raw function, so there are no listed (ABI-encoded) inputs / outputs.
* Below format of the bytes expected as input and written as output:
* returndata: 32-byte ORIGIN address containing the left-padded, big-endian encoding of the address.
*/
function ovmORIGIN() public view {
require(executionContext.ovmTxOrigin != ZERO_ADDRESS, "Error: attempting to access non-existent txOrigin.");
bytes32 addressBytes = bytes32(bytes20(executionContext.ovmTxOrigin)) >> 96;
assembly {
let addressMemory := mload(0x40)
mstore(addressMemory, addressBytes)
return(addressMemory, 32)
}
}
/***************************
* Contract Creation Opcode *
***************************/
/**
* @notice CREATE opcode -- deploying a new ovm contract to a CREATE address.
* Note: This is a raw function, so there are no listed (ABI-encoded) inputs / outputs.
* Below format of the bytes expected as input and written as output:
* calldata: variable-length bytes:
* [methodID (bytes4)]
* [ovmInitcode (bytes (variable length))]
* returndata: [newOvmContractAddress (as bytes32)] -- will be all 0s if this create failed.
*/
function ovmCREATE() public {
if (executionContext.inStaticContext) {
// Cannot create new contracts from a STATICCALL -- return 0 address
assembly {
let returnData := mload(0x40)
mstore(returnData, 0)
return(returnData, 0x20)
}
}
bytes memory _ovmInitcode;
assembly {
_ovmInitcode := mload(0x40)
// ignore methodID
let initcodeSize := sub(calldatasize, 4)
// need to ABI-encode _ovmInitcode for solidity calls
mstore(_ovmInitcode, initcodeSize)
// read calldata, ignoring methodID -- the rest is _ovmInitcode
calldatacopy(add(_ovmInitcode, 0x20), 4, initcodeSize)
// update free mem pointer
mstore(0x40, add(add(_ovmInitcode, 0x20), initcodeSize))
}
// First we need to generate the CREATE address
address creator = executionContext.ovmActiveContract;
uint creatorNonce = stateManager.getOvmContractNonce(creator);
address _newOvmContractAddress = contractAddressGenerator.getAddressFromCREATE(creator, creatorNonce);
// Next we need to actually create the contract in our state at that address
if (!createNewContract(_newOvmContractAddress, _ovmInitcode)) {
// Failure: Return 0 address
assembly {
let returnData := mload(0x40)
mstore(returnData, 0)
return(returnData, 0x20)
}
}
// We also need to increment the contract nonce
stateManager.incrementOvmContractNonce(creator);
// Shifting so that it is left-padded, big-endian ('00'x12 + 20 bytes of address)
bytes32 newOvmContractAddressBytes32 = bytes32(bytes20(_newOvmContractAddress)) >> 96;
// And finally return the address of the newly created ovmContract
assembly {
let returnData := mload(0x40)
mstore(returnData, newOvmContractAddressBytes32)
return(returnData, 0x20)
}
}
/**
* @notice CREATE2 opcode -- deploying a new ovm contract to a CREATE2 address.
* Note: This is a raw function, so there are no listed (ABI-encoded) inputs / outputs.
* Below format of the bytes expected as input and written as output:
* calldata: variable-length bytes:
* [methodID (bytes4)]
* [salt (bytes32)]
* [ovmInitcode (bytes (variable length))]
* returndata: [newOvmContractAddress (as bytes32)] -- will be all 0s if this create failed.
*/
function ovmCREATE2() public {
if (executionContext.inStaticContext) {
// Cannot create new contracts from a STATICCALL -- return 0 address
assembly {
let returnData := mload(0x40)
mstore(returnData, 0)
return(returnData, 0x20)
}
}
bytes memory _ovmInitcode;
bytes32 _salt;
assembly {
// everything other than MethodID and salt is initcode
let initcodeSize := sub(calldatasize, 0x24)
_ovmInitcode := mload(0x40)
// skip methodID, copy first 32 bytes for _salt
_salt := calldataload(4)
// need to ABI-encode _ovmInitcode for solidity calls
mstore(_ovmInitcode, initcodeSize)
// read calldata, ignoring methodID and salt -- the rest is _ovmInitcode
calldatacopy(add(_ovmInitcode, 0x20), 0x24, initcodeSize)
mstore(0x40, add(0x20,add(_ovmInitcode, initcodeSize)))
}
// First we need to generate the CREATE2 address
address creator = executionContext.ovmActiveContract;
address _newOvmContractAddress = contractAddressGenerator.getAddressFromCREATE2(creator, _salt, _ovmInitcode);
// Next we need to actually create the contract in our state at that address
if (!createNewContract(_newOvmContractAddress, _ovmInitcode)) {
// Failure: Return 0 address
assembly {
let returnData := mload(0x40)
mstore(returnData, 0)
return(returnData, 0x20)
}
}
// Shifting so that it is left-padded, big-endian ('00'x12 + 20 bytes of address)
bytes32 newOvmContractAddressBytes32 = bytes32(bytes20(_newOvmContractAddress)) >> 96;
// And finally return the address of the newly created ovmContract
assembly {
let returnData := mload(0x40)
mstore(returnData, newOvmContractAddressBytes32)
return(returnData, 0x20)
}
}
/********* Utils *********/
/**
* @notice Create a new contract at some OVM contract address.
* @param _newOvmContractAddress The desired OVM contract address for this new contract we will deploy.
* @param _ovmInitcode The initcode for our new contract
* @return True if this succeeded, false otherwise.
*/
function createNewContract(address _newOvmContractAddress, bytes memory _ovmInitcode) internal returns (bool){
// Purity check the initcode -- unless the overridePurityChecker flag is set to true
if (!overridePurityChecker && !purityChecker.isBytecodePure(_ovmInitcode)) {
// Contract init code is not pure.
return false;
}
// Switch the context to be the new contract
(address oldMsgSender, address oldActiveContract) = switchActiveContract(_newOvmContractAddress);
// Deploy the _ovmInitcode as a code contract -- Note the init script will run in the newly set context
address codeContractAddress = stateManager.deployCodeContract(_ovmInitcode);
// Get the runtime bytecode
bytes memory codeContractBytecode = stateManager.getCodeContractBytecode(codeContractAddress);
// Purity check the runtime bytecode -- unless the overridePurityChecker flag is set to true
if (!overridePurityChecker && !purityChecker.isBytecodePure(codeContractBytecode)) {
// Contract runtime bytecode is not pure.
return false;
}
// Associate the code contract with our ovm contract
stateManager.associateCodeContract(_newOvmContractAddress, codeContractAddress);
// Get the code contract address to be emitted by a CreatedContract event
bytes32 codeContractHash = keccak256(codeContractBytecode);
// Revert to the previous the context
restoreContractContext(oldMsgSender, oldActiveContract);
// Emit CreatedContract event! We've created a new contract!
emit CreatedContract(_newOvmContractAddress, codeContractAddress, codeContractHash);
return true;
}
/************************
* Contract CALL Opcodes *
************************/
/**
* @notice CALL opcode -- simply calls a particular code contract with the desired OVM contract context.
* Note: This is a raw function, so there are no listed (ABI-encoded) inputs / outputs.
* Below format of the bytes expected as input and written as output:
* calldata: variable-length bytes:
* [methodID (bytes4)]
* [targetOvmContractAddress (address as bytes32 (left-padded, big-endian))]
* [callBytes (bytes (variable length))]
* returndata: [variable-length bytes returned from call]
*/
function ovmCALL() public {
uint callSize;
bytes memory _callBytes;
bytes32 _targetOvmContractAddressBytes;
// parse calldata
assembly {
// skip 4 bytes for methodID and first 12 bytes of address
_targetOvmContractAddressBytes := calldataload(16)
// size is calldata - methodID - address (as bytes32)
callSize := sub(calldatasize, 0x24)
// set callBytes
_callBytes := mload(0x40)
mstore(0x40, add(_callBytes, callSize))
calldatacopy(_callBytes, 0x24, callSize)
}
address _targetOvmContractAddress = address(bytes20(_targetOvmContractAddressBytes));
// switch the context to the _targetOvmContractAddress
(address oldMsgSender, address oldActiveContract) = switchActiveContract(_targetOvmContractAddress);
address codeAddress = stateManager.getCodeContractAddress(_targetOvmContractAddress);
bytes memory returnData;
uint returnSize;
// make the call
assembly {
let success := call(
gas,
codeAddress,
0,
_callBytes,
callSize,
0,
0
)
returnData := mload(0x40)
returndatacopy(returnData, 0, returndatasize)
if eq(success, 0) {
revert(returnData, returndatasize)
}
returnSize := returndatasize
mstore(0x40, add(returnData, returnSize))
}
// Revert back to our old execution context
restoreContractContext(oldMsgSender, oldActiveContract);
// Return the return value
assembly {
return(returnData, returnSize)
}
}
/**
* @notice STATICCALL opcode -- calls the code in question without allowing state modification.
* Note: This is a raw function, so there are no listed (ABI-encoded) inputs / outputs.
* Below format of the bytes expected as input and written as output:
* calldata: variable-length bytes:
* [methodID (bytes4)]
* [targetOvmContractAddress (address as bytes32 (big-endian))]
* [callBytes (bytes (variable length))]
* returndata: [variable-length bytes returned from call]
*/
function ovmSTATICCALL() public {
uint callSize;
bytes memory _callBytes;
bytes32 _targetOvmContractAddressBytes;
// parse calldata
assembly {
// skip 4 bytes for methodID and first 12 bytes of address
_targetOvmContractAddressBytes := calldataload(16)
// size is calldata - methodID - address (as bytes32)
callSize := sub(calldatasize, 0x24)
// set callBytes
_callBytes := mload(0x40)
mstore(0x40, add(_callBytes, callSize))
calldatacopy(_callBytes, 0x24, callSize)
}
bool wasStaticContext = executionContext.inStaticContext;
executionContext.inStaticContext = true;
address _targetOvmContractAddress = address(bytes20(_targetOvmContractAddressBytes));
// switch the context to the _targetOvmContractAddress
(address oldMsgSender, address oldActiveContract) = switchActiveContract(_targetOvmContractAddress);
address codeAddress = stateManager.getCodeContractAddress(_targetOvmContractAddress);
bytes memory returnData;
uint returnSize;
// make the call
assembly {
let success := call(
gas,
codeAddress,
0,
_callBytes,
callSize,
0,
0
)
returnData := mload(0x40)
returndatacopy(returnData, 0, returndatasize)
returnSize := returndatasize
if eq(success, 0) {
revert(returnData, returndatasize)
}
}
// Revert back to our old execution context
restoreContractContext(oldMsgSender, oldActiveContract);
// This covers the nested STATICCALL case
executionContext.inStaticContext = wasStaticContext;
// Return the return value
assembly {
return(returnData, returnSize)
}
}
/**
* @notice DELEGATECALL opcode -- calls the code in question without changing the OVM contract context.
* Note: This is a raw function, so there are no listed (ABI-encoded) inputs / outputs.
* Below format of the bytes expected as input and written as output:
* calldata: variable-length bytes:
* [methodID (bytes4)]
* [targetOvmContractAddress (address as bytes32 (big-endian))]
* [callBytes (bytes (variable length))]
* returndata: [variable-length bytes returned from call]
*/
function ovmDELEGATECALL() public {
uint callSize;
bytes memory _callBytes;
bytes32 _targetOvmContractAddressBytes;
// parse calldata
assembly {
// skip 4 bytes for methodID and first 12 bytes of address
_targetOvmContractAddressBytes := calldataload(16)
// size is calldata - methodID - address (as bytes32)
callSize := sub(calldatasize, 0x24)
// set callBytes
_callBytes := mload(0x40)
mstore(0x40, add(_callBytes, callSize))
calldatacopy(_callBytes, 0x24, callSize)
}
address _targetOvmContractAddress = address(bytes20(_targetOvmContractAddressBytes));
// NOTE: WE DO NOT SWITCH CONTEXTS HERE.
address codeAddress = stateManager.getCodeContractAddress(_targetOvmContractAddress);
// make the call
assembly {
let success := call(
gas,
codeAddress,
0,
_callBytes,
callSize,
0,
0
)
let returnData := mload(0x40)
returndatacopy(returnData, 0, returndatasize)
if eq(success, 0) {
revert(returnData, returndatasize)
}
return(returnData, returndatasize)
}
}
/***************************
* Contract Storage Opcodes *
***************************/
/**
* @notice Load a value from storage. Note each contract has it's own storage.
* Note: This is a raw function, so there are no listed (ABI-encoded) inputs / outputs.
* Below format of the bytes expected as input and written as output:
* calldata: 36 bytes:
* [methodID (bytes4)]
* [storageSlot (bytes32)]
* returndata: [storageValue (bytes32)]
*/
function ovmSLOAD() public view {
bytes32 _storageSlot;
assembly {
// skip methodID (4 bytes)
_storageSlot := calldataload(4)
}
bytes32 slotValue = stateManager.getStorage(executionContext.ovmActiveContract, _storageSlot);
assembly {
let ret := mload(0x40)
mstore(ret, slotValue)
return(ret, 0x20)
}
}
/**
* @notice Store a value. Note each contract has it's own storage.
* Note: This is a raw function, so there are no listed (ABI-encoded) inputs / outputs.
* Below format of the bytes expected as input and written as output:
* calldata: 68 bytes:
* [methodID (bytes4)]
* [storageSlot (bytes32)]
* [storageValue (bytes32)]
* returndata: empty.
*/
function ovmSSTORE() public {
require(!executionContext.inStaticContext, "Cannot call SSTORE from within a STATICCALL.");
bytes32 _storageSlot;
bytes32 _storageValue;
assembly {
// skip methodID (4 bytes)
_storageSlot := calldataload(4)
_storageValue := calldataload(0x24)
}
stateManager.setStorage(executionContext.ovmActiveContract, _storageSlot, _storageValue);
// Emit SetStorage event!
emit SetStorage(executionContext.ovmActiveContract, _storageSlot, _storageValue);
}
/***********************
* Code-related Opcodes *
************************/
/**
* @notice Executes the extcodesize operation for the contract address provided.
* Note: This is a raw function, so there are no listed (ABI-encoded) inputs / outputs.
* Below format of the bytes expected as input and written as output:
* calldata: 36 bytes:
* [methodID (bytes4)]
* [targetOvmContractAddress (address as bytes32 (left-padded, big-endian))]
* returndata: 32 bytes: the big-endian codesize int.
*/
function ovmEXTCODESIZE() public view {
bytes32 _targetAddressBytes;
assembly {
// read calldata, ignoring methodID and first 12 bytes of address
_targetAddressBytes := calldataload(16)
}
address _targetOvmContractAddress = address(bytes20(_targetAddressBytes));
address codeContractAddress = stateManager.getCodeContractAddress(_targetOvmContractAddress);
assembly {
let sizeBytes := mload(0x40)
mstore(sizeBytes, extcodesize(codeContractAddress))
return(sizeBytes, 32)
}
}
/**
* @notice Executes the extcodehash operation for the contract address provided.
* Note: This is a raw function, so there are no listed (ABI-encoded) inputs / outputs.
* Below format of the bytes expected as input and written as output:
* calldata: 36 bytes:
* [methodID (bytes4)]
* [targetOvmContractAddress (address as bytes32 (left-padded, big-endian))]
* returndata: 32 bytes: the hash.
*/
function ovmEXTCODEHASH() public view {
bytes32 _targetAddressBytes;
assembly {
// read calldata, ignoring methodID and first 12 bytes of address
_targetAddressBytes := calldataload(16)
}
address _targetOvmContractAddress = address(bytes20(_targetAddressBytes));
address codeContractAddress = stateManager.getCodeContractAddress(_targetOvmContractAddress);
bytes32 hash = stateManager.getCodeContractHash(codeContractAddress);
assembly {
let hashBytes := mload(0x40)
mstore(hashBytes, hash)
return(hashBytes, 32)
}
}
/**
* @notice Executes the extcodecopy operation for the contract address, index, and length provided.
* Note: This is a raw function, so there are no listed (ABI-encoded) inputs / outputs.
* Below format of the bytes expected as input and written as output:
* calldata: 100 bytes:
* [methodID (bytes4)]
* [targetOvmContractAddress (address as bytes32 (left-padded, big-endian))]
* [index (uint (32)]
* [length (uint (32))]
* returndata: length (input param) bytes of contract at address, starting at index.
*/
function ovmEXTCODECOPY() public view {
bytes32 _targetAddressBytes;
uint _index;
uint _length;
assembly {
// read calldata, ignoring methodID
_targetAddressBytes := calldataload(16)
// skip 4 + 32
_index := calldataload(0x24)
// skip 4 + 32 + 32
_length := calldataload(0x44)
}
address _targetOvmContractAddress = address(bytes20(_targetAddressBytes));
address codeContractAddress = stateManager.getCodeContractAddress(_targetOvmContractAddress);
assembly {
let codeContractBytecode := mload(0x40)
// store code in memory
extcodecopy(codeContractAddress, codeContractBytecode, _index, _length)
// write code to returndata
return(codeContractBytecode, _length)
}
}
/********
* Utils *
********/
/**
* @notice Initialize a new context, setting the timestamp, queue origin, and gasLimit as well as zeroing out the
* msgSender of the previous context.
* NOTE: this zeroing may not technically be needed as the context should always end up as zero at the end of each execution.
* @param _timestamp The timestamp which should be used for this context.
* @param _queueOrigin The queue which this context's transaction was sent from.
* @param _ovmTxOrigin The tx.origin for the currently executing transaction. It will be ZERO_ADDRESS if it's not an EOA call.
*/
function initializeContext(uint _timestamp, uint _queueOrigin, address _ovmTxOrigin, address _l1MsgSender) internal {
// First zero out the context for good measure (Note ZERO_ADDRESS is reserved for the genesis contract & initial msgSender)
restoreContractContext(ZERO_ADDRESS, ZERO_ADDRESS);
// And finally set the timestamp, queue origin, tx origin, and l1MessageSender
executionContext.timestamp = _timestamp;
executionContext.queueOrigin = _queueOrigin;
executionContext.ovmTxOrigin = _ovmTxOrigin;
executionContext.l1MessageSender = _l1MsgSender;
}
/**
* @notice Change the active contract to be something new. This is used when a new contract is called.
* @param _newActiveContract The new active contract
* @return The old msgSender and activeContract. This will be used when we restore the old active contract.
*/
function switchActiveContract(address _newActiveContract) internal returns(address _oldMsgSender, address _oldActiveContract) {
// Store references to the old context
_oldActiveContract = executionContext.ovmActiveContract;
_oldMsgSender = executionContext.ovmMsgSender;
// Set our new context
executionContext.ovmActiveContract = _newActiveContract;
executionContext.ovmMsgSender = _oldActiveContract;
// Emit an event so we can track the active contract. This is used in order to parse transaction receipts in the fullnode
emit ActiveContract(_newActiveContract);
// Return old context so we can later revert to it
return (_oldMsgSender, _oldActiveContract);
}
/**
* @notice Restore the contract context to some old values.
* @param _msgSender The msgSender to be restored.
* @param _activeContract The activeContract to be restored.
*/
function restoreContractContext(address _msgSender, address _activeContract) internal {
// Revert back to the old context
executionContext.ovmActiveContract = _activeContract;
executionContext.ovmMsgSender = _msgSender;
}