Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Validation to enforce nullifier uniqueness #41

Merged
merged 5 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion world-chain-builder/src/node/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ where
) -> TransactionValidationOutcome<Self::Transaction> {
if let Some(pbh_paylaod) = transaction.pbh_payload() {
self.inner
.set_validated(&transaction, pbh_paylaod)
.set_validated(pbh_paylaod)
.expect("Error when writing to the db");
}

Expand Down
26 changes: 3 additions & 23 deletions world-chain-builder/src/pbh/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,6 @@ use tracing::info;
use reth_db::cursor::DbCursorRW;
use reth_db::transaction::DbTxMut;

/// Table for executed nullifiers.
///
/// This table stores the nullifiers of PBH transactions that have been
/// included into a block after it has been sealed.
#[derive(Debug, Clone, Default)]
pub struct ExecutedPbhNullifierTable;

impl Table for ExecutedPbhNullifierTable {
const NAME: &'static str = "ExecutedPbhNullifiers";

type Key = B256;

type Value = EmptyValue;
}

/// Table to store PBH validated transactions along with their nullifiers.
///
/// When a trasnaction is validated before being inserted into the pool,
Expand All @@ -43,9 +28,9 @@ pub struct ValidatedPbhTransactionTable;
impl Table for ValidatedPbhTransactionTable {
const NAME: &'static str = "ValidatedPbhTransactions";

type Key = TxHash;
type Key = B256;

type Value = B256;
type Value = EmptyValue;
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
Expand All @@ -68,7 +53,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::<ExecutedPbhNullifierTable>()?;
let mut cursor = db_tx.cursor_write::<ValidatedPbhTransactionTable>()?;
cursor.insert(bytes, EmptyValue)?;
Ok(())
}
Expand All @@ -90,11 +75,6 @@ pub fn load_world_chain_db(
.begin_rw_txn()
.map_err(|e| DatabaseError::InitTx(e.into()))?;

tx.create_db(
Some(ExecutedPbhNullifierTable::NAME),
DatabaseFlags::default(),
)
.map_err(|e| DatabaseError::CreateTable(e.into()))?;
tx.create_db(
Some(ValidatedPbhTransactionTable::NAME),
DatabaseFlags::default(),
Expand Down
30 changes: 12 additions & 18 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::{ExecutedPbhNullifierTable, ValidatedPbhTransactionTable};
use crate::pbh::db::{EmptyValue, ValidatedPbhTransactionTable};
use crate::pbh::external_nullifier::ExternalNullifier;
use crate::pbh::payload::{PbhPayload, TREE_DEPTH};

Expand Down Expand Up @@ -69,10 +69,10 @@ where
&self.inner
}

pub fn set_validated(&self, tx: &Tx, pbh_payload: &PbhPayload) -> Result<(), DatabaseError> {
let db_tx = self.pbh_db.tx_mut()?;
pub fn set_validated(&self, pbh_payload: &PbhPayload) -> Result<(), DatabaseError> {
let db_tx: reth_db::mdbx::tx::Tx<reth_db::mdbx::RW> = self.pbh_db.tx_mut()?;
0xKitsune marked this conversation as resolved.
Show resolved Hide resolved
let mut cursor = db_tx.cursor_write::<ValidatedPbhTransactionTable>()?;
cursor.insert(*tx.hash(), pbh_payload.nullifier_hash.to_be_bytes().into())?;
cursor.insert(pbh_payload.nullifier_hash.to_be_bytes().into(), EmptyValue)?;
db_tx.commit()?;
Ok(())
}
Expand Down Expand Up @@ -131,7 +131,9 @@ where
pbh_payload: &PbhPayload,
) -> Result<(), TransactionValidationError> {
let tx = self.pbh_db.tx()?;
match tx.get::<ExecutedPbhNullifierTable>(pbh_payload.nullifier_hash.to_be_bytes().into()) {
match tx
.get::<ValidatedPbhTransactionTable>(pbh_payload.nullifier_hash.to_be_bytes().into())
{
Ok(Some(_)) => Err(WorldChainTransactionPoolInvalid::NullifierAlreadyExists.into()),
Ok(None) => Ok(()),
Err(e) => Err(TransactionValidationError::Error(
Expand All @@ -147,20 +149,13 @@ where
) -> Result<(), TransactionValidationError> {
// Create db transaction and insert the nullifier hash
// We do this first to prevent repeatedly validating the same transaction
//
// This should prevent DOS attacks for tranasctions with the same hash
// However i'm not sure there's anything we can do for transactions with different hashes
let db_tx = self.pbh_db.tx_mut()?;
let mut cursor = db_tx.cursor_write::<ValidatedPbhTransactionTable>()?;
cursor.insert(
*transaction.hash(),
payload.nullifier_hash.to_be_bytes().into(),
)?;
cursor.insert(payload.nullifier_hash.to_be_bytes().into(), EmptyValue)?;

let date = chrono::Utc::now();
self.validate_root(payload)?;
self.validate_external_nullifier(date, payload)?;
self.validate_nullifier(payload)?;

let res = verify_proof(
payload.root,
Expand All @@ -187,13 +182,15 @@ where
origin: TransactionOrigin,
transaction: Tx,
) -> TransactionValidationOutcome<Tx> {
let validation_outcome = self.inner.validate_one(origin, transaction.clone());

if let Some(pbh_payload) = transaction.pbh_payload() {
if let Err(e) = self.validate_pbh_payload(&transaction, pbh_payload) {
return e.to_outcome(transaction);
}
};

self.inner.validate_one(origin, transaction.clone())
validation_outcome
}

/// Validates all given transactions.
Expand Down Expand Up @@ -504,9 +501,6 @@ pub mod tests {
proof,
};

let mut tx = get_non_pbh_transaction();
tx.pbh_payload = Some(payload.clone());

validator.set_validated(&tx, &payload).unwrap();
validator.set_validated(&payload).unwrap();
}
}
Loading