Skip to content

Commit

Permalink
Remove ProverValidator and do not require onchian config for offchain…
Browse files Browse the repository at this point in the history
… mode.
  • Loading branch information
piohei committed Jun 19, 2024
1 parent 9fd228b commit 665a357
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 95 deletions.
6 changes: 0 additions & 6 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use crate::identity_tree::{
};
use crate::prover::map::initialize_prover_maps;
use crate::prover::repository::ProverRepository;
use crate::prover::validator::ProofValidator;
use crate::prover::{ProverConfig, ProverType};
use crate::server::data::{
InclusionProofResponse, ListBatchSizesResponse, VerifySemaphoreProofQuery,
Expand All @@ -39,7 +38,6 @@ pub struct App {
pub config: Config,

pub identity_validator: IdentityValidator,
pub proof_validator: Arc<ProofValidator>,
}

impl App {
Expand Down Expand Up @@ -69,8 +67,6 @@ impl App {
deletion_prover_map,
));

let proof_validator = Arc::new(ProofValidator::new(&config));

let identity_processor: Arc<dyn IdentityProcessor> = if config.offchain_mode.enabled {
Arc::new(OffChainIdentityProcessor::new(database.clone()))
} else {
Expand All @@ -84,7 +80,6 @@ impl App {
database.clone(),
identity_manager.clone(),
prover_repository.clone(),
proof_validator.clone(),
)?)
};

Expand All @@ -97,7 +92,6 @@ impl App {
tree_state: OnceLock::new(),
config,
identity_validator,
proof_validator,
});

