forked from bluealloy/revm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_register.rs
626 lines (568 loc) · 22.9 KB
/
handler_register.rs
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
//! Handler related to Optimism chain
use crate::{
handler::{
mainnet::{self, deduct_caller_inner},
register::EvmHandler,
},
interpreter::{return_ok, return_revert, Gas, InstructionResult},
optimism,
primitives::{
db::Database, spec_to_generic, Account, EVMError, Env, ExecutionResult, HaltReason,
HashMap, InvalidTransaction, ResultAndState, Spec, SpecId, SpecId::REGOLITH, U256,
},
Context, FrameResult,
};
use core::ops::Mul;
use std::string::ToString;
use std::sync::Arc;
pub fn optimism_handle_register<DB: Database, EXT>(handler: &mut EvmHandler<'_, EXT, DB>) {
spec_to_generic!(handler.cfg.spec_id, {
// validate environment
handler.validation.env = Arc::new(validate_env::<SPEC, DB>);
// Validate transaction against state.
handler.validation.tx_against_state = Arc::new(validate_tx_against_state::<SPEC, EXT, DB>);
// load l1 data
handler.pre_execution.load_accounts = Arc::new(load_accounts::<SPEC, EXT, DB>);
// An estimated batch cost is charged from the caller and added to L1 Fee Vault.
handler.pre_execution.deduct_caller = Arc::new(deduct_caller::<SPEC, EXT, DB>);
// Refund is calculated differently then mainnet.
handler.execution.last_frame_return = Arc::new(last_frame_return::<SPEC, EXT, DB>);
handler.post_execution.reward_beneficiary = Arc::new(reward_beneficiary::<SPEC, EXT, DB>);
// In case of halt of deposit transaction return Error.
handler.post_execution.output = Arc::new(output::<SPEC, EXT, DB>);
handler.post_execution.end = Arc::new(end::<SPEC, EXT, DB>);
});
}
/// Validate environment for the Optimism chain.
pub fn validate_env<SPEC: Spec, DB: Database>(env: &Env) -> Result<(), EVMError<DB::Error>> {
// Do not perform any extra validation for deposit transactions, they are pre-verified on L1.
if env.tx.optimism.source_hash.is_some() {
return Ok(());
}
// Important: validate block before tx.
env.validate_block_env::<SPEC>()?;
// Do not allow for a system transaction to be processed if Regolith is enabled.
let tx = &env.tx.optimism;
if tx.is_system_transaction.unwrap_or(false) && SPEC::enabled(SpecId::REGOLITH) {
return Err(InvalidTransaction::DepositSystemTxPostRegolith.into());
}
env.validate_tx::<SPEC>()?;
Ok(())
}
/// Don not perform any extra validation for deposit transactions, they are pre-verified on L1.
pub fn validate_tx_against_state<SPEC: Spec, EXT, DB: Database>(
context: &mut Context<EXT, DB>,
) -> Result<(), EVMError<DB::Error>> {
if context.evm.env.tx.optimism.source_hash.is_some() {
return Ok(());
}
mainnet::validate_tx_against_state::<SPEC, EXT, DB>(context)
}
/// Handle output of the transaction
#[inline]
pub fn last_frame_return<SPEC: Spec, EXT, DB: Database>(
context: &mut Context<EXT, DB>,
frame_result: &mut FrameResult,
) -> Result<(), EVMError<DB::Error>> {
let env = context.evm.env();
let is_deposit = env.tx.optimism.source_hash.is_some();
let tx_system = env.tx.optimism.is_system_transaction;
let tx_gas_limit = env.tx.gas_limit;
let is_regolith = SPEC::enabled(REGOLITH);
let instruction_result = frame_result.interpreter_result().result;
let gas = frame_result.gas_mut();
let remaining = gas.remaining();
let refunded = gas.refunded();
// Spend the gas limit. Gas is reimbursed when the tx returns successfully.
*gas = Gas::new(tx_gas_limit);
gas.record_cost(tx_gas_limit);
match instruction_result {
return_ok!() => {
// On Optimism, deposit transactions report gas usage uniquely to other
// transactions due to them being pre-paid on L1.
//
// Hardfork Behavior:
// - Bedrock (success path):
// - Deposit transactions (non-system) report their gas limit as the usage.
// No refunds.
// - Deposit transactions (system) report 0 gas used. No refunds.
// - Regular transactions report gas usage as normal.
// - Regolith (success path):
// - Deposit transactions (all) report their gas used as normal. Refunds
// enabled.
// - Regular transactions report their gas used as normal.
if !is_deposit || is_regolith {
// For regular transactions prior to Regolith and all transactions after
// Regolith, gas is reported as normal.
gas.erase_cost(remaining);
gas.record_refund(refunded);
} else if is_deposit && tx_system.unwrap_or(false) {
// System transactions were a special type of deposit transaction in
// the Bedrock hardfork that did not incur any gas costs.
gas.erase_cost(tx_gas_limit);
}
}
return_revert!() => {
// On Optimism, deposit transactions report gas usage uniquely to other
// transactions due to them being pre-paid on L1.
//
// Hardfork Behavior:
// - Bedrock (revert path):
// - Deposit transactions (all) report the gas limit as the amount of gas
// used on failure. No refunds.
// - Regular transactions receive a refund on remaining gas as normal.
// - Regolith (revert path):
// - Deposit transactions (all) report the actual gas used as the amount of
// gas used on failure. Refunds on remaining gas enabled.
// - Regular transactions receive a refund on remaining gas as normal.
if !is_deposit || is_regolith {
gas.erase_cost(remaining);
}
}
_ => {}
}
// Prior to Regolith, deposit transactions did not receive gas refunds.
let is_gas_refund_disabled = env.cfg.is_gas_refund_disabled() || (is_deposit && !is_regolith);
if !is_gas_refund_disabled {
gas.set_final_refund::<SPEC>();
}
Ok(())
}
/// Load account (make them warm) and l1 data from database.
#[inline]
pub fn load_accounts<SPEC: Spec, EXT, DB: Database>(
context: &mut Context<EXT, DB>,
) -> Result<(), EVMError<DB::Error>> {
// the L1-cost fee is only computed for Optimism non-deposit transactions.
if context.evm.env.tx.optimism.source_hash.is_none() {
let l1_block_info =
crate::optimism::L1BlockInfo::try_fetch(&mut context.evm.db, SPEC::SPEC_ID)
.map_err(EVMError::Database)?;
// storage l1 block info for later use.
context.evm.l1_block_info = Some(l1_block_info);
}
mainnet::load_accounts::<SPEC, EXT, DB>(context)
}
/// Deduct max balance from caller
#[inline]
pub fn deduct_caller<SPEC: Spec, EXT, DB: Database>(
context: &mut Context<EXT, DB>,
) -> Result<(), EVMError<DB::Error>> {
// load caller's account.
let (caller_account, _) = context
.evm
.journaled_state
.load_account(context.evm.env.tx.caller, &mut context.evm.db)?;
// If the transaction is a deposit with a `mint` value, add the mint value
// in wei to the caller's balance. This should be persisted to the database
// prior to the rest of execution.
if let Some(mint) = context.evm.env.tx.optimism.mint {
caller_account.info.balance += U256::from(mint);
}
// We deduct caller max balance after minting and before deducing the
// l1 cost, max values is already checked in pre_validate but l1 cost wasn't.
deduct_caller_inner::<SPEC>(caller_account, &context.evm.env);
// If the transaction is not a deposit transaction, subtract the L1 data fee from the
// caller's balance directly after minting the requested amount of ETH.
if context.evm.env.tx.optimism.source_hash.is_none() {
// get envelope
let Some(enveloped_tx) = &context.evm.env.tx.optimism.enveloped_tx else {
return Err(EVMError::Custom(
"[OPTIMISM] Failed to load enveloped transaction.".to_string(),
));
};
let tx_l1_cost = context
.evm
.l1_block_info
.as_ref()
.expect("L1BlockInfo should be loaded")
.calculate_tx_l1_cost(enveloped_tx, SPEC::SPEC_ID);
if tx_l1_cost.gt(&caller_account.info.balance) {
return Err(EVMError::Transaction(
InvalidTransaction::LackOfFundForMaxFee {
fee: tx_l1_cost.into(),
balance: caller_account.info.balance.into(),
},
));
}
caller_account.info.balance = caller_account.info.balance.saturating_sub(tx_l1_cost);
}
Ok(())
}
/// Reward beneficiary with gas fee.
#[inline]
pub fn reward_beneficiary<SPEC: Spec, EXT, DB: Database>(
context: &mut Context<EXT, DB>,
gas: &Gas,
) -> Result<(), EVMError<DB::Error>> {
let is_deposit = context.evm.env.tx.optimism.source_hash.is_some();
// transfer fee to coinbase/beneficiary.
if !is_deposit {
mainnet::reward_beneficiary::<SPEC, EXT, DB>(context, gas)?;
}
if !is_deposit {
// If the transaction is not a deposit transaction, fees are paid out
// to both the Base Fee Vault as well as the L1 Fee Vault.
let Some(l1_block_info) = &context.evm.l1_block_info else {
return Err(EVMError::Custom(
"[OPTIMISM] Failed to load L1 block information.".to_string(),
));
};
let Some(enveloped_tx) = &context.evm.env.tx.optimism.enveloped_tx else {
return Err(EVMError::Custom(
"[OPTIMISM] Failed to load enveloped transaction.".to_string(),
));
};
let l1_cost = l1_block_info.calculate_tx_l1_cost(enveloped_tx, SPEC::SPEC_ID);
// Send the L1 cost of the transaction to the L1 Fee Vault.
let Ok((l1_fee_vault_account, _)) = context
.evm
.journaled_state
.load_account(optimism::L1_FEE_RECIPIENT, &mut context.evm.db)
else {
return Err(EVMError::Custom(
"[OPTIMISM] Failed to load L1 Fee Vault account.".to_string(),
));
};
l1_fee_vault_account.mark_touch();
l1_fee_vault_account.info.balance += l1_cost;
// Send the base fee of the transaction to the Base Fee Vault.
let Ok((base_fee_vault_account, _)) = context
.evm
.journaled_state
.load_account(optimism::BASE_FEE_RECIPIENT, &mut context.evm.db)
else {
return Err(EVMError::Custom(
"[OPTIMISM] Failed to load Base Fee Vault account.".to_string(),
));
};
base_fee_vault_account.mark_touch();
base_fee_vault_account.info.balance += context
.evm
.env
.block
.basefee
.mul(U256::from(gas.spend() - gas.refunded() as u64));
}
Ok(())
}
/// Main return handle, returns the output of the transaction.
#[inline]
pub fn output<SPEC: Spec, EXT, DB: Database>(
context: &mut Context<EXT, DB>,
frame_result: FrameResult,
) -> Result<ResultAndState, EVMError<DB::Error>> {
let result = mainnet::output::<EXT, DB>(context, frame_result)?;
if result.result.is_halt() {
// Post-regolith, if the transaction is a deposit transaction and it halts,
// we bubble up to the global return handler. The mint value will be persisted
// and the caller nonce will be incremented there.
let is_deposit = context.evm.env.tx.optimism.source_hash.is_some();
if is_deposit && SPEC::enabled(REGOLITH) {
return Err(EVMError::Transaction(
InvalidTransaction::HaltedDepositPostRegolith,
));
}
}
Ok(result)
}
/// Optimism end handle changes output if the transaction is a deposit transaction.
/// Deposit transaction can't be reverted and is always successful.
#[inline]
pub fn end<SPEC: Spec, EXT, DB: Database>(
context: &mut Context<EXT, DB>,
evm_output: Result<ResultAndState, EVMError<DB::Error>>,
) -> Result<ResultAndState, EVMError<DB::Error>> {
evm_output.or_else(|err| {
if matches!(err, EVMError::Transaction(_))
&& context.evm.env().tx.optimism.source_hash.is_some()
{
// If the transaction is a deposit transaction and it failed
// for any reason, the caller nonce must be bumped, and the
// gas reported must be altered depending on the Hardfork. This is
// also returned as a special Halt variant so that consumers can more
// easily distinguish between a failed deposit and a failed
// normal transaction.
let caller = context.evm.env().tx.caller;
// Increment sender nonce and account balance for the mint amount. Deposits
// always persist the mint amount, even if the transaction fails.
let account = {
let mut acc = Account::from(
context
.evm
.db
.basic(caller)
.unwrap_or_default()
.unwrap_or_default(),
);
acc.info.nonce = acc.info.nonce.saturating_add(1);
acc.info.balance = acc
.info
.balance
.saturating_add(U256::from(context.evm.env().tx.optimism.mint.unwrap_or(0)));
acc.mark_touch();
acc
};
let state = HashMap::from([(caller, account)]);
// The gas used of a failed deposit post-regolith is the gas
// limit of the transaction. pre-regolith, it is the gas limit
// of the transaction for non system transactions and 0 for system
// transactions.
let is_system_tx = context
.evm
.env()
.tx
.optimism
.is_system_transaction
.unwrap_or(false);
let gas_used = if SPEC::enabled(REGOLITH) || !is_system_tx {
context.evm.env().tx.gas_limit
} else {
0
};
Ok(ResultAndState {
result: ExecutionResult::Halt {
reason: HaltReason::FailedDeposit,
gas_used,
},
state,
})
} else {
Err(err)
}
})
}
#[cfg(test)]
mod tests {
use revm_interpreter::{CallOutcome, InterpreterResult};
use super::*;
use crate::{
db::{EmptyDB, InMemoryDB},
primitives::{
bytes, state::AccountInfo, Address, BedrockSpec, Bytes, Env, LatestSpec, RegolithSpec,
B256,
},
L1BlockInfo,
};
/// Creates frame result.
fn call_last_frame_return<SPEC: Spec>(
env: Env,
instruction_result: InstructionResult,
gas: Gas,
) -> Gas {
let mut ctx = Context::new_empty();
ctx.evm.env = Box::new(env);
let mut first_frame = FrameResult::Call(CallOutcome::new(
InterpreterResult {
result: instruction_result,
output: Bytes::new(),
gas,
},
0..0,
));
last_frame_return::<SPEC, _, _>(&mut ctx, &mut first_frame).unwrap();
*first_frame.gas()
}
#[test]
fn test_revert_gas() {
let mut env = Env::default();
env.tx.gas_limit = 100;
env.tx.optimism.source_hash = None;
let gas =
call_last_frame_return::<BedrockSpec>(env, InstructionResult::Revert, Gas::new(90));
assert_eq!(gas.remaining(), 90);
assert_eq!(gas.spend(), 10);
assert_eq!(gas.refunded(), 0);
}
#[test]
fn test_consume_gas() {
let mut env = Env::default();
env.tx.gas_limit = 100;
env.tx.optimism.source_hash = Some(B256::ZERO);
let gas =
call_last_frame_return::<RegolithSpec>(env, InstructionResult::Stop, Gas::new(90));
assert_eq!(gas.remaining(), 90);
assert_eq!(gas.spend(), 10);
assert_eq!(gas.refunded(), 0);
}
#[test]
fn test_consume_gas_with_refund() {
let mut env = Env::default();
env.tx.gas_limit = 100;
env.tx.optimism.source_hash = Some(B256::ZERO);
let mut ret_gas = Gas::new(90);
ret_gas.record_refund(20);
let gas =
call_last_frame_return::<RegolithSpec>(env.clone(), InstructionResult::Stop, ret_gas);
assert_eq!(gas.remaining(), 90);
assert_eq!(gas.spend(), 10);
assert_eq!(gas.refunded(), 2); // min(20, 10/5)
let gas = call_last_frame_return::<RegolithSpec>(env, InstructionResult::Revert, ret_gas);
assert_eq!(gas.remaining(), 90);
assert_eq!(gas.spend(), 10);
assert_eq!(gas.refunded(), 0);
}
#[test]
fn test_consume_gas_sys_deposit_tx() {
let mut env = Env::default();
env.tx.gas_limit = 100;
env.tx.optimism.source_hash = Some(B256::ZERO);
let gas = call_last_frame_return::<BedrockSpec>(env, InstructionResult::Stop, Gas::new(90));
assert_eq!(gas.remaining(), 0);
assert_eq!(gas.spend(), 100);
assert_eq!(gas.refunded(), 0);
}
#[test]
fn test_commit_mint_value() {
let caller = Address::ZERO;
let mut db = InMemoryDB::default();
db.insert_account_info(
caller,
AccountInfo {
balance: U256::from(1000),
..Default::default()
},
);
let mut context: Context<(), InMemoryDB> = Context::new_with_db(db);
context.evm.l1_block_info = Some(L1BlockInfo {
l1_base_fee: U256::from(1_000),
l1_fee_overhead: Some(U256::from(1_000)),
l1_base_fee_scalar: U256::from(1_000),
..Default::default()
});
// Enveloped needs to be some but it will deduce zero fee.
context.evm.env.tx.optimism.enveloped_tx = Some(bytes!(""));
// added mint value is 10.
context.evm.env.tx.optimism.mint = Some(10);
deduct_caller::<RegolithSpec, (), _>(&mut context).unwrap();
// Check the account balance is updated.
let (account, _) = context
.evm
.journaled_state
.load_account(caller, &mut context.evm.db)
.unwrap();
assert_eq!(account.info.balance, U256::from(1010));
}
#[test]
fn test_remove_l1_cost_non_deposit() {
let caller = Address::ZERO;
let mut db = InMemoryDB::default();
db.insert_account_info(
caller,
AccountInfo {
balance: U256::from(1000),
..Default::default()
},
);
let mut context: Context<(), InMemoryDB> = Context::new_with_db(db);
context.evm.l1_block_info = Some(L1BlockInfo {
l1_base_fee: U256::from(1_000),
l1_fee_overhead: Some(U256::from(1_000)),
l1_base_fee_scalar: U256::from(1_000),
..Default::default()
});
// l1block cost is 1048 fee.
context.evm.env.tx.optimism.enveloped_tx = Some(bytes!("FACADE"));
// added mint value is 10.
context.evm.env.tx.optimism.mint = Some(10);
// Putting source_hash to some makes it a deposit transaction.
// so enveloped_tx gas cost is ignored.
context.evm.env.tx.optimism.source_hash = Some(B256::ZERO);
deduct_caller::<RegolithSpec, (), _>(&mut context).unwrap();
// Check the account balance is updated.
let (account, _) = context
.evm
.journaled_state
.load_account(caller, &mut context.evm.db)
.unwrap();
assert_eq!(account.info.balance, U256::from(1010));
}
#[test]
fn test_remove_l1_cost() {
let caller = Address::ZERO;
let mut db = InMemoryDB::default();
db.insert_account_info(
caller,
AccountInfo {
balance: U256::from(1049),
..Default::default()
},
);
let mut context: Context<(), InMemoryDB> = Context::new_with_db(db);
context.evm.l1_block_info = Some(L1BlockInfo {
l1_base_fee: U256::from(1_000),
l1_fee_overhead: Some(U256::from(1_000)),
l1_base_fee_scalar: U256::from(1_000),
..Default::default()
});
// l1block cost is 1048 fee.
context.evm.env.tx.optimism.enveloped_tx = Some(bytes!("FACADE"));
deduct_caller::<RegolithSpec, (), _>(&mut context).unwrap();
// Check the account balance is updated.
let (account, _) = context
.evm
.journaled_state
.load_account(caller, &mut context.evm.db)
.unwrap();
assert_eq!(account.info.balance, U256::from(1));
}
#[test]
fn test_remove_l1_cost_lack_of_funds() {
let caller = Address::ZERO;
let mut db = InMemoryDB::default();
db.insert_account_info(
caller,
AccountInfo {
balance: U256::from(48),
..Default::default()
},
);
let mut context: Context<(), InMemoryDB> = Context::new_with_db(db);
context.evm.l1_block_info = Some(L1BlockInfo {
l1_base_fee: U256::from(1_000),
l1_fee_overhead: Some(U256::from(1_000)),
l1_base_fee_scalar: U256::from(1_000),
..Default::default()
});
// l1block cost is 1048 fee.
context.evm.env.tx.optimism.enveloped_tx = Some(bytes!("FACADE"));
assert_eq!(
deduct_caller::<RegolithSpec, (), _>(&mut context),
Err(EVMError::Transaction(
InvalidTransaction::LackOfFundForMaxFee {
fee: Box::new(U256::from(1048)),
balance: Box::new(U256::from(48)),
},
))
);
}
#[test]
fn test_validate_sys_tx() {
// mark the tx as a system transaction.
let mut env = Env::default();
env.tx.optimism.is_system_transaction = Some(true);
assert_eq!(
validate_env::<RegolithSpec, EmptyDB>(&env),
Err(EVMError::Transaction(
InvalidTransaction::DepositSystemTxPostRegolith
))
);
// Pre-regolith system transactions should be allowed.
assert!(validate_env::<BedrockSpec, EmptyDB>(&env).is_ok());
}
#[test]
fn test_validate_deposit_tx() {
// Set source hash.
let mut env = Env::default();
env.tx.optimism.source_hash = Some(B256::ZERO);
assert!(validate_env::<RegolithSpec, EmptyDB>(&env).is_ok());
}
#[test]
fn test_validate_tx_against_state_deposit_tx() {
// Set source hash.
let mut env = Env::default();
env.tx.optimism.source_hash = Some(B256::ZERO);
// Nonce and balance checks should be skipped for deposit transactions.
assert!(validate_env::<LatestSpec, EmptyDB>(&env).is_ok());
}
}