From b9700791c82218ae40d0bf7edc47e83540d7da65 Mon Sep 17 00:00:00 2001 From: Roman Krasiuk Date: Tue, 8 Nov 2022 17:56:13 +0200 Subject: [PATCH] feat(db): transaction value type (#175) --- crates/interfaces/src/db/codecs/scale.rs | 2 +- crates/interfaces/src/db/tables.rs | 10 ++++----- crates/primitives/src/hex_bytes.rs | 6 ++++-- .../primitives/src/transaction/access_list.rs | 21 +++++-------------- crates/primitives/src/transaction/mod.rs | 14 ++++++++++--- .../primitives/src/transaction/signature.rs | 2 ++ 6 files changed, 27 insertions(+), 28 deletions(-) diff --git a/crates/interfaces/src/db/codecs/scale.rs b/crates/interfaces/src/db/codecs/scale.rs index 533d00ac5..93e2d2374 100644 --- a/crates/interfaces/src/db/codecs/scale.rs +++ b/crates/interfaces/src/db/codecs/scale.rs @@ -53,7 +53,7 @@ impl ScaleValue for Vec {} impl sealed::Sealed for Vec {} impl_scale!(U256, H256, H160); -impl_scale!(Header, Account, Log, Receipt, TxType, StorageEntry); +impl_scale!(Header, Account, Log, Receipt, TxType, StorageEntry, TransactionSigned); impl_scale!(AccountBeforeTx); impl_scale_value!(u8, u32, u16, u64); diff --git a/crates/interfaces/src/db/tables.rs b/crates/interfaces/src/db/tables.rs index ece52bfd4..b9f0c1971 100644 --- a/crates/interfaces/src/db/tables.rs +++ b/crates/interfaces/src/db/tables.rs @@ -9,8 +9,8 @@ use crate::db::{ DupSort, }; use reth_primitives::{ - Account, Address, BlockHash, BlockNumber, Header, IntegerList, Receipt, StorageEntry, TxNumber, - H256, + Account, Address, BlockHash, BlockNumber, Header, IntegerList, Receipt, StorageEntry, + TransactionSigned, TxNumber, H256, }; /// Enum for the type of table present in libmdbx. @@ -128,11 +128,11 @@ table!( table!( /// Stores the transaction body from non canonical transactions. - NonCanonicalTransactions => BlockNumHashTxNumber => RlpTxBody); + NonCanonicalTransactions => BlockNumHashTxNumber => TransactionSigned); table!( /// Stores the transaction body from canonical transactions. Canonical only - Transactions => TxNumber => RlpTxBody); + Transactions => TxNumber => TransactionSigned); table!( /// Stores transaction receipts. Canonical only @@ -244,8 +244,6 @@ pub type BlockNumHashTxNumber = Vec; /// Temporary placeholder type for DB. pub type RlpTotalDifficulty = Vec; /// Temporary placeholder type for DB. -pub type RlpTxBody = Vec; -/// Temporary placeholder type for DB. pub type AddressStorageKey = Vec; /// Temporary placeholder type for DB. pub type Bytecode = Vec; diff --git a/crates/primitives/src/hex_bytes.rs b/crates/primitives/src/hex_bytes.rs index c0bf3aa54..6233f90de 100644 --- a/crates/primitives/src/hex_bytes.rs +++ b/crates/primitives/src/hex_bytes.rs @@ -1,5 +1,6 @@ +use reth_codecs::main_codec; use reth_rlp::{Decodable, DecodeError, Encodable}; -use serde::{Deserialize, Deserializer, Serialize, Serializer}; +use serde::{Deserialize, Deserializer, Serializer}; use std::{ borrow::Borrow, clone::Clone, @@ -10,7 +11,8 @@ use std::{ use thiserror::Error; /// Wrapper type around Bytes to deserialize/serialize "0x" prefixed ethereum hex strings -#[derive(Clone, Default, PartialEq, Eq, Hash, Serialize, Deserialize, Ord, PartialOrd)] +#[main_codec] +#[derive(Clone, Default, PartialEq, Eq, Hash, Ord, PartialOrd)] pub struct Bytes( #[serde(serialize_with = "serialize_bytes", deserialize_with = "deserialize_bytes")] pub bytes::Bytes, diff --git a/crates/primitives/src/transaction/access_list.rs b/crates/primitives/src/transaction/access_list.rs index 2ca16ade2..098b7167c 100644 --- a/crates/primitives/src/transaction/access_list.rs +++ b/crates/primitives/src/transaction/access_list.rs @@ -1,13 +1,12 @@ +use reth_codecs::main_codec; use reth_rlp::{RlpDecodable, RlpDecodableWrapper, RlpEncodable, RlpEncodableWrapper}; -use serde::{Deserialize, Serialize}; 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, Hash, Default, RlpDecodable, RlpEncodable, Serialize, Deserialize, -)] +#[main_codec] +#[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, @@ -16,16 +15,6 @@ pub struct AccessListItem { } /// AccessList as defined in EIP-2930 -#[derive( - Clone, - Debug, - PartialEq, - Eq, - Hash, - Default, - RlpDecodableWrapper, - RlpEncodableWrapper, - Serialize, - Deserialize, -)] +#[main_codec] +#[derive(Clone, Debug, PartialEq, Eq, Hash, Default, RlpDecodableWrapper, RlpEncodableWrapper)] pub struct AccessList(pub Vec); diff --git a/crates/primitives/src/transaction/mod.rs b/crates/primitives/src/transaction/mod.rs index f995c6cd6..71ebe505c 100644 --- a/crates/primitives/src/transaction/mod.rs +++ b/crates/primitives/src/transaction/mod.rs @@ -6,6 +6,7 @@ use crate::{Address, Bytes, TxHash, U256}; pub use access_list::{AccessList, AccessListItem}; use bytes::Buf; use ethers_core::utils::keccak256; +use reth_codecs::main_codec; use reth_rlp::{length_of_length, Decodable, DecodeError, Encodable, Header, EMPTY_STRING_CODE}; pub use signature::Signature; use std::ops::Deref; @@ -13,6 +14,7 @@ pub use tx_type::TxType; /// Raw Transaction. /// Transaction type is introduced in EIP-2718: https://eips.ethereum.org/EIPS/eip-2718 +#[main_codec] #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum Transaction { /// Legacy transaciton. @@ -388,6 +390,7 @@ impl Encodable for Transaction { } /// Whether or not the transaction is a contract creation. +#[main_codec] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum TransactionKind { /// A transaction that creates a contract. @@ -428,11 +431,15 @@ impl Decodable for TransactionKind { } /// Signed transaction. +#[main_codec] #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct TransactionSigned { - transaction: Transaction, - hash: TxHash, - signature: Signature, + /// Raw transaction info + pub transaction: Transaction, + /// Transaction hash + pub hash: TxHash, + /// The transaction signature values + pub signature: Signature, } impl AsRef for TransactionSigned { @@ -456,6 +463,7 @@ impl Encodable for TransactionSigned { // add the length of the RLP header len + length_of_length(len) } + fn encode(&self, out: &mut dyn bytes::BufMut) { if let Transaction::Legacy { chain_id, .. } = self.transaction { let header = Header { list: true, payload_length: self.payload_len() }; diff --git a/crates/primitives/src/transaction/signature.rs b/crates/primitives/src/transaction/signature.rs index bfbb67495..fe8f2b035 100644 --- a/crates/primitives/src/transaction/signature.rs +++ b/crates/primitives/src/transaction/signature.rs @@ -1,3 +1,4 @@ +use reth_codecs::main_codec; use reth_rlp::{Decodable, DecodeError, Encodable}; use crate::U256; @@ -5,6 +6,7 @@ use crate::U256; /// r, s: Values corresponding to the signature of the /// transaction and used to determine the sender of /// the transaction; formally Tr and Ts. This is expanded in Appendix F of yellow paper. +#[main_codec] #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Signature { /// The R field of the signature; the point on the curve.