Skip to content

Commit

Permalink
Added author module to ros-rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
akru committed Jul 6, 2019
1 parent b381ab5 commit 6e51a20
Show file tree
Hide file tree
Showing 16 changed files with 182 additions and 436 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,14 @@ construct_service_factory! {
*/

#[cfg(feature = "ros")]
service.spawn_task(Box::new(ros_rpc::start_ros_rpc(
service.client(),
service.transaction_pool(),
service.on_exit(),
)));
{
let author = ros_rpc::author::Author::new(
service.client(),
service.transaction_pool(),
);

service.spawn_task(Box::new(author.start_rpc(service.on_exit())));
}

Ok(service)
}
Expand Down
2 changes: 1 addition & 1 deletion shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ in
stdenv.mkDerivation {
name = "substrate-nix-shell";
buildInputs = [
rust pkgconfig openssl clang msgs
rust wasm-gc pkgconfig openssl clang msgs
];
LIBCLANG_PATH = "${llvmPackages.libclang}/lib";
}
7 changes: 7 additions & 0 deletions substrate-ros/msgs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ rosmsg_include!(
substrate_ros_msgs / Liability,

// substrate rpc messages
substrate_ros_msgs / ExHash,
substrate_ros_msgs / RawExtrinsic,

substrate_ros_msgs / PendingExtrinsics,
substrate_ros_msgs / RemoveExtrinsic,
substrate_ros_msgs / SubmitExtrinsic,

substrate_ros_msgs / BlockHash,
substrate_ros_msgs / BlockNumber,
substrate_ros_msgs / BlockHashByNumber,
Expand Down
7 changes: 7 additions & 0 deletions substrate-ros/msgs/substrate_ros_msgs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ add_message_files(
Offer.msg
Finalize.msg
Liability.msg

RawExtrinsic.msg
ExHash.msg
)

## Generate services in the 'srv' folder
Expand All @@ -62,6 +65,10 @@ add_service_files(
BlockNumber.srv
BlockHashByNumber.srv
BlockNumberByHash.srv

SubmitExtrinsic.srv
PendingExtrinsics.srv
RemoveExtrinsic.srv
)

## Generate actions in the 'action' folder
Expand Down
1 change: 1 addition & 0 deletions substrate-ros/msgs/substrate_ros_msgs/msg/ExHash.msg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uint8[32] data
1 change: 1 addition & 0 deletions substrate-ros/msgs/substrate_ros_msgs/msg/RawExtrinsic.msg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uint8[] data
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
RawExtrinsic[] extrinsics
3 changes: 3 additions & 0 deletions substrate-ros/msgs/substrate_ros_msgs/srv/RemoveExtrinsic.srv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ExHash[] extrinsics
---
ExHash[] extrinsics
4 changes: 4 additions & 0 deletions substrate-ros/msgs/substrate_ros_msgs/srv/SubmitExtrinsic.srv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
RawExtrinsic extrinsic
---
ExHash hash
string error
3 changes: 3 additions & 0 deletions substrate-ros/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ authors = ["Airalab <[email protected]>"]
edition = "2018"

[dependencies]
log = "0.4"
futures = "0.1"
rosrust = "0.8"
serde_json = "1.0"
parity-codec = "4.1"
msgs = { package = "substrate-ros-msgs", path = "../msgs" }
client = { package = "substrate-client", git = "https://github.com/paritytech/substrate" }
service = { package = "substrate-service", git = "https://github.com/paritytech/substrate" }
Expand Down
141 changes: 141 additions & 0 deletions substrate-ros/rpc/src/author.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright 2018-2019 Airalab <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///////////////////////////////////////////////////////////////////////////////

use std::sync::Arc;

use serde_json;
use futures::Future;
use client::{self, Client};
use parity_codec::{Encode, Decode};
use primitives::{Bytes, Blake2Hasher, H256};
use runtime_primitives::{generic, traits};
use transaction_pool::{
txpool::{
ChainApi as PoolChainApi,
BlockHash,
ExHash as ExHashT,
IntoPoolError,
Pool,
},
};
use msgs::substrate_ros_msgs::{
ExHash, RawExtrinsic,
SubmitExtrinsic, SubmitExtrinsicRes,
PendingExtrinsics, PendingExtrinsicsRes,
RemoveExtrinsic, RemoveExtrinsicRes,
};

const SUBMIT_SRV_NAME: &str = "/author/submit_extrinsic";
const REMOVE_SRV_NAME: &str = "/author/remove_extrinsic";
const PENDING_SRV_NAME: &str = "/author/pending_extrinsics";

/// Authoring API
pub struct Author<B, E, P, RA> where P: PoolChainApi + Sync + Send + 'static {
/// Substrate client
client: Arc<Client<B, E, <P as PoolChainApi>::Block, RA>>,
/// Transactions pool
pool: Arc<Pool<P>>,
}

