Skip to content

Commit

Permalink
feat: rename headers to block_headers
Browse files Browse the repository at this point in the history
Co-authored-by: Krisztian Kovacs <krisztian@equilibrium.co>

Review changes and fixes.
  • Loading branch information
Mirko-von-Leipzig committed Aug 10, 2023
1 parent 5b64082 commit e8dc9c8
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 49 deletions.
15 changes: 8 additions & 7 deletions crates/p2p_proto/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,14 @@ pub enum ExecutionStatus {

impl TryFromProtobuf<i32> for ExecutionStatus {
fn try_from_protobuf(input: i32, field_name: &'static str) -> Result<Self, std::io::Error> {
match input {
0 => Ok(ExecutionStatus::Succeeded),
1 => Ok(ExecutionStatus::Reverted),
_ => Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!("Failed to parse {field_name}: missing txn field"),
)),
let status = proto::common::ExecutionStatus::from_i32(input).ok_or(std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!("Failed to convert protobuf output for {field_name}: {input} did not match any known execution status"),
))?;

match status {
proto::common::ExecutionStatus::Succeeded => Ok(Self::Succeeded),
proto::common::ExecutionStatus::Reverted => Ok(Self::Reverted),
}
}
}
Expand Down
Binary file modified crates/rpc/fixtures/mainnet.sqlite
Binary file not shown.
188 changes: 188 additions & 0 deletions crates/rpc/src/j2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
// #![allow(dead_code)]

// use std::collections::HashMap;
// use std::marker::PhantomData;

// use futures::Future;
// use serde::de::DeserializeOwned;
// use serde_json::Value;

// use crate::context::RpcContext;

// #[derive(Default)]
// struct MethodRouter {
// methods: HashMap<&'static str, Box<dyn Method>>,
// }

// impl MethodRouter {
// fn register<I>(
// mut self,
// method_name: &'static str,
// method: impl IntoMethod<I> + 'static,
// ) -> Self {
// self.methods.insert(method_name, method.into_method());
// self
// }

// async fn invoke(
// &self,
// method: &str,
// input: Value,
// state: RpcContext,
// ) -> Result<Value, crate::jsonrpc::RpcError> {
// match self.methods.get(method) {
// Some(method) => method.invoke(input, state).await.map_err(Into::into),
// None => Err(crate::jsonrpc::RpcError::MethodNotFound {
// method: method.to_owned(),
// }),
// }
// }
// }

// #[axum::async_trait]
// trait Method {
// async fn invoke(
// &self,
// input: Value,
// state: RpcContext,
// ) -> Result<Value, crate::error::RpcError>;
// }

// trait IntoMethod<I> {
// fn into_method(self) -> Box<dyn Method>;
// }

// /// impl for
// /// () -> T
// /// input only
// /// state only
// /// input and state



// impl<'de, Func, Fut, Input> IntoMethod<((), Input)> for Func
// where
// Func: Fn(Input) -> Fut + Clone + 'static + std::marker::Sync + std::marker::Send,
// Fut: Future<Output = u32> + std::marker::Send + 'static,
// Input: DeserializeOwned + std::marker::Sync + std::marker::Send + 'static,
// {
// fn into_method(self) -> Box<dyn Method> {
// struct HandlerImpl<Func, Fut, Input>
// where
// Func: Fn(Input) -> Fut + Clone,
// Fut: Future<Output = u32>,
// {
// f: Func,
// _marker: PhantomData<Input>,
// }

// #[axum::async_trait]
// impl<'de, Func, Fut, Input> Method for HandlerImpl<Func, Fut, Input>
// where
// Func: Fn(Input) -> Fut + Clone + 'static + std::marker::Sync + std::marker::Send,
// Fut: Future<Output = u32> + std::marker::Send,
// Input: DeserializeOwned + std::marker::Sync + std::marker::Send + 'static,
// {
// async fn invoke(&self, input: Value) -> u32 {
// let input: Input = serde_json::from_value(input).unwrap();
// let f = self.f.clone();
// async move { f(input).await }.await
// }
// }

