From 2cf0d7d7757829a8d6d3a65547c96258c73b9b91 Mon Sep 17 00:00:00 2001 From: Seun LanLege Date: Fri, 10 May 2019 20:20:42 +0100 Subject: [PATCH] Adds parity_getRawBlockByNumber, parity_submitRawBlock (#10609) * adds parity_getRawBlockByNumber (cherry picked from commit 1b418f592a03d45997f54304ca10889a96199509) * added parity_submitRawBlock * remove space Co-Authored-By: Andronik Ordian --- rpc/src/v1/helpers/errors.rs | 9 +++++++++ rpc/src/v1/impls/light/parity.rs | 14 ++++++++++++++ rpc/src/v1/impls/parity.rs | 19 +++++++++++++++++++ rpc/src/v1/traits/parity.rs | 8 ++++++++ 4 files changed, 50 insertions(+) diff --git a/rpc/src/v1/helpers/errors.rs b/rpc/src/v1/helpers/errors.rs index 4845f76f5..023f7df3c 100644 --- a/rpc/src/v1/helpers/errors.rs +++ b/rpc/src/v1/helpers/errors.rs @@ -39,6 +39,7 @@ mod codes { pub const NO_NEW_WORK: i64 = -32003; pub const NO_WORK_REQUIRED: i64 = -32004; pub const CANNOT_SUBMIT_WORK: i64 = -32005; + pub const CANNOT_SUBMIT_BLOCK: i64 = -32006; pub const UNKNOWN_ERROR: i64 = -32009; pub const TRANSACTION_ERROR: i64 = -32010; pub const EXECUTION_ERROR: i64 = -32015; @@ -246,6 +247,14 @@ pub fn unavailable_block(no_ancient_block: bool, by_hash: bool) -> Error { } } +pub fn cannot_submit_block(err: EthcoreError) -> Error { + Error { + code: ErrorCode::ServerError(codes::CANNOT_SUBMIT_BLOCK), + message: "Cannot submit block.".into(), + data: Some(Value::String(err.to_string())), + } +} + pub fn check_block_number_existence<'a, T, C>( client: &'a C, num: BlockNumber, diff --git a/rpc/src/v1/impls/light/parity.rs b/rpc/src/v1/impls/light/parity.rs index 0486366de..f3dea5485 100644 --- a/rpc/src/v1/impls/light/parity.rs +++ b/rpc/src/v1/impls/light/parity.rs @@ -47,6 +47,8 @@ use v1::types::{ Log, Filter, }; use Host; +use v1::helpers::errors::light_unimplemented; +use v1::types::block_number_to_id; /// Parity implementation for light client. pub struct ParityClient @@ -407,4 +409,16 @@ where fn verify_signature(&self, is_prefixed: bool, message: Bytes, r: H256, s: H256, v: U64) -> Result { verify_signature(is_prefixed, message, r, s, v, self.light_dispatch.client.signing_chain_id()) } + + fn get_raw_block_by_number(&self, block: BlockNumber) -> BoxFuture> { + Box::new( + self.fetcher() + .block(block_number_to_id(block)) + .map(|block| Some(Bytes::from(block.raw().to_vec()))) + ) + } + + fn submit_raw_block(&self, _block: Bytes) -> Result { + Err(light_unimplemented(None)) + } } diff --git a/rpc/src/v1/impls/parity.rs b/rpc/src/v1/impls/parity.rs index 796b3f9f5..0430acb75 100644 --- a/rpc/src/v1/impls/parity.rs +++ b/rpc/src/v1/impls/parity.rs @@ -49,6 +49,7 @@ use v1::types::{ block_number_to_id }; use Host; +use ethcore::verification::queue::kind::blocks::Unverified; /// Parity implementation. pub struct ParityClient { @@ -462,4 +463,22 @@ impl Parity for ParityClient where fn verify_signature(&self, is_prefixed: bool, message: Bytes, r: H256, s: H256, v: U64) -> Result { verify_signature(is_prefixed, message, r, s, v, self.client.signing_chain_id()) } + + fn get_raw_block_by_number(&self, block_number: BlockNumber) -> BoxFuture> { + Box::new(futures::done( + Ok( + self.client + .block(block_number_to_id(block_number)) + .map(|block| Bytes::from(block.raw().to_vec())) + ) + )) + } + + + fn submit_raw_block(&self, block: Bytes) -> Result { + let result = self.client.import_block( + Unverified::from_rlp(block.into_vec()).map_err(errors::rlp)? + ); + Ok(result.map_err(errors::cannot_submit_block)?) + } } diff --git a/rpc/src/v1/traits/parity.rs b/rpc/src/v1/traits/parity.rs index e3821355e..a89e13171 100644 --- a/rpc/src/v1/traits/parity.rs +++ b/rpc/src/v1/traits/parity.rs @@ -232,4 +232,12 @@ pub trait Parity { /// Is allowed to skip filling transaction hash for faster query. #[rpc(name = "parity_getLogsNoTransactionHash")] fn logs_no_tx_hash(&self, Filter) -> BoxFuture>; + + /// Returns raw block RLP with given number. + #[rpc(name = "parity_getRawBlockByNumber")] + fn get_raw_block_by_number(&self, BlockNumber) -> BoxFuture>; + + /// Submit raw block to be published to the network + #[rpc(name = "parity_submitRawBlock")] + fn submit_raw_block(&self, Bytes) -> Result; }