Improve dynamic events type sizes API (#61)

* Do not error if type sizes missing: log warn

* Split out EventsSubscriber, allow updating events decoder

* Format code

* Check missing type sizes after registration

* Add example which for submit and watching events
This commit is contained in:
Andrew Jones
2020-01-07 15:14:30 +00:00
committed by GitHub
parent 80663ec6e3
commit b159d0dae1
6 changed files with 130 additions and 57 deletions
+11 -32
View File
@@ -43,9 +43,9 @@ use crate::{
Metadata,
MetadataError,
},
Phase,
System,
SystemEvent,
Phase,
};
/// Top level Event that can be produced by a substrate runtime
@@ -72,18 +72,10 @@ pub enum EventsError {
CodecError(#[from] CodecError),
#[error("Metadata error: {0:?}")]
Metadata(#[from] MetadataError),
#[error("Type Sizes Missing: {0:?}")]
TypeSizesMissing(Vec<String>),
#[error("Type Sizes Unavailable: {0:?}")]
TypeSizeUnavailable(String),
}
impl From<Vec<String>> for EventsError {
fn from(error: Vec<String>) -> Self {
EventsError::TypeSizesMissing(error)
}
}
pub struct EventsDecoder<T> {
metadata: Metadata, // todo: [AJ] borrow?
type_sizes: HashMap<String, usize>,
@@ -120,16 +112,6 @@ impl<T: System + Balances + 'static> TryFrom<Metadata> for EventsDecoder<T> {
// VoteThreshold enum index
decoder.register_type_size::<u8>("VoteThreshold")?;
// Ignore these unregistered types, which are not fixed size primitives
decoder.check_missing_type_sizes(vec![
"DispatchInfo",
"DispatchError",
"Result<(), DispatchError>",
"OpaqueTimeSlot",
// FIXME: determine type size for the following if necessary/possible
"IdentificationTuple",
"AuthorityList",
])?;
Ok(decoder)
}
}
@@ -148,31 +130,28 @@ impl<T: System + Balances + 'static> EventsDecoder<T> {
}
}
fn check_missing_type_sizes<I: IntoIterator<Item = &'static str>>(
&self,
ignore: I,
) -> Result<(), Vec<String>> {
pub fn check_missing_type_sizes(&self) {
let mut missing = HashSet::new();
let mut ignore_set = HashSet::new();
ignore_set.extend(ignore);
for module in self.metadata.modules_with_events() {
for event in module.events() {
for arg in event.arguments() {
for primitive in arg.primitives() {
if !self.type_sizes.contains_key(&primitive)
&& !ignore_set.contains(primitive.as_str())
if module.name() != "System"
&& !self.type_sizes.contains_key(&primitive)
&& !primitive.contains("PhantomData")
{
missing.insert(primitive);
missing.insert(format!("{}::{}::{}", module.name(), event.name, primitive));
}
}
}
}
}
if missing.is_empty() {
Ok(())
} else {
Err(missing.into_iter().collect())
if missing.len() > 0 {
log::warn!(
"The following primitive types do not have registered sizes: {:?} \
If any of these events are received, an error will occur since we cannot decode them",
missing
);
}
}
+15 -10
View File
@@ -186,7 +186,8 @@ mod tests {
let wasm = wabt::wat2wasm(CONTRACT).expect("invalid wabt");
client.xt(signer, None).and_then(|xt| {
xt.submit_and_watch(super::put_code(500_000, wasm))
xt.watch()
.submit(super::put_code(500_000, wasm))
.map(|result| result.find_event::<T::Hash>(MODULE, events::CODE_STORED))
})
}
@@ -224,15 +225,19 @@ mod tests {
println!("{:?}", code_hash);
let instantiate = client.xt(signer, None).and_then(move |xt| {
xt.submit_and_watch(super::instantiate::<Runtime>(
100_000_000_000_000,
500_000,
code_hash,
Vec::new(),
))
.map(|result| {
result.find_event::<(AccountId, AccountId)>(MODULE, events::INSTANTIATED)
})
xt.watch()
.submit(super::instantiate::<Runtime>(
100_000_000_000_000,
500_000,
code_hash,
Vec::new(),
))
.map(|result| {
result.find_event::<(AccountId, AccountId)>(
MODULE,
events::INSTANTIATED,
)
})
});
let result = rt.block_on(instantiate).unwrap();
+48 -11
View File
@@ -72,7 +72,7 @@ pub use self::{
runtimes::*,
};
use self::{
events::EventsDecoder,
events::{EventsDecoder, EventsError},
extrinsic::{
DefaultExtra,
SignedExtra,
@@ -80,9 +80,9 @@ use self::{
frame::{
balances::Balances,
system::{
Phase,
System,
SystemEvent,
Phase,
SystemStore,
},
},
@@ -209,8 +209,7 @@ impl<T: System + Balances + 'static, S: 'static> Client<T, S> {
/// Get a block hash of the latest finalized block
pub fn finalized_head(&self) -> impl Future<Item = T::Hash, Error = Error> {
self.connect()
.and_then(|rpc| rpc.finalized_head())
self.connect().and_then(|rpc| rpc.finalized_head())
}
/// Get a block
@@ -280,6 +279,7 @@ impl<T: System + Balances + 'static, S: 'static> Client<T, S> {
}
/// Transaction builder.
#[derive(Clone)]
pub struct XtBuilder<T: System, P, S> {
client: Client<T, S>,
nonce: T::Index,
@@ -373,21 +373,58 @@ where
}
/// Submits transaction to the chain and watch for events.
pub fn submit_and_watch<C: Encode>(
&self,
pub fn watch(self) -> EventsSubscriber<T, P, S> {
let metadata = self.client.metadata().clone();
let decoder = EventsDecoder::try_from(metadata).map_err(Into::into);
EventsSubscriber {
client: self.client.clone(),
builder: self,
decoder,
}
}
}
/// Submits an extrinsic and subscribes to the triggered events
pub struct EventsSubscriber<T: System, P, S> {
client: Client<T, S>,
builder: XtBuilder<T, P, S>,
decoder: Result<EventsDecoder<T>, EventsError>,
}
impl<T: System + Balances + Send + Sync + 'static, P, S: 'static>
EventsSubscriber<T, P, S>
where
P: Pair,
S: Verify + Codec + From<P::Signature>,
S::Signer: From<P::Public> + IdentifyAccount<AccountId = T::AccountId>,
T::Address: From<T::AccountId>,
{
/// Access the events decoder for registering custom type sizes
pub fn events_decoder<F: FnOnce(&mut EventsDecoder<T>) -> Result<usize, EventsError>>(self, f: F) -> Self {
let mut this = self;
if let Ok(ref mut decoder) = this.decoder {
if let Err(err) = f(decoder) {
this.decoder = Err(err)
}
}
this
}
/// Submits transaction to the chain and watch for events.
pub fn submit<C: Encode>(
self,
call: Call<C>,
) -> impl Future<Item = ExtrinsicSuccess<T>, Error = Error> {
let cli = self.client.connect();
let metadata = self.client.metadata().clone();
let decoder = EventsDecoder::try_from(metadata)
.into_future()
.map_err(Into::into);
let decoder = self.decoder.into_future().map_err(Into::into);
self.create_and_sign(call)
self.builder
.create_and_sign(call)
.into_future()
.map_err(Into::into)
.join(decoder)
.and_then(move |(extrinsic, decoder)| {
decoder.check_missing_type_sizes();
cli.and_then(move |rpc| {
rpc.submit_and_watch_extrinsic(extrinsic, decoder)
})
+1 -1
View File
@@ -244,7 +244,7 @@ impl<K: Encode, V: Decode + Clone> StorageMap<K, V> {
.chain(&encoded_key)
.cloned()
.collect::<Vec<_>>()
},
}
StorageHasher::Blake2_256 => sp_core::blake2_256(&encoded_key).to_vec(),
StorageHasher::Twox128 => sp_core::twox_128(&encoded_key).to_vec(),
StorageHasher::Twox256 => sp_core::twox_256(&encoded_key).to_vec(),
+1 -3
View File
@@ -170,9 +170,7 @@ impl<T: System> Rpc<T> {
/// Get a block hash of the latest finalized block
pub fn finalized_head(&self) -> impl Future<Item = T::Hash, Error = Error> {
self.chain
.finalized_head()
.map_err(Into::into)
self.chain.finalized_head().map_err(Into::into)
}
/// Get a Block