forked from paritytech/substrate
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support rpc module ext and format code (paritytech#24)
* fix genesis config * update runtime privcall * add cargo.lock in runtime/wasm * add rpc module and format code * fix spell error
- Loading branch information
Showing
10 changed files
with
396 additions
and
111 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use client; | ||
use jsonrpc_core as rpccore; | ||
|
||
pub fn unimplemented() -> rpccore::Error { | ||
rpccore::Error { | ||
code: rpccore::ErrorCode::ServerError(1), | ||
message: "Not implemented yet".into(), | ||
data: None, | ||
} | ||
} | ||
|
||
pub fn internal<E: ::std::fmt::Debug>(e: E) -> rpccore::Error { | ||
warn!("Unknown error: {:?}", e); | ||
rpccore::Error { | ||
code: rpccore::ErrorCode::InternalError, | ||
message: "Unknown error occured".into(), | ||
data: Some(format!("{:?}", e).into()), | ||
} | ||
} | ||
|
||
error_chain! { | ||
links { | ||
Client(client::error::Error, client::error::ErrorKind) #[doc = "Client error"]; | ||
} | ||
errors { | ||
/// Not implemented yet | ||
Unimplemented { | ||
description("not yet implemented"), | ||
display("Method Not Implemented"), | ||
} | ||
} | ||
} | ||
|
||
impl From<Error> for rpccore::Error { | ||
fn from(e: Error) -> Self { | ||
match e { | ||
Error(ErrorKind::Unimplemented, _) => unimplemented(), | ||
e => internal(e), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use jsonrpc_macros::Trailing; | ||
use primitives::{KeccakHasher, RlpCodec}; | ||
use runtime_primitives::generic::{BlockId, SignedBlock}; | ||
use runtime_primitives::traits::{Block as BlockT, Header, NumberFor}; | ||
|
||
use client::{self, Client}; | ||
use std::sync::Arc; | ||
use tokio::runtime::TaskExecutor; | ||
|
||
mod error; | ||
use self::error::Result; | ||
|
||
build_rpc_trait! { | ||
pub trait ChainApiExt<Hash, Header, Number, Extrinsic> { | ||
|
||
#[rpc(name = "chainext_getBlockByNumber")] | ||
fn block_info(&self, Trailing<Number>) -> Result<Option<SignedBlock<Header, Extrinsic, Hash>>>; | ||
} | ||
} | ||
|
||
pub struct ChainExt<B, E, Block: BlockT> { | ||
client: Arc<Client<B, E, Block>>, | ||
} | ||
|
||
impl<B, E, Block: BlockT> ChainExt<B, E, Block> { | ||
pub fn new(client: Arc<Client<B, E, Block>>, _executor: TaskExecutor) -> Self { | ||
Self { client } | ||
} | ||
} | ||
|
||
impl<B, E, Block> ChainApiExt<Block::Hash, Block::Header, NumberFor<Block>, Block::Extrinsic> | ||
for ChainExt<B, E, Block> | ||
where | ||
Block: BlockT + 'static, | ||
B: client::backend::Backend<Block, KeccakHasher, RlpCodec> + Send + Sync + 'static, | ||
E: client::CallExecutor<Block, KeccakHasher, RlpCodec> + Send + Sync + 'static, | ||
{ | ||
fn block_info( | ||
&self, | ||
number: Trailing<NumberFor<Block>>, | ||
) -> Result<Option<SignedBlock<Block::Header, Block::Extrinsic, Block::Hash>>> { | ||
let hash = match number.into() { | ||
None => Some(self.client.info()?.chain.best_hash), | ||
Some(number) => self | ||
.client | ||
.header(&BlockId::number(number))? | ||
.map(|h| h.hash()), | ||
}; | ||
let block_hash = match hash { | ||
None => self.client.info()?.chain.best_hash, | ||
Some(h) => h, | ||
}; | ||
|
||
Ok(self.client.block(&BlockId::Hash(block_hash))?) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
//! chainx RPC servers. | ||
|
||
use apis; | ||
use http; | ||
use pubsub; | ||
use runtime_primitives; | ||
use runtime_primitives::traits::{Block as BlockT, NumberFor}; | ||
use serde; | ||
use std; | ||
use std::io; | ||
use ws; | ||
|
||
use chainext; | ||
|
||
type Metadata = apis::metadata::Metadata; | ||
type RpcHandler = pubsub::PubSubHandler<Metadata>; | ||
pub type HttpServer = http::Server; | ||
pub type WsServer = ws::Server; | ||
|
||
/// Construct rpc `IoHandler` | ||
pub fn rpc_handler<Block: BlockT, ExHash, PendingExtrinsics, S, C, CE, A, Y>( | ||
state: S, | ||
chain: C, | ||
chainext: CE, | ||
author: A, | ||
system: Y, | ||
) -> RpcHandler | ||
where | ||
Block: BlockT + 'static, | ||
ExHash: Send | ||
+ Sync | ||
+ 'static | ||
+ runtime_primitives::Serialize | ||
+ runtime_primitives::DeserializeOwned, | ||
PendingExtrinsics: serde::Serialize + serde::de::DeserializeOwned + Send + Sync + 'static, | ||
S: apis::state::StateApi<Block::Hash, Metadata = Metadata>, | ||
C: apis::chain::ChainApi< | ||
Block::Hash, | ||
Block::Header, | ||
NumberFor<Block>, | ||
Block::Extrinsic, | ||
Metadata = Metadata, | ||
>, | ||
CE: chainext::ChainApiExt<Block::Hash, Block::Header, NumberFor<Block>, Block::Extrinsic>, | ||
A: apis::author::AuthorApi<ExHash, Block::Extrinsic, PendingExtrinsics, Metadata = Metadata>, | ||
Y: apis::system::SystemApi, | ||
{ | ||
let mut io = pubsub::PubSubHandler::default(); | ||
io.extend_with(state.to_delegate()); | ||
io.extend_with(chain.to_delegate()); | ||
io.extend_with(chainext.to_delegate()); | ||
io.extend_with(author.to_delegate()); | ||
io.extend_with(system.to_delegate()); | ||
io | ||
} | ||
|
||
/// Start HTTP server listening on given address. | ||
pub fn start_http(addr: &std::net::SocketAddr, io: RpcHandler) -> io::Result<http::Server> { | ||
http::ServerBuilder::new(io) | ||
.threads(4) | ||
.rest_api(http::RestApi::Unsecure) | ||
.cors(http::DomainsValidation::Disabled) | ||
.start_http(addr) | ||
} | ||
|
||
/// Start WS server listening on given address. | ||
pub fn start_ws(addr: &std::net::SocketAddr, io: RpcHandler) -> io::Result<ws::Server> { | ||
ws::ServerBuilder::with_meta_extractor(io, |context: &ws::RequestContext| { | ||
Metadata::new(context.sender()) | ||
}).start(addr) | ||
.map_err(|err| match err { | ||
ws::Error(ws::ErrorKind::Io(io), _) => io, | ||
ws::Error(ws::ErrorKind::ConnectionClosed, _) => io::ErrorKind::BrokenPipe.into(), | ||
ws::Error(e, _) => { | ||
error!("{}", e); | ||
io::ErrorKind::Other.into() | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.