diff --git a/src/content.rs b/src/content.rs index 4f5c190..fdbabef 100644 --- a/src/content.rs +++ b/src/content.rs @@ -1,4 +1,5 @@ use crate::DataEntity; +use crate::DataEvent; use subxt::Phase; /// Is this extrinsic part of the overheads of running this blockchain? @@ -8,7 +9,7 @@ pub fn is_utility_extrinsic(event: &DataEntity) -> bool { &DataEntity::Extrinsic { ref details, .. } => { return is_boring(details.pallet.as_str(), details.variant.as_str()); } - &DataEntity::Event { ref raw, .. } => { + &DataEntity::Event(DataEvent { ref raw, .. }) => { !matches!(raw.phase, Phase::ApplyExtrinsic(_)) || is_boring(&raw.pallet, &raw.variant) } } @@ -36,9 +37,9 @@ fn is_boring(pallet: &str, variant: &str) -> bool { } } -pub fn is_message(entry: &DataEntity) -> bool { +pub fn is_event_message(entry: &DataEvent) -> bool { match entry { - &DataEntity::Event { ref raw, .. } => { + &DataEvent { ref raw, .. } => { matches!( raw.pallet.as_str().to_ascii_lowercase().as_str(), "ump" | "dmpqueue" | "polkadotxcm" | "xcmpallet" @@ -47,3 +48,10 @@ pub fn is_message(entry: &DataEntity) -> bool { _ => false, } } + +pub fn is_message(entry: &DataEntity) -> bool { + match entry { + &DataEntity::Event(ref event) => is_event_message(event), + _ => false, + } +} diff --git a/src/datasource.rs b/src/datasource.rs index f036cbd..a75f4fb 100644 --- a/src/datasource.rs +++ b/src/datasource.rs @@ -2,6 +2,7 @@ use super::polkadot; use crate::polkadot::runtime_types::xcm::VersionedXcm; use crate::ABlocks; use crate::DataEntity; +use crate::DataEvent; use crate::Details; use async_std::stream::StreamExt; use async_std::sync::RwLock; @@ -494,7 +495,6 @@ pub async fn watch_blocks( }, "horizontal_messages" => { if let ValueDef::Composite(Composite::Unnamed(vals)) = &val.value { - for val in vals { // channels if let ValueDef::Composite(Composite::Unnamed(vals)) = &val.value { @@ -506,7 +506,7 @@ pub async fn watch_blocks( // for val in vals { //msgs if let ValueDef::Composite(Composite::Unnamed(vals)) = &val.value { - if vals.len() > 0 { + if vals.len() > 0 { for m in vals { if let ValueDef::Primitive(Primitive::U32(_from_para_id))= &m.value { // println!("from {}", from_para_id); @@ -517,7 +517,7 @@ pub async fn watch_blocks( flattern(&val.value, "",&mut results); println!("INNER {:#?}", results); //Could be that these are not yet in the wild - std::process::exit(1); + std::process::exit(1); } } } @@ -528,7 +528,7 @@ pub async fn watch_blocks( } } } - }, + }, "downward_messages" => { if let ValueDef::Composite(Composite::Unnamed(vals)) = &val.value { for val in vals { @@ -541,13 +541,12 @@ pub async fn watch_blocks( let bytes = hex::decode(msg).unwrap(); if let Ok(ver_msg) = ::decode(&mut bytes.as_slice()) { match ver_msg { - VersionedXcm::V0(msg) => { + VersionedXcm::V0(msg) => { // Only one xcm instruction in a v1 message. let instruction = format!("{:?}", &msg); println!("instruction {:?}", &instruction); children.push(DataEntity::Extrinsic { id: (block_header.number, i as u32), - args: vec![instruction.clone()], contains: vec![], raw: vec![], //TODO: should be simples @@ -567,15 +566,15 @@ pub async fn watch_blocks( println!("RECIEVE HASH v0 {}", msg_id); link.push(msg_id); }; - } else { panic!("unknonwn") } } + } else { panic!("unknonwn") } + } } - VersionedXcm::V1(msg) => { + VersionedXcm::V1(msg) => { // Only one xcm instruction in a v1 message. let instruction = format!("{:?}", &msg); println!("instruction {:?}", &instruction); children.push(DataEntity::Extrinsic { id: (block_header.number, i as u32), - args: vec![instruction.clone()], contains: vec![], raw: vec![], //TODO: should be simples @@ -605,16 +604,15 @@ pub async fn watch_blocks( println!("instruction {:?}", &instruction); children.push(DataEntity::Extrinsic { id: (block_header.number, i as u32), - args: vec![instruction.clone()], contains: vec![], raw: vec![], //TODO: should be simples link: vec![], details: Details - { + { pallet: "Instruction".to_string(), variant: instruction.split_once(' ').unwrap_or((&instruction,"")).0.to_string(), - ..Details::default() + ..Details::default() } }); } @@ -854,8 +852,6 @@ pub async fn watch_blocks( flattern(&arg.value, &arg_index.to_string(),&mut results); } // println!("FLATTERN UMP {:#?}", results); - - // args.insert(0, format!("{results:#?}")); exts.push(DataEntity::Extrinsic { @@ -909,9 +905,9 @@ pub struct PolkaBlock { pub blocknum: usize, pub blockhash: H256, pub extrinsics: Vec, - pub events: Vec, + pub events: Vec, } - +use core::slice::SlicePattern; pub async fn watch_events(tx: ABlocks, url: &str) -> Result<(), Box> { let api = ClientBuilder::new() .set_url(url) @@ -926,26 +922,42 @@ pub async fn watch_events(tx: ABlocks, url: &str) -> Result<(), Box::decode(&mut msg.as_slice()) { - println!("decodearama {:#?}!!!!", ver_msg); - } else { - println!("booo didn't decode!!!! {}", hex::encode(msg.as_slice())); + let mut data_events = vec![]; + for ev_raw in events.iter_raw() { + let mut details = Details::default(); + let mut link = vec![]; + + if let Ok(ev) = &ev_raw { + let ev = ::decode(&mut ev.data.as_slice()); + if let Ok(event) = ev { + // println!("{:#?}", ev); + // if let EventDetails { event, .. } = ev { + if let polkadot::Event::Ump(polkadot::runtime_types::polkadot_runtime_parachains::ump::pallet::Event::ExecutedUpward(ref msg, ..)) = event { //.pallet == "Ump" && ev.variant == "ExecutedUpward" { + println!("got here rnrtnrtrtnrt"); + println!("got here rnrtnrtrtnrt"); + println!("got here rnrtnrtrtnrt"); + println!("got here rnrtnrtrtnrt"); + println!("got here rnrtnrtrtnrt"); + println!("{:#?}", event); + + let received_hash = format!("{}-{}",blockhash, hex::encode(msg)); + println!("recieved UMP hash {}", &received_hash); + link.push(received_hash); + // // msg is a msg id! not decodable - match against hash of original + // if let Ok(ver_msg) = ::decode(&mut msg.as_slice()) { + // println!("decodearama {:#?}!!!!", ver_msg); + // } else { + // println!("booo didn't decode!!!! {}", hex::encode(msg.as_slice())); + // } } } + // } } + data_events.push(DataEvent { + raw: ev_raw.unwrap(), + link, + details, + }) } tx.lock().unwrap().0.insert( @@ -954,7 +966,7 @@ pub async fn watch_events(tx: ABlocks, url: &str) -> Result<(), Box>(), + events: data_events, }, ); } @@ -964,9 +976,9 @@ pub async fn watch_events(tx: ABlocks, url: &str) -> Result<(), Box, - mut events: Vec, -) -> Vec<(Option, Vec)> { - let mut ext: Vec<(Option, Vec)> = ext + mut events: Vec, +) -> Vec<(Option, Vec)> { + let mut ext: Vec<(Option, Vec)> = ext .into_iter() .map(|extrinsic| { let eid = if let DataEntity::Extrinsic { @@ -981,8 +993,8 @@ pub fn associate_events( ( Some(extrinsic), events - .drain_filter(|raw| match &raw.phase { - subxt::Phase::ApplyExtrinsic(extrinsic_id) => *extrinsic_id == eid, + .drain_filter(|ev| match ev.raw.phase { + subxt::Phase::ApplyExtrinsic(extrinsic_id) => extrinsic_id == eid, _ => false, }) .collect(), @@ -1034,36 +1046,24 @@ mod tests { // let result = // ::decode(&mut result.0.as_slice()).unwrap(); - - let result = - ::decode(&mut result.0.as_slice()); + let result = ::decode( + &mut result.0.as_slice(), + ); println!("{:?}", result); } #[test] - fn decode_xcm_cant_transact_error(){ - use crate::polkadot::runtime_types::xcm::v2::traits::Outcome; + fn decode_xcm_cant_transact_error() { use crate::polkadot::runtime_types::xcm::v2::traits::Error; - let msg = vec![ - 1u8, - 0, - 202, - 154, - 59, - 0, - 0, - 0, - 0, - 9, - ]; - let result = - ::decode(&mut msg.as_slice()).unwrap(); + use crate::polkadot::runtime_types::xcm::v2::traits::Outcome; + let msg = vec![1u8, 0, 202, 154, 59, 0, 0, 0, 0, 9]; + let result = ::decode(&mut msg.as_slice()).unwrap(); if let Outcome::Incomplete(_weight, Error::FailedToTransactAsset) = result { // The thing only has a string message locally!!! //(err_msg) - // println!("err msg: {}", err_msg); + // println!("err msg: {}", err_msg); } println!("{:?}", result); } -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index e33fb2e..f792a6f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,13 +2,13 @@ #![feature(hash_drain_filter)] #![feature(slice_pattern)] use bevy::ecs as bevy_ecs; -use bevy::winit::WinitSettings; use bevy::prelude::*; +use bevy::winit::WinitSettings; use bevy_ecs::prelude::Component; use bevy_flycam::FlyCam; use bevy_flycam::MovementSettings; use bevy_flycam::NoCameraPlayerPlugin; -use bevy_inspector_egui::{Inspectable, InspectorPlugin, plugin::InspectorWindows}; +use bevy_inspector_egui::{plugin::InspectorWindows, Inspectable, InspectorPlugin}; use bevy_mod_picking::*; //use bevy_egui::render_systems::ExtractedWindowSizes; use bevy_polyline::{prelude::*, PolylinePlugin}; @@ -24,10 +24,10 @@ mod movement; mod style; use crate::details::Details; use bevy_inspector_egui::RegisterInspectable; +#[cfg(feature = "spacemouse")] +use bevy_spacemouse::{SpaceMouseControllable, SpaceMousePlugin}; use sp_core::H256; use std::convert::AsRef; -#[cfg(feature="spacemouse")] -use bevy_spacemouse::{SpaceMouseControllable, SpaceMousePlugin}; // #[subxt::subxt(runtime_metadata_path = "wss://kusama-rpc.polkadot.io:443")] // pub mod polkadot {} @@ -84,14 +84,14 @@ async fn main() -> Result<(), Box> { .insert_resource(MovementSettings { sensitivity: 0.00020, // default: 0.00012 speed: 12.0, // default: 12.0 - }) + }) .insert_resource(movement::MouseCapture::default()) .add_plugin(NoCameraPlayerPlugin); - #[cfg(feature="spacemouse")] - app.add_plugin(SpaceMousePlugin); + #[cfg(feature = "spacemouse")] + app.add_plugin(SpaceMousePlugin); - app.add_plugins(DefaultPickingPlugins) + app.add_plugins(DefaultPickingPlugins) // .add_plugin(DebugCursorPickingPlugin) // <- Adds the green debug cursor. .add_plugin(InspectorPlugin::::new()) .register_inspectable::
() @@ -140,8 +140,7 @@ async fn main() -> Result<(), Box> { ) }, ) - .add_system_to_stage(CoreStage::PostUpdate, print_events) - ; + .add_system_to_stage(CoreStage::PostUpdate, print_events); for (relay_id, relay) in relays.into_iter().enumerate() { for (arc, mut chain_name) in relay { @@ -267,7 +266,7 @@ enum BuildDirection { fn format_entity(chain_name: &str, entity: &DataEntity) -> String { let res = match entity { - DataEntity::Event { raw, .. } => { + DataEntity::Event(DataEvent { raw, .. }) => { format!("{:#?}", raw) } DataEntity::Extrinsic { @@ -298,10 +297,7 @@ fn format_entity(chain_name: &str, entity: &DataEntity) -> String { #[derive(Clone)] pub enum DataEntity { - Event { - raw: RawEventDetails, - details: Details, - }, + Event(DataEvent), Extrinsic { id: (u32, u32), // pallet: String, @@ -317,6 +313,13 @@ pub enum DataEntity { }, } +#[derive(Clone)] +pub struct DataEvent { + raw: RawEventDetails, + details: Details, + link: Vec, +} + /// A tag to identify an entity as being the source of a message. #[derive(Component)] pub struct MessageSource { @@ -330,33 +333,33 @@ static EMPTY_BYTE_SLICE: Vec = vec![]; impl DataEntity { pub fn details(&self) -> &Details { match self { - Self::Event { details, .. } => details, + Self::Event(DataEvent { details, .. }) => details, Self::Extrinsic { details, .. } => details, } } pub fn pallet(&self) -> &str { match self { - Self::Event { raw, .. } => raw.pallet.as_ref(), + Self::Event(DataEvent { raw, .. }) => raw.pallet.as_ref(), Self::Extrinsic { details, .. } => &details.pallet, } } pub fn variant(&self) -> &str { match self { - Self::Event { raw, .. } => raw.variant.as_ref(), + Self::Event(DataEvent { raw, .. }) => raw.variant.as_ref(), Self::Extrinsic { details, .. } => &details.variant, } } pub fn contains(&self) -> &[DataEntity] { match self { - Self::Event { .. } => EMPTY_SLICE.as_slice(), + Self::Event(DataEvent { .. }) => EMPTY_SLICE.as_slice(), Self::Extrinsic { contains, .. } => contains.as_slice(), } } pub fn as_bytes(&self) -> &[u8] { match self { - Self::Event { .. } => EMPTY_BYTE_SLICE.as_slice(), + Self::Event(DataEvent { .. }) => EMPTY_BYTE_SLICE.as_slice(), Self::Extrinsic { raw, .. } => raw.as_slice(), } } @@ -364,7 +367,7 @@ impl DataEntity { pub fn link(&self) -> &Vec { match self { Self::Extrinsic { link, .. } => &link, - Self::Event { .. } => &EMPTY_VEC, + Self::Event(DataEvent { .. }) => &EMPTY_VEC, } } } @@ -589,7 +592,7 @@ fn add_blocks<'a>( chain_info: &ChainInfo, block_num: u32, chain: usize, - block_events: Vec<(Option, Vec)>, + block_events: Vec<(Option, Vec)>, commands: &mut Commands, meshes: &mut ResMut>, materials: &mut ResMut>, @@ -727,7 +730,7 @@ fn add_blocks<'a>( id: link.to_string(), }); } - }; + } let mut bun = commands.spawn_bundle(PbrBundle { mesh, @@ -765,25 +768,26 @@ fn add_blocks<'a>( for event in events { let details = Details { - hover: format!("{:#?}", event), - flattern: String::new(), + hover: format!("{:#?}", event.raw), + // flattern: String::new(), url: format!( "https://polkadot.js.org/apps/?{}#/explorer/query/{}", &encoded, &hex_block_hash ), - pallet: event.pallet.to_string(), - variant: event.variant.to_string(), + // pallet: event.raw.pallet.to_string(), + // variant: event.raw.variant.to_string(), + ..event.details.clone() }; - let entity = DataEntity::Event { - raw: (*event).clone(), + let entity = DataEvent { details, + ..event.clone() }; - let style = style::style_event(&entity); + let style = style::style_data_event(&entity); let material = mat_map .entry(style.clone()) .or_insert_with(|| materials.add(style.color.clone().into())); - let mesh = if content::is_message(&entity) { + let mesh = if content::is_event_message(&entity) { mesh_xcm.clone() } else { mesh.clone() @@ -807,7 +811,7 @@ fn add_blocks<'a>( ..Default::default() }) .insert_bundle(PickableBundle::default()) - .insert(entity.details().clone()) + .insert(entity.details.clone()) .insert(Rainable { dest: target_y * build_direction, }) @@ -948,15 +952,15 @@ pub fn print_events( mut events: EventReader, mut query2: Query, mut inspector: ResMut, - // mut inspector_windows: Res, + // mut inspector_windows: Res, ) { for event in events.iter() { - match event { + match event { PickingEvent::Selection(selection) => { if let SelectionEvent::JustSelected(entity) = selection { - // let mut inspector_window_data = inspector_windows.window_data::
(); - // let window_size = &world.get_resource::().unwrap().0[&self.window_id]; - + // let mut inspector_window_data = inspector_windows.window_data::
(); + // let window_size = &world.get_resource::().unwrap().0[&self.window_id]; + // let selection = query.get_mut(*entity).unwrap(); // Unspawn the previous text: @@ -1066,28 +1070,28 @@ fn setup( //somehow this can change the color // mesh_highlighting(None, None, None); // camera - - let mut entity_comands = commands - .spawn_bundle(PerspectiveCameraBundle { - transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y), - perspective_projection: PerspectiveProjection { - // far: 1., // 1000 will be 100 blocks that you can s - far: 0.0001, - near: 0.000001, - ..default() - }, - camera: Camera { - far: 0.0001, - near: 0.000001, - ..default() - }, + let mut entity_comands = commands.spawn_bundle(PerspectiveCameraBundle { + transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y), + perspective_projection: PerspectiveProjection { + // far: 1., // 1000 will be 100 blocks that you can s + far: 0.0001, + near: 0.000001, ..default() - }); - entity_comands.insert(FlyCam) + }, + camera: Camera { + far: 0.0001, + near: 0.000001, + + ..default() + }, + ..default() + }); + entity_comands + .insert(FlyCam) .insert_bundle(PickingCameraBundle { ..default() }); - #[cfg(feature="spacemouse")] + #[cfg(feature = "spacemouse")] entity_comands.insert(SpaceMouseControllable); use std::time::Duration; diff --git a/src/style.rs b/src/style.rs index 9555221..f33ce5d 100644 --- a/src/style.rs +++ b/src/style.rs @@ -1,3 +1,5 @@ +use crate::DataEvent; + use super::DataEntity; use bevy::render::color::Color; @@ -34,29 +36,7 @@ use palette::FromColor; pub fn style_event(entry: &DataEntity) -> ExStyle { let msg = crate::content::is_message(entry); match entry { - DataEntity::Event { raw, .. } => { - if matches!((raw.pallet.as_str(), raw.variant.as_str()), - ("System","ExtrinsicFailed") - // | ("PolkadotXcm", "Attempted") - only an error if !completed variant. - ) { - return ExStyle { - color: Color::rgb(1., 0., 0.), - }; - } - - let color = palette::Lchuv::new( - 80., - 80. + (calculate_hash(&raw.variant) as f32 % 100.), - (calculate_hash(&raw.pallet) as f32) % 360., - ); - let rgb: palette::rgb::Srgb = palette::rgb::Srgb::from_color(color); - - // println!("rgb {} {} {}", rgb.red, rgb.green, rgb.blue); - - ExStyle { - color: Color::rgb(rgb.red, rgb.green, rgb.blue), - } - } + DataEntity::Event(data_event @ DataEvent { .. }) => style_data_event(data_event), // match event.pallet.as_str() { // "Staking" => ExStyle { // color: Color::hex("00ffff").unwrap(), @@ -85,3 +65,29 @@ pub fn style_event(entry: &DataEntity) -> ExStyle { } } } + +pub fn style_data_event(entry: &DataEvent) -> ExStyle { + let msg = crate::content::is_event_message(entry); + let raw = &entry.raw; + if matches!( + (raw.pallet.as_str(), raw.variant.as_str()), + ("System", "ExtrinsicFailed") // | ("PolkadotXcm", "Attempted") - only an error if !completed variant. + ) { + return ExStyle { + color: Color::rgb(1., 0., 0.), + }; + } + + let color = palette::Lchuv::new( + 80., + 80. + (calculate_hash(&raw.variant) as f32 % 100.), + (calculate_hash(&raw.pallet) as f32) % 360., + ); + let rgb: palette::rgb::Srgb = palette::rgb::Srgb::from_color(color); + + // println!("rgb {} {} {}", rgb.red, rgb.green, rgb.blue); + + ExStyle { + color: Color::rgb(rgb.red, rgb.green, rgb.blue), + } +}