Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Return null number for pending block in eth_getBlockByNumber (#8281)
Browse files Browse the repository at this point in the history
* Return null number for pending block in eth_getBlockByNumber

* Inline false in client_query

* block hash for pending should be null

* logsBloom should be null for pending blocks

* Fix test due to logsBloom type change
  • Loading branch information
sorpaas authored and andresilva committed Apr 2, 2018
1 parent 9c9ddac commit f1b7d8a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
23 changes: 16 additions & 7 deletions rpc/src/v1/impls/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ impl<C, SN: ?Sized, S: ?Sized, M, EM, T: StateInfo + 'static> EthClient<C, SN, S
fn rich_block(&self, id: BlockNumberOrId, include_txs: bool) -> Result<Option<RichBlock>> {
let client = &self.client;

let client_query = |id| (client.block(id), client.block_total_difficulty(id), client.block_extra_info(id));
let client_query = |id| (client.block(id), client.block_total_difficulty(id), client.block_extra_info(id), false);

let (block, difficulty, extra) = match id {
let (block, difficulty, extra, is_pending) = match id {
BlockNumberOrId::Number(BlockNumber::Pending) => {
let info = self.client.chain_info();
let pending_block = self.miner.pending_block(info.best_block_number);
Expand All @@ -199,7 +199,7 @@ impl<C, SN: ?Sized, S: ?Sized, M, EM, T: StateInfo + 'static> EthClient<C, SN, S

let extra = pending_block.as_ref().map(|b| self.client.engine().extra_info(&b.header));

(pending_block.map(|b| encoded::Block::new(b.rlp_bytes(Seal::Without))), Some(difficulty), extra)
(pending_block.map(|b| encoded::Block::new(b.rlp_bytes(Seal::Without))), Some(difficulty), extra, true)
},

BlockNumberOrId::Number(num) => {
Expand All @@ -221,7 +221,10 @@ impl<C, SN: ?Sized, S: ?Sized, M, EM, T: StateInfo + 'static> EthClient<C, SN, S
let view = block.header_view();
Ok(Some(RichBlock {
inner: Block {
hash: Some(view.hash().into()),
hash: match is_pending {
true => None,
false => Some(view.hash().into()),
},
size: Some(block.rlp().as_raw().len().into()),
parent_hash: view.parent_hash().into(),
uncles_hash: view.uncles_hash().into(),
Expand All @@ -230,10 +233,16 @@ impl<C, SN: ?Sized, S: ?Sized, M, EM, T: StateInfo + 'static> EthClient<C, SN, S
state_root: view.state_root().into(),
transactions_root: view.transactions_root().into(),
receipts_root: view.receipts_root().into(),
number: Some(view.number().into()),
number: match is_pending {
true => None,
false => Some(view.number().into()),
},
gas_used: view.gas_used().into(),
gas_limit: view.gas_limit().into(),
logs_bloom: view.log_bloom().into(),
logs_bloom: match is_pending {
true => None,
false => Some(view.log_bloom().into()),
},
timestamp: view.timestamp().into(),
difficulty: view.difficulty().into(),
total_difficulty: Some(total_difficulty.into()),
Expand Down Expand Up @@ -368,7 +377,7 @@ impl<C, SN: ?Sized, S: ?Sized, M, EM, T: StateInfo + 'static> EthClient<C, SN, S
number: Some(uncle.number().into()),
gas_used: uncle.gas_used().clone().into(),
gas_limit: uncle.gas_limit().clone().into(),
logs_bloom: uncle.log_bloom().clone().into(),
logs_bloom: Some(uncle.log_bloom().clone().into()),
timestamp: uncle.timestamp().into(),
difficulty: uncle.difficulty().clone().into(),
total_difficulty: Some((uncle.difficulty().clone() + parent_difficulty).into()),
Expand Down
4 changes: 2 additions & 2 deletions rpc/src/v1/impls/light/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ impl<T: LightChainClient + 'static> EthClient<T> {
number: Some(header.number().into()),
gas_used: header.gas_used().clone().into(),
gas_limit: header.gas_limit().clone().into(),
logs_bloom: header.log_bloom().clone().into(),
logs_bloom: Some(header.log_bloom().clone().into()),
timestamp: header.timestamp().into(),
difficulty: header.difficulty().clone().into(),
total_difficulty: score.map(Into::into),
Expand Down Expand Up @@ -570,7 +570,7 @@ fn extract_uncle_at_index<T: LightChainClient>(block: encoded::Block, index: Ind
number: Some(uncle.number().into()),
gas_used: uncle.gas_used().clone().into(),
gas_limit: uncle.gas_limit().clone().into(),
logs_bloom: uncle.log_bloom().clone().into(),
logs_bloom: Some(uncle.log_bloom().clone().into()),
timestamp: uncle.timestamp().into(),
difficulty: uncle.difficulty().clone().into(),
total_difficulty: None,
Expand Down
6 changes: 3 additions & 3 deletions rpc/src/v1/types/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub struct Block {
pub extra_data: Bytes,
/// Logs bloom
#[serde(rename="logsBloom")]
pub logs_bloom: H2048,
pub logs_bloom: Option<H2048>,
/// Timestamp
pub timestamp: U256,
/// Difficulty
Expand Down Expand Up @@ -254,7 +254,7 @@ mod tests {
gas_used: U256::default(),
gas_limit: U256::default(),
extra_data: Bytes::default(),
logs_bloom: H2048::default(),
logs_bloom: Some(H2048::default()),
timestamp: U256::default(),
difficulty: U256::default(),
total_difficulty: Some(U256::default()),
Expand Down Expand Up @@ -292,7 +292,7 @@ mod tests {
gas_used: U256::default(),
gas_limit: U256::default(),
extra_data: Bytes::default(),
logs_bloom: H2048::default(),
logs_bloom: Some(H2048::default()),
timestamp: U256::default(),
difficulty: U256::default(),
total_difficulty: Some(U256::default()),
Expand Down

0 comments on commit f1b7d8a

Please sign in to comment.