Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Hydrate" the transactions returned by eth_getBlockByHash #982

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ethportal-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ nanotemplate = "0.3.0"
quickcheck = "1.0.3"
rand = "0.8.5"
reth-rpc-types = { tag = "v0.1.0-alpha.10", git = "https://github.com/paradigmxyz/reth.git"}
reth-primitives = { tag = "v0.1.0-alpha.10", git = "https://github.com/paradigmxyz/reth.git"}
rlp = "0.5.0"
rlp-derive = "0.1.0"
ruint = { version = "1.9.0", features = ["primitive-types"] }
Expand Down
18 changes: 1 addition & 17 deletions ethportal-api/src/types/execution/header.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use ethereum_types::{Bloom, H160, H256, H64, U256};
use reth_rpc_types::Header as RpcHeader;
use rlp::{Decodable, DecoderError, Encodable, Rlp, RlpStream};
use ruint::Uint;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use ssz::{Encode, SszDecoderBuilder, SszEncoder};
use ssz_derive::{Decode, Encode};

use crate::types::bytes::ByteList;
use crate::utils::bytes::{hex_decode, hex_encode};
use crate::utils::rethtypes::{u256_to_uint256, u64_to_uint256};

const LONDON_BLOCK_NUMBER: u64 = 12965000;
const SHANGHAI_BLOCK_NUMBER: u64 = 17034871;
Expand Down Expand Up @@ -258,22 +258,6 @@ impl From<Header> for RpcHeader {
}
}

fn u256_to_uint256(u256: U256) -> Uint<256, 4> {
let mut bytes = [0u8; 32];
u256.to_big_endian(&mut bytes);
Uint::from_be_bytes(bytes)
}

