mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 22:11:06 +00:00
Runtime agnostic Events (#20)
* Introduce OpaqueEvent * Look up event by module and variant * Index events by module * Get events by module * Dynamically decode events * Decode System events and EventRecord topics * Use type sizes to decode raw events * Remove unused imports * rustfmt * Unify error types, fix some compiler errors * Make dynamic event decoding work - fix compilation errors - skip modules with no events when indexing - preallocate vec for raw event data * Remove printlns, replace where required with log * Remove unused import * Check missing type sizes * Ignore unknown event arg type sizes * Decode concrete System events, assumes every Runtime has the module * Reorganise usings * pub use some types * Code docs * Export Error * Error Display impls * Format code
This commit is contained in:
+150
-15
@@ -13,16 +13,21 @@ use runtime_metadata::{
|
||||
META_RESERVED,
|
||||
};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
collections::{
|
||||
HashMap,
|
||||
HashSet,
|
||||
},
|
||||
convert::TryFrom,
|
||||
marker::PhantomData,
|
||||
str::FromStr,
|
||||
};
|
||||
use substrate_primitives::storage::StorageKey;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, derive_more::Display)]
|
||||
pub enum MetadataError {
|
||||
ModuleNotFound(&'static str),
|
||||
ModuleNotFound(String),
|
||||
CallNotFound(&'static str),
|
||||
EventNotFound(u8),
|
||||
StorageNotFound(&'static str),
|
||||
StorageTypeError,
|
||||
MapValueTypeError,
|
||||
@@ -31,15 +36,31 @@ pub enum MetadataError {
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Metadata {
|
||||
modules: HashMap<String, ModuleMetadata>,
|
||||
modules_by_event_index: HashMap<u8, String>,
|
||||
}
|
||||
|
||||
impl Metadata {
|
||||
pub fn module(&self, name: &'static str) -> Result<&ModuleMetadata, MetadataError> {
|
||||
pub fn modules(&self) -> impl Iterator<Item = &ModuleMetadata> {
|
||||
self.modules.values()
|
||||
}
|
||||
|
||||
pub fn module<S>(&self, name: S) -> Result<&ModuleMetadata, MetadataError>
|
||||
where
|
||||
S: ToString,
|
||||
{
|
||||
let name = name.to_string();
|
||||
self.modules
|
||||
.get(name)
|
||||
.get(&name)
|
||||
.ok_or(MetadataError::ModuleNotFound(name))
|
||||
}
|
||||
|
||||
pub fn module_name(&self, module_index: u8) -> Result<String, MetadataError> {
|
||||
self.modules_by_event_index
|
||||
.get(&module_index)
|
||||
.cloned()
|
||||
.ok_or(MetadataError::EventNotFound(module_index))
|
||||
}
|
||||
|
||||
pub fn pretty(&self) -> String {
|
||||
let mut string = String::new();
|
||||
for (name, module) in &self.modules {
|
||||
@@ -55,9 +76,9 @@ impl Metadata {
|
||||
string.push_str(call.as_str());
|
||||
string.push('\n');
|
||||
}
|
||||
for (event, _) in &module.events {
|
||||
for (_, event) in &module.events {
|
||||
string.push_str(" e ");
|
||||
string.push_str(event.as_str());
|
||||
string.push_str(event.name.as_str());
|
||||
string.push('\n');
|
||||
}
|
||||
}
|
||||
@@ -67,14 +88,19 @@ impl Metadata {
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ModuleMetadata {
|
||||
index: Vec<u8>,
|
||||
index: u8,
|
||||
name: String,
|
||||
storage: HashMap<String, StorageMetadata>,
|
||||
calls: HashMap<String, Vec<u8>>,
|
||||
events: HashMap<String, Vec<u8>>,
|
||||
events: HashMap<u8, ModuleEventMetadata>,
|
||||
// constants
|
||||
}
|
||||
|
||||
impl ModuleMetadata {
|
||||
pub fn name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
|
||||
pub fn call<T: Encode>(
|
||||
&self,
|
||||
function: &'static str,
|
||||
@@ -84,7 +110,7 @@ impl ModuleMetadata {
|
||||
.calls
|
||||
.get(function)
|
||||
.ok_or(MetadataError::CallNotFound(function))?;
|
||||
let mut bytes = self.index.clone();
|
||||
let mut bytes = vec![self.index];
|
||||
bytes.extend(fn_bytes);
|
||||
bytes.extend(params.encode());
|
||||
Ok(Encoded(bytes))
|
||||
@@ -95,6 +121,16 @@ impl ModuleMetadata {
|
||||
.get(key)
|
||||
.ok_or(MetadataError::StorageNotFound(key))
|
||||
}
|
||||
|
||||
pub fn events(&self) -> impl Iterator<Item = &ModuleEventMetadata> {
|
||||
self.events.values()
|
||||
}
|
||||
|
||||
pub fn event(&self, index: u8) -> Result<&ModuleEventMetadata, MetadataError> {
|
||||
self.events
|
||||
.get(&index)
|
||||
.ok_or(MetadataError::EventNotFound(index))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
@@ -158,11 +194,86 @@ impl<K: Encode, V: Decode + Clone> StorageMap<K, V> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ModuleEventMetadata {
|
||||
pub name: String,
|
||||
arguments: HashSet<EventArg>,
|
||||
}
|
||||
|
||||
impl ModuleEventMetadata {
|
||||
pub fn arguments(&self) -> Vec<EventArg> {
|
||||
self.arguments.iter().cloned().collect()
|
||||
}
|
||||
}
|
||||
|
||||
/// Naive representation of event argument types, supports current set of substrate EventArg types.
|
||||
/// If and when Substrate uses `type-metadata`, this can be replaced.
|
||||
///
|
||||
/// Used to calculate the size of a instance of an event variant without having the concrete type,
|
||||
/// so the raw bytes can be extracted from the encoded `Vec<EventRecord<E>>` (without `E` defined).
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
|
||||
pub enum EventArg {
|
||||
Primitive(String),
|
||||
Vec(Box<EventArg>),
|
||||
Tuple(Vec<EventArg>),
|
||||
}
|
||||
|
||||
impl FromStr for EventArg {
|
||||
type Err = Error;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
if s.starts_with("Vec<") {
|
||||
if s.ends_with('>') {
|
||||
Ok(EventArg::Vec(Box::new(s[4..s.len() - 1].parse()?)))
|
||||
} else {
|
||||
Err(Error::InvalidEventArg(
|
||||
s.to_string(),
|
||||
"Expected closing `>` for `Vec`",
|
||||
))
|
||||
}
|
||||
} else if s.starts_with("(") {
|
||||
if s.ends_with(")") {
|
||||
let mut args = Vec::new();
|
||||
for arg in s[1..s.len() - 1].split(',') {
|
||||
let arg = arg.trim().parse()?;
|
||||
args.push(arg)
|
||||
}
|
||||
Ok(EventArg::Tuple(args))
|
||||
} else {
|
||||
Err(Error::InvalidEventArg(
|
||||
s.to_string(),
|
||||
"Expecting closing `)` for tuple",
|
||||
))
|
||||
}
|
||||
} else {
|
||||
Ok(EventArg::Primitive(s.to_string()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl EventArg {
|
||||
/// Returns all primitive types for this EventArg
|
||||
pub fn primitives(&self) -> Vec<String> {
|
||||
match self {
|
||||
EventArg::Primitive(p) => vec![p.clone()],
|
||||
EventArg::Vec(arg) => arg.primitives(),
|
||||
EventArg::Tuple(args) => {
|
||||
let mut primitives = Vec::new();
|
||||
for arg in args {
|
||||
primitives.extend(arg.primitives())
|
||||
}
|
||||
primitives
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
InvalidPrefix,
|
||||
InvalidVersion,
|
||||
ExpectedDecoded,
|
||||
InvalidEventArg(String, &'static str),
|
||||
}
|
||||
|
||||
impl TryFrom<RuntimeMetadataPrefixed> for Metadata {
|
||||
@@ -177,10 +288,22 @@ impl TryFrom<RuntimeMetadataPrefixed> for Metadata {
|
||||
_ => Err(Error::InvalidVersion)?,
|
||||
};
|
||||
let mut modules = HashMap::new();
|
||||
let mut modules_by_event_index = HashMap::new();
|
||||
let mut event_index = 0;
|
||||
for (i, module) in convert(meta.modules)?.into_iter().enumerate() {
|
||||
modules.insert(convert(module.name.clone())?, convert_module(i, module)?);
|
||||
let module_name = convert(module.name.clone())?;
|
||||
let module_metadata = convert_module(i, module)?;
|
||||
// modules with no events have no corresponding definition in the top level enum
|
||||
if !module_metadata.events.is_empty() {
|
||||
modules_by_event_index.insert(event_index, module_name.clone());
|
||||
event_index = event_index + 1;
|
||||
}
|
||||
modules.insert(module_name, module_metadata);
|
||||
}
|
||||
Ok(Metadata { modules })
|
||||
Ok(Metadata {
|
||||
modules,
|
||||
modules_by_event_index,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -216,18 +339,30 @@ fn convert_module(
|
||||
let mut event_map = HashMap::new();
|
||||
if let Some(events) = module.event {
|
||||
for (index, event) in convert(events)?.into_iter().enumerate() {
|
||||
let name = convert(event.name)?;
|
||||
event_map.insert(name, vec![index as u8]);
|
||||
event_map.insert(index as u8, convert_event(event)?);
|
||||
}
|
||||
}
|
||||
Ok(ModuleMetadata {
|
||||
index: vec![index as u8],
|
||||
index: index as u8,
|
||||
name: convert(module.name)?,
|
||||
storage: storage_map,
|
||||
calls: call_map,
|
||||
events: event_map,
|
||||
})
|
||||
}
|
||||
|
||||
fn convert_event(
|
||||
event: runtime_metadata::EventMetadata,
|
||||
) -> Result<ModuleEventMetadata, Error> {
|
||||
let name = convert(event.name)?;
|
||||
let mut arguments = HashSet::new();
|
||||
for arg in convert(event.arguments)? {
|
||||
let arg = arg.parse::<EventArg>()?;
|
||||
arguments.insert(arg);
|
||||
}
|
||||
Ok(ModuleEventMetadata { name, arguments })
|
||||
}
|
||||
|
||||
fn convert_entry(
|
||||
prefix: String,
|
||||
entry: runtime_metadata::StorageEntryMetadata,
|
||||
|
||||
Reference in New Issue
Block a user