Skip to content

Commit

Permalink
catch up AZ audit module with changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gmawdo committed Jan 7, 2025
1 parent 8716287 commit 3c4347e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 38 deletions.
38 changes: 25 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion src/audit_adapters/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
41 changes: 19 additions & 22 deletions src/audit_adapters/substrate_based_audit.rs
Original file line number Diff line number Diff line change
@@ -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<PolkadotConfig>,
//account_id: AccountId32,
signer: Keypair,
tx_sender: Sender<AuditEvent>,
//rpc: LegacyRpcMethods<PolkadotConfig>,
tx_sender: tokio_mpsc::Sender<AuditEvent>,
}


Expand Down Expand Up @@ -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,
Expand All @@ -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<AuditEvent> {
fn get_sender(&self) -> &tokio_mpsc::Sender<AuditEvent> {
&self.tx_sender
}

fn spawn_event_handler(receiver: Receiver<AuditEvent>) -> Result<(), Box<dyn Error>> {
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<dyn IrrefutableAudit>,
mut receiver: tokio_mpsc::Receiver<AuditEvent>
) -> Result<(), Box<dyn Error>> {
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<dyn Error>> {
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();
Expand Down
4 changes: 2 additions & 2 deletions src/bin/graymamba/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -174,7 +174,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

#[cfg(feature = "az_audit")]
{
match SubstrateAuditSystem::new().await {
match SubstrateBasedAudit::new().await {
Ok(audit) => {
println!("✅ Aleph Zero audit initialization successful");
Arc::new(audit)
Expand Down

0 comments on commit 3c4347e

Please sign in to comment.