From e4f863b4b0721ed993fdfe3333957f8231e8ae72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Silva?= Date: Fri, 23 Feb 2018 09:41:29 +0000 Subject: [PATCH] rpc: don't include current block in new_block_filter (#7982) --- rpc/src/v1/impls/eth_filter.rs | 5 +++-- rpc/src/v1/tests/mocked/eth.rs | 25 ++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/rpc/src/v1/impls/eth_filter.rs b/rpc/src/v1/impls/eth_filter.rs index 4803c2541..0ff9f4c20 100644 --- a/rpc/src/v1/impls/eth_filter.rs +++ b/rpc/src/v1/impls/eth_filter.rs @@ -111,7 +111,8 @@ impl EthFilter for T { fn new_block_filter(&self) -> Result { let mut polls = self.polls().lock(); - let id = polls.create_poll(PollFilter::Block(self.best_block_number())); + // +1, since we don't want to include the current block + let id = polls.create_poll(PollFilter::Block(self.best_block_number() + 1)); Ok(id.into()) } @@ -129,7 +130,7 @@ impl EthFilter for T { None => Either::A(future::ok(FilterChanges::Empty)), Some(filter) => match *filter { PollFilter::Block(ref mut block_number) => { - // + 1, cause we want to return hashes including current block hash. + // +1, cause we want to return hashes including current block hash. let current_number = self.best_block_number() + 1; let hashes = (*block_number..current_number).into_iter() .map(BlockId::Number) diff --git a/rpc/src/v1/tests/mocked/eth.rs b/rpc/src/v1/tests/mocked/eth.rs index c66da9b1c..1b15b960d 100644 --- a/rpc/src/v1/tests/mocked/eth.rs +++ b/rpc/src/v1/tests/mocked/eth.rs @@ -22,7 +22,7 @@ use std::time::{Instant, Duration}; use ethereum_types::{H256, U256, Address}; use parking_lot::Mutex; use ethcore::account_provider::AccountProvider; -use ethcore::client::{TestBlockChainClient, EachBlockWith, Executed, TransactionId}; +use ethcore::client::{BlockChainClient, BlockId, EachBlockWith, Executed, TestBlockChainClient, TransactionId}; use ethcore::log_entry::{LocalizedLogEntry, LogEntry}; use ethcore::miner::MinerService; use ethcore::receipt::{LocalizedReceipt, TransactionOutcome}; @@ -284,6 +284,29 @@ fn rpc_logs_filter() { assert_eq!(tester.io.handle_request_sync(request_changes2), Some(response2.to_owned())); } +#[test] +fn rpc_blocks_filter() { + let tester = EthTester::default(); + let request_filter = r#"{"jsonrpc": "2.0", "method": "eth_newBlockFilter", "id": 1}"#; + let response = r#"{"jsonrpc":"2.0","result":"0x0","id":1}"#; + + assert_eq!(tester.io.handle_request_sync(request_filter), Some(response.to_owned())); + + let request_changes = r#"{"jsonrpc": "2.0", "method": "eth_getFilterChanges", "params": ["0x0"], "id": 1}"#; + let response = r#"{"jsonrpc":"2.0","result":[],"id":1}"#; + + assert_eq!(tester.io.handle_request_sync(request_changes), Some(response.to_owned())); + + tester.client.add_blocks(2, EachBlockWith::Nothing); + + let response = format!( + r#"{{"jsonrpc":"2.0","result":["0x{:x}","0x{:x}"],"id":1}}"#, + tester.client.block_hash(BlockId::Number(1)).unwrap(), + tester.client.block_hash(BlockId::Number(2)).unwrap()); + + assert_eq!(tester.io.handle_request_sync(request_changes), Some(response.to_owned())); +} + #[test] fn rpc_eth_submit_hashrate() { let tester = EthTester::default();