Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hex-encoded IdProvider #203

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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