impl<B, E, P, RA> Author<B, E, P, RA> where
B: client::backend::Backend<<P as PoolChainApi>::Block, Blake2Hasher> + Send + Sync + 'static,
E: client::CallExecutor<<P as PoolChainApi>::Block, Blake2Hasher> + Send + Sync + 'static,
P: PoolChainApi<Hash=H256> + Sync + Send + 'static,
P::Block: traits::Block<Hash=H256>,
P::Error: 'static,
RA: Send + Sync + 'static
{
/// Create new instance of Authoring API.
pub fn new(
client: Arc<Client<B, E, <P as PoolChainApi>::Block, RA>>,
pool: Arc<Pool<P>>,
) -> Self {
Author {
client,
pool,
}
}

pub fn start_rpc(self, on_exit: impl Future<Item=(), Error=()>) -> impl Future<Item=(), Error=()> {
rosrust::try_init_with_options("robonomics", false);

let api = Arc::new(self);

let api1 = api.clone();
let _submit =
rosrust::service::<SubmitExtrinsic, _>(SUBMIT_SRV_NAME, move |req| {
let mut res = SubmitExtrinsicRes::default();
match api1.submit_extrinsic(req.extrinsic.data.into()) {
Ok(hash) => {
res.hash = ExHash::default();
res.hash.data = hash.into();
},
Err(err) => res.error = err.to_string()
}
Ok(res)
});

let api2 = api.clone();
let _pending =
rosrust::service::<PendingExtrinsics, _>(PENDING_SRV_NAME, move |_req| {
let mut res = PendingExtrinsicsRes::default();
for xt in api2.pending_extrinsics() {
let mut xt_msg = RawExtrinsic::default();
for b in xt.iter() { xt_msg.data.push(*b); }
res.extrinsics.push(xt_msg);
}
Ok(res)
});

let _remove =
rosrust::service::<RemoveExtrinsic, _>(REMOVE_SRV_NAME, move |req| {
let mut res = RemoveExtrinsicRes::default();
let hashes = req.extrinsics.iter().map(|h| h.data.into()).collect();
for xt in api.remove_extrinsic(hashes) {
let mut hash_msg = ExHash::default();
hash_msg.data = xt.into();
res.extrinsics.push(hash_msg);
}
Ok(res)
});

on_exit.then(move |_| {
_submit; _pending; _remove;
Ok(())
})
}

fn submit_extrinsic(&self, ext: Bytes) -> Result<ExHashT<P>, &str> {
let xt = Decode::decode(&mut &ext[..]).ok_or("Bad extrinsic format")?;
let best_block_hash = self.client.info().chain.best_hash;
self.pool
.submit_one(&generic::BlockId::hash(best_block_hash), xt)
.map_err(|_| "Extrinsic pool error")
}

fn pending_extrinsics(&self) -> Vec<Bytes> {
self.pool.ready().map(|tx| tx.data.encode().into()).collect()
}

fn remove_extrinsic(&self, hashes: Vec<ExHashT<P>>) -> Vec<ExHashT<P>> {
self.pool.remove_invalid(&hashes)
.into_iter()
.map(|tx| tx.hash.clone())
.collect()
}
}
Empty file removed substrate-ros/rpc/src/chain.rs
Empty file.
Loading

0 comments on commit 6e51a20

Please sign in to comment.