forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheffects.fe
More file actions
998 lines (859 loc) · 29.7 KB
/
Copy patheffects.fe
File metadata and controls
998 lines (859 loc) · 29.7 KB
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
use core::effect_ref::{EffectHandle, EffectHandleMut, EffectRef, EffectRefMut}
use core::{size_of}
use core::abi::{
Abi,
AbiDecoder,
AbiEncoder,
AbiSize,
AbiSpan,
ByteInput,
Decode,
Encode,
EventAbiEncode,
encode_alloc,
encode_single_root_alloc,
}
use core::contracts::ContractHost
use core::error::ErrorVariant
use core::message::MsgVariant
use ingot::evm::intrinsic::{
code_region_len as intrinsic_code_region_len,
code_region_offset as intrinsic_code_region_offset,
}
use ingot::abi::Sol
use ingot::abi::sol::{decode_input_at, decode_output}
use super::calldata::CallData
use super::contract_field_slot
use super::panic::Panic
use ingot::evm::mem
use ingot::evm::memory_input::MemoryBytes
use ingot::evm::ops
// -----------------------------------------------------------------------------
// Value types
// -----------------------------------------------------------------------------
/// EVM address (20 bytes).
///
/// Represented as a single `u256` ABI word with the address stored in the low
/// 160 bits.
pub struct Address { pub inner: u256 }
impl Address {
pub fn zero() -> Self {
Address { inner: 0 }
}
}
impl core::ops::Eq for Address {
const fn eq(self, _ other: Address) -> bool {
self.inner == other.inner
}
}
impl Copy for Address {}
impl Default for Address {
fn default() -> Self {
Address::zero()
}
}
impl AbiSize for Address {
const HEAD_SIZE: u256 = 32
const IS_DYNAMIC: bool = false
}
impl<A: Abi> Decode<A> for Address {
#[inline(always)]
fn decode_payload<D: AbiDecoder<A>>(_ d: mut D) -> Self {
let word = d.read_word()
if word >> 160 != 0 {
ops::revert(offset: 0, len: 0)
}
Address { inner: word }
}
#[inline(always)]
fn decode_from<I: ByteInput>(_ input: I, _ pos: u256) -> Self {
let word = input.word_at(pos)
if word >> 160 != 0 {
ops::revert(offset: 0, len: 0)
}
Address { inner: word }
}
}
impl<A: Abi> Encode<A> for Address {
const DIRECT_ENCODE: bool = true
fn encode<E: AbiEncoder<A>>(own self, _ e: mut E) {
e.write_word(self.inner)
}
fn encode_to_ptr(own self, _ ptr: u256) {
A::store_word(ptr: ptr, value: self.inner)
}
}
impl<A: Abi> EventAbiEncode<A> for Address {
const DIRECT_EVENT_ENCODE: bool = true
}
impl<A: Abi> AbiSpan<A> for Address {
fn payload_end<I: core::abi::ByteInput>(_: I, base: u256, pos: u256) -> u256 {
pos + 32
}
}
/// Convert an EVM `Address` into its ABI word representation (`u256`).
pub fn address_to_word(_ addr: Address) -> u256 {
addr.inner
}
// -----------------------------------------------------------------------------
// Effect pointer providers
// -----------------------------------------------------------------------------
/// Memory pointer to a value of type `T`.
pub use core::effect_ref::MemPtr
/// Storage pointer/handle to a value of type `T`.
pub use core::effect_ref::StorPtr
/// Calldata pointer to a value of type `T` (read-only).
pub struct CalldataPtr<T> { offset: u256 }
impl<T> Copy for CalldataPtr<T> {}
impl<T> EffectHandle for CalldataPtr<T> {
type Target = T
const SPACE: core::effect_ref::AddressSpace = core::effect_ref::AddressSpace::Calldata
fn from_raw(_ raw: u256) -> Self {
Self { offset: raw }
}
fn raw(self) -> u256 {
self.offset
}
}
impl<T> EffectRef<T> for CalldataPtr<T> {}
// no EffectRefMut for calldata
/// Transient storage pointer/handle to a value of type `T`.
///
/// This is like `StorPtr<T>`, but reads/writes are intended to compile to
/// EVM transient storage operations (TLOAD/TSTORE).
pub struct TStorPtr<T> { slot: u256 }
impl<T> Copy for TStorPtr<T> {}
impl<T> EffectHandle for TStorPtr<T> {
type Target = T
const SPACE: core::effect_ref::AddressSpace = core::effect_ref::AddressSpace::TransientStorage
fn from_raw(_ raw: u256) -> Self {
Self { slot: raw }
}
fn raw(self) -> u256 {
self.slot
}
}
impl<T> EffectHandleMut for TStorPtr<T> {}
impl<T> EffectRef<T> for TStorPtr<T> {}
impl<T> EffectRefMut<T> for TStorPtr<T> {}
impl<T> TStorPtr<T> {
/// Reads the pointed-at transient value.
pub fn read(self) -> T
where T: Copy
{
let mut ptr = self
with (ptr) {
core::effect_ref::read(ptr)
}
}
/// Writes the pointed-at transient value.
pub fn write(self, _ value: T) {
let mut ptr = self
with (ptr) {
core::effect_ref::write(ptr, value)
}
}
}
/// Zero-sized pointer to a statically assigned transient storage slot.
///
/// `SLOT` defaults to a layout hole: when a type containing a `TSlot`
/// reaches contract storage, the layout assigns the slot from the
/// contract's transient slot counter — shared with `TStorPtr` provider
/// fields, so static and runtime transient slots never collide.
struct TSlotSeal {}
pub struct TSlot<T, const SLOT: u256 = _> {
seal: TSlotSeal,
}
impl<T, const SLOT: u256> Copy for TSlot<T, SLOT> {}
impl<T, const SLOT: u256> core::effect_ref::StaticSlot for TSlot<T, SLOT> {
const SPACE: core::effect_ref::AddressSpace = core::effect_ref::AddressSpace::TransientStorage
}
impl<T, const SLOT: u256> TSlot<T, SLOT> {
pub(ingot) fn ptr_unchecked(self) -> TStorPtr<T> {
TStorPtr::from_raw(SLOT)
}
/// Runtime pointer to the assigned slot.
pub fn ptr(mut self) -> TStorPtr<T> {
self.ptr_unchecked()
}
/// Reads the value at the assigned slot.
pub fn read(self) -> T
where T: Copy
{
self.ptr_unchecked().read()
}
/// Writes the value at the assigned slot.
pub fn write(mut self, _ value: T) {
self.ptr_unchecked().write(value)
}
}
// -----------------------------------------------------------------------------
// EVM effects
// -----------------------------------------------------------------------------
trait EvmCapabilitySeal {}
/// EVM execution context (env access).
pub trait Ctx: EvmCapabilitySeal {
fn address(self) -> Address
fn caller(self) -> Address
fn origin(self) -> Address
fn coinbase(self) -> Address
fn balance(self, _ addr: Address) -> u256
fn value(self) -> u256
fn gasprice(self) -> u256
fn timestamp(self) -> u256
fn block_number(self) -> u256
fn prevrandao(self) -> u256
fn gaslimit(self) -> u256
fn chainid(self) -> u256
fn basefee(self) -> u256
fn selfbalance(self) -> u256
fn blockhash(self, _: u256) -> u256
fn blobhash(self, _: u256) -> u256
fn blobbasefee(self) -> u256
fn extcodesize(self, _ addr: Address) -> u256
fn extcodecopy(mut self, _ addr: Address, dest: u256, offset: u256, len: u256)
fn extcodehash(self, _ addr: Address) -> u256
fn gas(self) -> u256
}
/// Low-level raw memory operations.
pub trait RawMem: EvmCapabilitySeal {
fn mload(self, _: u256) -> u256
fn mstore(mut self, addr: u256, value: u256)
fn mstore8(mut self, addr: u256, value: u8)
fn mem_ptr<T>(self, _ addr: u256) -> MemPtr<T> {
MemPtr::from_raw(addr)
}
}
/// Low-level raw storage operations.
pub trait RawStorage: EvmCapabilitySeal {
fn sload(self, _: u256) -> u256
fn sstore(mut self, slot: u256, value: u256)
fn stor_ptr<T>(self, _ slot: u256) -> StorPtr<T> {
StorPtr::from_raw(slot)
}
}
/// Access to low-level EVM op functions (excluding raw stack ops).
pub trait RawOps: RawMem + RawStorage {
fn calldataload(self, _: u256) -> u256
fn calldatasize(self) -> u256
fn calldatacopy(mut self, dest: u256, offset: u256, len: u256)
fn returndatasize(self) -> u256
fn returndatacopy(mut self, dest: u256, offset: u256, len: u256)
fn codecopy(mut self, dest: u256, offset: u256, len: u256)
fn codesize(self) -> u256
fn keccak256(self, offset: u256, len: u256) -> u256
fn revert(self, offset: u256, len: u256) -> !
fn return_data(self, offset: u256, len: u256) -> !
fn code_region_offset<F>(self, _: F) -> u256
fn code_region_len<F>(self, _: F) -> u256
}
pub trait Log: EvmCapabilitySeal {
fn log0(mut self, offset: u256, len: u256)
fn log1(mut self, offset: u256, len: u256, topic0: u256)
fn log2(mut self, offset: u256, len: u256, topic0: u256, topic1: u256)
fn log3(mut self, offset: u256, len: u256, topic0: u256, topic1: u256, topic2: u256)
fn log4(
mut self,
offset: u256,
len: u256,
topic0: u256,
topic1: u256,
topic2: u256,
topic3: u256,
)
/// Emit a Solidity-compatible event log (`log1`-`log4`).
///
/// This delegates to the `std::evm::Event` implementation generated for
/// `#[event]` structs.
fn emit<E>(mut self, _ event: own E)
where E: super::event::Event
{
event.emit(self)
}
}
/// Metadata for deployable high-level contracts.
///
/// The compiler synthesizes an implementation of this trait for each high-level
/// `contract` item, enabling typed `CREATE`/`CREATE2` from within other
/// contracts.
pub trait Contract {
/// Constructor argument tuple, ABI-encodable under the default ABI.
type InitArgs: Encode<Sol>
/// Offset of this contract's initcode within the current artifact.
fn init_code_offset() -> u256
/// Length (in bytes) of this contract's initcode within the current artifact.
fn init_code_len() -> u256
}
pub trait Create: EvmCapabilitySeal {
/// Low-level `CREATE` opcode.
fn create_raw(mut self, value: u256, offset: u256, len: u256) -> Address {
Address { inner: ops::create(value: value, offset: offset, len: len) }
}
/// Low-level `CREATE2` opcode.
fn create2_raw(mut self, value: u256, offset: u256, len: u256, salt: u256) -> Address {
Address { inner: ops::create2(value: value, offset: offset, len: len, salt: salt) }
}
/// Deploy a high-level contract using `CREATE`, bubbling revert data on failure.
fn create<C: Contract>(mut self, value: u256, args: own C::InitArgs) -> Address {
let init_len = C::init_code_len()
let init_off = C::init_code_offset()
let mut addr: Address
if !C::InitArgs::IS_DYNAMIC && C::InitArgs::HEAD_SIZE == 0 {
let init_ptr = mem::alloc(init_len)
ops::codecopy(dest: init_ptr, offset: init_off, len: init_len)
addr = self.create_raw(value: value, offset: init_ptr, len: init_len)
} else {
let encoded = encode_abi_payload<C::InitArgs>(args)
let init_ptr = mem::alloc(init_len + encoded.1)
ops::codecopy(dest: init_ptr, offset: init_off, len: init_len)
copy_words(dest: init_ptr + init_len, src: encoded.0, len: encoded.1)
let total_len = init_len + encoded.1
addr = self.create_raw(value: value, offset: init_ptr, len: total_len)
}
// Solidity-style behavior: bubble revert data if creation fails.
if addr.inner == 0 {
let ret_len = ops::returndatasize()
let ret_ptr = mem::alloc(ret_len)
ops::returndatacopy(dest: ret_ptr, offset: 0, len: ret_len)
ops::revert(offset: ret_ptr, len: ret_len)
}
addr
}
/// Deploy a high-level contract using `CREATE2`, bubbling revert data on failure.
fn create2<C: Contract>(mut self, value: u256, args: own C::InitArgs, salt: u256) -> Address {
let init_len = C::init_code_len()
let init_off = C::init_code_offset()
let mut addr: Address
if !C::InitArgs::IS_DYNAMIC && C::InitArgs::HEAD_SIZE == 0 {
let init_ptr = mem::alloc(init_len)
ops::codecopy(dest: init_ptr, offset: init_off, len: init_len)
addr = self.create2_raw(value: value, offset: init_ptr, len: init_len, salt: salt)
} else {
let encoded = encode_abi_payload<C::InitArgs>(args)
let init_ptr = mem::alloc(init_len + encoded.1)
ops::codecopy(dest: init_ptr, offset: init_off, len: init_len)
copy_words(dest: init_ptr + init_len, src: encoded.0, len: encoded.1)
let total_len = init_len + encoded.1
addr = self.create2_raw(value: value, offset: init_ptr, len: total_len, salt: salt)
}
// Solidity-style behavior: bubble revert data if creation fails.
if addr.inner == 0 {
let ret_len = ops::returndatasize()
let ret_ptr = mem::alloc(ret_len)
ops::returndatacopy(dest: ret_ptr, offset: 0, len: ret_len)
ops::revert(offset: ret_ptr, len: ret_len)
}
addr
}
}
fn copy_words(dest: u256, src: u256, len: u256) {
let mut offset: u256 = 0
while offset < len {
ops::mstore(addr: dest + offset, value: ops::mload(src + offset))
offset += 32
}
}
pub fn encode_abi_payload<T>(_ value: own T) -> (u256, u256)
where T: Encode<Sol> + AbiSize
{
encode_alloc<Sol, T>(value)
}
#[inline(always)]
pub fn encode_event_payload<T>(_ value: own T) -> (u256, u256)
where T: EventAbiEncode<Sol>
{
if T::DIRECT_EVENT_ENCODE {
let total = value.event_payload_size()
let base = mem::alloc(total)
value.encode_event_to_ptr(base)
return (base, total)
}
encode_alloc<Sol, T>(value)
}
/// High-level contract calls with ABI encoding/decoding.
pub trait Call: EvmCapabilitySeal {
/// Low-level `CALL` opcode.
///
/// Returns `true` on success and `false` on failure. Any returndata is left
/// available for inspection via `last_returndata()`.
fn raw_call(
mut self,
addr: own Address,
gas: u256,
value: u256,
args_offset: u256,
args_len: u256,
) -> bool {
ops::call(
gas: gas,
addr: addr.inner,
value: value,
args_offset: args_offset,
args_len: args_len,
ret_offset: 0,
ret_len: 0,
) != 0
}
/// Low-level `STATICCALL` opcode with explicit input and output buffers.
///
/// Returns `true` on success and `false` on failure. Any returndata beyond
/// the requested output buffer remains available via `last_returndata()`.
fn raw_staticcall(
mut self,
addr: own Address,
gas: u256,
args_offset: u256,
args_len: u256,
ret_offset: u256,
ret_len: u256,
) -> bool {
ops::staticcall(
gas: gas,
addr: addr.inner,
args_offset: args_offset,
args_len: args_len,
ret_offset: ret_offset,
ret_len: ret_len,
) != 0
}
fn call<M>(mut self, addr: own Address, gas: u256, value: u256, message: own M) -> M::Return
where M: MsgVariant<Sol> + Encode<Sol>, M::Return: Decode<Sol> + AbiSize
/// Like `call<M>`, but if the call succeeds with EMPTY returndata, returns
/// `default` instead of reverting in the decoder. Reverts are still bubbled,
/// and non-empty returndata is still decoded strictly.
///
/// Caveat (same as Vyper's `default_return_value`): a call to an address
/// with no code succeeds with empty returndata at the EVM level, so it
/// yields `default`, a silent no-op "success". For ERC20 interactions use
/// `std::evm::erc20`, which closes this hole with a code-existence check.
fn call_with_default<M>(
mut self,
addr: own Address,
gas: u256,
value: u256,
message: own M,
default: own M::Return,
) -> M::Return
where M: MsgVariant<Sol> + Encode<Sol>, M::Return: Decode<Sol> + AbiSize
fn static<M>(mut self, addr: own Address, gas: u256, message: own M) -> M::Return
where M: MsgVariant<Sol> + Encode<Sol>, M::Return: Decode<Sol> + AbiSize
fn delegate<M>(mut self, addr: own Address, gas: u256, message: own M) -> M::Return
where M: MsgVariant<Sol> + Encode<Sol>, M::Return: Decode<Sol> + AbiSize
}
impl Address {
/// Call a contract at this address with a typed message.
///
/// Forwards all available gas and sends no value. Reverts on failure.
pub fn call<M>(self, _ message: own M) -> M::Return
uses (call: mut Call) where M: MsgVariant<Sol> + Encode<Sol>, M::Return: Decode<Sol> + AbiSize
{
call.call(addr: self, gas: ops::gas(), value: 0, message)
}
/// `STATICCALL` a contract at this address with a typed message.
///
/// Forwards all available gas. Bubbles the callee's revert data on failure.
pub fn static<M>(self, _ message: own M) -> M::Return
uses (call: mut Call) where M: MsgVariant<Sol> + Encode<Sol>, M::Return: Decode<Sol> + AbiSize
{
call.static(addr: self, gas: ops::gas(), message)
}
/// Call a contract at this address with a typed message, returning
/// `default` if the call succeeds with empty returndata.
///
/// Forwards all available gas and sends no value. Reverts are bubbled and
/// non-empty returndata is decoded strictly; see `Call::call_with_default`
/// for the no-code-at-address caveat.
pub fn call_with_default<M>(self, _ message: own M, default: own M::Return) -> M::Return
uses (call: mut Call) where M: MsgVariant<Sol> + Encode<Sol>, M::Return: Decode<Sol> + AbiSize
{
call.call_with_default(addr: self, gas: ops::gas(), value: 0, message, default)
}
}
/// Combined EVM capability: provides all EVM effects.
pub trait Super: Ctx + RawOps + Log + Create + Call {}
struct Private {}
/// Default EVM effect provider (zero-sized).
pub struct Evm { seal: Private }
impl EvmCapabilitySeal for Evm {}
impl Evm {
/// Return the current Solidity selector from raw calldata.
pub fn selector(self) -> u32 {
self.runtime_selector<Sol>()
}
/// Return a calldata view over the current Solidity call arguments.
pub fn args(self) -> CallData {
CallData::with_base(size_of<Sol::Selector>())
}
/// Decode the current Solidity call arguments, skipping the 4-byte selector.
pub fn decode_args<T>(self) -> T
where T: Decode<Sol> + AbiSize
{
decode_input_at(self.input(), size_of<Sol::Selector>())
}
}
impl Ctx for Evm {
fn address(self) -> Address {
Address { inner: ops::address() }
}
fn caller(self) -> Address {
Address { inner: ops::caller() }
}
fn origin(self) -> Address {
Address { inner: ops::origin() }
}
fn coinbase(self) -> Address {
Address { inner: ops::coinbase() }
}
fn balance(self, _ addr: Address) -> u256 {
ops::balance(addr.inner)
}
#[inline(always)]
fn value(self) -> u256 {
ops::callvalue()
}
fn gasprice(self) -> u256 {
ops::gasprice()
}
fn timestamp(self) -> u256 {
ops::timestamp()
}
fn block_number(self) -> u256 {
ops::number()
}
fn prevrandao(self) -> u256 {
ops::prevrandao()
}
fn gaslimit(self) -> u256 {
ops::gaslimit()
}
fn chainid(self) -> u256 {
ops::chainid()
}
fn basefee(self) -> u256 {
ops::basefee()
}
fn selfbalance(self) -> u256 {
ops::selfbalance()
}
fn blockhash(self, _ block: u256) -> u256 {
ops::blockhash(block)
}
fn blobhash(self, _ index: u256) -> u256 {
ops::blobhash(index)
}
fn blobbasefee(self) -> u256 {
ops::blobbasefee()
}
fn extcodesize(self, _ addr: Address) -> u256 {
ops::extcodesize(addr.inner)
}
fn extcodecopy(mut self, _ addr: Address, dest: u256, offset: u256, len: u256) {
ops::extcodecopy(addr: addr.inner, dest: dest, offset: offset, len: len)
}
fn extcodehash(self, _ addr: Address) -> u256 {
ops::extcodehash(addr.inner)
}
fn gas(self) -> u256 {
ops::gas()
}
}
impl RawMem for Evm {
fn mload(self, _ addr: u256) -> u256 {
ops::mload(addr)
}
fn mstore(mut self, addr: u256, value: u256) {
ops::mstore(addr: addr, value: value)
}
fn mstore8(mut self, addr: u256, value: u8) {
ops::mstore8(addr, value)
}
}
impl RawStorage for Evm {
fn sload(self, _ slot: u256) -> u256 {
ops::sload(slot)
}
fn sstore(mut self, slot: u256, value: u256) {
ops::sstore(slot: slot, value: value)
}
}
impl RawOps for Evm {
fn calldataload(self, _ offset: u256) -> u256 {
ops::calldataload(offset)
}
fn calldatasize(self) -> u256 {
ops::calldatasize()
}
fn calldatacopy(mut self, dest: u256, offset: u256, len: u256) {
ops::calldatacopy(dest: dest, offset: offset, len: len)
}
fn returndatasize(self) -> u256 {
ops::returndatasize()
}
fn returndatacopy(mut self, dest: u256, offset: u256, len: u256) {
ops::returndatacopy(dest: dest, offset: offset, len: len)
}
fn codecopy(mut self, dest: u256, offset: u256, len: u256) {
ops::codecopy(dest: dest, offset: offset, len: len)
}
fn codesize(self) -> u256 {
ops::codesize()
}
fn keccak256(self, offset: u256, len: u256) -> u256 {
ops::keccak256(offset, len)
}
fn revert(self, offset: u256, len: u256) -> ! {
ops::revert(offset: offset, len: len)
}
fn return_data(self, offset: u256, len: u256) -> ! {
ops::return_data(offset: offset, len: len)
}
fn code_region_offset<F>(self, _ f: F) -> u256 {
intrinsic_code_region_offset(f)
}
fn code_region_len<F>(self, _ f: F) -> u256 {
intrinsic_code_region_len(f)
}
}
impl Log for Evm {
// Logging opcodes.
fn log0(mut self, offset: u256, len: u256) {
ops::log0(offset: offset, len: len)
}
fn log1(mut self, offset: u256, len: u256, topic0: u256) {
ops::log1(offset: offset, len: len, topic0: topic0)
}
fn log2(mut self, offset: u256, len: u256, topic0: u256, topic1: u256) {
ops::log2(offset: offset, len: len, topic0: topic0, topic1: topic1)
}
fn log3(mut self, offset: u256, len: u256, topic0: u256, topic1: u256, topic2: u256) {
ops::log3(
offset: offset,
len: len,
topic0: topic0,
topic1: topic1,
topic2: topic2,
)
}
fn log4(
mut self,
offset: u256,
len: u256,
topic0: u256,
topic1: u256,
topic2: u256,
topic3: u256,
) {
ops::log4(
offset: offset,
len: len,
topic0: topic0,
topic1: topic1,
topic2: topic2,
topic3: topic3,
)
}
}
impl Create for Evm {
fn create_raw(mut self, value: u256, offset: u256, len: u256) -> Address {
Address { inner: ops::create(value: value, offset: offset, len: len) }
}
fn create2_raw(mut self, value: u256, offset: u256, len: u256, salt: u256) -> Address {
Address { inner: ops::create2(value: value, offset: offset, len: len, salt: salt) }
}
}
#[arithmetic(unchecked)]
#[inline(always)]
fn encode_calldata<M>(_ selector: u32, _ message: own M) -> (u256, u256)
where M: Encode<Sol>
{
let selector_size = size_of<Sol::Selector>()
let payload_len = message.payload_size()
let total_len = selector_size + payload_len
let ptr = mem::alloc(total_len)
let selector_word = (selector as u256) << 224
ops::mstore(addr: ptr, value: selector_word)
let payload_ptr = ptr + selector_size
let mut encoder = Sol::encoder_at(payload_ptr)
message.encode(mut encoder)
(ptr, total_len)
}
/// ABI-encode a message (4-byte selector + payload) into freshly allocated
/// memory without performing a call. Returns `(offset, length)` suitable for
/// `Call::raw_call` / `Call::raw_staticcall`.
pub fn encode_msg_calldata<M>(_ message: own M) -> (u256, u256)
where M: MsgVariant<Sol> + Encode<Sol>
{
encode_calldata(<M as MsgVariant<Sol>>::SELECTOR, message)
}
fn copy_returndata() -> MemoryBytes {
let ret_len = ops::returndatasize()
let ret_ptr = mem::alloc(ret_len)
ops::returndatacopy(dest: ret_ptr, offset: 0, len: ret_len)
MemoryBytes::new(base: ret_ptr, len: ret_len)
}
/// Copy the current returndata into freshly allocated memory.
pub fn last_returndata() -> MemoryBytes {
copy_returndata()
}
/// Revert, bubbling the current returndata payload unchanged.
pub fn bubble_last_revert() -> ! {
let ret = copy_returndata()
ops::revert(offset: ret.base, len: ret.len)
}
/// Decode Solidity ABI data from an in-memory returndata buffer.
pub fn decode_returndata<R>(_ ret: MemoryBytes) -> R
where R: Decode<Sol> + AbiSize
{
decode_output(ret)
}
/// Perform a raw `STATICCALL` and ABI-decode the returndata on success.
///
/// This bubbles revert data on call failure and reuses the regular Solidity
/// decoder, so short or non-canonical returndata also reverts.
pub fn staticcall_decode<R>(addr: Address, gas: u256, args_offset: u256, args_len: u256) -> R
where R: Decode<Sol> + AbiSize
{
let ok = ops::staticcall(
gas: gas,
addr: addr.inner,
args_offset: args_offset,
args_len: args_len,
ret_offset: 0,
ret_len: 0,
)
if ok == 0 {
bubble_last_revert()
}
decode_returndata(last_returndata())
}
impl Call for Evm {
fn call<M>(mut self, addr: own Address, gas: u256, value: u256, message: own M) -> M::Return
where M: MsgVariant<Sol> + Encode<Sol>, M::Return: Decode<Sol> + AbiSize
{
let out: (u256, u256) = encode_calldata(<M as MsgVariant<Sol>>::SELECTOR, message)
if !self.raw_call(addr, gas, value, args_offset: out.0, args_len: out.1) {
bubble_last_revert()
}
decode_returndata(last_returndata())
}
fn call_with_default<M>(
mut self,
addr: own Address,
gas: u256,
value: u256,
message: own M,
default: own M::Return,
) -> M::Return
where M: MsgVariant<Sol> + Encode<Sol>, M::Return: Decode<Sol> + AbiSize
{
let out: (u256, u256) = encode_calldata(<M as MsgVariant<Sol>>::SELECTOR, message)
if !self.raw_call(addr, gas, value, args_offset: out.0, args_len: out.1) {
bubble_last_revert()
}
if ops::returndatasize() == 0 {
return default
}
decode_returndata(last_returndata())
}
fn static<M>(mut self, addr: own Address, gas: u256, message: own M) -> M::Return
where M: MsgVariant<Sol> + Encode<Sol>, M::Return: Decode<Sol> + AbiSize
{
let out: (u256, u256) = encode_calldata(<M as MsgVariant<Sol>>::SELECTOR, message)
staticcall_decode(addr: addr, gas: gas, args_offset: out.0, args_len: out.1)
}
fn delegate<M>(mut self, addr: own Address, gas: u256, message: own M) -> M::Return
where M: MsgVariant<Sol> + Encode<Sol>, M::Return: Decode<Sol> + AbiSize
{
let out: (u256, u256) = encode_calldata(<M as MsgVariant<Sol>>::SELECTOR, message)
let ok = ops::delegatecall(
gas: gas,
addr: addr.inner,
args_offset: out.0,
args_len: out.1,
ret_offset: 0,
ret_len: 0,
)
if ok == 0 {
bubble_last_revert()
}
decode_returndata(last_returndata())
}
}
#[inline(always)]
pub fn assert(_ b: own bool) {
assert!(b)
}
/// Revert the current transaction with ABI-encoded data.
///
/// Encodes the given value using Solidity ABI encoding and reverts
/// with the encoded bytes as revert data.
pub fn revert<T>(_ value: own T) -> !
where T: Encode<Sol> + AbiSize
{
let (result_ptr, result_len) = encode_single_root_alloc<Sol, T>(value)
ops::revert(offset: result_ptr, len: result_len)
}
/// Revert with a Solidity-compatible custom error.
///
/// Encodes the error with its 4-byte selector prefix followed by
/// ABI-encoded fields, matching Solidity's custom error encoding.
pub fn revert_error<T>(_ error: own T) -> !
where T: ErrorVariant<Sol> + Encode<Sol>
{
let (ptr, len) = encode_calldata(<T as ErrorVariant<Sol>>::SELECTOR, error)
ops::revert(offset: ptr, len: len)
}
impl Super for Evm {}
impl ContractHost for Evm {
type Input = CallData
type InitInput = MemoryBytes
fn input(self) -> Self::Input {
CallData::new()
}
fn init_input<F>(mut self, _ runtime: F) -> Self::InitInput {
let offset = self.code_region_offset(runtime)
let len = self.code_region_len(runtime)
let args_offset = offset + len
let code_size = self.codesize()
if code_size < args_offset {
self.abort()
}
let args_len = code_size - args_offset
let args_ptr = mem::alloc(args_len)
self.codecopy(dest: args_ptr, offset: args_offset, len: args_len)
MemoryBytes::new(base: args_ptr, len: args_len)
}
fn field<T>(mut self, _ slot: u256) -> core::effect_ref::StorPtr<T> {
self.stor_ptr(slot)
}
fn create_contract<F>(mut self, _ runtime: F) -> ! {
let len = self.code_region_len(runtime)
let offset = self.code_region_offset(runtime)
let ptr = mem::alloc(len)
self.codecopy(dest: ptr, offset: offset, len: len)
self.return_data(offset: ptr, len: len)
}
fn abort(self) -> ! {
self.revert(offset: 0, len: 0)
}
fn return_bytes(self, ptr: u256, len: u256) -> ! {
self.return_data(offset: ptr, len: len)
}
fn return_value<A: Abi, T>(mut self, _ value: own T) -> !
where T: Encode<A> + AbiSize
{
if !T::IS_DYNAMIC && T::DIRECT_ENCODE {
if T::HEAD_SIZE <= 64 {
value.encode_to_ptr(0)
self.return_data(offset: 0, len: T::HEAD_SIZE)
} else {
let ptr = mem::alloc(T::HEAD_SIZE)
value.encode_to_ptr(ptr)
self.return_data(offset: ptr, len: T::HEAD_SIZE)
}
} else {
let (out_ptr, out_len) = encode_single_root_alloc<A, T>(value)
self.return_bytes(ptr: out_ptr, len: out_len)
}
}
}