diff --git a/client/rpc/src/eth_pubsub.rs b/client/rpc/src/eth_pubsub.rs index db181c8b3..d27ba8742 100644 --- a/client/rpc/src/eth_pubsub.rs +++ b/client/rpc/src/eth_pubsub.rs @@ -16,8 +16,11 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use std::{marker::PhantomData, sync::Arc}; +use std::{marker::PhantomData, sync::Arc, iter}; use std::collections::BTreeMap; +use rand::distributions::Alphanumeric; +use rand::{thread_rng, Rng}; +use rustc_hex::ToHex; use sp_runtime::traits::{ Block as BlockT, BlakeTwo256, UniqueSaturatedInto @@ -34,7 +37,10 @@ use sc_client_api::{ use sc_rpc::Metadata; use log::warn; -use jsonrpc_pubsub::{typed::Subscriber, SubscriptionId, manager::SubscriptionManager}; +use jsonrpc_pubsub::{ + typed::Subscriber, SubscriptionId, + manager::{SubscriptionManager, IdProvider} +}; use fc_rpc_core::EthPubSubApi::{self as EthPubSubApiT}; use fc_rpc_core::types::{ Rich, Header, Bytes, Log, FilteredParams, @@ -52,11 +58,35 @@ use fp_rpc::{EthereumRuntimeRPCApi, TransactionStatus}; use sc_network::{NetworkService, ExHashT}; +#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] +pub struct HexEncodedIdProvider { + len: usize, +} + +impl Default for HexEncodedIdProvider { + fn default() -> Self { + Self { len: 16 } + } +} + +impl IdProvider for HexEncodedIdProvider { + type Id = String; + fn next_id(&self) -> Self::Id { + let mut rng = thread_rng(); + let id: String = iter::repeat(()) + .map(|()| rng.sample(Alphanumeric)) + .take(self.len) + .collect(); + let out: String = id.as_bytes().to_hex(); + format!("0x{}", out) + } +} + pub struct EthPubSubApi { _pool: Arc

, client: Arc, network: Arc>, - subscriptions: SubscriptionManager, + subscriptions: SubscriptionManager, _marker: PhantomData<(B, BE)>, } @@ -65,7 +95,7 @@ impl EthPubSubApi { _pool: Arc

, client: Arc, network: Arc>, - subscriptions: SubscriptionManager, + subscriptions: SubscriptionManager, ) -> Self { Self { _pool, client, network, subscriptions, _marker: PhantomData } } diff --git a/client/rpc/src/lib.rs b/client/rpc/src/lib.rs index f7e07eaed..c575284f2 100644 --- a/client/rpc/src/lib.rs +++ b/client/rpc/src/lib.rs @@ -20,7 +20,7 @@ mod eth; mod eth_pubsub; pub use eth::{EthApi, EthApiServer, NetApi, NetApiServer, Web3Api, Web3ApiServer}; -pub use eth_pubsub::{EthPubSubApi, EthPubSubApiServer}; +pub use eth_pubsub::{EthPubSubApi, EthPubSubApiServer, HexEncodedIdProvider}; use ethereum_types::{H160, H256}; use jsonrpc_core::{ErrorCode, Error, Value}; diff --git a/template/node/src/rpc.rs b/template/node/src/rpc.rs index 6e97a2745..b194bbb45 100644 --- a/template/node/src/rpc.rs +++ b/template/node/src/rpc.rs @@ -70,7 +70,7 @@ pub fn create_full( use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi}; use fc_rpc::{ EthApi, EthApiServer, NetApi, NetApiServer, EthPubSubApi, EthPubSubApiServer, - Web3Api, Web3ApiServer, EthDevSigner, EthSigner, + Web3Api, Web3ApiServer, EthDevSigner, EthSigner, HexEncodedIdProvider, }; let mut io = jsonrpc_core::IoHandler::default(); @@ -124,7 +124,10 @@ pub fn create_full( pool.clone(), client.clone(), network.clone(), - SubscriptionManager::new(Arc::new(subscription_task_executor)), + SubscriptionManager::::with_id_provider( + HexEncodedIdProvider::default(), + Arc::new(subscription_task_executor) + ), )) ); diff --git a/ts-tests/tests/test-subscription.ts b/ts-tests/tests/test-subscription.ts index fe860b996..ff3d2b486 100644 --- a/ts-tests/tests/test-subscription.ts +++ b/ts-tests/tests/test-subscription.ts @@ -52,7 +52,7 @@ describeWithFrontier("Frontier RPC (Subscription)", `simple-specs.json`, (contex subscription.unsubscribe(); expect(connected).to.equal(true); - expect(subscriptionId).to.have.lengthOf(16); + expect(subscriptionId).to.have.lengthOf(34); }); step("should get newHeads stream", async function (done) {