Skip to content

Commit

Permalink
WIP: Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzejkop committed Dec 17, 2024
1 parent f56a6c2 commit b55606e
Show file tree
Hide file tree
Showing 9 changed files with 423 additions and 82 deletions.
2 changes: 2 additions & 0 deletions world-chain-builder/Cargo.lock

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

38 changes: 36 additions & 2 deletions world-chain-builder/crates/world/pbh/src/external_nullifier.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
use crate::date_marker::{DateMarker, DateMarkerParsingError};
use semaphore::{hash_to_field, Field};
use std::str::FromStr;

use alloy_rlp::{Decodable, Encodable, Rlp};
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 {
Expand All @@ -18,6 +36,10 @@ pub struct ExternalNullifier {
}

impl ExternalNullifier {
pub fn v1(month: u32, year: i32, nonce: u16) -> Self {
Self::new(Prefix::V1, DateMarker::new(year, month), nonce)
}

pub fn new(prefix: Prefix, date_marker: DateMarker, nonce: u16) -> Self {
Self {
prefix,
Expand Down Expand Up @@ -93,6 +115,18 @@ impl FromStr for ExternalNullifier {
}
}

impl Decodable for ExternalNullifier {
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
todo!()
}
}

impl Encodable for ExternalNullifier {
fn encode(&self, out: &mut dyn alloy_rlp::BufMut) {
todo!()
}
}

#[cfg(test)]
mod tests {
use test_case::test_case;
Expand Down
8 changes: 5 additions & 3 deletions world-chain-builder/crates/world/pbh/src/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use semaphore::packed_proof::PackedProof;
use semaphore::Field;
use serde::{Deserialize, Serialize};

use crate::external_nullifier::ExternalNullifier;

pub const TREE_DEPTH: usize = 30;

const LEN: usize = 256;
Expand Down Expand Up @@ -46,10 +48,10 @@ impl Encodable for Proof {
///
/// Contains the semaphore proof and relevent metadata
/// required to to verify the pbh transaction.
#[derive(Clone, Debug, RlpEncodable, RlpDecodable, PartialEq, Eq, Default)]
#[derive(Clone, Debug, RlpEncodable, RlpDecodable, PartialEq, Eq)]
pub struct PbhPayload {
/// A string containing a prefix, the date marker, and the pbh nonce
pub external_nullifier: String,
pub external_nullifier: ExternalNullifier,
/// A nullifier hash used to keep track of
/// previously used pbh transactions
pub nullifier_hash: Field,
Expand Down Expand Up @@ -78,7 +80,7 @@ mod test {
(U256::from(7u64), U256::from(8u64)),
));
let pbh_payload = PbhPayload {
external_nullifier: "0-012025-11".to_string(),
external_nullifier: ExternalNullifier::v1(1, 2024, 11),
nullifier_hash: Field::from(10u64),
root: Field::from(12u64),
proof,
Expand Down
5 changes: 3 additions & 2 deletions world-chain-builder/crates/world/pool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ alloy-rpc-types.workspace = true
alloy-eips.workspace = true
alloy-sol-types.workspace = true
alloy-rlp.workspace = true
alloy-signer-local = { workspace = true, optional = true }
alloy-signer-local.workspace = true
alloy-network.workspace = true

# 3rd party
tokio.workspace = true
Expand All @@ -41,6 +42,7 @@ parking_lot.workspace = true
tempfile.workspace = true
bon.workspace = true
rayon.workspace = true
bytes.workspace = true

[dev-dependencies]
ethers-core.workspace = true
Expand All @@ -52,7 +54,6 @@ reth-e2e-test-utils.workspace = true
default = []
test = [
"dep:reth-e2e-test-utils",
"dep:alloy-signer-local",
]

[lints]
Expand Down
20 changes: 20 additions & 0 deletions world-chain-builder/crates/world/pool/src/eip4337.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use alloy_primitives::keccak256;
use alloy_sol_types::SolValue;
use semaphore::{hash_to_field, Field};

use crate::bindings::IEntryPoint::PackedUserOperation;

pub fn hash_user_op(user_op: &PackedUserOperation) -> Field {
let keccak_hash = keccak256(SolValue::abi_encode_packed(&(
&user_op.sender,
&user_op.nonce,
&user_op.initCode,
&user_op.callData,
&user_op.accountGasLimits,
&user_op.preVerificationGas,
&user_op.gasFees,
&user_op.paymasterAndData,
)));

hash_to_field(keccak_hash.as_slice())
}
2 changes: 2 additions & 0 deletions world-chain-builder/crates/world/pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub mod ordering;
pub mod root;
pub mod tx;
pub mod validator;
pub mod payload;
pub mod eip4337;

#[cfg(any(feature = "test", test))]
pub mod test_utils;
23 changes: 23 additions & 0 deletions world-chain-builder/crates/world/pool/src/payload.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use bon::builder;

#[builder(on(String, into))]
pub fn build_pbh_tx(
#[builder(default = "test test test test test test test test test test test junk")]
mnemonic: String,

user_ops: Vec<String>,
) {
}

#[builder(on(String, into))]
pub fn build_user_op() {}

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

#[test]
fn whatever() {
// build_pbh_tx().call();
}
}
Loading

0 comments on commit b55606e

Please sign in to comment.