Skip to content

Commit

Permalink
Validate user ops and payload counts
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzejkop committed Dec 17, 2024
1 parent d50e8bb commit eb37438
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 125 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/crates/world/pbh/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ thiserror = { workspace = true }
semaphore.workspace = true
strum.workspace = true
serde.workspace = true
bon.workspace = true

[dev-dependencies]
ethers-core.workspace = true
Expand Down
30 changes: 11 additions & 19 deletions world-chain-builder/crates/world/pbh/src/external_nullifier.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,25 @@
use std::str::FromStr;

use alloy_rlp::{Decodable, Encodable, Rlp};
use alloy_rlp::{Decodable, Encodable};
use bon::Builder;
use semaphore::{hash_to_field, Field};
use strum::{Display, EnumString};
use thiserror::Error;

use crate::date_marker::{DateMarker, DateMarkerParsingError};

#[macro_export]
macro_rules! ext_nullifier {
($mo:expr,$yr:expr,$no:expr) => {{
let prefix = $crate::external_nullifier::Prefix::V1;
let date_marker = $crate::date_marker::DateMarker::new($yr, $mo);

$crate::external_nullifier::ExternalNullifier::new(prefix, date_marker, $no)
}};
}

#[test]
fn whatever() {
ext_nullifier!(01, 2025, 1);
}

#[derive(Display, EnumString, Debug, Clone, Copy, PartialEq, Eq)]
#[strum(serialize_all = "snake_case")]
pub enum Prefix {
V1,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Builder, Debug, Clone, Copy, PartialEq, Eq)]
pub struct ExternalNullifier {
#[builder(default = Prefix::V1)]
pub prefix: Prefix,
pub date_marker: DateMarker,
#[builder(default = 0)]
pub nonce: u16,
}

Expand Down Expand Up @@ -117,13 +105,17 @@ impl FromStr for ExternalNullifier {

impl Decodable for ExternalNullifier {
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
todo!()
let s: String = Decodable::decode(buf)?;

s.parse::<ExternalNullifier>()
.map_err(|_| alloy_rlp::Error::Custom("Failed to parse string to external nullifier"))
}
}

impl Encodable for ExternalNullifier {
fn encode(&self, out: &mut dyn alloy_rlp::BufMut) {
todo!()
let s = self.to_string();
Encodable::encode(&s, out)
}
}

Expand Down
4 changes: 3 additions & 1 deletion world-chain-builder/crates/world/pool/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use reth_provider::ProviderError;

use world_chain_builder_pbh::external_nullifier::ExternalNullifierParsingError;

#[derive(Debug, thiserror::Error)]
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
pub enum WorldChainTransactionPoolInvalid {
#[error("nullifier has already been seen")]
NullifierAlreadyExists,
Expand All @@ -29,6 +29,8 @@ pub enum WorldChainTransactionPoolInvalid {
InvalidRoot,
#[error(transparent)]
MalformedSignature(#[from] alloy_rlp::Error),
#[error("One or more user ops are missing pbh payloads")]
MissingPbhPayload,
}

#[derive(Debug, thiserror::Error)]
Expand Down
1 change: 0 additions & 1 deletion world-chain-builder/crates/world/pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pub mod eip4337;
pub mod error;
pub mod noop;
pub mod ordering;
pub mod payload;
pub mod root;
pub mod tx;
pub mod validator;
Expand Down
23 changes: 0 additions & 23 deletions world-chain-builder/crates/world/pool/src/payload.rs

This file was deleted.

36 changes: 26 additions & 10 deletions world-chain-builder/crates/world/pool/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use reth_primitives::PooledTransactionsElement;
use reth_provider::test_utils::MockEthProvider;
use revm_primitives::{hex, Address, TxKind};
use semaphore::identity::Identity;
use semaphore::poseidon_tree::{LazyPoseidonTree, PoseidonTree};
use semaphore::poseidon_tree::LazyPoseidonTree;
use semaphore::protocol::{generate_nullifier_hash, generate_proof};
use semaphore::{hash_to_field, Field};
use world_chain_builder_pbh::date_marker::DateMarker;
Expand Down Expand Up @@ -173,16 +173,9 @@ pub fn user_op(
}

pub fn pbh_bundle(
ops: Vec<(PackedUserOperation, PbhPayload)>,
user_ops: Vec<PackedUserOperation>,
proofs: Vec<PbhPayload>,
) -> IPBHValidator::handleAggregatedOpsCall {
let mut user_ops = Vec::new();
let mut proofs = Vec::new();

for (user_op, proof) in ops {
user_ops.push(user_op);
proofs.push(proof);
}

let mut signature_buff = Vec::new();
proofs.encode(&mut signature_buff);

Expand Down Expand Up @@ -350,3 +343,26 @@ pub fn create_pbh_paylaod(
proof,
}
}

#[cfg(test)]
mod tests {
use test_case::test_case;

use super::*;

#[test_case(0, "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266")]
#[test_case(1, "0x70997970C51812dc3A010C7d01b50e0d17dc79C8")]
#[test_case(2, "0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC")]
#[test_case(3, "0x90F79bf6EB2c4f870365E785982E1f101E93b906")]
#[test_case(4, "0x15d34AAf54267DB7D7c367839AAf71A00a2C6A65")]
#[test_case(5, "0x9965507D1a55bcC2695C58ba16FB37d819B0A4dc")]
#[test_case(6, "0x976EA74026E726554dB657fA54763abd0C3a0aa9")]
#[test_case(7, "0x14dC79964da2C08b23698B3D3cc7Ca32193d9955")]
#[test_case(8, "0x23618e81E3f5cdF7f54C3d65f7FBc0aBf5B21E8f")]
#[test_case(9, "0xa0Ee7A142d267C1f36714E4a8F75612F20a79720")]
fn mnemonic_accounts(index: u32, exp_address: &str) {
let exp: Address = exp_address.parse().unwrap();

assert_eq!(exp, account(index));
}
}
Loading

0 comments on commit eb37438

Please sign in to comment.