ac-node-api 1.21.0

Substrate node type definitions and helpers for the substrate-api-client
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// This file was taken from subxt (Parity Technologies (UK))
// https://github.com/paritytech/subxt/
// And was adapted by Supercomputing Systems AG and Integritee AG.
//
// Copyright 2019-2022 Parity Technologies (UK) Ltd, Supercomputing Systems AG and Integritee AG.
// This file is licensed as Apache-2.0
// see LICENSE for license details.

//! A representation of a block of events.
//! This file bases on https://github.com/paritytech/subxt/blob/8413c4d2dd625335b9200dc2289670accdf3391a/subxt/src/events/events_type.rs#L19-L196

use crate::{error::Error, metadata::PalletMetadata, Metadata, StaticEvent};
use alloc::{sync::Arc, vec::Vec};
use codec::{Compact, Decode, Encode};

mod event_details;
mod raw_event_details;
pub use event_details::EventDetails;
pub use raw_event_details::RawEventDetails;

/// A collection of events obtained from a block, bundled with the necessary
/// information needed to decode and iterate over them.
#[derive(Clone)]
pub struct Events<Hash> {
	metadata: Metadata,
	block_hash: Hash,
	// Note; raw event bytes are prefixed with a Compact<u32> containing
	// the number of events to be decoded. The start_idx reflects that, so
	// that we can skip over those bytes when decoding them
	event_bytes: Arc<[u8]>,
	start_idx: usize,
	num_events: u32,
}

// Ignore the Metadata when debug-logging events; it's big and distracting.
impl<Hash: core::fmt::Debug> core::fmt::Debug for Events<Hash> {
	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
		f.debug_struct("Events")
			.field("block_hash", &self.block_hash)
			.field("event_bytes", &self.event_bytes)
			.field("start_idx", &self.start_idx)
			.field("num_events", &self.num_events)
			.finish()
	}
}

impl<Hash: Copy + Encode + Decode> Events<Hash> {
	pub fn new(metadata: Metadata, block_hash: Hash, event_bytes: Vec<u8>) -> Self {
		// event_bytes is a SCALE encoded vector of events. So, pluck the
		// compact encoded length from the front, leaving the remaining bytes
		// for our iterating to decode.
		//
		// Note: if we get no bytes back, avoid an error reading vec length
		// and default to 0 events.
		let cursor = &mut &*event_bytes;
		let num_events = <Compact<u32>>::decode(cursor).unwrap_or(Compact(0)).0;

		// Start decoding after the compact encoded bytes.
		let start_idx = event_bytes.len() - cursor.len();

		Self { metadata, block_hash, event_bytes: event_bytes.into(), start_idx, num_events }
	}

	/// The number of events.
	pub fn len(&self) -> u32 {
		self.num_events
	}

	/// Are there no events in this block?
	// Note: mainly here to satisfy clippy.
	pub fn is_empty(&self) -> bool {
		self.num_events == 0
	}

	/// Return the block hash that these events are from.
	pub fn block_hash(&self) -> Hash {
		self.block_hash
	}

	/// Return the encoded bytes of the Events.
	pub fn event_bytes(&self) -> Arc<[u8]> {
		self.event_bytes.clone()
	}

	/// Iterate over all of the events, using metadata to dynamically
	/// decode them as we go, and returning the raw bytes and other associated
	/// details. If an error occurs, all subsequent iterations return `None`.
	// Dev note: The returned iterator is 'static + Send so that we can box it up and make
	// use of it with our `FilterEvents` stuff.
	pub fn iter(
		&self,
	) -> impl Iterator<Item = Result<EventDetails<Hash>, Error>> + Send + Sync + 'static {
		// The event bytes ignoring the compact encoded length on the front:
		let event_bytes = self.event_bytes.clone();
		let metadata = self.metadata.clone();
		let num_events = self.num_events;

