1use crate::TransactionType;
3use context_interface::{
4 either::Either,
5 transaction::{
6 AccessList, AccessListItem, Authorization, RecoveredAuthority, RecoveredAuthorization,
7 SignedAuthorization, Transaction,
8 },
9};
10use core::fmt::Debug;
11use database_interface::{BENCH_CALLER, BENCH_TARGET};
12use primitives::{eip7825, Address, Bytes, TxKind, B256, U256};
13use std::{vec, vec::Vec};
14
15#[derive(Clone, Debug, PartialEq, Eq)]
21#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
22pub struct TxEnv {
23 pub tx_type: u8,
25 pub caller: Address,
27 pub gas_limit: u64,
29 pub gas_price: u128,
33 pub kind: TxKind,
35 pub value: U256,
37 pub data: Bytes,
39
40 pub nonce: u64,
42
43 pub chain_id: Option<u64>,
49
50 pub access_list: AccessList,
56
57 pub gas_priority_fee: Option<u128>,
63
64 pub blob_hashes: Vec<B256>,
72
73 pub max_fee_per_blob_gas: u128,
79
80 pub authorization_list: Vec<Either<SignedAuthorization, RecoveredAuthorization>>,
89}
90
91impl Default for TxEnv {
92 fn default() -> Self {
93 Self::builder().build().unwrap()
94 }
95}
96
97#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
99#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
100pub enum DeriveTxTypeError {
101 MissingTargetForEip4844,
103 MissingTargetForEip7702,
105 MissingTargetForEip7873,
107}
108
109impl TxEnv {
110 pub fn new_bench() -> Self {
112 Self {
113 caller: BENCH_CALLER,
114 kind: TxKind::Call(BENCH_TARGET),
115 gas_limit: 1_000_000_000,
116 ..Default::default()
117 }
118 }
119
120 pub fn derive_tx_type(&mut self) -> Result<(), DeriveTxTypeError> {
123 if !self.access_list.0.is_empty() {
124 self.tx_type = TransactionType::Eip2930 as u8;
125 }
126
127 if self.gas_priority_fee.is_some() {
128 self.tx_type = TransactionType::Eip1559 as u8;
129 }
130
131 if !self.blob_hashes.is_empty() || self.max_fee_per_blob_gas > 0 {
132 if let TxKind::Call(_) = self.kind {
133 self.tx_type = TransactionType::Eip4844 as u8;
134 return Ok(());
135 } else {
136 return Err(DeriveTxTypeError::MissingTargetForEip4844);
137 }
138 }
139
140 if !self.authorization_list.is_empty() {
141 if let TxKind::Call(_) = self.kind {
142 self.tx_type = TransactionType::Eip7702 as u8;
143 return Ok(());
144 } else {
145 return Err(DeriveTxTypeError::MissingTargetForEip7702);
146 }
147 }
148 Ok(())
149 }
150
151 pub fn set_signed_authorization(&mut self, auth: Vec<SignedAuthorization>) {
153 self.authorization_list = auth.into_iter().map(Either::Left).collect();
154 }
155
156 pub fn set_recovered_authorization(&mut self, auth: Vec<RecoveredAuthorization>) {
158 self.authorization_list = auth.into_iter().map(Either::Right).collect();
159 }
160}
161
162impl Transaction for TxEnv {
163 type AccessListItem<'a> = &'a AccessListItem;
164 type Authorization<'a> = &'a Either<SignedAuthorization, RecoveredAuthorization>;
165
166 fn tx_type(&self) -> u8 {
167 self.tx_type
168 }
169
170 fn kind(&self) -> TxKind {
171 self.kind
172 }
173
174 fn caller(&self) -> Address {
175 self.caller
176 }
177
178 fn gas_limit(&self) -> u64 {
179 self.gas_limit
180 }
181
182 fn gas_price(&self) -> u128 {
183 self.gas_price
184 }
185
186 fn value(&self) -> U256 {
187 self.value
188 }
189
190 fn nonce(&self) -> u64 {
191 self.nonce
192 }
193
194 fn chain_id(&self) -> Option<u64> {
195 self.chain_id
196 }
197
198 fn access_list(&self) -> Option<impl Iterator<Item = Self::AccessListItem<'_>>> {
199 Some(self.access_list.0.iter())
200 }
201
202 fn max_fee_per_gas(&self) -> u128 {
203 self.gas_price
204 }
205
206 fn max_fee_per_blob_gas(&self) -> u128 {
207 self.max_fee_per_blob_gas
208 }
209
210 fn authorization_list_len(&self) -> usize {
211 self.authorization_list.len()
212 }
213
214 fn authorization_list(&self) -> impl Iterator<Item = Self::Authorization<'_>> {
215 self.authorization_list.iter()
216 }
217
218 fn input(&self) -> &Bytes {
219 &self.data
220 }
221
222 fn blob_versioned_hashes(&self) -> &[B256] {
223 &self.blob_hashes
224 }
225
226 fn max_priority_fee_per_gas(&self) -> Option<u128> {
227 self.gas_priority_fee
228 }
229}
230
231#[derive(Default, Debug)]
233pub struct TxEnvBuilder {
234 tx_type: Option<u8>,
235 caller: Address,
236 gas_limit: u64,
237 gas_price: u128,
238 kind: TxKind,
239 value: U256,
240 data: Bytes,
241 nonce: u64,
242 chain_id: Option<u64>,
243 access_list: AccessList,
244 gas_priority_fee: Option<u128>,
245 blob_hashes: Vec<B256>,
246 max_fee_per_blob_gas: u128,
247 authorization_list: Vec<Either<SignedAuthorization, RecoveredAuthorization>>,
248}
249
250impl TxEnvBuilder {
251 pub fn new() -> Self {
253 Self {
254 tx_type: None,
255 caller: Address::default(),
256 gas_limit: eip7825::TX_GAS_LIMIT_CAP,
257 gas_price: 0,
258 kind: TxKind::Call(Address::default()),
259 value: U256::ZERO,
260 data: Bytes::default(),
261 nonce: 0,
262 chain_id: Some(1), access_list: Default::default(),
264 gas_priority_fee: None,
265 blob_hashes: Vec::new(),
266 max_fee_per_blob_gas: 0,
267 authorization_list: Vec::new(),
268 }
269 }
270
271 pub fn tx_type(mut self, tx_type: Option<u8>) -> Self {
273 self.tx_type = tx_type;
274 self
275 }
276
277 pub fn get_tx_type(&self) -> Option<u8> {
279 self.tx_type
280 }
281
282 pub fn caller(mut self, caller: Address) -> Self {
284 self.caller = caller;
285 self
286 }
287
288 pub fn gas_limit(mut self, gas_limit: u64) -> Self {
290 self.gas_limit = gas_limit;
291 self
292 }
293
294 pub fn max_fee_per_gas(mut self, max_fee_per_gas: u128) -> Self {
296 self.gas_price = max_fee_per_gas;
297 self
298 }
299
300 pub fn gas_price(mut self, gas_price: u128) -> Self {
302 self.gas_price = gas_price;
303 self
304 }
305
306 pub fn kind(mut self, kind: TxKind) -> Self {
308 self.kind = kind;
309 self
310 }
311
312 pub fn call(mut self, target: Address) -> Self {
314 self.kind = TxKind::Call(target);
315 self
316 }
317
318 pub fn create(mut self) -> Self {
320 self.kind = TxKind::Create;
321 self
322 }
323
324 pub fn to(self, target: Address) -> Self {
326 self.call(target)
327 }
328
329 pub fn value(mut self, value: U256) -> Self {
331 self.value = value;
332 self
333 }
334
335 pub fn data(mut self, data: Bytes) -> Self {
337 self.data = data;
338 self
339 }
340
341 pub fn nonce(mut self, nonce: u64) -> Self {
343 self.nonce = nonce;
344 self
345 }
346
347 pub fn chain_id(mut self, chain_id: Option<u64>) -> Self {
349 self.chain_id = chain_id;
350 self
351 }
352
353 pub fn access_list(mut self, access_list: AccessList) -> Self {
355 self.access_list = access_list;
356 self
357 }
358
359 pub fn gas_priority_fee(mut self, gas_priority_fee: Option<u128>) -> Self {
361 self.gas_priority_fee = gas_priority_fee;
362 self
363 }
364
365 pub fn blob_hashes(mut self, blob_hashes: Vec<B256>) -> Self {
367 self.blob_hashes = blob_hashes;
368 self
369 }
370
371 pub fn max_fee_per_blob_gas(mut self, max_fee_per_blob_gas: u128) -> Self {
373 self.max_fee_per_blob_gas = max_fee_per_blob_gas;
374 self
375 }
376
377 pub fn authorization_list(
379 mut self,
380 authorization_list: Vec<Either<SignedAuthorization, RecoveredAuthorization>>,
381 ) -> Self {
382 self.authorization_list = authorization_list;
383 self
384 }
385
386 pub fn authorization_list_signed(mut self, auth: Vec<SignedAuthorization>) -> Self {
388 self.authorization_list = auth.into_iter().map(Either::Left).collect();
389 self
390 }
391
392 pub fn authorization_list_recovered(mut self, auth: Vec<RecoveredAuthorization>) -> Self {
394 self.authorization_list = auth.into_iter().map(Either::Right).collect();
395 self
396 }
397
398 pub fn build_fill(mut self) -> TxEnv {
400 if let Some(tx_type) = self.tx_type {
401 match TransactionType::from(tx_type) {
402 TransactionType::Legacy => {
403 }
405 TransactionType::Eip2930 => {
406 }
408 TransactionType::Eip1559 => {
409 if self.gas_priority_fee.is_none() {
411 self.gas_priority_fee = Some(0);
412 }
413 }
414 TransactionType::Eip4844 => {
415 if self.gas_priority_fee.is_none() {
417 self.gas_priority_fee = Some(0);
418 }
419
420 if self.blob_hashes.is_empty() {
422 self.blob_hashes = vec![B256::default()];
423 }
424
425 if !self.kind.is_call() {
427 self.kind = TxKind::Call(Address::default());
428 }
429 }
430 TransactionType::Eip7702 => {
431 if self.gas_priority_fee.is_none() {
433 self.gas_priority_fee = Some(0);
434 }
435
436 if self.authorization_list.is_empty() {
438 self.authorization_list =
440 vec![Either::Right(RecoveredAuthorization::new_unchecked(
441 Authorization {
442 chain_id: U256::from(self.chain_id.unwrap_or(1)),
443 address: self.caller,
444 nonce: self.nonce,
445 },
446 RecoveredAuthority::Invalid,
447 ))];
448 }
449
450 if !self.kind.is_call() {
452 self.kind = TxKind::Call(Address::default());
453 }
454 }
455 TransactionType::Custom => {
456 }
458 }
459 }
460
461 let mut tx = TxEnv {
462 tx_type: self.tx_type.unwrap_or(0),
463 caller: self.caller,
464 gas_limit: self.gas_limit,
465 gas_price: self.gas_price,
466 kind: self.kind,
467 value: self.value,
468 data: self.data,
469 nonce: self.nonce,
470 chain_id: self.chain_id,
471 access_list: self.access_list,
472 gas_priority_fee: self.gas_priority_fee,
473 blob_hashes: self.blob_hashes,
474 max_fee_per_blob_gas: self.max_fee_per_blob_gas,
475 authorization_list: self.authorization_list,
476 };
477
478 if self.tx_type.is_none() {
480 match tx.derive_tx_type() {
481 Ok(_) => {}
482 Err(DeriveTxTypeError::MissingTargetForEip4844) => {
483 tx.kind = TxKind::Call(Address::default());
484 }
485 Err(DeriveTxTypeError::MissingTargetForEip7702) => {
486 tx.kind = TxKind::Call(Address::default());
487 }
488 Err(DeriveTxTypeError::MissingTargetForEip7873) => {
489 tx.kind = TxKind::Call(Address::default());
490 }
491 }
492 }
493
494 tx
495 }
496
497 pub fn build(self) -> Result<TxEnv, TxEnvBuildError> {
500 if let Some(tx_type) = self.tx_type {
502 match TransactionType::from(tx_type) {
503 TransactionType::Legacy => {
504 }
506 TransactionType::Eip2930 => {
507 }
509 TransactionType::Eip1559 => {
510 if self.gas_priority_fee.is_none() {
512 return Err(TxEnvBuildError::MissingGasPriorityFeeForEip1559);
513 }
514 }
515 TransactionType::Eip4844 => {
516 if self.gas_priority_fee.is_none() {
518 return Err(TxEnvBuildError::MissingGasPriorityFeeForEip1559);
519 }
520
521 if self.blob_hashes.is_empty() {
523 return Err(TxEnvBuildError::MissingBlobHashesForEip4844);
524 }
525
526 if !self.kind.is_call() {
528 return Err(TxEnvBuildError::MissingTargetForEip4844);
529 }
530 }
531 TransactionType::Eip7702 => {
532 if self.gas_priority_fee.is_none() {
534 return Err(TxEnvBuildError::MissingGasPriorityFeeForEip1559);
535 }
536
537 if self.authorization_list.is_empty() {
539 return Err(TxEnvBuildError::MissingAuthorizationListForEip7702);
540 }
541
542 if !self.kind.is_call() {
544 return Err(DeriveTxTypeError::MissingTargetForEip4844.into());
545 }
546 }
547 TransactionType::Custom => {
548 }
550 }
551 }
552
553 let mut tx = TxEnv {
554 tx_type: self.tx_type.unwrap_or(0),
555 caller: self.caller,
556 gas_limit: self.gas_limit,
557 gas_price: self.gas_price,
558 kind: self.kind,
559 value: self.value,
560 data: self.data,
561 nonce: self.nonce,
562 chain_id: self.chain_id,
563 access_list: self.access_list,
564 gas_priority_fee: self.gas_priority_fee,
565 blob_hashes: self.blob_hashes,
566 max_fee_per_blob_gas: self.max_fee_per_blob_gas,
567 authorization_list: self.authorization_list,
568 };
569
570 if self.tx_type.is_none() {
572 tx.derive_tx_type()?;
573 }
574
575 Ok(tx)
576 }
577}
578
579#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
581#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
582pub enum TxEnvBuildError {
583 DeriveErr(DeriveTxTypeError),
585 MissingGasPriorityFeeForEip1559,
587 MissingBlobHashesForEip4844,
589 MissingAuthorizationListForEip7702,
591 MissingTargetForEip4844,
593}
594
595impl From<DeriveTxTypeError> for TxEnvBuildError {
596 fn from(error: DeriveTxTypeError) -> Self {
597 TxEnvBuildError::DeriveErr(error)
598 }
599}
600
601impl TxEnv {
602 pub fn builder() -> TxEnvBuilder {
604 TxEnvBuilder::new()
605 }
606
607 pub fn builder_for_bench() -> TxEnvBuilder {
609 TxEnv::new_bench().modify()
610 }
611
612 pub fn modify(self) -> TxEnvBuilder {
614 let TxEnv {
615 tx_type,
616 caller,
617 gas_limit,
618 gas_price,
619 kind,
620 value,
621 data,
622 nonce,
623 chain_id,
624 access_list,
625 gas_priority_fee,
626 blob_hashes,
627 max_fee_per_blob_gas,
628 authorization_list,
629 } = self;
630
631 TxEnvBuilder::new()
632 .tx_type(Some(tx_type))
633 .caller(caller)
634 .gas_limit(gas_limit)
635 .gas_price(gas_price)
636 .kind(kind)
637 .value(value)
638 .data(data)
639 .nonce(nonce)
640 .chain_id(chain_id)
641 .access_list(access_list)
642 .gas_priority_fee(gas_priority_fee)
643 .blob_hashes(blob_hashes)
644 .max_fee_per_blob_gas(max_fee_per_blob_gas)
645 .authorization_list(authorization_list)
646 }
647}
648
649#[cfg(test)]
650mod tests {
651 use super::*;
652
653 fn effective_gas_setup(
654 tx_type: TransactionType,
655 gas_price: u128,
656 gas_priority_fee: Option<u128>,
657 ) -> u128 {
658 let tx = TxEnv {
659 tx_type: tx_type as u8,
660 gas_price,
661 gas_priority_fee,
662 ..Default::default()
663 };
664 let base_fee = 100;
665 tx.effective_gas_price(base_fee)
666 }
667
668 #[test]
669 fn test_effective_gas_price() {
670 assert_eq!(90, effective_gas_setup(TransactionType::Legacy, 90, None));
671 assert_eq!(
672 90,
673 effective_gas_setup(TransactionType::Legacy, 90, Some(0))
674 );
675 assert_eq!(
676 90,
677 effective_gas_setup(TransactionType::Legacy, 90, Some(10))
678 );
679 assert_eq!(
680 120,
681 effective_gas_setup(TransactionType::Legacy, 120, Some(10))
682 );
683 assert_eq!(90, effective_gas_setup(TransactionType::Eip2930, 90, None));
684 assert_eq!(
685 90,
686 effective_gas_setup(TransactionType::Eip2930, 90, Some(0))
687 );
688 assert_eq!(
689 90,
690 effective_gas_setup(TransactionType::Eip2930, 90, Some(10))
691 );
692 assert_eq!(
693 120,
694 effective_gas_setup(TransactionType::Eip2930, 120, Some(10))
695 );
696 assert_eq!(90, effective_gas_setup(TransactionType::Eip1559, 90, None));
697 assert_eq!(
698 90,
699 effective_gas_setup(TransactionType::Eip1559, 90, Some(0))
700 );
701 assert_eq!(
702 90,
703 effective_gas_setup(TransactionType::Eip1559, 90, Some(10))
704 );
705 assert_eq!(
706 110,
707 effective_gas_setup(TransactionType::Eip1559, 120, Some(10))
708 );
709 assert_eq!(90, effective_gas_setup(TransactionType::Eip4844, 90, None));
710 assert_eq!(
711 90,
712 effective_gas_setup(TransactionType::Eip4844, 90, Some(0))
713 );
714 assert_eq!(
715 90,
716 effective_gas_setup(TransactionType::Eip4844, 90, Some(10))
717 );
718 assert_eq!(
719 110,
720 effective_gas_setup(TransactionType::Eip4844, 120, Some(10))
721 );
722 assert_eq!(90, effective_gas_setup(TransactionType::Eip7702, 90, None));
723 assert_eq!(
724 90,
725 effective_gas_setup(TransactionType::Eip7702, 90, Some(0))
726 );
727 assert_eq!(
728 90,
729 effective_gas_setup(TransactionType::Eip7702, 90, Some(10))
730 );
731 assert_eq!(
732 110,
733 effective_gas_setup(TransactionType::Eip7702, 120, Some(10))
734 );
735 }
736}