Skip to content

Commit

Permalink
feat(primitives): transaction encoding and decoding (#102)
Browse files Browse the repository at this point in the history
* feat(core): transaction encoding and decoding

* cargo fmt

* remove printlns

* fix tx conversion
  • Loading branch information
Rjected committed Oct 19, 2022
1 parent f672781 commit 7e26ba5
Show file tree
Hide file tree
Showing 5 changed files with 401 additions and 21 deletions.
22 changes: 15 additions & 7 deletions crates/executor/src/revm_wrap.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use reth_interfaces::executor::ExecutorDb;
use reth_primitives::{BlockLocked, Transaction, H160, H256, U256};
use reth_primitives::{BlockLocked, Transaction, TransactionKind, H160, H256, U256};
use revm::{
db::{CacheDB, Database, EmptyDB},
BlockEnv, TransactTo, TxEnv,
Expand Down Expand Up @@ -58,8 +58,10 @@ pub fn fill_tx_env(tx_env: &mut TxEnv, transaction: &Transaction) {
tx_env.gas_limit = *gas_limit;
tx_env.gas_price = (*gas_price).into();
tx_env.gas_priority_fee = None;
tx_env.transact_to =
if let Some(to) = to { TransactTo::Call(*to) } else { TransactTo::create() };
tx_env.transact_to = match to {
TransactionKind::Call(to) => TransactTo::Call(*to),
TransactionKind::Create => TransactTo::create(),
};
tx_env.value = *value;
tx_env.data = input.0.clone();
tx_env.chain_id = *chain_id;
Expand All @@ -78,13 +80,16 @@ pub fn fill_tx_env(tx_env: &mut TxEnv, transaction: &Transaction) {
tx_env.gas_limit = *gas_limit;
tx_env.gas_price = (*gas_price).into();
tx_env.gas_priority_fee = None;
tx_env.transact_to =
if let Some(to) = to { TransactTo::Call(*to) } else { TransactTo::create() };
tx_env.transact_to = match to {
TransactionKind::Call(to) => TransactTo::Call(*to),
TransactionKind::Create => TransactTo::create(),
};
tx_env.value = *value;
tx_env.data = input.0.clone();
tx_env.chain_id = Some(*chain_id);
tx_env.nonce = Some(*nonce);
tx_env.access_list = access_list
.0
.iter()
.map(|l| {
(
Expand All @@ -108,13 +113,16 @@ pub fn fill_tx_env(tx_env: &mut TxEnv, transaction: &Transaction) {
tx_env.gas_limit = *gas_limit;
tx_env.gas_price = (*max_fee_per_gas).into();
tx_env.gas_priority_fee = Some((*max_priority_fee_per_gas).into());
tx_env.transact_to =
if let Some(to) = to { TransactTo::Call(*to) } else { TransactTo::create() };
tx_env.transact_to = match to {
TransactionKind::Call(to) => TransactTo::Call(*to),
TransactionKind::Create => TransactTo::create(),
};
tx_env.value = *value;
tx_env.data = input.0.clone();
tx_env.chain_id = Some(*chain_id);
tx_env.nonce = Some(*nonce);
tx_env.access_list = access_list
.0
.iter()
.map(|l| {
(
Expand Down
6 changes: 3 additions & 3 deletions crates/primitives/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::ops::Deref;

/// Block header
#[main_codec]
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Hash)]
pub struct Header {
/// The Keccak 256-bit hash of the parent
/// block’s header, in its entirety; formally Hp.
Expand Down Expand Up @@ -174,7 +174,7 @@ impl Decodable for Header {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Hash)]
/// HeaderLocked that has precalculated hash, use unlock if you want to modify header.
pub struct HeaderLocked {
/// Locked Header fields.
Expand Down Expand Up @@ -213,7 +213,7 @@ impl HeaderLocked {
mod tests {
use crate::Address;

use super::{Decodable, Encodable, Header, H160, H256};
use super::{Decodable, Encodable, Header, H256};
use ethers_core::{
types::Bytes,
utils::hex::{self, FromHex},
Expand Down
6 changes: 4 additions & 2 deletions crates/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ pub use header::{Header, HeaderLocked};
pub use jsonu256::JsonU256;
pub use log::Log;
pub use receipt::Receipt;
pub use transaction::{AccessList, AccessListItem, Transaction, TransactionSigned, TxType};
pub use transaction::{
AccessList, AccessListItem, Transaction, TransactionKind, TransactionSigned, TxType,
};

/// Block hash.
pub type BlockHash = H256;
Expand All @@ -45,5 +47,5 @@ pub type StorageValue = H256;
// NOTE: There is a benefit of using wrapped Bytes as it gives us serde and debug
pub use ethers_core::{
types as rpc,
types::{Bloom, Bytes, H128, H160, H256, H512, H64, U256, U64},
types::{Bloom, Bytes, H128, H160, H256, H512, H64, U128, U256, U64},
};
7 changes: 5 additions & 2 deletions crates/primitives/src/transaction/access_list.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use reth_rlp::{RlpDecodable, RlpDecodableWrapper, RlpEncodable, RlpEncodableWrapper};

use crate::{Address, H256};

/// A list of addresses and storage keys that the transaction plans to access.
/// Accesses outside the list are possible, but become more expensive.
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, Hash, Default, RlpDecodable, RlpEncodable)]
pub struct AccessListItem {
/// Account addresses that would be loaded at the start of execution
pub address: Address,
Expand All @@ -11,4 +13,5 @@ pub struct AccessListItem {
}

/// AccessList as defined in EIP-2930
pub type AccessList = Vec<AccessListItem>;
#[derive(Clone, Debug, PartialEq, Eq, Hash, Default, RlpDecodableWrapper, RlpEncodableWrapper)]
pub struct AccessList(pub Vec<AccessListItem>);
Loading

0 comments on commit 7e26ba5

Please sign in to comment.