Skip to content

Commit

Permalink
Merge pull request #1883 from eqlabs/sistemd/bincode-instead-of-json
Browse files Browse the repository at this point in the history
feat(db): use bincode for `starknet_transactions`
  • Loading branch information
sistemd authored Mar 25, 2024
2 parents 89e4b17 + f46d11a commit cbe4de5
Show file tree
Hide file tree
Showing 33 changed files with 2,698 additions and 1,047 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ name: CI
on:
workflow_dispatch:
push:
pull_request:
# Daily cron job used to clear and rebuild cache (https://github.com/Swatinem/rust-cache/issues/181).
schedule:
- cron: '0 0 * * *'
Expand Down
24 changes: 23 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/common/src/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::prelude::*;

#[derive(Clone, Default, Debug, PartialEq, Eq)]
pub struct Receipt {
pub actual_fee: Option<Fee>,
pub actual_fee: Fee,
pub execution_resources: ExecutionResources,
pub l2_to_l1_messages: Vec<L2ToL1Message>,
pub execution_status: ExecutionStatus,
Expand Down
25 changes: 10 additions & 15 deletions crates/common/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Transaction {
TransactionVariant::DeclareV2(_) => TransactionVersion::TWO,
TransactionVariant::DeclareV3(_) => TransactionVersion::THREE,
TransactionVariant::Deploy(tx) => tx.version,
TransactionVariant::DeployAccountV0V1(tx) => tx.version,
TransactionVariant::DeployAccountV1(_) => TransactionVersion::ONE,
TransactionVariant::DeployAccountV3(_) => TransactionVersion::THREE,
TransactionVariant::InvokeV0(_) => TransactionVersion::ZERO,
TransactionVariant::InvokeV1(_) => TransactionVersion::ONE,
Expand All @@ -46,10 +46,7 @@ pub enum TransactionVariant {
DeclareV2(DeclareTransactionV2),
DeclareV3(DeclareTransactionV3),
Deploy(DeployTransaction),
// FIXME: This should get separated into v0 and v1 variants.
// Currently this allows for ambiguity as version is
// flexible.
DeployAccountV0V1(DeployAccountTransactionV0V1),
DeployAccountV1(DeployAccountTransactionV1),
DeployAccountV3(DeployAccountTransactionV3),
InvokeV0(InvokeTransactionV0),
InvokeV1(InvokeTransactionV1),
Expand Down Expand Up @@ -86,7 +83,7 @@ impl TransactionVariant {
TransactionVariant::DeclareV2(tx) => tx.calculate_hash(chain_id, query_only),
TransactionVariant::DeclareV3(tx) => tx.calculate_hash(chain_id, query_only),
TransactionVariant::Deploy(tx) => tx.calculate_hash(chain_id),
TransactionVariant::DeployAccountV0V1(tx) => tx.calculate_hash(chain_id, query_only),
TransactionVariant::DeployAccountV1(tx) => tx.calculate_hash(chain_id, query_only),
TransactionVariant::DeployAccountV3(tx) => tx.calculate_hash(chain_id, query_only),
TransactionVariant::InvokeV0(tx) => tx.calculate_hash(chain_id, query_only),
TransactionVariant::InvokeV1(tx) => tx.calculate_hash(chain_id, query_only),
Expand Down Expand Up @@ -124,9 +121,9 @@ impl From<DeployTransaction> for TransactionVariant {
Self::Deploy(value)
}
}
impl From<DeployAccountTransactionV0V1> for TransactionVariant {
fn from(value: DeployAccountTransactionV0V1) -> Self {
Self::DeployAccountV0V1(value)
impl From<DeployAccountTransactionV1> for TransactionVariant {
fn from(value: DeployAccountTransactionV1) -> Self {
Self::DeployAccountV1(value)
}
}
impl From<DeployAccountTransactionV3> for TransactionVariant {
Expand Down Expand Up @@ -199,10 +196,9 @@ pub struct DeployTransaction {
}

#[derive(Clone, Default, Debug, PartialEq, Eq)]
pub struct DeployAccountTransactionV0V1 {
pub struct DeployAccountTransactionV1 {
pub contract_address: ContractAddress,
pub max_fee: Fee,
pub version: TransactionVersion,
pub signature: Vec<TransactionSignatureElem>,
pub nonce: TransactionNonce,
pub contract_address_salt: ContractAddressSalt,
Expand Down Expand Up @@ -377,7 +373,7 @@ impl DeployTransaction {
}
}

impl DeployAccountTransactionV0V1 {
impl DeployAccountTransactionV1 {
fn calculate_hash(&self, chain_id: ChainId, query_only: bool) -> TransactionHash {
let constructor_calldata_hash = std::iter::once(self.class_hash.0)
.chain(std::iter::once(self.contract_address_salt.0))
Expand All @@ -389,7 +385,7 @@ impl DeployAccountTransactionV0V1 {

PreV3Hasher {
prefix: felt_bytes!(b"deploy_account"),
version: self.version.with_query_only(query_only),
version: TransactionVersion::ONE.with_query_only(query_only),
address: self.contract_address,
data_hash: constructor_calldata_hash,
max_fee: self.max_fee,
Expand Down Expand Up @@ -973,12 +969,11 @@ mod tests {
hash: transaction_hash!(
"0x63b72dba5a1b5cdd2585b0c7103242244860453f7013023c1a21f32e1863ec"
),
variant: TransactionVariant::DeployAccountV0V1(DeployAccountTransactionV0V1 {
variant: TransactionVariant::DeployAccountV1(DeployAccountTransactionV1 {
contract_address: contract_address!(
"0x3faed8332496d9de9c546e7942b35ba3ea323a6af72d6033f746ea60ecc02ef"
),
max_fee: fee!("0xb48040809d4b"),
version: TransactionVersion::ONE,
signature: vec![
transaction_signature_elem!(
"0x463d21c552a810c59be86c336c0cc68f28e3815eafbe1a2eaf9b3a6fe1c2b82"
Expand Down
17 changes: 9 additions & 8 deletions crates/gateway-types/src/reply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ pub(crate) mod transaction {
};

Self {
actual_fee,
actual_fee: Some(actual_fee),
events,
execution_resources: Some(execution_resources.into()),
l1_to_l2_consumed_message: None,
Expand Down Expand Up @@ -712,7 +712,7 @@ pub(crate) mod transaction {

(
common::Receipt {
actual_fee,
actual_fee: actual_fee.unwrap_or_default(),
execution_resources: execution_resources.unwrap_or_default().into(),
l2_to_l1_messages: l2_to_l1_messages.into_iter().map(Into::into).collect(),
transaction_hash,
Expand Down Expand Up @@ -1048,10 +1048,9 @@ pub(crate) mod transaction {
transaction_hash,
version,
}),
DeployAccountV0V1(DeployAccountTransactionV0V1 {
DeployAccountV1(DeployAccountTransactionV1 {
contract_address,
max_fee,
version,
signature,
nonce,
contract_address_salt,
Expand All @@ -1062,7 +1061,7 @@ pub(crate) mod transaction {
contract_address,
transaction_hash,
max_fee,
version,
version: TransactionVersion::ONE,
signature,
nonce,
contract_address_salt,
Expand Down Expand Up @@ -1282,18 +1281,20 @@ pub(crate) mod transaction {
constructor_calldata,
class_hash,
},
)) => TransactionVariant::DeployAccountV0V1(
pathfinder_common::transaction::DeployAccountTransactionV0V1 {
)) if version == TransactionVersion::ONE => TransactionVariant::DeployAccountV1(
pathfinder_common::transaction::DeployAccountTransactionV1 {
contract_address,
max_fee,
version,
signature,
nonce,
contract_address_salt,
constructor_calldata,
class_hash,
},
),
Transaction::DeployAccount(DeployAccountTransaction::V0V1(
DeployAccountTransactionV0V1 { version, .. },
)) => panic!("unexpected deploy account transaction version {version:?}"),
Transaction::DeployAccount(DeployAccountTransaction::V3(
DeployAccountTransactionV3 {
nonce,
Expand Down
9 changes: 4 additions & 5 deletions crates/p2p/src/client/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use pathfinder_common::{
state_update::StateUpdateCounts,
transaction::{
DataAvailabilityMode, DeclareTransactionV0V1, DeclareTransactionV2, DeclareTransactionV3,
DeployAccountTransactionV0V1, DeployAccountTransactionV3, DeployTransaction,
DeployAccountTransactionV1, DeployAccountTransactionV3, DeployTransaction,
InvokeTransactionV0, InvokeTransactionV1, InvokeTransactionV3, L1HandlerTransaction,
ResourceBound, ResourceBounds, Transaction, TransactionVariant,
},
Expand Down Expand Up @@ -93,7 +93,7 @@ impl TryFromDto<p2p_proto::transaction::TransactionVariant> for TransactionVaria
/// ## Important
///
/// This conversion does not compute deployed contract address for deploy account transactions
/// ([`TransactionVariant::DeployAccountV0V1`] and [`TransactionVariant::DeployAccountV3`]),
/// ([`TransactionVariant::DeployAccountV1`] and [`TransactionVariant::DeployAccountV3`]),
/// filling it with a zero address instead. The caller is responsible for performing the computation after the conversion succeeds.
fn try_from_dto(dto: p2p_proto::transaction::TransactionVariant) -> anyhow::Result<Self>
where
Expand Down Expand Up @@ -172,10 +172,9 @@ impl TryFromDto<p2p_proto::transaction::TransactionVariant> for TransactionVaria
_ => anyhow::bail!("Invalid deploy transaction version"),
},
}),
DeployAccountV1(x) => Self::DeployAccountV0V1(DeployAccountTransactionV0V1 {
DeployAccountV1(x) => Self::DeployAccountV1(DeployAccountTransactionV1 {
contract_address: ContractAddress::ZERO,
max_fee: Fee(x.max_fee),
version: TransactionVersion::ONE,
signature: x
.signature
.parts
Expand Down Expand Up @@ -275,7 +274,7 @@ impl TryFromDto<(p2p_proto::receipt::Receipt, TransactionIndex)> for Receipt {
| Deploy(DeployTransactionReceipt { common, .. })
| DeployAccount(DeployAccountTransactionReceipt { common, .. }) => Ok(Self {
transaction_hash: TransactionHash(common.transaction_hash.0),
actual_fee: Some(Fee(common.actual_fee)),
actual_fee: Fee(common.actual_fee),
execution_resources: ExecutionResources {
builtins: BuiltinCounters {
output: common.execution_resources.builtins.output.into(),
Expand Down
7 changes: 3 additions & 4 deletions crates/p2p/src/client/peer_agnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use pathfinder_common::{
BlockNumber, ClassHash, ContractAddress, ContractNonce, SignedBlockHeader, StorageAddress,
StorageValue,
};
use smallvec::SmallVec;
use tokio::{sync::RwLock, task::spawn_blocking};

use crate::client::{conv::TryFromDto, peer_aware};
Expand Down Expand Up @@ -213,7 +212,7 @@ impl Client {
mut start: BlockNumber,
stop_inclusive: BlockNumber,
getter: Arc<
impl Fn(BlockNumber, NonZeroUsize) -> anyhow::Result<SmallVec<[StateUpdateCounts; 10]>>
impl Fn(BlockNumber, NonZeroUsize) -> anyhow::Result<Vec<StateUpdateCounts>>
+ Send
+ Sync
+ 'static,
Expand Down Expand Up @@ -349,9 +348,9 @@ impl Client {
&self,
start: BlockNumber,
stop_inclusive: BlockNumber,
counts: &mut SmallVec<[StateUpdateCounts; 10]>,
counts: &mut Vec<StateUpdateCounts>,
getter: Arc<
impl Fn(BlockNumber, NonZeroUsize) -> anyhow::Result<SmallVec<[StateUpdateCounts; 10]>>
impl Fn(BlockNumber, NonZeroUsize) -> anyhow::Result<Vec<StateUpdateCounts>>
+ Send
+ Sync
+ 'static,
Expand Down
82 changes: 40 additions & 42 deletions crates/pathfinder/examples/re_execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,48 +150,46 @@ fn execute(storage: &mut Storage, chain_id: ChainId, work: Work) {
match pathfinder_executor::simulate(execution_state, transactions, false, false) {
Ok(simulations) => {
for (simulation, receipt) in simulations.iter().zip(work.receipts.iter()) {
if let Some(actual_fee) = receipt.actual_fee {
let actual_fee =
u128::from_be_bytes(actual_fee.0.to_be_bytes()[16..].try_into().unwrap());

// L1 handler transactions have a fee of zero in the receipt.
if actual_fee == 0 {
continue;
}

let estimate = &simulation.fee_estimation;

let (gas_price, data_gas_price) = match estimate.unit {
pathfinder_executor::types::PriceUnit::Wei => (
work.header.eth_l1_gas_price.0,
work.header.eth_l1_data_gas_price.0,
),
pathfinder_executor::types::PriceUnit::Fri => (
work.header.strk_l1_gas_price.0,
work.header.strk_l1_data_gas_price.0,
),
};

let actual_data_gas_consumed =
receipt.execution_resources.data_availability.l1_data_gas;
let actual_gas_consumed = (actual_fee
- actual_data_gas_consumed.saturating_mul(data_gas_price))
/ gas_price.max(1);

let estimated_gas_consumed = estimate.gas_consumed.as_u128();
let estimated_data_gas_consumed = estimate.data_gas_consumed.as_u128();

let gas_diff = actual_gas_consumed.abs_diff(estimated_gas_consumed);
let data_gas_diff =
actual_data_gas_consumed.abs_diff(estimated_data_gas_consumed);

if gas_diff > (actual_gas_consumed * 2 / 10)
|| data_gas_diff > (actual_data_gas_consumed * 2 / 10)
{
tracing::warn!(block_number=%work.header.number, transaction_hash=%receipt.transaction_hash, %estimated_gas_consumed, %actual_gas_consumed, %estimated_data_gas_consumed, %actual_data_gas_consumed, estimated_fee=%estimate.overall_fee, %actual_fee, "Estimation mismatch");
} else {
tracing::debug!(block_number=%work.header.number, transaction_hash=%receipt.transaction_hash, %estimated_gas_consumed, %actual_gas_consumed, %estimated_data_gas_consumed, %actual_data_gas_consumed, estimated_fee=%estimate.overall_fee, %actual_fee, "Estimation matches");
}
let actual_fee = u128::from_be_bytes(
receipt.actual_fee.0.to_be_bytes()[16..].try_into().unwrap(),
);

// L1 handler transactions have a fee of zero in the receipt.
if actual_fee == 0 {
continue;
}

let estimate = &simulation.fee_estimation;

let (gas_price, data_gas_price) = match estimate.unit {
pathfinder_executor::types::PriceUnit::Wei => (
work.header.eth_l1_gas_price.0,
work.header.eth_l1_data_gas_price.0,
),
pathfinder_executor::types::PriceUnit::Fri => (
work.header.strk_l1_gas_price.0,
work.header.strk_l1_data_gas_price.0,
),
};

let actual_data_gas_consumed =
receipt.execution_resources.data_availability.l1_data_gas;
let actual_gas_consumed = (actual_fee
- actual_data_gas_consumed.saturating_mul(data_gas_price))
/ gas_price.max(1);

let estimated_gas_consumed = estimate.gas_consumed.as_u128();
let estimated_data_gas_consumed = estimate.data_gas_consumed.as_u128();

let gas_diff = actual_gas_consumed.abs_diff(estimated_gas_consumed);
let data_gas_diff = actual_data_gas_consumed.abs_diff(estimated_data_gas_consumed);

if gas_diff > (actual_gas_consumed * 2 / 10)
|| data_gas_diff > (actual_data_gas_consumed * 2 / 10)
{
tracing::warn!(block_number=%work.header.number, transaction_hash=%receipt.transaction_hash, %estimated_gas_consumed, %actual_gas_consumed, %estimated_data_gas_consumed, %actual_data_gas_consumed, estimated_fee=%estimate.overall_fee, %actual_fee, "Estimation mismatch");
} else {
tracing::debug!(block_number=%work.header.number, transaction_hash=%receipt.transaction_hash, %estimated_gas_consumed, %actual_gas_consumed, %estimated_data_gas_consumed, %actual_data_gas_consumed, estimated_fee=%estimate.overall_fee, %actual_fee, "Estimation matches");
}
}
}
Expand Down
Loading

0 comments on commit cbe4de5

Please sign in to comment.