Skip to content

Commit

Permalink
Add hex-encoded IdProvider (#203)
Browse files Browse the repository at this point in the history
* Add hex-encoded IdProvider

* Update tests

* Merge remote-tracking branch 'upstream/master' into tgmichel-pubsub-idprovider
  • Loading branch information
tgmichel committed Nov 24, 2020
1 parent 384b8f6 commit 4497bd3
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
38 changes: 34 additions & 4 deletions client/rpc/src/eth_pubsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

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
Expand All @@ -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,
Expand All @@ -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<B: BlockT, P, C, BE, H: ExHashT> {
_pool: Arc<P>,
client: Arc<C>,
network: Arc<NetworkService<B, H>>,
subscriptions: SubscriptionManager,
subscriptions: SubscriptionManager<HexEncodedIdProvider>,
_marker: PhantomData<(B, BE)>,
}

Expand All @@ -65,7 +95,7 @@ impl<B: BlockT, P, C, BE, H: ExHashT> EthPubSubApi<B, P, C, BE, H> {
_pool: Arc<P>,
client: Arc<C>,
network: Arc<NetworkService<B, H>>,
subscriptions: SubscriptionManager,
subscriptions: SubscriptionManager<HexEncodedIdProvider>,
) -> Self {
Self { _pool, client, network, subscriptions, _marker: PhantomData }
}
Expand Down
2 changes: 1 addition & 1 deletion client/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
7 changes: 5 additions & 2 deletions template/node/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub fn create_full<C, P, BE>(
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();
Expand Down Expand Up @@ -124,7 +124,10 @@ pub fn create_full<C, P, BE>(
pool.clone(),
client.clone(),
network.clone(),
SubscriptionManager::new(Arc::new(subscription_task_executor)),
SubscriptionManager::<HexEncodedIdProvider>::with_id_provider(
HexEncodedIdProvider::default(),
Arc::new(subscription_task_executor)
),
))
);

Expand Down
2 changes: 1 addition & 1 deletion ts-tests/tests/test-subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 4497bd3

Please sign in to comment.