Print warnings when fetching pending blocks (#8711)

* Lots of println to figure out what eth_getBlockByNumber does/should do

* Remove debugging

* Print warnings when fetching pending blocks

When calling `eth_getBlockByNumber` with `pending`, we now print a deprecation warning and:

* if a pending block is found, use it to respond
* if no pending block is found, respond as if if was a request for `Latest`

Addresses issue #8703 (not sure if it's enough to close it tbh)
This commit is contained in:
David 2018-06-01 09:38:20 +02:00 committed by Marek Kotewicz
parent 00b209a29e
commit 799ae29ac4
2 changed files with 30 additions and 18 deletions

View File

@ -290,10 +290,12 @@ impl Miner {
{ {
self.sealing.lock().queue self.sealing.lock().queue
.peek_last_ref() .peek_last_ref()
.and_then(|b| if b.block().header().number() > latest_block_number { .and_then(|b| {
Some(f(b)) if b.block().header().number() > latest_block_number {
} else { Some(f(b))
None } else {
None
}
}) })
} }

View File

@ -108,6 +108,7 @@ pub struct EthClient<C, SN: ?Sized, S: ?Sized, M, EM> where
eip86_transition: u64, eip86_transition: u64,
} }
#[derive(Debug)]
enum BlockNumberOrId { enum BlockNumberOrId {
Number(BlockNumber), Number(BlockNumber),
Id(BlockId), Id(BlockId),
@ -184,21 +185,30 @@ impl<C, SN: ?Sized, S: ?Sized, M, EM, T: StateInfo + 'static> EthClient<C, SN, S
let (block, difficulty, extra, is_pending) = match id { let (block, difficulty, extra, is_pending) = match id {
BlockNumberOrId::Number(BlockNumber::Pending) => { BlockNumberOrId::Number(BlockNumber::Pending) => {
let info = self.client.chain_info(); let info = self.client.chain_info();
let pending_block = self.miner.pending_block(info.best_block_number); match self.miner.pending_block(info.best_block_number) {
let difficulty = { Some(pending_block) => {
let latest_difficulty = self.client.block_total_difficulty(BlockId::Latest).expect("blocks in chain have details; qed"); warn!("`Pending` is deprecated and may be removed in future versions.");
let pending_difficulty = self.miner.pending_block_header(info.best_block_number).map(|header| *header.difficulty());
if let Some(difficulty) = pending_difficulty { let difficulty = {
difficulty + latest_difficulty let latest_difficulty = self.client.block_total_difficulty(BlockId::Latest).expect("blocks in chain have details; qed");
} else { let pending_difficulty = self.miner.pending_block_header(info.best_block_number).map(|header| *header.difficulty());
latest_difficulty
if let Some(difficulty) = pending_difficulty {
difficulty + latest_difficulty
} else {
latest_difficulty
}
};
let extra = self.client.engine().extra_info(&pending_block.header);
(Some(encoded::Block::new(pending_block.rlp_bytes())), Some(difficulty), Some(extra), true)
},
None => {
warn!("`Pending` is deprecated and may be removed in future versions. Falling back to `Latest`");
client_query(BlockId::Latest)
} }
}; }
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())), Some(difficulty), extra, true)
}, },
BlockNumberOrId::Number(num) => { BlockNumberOrId::Number(num) => {
@ -206,7 +216,7 @@ impl<C, SN: ?Sized, S: ?Sized, M, EM, T: StateInfo + 'static> EthClient<C, SN, S
BlockNumber::Latest => BlockId::Latest, BlockNumber::Latest => BlockId::Latest,
BlockNumber::Earliest => BlockId::Earliest, BlockNumber::Earliest => BlockId::Earliest,
BlockNumber::Num(n) => BlockId::Number(n), BlockNumber::Num(n) => BlockId::Number(n),
BlockNumber::Pending => unreachable!(), // Already covered BlockNumber::Pending => unreachable!() // Already covered
}; };
client_query(id) client_query(id)