// Box::new(HandlerImpl {
// f: self,
// _marker: Default::default(),
// }) as Box<dyn Method>
// }
// }

// impl<Func, Fut> IntoMethod<u32> for Func
// where
// Func: Fn(u32) -> Fut + Clone + 'static + std::marker::Sync + std::marker::Send,
// Fut: Future<Output = u32> + std::marker::Send + 'static,
// {
// fn into_method(self) -> Box<dyn Method> {
// struct HandlerImpl<Func, Fut>
// where
// Func: Fn(u32) -> Fut + Clone,
// Fut: Future<Output = u32>,
// {
// f: Func,
// }

// #[derive(serde::Deserialize)]
// struct Input(u32);

// #[axum::async_trait]
// impl<Func, Fut> Method for HandlerImpl<Func, Fut>
// where
// Func: Fn(u32) -> Fut + Clone + std::marker::Sync + std::marker::Send,
// Fut: Future<Output = u32> + std::marker::Send,
// {
// async fn invoke(&self, input: Value) -> u32 {
// let input: Input = serde_json::from_value(input).unwrap();
// let f = self.f.clone();
// f(input.0).await
// }
// }

// Box::new(HandlerImpl { f: self }) as Box<dyn Method>
// }
// }

// impl<Func> IntoMethod<()> for Func
// where
// Func: Fn() -> Value + Clone + 'static + std::marker::Sync,
// {
// fn into_method(self) -> Box<dyn Method> {
// struct HandlerImpl<Func: Fn() -> u32 + Clone> {
// f: Func,
// }

// #[axum::async_trait]
// impl<Func: Fn() -> u32 + Clone + std::marker::Sync> Method for HandlerImpl<Func> {
// async fn invoke(&self, _input: Value) -> u32 {
// let f = self.f.clone();
// f()
// }
// }

// Box::new(HandlerImpl { f: self }) as Box<dyn Method>
// }
// }

// #[tokio::test]
// async fn feature() {
// async fn echo(x: u32) -> u32 {
// x
// }

// fn always_10() -> u32 {
// 10
// }

// async fn double(x: u32) -> u32 {
// x * 2
// }

// #[derive(serde::Deserialize)]
// struct In(u32);

// async fn echo_serde(input: In) -> u32 {
// input.0
// }

// let router = MethodRouter::default()
// .register::<u32>("echo", echo)
// .register("always_10", always_10)
// .register::<u32>("double", double)
// .register("echo_serde", echo_serde);

// let input = Value::Number(20.into());