Ok(app)
Expand Down
17 changes: 3 additions & 14 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ use crate::utils::serde_utils::JsonStrWrapper;
pub struct Config {
pub app: AppConfig,
pub tree: TreeConfig,
pub network: NetworkConfig,
pub providers: ProvidersConfig,
pub relayer: RelayerConfig,
pub network: Option<NetworkConfig>,
pub providers: Option<ProvidersConfig>,
pub relayer: Option<RelayerConfig>,
pub database: DatabaseConfig,
pub server: ServerConfig,
#[serde(default)]
Expand Down Expand Up @@ -335,17 +335,6 @@ mod tests {
[tree]
[network]
identity_manager_address = "0x0000000000000000000000000000000000000000"
[providers]
primary_network_provider = "http://localhost:8545"
[relayer]
kind = "tx_sitter"
tx_sitter_url = "http://localhost:3000"
tx_sitter_address = "0x0000000000000000000000000000000000000000"
[database]
database = "postgres://user:password@localhost:5432/database"
Expand Down
12 changes: 8 additions & 4 deletions src/contracts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pub mod abi;
pub mod scanner;

use anyhow::{anyhow, Context};
use anyhow::{anyhow, bail, Context};
use ethers::providers::Middleware;
use ethers::types::{H256, U256};
use tracing::{error, info, instrument};
Expand Down Expand Up @@ -40,8 +40,12 @@ impl IdentityManager {
where
Self: Sized,
{
let Some(network_config) = &config.network else {
bail!("Network config is required for IdentityManager.");
};

// Check that there is code deployed at the target address.
let address = config.network.identity_manager_address;
let address = network_config.identity_manager_address;
let code = ethereum.provider().get_code(address, None).await?;
if code.as_ref().is_empty() {
error!(
Expand All @@ -52,7 +56,7 @@ impl IdentityManager {

// Connect to the running batching contract.
let abi = WorldId::new(
config.network.identity_manager_address,
network_config.identity_manager_address,
ethereum.provider().clone(),
);

Expand All @@ -71,7 +75,7 @@ impl IdentityManager {
let secondary_providers = ethereum.secondary_providers();

let mut secondary_abis = Vec::new();
for (chain_id, address) in &config.network.relayed_identity_manager_addresses.0 {
for (chain_id, address) in &network_config.relayed_identity_manager_addresses.0 {
let provider = secondary_providers
.get(chain_id)
.ok_or_else(|| anyhow!("No provider for chain id: {}", chain_id))?;
Expand Down
18 changes: 13 additions & 5 deletions src/ethereum/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::sync::Arc;

use anyhow::bail;
use ethers::types::transaction::eip2718::TypedTransaction;
use ethers::types::Address;
pub use read::ReadProvider;
Expand All @@ -27,22 +28,29 @@ pub struct Ethereum {
impl Ethereum {
#[instrument(name = "Ethereum::new", level = "debug", skip_all)]
pub async fn new(config: &Config) -> anyhow::Result<Self> {
let Some(providers_config) = &config.providers else {
bail!("Providers config is required for Ethereum.");
};

let Some(relayer_config) = &config.relayer else {
bail!("Relayer config is required for Ethereum.");
};

let read_provider =
ReadProvider::new(config.providers.primary_network_provider.clone().into()).await?;
ReadProvider::new(providers_config.primary_network_provider.clone().into()).await?;

let mut secondary_read_providers = HashMap::new();

for secondary_url in &config.providers.relayed_network_providers.0 {
for secondary_url in &providers_config.relayed_network_providers.0 {
let secondary_read_provider = ReadProvider::new(secondary_url.clone().into()).await?;
secondary_read_providers.insert(
secondary_read_provider.chain_id.as_u64(),
Arc::new(secondary_read_provider),
);
}

let write_provider: Arc<WriteProvider> = Arc::new(
write_provider::WriteProvider::new(read_provider.clone(), &config.relayer).await?,
);
let write_provider: Arc<WriteProvider> =
Arc::new(WriteProvider::new(read_provider.clone(), relayer_config).await?);

Ok(Self {
read_provider: Arc::new(read_provider),
Expand Down
30 changes: 21 additions & 9 deletions src/identity/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use anyhow::Context;
use anyhow::{anyhow, Context};
use async_trait::async_trait;
use chrono::Utc;
use ethers::abi::RawLog;
Expand All @@ -21,8 +21,8 @@ use crate::database::types::{BatchEntry, BatchType};
use crate::database::Database;
use crate::ethereum::{Ethereum, ReadProvider};
use crate::identity_tree::{Canonical, Hash, Intermediate, TreeVersion, TreeWithNextVersion};
use crate::prover::identity::Identity;
use crate::prover::repository::ProverRepository;
use crate::prover::validator::ProofValidator;
use crate::prover::Prover;
use crate::utils::index_packing::pack_indices;
use crate::utils::retry_tx;
Expand Down Expand Up @@ -56,7 +56,6 @@ pub struct OnChainIdentityProcessor {
database: Arc<Database>,
identity_manager: Arc<IdentityManager>,
prover_repository: Arc<ProverRepository>,
proof_validator: Arc<ProofValidator>,
}

#[async_trait]
Expand Down Expand Up @@ -169,15 +168,13 @@ impl OnChainIdentityProcessor {
database: Arc<Database>,
identity_manager: Arc<IdentityManager>,
prover_repository: Arc<ProverRepository>,
proof_validator: Arc<ProofValidator>,
) -> anyhow::Result<Self> {
Ok(Self {
ethereum,
config,
database,
identity_manager,
prover_repository,
proof_validator,
})
}

Expand Down Expand Up @@ -210,8 +207,7 @@ impl OnChainIdentityProcessor {
prover: &Prover,
batch: &BatchEntry,
) -> anyhow::Result<TransactionId> {
self.proof_validator
.validate_merkle_proofs(&batch.data.0.identities)?;
self.validate_merkle_proofs(&batch.data.0.identities)?;
let start_index = *batch.data.0.indexes.first().expect("Should exist.");
let pre_root: U256 = batch.prev_root.expect("Should exist.").into();
let post_root: U256 = batch.next_root.into();
Expand Down Expand Up @@ -267,8 +263,7 @@ impl OnChainIdentityProcessor {
prover: &Prover,
batch: &BatchEntry,
) -> anyhow::Result<TransactionId> {
self.proof_validator
.validate_merkle_proofs(&batch.data.0.identities)?;
self.validate_merkle_proofs(&batch.data.0.identities)?;
let pre_root: U256 = batch.prev_root.expect("Should exist.").into();
let post_root: U256 = batch.next_root.into();
let deletion_indices: Vec<_> = batch.data.0.indexes.iter().map(|&v| v as u32).collect();
Expand Down Expand Up @@ -314,6 +309,23 @@ impl OnChainIdentityProcessor {

Ok(pending_identities)
}

/// Validates that merkle proofs are of the correct length against tree
/// depth
pub fn validate_merkle_proofs(&self, identity_commitments: &[Identity]) -> anyhow::Result<()> {
let tree_depth = self.config.tree.tree_depth;
for id in identity_commitments {
if id.merkle_proof.len() != tree_depth {
return Err(anyhow!(format!(
"Length of merkle proof ({len}) did not match tree depth ({depth})",
len = id.merkle_proof.len(),
depth = tree_depth
)));
}
}

Ok(())
}
}

pub struct OnChainIdentityFinalizer {
Expand Down
2 changes: 0 additions & 2 deletions src/prover/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ pub mod identity;
pub mod map;
pub mod proof;
pub mod repository;
pub mod validator;

use std::fmt::{Display, Formatter};
use std::hash::{Hash, Hasher};
use std::mem::size_of;
Expand Down
32 changes: 0 additions & 32 deletions src/prover/validator.rs

This file was deleted.

52 changes: 32 additions & 20 deletions tests/common/test_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,28 +158,40 @@ impl TestConfigBuilder {
force_cache_purge: default::force_cache_purge(),
initial_leaf_value: default::initial_leaf_value(),
},
network: NetworkConfig {
identity_manager_address: self
.identity_manager_address
.context("Missing identity manager address")?,
relayed_identity_manager_addresses: Default::default(),
network: if self.offchain_mode {
None
} else {
Some(NetworkConfig {
identity_manager_address: self
.identity_manager_address
.context("Missing identity manager address")?,
relayed_identity_manager_addresses: Default::default(),
})
},
providers: ProvidersConfig {
primary_network_provider: self
.primary_network_provider
.context("Missing primary network provider")?,
relayed_network_providers: Default::default(),
providers: if self.offchain_mode {
None
} else {
Some(ProvidersConfig {
primary_network_provider: self
.primary_network_provider
.context("Missing primary network provider")?,
relayed_network_providers: Default::default(),
})
},
relayer: if self.offchain_mode {
None
} else {
Some(RelayerConfig::OzDefender(OzDefenderConfig {
oz_api_url: self.oz_api_url.context("Missing oz api url")?,
oz_address: self.oz_address.context("Missing oz address")?,
oz_api_key: "".to_string(),
oz_api_secret: "".to_string(),
oz_transaction_validity: default::oz_transaction_validity(),
oz_send_timeout: default::oz_send_timeout(),
oz_mine_timeout: default::oz_mine_timeout(),
oz_gas_limit: Default::default(),
}))
},
relayer: RelayerConfig::OzDefender(OzDefenderConfig {
oz_api_url: self.oz_api_url.context("Missing oz api url")?,
oz_address: self.oz_address.context("Missing oz address")?,
oz_api_key: "".to_string(),
oz_api_secret: "".to_string(),
oz_transaction_validity: default::oz_transaction_validity(),
oz_send_timeout: default::oz_send_timeout(),
oz_mine_timeout: default::oz_mine_timeout(),
oz_gas_limit: Default::default(),
}),
database: DatabaseConfig {
database,
migrate: default::migrate(),
Expand Down
5 changes: 2 additions & 3 deletions tests/immediate_deletion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async fn immediate_deletion() -> anyhow::Result<()> {
.add_prover(mock_deletion_prover)
.build()?;

let (_, app_handle, local_addr) = spawn_app(config.clone())
let (_, app_handle, local_addr, shutdown) = spawn_app(config.clone())
.await
.expect("Failed to spawn app.");

Expand Down Expand Up @@ -133,9 +133,8 @@ async fn immediate_deletion() -> anyhow::Result<()> {
// Shutdown the app and reset the mock shutdown, allowing us to test the
// behaviour with saved data.
info!("Stopping the app for testing purposes");
shutdown();
shutdown.shutdown();
app_handle.await.unwrap();
reset_shutdown();

Ok(())
}

0 comments on commit 665a357

Please sign in to comment.