Skip to content

Commit

Permalink
Generalized RosRpc trait; Added chain ros-rpc module
Browse files Browse the repository at this point in the history
  • Loading branch information
akru committed Jul 7, 2019
1 parent 6e51a20 commit 4ed6548
Show file tree
Hide file tree
Showing 22 changed files with 346 additions and 137 deletions.
142 changes: 71 additions & 71 deletions Cargo.lock

Large diffs are not rendered by default.

15 changes: 12 additions & 3 deletions node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,10 @@ construct_service_factory! {

/*
#[cfg(feature = "ros")]
service.spawn_task(Box::new(ros_integration::start_ros_api(
service.spawn_task(Box::new(ros_robonomics::start_ros_api(
service.network(),
service.client(),
service.transaction_pool(),
service.keystore(),
service.on_exit(),
)));
*/
Expand All @@ -101,7 +100,17 @@ construct_service_factory! {
service.transaction_pool(),
);

service.spawn_task(Box::new(author.start_rpc(service.on_exit())));
let chain = ros_rpc::chain::Chain::new(
service.client(),
);

let _services = vec![
ros_rpc::traits::RosRpc::start(Arc::new(author)).unwrap(),
ros_rpc::traits::RosRpc::start(Arc::new(chain)).unwrap(),
];

let on_exit = service.on_exit().then(move |_| {_services; Ok(())});
service.spawn_task(Box::new(on_exit));
}

Ok(service)
Expand Down
10 changes: 6 additions & 4 deletions substrate-ros/msgs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ rosmsg_include!(

// substrate rpc messages
substrate_ros_msgs / ExHash,
substrate_ros_msgs / BlockHash,
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,
substrate_ros_msgs / BlockNumberByHash,
substrate_ros_msgs / GetBlock,
substrate_ros_msgs / GetBlockHash,
substrate_ros_msgs / GetBlockHeader,
substrate_ros_msgs / GetBestHead,
substrate_ros_msgs / GetFinalizedHead,
);
12 changes: 7 additions & 5 deletions substrate-ros/msgs/substrate_ros_msgs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,21 @@ add_message_files(

RawExtrinsic.msg
ExHash.msg
BlockHash.msg
)

## Generate services in the 'srv' folder
add_service_files(
FILES
BlockHash.srv
BlockNumber.srv
BlockHashByNumber.srv
BlockNumberByHash.srv

SubmitExtrinsic.srv
PendingExtrinsics.srv
RemoveExtrinsic.srv

GetBlock.srv
GetBlockHash.srv
GetBlockHeader.srv
GetBestHead.srv
GetFinalizedHead.srv
)

## Generate actions in the 'action' folder
Expand Down
1 change: 1 addition & 0 deletions substrate-ros/msgs/substrate_ros_msgs/msg/BlockHash.msg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uint8[32] data
2 changes: 0 additions & 2 deletions substrate-ros/msgs/substrate_ros_msgs/srv/BlockHash.srv

This file was deleted.

This file was deleted.

2 changes: 0 additions & 2 deletions substrate-ros/msgs/substrate_ros_msgs/srv/BlockNumber.srv

This file was deleted.

This file was deleted.

