Skip to content

Commit

Permalink
feat(storage): add reorg_counter
Browse files Browse the repository at this point in the history
  • Loading branch information
kkovaacs committed Jan 22, 2024
1 parent fb08760 commit 3479318
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 3 deletions.
9 changes: 9 additions & 0 deletions crates/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,15 @@ pub fn calculate_class_commitment_leaf_hash(
)
}

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub struct ReorgCounter(i64);

impl ReorgCounter {
pub fn new(value: i64) -> Self {
Self(value)
}
}

#[cfg(test)]
mod tests {
use crate::{felt, CallParam, ClassHash, ContractAddress, ContractAddressSalt};
Expand Down
10 changes: 10 additions & 0 deletions crates/storage/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ mod class;
mod ethereum;
mod event;
mod reference;
mod reorg_counter;
mod signature;
mod state_update;
mod transaction;
mod trie;

use pathfinder_common::ReorgCounter;
// Re-export this so users don't require rusqlite as a direct dep.
pub use rusqlite::TransactionBehavior;

Expand Down Expand Up @@ -509,6 +511,14 @@ impl<'inner> Transaction<'inner> {
signature::signature(self, block)
}

pub fn increment_reorg_counter(&self) -> anyhow::Result<()> {
reorg_counter::increment_reorg_counter(self)
}

pub fn reorg_counter(&self) -> anyhow::Result<ReorgCounter> {
reorg_counter::reorg_counter(self)
}

pub(self) fn inner(&self) -> &rusqlite::Transaction<'_> {
&self.0
}
Expand Down
55 changes: 55 additions & 0 deletions crates/storage/src/connection/reorg_counter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use pathfinder_common::ReorgCounter;

use crate::prelude::*;

pub(super) fn increment_reorg_counter(tx: &Transaction<'_>) -> anyhow::Result<()> {
tx.inner().execute(
"UPDATE reorg_counter SET counter=counter+1 WHERE id = 1",
[],
)?;

Ok(())
}

pub(super) fn reorg_counter(tx: &Transaction<'_>) -> anyhow::Result<ReorgCounter> {
// This table always contains exactly one row.
tx.inner()
.query_row(
"SELECT counter FROM reorg_counter WHERE id = 1",
[],
|row| row.get_reorg_counter(0),
)
.map_err(|e| e.into())
}

#[cfg(test)]
mod tests {
use crate::Storage;

use super::*;

#[test]
fn empty_is_zero() {
let storage = Storage::in_memory().unwrap();
let mut connection = storage.connection().unwrap();
let tx = connection.transaction().unwrap();

let result = reorg_counter(&tx).unwrap();
assert_eq!(result, ReorgCounter::new(0));
}

#[test]
fn increment() {
let storage = Storage::in_memory().unwrap();
let mut connection = storage.connection().unwrap();
let tx = connection.transaction().unwrap();

increment_reorg_counter(&tx).unwrap();
let result = reorg_counter(&tx).unwrap();
assert_eq!(result, ReorgCounter::new(1));

increment_reorg_counter(&tx).unwrap();
let result = reorg_counter(&tx).unwrap();
assert_eq!(result, ReorgCounter::new(2));
}
}
12 changes: 9 additions & 3 deletions crates/storage/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use pathfinder_common::{
CallParam, CallResultValue, CasmHash, ClassCommitment, ClassCommitmentLeafHash, ClassHash,
ConstructorParam, ContractAddress, ContractAddressSalt, ContractNonce, ContractRoot,
ContractStateHash, EntryPoint, EventCommitment, EventData, EventKey, Fee, GasPrice,
L1ToL2MessageNonce, L1ToL2MessagePayloadElem, L2ToL1MessagePayloadElem, SequencerAddress,
SierraHash, StarknetVersion, StateCommitment, StorageAddress, StorageCommitment, StorageValue,
TransactionCommitment, TransactionHash, TransactionNonce, TransactionSignatureElem,
L1ToL2MessageNonce, L1ToL2MessagePayloadElem, L2ToL1MessagePayloadElem, ReorgCounter,
SequencerAddress, SierraHash, StarknetVersion, StateCommitment, StorageAddress,
StorageCommitment, StorageValue, TransactionCommitment, TransactionHash, TransactionNonce,
TransactionSignatureElem,
};
use pathfinder_crypto::Felt;
use rusqlite::types::{FromSqlError, ToSqlOutput};
Expand Down Expand Up @@ -261,6 +262,11 @@ pub trait RowExt {
Ok(addr)
}

fn get_reorg_counter<Index: RowIndex>(&self, index: Index) -> rusqlite::Result<ReorgCounter> {
let num = self.get_i64(index)?;
Ok(ReorgCounter::new(num))
}

row_felt_wrapper!(get_block_hash, BlockHash);
row_felt_wrapper!(get_casm_hash, CasmHash);
row_felt_wrapper!(get_class_hash, ClassHash);
Expand Down
10 changes: 10 additions & 0 deletions crates/storage/src/schema/revision_0046.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,15 @@ pub(crate) fn migrate(tx: &rusqlite::Transaction<'_>) -> anyhow::Result<()> {
)
.context("Dropping starknet_events table")?;

tracing::debug!("Creating reorg counter table");
tx.execute_batch(
"CREATE TABLE reorg_counter (
id INTEGER NOT NULL PRIMARY KEY,
counter INTEGER NOT NULL
);
INSERT INTO reorg_counter (id, counter) VALUES (1, 0);",
)
.context("Creating reorg counter table")?;

Ok(())
}

0 comments on commit 3479318

Please sign in to comment.