Skip to content

Commit

Permalink
Consolidate providers
Browse files Browse the repository at this point in the history
  • Loading branch information
ryardley committed Oct 31, 2024
1 parent 516dea2 commit 14cfe86
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 28 deletions.
16 changes: 12 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,19 @@ 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, &chain.contracts.enclave, &signer).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
7 changes: 4 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,7 @@ 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 +42,9 @@ 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
10 changes: 5 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,18 @@ 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(())
}
}
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)
}
}
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
15 changes: 6 additions & 9 deletions packages/ciphernode/evm/src/registry_filter_sol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,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 +130,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 14cfe86

Please sign in to comment.