// assert_eq!(router.invoke("echo_serde", input.clone()).await, 20);
// assert_eq!(router.invoke("echo", input.clone()).await, 20);
// assert_eq!(router.invoke("always_10", input.clone()).await, 10);
// assert_eq!(router.invoke("double", input).await, 20 * 2);
// }
15 changes: 9 additions & 6 deletions crates/storage/src/connection/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub(super) fn insert_block_header(

// Insert the header
tx.inner().execute(
r"INSERT INTO headers
r"INSERT INTO block_headers
( 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! {
Expand Down Expand Up @@ -104,8 +104,11 @@ 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])
.context("Deleting block from headers table")?;
.execute(
"DELETE FROM block_headers WHERE number = ?",
params![&block],
)
.context("Deleting block from block_headers table")?;

Ok(())
}
Expand Down Expand Up @@ -173,7 +176,7 @@ pub(super) fn block_header(
block: BlockId,
) -> anyhow::Result<Option<BlockHeader>> {
// TODO: is LEFT JOIN reasonable? It's required because version ID can be null for non-existent versions.
const BASE_SQL: &str = "SELECT * FROM headers LEFT JOIN starknet_versions ON headers.version_id = starknet_versions.id";
const BASE_SQL: &str = "SELECT * FROM block_headers LEFT JOIN starknet_versions ON block_headers.version_id = starknet_versions.id";
let sql = match block {
BlockId::Latest => format!("{BASE_SQL} ORDER BY number DESC LIMIT 1"),
BlockId::Number(_) => format!("{BASE_SQL} WHERE number = ?"),
Expand Down Expand Up @@ -235,7 +238,7 @@ pub(super) fn block_header(
let parent_hash = tx
.inner()
.query_row(
"SELECT hash FROM headers WHERE number = ?",
"SELECT hash FROM block_headers WHERE number = ?",
params![&(header.number - 1)],
|row| row.get_block_hash(0),
)
Expand Down Expand Up @@ -388,7 +391,7 @@ mod tests {

// Overwrite the commitment fields to NULL.
tx.inner().execute(
r"UPDATE headers
r"UPDATE block_headers
SET transaction_commitment=NULL, event_commitment=NULL, class_commitment=NULL, version_id=NULL
WHERE number=?",
params![&target.number],
Expand Down
4 changes: 2 additions & 2 deletions crates/storage/src/connection/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,15 @@ pub(super) fn get_events<K: KeyFilter>(

let base_query = r#"SELECT
block_number,
headers.hash as block_hash,
block_headers.hash as block_hash,
transaction_hash,
starknet_transactions.idx as transaction_idx,
from_address,
data,
starknet_events.keys as keys
FROM starknet_events
INNER JOIN starknet_transactions ON (starknet_transactions.hash = starknet_events.transaction_hash)
INNER JOIN headers ON (headers.number = starknet_events.block_number)"#;
INNER JOIN block_headers ON (block_headers.number = starknet_events.block_number)"#;

let (mut base_query, mut params) = event_query(
base_query,
Expand Down
4 changes: 2 additions & 2 deletions crates/storage/src/connection/state_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ fn block_details(
) -> anyhow::Result<Option<(BlockNumber, BlockHash, StateCommitment, StateCommitment)>> {
use const_format::formatcp;

const PREFIX: &str = r"SELECT b1.number, b1.hash, b1.storage_commitment, b1.class_commitment, b2.storage_commitment, b2.class_commitment FROM headers b1
LEFT OUTER JOIN headers b2 ON b2.number = b1.number - 1";
const PREFIX: &str = r"SELECT b1.number, b1.hash, b1.storage_commitment, b1.class_commitment, b2.storage_commitment, b2.class_commitment FROM block_headers b1
LEFT OUTER JOIN block_headers b2 ON b2.number = b1.number - 1";

const LATEST: &str = formatcp!("{PREFIX} ORDER BY b1.number DESC LIMIT 1");
const NUMBER: &str = formatcp!("{PREFIX} WHERE b1.number = ?");
Expand Down
2 changes: 1 addition & 1 deletion crates/storage/src/connection/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ pub(super) fn transaction_count(tx: &Transaction<'_>, block: BlockId) -> anyhow:
.inner()
.query_row(
"SELECT COUNT(*) FROM starknet_transactions
JOIN headers ON starknet_transactions.block_hash = headers.hash
JOIN block_headers ON starknet_transactions.block_hash = block_headers.hash
WHERE number = ?1",
params![&number],
|row| row.get(0),
Expand Down
34 changes: 17 additions & 17 deletions crates/storage/src/schema/revision_0039.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,63 @@ use anyhow::Context;
use rusqlite::OptionalExtension;
use stark_hash::Felt;

/// This migration renames the starknet_blocks to headers and adds the state_commitment,
/// This migration renames the starknet_blocks to block_headers and adds the state_commitment,
/// transaction_count and event_count columns, and also renames the root column to storage_commitment.
pub(crate) fn migrate(tx: &rusqlite::Transaction<'_>) -> anyhow::Result<()> {
tracing::info!("Refactoring the headers table, this may take a while");
tracing::info!("Refactoring the block_headers table, this may take a while");

tx.execute("ALTER TABLE starknet_blocks RENAME TO headers", [])
.context("Renaming starknet_blocks table to headers")?;
tx.execute("ALTER TABLE starknet_blocks RENAME TO block_headers", [])
.context("Renaming starknet_blocks table to block_headers")?;

tx.execute(
"ALTER TABLE headers RENAME COLUMN root TO storage_commitment",
"ALTER TABLE block_headers RENAME COLUMN root TO storage_commitment",
[],
)
.context("Renaming headers.root table to headers.storage_commitment")?;
.context("Renaming block_headers.root column to block_headers.storage_commitment")?;

tx.execute(
"ALTER TABLE headers ADD COLUMN state_commitment BLOB NOT NULL DEFAULT x'0000000000000000000000000000000000000000000000000000000000000000'",
"ALTER TABLE block_headers ADD COLUMN state_commitment BLOB NOT NULL DEFAULT x'0000000000000000000000000000000000000000000000000000000000000000'",
[],
)
.context("Adding state_commitment column")?;

tx.execute(
"ALTER TABLE headers ADD COLUMN transaction_count INTEGER NOT NULL DEFAULT 0",
"ALTER TABLE block_headers ADD COLUMN transaction_count INTEGER NOT NULL DEFAULT 0",
[],
)
.context("Adding transaction_count column")?;

tx.execute(
"ALTER TABLE headers ADD COLUMN event_count INTEGER NOT NULL DEFAULT 0",
"ALTER TABLE block_headers ADD COLUMN event_count INTEGER NOT NULL DEFAULT 0",
[],
)
.context("Adding event_count column")?;

tx.execute(
r"UPDATE headers SET transaction_count = (
SELECT COUNT(1) FROM starknet_transactions WHERE starknet_transactions.block_hash = headers.hash
r"UPDATE block_headers SET transaction_count = (
SELECT COUNT(1) FROM starknet_transactions WHERE starknet_transactions.block_hash = block_headers.hash
)",
[],
)
.context("Setting tx counts")?;

tx.execute(
r"UPDATE headers SET event_count = (
SELECT COUNT(1) FROM starknet_events WHERE starknet_events.block_number = headers.number
r"UPDATE block_headers SET event_count = (
SELECT COUNT(1) FROM starknet_events WHERE starknet_events.block_number = block_headers.number
)",
[],
)
.context("Setting event counts")?;

tx.execute(
r"UPDATE headers SET state_commitment = storage_commitment WHERE class_commitment IS NULL OR class_commitment = x'0000000000000000000000000000000000000000000000000000000000000000'",
r"UPDATE block_headers SET state_commitment = storage_commitment WHERE class_commitment IS NULL OR class_commitment = x'0000000000000000000000000000000000000000000000000000000000000000'",
[],
)
.context("Setting state_commitment = storage_commitment")?;

let Some(start): Option<i64> = tx
.query_row(
"SELECT number FROM headers WHERE state_commitment = x'0000000000000000000000000000000000000000000000000000000000000000' ORDER BY number ASC LIMIT 1",
"SELECT number FROM block_headers WHERE state_commitment = x'0000000000000000000000000000000000000000000000000000000000000000' ORDER BY number ASC LIMIT 1",
[],
|row| row.get(0),
)
Expand All @@ -69,11 +69,11 @@ pub(crate) fn migrate(tx: &rusqlite::Transaction<'_>) -> anyhow::Result<()> {

let mut reader = tx
.prepare(
"SELECT number, storage_commitment, class_commitment FROM headers WHERE number >= ?",
"SELECT number, storage_commitment, class_commitment FROM block_headers WHERE number >= ?",
)
.context("Preparing commitment reader statement")?;
let mut writer = tx
.prepare("UPDATE headers SET state_commitment = ? WHERE number = ?")
.prepare("UPDATE block_headers SET state_commitment = ? WHERE number = ?")
.context("Preparing commitment writer statement")?;

let rows = reader
Expand Down
Loading

0 comments on commit e8dc9c8

Please sign in to comment.