2
2
3
3
use crate :: {
4
4
handler:: {
5
- mainnet:: { self , deduct_caller_inner} ,
5
+ mainnet:: { self , deduct_caller , deduct_caller_inner, end , last_frame_return , output } ,
6
6
register:: EvmHandler ,
7
7
} ,
8
8
interpreter:: { return_ok, return_revert, Gas , InstructionResult } ,
@@ -12,3 +12,88 @@ use crate::{
12
12
} ,
13
13
Context ,
14
14
} ;
15
+ use alloc:: sync:: Arc ;
16
+ use core:: ops:: Mul ;
17
+ use SpecId :: LONDON ;
18
+
19
+ pub fn taiko_handle_register < DB : Database , EXT > ( handler : & mut EvmHandler < ' _ , EXT , DB > ) {
20
+ spec_to_generic ! ( handler. spec_id, {
21
+ handler. post_execution. reimburse_caller = Arc :: new( reimburse_caller:: <SPEC , EXT , DB >) ;
22
+ handler. post_execution. reward_beneficiary = Arc :: new( reward_beneficiary:: <SPEC , EXT , DB >) ;
23
+ } ) ;
24
+ }
25
+
26
+ #[ inline]
27
+ pub fn reimburse_caller < SPEC : Spec , EXT , DB : Database > (
28
+ context : & mut Context < EXT , DB > ,
29
+ gas : & Gas ,
30
+ ) -> Result < ( ) , EVMError < DB :: Error > > {
31
+ if context. evm . env . tx . taiko . is_anchor {
32
+ return Ok ( ( ) ) ;
33
+ }
34
+ let caller = context. evm . env . tx . caller ;
35
+ let effective_gas_price = context. evm . env . effective_gas_price ( ) ;
36
+
37
+ // return balance of not spend gas.
38
+ let ( caller_account, _) = context
39
+ . evm
40
+ . journaled_state
41
+ . load_account ( caller, & mut context. evm . db )
42
+ . map_err ( EVMError :: Database ) ?;
43
+
44
+ caller_account. info . balance = caller_account
45
+ . info
46
+ . balance
47
+ . saturating_add ( effective_gas_price * U256 :: from ( gas. remaining ( ) + gas. refunded ( ) as u64 ) ) ;
48
+
49
+ Ok ( ( ) )
50
+ }
51
+
52
+ /// Reward beneficiary with gas fee.
53
+ #[ inline]
54
+ pub fn reward_beneficiary < SPEC : Spec , EXT , DB : Database > (
55
+ context : & mut Context < EXT , DB > ,
56
+ gas : & Gas ,
57
+ ) -> Result < ( ) , EVMError < DB :: Error > > {
58
+ if context. evm . env . tx . taiko . is_anchor {
59
+ return Ok ( ( ) ) ;
60
+ }
61
+ let beneficiary = context. evm . env . block . coinbase ;
62
+ let effective_gas_price = context. evm . env . effective_gas_price ( ) ;
63
+
64
+ // transfer fee to coinbase/beneficiary.
65
+ // EIP-1559 discard basefee for coinbase transfer. Basefee amount of gas is discarded.
66
+ let coinbase_gas_price = if SPEC :: enabled ( LONDON ) {
67
+ effective_gas_price. saturating_sub ( context. evm . env . block . basefee )
68
+ } else {
69
+ effective_gas_price
70
+ } ;
71
+
72
+ let ( coinbase_account, _) = context
73
+ . evm
74
+ . journaled_state
75
+ . load_account ( beneficiary, & mut context. evm . db )
76
+ . map_err ( EVMError :: Database ) ?;
77
+
78
+ coinbase_account. mark_touch ( ) ;
79
+ coinbase_account. info . balance = coinbase_account
80
+ . info
81
+ . balance
82
+ . saturating_add ( coinbase_gas_price * U256 :: from ( gas. spend ( ) - gas. refunded ( ) as u64 ) ) ;
83
+
84
+ let treasury = context. evm . env . tx . taiko . treasury ;
85
+ let basefee = context. evm . env . block . basefee ;
86
+
87
+ let ( treasury_account, _) = context
88
+ . evm
89
+ . journaled_state
90
+ . load_account ( treasury, & mut context. evm . db )
91
+ . map_err ( EVMError :: Database ) ?;
92
+
93
+ treasury_account. mark_touch ( ) ;
94
+ treasury_account. info . balance = treasury_account
95
+ . info
96
+ . balance
97
+ . saturating_add ( basefee * U256 :: from ( gas. spend ( ) - gas. refunded ( ) as u64 ) ) ;
98
+ Ok ( ( ) )
99
+ }
0 commit comments