2 changes: 2 additions & 0 deletions substrate-ros/msgs/substrate_ros_msgs/srv/GetBestHead.srv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
BlockHash hash
3 changes: 3 additions & 0 deletions substrate-ros/msgs/substrate_ros_msgs/srv/GetBlock.srv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BlockHash hash
---
string block_json
3 changes: 3 additions & 0 deletions substrate-ros/msgs/substrate_ros_msgs/srv/GetBlockHash.srv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
uint32 number
---
BlockHash hash
3 changes: 3 additions & 0 deletions substrate-ros/msgs/substrate_ros_msgs/srv/GetBlockHeader.srv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BlockHash hash
---
string header_json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
BlockHash hash
1 change: 1 addition & 0 deletions substrate-ros/robonomics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ futures = "0.1"
ipfs-api = "0.5"
lazy_static = "1.2"
robonomics-runtime = { path = "../../node/runtime" }
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" }
network = { package = "substrate-network", git = "https://github.com/paritytech/substrate" }
Expand Down
5 changes: 1 addition & 4 deletions substrate-ros/robonomics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use log::debug;
use std::sync::Arc;
use futures::{Future, Stream, sync::mpsc};
use network::SyncProvider;
use keystore::Store as Keystore;
use client::{
Client, CallExecutor, BlockchainEvents,
Expand All @@ -30,7 +29,7 @@ use client::{
use runtime_primitives::{
codec::{Decode, Encode, Compact},
generic::{BlockId, Era},
traits::{As, Block, Header, BlockNumberToHash}
traits::{Block, Header, BlockNumberToHash}
};
use primitives::{
Blake2Hasher, H256, twox_128,
Expand Down Expand Up @@ -200,13 +199,11 @@ fn event_stream<B, C>(

/// ROS API main routine.
pub fn start_ros_api<N, B, E, P, RA>(
network: Arc<N>,
client: Arc<Client<B, E, P::Block, RA>>,
pool: Arc<Pool<P>>,
keystore: &Keystore,
on_exit: impl Future<Item=(),Error=()> + 'static,
) -> impl Future<Item=(),Error=()> + 'static where
N: SyncProvider<P::Block> + 'static,
B: Backend<P::Block, Blake2Hasher> + 'static,
E: CallExecutor<P::Block, Blake2Hasher> + Send + Sync + 'static,
P: ChainApi + 'static,
Expand Down
2 changes: 1 addition & 1 deletion substrate-ros/robonomics/src/rosbag_player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::string::String;
use std::sync::Arc;
use log::debug;

use crate::msg::std_msgs;
use msgs::std_msgs;

#[derive(Debug, PartialEq, Clone)]
pub enum WorkerMsg {
Expand Down
1 change: 0 additions & 1 deletion substrate-ros/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ edition = "2018"

[dependencies]
log = "0.4"
futures = "0.1"
rosrust = "0.8"
serde_json = "1.0"
parity-codec = "4.1"
Expand Down
86 changes: 48 additions & 38 deletions substrate-ros/rpc/src/author.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,14 @@
///////////////////////////////////////////////////////////////////////////////

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,
},
};
Expand All @@ -39,6 +34,8 @@ use msgs::substrate_ros_msgs::{
PendingExtrinsics, PendingExtrinsicsRes,
RemoveExtrinsic, RemoveExtrinsicRes,
};
use rosrust::api::error::Error;
use crate::traits::RosRpc;

const SUBMIT_SRV_NAME: &str = "/author/submit_extrinsic";
const REMOVE_SRV_NAME: &str = "/author/remove_extrinsic";
Expand Down Expand Up @@ -71,13 +68,44 @@ impl<B, E, P, RA> Author<B, E, P, RA> where
}
}

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

let api = Arc::new(self);
fn remove_extrinsic(&self, hashes: Vec<ExHashT<P>>) -> Vec<ExHashT<P>> {
self.pool.remove_invalid(&hashes)
.into_iter()
.map(|tx| tx.hash.clone())
.collect()
}
}

impl<B, E, P, RA> RosRpc for 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
{
fn start(api: Arc<Self>) -> Result<Vec<rosrust::Service>, Error> {
// TODO: drop this hack
// <
rosrust::try_init_with_options("robonomics", false).unwrap();
// !>

let mut services = vec![];

let api1 = api.clone();
let _submit =
services.push(
rosrust::service::<SubmitExtrinsic, _>(SUBMIT_SRV_NAME, move |req| {
let mut res = SubmitExtrinsicRes::default();
match api1.submit_extrinsic(req.extrinsic.data.into()) {
Expand All @@ -88,10 +116,11 @@ impl<B, E, P, RA> Author<B, E, P, RA> where
Err(err) => res.error = err.to_string()
}
Ok(res)
});
})?
);

let api2 = api.clone();
let _pending =
services.push(
rosrust::service::<PendingExtrinsics, _>(PENDING_SRV_NAME, move |_req| {
let mut res = PendingExtrinsicsRes::default();
for xt in api2.pending_extrinsics() {
Expand All @@ -100,42 +129,23 @@ impl<B, E, P, RA> Author<B, E, P, RA> where
res.extrinsics.push(xt_msg);
}
Ok(res)
});
})?
);

let _remove =
let api3 = api.clone();
services.push(
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) {
for xt in api3.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(())
})
Ok(services)
}

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()
}
}
Loading

0 comments on commit 4ed6548

Please sign in to comment.