Skip to content

Commit

Permalink
Merge branch 'main' into osiris/tests
Browse files Browse the repository at this point in the history
  • Loading branch information
0xOsiris committed Oct 16, 2024
2 parents 2b97ddf + 642bcdf commit 81ea8c4
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 117 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ The PBH payload contains the following fields:

`external_nullifier` - A string containing a prefix, the date marker, and the pbh nonce

`external_nullifier_hash` - The hash of the external nullifier

`nullifier_hash` - A nullifier hash used to keep track of previously used pbh transactions

`root` - The root of the merkle tree for which this proof was generated
Expand Down
3 changes: 0 additions & 3 deletions devnet/clean.sh

This file was deleted.

4 changes: 2 additions & 2 deletions world-chain-builder/src/e2e_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl WorldChainBuilderTestContext {
let raw_tx = envelope.encoded_2718();
let mut data = raw_tx.as_ref();
let recovered = PooledTransactionsElement::decode_enveloped(&mut data).unwrap();
let proof = self.valid_proof(
let pbh_payload = self.valid_proof(
signer.address(),
recovered.hash().as_slice(),
chrono::Utc::now(),
Expand All @@ -177,7 +177,7 @@ impl WorldChainBuilderTestContext {

let world_chain_pooled_tx_element = WorldChainPooledTransactionsElement {
inner: recovered,
semaphore_proof: Some(proof.clone()),
pbh_payload: Some(pbh_payload.clone()),
};

let mut buff = Vec::<u8>::new();
Expand Down
4 changes: 2 additions & 2 deletions world-chain-builder/src/node/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,9 +599,9 @@ where
_origin: TransactionOrigin,
transaction: Self::Transaction,
) -> TransactionValidationOutcome<Self::Transaction> {
if let Some(semaphore_proof) = transaction.pbh_payload() {
if let Some(pbh_paylaod) = transaction.pbh_payload() {
self.inner
.set_validated(&transaction, semaphore_proof)
.set_validated(&transaction, pbh_paylaod)
.expect("Error when writing to the db");
}

Expand Down
8 changes: 4 additions & 4 deletions world-chain-builder/src/payload/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ mod tests {
);

let wc_noop_validator = WorldChainNoopValidator::new(wc_validator);
let ordering = WorldChainOrdering::new(db.clone());
let ordering = WorldChainOrdering::default();

let world_chain_tx_pool = reth::transaction_pool::Pool::new(
wc_noop_validator,
Expand Down Expand Up @@ -876,7 +876,7 @@ mod tests {
);

let wc_noop_validator = WorldChainNoopValidator::new(wc_validator);
let ordering = WorldChainOrdering::new(db.clone());
let ordering = WorldChainOrdering::default();

let world_chain_tx_pool = reth::transaction_pool::Pool::new(
wc_noop_validator,
Expand Down Expand Up @@ -1030,15 +1030,15 @@ mod tests {
);
let pooled_tx = EthPooledTransaction::new(tx_recovered.clone(), 200);

let semaphore_proof = if pbh {
let pbh_payload = if pbh {
Some(PbhPayload::default())
} else {
None
};

WorldChainPooledTransaction {
inner: pooled_tx,
semaphore_proof,
pbh_payload,
}
})
.collect::<Vec<_>>()
Expand Down
19 changes: 9 additions & 10 deletions world-chain-builder/src/pbh/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ use serde::{Deserialize, Serialize};
use tracing::info;

use reth_db::cursor::DbCursorRW;
use reth_db::transaction::{DbTx, DbTxMut};
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;

Expand All @@ -29,6 +32,11 @@ impl Table for ExecutedPbhNullifierTable {
}

/// 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;

Expand All @@ -55,15 +63,6 @@ impl Compress for EmptyValue {
fn compress_to_buf<B: BufMut + AsMut<[u8]>>(self, _buf: &mut B) {}
}

/// Check the database to see if a tx has been validated for PBH
/// If so return the nullifier
pub fn get_pbh_validated(
db_tx: Tx<RW>,
tx: TxHash,
) -> Result<Option<FixedBytes<32>>, DatabaseError> {
db_tx.get::<ValidatedPbhTransactionTable>(tx)
}

/// Set the store the nullifier for a tx after it
/// has been included in the block
/// don't forget to call db_tx.commit() at the very end
Expand Down
6 changes: 3 additions & 3 deletions world-chain-builder/src/pbh/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ mod test {
),
(U256::from(7u64), U256::from(8u64)),
));
let semaphore_proof = PbhPayload {
let pbh_payload = PbhPayload {
external_nullifier: "0-012025-11".to_string(),
nullifier_hash: Field::from(10u64),
root: Field::from(12u64),
proof,
};
let encoded = alloy_rlp::encode(&semaphore_proof);
let encoded = alloy_rlp::encode(&pbh_payload);
let mut buf = encoded.as_slice();
let decoded = PbhPayload::decode(&mut buf).unwrap();
assert_eq!(semaphore_proof, decoded);
assert_eq!(pbh_payload, decoded);
}
}
2 changes: 1 addition & 1 deletion world-chain-builder/src/pool/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ where
)
});

