From 18acd94f46a8c78a3f2b8ebd42ee02039962e7bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Tr=C4=85d?= Date: Wed, 11 Dec 2024 15:41:33 +0100 Subject: [PATCH] Use tables macro (#75) --- world-chain-builder/Cargo.lock | 1 + world-chain-builder/Cargo.toml | 1 + world-chain-builder/src/pbh/db.rs | 36 ++++++++++------------- world-chain-builder/src/pool/validator.rs | 6 ++-- 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/world-chain-builder/Cargo.lock b/world-chain-builder/Cargo.lock index 8c84e87..bc87184 100644 --- a/world-chain-builder/Cargo.lock +++ b/world-chain-builder/Cargo.lock @@ -13549,6 +13549,7 @@ dependencies = [ "reth-cli-util", "reth-consensus", "reth-db", + "reth-db-api", "reth-e2e-test-utils", "reth-eth-wire-types", "reth-evm", diff --git a/world-chain-builder/Cargo.toml b/world-chain-builder/Cargo.toml index 1bdfabe..52be3af 100644 --- a/world-chain-builder/Cargo.toml +++ b/world-chain-builder/Cargo.toml @@ -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", ] } diff --git a/world-chain-builder/src/pbh/db.rs b/world-chain-builder/src/pbh/db.rs index e643cac..341d7b5 100644 --- a/world-chain-builder/src/pbh/db.rs +++ b/world-chain-builder/src/pbh/db.rs @@ -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; } #[derive(Debug, Clone, Default, Serialize, Deserialize)] @@ -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, nullifier: Field) -> Result<(), DatabaseError> { let bytes: FixedBytes<32> = nullifier.into(); - let mut cursor = db_tx.cursor_write::()?; + let mut cursor = db_tx.cursor_write::()?; cursor.insert(bytes, EmptyValue)?; Ok(()) } @@ -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()))?; diff --git a/world-chain-builder/src/pool/validator.rs b/world-chain-builder/src/pool/validator.rs index 332de20..3c32df0 100644 --- a/world-chain-builder/src/pool/validator.rs +++ b/world-chain-builder/src/pool/validator.rs @@ -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}; @@ -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::()?; + let mut cursor = db_tx.cursor_write::()?; cursor.insert(pbh_payload.nullifier_hash.to_be_bytes().into(), EmptyValue)?; db_tx.commit()?; Ok(()) @@ -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::()?; + let mut cursor = db_tx.cursor_write::()?; cursor.insert(payload.nullifier_hash.to_be_bytes().into(), EmptyValue)?; let res = verify_proof(