Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 0ad8b40

Browse files
authored
Fix transaction submission for hex (#259)
* Fix transaction submission for hex * Fix grumbles.
1 parent 7b1dca2 commit 0ad8b40

File tree

7 files changed

+54
-5
lines changed

7 files changed

+54
-5
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

substrate/primitives/src/lib.rs

+16
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ macro_rules! map {
6161
)
6262
}
6363

64+
use rstd::prelude::*;
65+
use rstd::ops::Deref;
6466

6567
#[cfg(feature = "std")]
6668
pub mod bytes;
@@ -87,3 +89,17 @@ pub type AuthorityId = [u8; 32];
8789

8890
/// A 512-bit value interpreted as a signature.
8991
pub type Signature = hash::H512;
92+
93+
/// Hex-serialised shim for `Vec<u8>`.
94+
#[derive(PartialEq, Eq, Clone)]
95+
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug, Hash, PartialOrd, Ord))]
96+
pub struct Bytes(#[cfg_attr(feature = "std", serde(with="bytes"))] pub Vec<u8>);
97+
98+
impl From<Vec<u8>> for Bytes {
99+
fn from(s: Vec<u8>) -> Self { Bytes(s) }
100+
}
101+
102+
impl Deref for Bytes {
103+
type Target = [u8];
104+
fn deref(&self) -> &[u8] { &self.0[..] }
105+
}

substrate/rpc/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ jsonrpc-macros = { git="https://github.com/paritytech/jsonrpc.git" }
1010
jsonrpc-pubsub = { git="https://github.com/paritytech/jsonrpc.git" }
1111
log = "0.3"
1212
parking_lot = "0.4"
13+
substrate-codec = { path = "../codec" }
1314
substrate-client = { path = "../client" }
1415
substrate-executor = { path = "../executor" }
1516
substrate-extrinsic-pool = { path = "../extrinsic-pool" }

substrate/rpc/src/author/error.rs

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ error_chain! {
2424
Pool(txpool::Error, txpool::ErrorKind) #[doc = "Pool error"];
2525
}
2626
errors {
27+
/// Incorrect transaction format.
28+
BadFormat {
29+
description("bad format"),
30+
display("Invalid transaction format"),
31+
}
2732
/// Not implemented yet
2833
Unimplemented {
2934
description("not yet implemented"),

substrate/rpc/src/author/mod.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ use std::sync::Arc;
2020

2121
use client::{self, Client};
2222
use extrinsic_pool::api::{Error, ExtrinsicPool};
23+
use codec::Slicable;
2324

25+
use primitives::Bytes;
2426
use runtime_primitives::{generic, traits::Block as BlockT};
2527
use state_machine;
2628

@@ -35,8 +37,11 @@ build_rpc_trait! {
3537
/// Substrate authoring RPC API
3638
pub trait AuthorApi<Hash, Extrinsic> {
3739
/// Submit extrinsic for inclusion in block.
40+
#[rpc(name = "author_submitRichExtrinsic")]
41+
fn submit_rich_extrinsic(&self, Extrinsic) -> Result<Hash>;
42+
/// Submit hex-encoded extrinsic for inclusion in block.
3843
#[rpc(name = "author_submitExtrinsic")]
39-
fn submit_extrinsic(&self, Extrinsic) -> Result<Hash>;
44+
fn submit_extrinsic(&self, Bytes) -> Result<Hash>;
4045
}
4146
}
4247

@@ -55,16 +60,20 @@ impl<B, E, Block: BlockT, P> Author<B, E, Block, P> {
5560
}
5661
}
5762

58-
5963
impl<B, E, Block, P, Ex, Hash> AuthorApi<Hash, Ex> for Author<B, E, Block, P> where
6064
B: client::backend::Backend<Block> + Send + Sync + 'static,
6165
E: client::CallExecutor<Block> + Send + Sync + 'static,
6266
Block: BlockT + 'static,
6367
client::error::Error: From<<<B as client::backend::Backend<Block>>::State as state_machine::backend::Backend>::Error>,
6468
P: ExtrinsicPool<Ex, generic::BlockId<Block>, Hash>,
6569
P::Error: 'static,
70+
Ex: Slicable,
6671
{
67-
fn submit_extrinsic(&self, xt: Ex) -> Result<Hash> {
72+
fn submit_extrinsic(&self, xt: Bytes) -> Result<Hash> {
73+
self.submit_rich_extrinsic(Ex::decode(&mut &xt[..]).ok_or(error::Error::from(error::ErrorKind::BadFormat))?)
74+
}
75+
76+
fn submit_rich_extrinsic(&self, xt: Ex) -> Result<Hash> {
6877
let best_block_hash = self.client.info().unwrap().chain.best_hash;
6978
self.pool
7079
.submit(generic::BlockId::hash(best_block_hash), vec![xt])

substrate/rpc/src/author/tests.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,26 @@ fn submit_transaction_should_not_cause_error() {
6565
};
6666

6767
assert_matches!(
68-
AuthorApi::submit_extrinsic(&p, 5),
68+
AuthorApi::submit_extrinsic(&p, u64::encode(&5).into()),
6969
Ok(1)
7070
);
7171
assert!(
72-
AuthorApi::submit_extrinsic(&p, 5).is_err()
72+
AuthorApi::submit_extrinsic(&p, u64::encode(&5).into()).is_err()
73+
);
74+
}
75+
76+
#[test]
77+
fn submit_rich_transaction_should_not_cause_error() {
78+
let p = Author {
79+
client: Arc::new(test_client::new()),
80+
pool: Arc::new(DummyTxPool::default()),
81+
};
82+
83+
assert_matches!(
84+
AuthorApi::submit_rich_extrinsic(&p, 5),
85+
Ok(1)
86+
);
87+
assert!(
88+
AuthorApi::submit_rich_extrinsic(&p, 5).is_err()
7389
);
7490
}

substrate/rpc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
extern crate jsonrpc_core as rpc;
2222
extern crate jsonrpc_pubsub;
2323
extern crate parking_lot;
24+
extern crate substrate_codec as codec;
2425
extern crate substrate_client as client;
2526
extern crate substrate_extrinsic_pool as extrinsic_pool;
2627
extern crate substrate_primitives as primitives;

0 commit comments

Comments
 (0)