let ordering = WorldChainOrdering::new(self.db.clone());
let ordering = WorldChainOrdering::default();

let transaction_pool =
reth::transaction_pool::Pool::new(validator, ordering, blob_store, ctx.pool_config());
Expand Down
44 changes: 10 additions & 34 deletions world-chain-builder/src/pool/ordering.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
use super::tx::WorldChainPoolTransaction;
use crate::pbh::db::ValidatedPbhTransactionTable;
use reth::transaction_pool::{CoinbaseTipOrdering, Priority, TransactionOrdering};
use reth_db::transaction::DbTx;
use reth_db::{Database as _, DatabaseEnv, DatabaseError};
use revm_primitives::U256;
use std::sync::Arc;

/// Default ordering for the pool.
///
/// The transactions are ordered by their coinbase tip.
/// The higher the coinbase tip is, the higher the priority of the transaction.
#[derive(Debug)]
#[non_exhaustive]
pub struct WorldChainOrdering<T> {
inner: CoinbaseTipOrdering<T>,
pbh_db: Arc<DatabaseEnv>,
}

/// Ordering is automatically derived.
Expand All @@ -26,31 +20,6 @@ pub struct WorldChainPriority {
effective_tip_per_gas: Option<U256>,
}

impl<T> WorldChainOrdering<T>
where
T: WorldChainPoolTransaction + 'static,
{
/// Create a new [`WorldChainOrdering`].
pub fn new(database_env: Arc<DatabaseEnv>) -> Self {
Self {
inner: CoinbaseTipOrdering::default(),
pbh_db: database_env,
}
}
fn try_is_pbh(&self, transaction: &T) -> Result<bool, DatabaseError> {
let db_tx = self.pbh_db.tx()?;
Ok(db_tx
.get::<ValidatedPbhTransactionTable>(*transaction.hash())?
.is_some())
}
fn is_pbh(&self, transaction: &T) -> bool {
self.try_is_pbh(transaction).unwrap_or_else(|error| {
tracing::error!(?error, "Failed to load transaction from database");
false
})
}
}

impl<T> TransactionOrdering for WorldChainOrdering<T>
where
T: WorldChainPoolTransaction + 'static,
Expand All @@ -64,9 +33,9 @@ where
base_fee: u64,
) -> Priority<Self::PriorityValue> {
let effective_tip_per_gas = transaction.effective_tip_per_gas(base_fee).map(U256::from);
let is_pbh = self.is_pbh(transaction);

Some(WorldChainPriority {
is_pbh,
is_pbh: transaction.pbh_payload().is_some(),
effective_tip_per_gas,
})
.into()
Expand All @@ -77,7 +46,14 @@ impl<T> Clone for WorldChainOrdering<T> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
pbh_db: self.pbh_db.clone(),
}
}
}

impl<T> Default for WorldChainOrdering<T> {
fn default() -> Self {
Self {
inner: CoinbaseTipOrdering::default(),
}
}
}
12 changes: 6 additions & 6 deletions world-chain-builder/src/pool/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub trait WorldChainPoolTransaction: EthPoolTransaction {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WorldChainPooledTransaction {
pub inner: EthPooledTransaction,
pub semaphore_proof: Option<PbhPayload>,
pub pbh_payload: Option<PbhPayload>,
}

impl EthPoolTransaction for WorldChainPooledTransaction {
Expand All @@ -41,7 +41,7 @@ impl EthPoolTransaction for WorldChainPooledTransaction {

impl WorldChainPoolTransaction for WorldChainPooledTransaction {
fn pbh_payload(&self) -> Option<&PbhPayload> {
self.semaphore_proof.as_ref()
self.pbh_payload.as_ref()
}
}

Expand All @@ -57,7 +57,7 @@ impl TryFrom<TransactionSignedEcRecovered> for WorldChainPooledTransaction {
fn try_from(tx: TransactionSignedEcRecovered) -> Result<Self, Self::Error> {
Ok(Self {
inner: EthPooledTransaction::try_from(tx)?,
semaphore_proof: None,
pbh_payload: None,
})
}
}
Expand All @@ -66,7 +66,7 @@ impl From<WorldChainPooledTransactionsElementEcRecovered> for WorldChainPooledTr
fn from(tx: WorldChainPooledTransactionsElementEcRecovered) -> Self {
Self {
inner: EthPooledTransaction::from_pooled(tx.inner),
semaphore_proof: tx.semaphore_proof,
pbh_payload: tx.pbh_payload,
}
}
}
Expand All @@ -77,7 +77,7 @@ impl From<PooledTransactionsElementEcRecovered> for WorldChainPooledTransactions
inner: value,
// Incoming consensus transactions do not have a semaphore proof
// Is this problematic?
semaphore_proof: None,
pbh_payload: None,
}
}
}
Expand All @@ -98,7 +98,7 @@ impl PoolTransaction for WorldChainPooledTransaction {
fn try_from_consensus(tx: Self::Consensus) -> Result<Self, Self::TryFromConsensusError> {
EthPooledTransaction::try_from_consensus(tx).map(|inner| Self {
inner,
semaphore_proof: None,
pbh_payload: None,
})
}

Expand Down
Loading

0 comments on commit 81ea8c4

Please sign in to comment.