Skip to content

Commit

Permalink
Merge pull request #168 from gnosisguild/ry/167-consolidate-providers
Browse files Browse the repository at this point in the history
Consolidate providers
  • Loading branch information
hmzakhalid authored Nov 1, 2024
2 parents d9867ed + d5a90a9 commit eaea0a8
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 51 deletions.
23 changes: 19 additions & 4 deletions packages/ciphernode/enclave_node/src/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ use cipher::Cipher;
use config::AppConfig;
use enclave_core::EventBus;
use evm::{
helpers::get_signer_from_repository, CiphernodeRegistrySol, EnclaveSol, RegistryFilterSol,
helpers::{
create_provider_with_signer, create_readonly_provider, ensure_http_rpc, ensure_ws_rpc,
get_signer_from_repository,
},
CiphernodeRegistrySol, EnclaveSol, RegistryFilterSol,
};
use logger::SimpleLogger;
use p2p::P2p;
Expand Down Expand Up @@ -35,15 +39,26 @@ pub async fn setup_aggregator(
let sortition = Sortition::attach(&bus, repositories.sortition());
let cipher = Arc::new(Cipher::from_config(&config).await?);
let signer = get_signer_from_repository(repositories.eth_private_key(), &cipher).await?;

for chain in config
.chains()
.iter()
.filter(|chain| chain.enabled.unwrap_or(true))
{
let rpc_url = &chain.rpc_url;
EnclaveSol::attach(&bus, rpc_url, &chain.contracts.enclave, &signer).await?;
RegistryFilterSol::attach(&bus, rpc_url, &chain.contracts.filter_registry, &signer).await?;
CiphernodeRegistrySol::attach(&bus, rpc_url, &chain.contracts.ciphernode_registry).await?;
let read_provider = create_readonly_provider(&ensure_ws_rpc(rpc_url)).await?;
let write_provider =
create_provider_with_signer(&ensure_http_rpc(rpc_url), &signer).await?;
EnclaveSol::attach(
&bus,
&read_provider,
&write_provider,
&chain.contracts.enclave,
)
.await?;
RegistryFilterSol::attach(&bus, &write_provider, &chain.contracts.filter_registry).await?;
CiphernodeRegistrySol::attach(&bus, &read_provider, &chain.contracts.ciphernode_registry)
.await?;
}

E3RequestRouter::builder(&bus, store)
Expand Down
11 changes: 8 additions & 3 deletions packages/ciphernode/enclave_node/src/ciphernode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use cipher::Cipher;
use config::AppConfig;
use data::{DataStore, InMemStore, SledStore};
use enclave_core::EventBus;
use evm::{CiphernodeRegistrySol, EnclaveSolReader};
use evm::{
helpers::{create_readonly_provider, ensure_ws_rpc},
CiphernodeRegistrySol, EnclaveSolReader,
};
use logger::SimpleLogger;
use p2p::P2p;
use rand::SeedableRng;
Expand Down Expand Up @@ -42,8 +45,10 @@ pub async fn setup_ciphernode(
{
let rpc_url = &chain.rpc_url;

EnclaveSolReader::attach(&bus, rpc_url, &chain.contracts.enclave).await?;
CiphernodeRegistrySol::attach(&bus, rpc_url, &chain.contracts.ciphernode_registry).await?;
let read_provider = create_readonly_provider(&ensure_ws_rpc(rpc_url)).await?;
EnclaveSolReader::attach(&bus, &read_provider, &chain.contracts.enclave).await?;
CiphernodeRegistrySol::attach(&bus, &read_provider, &chain.contracts.ciphernode_registry)
.await?;
}

E3RequestRouter::builder(&bus, store.clone())
Expand Down
14 changes: 9 additions & 5 deletions packages/ciphernode/evm/src/ciphernode_registry_sol.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::EvmEventReader;
use crate::{helpers::ReadonlyProvider, EvmEventReader};
use actix::Addr;
use alloy::{
primitives::{LogData, B256},
Expand Down Expand Up @@ -97,18 +97,22 @@ pub struct CiphernodeRegistrySolReader;
impl CiphernodeRegistrySolReader {
pub async fn attach(
bus: &Addr<EventBus>,
rpc_url: &str,
provider: &ReadonlyProvider,
contract_address: &str,
) -> Result<Addr<EvmEventReader>> {
let addr = EvmEventReader::attach(bus, rpc_url, extractor, contract_address).await?;
let addr = EvmEventReader::attach(bus, provider, extractor, contract_address).await?;
Ok(addr)
}
}

pub struct CiphernodeRegistrySol;
impl CiphernodeRegistrySol {
pub async fn attach(bus: &Addr<EventBus>, rpc_url: &str, contract_address: &str) -> Result<()> {
CiphernodeRegistrySolReader::attach(bus, rpc_url, contract_address).await?;
pub async fn attach(
bus: &Addr<EventBus>,
provider: &ReadonlyProvider,
contract_address: &str,
) -> Result<()> {
CiphernodeRegistrySolReader::attach(bus, provider, contract_address).await?;
Ok(())
}
}
17 changes: 9 additions & 8 deletions packages/ciphernode/evm/src/enclave_sol.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use std::sync::Arc;

use crate::{enclave_sol_reader::EnclaveSolReader, enclave_sol_writer::EnclaveSolWriter};
use crate::{
enclave_sol_reader::EnclaveSolReader,
enclave_sol_writer::EnclaveSolWriter,
helpers::{ReadonlyProvider, SignerProvider},
};
use actix::Addr;
use alloy::signers::local::PrivateKeySigner;
use anyhow::Result;
use enclave_core::EventBus;

pub struct EnclaveSol;
impl EnclaveSol {
pub async fn attach(
bus: &Addr<EventBus>,
rpc_url: &str,
read_provider: &ReadonlyProvider,
write_provider: &SignerProvider,
contract_address: &str,
signer: &Arc<PrivateKeySigner>,
) -> Result<()> {
EnclaveSolReader::attach(bus, rpc_url, contract_address).await?;
EnclaveSolWriter::attach(bus, rpc_url, contract_address, signer).await?;
EnclaveSolReader::attach(bus, read_provider, contract_address).await?;
EnclaveSolWriter::attach(bus, write_provider, contract_address).await?;
Ok(())
}
}
5 changes: 3 additions & 2 deletions packages/ciphernode/evm/src/enclave_sol_reader.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::helpers::ReadonlyProvider;
use crate::EvmEventReader;
use actix::Addr;
use alloy::primitives::{LogData, B256};
Expand Down Expand Up @@ -82,10 +83,10 @@ pub struct EnclaveSolReader;
impl EnclaveSolReader {
pub async fn attach(
bus: &Addr<EventBus>,
rpc_url: &str,
provider: &ReadonlyProvider,
contract_address: &str,
) -> Result<Addr<EvmEventReader>> {
let addr = EvmEventReader::attach(bus, rpc_url, extractor, contract_address).await?;
let addr = EvmEventReader::attach(bus, provider, extractor, contract_address).await?;
Ok(addr)
}
}
16 changes: 4 additions & 12 deletions packages/ciphernode/evm/src/enclave_sol_writer.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
use std::sync::Arc;

use crate::helpers::create_provider_with_signer;
use crate::helpers::ensure_http_rpc;
use crate::helpers::SignerProvider;
use actix::prelude::*;
use actix::Addr;
use alloy::signers::local::PrivateKeySigner;
use alloy::{primitives::Address, sol};
use alloy::{
primitives::{Bytes, U256},
rpc::types::TransactionReceipt,
};
use anyhow::Result;
use data::DataStore;
use enclave_core::Shutdown;
use enclave_core::{BusError, E3id, EnclaveErrorType, PlaintextAggregated, Subscribe};
use enclave_core::{EnclaveEvent, EventBus};
Expand All @@ -34,24 +28,22 @@ pub struct EnclaveSolWriter {
impl EnclaveSolWriter {
pub async fn new(
bus: &Addr<EventBus>,
rpc_url: &str,
provider: &SignerProvider,
contract_address: Address,
signer: &Arc<PrivateKeySigner>,
) -> Result<Self> {
Ok(Self {
provider: create_provider_with_signer(&ensure_http_rpc(rpc_url), signer).await?,
provider: provider.clone(),
contract_address,
bus: bus.clone(),
})
}

pub async fn attach(
bus: &Addr<EventBus>,
rpc_url: &str,
provider: &SignerProvider,
contract_address: &str,
signer: &Arc<PrivateKeySigner>,
) -> Result<Addr<EnclaveSolWriter>> {
let addr = EnclaveSolWriter::new(bus, rpc_url, contract_address.parse()?, signer)
let addr = EnclaveSolWriter::new(bus, provider, contract_address.parse()?)
.await?
.start();
bus.send(Subscribe::new("PlaintextAggregated", addr.clone().into()))
Expand Down
9 changes: 4 additions & 5 deletions packages/ciphernode/evm/src/event_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ pub struct EvmEventReader {
impl EvmEventReader {
pub async fn new(
bus: &Addr<EventBus>,
rpc_url: &str,
provider: &ReadonlyProvider,
extractor: ExtractorFn,
contract_address: Address,
) -> Result<Self> {
let (shutdown_tx, shutdown_rx) = oneshot::channel();
let provider = create_readonly_provider(&ensure_ws_rpc(rpc_url)).await?;
Ok(Self {
contract_address,
provider: Some(provider),
provider: Some(provider.clone()),
extractor,
bus: bus.clone().into(),
shutdown_rx: Some(shutdown_rx),
Expand All @@ -41,11 +40,11 @@ impl EvmEventReader {

pub async fn attach(
bus: &Addr<EventBus>,
rpc_url: &str,
provider: &ReadonlyProvider,
extractor: ExtractorFn,
contract_address: &str,
) -> Result<Addr<Self>> {
let addr = EvmEventReader::new(bus, rpc_url, extractor, contract_address.parse()?)
let addr = EvmEventReader::new(bus, provider, extractor, contract_address.parse()?)
.await?
.start();

Expand Down
2 changes: 2 additions & 0 deletions packages/ciphernode/evm/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ pub async fn stream_from_evm<P: Provider>(
info!("Exiting stream loop");
}

/// We need to cache the chainId so we can easily use it in a non-async situation
/// This wrapper just stores the chain_id with the Provider
#[derive(Clone)]
pub struct WithChainId<P>
where
Expand Down
19 changes: 7 additions & 12 deletions packages/ciphernode/evm/src/registry_filter_sol.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use crate::helpers::{create_provider_with_signer, ensure_http_rpc, SignerProvider};
use crate::helpers::SignerProvider;
use actix::prelude::*;
use alloy::{
primitives::{Address, Bytes, U256},
rpc::types::TransactionReceipt,
signers::local::PrivateKeySigner,
sol,
};
use anyhow::Result;
use enclave_core::{
BusError, E3id, EnclaveErrorType, EnclaveEvent, EventBus, OrderedSet, PublicKeyAggregated,
Shutdown, Subscribe,
};
use std::sync::Arc;
use tracing::info;

sol!(
Expand All @@ -29,24 +27,22 @@ pub struct RegistryFilterSolWriter {
impl RegistryFilterSolWriter {
pub async fn new(
bus: &Addr<EventBus>,
rpc_url: &str,
provider: &SignerProvider,
contract_address: Address,
signer: &Arc<PrivateKeySigner>,
) -> Result<Self> {
Ok(Self {
provider: create_provider_with_signer(&ensure_http_rpc(rpc_url), signer).await?,
provider: provider.clone(),
contract_address,
bus: bus.clone(),
})
}

pub async fn attach(
bus: &Addr<EventBus>,
rpc_url: &str,
provider: &SignerProvider,
contract_address: &str,
signer: &Arc<PrivateKeySigner>,
) -> Result<Addr<RegistryFilterSolWriter>> {
let addr = RegistryFilterSolWriter::new(bus, rpc_url, contract_address.parse()?, signer)
let addr = RegistryFilterSolWriter::new(bus, provider, contract_address.parse()?)
.await?
.start();
let _ = bus
Expand Down Expand Up @@ -132,11 +128,10 @@ pub struct RegistryFilterSol;
impl RegistryFilterSol {
pub async fn attach(
bus: &Addr<EventBus>,
rpc_url: &str,
provider: &SignerProvider,
contract_address: &str,
signer: &Arc<PrivateKeySigner>,
) -> Result<()> {
RegistryFilterSolWriter::attach(bus, rpc_url, contract_address, signer).await?;
RegistryFilterSolWriter::attach(bus, provider, contract_address).await?;
Ok(())
}
}

0 comments on commit eaea0a8

Please sign in to comment.