		let mut pos = self.start_idx;
		let mut index = 0;
		core::iter::from_fn(move || {
			if event_bytes.len() <= pos || num_events == index {
				None
			} else {
				match EventDetails::decode_from(metadata.clone(), event_bytes.clone(), pos, index) {
					Ok(event_details) => {
						// Skip over decoded bytes in next iteration:
						pos += event_details.bytes().len();
						// Increment the index:
						index += 1;
						// Return the event details:
						Some(Ok(event_details))
					},
					Err(e) => {
						// By setting the position to the "end" of the event bytes,
						// the cursor len will become 0 and the iterator will return `None`
						// from now on:
						pos = event_bytes.len();
						Some(Err(e))
					},
				}
			}
		})
	}

	/// Iterate through the events using metadata to dynamically decode and skip
	/// them, and return only those which should decode to the provided `Ev` type.
	/// If an error occurs, all subsequent iterations return `None`.
	pub fn find<Ev: StaticEvent>(&self) -> impl Iterator<Item = Result<Ev, Error>> + '_ {
		self.iter().filter_map(|ev| ev.and_then(|ev| ev.as_event::<Ev>()).transpose())
	}

	/// Iterate through the events using metadata to dynamically decode and skip
	/// them, and return the first event found which decodes to the provided `Ev` type.
	pub fn find_first<Ev: StaticEvent>(&self) -> Result<Option<Ev>, Error> {
		self.find::<Ev>().next().transpose()
	}

	/// Iterate through the events using metadata to dynamically decode and skip
	/// them, and return the last event found which decodes to the provided `Ev` type.
	pub fn find_last<Ev: StaticEvent>(&self) -> Result<Option<Ev>, Error> {
		self.find::<Ev>().last().transpose()
	}

	/// Find an event that decodes to the type provided. Returns true if it was found.
	pub fn has<Ev: StaticEvent>(&self) -> Result<bool, Error> {
		Ok(self.find::<Ev>().next().transpose()?.is_some())
	}
}

/// This trait is implemented on the statically generated root event type, so that we're able
/// to decode it properly via a pallet event that impls `DecodeAsMetadata`. This is necessary
/// becasue the "root event" type is generated using pallet info but doesn't actually exist in the
/// metadata types, so we have no easy way to decode things into it via type information and need a
/// little help via codegen.
// Based on https://github.com/paritytech/subxt/blob/8413c4d2dd625335b9200dc2289670accdf3391a/subxt/src/events/events_type.rs#L417-L432
#[doc(hidden)]
pub trait RootEvent: Sized {
	/// Given details of the pallet event we want to decode, and the name of the pallet, try to hand
	/// back a "root event".
	fn root_event(
		pallet_bytes: &[u8],
		pallet_name: &str,
		pallet_event_ty: u32,
		metadata: &Metadata,
	) -> Result<Self, Error>;
}

