From 3c4347ed85b95e5e9debe28e4e0404c6418f26db Mon Sep 17 00:00:00 2001 From: Gary Mawdsley <79173+gmawdo@users.noreply.github.com> Date: Tue, 7 Jan 2025 13:09:59 +0000 Subject: [PATCH] catch up AZ audit module with changes --- Cargo.toml | 38 ++++++++++++------- src/audit_adapters/mod.rs | 2 +- src/audit_adapters/substrate_based_audit.rs | 41 ++++++++++----------- src/bin/graymamba/main.rs | 4 +- 4 files changed, 47 insertions(+), 38 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0e32704..95b4453 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -64,19 +64,19 @@ config = "0.11" chrono = "0.4" # required for Polkadot substrate, Aleph Zero blockchain integration -#subxt = { version = "0.37.0", optional = true } -#subxt-signer = { version = "0.37.0", optional = true } +subxt = { version = "0.37.0", optional = true } +subxt-signer = { version = "0.37.0", optional = true } # Substrate specific -#frame-support = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "master", optional = true } -#frame-system = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "master", optional = true } -#pallet-balances = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "master", optional = true } -#pallet-identity = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "master", optional = true } -#pallet-staking = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "master", optional = true } +frame-support = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "master", optional = true } +frame-system = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "master", optional = true } +pallet-balances = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "master", optional = true } +pallet-identity = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "master", optional = true } +pallet-staking = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "master", optional = true } -#sp-core = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "master" , optional = true} -#sp-keyring = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "master" , optional = true} -#sp-runtime = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "master" , optional = true} -#sp-weights = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "master" , optional = true} +sp-core = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "master" , optional = true} +sp-keyring = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "master" , optional = true} +sp-runtime = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "master" , optional = true} +sp-weights = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "master" , optional = true} sha2 = "0.10" parking_lot = "0.12" @@ -113,10 +113,22 @@ http-body-util = "0.1" [features] strict = [] #traceability = ["tracing-subscriber", "tokio/rt-multi-thread", "intaglio"] -default = ["merkle_audit","rocksdb_store","compressed_store"] +#default = ["merkle_audit","rocksdb_store","compressed_store"] compressed_store = [] merkle_audit = [] -az_audit = [] +az_audit = [ + "subxt", + "subxt-signer", + "frame-support", + "frame-system", + "pallet-balances", + "pallet-identity", + "pallet-staking", + "sp-core", + "sp-keyring", + "sp-runtime", + "sp-weights" +] redis_store = [] rocksdb_store = [] metrics = ["prometheus", "hyper"] diff --git a/src/audit_adapters/mod.rs b/src/audit_adapters/mod.rs index c71a17a..d88d00e 100644 --- a/src/audit_adapters/mod.rs +++ b/src/audit_adapters/mod.rs @@ -1,7 +1,7 @@ pub mod audit_system; pub mod merkle_audit; pub mod merkle_tree; -//pub mod substrate_based_audit; +pub mod substrate_based_audit; pub mod poseidon_hash; pub mod snark_proof; pub mod irrefutable_audit; \ No newline at end of file diff --git a/src/audit_adapters/substrate_based_audit.rs b/src/audit_adapters/substrate_based_audit.rs index 6b59c52..7574d4f 100644 --- a/src/audit_adapters/substrate_based_audit.rs +++ b/src/audit_adapters/substrate_based_audit.rs @@ -1,29 +1,27 @@ use crate::audit_adapters::irrefutable_audit::{IrrefutableAudit, AuditEvent, AuditError}; use async_trait::async_trait; use std::error::Error; - -use std::sync::mpsc::{self, Receiver, Sender}; -use std::thread; +use std::sync::Arc; +use tokio::sync::mpsc::{self as tokio_mpsc}; use config::{Config, File}; use subxt::backend::legacy::LegacyRpcMethods; use subxt::backend::rpc::RpcClient; -//use subxt::utils::AccountId32; use subxt::OnlineClient; use subxt::PolkadotConfig; use subxt_signer::sr25519::dev; use subxt_signer::sr25519::Keypair; -use tokio::runtime::Runtime; #[subxt::subxt(runtime_metadata_path = "metadata.scale")] pub mod pallet_template {} +use tracing::debug; + +#[derive(Clone)] pub struct SubstrateBasedAudit { api: OnlineClient, - //account_id: AccountId32, signer: Keypair, - tx_sender: Sender, - //rpc: LegacyRpcMethods, + tx_sender: tokio_mpsc::Sender, } @@ -72,7 +70,7 @@ impl IrrefutableAudit for SubstrateBasedAudit { //let account_id: AccountId32 = dev::alice().public_key().into(); let signer = dev::alice(); - let (tx_sender, rx) = mpsc::channel(); + let (tx_sender, rx) = tokio_mpsc::channel(100); let audit = SubstrateBasedAudit { api, @@ -81,31 +79,30 @@ impl IrrefutableAudit for SubstrateBasedAudit { }; // Spawn the event handler - Self::spawn_event_handler(rx)?; + Self::spawn_event_handler(Arc::new(audit.clone()), rx)?; Ok(audit) } - fn get_sender(&self) -> &Sender { + fn get_sender(&self) -> &tokio_mpsc::Sender { &self.tx_sender } - fn spawn_event_handler(receiver: Receiver) -> Result<(), Box> { - thread::spawn(move || { - let rt = Runtime::new().unwrap(); - rt.block_on(async move { - while let Ok(event) = receiver.recv() { - // Just process the event - no new connection needed - debug!("Processing event: {:?}", event); - // TODO: Add actual event processing logic here - } - }); + fn spawn_event_handler( + audit: Arc, + mut receiver: tokio_mpsc::Receiver + ) -> Result<(), Box> { + tokio::spawn(async move { + while let Some(event) = receiver.recv().await { + debug!("Processing event: {:?}", event); + // TODO: Add actual event processing logic here + } }); Ok(()) } async fn process_event(&self, event: AuditEvent) -> Result<(), Box> { - debug("Processing event: {:?}", event); + debug!("Processing event: {:?}", event); let event_type_bytes = event.event_type.clone().into_bytes(); let creation_time = event.creation_time.into_bytes(); diff --git a/src/bin/graymamba/main.rs b/src/bin/graymamba/main.rs index a199498..b5823b4 100644 --- a/src/bin/graymamba/main.rs +++ b/src/bin/graymamba/main.rs @@ -7,7 +7,7 @@ use graymamba::audit_adapters::irrefutable_audit::IrrefutableAudit; use graymamba::audit_adapters::merkle_audit::MerkleBasedAuditSystem; #[cfg(feature = "az_audit")] -use graymamba::audit_adapters::substrate_audit::SubstrateAuditSystem; +use graymamba::audit_adapters::substrate_based_audit::SubstrateBasedAudit; use config::{Config, File as ConfigFile}; @@ -174,7 +174,7 @@ async fn main() -> Result<(), Box> { #[cfg(feature = "az_audit")] { - match SubstrateAuditSystem::new().await { + match SubstrateBasedAudit::new().await { Ok(audit) => { println!("✅ Aleph Zero audit initialization successful"); Arc::new(audit)