diff --git a/Cargo.lock b/Cargo.lock index f16547d4a..b24d399bb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4224,7 +4224,6 @@ dependencies = [ "static_assertions", "test-case", "thiserror-no-std", - "ts-rs", ] [[package]] @@ -4255,7 +4254,6 @@ dependencies = [ "serde", "sp-api", "sp-std", - "ts-rs", ] [[package]] @@ -5981,7 +5979,6 @@ dependencies = [ "sp-blockchain", "sp-core", "sp-runtime", - "ts-rs", ] [[package]] @@ -14621,27 +14618,6 @@ dependencies = [ "zstd 0.12.4", ] -[[package]] -name = "ts-rs" -version = "11.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ef1b7a6d914a34127ed8e1fa927eb7088903787bcded4fa3eef8f85ee1568be" -dependencies = [ - "thiserror 2.0.11", - "ts-rs-macros", -] - -[[package]] -name = "ts-rs-macros" -version = "11.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e9d4ed7b4c18cc150a6a0a1e9ea1ecfa688791220781af6e119f9599a8502a0a" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.96", -] - [[package]] name = "tt-call" version = "1.0.9" diff --git a/Cargo.toml b/Cargo.toml index 660f89b7f..134217f50 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -118,7 +118,6 @@ jsonrpsee = { version = "0.22", features = ["server", "client", "macros"] } moka = { version = "0.12.1", features = ["future"] } lru = "0.7.2" tempfile = "3.1.0" -ts-rs = { version = "11.0.1", default-features = false } # Ethereum rlp = { git = "https://github.com/paritytech/parity-common.git", tag = "rlp-v0.5.2", default-features = false } diff --git a/pallets/system/Cargo.toml b/pallets/system/Cargo.toml index 33790ed40..b93f1d240 100644 --- a/pallets/system/Cargo.toml +++ b/pallets/system/Cargo.toml @@ -31,7 +31,6 @@ derive_more.workspace = true hex-literal.workspace = true itertools = { workspace = true, default-features = false } thiserror-no-std.workspace = true -ts-rs = { workspace = true, optional = true } # Substrate codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false, features = [ "derive"] } @@ -100,8 +99,6 @@ secure_padding_fill = [] testing-environment = [] -ts = ["ts-rs"] - [[bench]] name = "bench" harness = false diff --git a/pallets/system/rpc/runtime-api/Cargo.toml b/pallets/system/rpc/runtime-api/Cargo.toml index 45849b252..45595605c 100644 --- a/pallets/system/rpc/runtime-api/Cargo.toml +++ b/pallets/system/rpc/runtime-api/Cargo.toml @@ -19,9 +19,7 @@ scale-info = { workspace = true, default-features = false } frame-system = { workspace = true, default-features = false } serde = { workspace = true, default-features = false } sp-std = { workspace = true, default-features = false } -ts-rs = { workspace = true, optional = true } [features] default = [ "std" ] std = [ "codec/std", "sp-api/std", "serde/std", "frame-system/std" ] -ts = ["ts-rs"] diff --git a/pallets/system/rpc/runtime-api/src/lib.rs b/pallets/system/rpc/runtime-api/src/lib.rs index d17ca2792..53a61d360 100644 --- a/pallets/system/rpc/runtime-api/src/lib.rs +++ b/pallets/system/rpc/runtime-api/src/lib.rs @@ -23,8 +23,8 @@ #![cfg_attr(not(feature = "std"), no_std)] -use system_events_api::fetch_events_v1::ApiResult as FetchEventsResult; -use system_events_api::fetch_events_v1::Options as FetchEventsOptions; +use system_events_api::fetch_events::ApiResult as FetchEventsResult; +use system_events_api::fetch_events::Options as FetchEventsOptions; sp_api::decl_runtime_apis! { /// The API to query account nonce. @@ -38,14 +38,14 @@ sp_api::decl_runtime_apis! { pub trait SystemEventsApi { - fn fetch_events_v1(options: FetchEventsOptions) -> FetchEventsResult; + fn fetch_events(options: FetchEventsOptions) -> FetchEventsResult; } } pub mod system_events_api { use sp_std::vec::Vec; - pub mod fetch_events_v1 { + pub mod fetch_events { use super::*; pub const MAX_INDICES_COUNT: usize = 30; @@ -55,18 +55,13 @@ pub mod system_events_api { #[derive(Clone, Default, scale_info::TypeInfo, codec::Decode, codec::Encode)] #[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))] - #[cfg_attr(feature = "ts", derive(ts_rs::TS))] - #[cfg_attr(feature = "ts", ts(export, export_to = "FetchEvents.ts"))] pub struct Options { pub filter: Option, - pub enable_encoding: Option, - pub enable_decoding: Option, + pub fetch_data: Option, } #[derive(Clone, scale_info::TypeInfo, codec::Decode, codec::Encode)] #[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))] - #[cfg_attr(feature = "ts", derive(ts_rs::TS))] - #[cfg_attr(feature = "ts", ts(export, export_to = "FetchEvents.ts"))] #[repr(u8)] pub enum Filter { All = 0, @@ -130,22 +125,19 @@ pub mod system_events_api { pub index: u32, // (Pallet Id, Event Id) pub emitted_index: (u8, u8), - pub encoded: Option>, - pub decoded: Option>, + pub data: Option>, } impl RuntimeEvent { pub fn new( index: u32, emitted_index: (u8, u8), - encoded: Option>, - decoded: Option>, + data: Option>, ) -> Self { Self { index, emitted_index, - encoded, - decoded, + data, } } } diff --git a/pallets/system/src/lib.rs b/pallets/system/src/lib.rs index 74d8055a4..6a338f158 100644 --- a/pallets/system/src/lib.rs +++ b/pallets/system/src/lib.rs @@ -1062,8 +1062,6 @@ pub type KeyValue = (Vec, Vec); /// A phase of a block's execution. #[derive(Encode, Decode, RuntimeDebug, TypeInfo, MaxEncodedLen, Clone, Copy, PartialEq, Eq)] #[cfg_attr(feature = "std", derive(Serialize, Deserialize))] -#[cfg_attr(feature = "ts", derive(ts_rs::TS))] -#[cfg_attr(feature = "ts", ts(export, export_to = "FetchEvents.ts"))] pub enum Phase { /// Applying an extrinsic. ApplyExtrinsic(u32), diff --git a/rpc/kate-rpc/Cargo.toml b/rpc/kate-rpc/Cargo.toml index 7df27a17a..597c37b4e 100644 --- a/rpc/kate-rpc/Cargo.toml +++ b/rpc/kate-rpc/Cargo.toml @@ -31,7 +31,6 @@ sp-runtime = { workspace = true, default-features = false } sc-service = { workspace = true, default-features = false } sp-core = { workspace = true, default-features = false } frame-system-rpc-runtime-api = { workspace = true, default-features = false } -ts-rs = { workspace = true, optional = true } [features] default = [ "std" ] @@ -54,4 +53,3 @@ std = [ # NOTE: WIP because that affects the Data Availability Protocol used during # the block import process. secure_padding_fill = [] -ts = ["ts-rs"] diff --git a/rpc/kate-rpc/src/justifications.rs b/rpc/kate-rpc/src/justifications.rs index fc94c8a09..93507eaec 100644 --- a/rpc/kate-rpc/src/justifications.rs +++ b/rpc/kate-rpc/src/justifications.rs @@ -192,36 +192,26 @@ impl<'de> serde::Deserialize<'de> for Signature { } #[derive(Clone, codec::Decode, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "ts", derive(ts_rs::TS))] -#[cfg_attr(feature = "ts", ts(export, export_to = "BlockJustification.ts"))] pub struct Precommit { /// The target block's hash. - #[cfg_attr(feature = "ts", ts(as = "String"))] pub target_hash: H256, /// The target block's number pub target_number: u32, } #[derive(Clone, codec::Decode, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "ts", derive(ts_rs::TS))] -#[cfg_attr(feature = "ts", ts(export, export_to = "BlockJustification.ts"))] pub struct SignedPrecommit { /// The precommit message which has been signed. pub precommit: Precommit, /// The signature on the message. - #[cfg_attr(feature = "ts", ts(as = "String"))] pub signature: Signature, /// The Id of the signer. - #[cfg_attr(feature = "ts", ts(as = "String"))] pub id: AuthorityId, } #[derive(Clone, codec::Decode, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "ts", derive(ts_rs::TS))] -#[cfg_attr(feature = "ts", ts(export, export_to = "BlockJustification.ts"))] pub struct Commit { /// The target block's hash. - #[cfg_attr(feature = "ts", ts(as = "String"))] pub target_hash: H256, /// The target block's number. pub target_number: u32, @@ -230,11 +220,8 @@ pub struct Commit { } #[derive(Clone, codec::Decode, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "ts", derive(ts_rs::TS))] -#[cfg_attr(feature = "ts", ts(export, export_to = "BlockJustification.ts"))] pub struct GrandpaJustification { pub round: u64, pub commit: Commit, - #[cfg_attr(feature = "ts", ts(as = "Vec"))] pub votes_ancestries: Vec
, } diff --git a/rpc/kate-rpc/src/system.rs b/rpc/kate-rpc/src/system.rs index d8457e802..24e729562 100644 --- a/rpc/kate-rpc/src/system.rs +++ b/rpc/kate-rpc/src/system.rs @@ -18,22 +18,18 @@ use std::{ #[rpc(client, server)] pub trait Api { - #[method(name = "system_fetchEventsV1")] - async fn fetch_events_v1( + #[method(name = "system_fetchEvents")] + async fn fetch_events( &self, at: H256, - options: Option, - ) -> RpcResult; + options: Option, + ) -> RpcResult; - #[method(name = "system_fetchExtrinsicsV1")] - async fn fetch_extrinsics_v1( + #[method(name = "system_fetchExtrinsics")] + async fn fetch_extrinsics( &self, - block_id: fetch_extrinsics_v1::BlockId, - options: Option, - ) -> RpcResult; - - #[method(name = "system_latestBlockInfo")] - async fn latest_block_info(&self, use_best_block: Option) -> RpcResult; + params: fetch_extrinsics::Params, + ) -> RpcResult; #[method(name = "system_latestChainInfo")] async fn latest_chain_info(&self) -> RpcResult; @@ -49,7 +45,7 @@ where Block: BlockT, { pub client: Arc, - pub block_cache: Arc>, + pub block_cache: Arc>, _phantom: PhantomData, } impl Rpc @@ -63,7 +59,7 @@ where pub fn new(client: Arc) -> Self { Self { client, - block_cache: Arc::new(Mutex::new(fetch_extrinsics_v1::Cache::new(5))), + block_cache: Arc::new(Mutex::new(fetch_extrinsics::Cache::new(5))), _phantom: PhantomData, } } @@ -110,16 +106,16 @@ where <::Header as HeaderT>::Number: From, <::Header as HeaderT>::Number: Into, { - async fn fetch_events_v1( + async fn fetch_events( &self, at: H256, - options: Option, - ) -> RpcResult { - use fetch_events_v1::GroupedRuntimeEvents; + options: Option, + ) -> RpcResult { + use fetch_events::GroupedRuntimeEvents; let runtime_api = self.client.runtime_api(); let result = runtime_api - .fetch_events_v1(at.into(), options.unwrap_or_default()) + .fetch_events(at.into(), options.unwrap_or_default()) .map_err(|x| Error::RuntimeApi.into_error_object(x.to_string()))?; match result { @@ -129,31 +125,19 @@ where } } - async fn fetch_extrinsics_v1( + async fn fetch_extrinsics( &self, - block_id: fetch_extrinsics_v1::BlockId, - options: Option, - ) -> RpcResult { - use fetch_extrinsics_v1::{ - BlockId, EncodeSelector, ExtrinsicInformation, TransactionFilterOptions, + params: fetch_extrinsics::Params, + ) -> RpcResult { + use fetch_extrinsics::{ + BlockId, EncodeSelector, ExtrinsicFilterOptions, ExtrinsicInformation, }; - let options = options.unwrap_or_default(); - let filter = options.filter.unwrap_or_default(); - let tx_filter = filter.transaction.unwrap_or_default(); - let sig_filter = filter.signature.unwrap_or_default(); - let encode_selector = options.encode_selector.unwrap_or_default(); - - if !tx_filter.is_valid() { - return Err(Error::InvalidInput - .into_error_object(String::from("Transaction filter: Invalid input"))); - } - if !sig_filter.is_valid() { - return Err(Error::InvalidInput - .into_error_object(String::from("Signature filter: Invalid input"))); + if !params.is_valid() { + return Err(Error::InvalidInput.into_error_object(String::from("Invalid input"))); } - let block_hash = match block_id { + let block_hash = match params.block_id { BlockId::Hash(h) => h, BlockId::Number(n) => { let hash = match self.client.block_hash(n.into()) { @@ -174,34 +158,34 @@ where let cached_block = match cache.block(block_hash) { Some(block) => block, None => { - let block = fetch_extrinsics_v1::cache_block::(&self.client, block_hash)?; + let block = fetch_extrinsics::cache_block::(&self.client, block_hash)?; cache.insert(block_hash, block) }, }; - let transactions = cached_block.transactions(); - let mut found_extrinsics = match &tx_filter { - TransactionFilterOptions::All => Vec::with_capacity(transactions.len()), - TransactionFilterOptions::TxHash(list) => Vec::with_capacity(list.len()), - TransactionFilterOptions::TxIndex(list) => Vec::with_capacity(list.len()), + let extrinsics = cached_block.transactions(); + let mut found_extrinsics = match ¶ms.extrinsic { + ExtrinsicFilterOptions::All => Vec::with_capacity(extrinsics.len()), + ExtrinsicFilterOptions::TxHash(list) => Vec::with_capacity(list.len()), + ExtrinsicFilterOptions::TxIndex(list) => Vec::with_capacity(list.len()), _ => Vec::new(), }; - for tx in transactions.iter() { - if !tx_filter.filter_in_tx_index(tx.index) || !tx_filter.filter_in_tx_hash(tx.tx_hash) { + for tx in extrinsics.iter() { + if !params.filter_in_tx_index(tx.index) || !params.filter_in_tx_hash(tx.tx_hash) { continue; } - if !tx_filter.filter_in_pallet(tx.dispatch_index.0) - || !tx_filter.filter_in_pallet_call(tx.dispatch_index) + if !params.filter_in_pallet(tx.dispatch_index.0) + || !params.filter_in_pallet_call(tx.dispatch_index) { continue; } - if !sig_filter.filter_in(&tx.signature) { + if !params.filter_in(&tx.signature) { continue; } - let encoded = match encode_selector { + let encoded = match params.encode_selector { EncodeSelector::None => None, EncodeSelector::Call => Some(tx.tx_encoded[tx.call_start_pos..].to_string()), EncodeSelector::Extrinsic => Some(tx.tx_encoded.clone()), @@ -217,13 +201,13 @@ where }; found_extrinsics.push(ext_info); - if let TransactionFilterOptions::TxIndex(list) = &tx_filter { + if let ExtrinsicFilterOptions::TxIndex(list) = ¶ms.extrinsic { if found_extrinsics.len() >= list.len() { break; } } - if let TransactionFilterOptions::TxHash(list) = &tx_filter { + if let ExtrinsicFilterOptions::TxHash(list) = ¶ms.extrinsic { if found_extrinsics.len() >= list.len() { break; } @@ -235,22 +219,6 @@ where Ok(found_extrinsics) } - async fn latest_block_info(&self, use_best_block: Option) -> RpcResult { - let info = self.client.info(); - let use_best_block = use_best_block.unwrap_or(true); - if use_best_block { - return Ok(types::BlockInfo { - hash: info.best_hash.into(), - height: info.best_number.into(), - }); - } - - Ok(types::BlockInfo { - hash: info.finalized_hash.into(), - height: info.finalized_number.into(), - }) - } - async fn latest_chain_info(&self) -> RpcResult { let info = self.client.info(); return Ok(types::ChainInfo { @@ -275,140 +243,23 @@ pub mod types { use super::*; #[derive(Clone, serde::Serialize, serde::Deserialize)] - #[cfg_attr(feature = "ts", derive(ts_rs::TS))] - #[cfg_attr(feature = "ts", ts(export, export_to = "Types.ts"))] - pub struct BlockInfo { - #[cfg_attr(feature = "ts", ts(as = "String"))] - pub hash: H256, - pub height: u32, - } - - #[derive(Clone, serde::Serialize, serde::Deserialize)] - #[cfg_attr(feature = "ts", derive(ts_rs::TS))] - #[cfg_attr(feature = "ts", ts(export, export_to = "Types.ts"))] pub struct ChainInfo { - #[cfg_attr(feature = "ts", ts(as = "String"))] pub best_hash: H256, pub best_height: u32, - #[cfg_attr(feature = "ts", ts(as = "String"))] pub finalized_hash: H256, pub finalized_height: u32, - #[cfg_attr(feature = "ts", ts(as = "String"))] pub genesis_hash: H256, } - - #[cfg(feature = "ts")] - pub mod ts_types { - use super::*; - - #[allow(dead_code)] - #[cfg_attr(feature = "ts", derive(ts_rs::TS))] - #[cfg_attr(feature = "ts", ts(export, export_to = "Types.ts"))] - struct RpcRequestBlockInfo { - id: u32, - jsonrpc: String, - method: String, - params: (bool,), - } - - #[allow(dead_code)] - #[cfg_attr(feature = "ts", derive(ts_rs::TS))] - #[cfg_attr(feature = "ts", ts(export, export_to = "Types.ts"))] - struct RpcResponseBlockInfo { - jsonrpc: String, - result: Option, - error: Option, - id: u32, - } - - #[allow(dead_code)] - #[cfg_attr(feature = "ts", derive(ts_rs::TS))] - #[cfg_attr(feature = "ts", ts(export, export_to = "Types.ts"))] - struct RpcRequestChainInfo { - id: u32, - jsonrpc: String, - method: String, - params: (), - } - - #[allow(dead_code)] - #[cfg_attr(feature = "ts", derive(ts_rs::TS))] - #[cfg_attr(feature = "ts", ts(export, export_to = "Types.ts"))] - struct RpcResponseChainInfo { - jsonrpc: String, - result: Option, - error: Option, - id: u32, - } - - #[allow(dead_code)] - #[cfg_attr(feature = "ts", derive(ts_rs::TS))] - #[cfg_attr(feature = "ts", ts(export, export_to = "Types.ts"))] - struct RpcRequestBlockNumber { - id: u32, - jsonrpc: String, - method: String, - #[cfg_attr(feature = "ts", ts(as = "(String,)"))] - params: (H256,), - } - - #[allow(dead_code)] - #[cfg_attr(feature = "ts", derive(ts_rs::TS))] - #[cfg_attr(feature = "ts", ts(export, export_to = "Types.ts"))] - struct RpcResponseBlockNumber { - jsonrpc: String, - result: Option, - error: Option, - id: u32, - } - - #[allow(dead_code)] - #[cfg_attr(feature = "ts", derive(ts_rs::TS))] - #[cfg_attr(feature = "ts", ts(export, export_to = "Types.ts"))] - pub struct Error { - code: i32, - message: String, - data: Option, - } - } } -pub mod fetch_events_v1 { - pub use frame_system_rpc_runtime_api::system_events_api::fetch_events_v1::{ +pub mod fetch_events { + pub use frame_system_rpc_runtime_api::system_events_api::fetch_events::{ GroupedRuntimeEvents as RuntimeGroupedRuntimeEvents, Options, RuntimeEvent as RuntimeRuntimeEvent, }; pub type ApiResult = Vec; - #[cfg(feature = "ts")] - pub mod ts_types { - use super::super::types::ts_types::Error; - use super::*; - - #[allow(dead_code)] - #[cfg_attr(feature = "ts", derive(ts_rs::TS))] - #[cfg_attr(feature = "ts", ts(export, export_to = "FetchEvents.ts"))] - struct RpcRequest { - id: u32, - jsonrpc: String, - method: String, - params: (String, Option), - } - - #[allow(dead_code)] - #[cfg_attr(feature = "ts", derive(ts_rs::TS))] - #[cfg_attr(feature = "ts", ts(export, export_to = "FetchEvents.ts"))] - struct RpcResponse { - jsonrpc: String, - result: Option>, - error: Option, - id: u32, - } - } - #[derive(Clone, serde::Serialize, serde::Deserialize)] - #[cfg_attr(feature = "ts", derive(ts_rs::TS))] - #[cfg_attr(feature = "ts", ts(export, export_to = "FetchEvents.ts"))] pub struct GroupedRuntimeEvents { pub phase: frame_system::Phase, pub events: Vec, @@ -433,14 +284,11 @@ pub mod fetch_events_v1 { } #[derive(Clone, serde::Serialize, serde::Deserialize)] - #[cfg_attr(feature = "ts", derive(ts_rs::TS))] - #[cfg_attr(feature = "ts", ts(export, export_to = "FetchEvents.ts"))] pub struct RuntimeEvent { pub index: u32, // (Pallet Id, Event Id) pub emitted_index: (u8, u8), - pub encoded: Option, - pub decoded: Option, + pub data: Option, } impl From for RuntimeEvent { @@ -448,14 +296,13 @@ pub mod fetch_events_v1 { Self { index: value.index, emitted_index: value.emitted_index, - encoded: value.encoded.map(const_hex::encode), - decoded: value.decoded.map(const_hex::encode), + data: value.data.map(const_hex::encode), } } } } -pub mod fetch_extrinsics_v1 { +pub mod fetch_extrinsics { use super::*; use avail_core::asdr::EXTRINSIC_FORMAT_VERSION; use codec::{Decode, Input}; @@ -466,38 +313,9 @@ pub mod fetch_extrinsics_v1 { pub type ApiResult = Vec; - #[cfg(feature = "ts")] - pub mod ts_types { - use super::super::types::ts_types::Error; - use super::*; - - #[allow(dead_code)] - #[cfg_attr(feature = "ts", derive(ts_rs::TS))] - #[cfg_attr(feature = "ts", ts(export, export_to = "FetchExtrinsics.ts"))] - struct RpcRequest { - id: u32, - jsonrpc: String, - method: String, - params: (BlockId, Option), - } - - #[allow(dead_code)] - #[cfg_attr(feature = "ts", derive(ts_rs::TS))] - #[cfg_attr(feature = "ts", ts(export, export_to = "FetchExtrinsics.ts"))] - struct RpcResponse { - jsonrpc: String, - result: Option>, - error: Option, - id: u32, - } - } - #[derive(Clone, Serialize, Deserialize)] - #[cfg_attr(feature = "ts", derive(ts_rs::TS))] - #[cfg_attr(feature = "ts", ts(export, export_to = "FetchExtrinsics.ts"))] pub struct ExtrinsicInformation { pub encoded: Option, - #[cfg_attr(feature = "ts", ts(as = "String"))] pub tx_hash: H256, pub tx_index: u32, pub pallet_id: u8, @@ -506,177 +324,153 @@ pub mod fetch_extrinsics_v1 { } #[derive(Clone, Copy, Serialize, Deserialize)] - #[cfg_attr(feature = "ts", derive(ts_rs::TS))] - #[cfg_attr(feature = "ts", ts(export, export_to = "FetchExtrinsics.ts"))] pub enum BlockId { /// Identify by block header hash. - #[cfg_attr(feature = "ts", ts(as = "String"))] Hash(H256), /// Identify by block number. Number(u32), } - #[derive(Default, Clone, Serialize, Deserialize)] - #[cfg_attr(feature = "ts", derive(ts_rs::TS))] - #[cfg_attr(feature = "ts", ts(export, export_to = "FetchExtrinsics.ts"))] - pub struct Options { - pub filter: Option, - pub encode_selector: Option, - } - #[derive(Clone, Serialize, Deserialize)] - #[cfg_attr(feature = "ts", derive(ts_rs::TS))] - #[cfg_attr(feature = "ts", ts(export, export_to = "FetchExtrinsics.ts"))] - #[repr(u8)] - pub enum EncodeSelector { - None = 0, - Call = 1, - Extrinsic = 2, + pub struct Params { + pub block_id: BlockId, + pub ss58_address: Option, + pub app_id: Option, + pub nonce: Option, + #[serde(default)] + pub extrinsic: ExtrinsicFilterOptions, + #[serde(default)] + pub encode_selector: EncodeSelector, } - impl EncodeSelector { - pub fn is_call(&self) -> bool { - match self { - EncodeSelector::Call => true, - _ => false, + impl Params { + pub fn is_valid(&self) -> bool { + if self.ss58_address.as_ref().is_some_and(|x| x.len() > 100) { + return false; } - } - pub fn is_extrinsic(&self) -> bool { - match self { - EncodeSelector::Extrinsic => true, - _ => false, + let valid = match &self.extrinsic { + ExtrinsicFilterOptions::All => true, + ExtrinsicFilterOptions::TxHash(items) => items.len() < 30, + ExtrinsicFilterOptions::TxIndex(items) => items.len() < 30, + ExtrinsicFilterOptions::Pallet(items) => items.len() < 30, + ExtrinsicFilterOptions::PalletCall(items) => items.len() < 30, + }; + + if !valid { + return false; } - } - } - impl Default for EncodeSelector { - fn default() -> Self { - Self::Extrinsic + true } - } - #[derive(Default, Clone, Serialize, Deserialize)] - #[cfg_attr(feature = "ts", derive(ts_rs::TS))] - #[cfg_attr(feature = "ts", ts(export, export_to = "FetchExtrinsics.ts"))] - pub struct Filter { - pub transaction: Option, - pub signature: Option, - } + pub fn filter_in(&self, signature: &Option) -> bool { + if !self.filter_in_ss58_address(signature.as_ref().and_then(|x| x.ss58_address.clone())) + { + return false; + } - #[derive(Clone, Serialize, Deserialize)] - #[cfg_attr(feature = "ts", derive(ts_rs::TS))] - #[cfg_attr(feature = "ts", ts(export, export_to = "FetchExtrinsics.ts"))] - pub enum TransactionFilterOptions { - All, - #[cfg_attr(feature = "ts", ts(as = "Vec"))] - TxHash(Vec), - TxIndex(Vec), - Pallet(Vec), - PalletCall(Vec<(u8, u8)>), - } + if !self.filter_in_nonce(signature.as_ref().map(|x| x.nonce)) { + return false; + } - impl TransactionFilterOptions { - pub fn is_valid(&self) -> bool { - match self { - TransactionFilterOptions::All => true, - TransactionFilterOptions::TxHash(items) => items.len() < 30, - TransactionFilterOptions::TxIndex(items) => items.len() < 30, - TransactionFilterOptions::Pallet(items) => items.len() < 30, - TransactionFilterOptions::PalletCall(items) => items.len() < 30, + true + } + + pub fn filter_in_ss58_address(&self, value: Option) -> bool { + if self.ss58_address.is_none() { + return true; + } + self.ss58_address == value + } + + pub fn filter_in_nonce(&self, value: Option) -> bool { + if self.nonce.is_none() { + return true; } + self.nonce == value } pub fn is_tx_hash(&self) -> bool { - match self { - Self::TxHash(_) => true, + match &self.extrinsic { + ExtrinsicFilterOptions::TxHash(_) => true, _ => false, } } pub fn filter_in_pallet(&self, value: u8) -> bool { - let TransactionFilterOptions::Pallet(list) = self else { + let ExtrinsicFilterOptions::Pallet(list) = &self.extrinsic else { return true; }; list.contains(&value) } pub fn filter_in_pallet_call(&self, value: (u8, u8)) -> bool { - let TransactionFilterOptions::PalletCall(list) = self else { + let ExtrinsicFilterOptions::PalletCall(list) = &self.extrinsic else { return true; }; list.contains(&value) } pub fn filter_in_tx_hash(&self, value: H256) -> bool { - let TransactionFilterOptions::TxHash(list) = self else { + let ExtrinsicFilterOptions::TxHash(list) = &self.extrinsic else { return true; }; list.contains(&value) } pub fn filter_in_tx_index(&self, value: u32) -> bool { - let TransactionFilterOptions::TxIndex(list) = self else { + let ExtrinsicFilterOptions::TxIndex(list) = &self.extrinsic else { return true; }; list.contains(&value) } - } - impl Default for TransactionFilterOptions { - fn default() -> Self { - Self::All + pub fn is_call(&self) -> bool { + match self.encode_selector { + EncodeSelector::Call => true, + _ => false, + } } - } - - #[derive(Default, Clone, Serialize, Deserialize)] - #[cfg_attr(feature = "ts", derive(ts_rs::TS))] - #[cfg_attr(feature = "ts", ts(export, export_to = "FetchExtrinsics.ts"))] - pub struct SignatureFilterOptions { - pub ss58_address: Option, - pub app_id: Option, - pub nonce: Option, - } - impl SignatureFilterOptions { - pub fn is_valid(&self) -> bool { - if self.ss58_address.as_ref().is_some_and(|x| x.len() > 100) { - return false; + pub fn is_extrinsic(&self) -> bool { + match self.encode_selector { + EncodeSelector::Extrinsic => true, + _ => false, } - - true } - pub fn filter_in(&self, signature: &Option) -> bool { - if !self.filter_in_ss58_address(signature.as_ref().and_then(|x| x.ss58_address.clone())) - { - return false; - } + } - if !self.filter_in_nonce(signature.as_ref().map(|x| x.nonce)) { - return false; - } + #[derive(Clone, Copy, Serialize, Deserialize)] + #[repr(u8)] + pub enum EncodeSelector { + None = 0, + Call = 1, + Extrinsic = 2, + } - true + impl Default for EncodeSelector { + fn default() -> Self { + Self::Extrinsic } + } - pub fn filter_in_ss58_address(&self, value: Option) -> bool { - if self.ss58_address.is_none() { - return true; - } - self.ss58_address == value - } + #[derive(Clone, Serialize, Deserialize)] + pub enum ExtrinsicFilterOptions { + All, + TxHash(Vec), + TxIndex(Vec), + Pallet(Vec), + PalletCall(Vec<(u8, u8)>), + } - pub fn filter_in_nonce(&self, value: Option) -> bool { - if self.nonce.is_none() { - return true; - } - self.nonce == value + impl Default for ExtrinsicFilterOptions { + fn default() -> Self { + Self::All } } #[derive(Clone, Serialize, Deserialize)] - #[cfg_attr(feature = "ts", derive(ts_rs::TS))] - #[cfg_attr(feature = "ts", ts(export, export_to = "FetchExtrinsics.ts"))] pub struct TransactionSignature { pub ss58_address: Option, pub nonce: u32, @@ -867,7 +661,7 @@ pub mod fetch_extrinsics_v1 { #[cfg(test)] mod test { - use super::fetch_extrinsics_v1::*; + use super::fetch_extrinsics::*; use sp_core::H256; #[test] diff --git a/runtime/src/apis.rs b/runtime/src/apis.rs index bda9d49fc..ceeb07533 100644 --- a/runtime/src/apis.rs +++ b/runtime/src/apis.rs @@ -281,9 +281,9 @@ impl_runtime_apis! { } impl frame_system_rpc_runtime_api::SystemEventsApi for Runtime { - fn fetch_events_v1(options: frame_system_rpc_runtime_api::system_events_api::fetch_events_v1::Options) -> frame_system_rpc_runtime_api::system_events_api::fetch_events_v1::ApiResult { + fn fetch_events(options: frame_system_rpc_runtime_api::system_events_api::fetch_events::Options) -> frame_system_rpc_runtime_api::system_events_api::fetch_events::ApiResult { use sp_std::vec; - use frame_system_rpc_runtime_api::system_events_api::fetch_events_v1::{RuntimeEvent, GroupedRuntimeEvents, ERROR_INVALID_INPUTS}; + use frame_system_rpc_runtime_api::system_events_api::fetch_events::{RuntimeEvent, GroupedRuntimeEvents, ERROR_INVALID_INPUTS}; use codec::Encode; let filter = options.filter.unwrap_or_default(); @@ -291,8 +291,7 @@ impl_runtime_apis! { return Err(ERROR_INVALID_INPUTS); } - let enable_encoding = options.enable_encoding.unwrap_or(false); - let enable_decoding = options.enable_decoding.unwrap_or(false); + let fetch_data = options.fetch_data.unwrap_or(false); let mut result: Vec = Vec::new(); let all_events = System::read_events_no_consensus(); @@ -301,16 +300,15 @@ impl_runtime_apis! { continue } - let encoded = event.event.encode(); - if encoded.len() <2 { + let data = event.event.encode(); + if data.len() <2 { continue } - let emitted_index: (u8, u8) = (encoded[0], encoded[1]); - let encoded = enable_encoding.then_some(encoded); - let decoded = enable_decoding.then(|| decode_runtime_event_v1(&event.event)).flatten(); + let emitted_index: (u8, u8) = (data[0], data[1]); + let data = fetch_data.then_some(data); - let ev = RuntimeEvent::new(position as u32, emitted_index, encoded, decoded); + let ev = RuntimeEvent::new(position as u32, emitted_index, data); if let Some(entry) = result.iter_mut().find(|x| x.phase == event.phase) { entry.events.push(ev); } else { @@ -757,70 +755,3 @@ impl_runtime_apis! { } } } - -fn decode_runtime_event_v1(event: &super::RuntimeEvent) -> Option> { - use super::*; - use codec::Encode; - - match event { - RuntimeEvent::Sudo(e) => match e { - pallet_sudo::Event::::Sudid { sudo_result } => { - let mut event_data = Vec::::new(); - sudo_result.is_ok().encode_to(&mut event_data); - - return Some(event_data); - }, - pallet_sudo::Event::::SudoAsDone { sudo_result } => { - let mut event_data = Vec::::new(); - sudo_result.is_ok().encode_to(&mut event_data); - - return Some(event_data); - }, - _ => (), - }, - RuntimeEvent::Multisig(e) => { - if let pallet_multisig::Event::::MultisigExecuted { - multisig, - call_hash, - result: x, - .. - } = e - { - let mut event_data = Vec::::new(); - multisig.encode_to(&mut event_data); - call_hash.encode_to(&mut event_data); - x.is_ok().encode_to(&mut event_data); - - return Some(event_data); - } - }, - RuntimeEvent::Proxy(e) => { - if let pallet_proxy::Event::::ProxyExecuted { result, .. } = e { - let mut event_data = Vec::::new(); - result.is_ok().encode_to(&mut event_data); - - return Some(event_data); - } - }, - RuntimeEvent::Scheduler(e) => { - if let pallet_scheduler::Event::::Dispatched { result, .. } = e { - let mut event_data = Vec::::new(); - result.is_ok().encode_to(&mut event_data); - - return Some(event_data); - } - }, - RuntimeEvent::DataAvailability(e) => { - if let da_control::Event::::DataSubmitted { who, data_hash } = e { - let mut event_data = Vec::::new(); - who.encode_to(&mut event_data); - data_hash.encode_to(&mut event_data); - - return Some(event_data); - } - }, - _ => (), - }; - - None -}