Skip to content

Commit

Permalink
feat: use new header columns
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirko-von-Leipzig committed Jul 28, 2023
1 parent aa2cbd1 commit f07485a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
12 changes: 12 additions & 0 deletions crates/common/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub struct BlockHeader {
pub state_commitment: StateCommitment,
pub storage_commitment: StorageCommitment,
pub transaction_commitment: TransactionCommitment,
pub transaction_count: usize,
pub event_count: usize,
}

pub struct BlockHeaderBuilder(BlockHeader);
Expand Down Expand Up @@ -112,6 +114,16 @@ impl BlockHeaderBuilder {
self
}

pub fn with_transaction_count(mut self, transaction_count: usize) -> Self {
self.0.transaction_count = transaction_count;
self
}

pub fn with_event_count(mut self, event_count: usize) -> Self {
self.0.event_count = event_count;
self
}

pub fn finalize_with_hash(mut self, hash: BlockHash) -> BlockHeader {
self.0.hash = hash;
self.0
Expand Down
2 changes: 2 additions & 0 deletions crates/pathfinder/src/p2p_network/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub mod conv {
state_commitment: StateCommitment(header.state_commitment),
storage_commitment: StorageCommitment(header.storage_commitment),
transaction_commitment: TransactionCommitment(header.transaction_commitment),
transaction_count: header.transaction_count as usize,
event_count: header.event_count as usize,
})
}
}
Expand Down
9 changes: 9 additions & 0 deletions crates/pathfinder/src/state/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,13 @@ async fn l2_update(
"State root mismatch"
);

let transaction_count = block.transactions.len();
let event_count = block
.transaction_receipts
.iter()
.map(|r| r.events.len())
.sum();

// Update L2 database. These types shouldn't be options at this level,
// but for now the unwraps are "safe" in that these should only ever be
// None for pending queries to the sequencer, but we aren't using those here.
Expand All @@ -677,6 +684,8 @@ async fn l2_update(
state_commitment,
storage_commitment,
transaction_commitment,
transaction_count,
event_count,
};

transaction
Expand Down
32 changes: 16 additions & 16 deletions crates/storage/src/connection/block.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Context;
use pathfinder_common::{BlockHash, BlockHeader, BlockNumber, StarknetVersion, StateCommitment};
use pathfinder_common::{BlockHash, BlockHeader, BlockNumber, StarknetVersion};

use crate::{prelude::*, BlockId};

Expand All @@ -14,8 +14,8 @@ pub(super) fn insert_block_header(
// Insert the header
tx.inner().execute(
r"INSERT INTO headers
( number, hash, storage_commitment, timestamp, gas_price, sequencer_address, version_id, transaction_commitment, event_commitment, class_commitment)
VALUES (:number, :hash, :storage_commitment, :timestamp, :gas_price, :sequencer_address, :version_id, :transaction_commitment, :event_commitment, :class_commitment)",
( number, hash, storage_commitment, timestamp, gas_price, sequencer_address, version_id, transaction_commitment, event_commitment, state_commitment, class_commitment, transaction_count, event_count)
VALUES (:number, :hash, :storage_commitment, :timestamp, :gas_price, :sequencer_address, :version_id, :transaction_commitment, :event_commitment, :state_commitment, :class_commitment, :transaction_count, :event_count)",
named_params! {
":number": &header.number,
":hash": &header.hash,
Expand All @@ -27,6 +27,9 @@ pub(super) fn insert_block_header(
":transaction_commitment": &header.transaction_commitment,
":event_commitment": &header.event_commitment,
":class_commitment": &header.class_commitment,
":transaction_count": &header.transaction_count,
":event_count": &header.event_count,
":state_commitment": &header.state_commitment,
},
).context("Inserting block header")?;

Expand Down Expand Up @@ -101,10 +104,7 @@ pub(super) fn purge_block(tx: &Transaction<'_>, block: BlockNumber) -> anyhow::R
.context("Deleting block from canonical_blocks table")?;

tx.inner()
.execute(
"DELETE FROM headers WHERE number = ?",
params![&block],
)
.execute("DELETE FROM headers WHERE number = ?", params![&block])
.context("Deleting block from headers table")?;

Ok(())
Expand Down Expand Up @@ -191,10 +191,9 @@ pub(super) fn block_header(
let event_commitment = row.get_event_commitment("event_commitment")?;
let class_commitment = row.get_class_commitment("class_commitment")?;
let starknet_version = row.get_starknet_version("version")?;

// TODO: this really needs to get stored instead.
let state_commitment = StateCommitment::calculate(storage_commitment, class_commitment);
// TODO: test what happens when a field is null.
let event_count: usize = row.get("event_count")?;
let transaction_count: usize = row.get("transaction_count")?;
let state_commitment = row.get_state_commitment("state_commitment")?;

let header = BlockHeader {
hash,
Expand All @@ -208,6 +207,8 @@ pub(super) fn block_header(
storage_commitment,
transaction_commitment,
starknet_version,
transaction_count,
event_count,
// TODO: store block hash in-line.
// This gets filled in by a separate query, but really should get stored as a column in
// order to support truncated history.
Expand Down Expand Up @@ -262,8 +263,8 @@ pub(super) fn block_is_l1_accepted(tx: &Transaction<'_>, block: BlockId) -> anyh
mod tests {
use pathfinder_common::macro_prelude::*;
use pathfinder_common::{
BlockTimestamp, ClassCommitment, ClassHash, EventCommitment, GasPrice, StateUpdate,
TransactionCommitment,
BlockTimestamp, ClassCommitment, ClassHash, EventCommitment, GasPrice, StateCommitment,
StateUpdate, TransactionCommitment,
};

use super::*;
Expand Down Expand Up @@ -292,10 +293,11 @@ mod tests {
starknet_version: StarknetVersion::default(),
class_commitment,
event_commitment: event_commitment_bytes!(b"event commitment genesis"),
// This needs to be calculated as this value is never actually stored directly.
state_commitment: StateCommitment::calculate(storage_commitment, class_commitment),
storage_commitment,
transaction_commitment: transaction_commitment_bytes!(b"tx commitment genesis"),
transaction_count: 37,
event_count: 40,
};
let header1 = genesis
.child_builder()
Expand Down Expand Up @@ -398,8 +400,6 @@ mod tests {
expected.transaction_commitment = TransactionCommitment::ZERO;
expected.event_commitment = EventCommitment::ZERO;
expected.class_commitment = ClassCommitment::ZERO;
expected.state_commitment =
StateCommitment::calculate(expected.storage_commitment, expected.class_commitment);

let result = tx.block_header(target.number.into()).unwrap().unwrap();

Expand Down
2 changes: 1 addition & 1 deletion py/src/pathfinder_worker/call.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@


# used from tests, and the query which asserts that the schema is of expected version.
EXPECTED_SCHEMA_REVISION = 38
EXPECTED_SCHEMA_REVISION = 39
EXPECTED_CAIRO_VERSION = "0.12.1a0"

# this is set by pathfinder automatically when #[cfg(debug_assertions)]
Expand Down

0 comments on commit f07485a

Please sign in to comment.