fn u64_to_uint256(val: u64) -> Uint<256, 4> {
let u64_bytes: &[u8] = &val.to_be_bytes();
let high_zero_bytes: &[u8] = &[0u8; 24];
let bytes: [u8; 32] = [high_zero_bytes, u64_bytes]
.concat()
.try_into()
.expect("8 bytes + 24 bytes should be 32 bytes");
Uint::from_be_bytes(bytes)
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TxHashes {
pub hashes: Vec<H256>,
Expand Down
68 changes: 60 additions & 8 deletions ethportal-api/src/types/execution/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bytes::Bytes;
use ethereum_types::{H160, H256, U256, U64};
use reth_primitives;
use rlp::{Decodable, DecoderError, Encodable, Rlp, RlpStream};
use rlp_derive::{RlpDecodable, RlpEncodable};
use serde::{Deserialize, Deserializer};
Expand All @@ -21,6 +22,15 @@ impl Transaction {
keccak_hash::keccak(self.encode())
}

pub fn type_id(&self) -> u8 {
let txn_id = match self {
Self::Legacy(_) => TransactionId::Legacy,
Self::AccessList(_) => TransactionId::AccessList,
Self::EIP1559(_) => TransactionId::EIP1559,
};
txn_id as u8
}

pub fn decode(tx: &[u8]) -> Result<Self, DecoderError> {
// at least one byte needs to be present
if tx.is_empty() {
Expand Down Expand Up @@ -90,6 +100,24 @@ impl<'de> Deserialize<'de> for Transaction {
}
}

impl From<Transaction> for reth_primitives::TransactionSigned {
fn from(tx: Transaction) -> Self {
let (core_tx, signature) = match tx {
Transaction::Legacy(tx) => {
let signature = reth_primitives::Signature {
v: tx.v,
r: tx.r.into(),
s: tx.s.into(),
};
(reth_primitives::Transaction::Legacy(tx.into()), signature)
}
Transaction::AccessList(tx) => Self::Eip2930(tx.into()),
Transaction::EIP1559(tx) => Self::Eip1559(tx.into()),
};
Self::from_transaction_and_signature(core_tx, signature)
}
}

#[derive(Default, Debug, Clone, PartialEq, Eq, RlpEncodable, RlpDecodable)]
pub struct LegacyTransaction {
pub nonce: U256,
Expand All @@ -103,6 +131,23 @@ pub struct LegacyTransaction {
pub s: U256,
}

impl From<LegacyTransaction> for reth_primitives::TxLegacy {
fn from(tx: LegacyTransaction) -> Self {
let to = match tx.to {
ToAddress::Empty => reth_primitives::TransactionKind::Create,
ToAddress::Exists(addr) => reth_primitives::TransactionKind::Call(addr.into()),
};
Self {
nonce: tx.nonce.as_u64(),
gas_price: tx.gas_price.as_u128(),
gas_limit: tx.gas.as_u64(),
to,
value: tx.value.as_u128(),
input: tx.data.into(),
}
}
}

#[derive(Default, Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
struct LegacyTransactionHelper {
Expand Down Expand Up @@ -140,7 +185,7 @@ pub struct AccessListTransaction {
pub chain_id: U256,
pub nonce: U256,
pub gas_price: U256,
pub gas_limit: U256,
pub gas: U256,
pub to: ToAddress,
pub value: U256,
pub data: Bytes,
Expand All @@ -156,8 +201,7 @@ struct AccessListTransactionHelper {
pub chain_id: U256,
pub nonce: U256,
pub gas_price: U256,
#[serde(rename(deserialize = "gas"))]
pub gas_limit: U256,
pub gas: U256,
pub to: ToAddress,
pub value: U256,
#[serde(rename(deserialize = "input"))]
Expand All @@ -176,7 +220,7 @@ impl Into<AccessListTransaction> for AccessListTransactionHelper {
chain_id: self.chain_id,
nonce: self.nonce,
gas_price: self.gas_price,
gas_limit: self.gas_limit,
gas: self.gas,
to: self.to,
value: self.value,
data: self.data.0,
Expand All @@ -196,7 +240,7 @@ pub struct EIP1559Transaction {
pub nonce: U256,
pub max_priority_fee_per_gas: U256,
pub max_fee_per_gas: U256,
pub gas_limit: U256,
pub gas: U256,
pub to: ToAddress,
pub value: U256,
pub data: Bytes,
Expand All @@ -213,8 +257,7 @@ struct EIP1559TransactionHelper {
pub nonce: U256,
pub max_priority_fee_per_gas: U256,
pub max_fee_per_gas: U256,
#[serde(rename(deserialize = "gas"))]
pub gas_limit: U256,
pub gas: U256,
pub to: ToAddress,
pub value: U256,
#[serde(rename(deserialize = "input"))]
Expand All @@ -234,7 +277,7 @@ impl Into<EIP1559Transaction> for EIP1559TransactionHelper {
nonce: self.nonce,
max_priority_fee_per_gas: self.max_priority_fee_per_gas,
max_fee_per_gas: self.max_fee_per_gas,
gas_limit: self.gas_limit,
gas: self.gas,
to: self.to,
value: self.value,
data: self.data.0,
Expand Down Expand Up @@ -346,3 +389,12 @@ pub struct AccessListItem {
pub address: H160,
pub storage_keys: Vec<H256>,
}

impl From<AccessListItem> for reth_primitives::AccessListItem {
fn from(val: AccessListItem) -> Self {
Self {
address: val.address.into(),
storage_keys: val.storage_keys.into_iter().map(Into::into).collect(),
}
}
}
1 change: 1 addition & 0 deletions ethportal-api/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod bytes;
pub mod rethtypes;
pub mod serde;
30 changes: 30 additions & 0 deletions ethportal-api/src/utils/rethtypes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use ethereum_types::{U256, U64};
use ruint::Uint;

pub fn u256_to_uint256(u256: U256) -> Uint<256, 4> {
let mut bytes = [0u8; 32];
u256.to_big_endian(&mut bytes);
Uint::from_be_bytes(bytes)
}

pub fn u256_to_uint128(u256: U256) -> Uint<128, 2> {
let mut bytes = [0u8; 16];
u256.to_big_endian(&mut bytes);
Uint::from_be_bytes(bytes)
}

pub fn u64_to_uint256(val: u64) -> Uint<256, 4> {
let u64_bytes: &[u8] = &val.to_be_bytes();
let high_zero_bytes: &[u8] = &[0u8; 24];
let bytes: [u8; 32] = [high_zero_bytes, u64_bytes]
.concat()
.try_into()
.expect("8 bytes + 24 bytes should be 32 bytes");
Uint::from_be_bytes(bytes)
}

pub fn ethtype_u64_to_uint256(val: U64) -> Uint<256, 4> {
let mut bytes = [0u8; 32];
val.to_big_endian(&mut bytes);
Uint::from_be_bytes(bytes)
}
1 change: 1 addition & 0 deletions rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ tokio = { version = "1.14.0", features = ["full"] }
hyper = "0.14"
reth-ipc = { tag = "v0.1.0-alpha.10", git = "https://github.com/paradigmxyz/reth.git"}
reth-rpc-types = { tag = "v0.1.0-alpha.10", git = "https://github.com/paradigmxyz/reth.git"}
ruint = { version = "1.9.0", features = ["primitive-types"] }
url = "2.3.1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.95"
Expand Down
Loading