/// Details for the given event plucked from the metadata.
// Based on https://github.com/paritytech/subxt/blob/8413c4d2dd625335b9200dc2289670accdf3391a/subxt/src/events/events_type.rs#L411-L415
#[derive(Clone)]
pub struct EventMetadataDetails<'a> {
	pub pallet: PalletMetadata<'a>,
	pub variant: &'a scale_info::Variant<scale_info::form::PortableForm>,
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::{
		test_utils::{
			event_record, events, events_raw, metadata_with_version, SupportedMetadataVersions,
		},
		Phase,
	};
	use codec::Encode;
	use scale_info::TypeInfo;
	use scale_value::Value;
	use sp_core::H256;
	use test_case::test_case;

	/// [`RawEventDetails`] can be annoying to test, because it contains
	/// type info in the decoded field Values. Strip that here so that
	/// we can compare fields more easily.
	#[derive(Debug, PartialEq, Clone)]
	pub struct TestRawEventDetails {
		pub phase: Phase,
		pub index: u32,
		pub pallet: String,
		pub pallet_index: u8,
		pub variant: String,
		pub variant_index: u8,
		pub fields: Vec<Value>,
	}

	/// Compare some actual [`RawEventDetails`] with a hand-constructed
	/// (probably) [`TestRawEventDetails`].
	pub fn assert_raw_events_match(actual: EventDetails<H256>, expected: TestRawEventDetails) {
		let actual_fields_no_context: Vec<_> = actual
			.field_values()
			.expect("can decode field values (2)")
			.into_values()
			.map(|value| value.remove_context())
			.collect();

		// Check each of the other fields:
		assert_eq!(actual.phase(), expected.phase);
		assert_eq!(actual.index(), expected.index);
		assert_eq!(actual.pallet_name(), expected.pallet);
		assert_eq!(actual.pallet_index(), expected.pallet_index);
		assert_eq!(actual.variant_name(), expected.variant);
		assert_eq!(actual.variant_index(), expected.variant_index);
		assert_eq!(actual_fields_no_context, expected.fields);
	}

	#[test_case(SupportedMetadataVersions::V14)]
	#[test_case(SupportedMetadataVersions::V15)]
	fn dynamically_decode_single_event(metadata_version: SupportedMetadataVersions) {
		#[derive(Clone, Debug, PartialEq, Decode, Encode, TypeInfo)]
		enum Event {
			A(u8, bool, Vec<String>),
		}

		// Create fake metadata that knows about our single event, above:
		let metadata = metadata_with_version::<Event>(metadata_version);

		// Encode our events in the format we expect back from a node, and
		// construst an Events object to iterate them:
		let event = Event::A(1, true, vec!["Hi".into()]);
		let events = events::<Event>(
			metadata.clone(),
			vec![event_record(Phase::ApplyExtrinsic(123), event)],
		);

		let mut event_details = events.iter();
		assert_raw_events_match(
			event_details.next().unwrap().unwrap(),
			TestRawEventDetails {
				phase: Phase::ApplyExtrinsic(123),
				index: 0,
				pallet: "Test".to_string(),
				pallet_index: 0,
				variant: "A".to_string(),
				variant_index: 0,
				fields: vec![
					Value::u128(1u128),
					Value::bool(true),
					Value::unnamed_composite(vec![Value::string("Hi")]),
				],
			},
		);
		assert!(event_details.next().is_none());
	}

	#[test_case(SupportedMetadataVersions::V14)]
	#[test_case(SupportedMetadataVersions::V15)]
	fn dynamically_decode_multiple_events(metadata_version: SupportedMetadataVersions) {
		#[derive(Clone, Copy, Debug, PartialEq, Decode, Encode, TypeInfo)]
		enum Event {
			A(u8),
			B(bool),
		}

		// Create fake metadata that knows about our single event, above:
		let metadata = metadata_with_version::<Event>(metadata_version);

		// Encode our events in the format we expect back from a node, and
		// construst an Events object to iterate them:
		let event1 = Event::A(1);
		let event2 = Event::B(true);
		let event3 = Event::A(234);

		let events = events::<Event>(
			metadata.clone(),
			vec![
				event_record(Phase::Initialization, event1),
				event_record(Phase::ApplyExtrinsic(123), event2),
				event_record(Phase::Finalization, event3),
			],
		);

		let mut event_details = events.iter();

		assert_raw_events_match(
			event_details.next().unwrap().unwrap(),
			TestRawEventDetails {
				index: 0,
				phase: Phase::Initialization,
				pallet: "Test".to_string(),
				pallet_index: 0,
				variant: "A".to_string(),
				variant_index: 0,
				fields: vec![Value::u128(1u128)],
			},
		);
		assert_raw_events_match(
			event_details.next().unwrap().unwrap(),
			TestRawEventDetails {
				index: 1,
				phase: Phase::ApplyExtrinsic(123),
				pallet: "Test".to_string(),
				pallet_index: 0,
				variant: "B".to_string(),
				variant_index: 1,
				fields: vec![Value::bool(true)],
			},
		);
		assert_raw_events_match(
			event_details.next().unwrap().unwrap(),
			TestRawEventDetails {
				index: 2,
				phase: Phase::Finalization,
				pallet: "Test".to_string(),
				pallet_index: 0,
				variant: "A".to_string(),
				variant_index: 0,
				fields: vec![Value::u128(234u128)],
			},
		);
		assert!(event_details.next().is_none());
	}

	#[test_case(SupportedMetadataVersions::V14)]
	#[test_case(SupportedMetadataVersions::V15)]
	fn dynamically_decode_multiple_events_until_error(metadata_version: SupportedMetadataVersions) {
		#[derive(Clone, Debug, PartialEq, Decode, Encode, TypeInfo)]
		enum Event {
			A(u8),
			B(bool),
		}

		// Create fake metadata that knows about our single event, above:
		let metadata = metadata_with_version::<Event>(metadata_version);

		// Encode 2 events:
		let mut event_bytes = vec![];
		event_record(Phase::Initialization, Event::A(1)).encode_to(&mut event_bytes);
		event_record(Phase::ApplyExtrinsic(123), Event::B(true)).encode_to(&mut event_bytes);

		// Push a few naff bytes to the end (a broken third event):
		event_bytes.extend_from_slice(&[3, 127, 45, 0, 2]);

		// Encode our events in the format we expect back from a node, and
		// construst an Events object to iterate them:
		let events = events_raw(
			metadata.clone(),
			event_bytes,
			3, // 2 "good" events, and then it'll hit the naff bytes.
		);

		let mut events_iter = events.iter();
		assert_raw_events_match(
			events_iter.next().unwrap().unwrap(),
			TestRawEventDetails {
				index: 0,
				phase: Phase::Initialization,
				pallet: "Test".to_string(),
				pallet_index: 0,
				variant: "A".to_string(),
				variant_index: 0,
				fields: vec![Value::u128(1u128)],
			},
		);
		assert_raw_events_match(
			events_iter.next().unwrap().unwrap(),
			TestRawEventDetails {
				index: 1,
				phase: Phase::ApplyExtrinsic(123),
				pallet: "Test".to_string(),
				pallet_index: 0,
				variant: "B".to_string(),
				variant_index: 1,
				fields: vec![Value::bool(true)],
			},
		);

		// We'll hit an error trying to decode the third event:
		assert!(events_iter.next().unwrap().is_err());
		// ... and then "None" from then on.
		assert!(events_iter.next().is_none());
		assert!(events_iter.next().is_none());
	}

	#[test_case(SupportedMetadataVersions::V14)]
	#[test_case(SupportedMetadataVersions::V15)]
	fn compact_event_field(metadata_version: SupportedMetadataVersions) {
		#[derive(Clone, Debug, PartialEq, Encode, Decode, TypeInfo)]
		enum Event {
			A(#[codec(compact)] u32),
		}

		// Create fake metadata that knows about our single event, above:
		let metadata = metadata_with_version::<Event>(metadata_version);

		// Encode our events in the format we expect back from a node, and
		// construst an Events object to iterate them:
		let events =
			events::<Event>(metadata.clone(), vec![event_record(Phase::Finalization, Event::A(1))]);

		// Dynamically decode:
		let mut event_details = events.iter();
		assert_raw_events_match(
			event_details.next().unwrap().unwrap(),
			TestRawEventDetails {
				index: 0,
				phase: Phase::Finalization,
				pallet: "Test".to_string(),
				pallet_index: 0,
				variant: "A".to_string(),
				variant_index: 0,
				fields: vec![Value::u128(1u128)],
			},
		);
		assert!(event_details.next().is_none());
	}

	#[test_case(SupportedMetadataVersions::V14)]
	#[test_case(SupportedMetadataVersions::V15)]
	fn compact_wrapper_struct_field(metadata_version: SupportedMetadataVersions) {
		#[derive(Clone, Decode, Debug, PartialEq, Encode, TypeInfo)]
		enum Event {
			A(#[codec(compact)] CompactWrapper),
		}

		#[derive(Clone, Decode, Debug, PartialEq, codec::CompactAs, Encode, TypeInfo)]
		struct CompactWrapper(u64);

		// Create fake metadata that knows about our single event, above:
		let metadata = metadata_with_version::<Event>(metadata_version);

		// Encode our events in the format we expect back from a node, and
		// construct an Events object to iterate them:
		let events = events::<Event>(
			metadata.clone(),
			vec![event_record(Phase::Finalization, Event::A(CompactWrapper(1)))],
		);

		// Dynamically decode:
		let mut event_details = events.iter();
		assert_raw_events_match(
			event_details.next().unwrap().unwrap(),
			TestRawEventDetails {
				index: 0,
				phase: Phase::Finalization,
				pallet: "Test".to_string(),
				pallet_index: 0,
				variant: "A".to_string(),
				variant_index: 0,
				fields: vec![Value::unnamed_composite(vec![Value::u128(1)])],
			},
		);
		assert!(event_details.next().is_none());
	}

	#[test_case(SupportedMetadataVersions::V14)]
	#[test_case(SupportedMetadataVersions::V15)]
	fn event_containing_explicit_index(metadata_version: SupportedMetadataVersions) {
		#[derive(Clone, Debug, PartialEq, Eq, Decode, Encode, TypeInfo)]
		#[repr(u8)]
		#[allow(trivial_numeric_casts, clippy::unnecessary_cast)] // required because the Encode derive produces a warning otherwise
		pub enum MyType {
			B = 10u8,
		}

		#[derive(Clone, Debug, PartialEq, Decode, Encode, TypeInfo)]
		enum Event {
			A(MyType),
		}

		// Create fake metadata that knows about our single event, above:
		let metadata = metadata_with_version::<Event>(metadata_version);

		// Encode our events in the format we expect back from a node, and
		// construct an Events object to iterate them:
		let events = events::<Event>(
			metadata.clone(),
			vec![event_record(Phase::Finalization, Event::A(MyType::B))],
		);

		// Dynamically decode:
		let mut event_details = events.iter();
		assert_raw_events_match(
			event_details.next().unwrap().unwrap(),
			TestRawEventDetails {
				index: 0,
				phase: Phase::Finalization,
				pallet: "Test".to_string(),
				pallet_index: 0,
				variant: "A".to_string(),
				variant_index: 0,
				fields: vec![Value::unnamed_variant("B", vec![])],
			},
		);
		assert!(event_details.next().is_none());
	}
}