Skip to content

Commit

Permalink
Use tables macro (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzejkop authored Dec 11, 2024
1 parent b8e89bb commit 18acd94
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 23 deletions.
1 change: 1 addition & 0 deletions world-chain-builder/Cargo.lock

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

1 change: 1 addition & 0 deletions world-chain-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ reth = { git = "https://github.com/paradigmxyz/reth", rev = "ddc9bda" }
reth-cli-util = { git = "https://github.com/paradigmxyz/reth", rev = "ddc9bda" }
reth-evm = { git = "https://github.com/paradigmxyz/reth", rev = "ddc9bda" }
reth-db = { git = "https://github.com/paradigmxyz/reth", rev = "ddc9bda" }
reth-db-api = { git = "https://github.com/paradigmxyz/reth", rev = "ddc9bda" }
reth-provider = { git = "https://github.com/paradigmxyz/reth", rev = "ddc9bda", features = [
"test-utils",
] }
Expand Down
36 changes: 16 additions & 20 deletions world-chain-builder/src/pbh/db.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
use std::fmt;
use std::path::Path;
use std::sync::Arc;

use bytes::BufMut;
use reth_db::cursor::DbCursorRW;
use reth_db::mdbx::tx::Tx;
use reth_db::mdbx::{DatabaseArguments, DatabaseFlags, RW};
use reth_db::table::{Compress, Decompress, Table};
use reth_db::transaction::DbTxMut;
use reth_db::{create_db, DatabaseError};
use reth_db::{create_db, tables, DatabaseError, TableType, TableViewer};
use reth_db_api;
use revm_primitives::{FixedBytes, B256};
use semaphore::Field;
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::sync::Arc;
use tracing::info;

/// Table to store PBH validated transactions along with their nullifiers.
///
/// When a trasnaction is validated before being inserted into the pool,
/// a mapping is created from the transaction hash to the nullifier here.
/// This is primarily used as a caching mechanism to avoid certain types of
/// DoS attacks.
#[derive(Debug, Clone, Default)]
pub struct ValidatedPbhTransactionTable;

impl Table for ValidatedPbhTransactionTable {
const NAME: &'static str = "ValidatedPbhTransactions";

type Key = B256;

type Value = EmptyValue;
tables! {
/// Table to store PBH validated transactions along with their nullifiers.
///
/// When a trasnaction is validated before being inserted into the pool,
/// a mapping is created from the transaction hash to the nullifier here.
/// This is primarily used as a caching mechanism to avoid certain types of
/// DoS attacks.
table ValidatedPbhTransaction<Key = B256, Value = EmptyValue>;
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
Expand All @@ -49,7 +45,7 @@ impl Compress for EmptyValue {
/// don't forget to call db_tx.commit() at the very end
pub fn set_pbh_nullifier(db_tx: &Tx<RW>, nullifier: Field) -> Result<(), DatabaseError> {
let bytes: FixedBytes<32> = nullifier.into();
let mut cursor = db_tx.cursor_write::<ValidatedPbhTransactionTable>()?;
let mut cursor = db_tx.cursor_write::<ValidatedPbhTransaction>()?;
cursor.insert(bytes, EmptyValue)?;
Ok(())
}
Expand All @@ -72,7 +68,7 @@ pub fn load_world_chain_db(
.map_err(|e| DatabaseError::InitTx(e.into()))?;

tx.create_db(
Some(ValidatedPbhTransactionTable::NAME),
Some(ValidatedPbhTransaction::NAME),
DatabaseFlags::default(),
)
.map_err(|e| DatabaseError::CreateTable(e.into()))?;
Expand Down
6 changes: 3 additions & 3 deletions world-chain-builder/src/pool/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use super::ordering::WorldChainOrdering;
use super::root::WorldChainRootValidator;
use super::tx::{WorldChainPoolTransaction, WorldChainPooledTransaction};
use crate::pbh::date_marker::DateMarker;
use crate::pbh::db::{EmptyValue, ValidatedPbhTransactionTable};
use crate::pbh::db::{EmptyValue, ValidatedPbhTransaction};
use crate::pbh::external_nullifier::ExternalNullifier;
use crate::pbh::payload::{PbhPayload, TREE_DEPTH};

Expand Down Expand Up @@ -71,7 +71,7 @@ where

pub fn set_validated(&self, pbh_payload: &PbhPayload) -> Result<(), DatabaseError> {
let db_tx = self.pbh_db.tx_mut()?;
let mut cursor = db_tx.cursor_write::<ValidatedPbhTransactionTable>()?;
let mut cursor = db_tx.cursor_write::<ValidatedPbhTransaction>()?;
cursor.insert(pbh_payload.nullifier_hash.to_be_bytes().into(), EmptyValue)?;
db_tx.commit()?;
Ok(())
Expand Down Expand Up @@ -138,7 +138,7 @@ where
// Create db transaction and insert the nullifier hash
// We do this first to prevent repeatedly validating the same transaction
let db_tx = self.pbh_db.tx_mut()?;
let mut cursor = db_tx.cursor_write::<ValidatedPbhTransactionTable>()?;
let mut cursor = db_tx.cursor_write::<ValidatedPbhTransaction>()?;
cursor.insert(payload.nullifier_hash.to_be_bytes().into(), EmptyValue)?;

let res = verify_proof(
Expand Down

0 comments on commit 18acd94

Please sign in to comment.