Return null number for pending block in eth_getBlockByNumber (#8281)

* 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
This commit is contained in:
Wei Tang 2018-04-02 17:04:14 +08:00 committed by André Silva
parent 9c9ddaccec
commit f1b7d8ab34
3 changed files with 21 additions and 12 deletions

View File

@ -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);
@ -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) => {
@ -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(),
@ -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()),
@ -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()),

View File

@ -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),
@ -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,

View File

@ -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
@ -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()),
@ -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()),