openethereum/crates/ethcore/sync/src/tests/chain.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

302 lines
9.5 KiB
Rust
Raw Normal View History

2020-09-22 14:53:52 +02:00
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
// This file is part of OpenEthereum.
2016-02-05 13:40:41 +01:00
2020-09-22 14:53:52 +02:00
// OpenEthereum is free software: you can redistribute it and/or modify
2016-02-05 13:40:41 +01:00
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
2020-09-22 14:53:52 +02:00
// OpenEthereum is distributed in the hope that it will be useful,
2016-02-05 13:40:41 +01:00
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
2020-09-22 14:53:52 +02:00
// along with OpenEthereum. If not, see <http://www.gnu.org/licenses/>.
2016-02-05 13:40:41 +01:00
2016-02-04 19:30:31 +01:00
use super::helpers::*;
use chain::SyncState;
`Client` refactoring (#7038) * Improves `BestBlock` comment * Improves `TraceDB` comment * Improves `journaldb::Algorithm` comment. Probably the whole enum should be renamed to `Strategy` or something alike. * Comments some of the `Client`'s fields * Deglobs client imports * Fixes comments * Extracts `import_lock` to `Importer` struct * Extracts `verifier` to `Importer` struct * Extracts `block_queue` to `Importer` struct * Extracts `miner` to `Importer` struct * Extracts `ancient_verifier` to `Importer` struct * Extracts `rng` to `Importer` struct * Extracts `import_old_block` to `Importer` struct * Adds `Nonce` trait * Adds `Balance` trait * Adds `ChainInfo` trait * Fixes imports for tests using `chain_info` method * Adds `BlockInfo` trait * Adds more `ChainInfo` imports * Adds `BlockInfo` imports * Adds `ReopenBlock` trait * Adds `PrepareOpenBlock` trait * Fixes import in tests * Adds `CallContract` trait * Fixes imports in tests using `call_contract` method * Adds `TransactionInfo` trait * Adds `RegistryInfo` trait * Fixes imports in tests using `registry_address` method * Adds `ScheduleInfo` trait * Adds `ImportSealedBlock` trait * Fixes imports in test using `import_sealed_block` method * Adds `BroadcastProposalBlock` trait * Migrates `Miner` to static dispatch * Fixes tests * Moves `calculate_enacted_retracted` to `Importer` * Moves import-related methods to `Importer` * Removes redundant `import_old_block` wrapper * Extracts `import_block*` into separate trait * Fixes tests * Handles `Pending` in `LightFetch` * Handles `Pending` in filters * Handles `Pending` in `ParityClient` * Handles `Pending` in `EthClient` * Removes `BlockId::Pending`, partly refactors dependent code * Adds `StateInfo` trait * Exports `StateOrBlock` and `BlockChain` types from `client` module * Refactors `balance` RPC using generic API * Refactors `storage_at` RPC using generic API * Makes `MinerService::pending_state`'s return type dynamic * Adds `StateOrBlock` and `BlockChain` types * Adds impl of `client::BlockChain` for `Client` * Exports `StateInfo` trait from `client` module * Missing `self` use To be fixed up to "Adds impl of `client::BlockChain` for `Client`" * Adds `number_to_id` and refactors dependent RPC methods * Refactors `code_at` using generic API * Adds `StateClient` trait * Refactors RPC to use `StateClient` trait * Reverts `client::BlockChain` trait stuff, refactors methods to accept `StateOrBlock` * Refactors TestClient * Adds helper function `block_number_to_id` * Uses `block_number_to_id` instead of local function * Handles `Pending` in `list_accounts` and `list_storage_keys` * Attempt to use associated types for state instead of trait objects * Simplifies `state_at_beginning` * Extracts `call` and `call_many` into separate trait * Refactors `build_last_hashes` to accept reference * Exports `Call` type from the module * Refactors `call` and `call_many` to accept state and header * Exports `state_at` in `StateClient` * Exports `pending_block_header` from `MinerService` * Refactors RPC `call` method using new API * Adds missing parentheses * Refactors `parity::call` to use new call API * Update .gitlab-ci.yml fix gitlab lint * Fixes error handling * Refactors `traces::call` and `call_many` to use new call API * Refactors `call_contract` * Refactors `block_header` * Refactors internal RPC method `block` * Moves `estimate_gas` to `Call` trait, refactors parameters * Refactors `estimate_gas` in RPC * Refactors `uncle` * Refactors RPC `transaction` * Covers missing branches * Makes it all compile, fixes compiler grumbles * Adds casts in `blockchain` module * Fixes `PendingBlock` tests, work on `MinerService` * Adds test stubs for StateClient and EngineInfo * Makes `state_db` public * Adds missing impls for `TestBlockChainClient` * Adds trait documentation * Adds missing docs to the `state_db` module * Fixes trivial compilation errors * Moves `code_hash` method to a `BlockInfo` trait * Refactors `Verifier` to be generic over client * Refactors `TransactionFilter` to be generic over client * Refactors `Miner` and `Client` to reflect changes in verifier and txfilter API * Moves `ServiceTransactionChecker` back to `ethcore` * Fixes trait bounds in `Miner` API * Fixes `Client` * Fixes lifetime bound in `FullFamilyParams` * Adds comments to `FullFamilyParams` * Fixes imports in `ethcore` * Fixes BlockNumber handling in `code_at` and `replay_block_transactions` * fix compile issues * First step to redundant trait merge * Fixes compilation error in RPC tests * Adds mock `State` as a stub for `TestClient` * Handles `StateOrBlock::State` in `TestBlockChainClient::balance` * Fixes `transaction_count` RPC * Fixes `transaction_count` * Moves `service_transaction.json` to the `contracts` subfolder * Fixes compilation errors in tests * Refactors client to use `AccountData` * Refactors client to use `BlockChain` * Refactors miner to use aggregate traits * Adds `SealedBlockImporter` trait * Refactors miner to use `SealedBlockImporter` trait * Removes unused imports * Simplifies `RegistryInfo::registry_address` * Fixes indentation * Removes commented out trait bound
2018-03-03 18:42:13 +01:00
use ethcore::client::{
BlockChainClient, BlockId, BlockInfo, ChainInfo, EachBlockWith, TestBlockChainClient,
};
use std::sync::Arc;
use SyncConfig;
use WarpSync;
2016-02-04 19:30:31 +01:00
#[test]
2016-02-04 20:03:14 +01:00
fn two_peers() {
::env_logger::try_init().ok();
2016-02-04 19:30:31 +01:00
let mut net = TestNet::new(3);
2016-12-06 19:23:15 +01:00
net.peer(1).chain.add_blocks(1000, EachBlockWith::Uncle);
net.peer(2).chain.add_blocks(1000, EachBlockWith::Uncle);
2016-02-04 19:30:31 +01:00
net.sync();
assert!(net.peer(0).chain.block(BlockId::Number(1000)).is_some());
assert_eq!(
*net.peer(0).chain.blocks.read(),
*net.peer(1).chain.blocks.read()
);
2016-02-04 19:30:31 +01:00
}
2016-05-16 14:41:41 +02:00
#[test]
fn long_chain() {
::env_logger::try_init().ok();
2016-05-16 14:41:41 +02:00
let mut net = TestNet::new(2);
2016-12-06 19:23:15 +01:00
net.peer(1).chain.add_blocks(50000, EachBlockWith::Nothing);
2016-05-16 14:41:41 +02:00
net.sync();
assert!(net.peer(0).chain.block(BlockId::Number(50000)).is_some());
assert_eq!(
*net.peer(0).chain.blocks.read(),
*net.peer(1).chain.blocks.read()
);
2016-05-16 14:41:41 +02:00
}
2016-02-04 19:30:31 +01:00
#[test]
2016-02-04 20:03:14 +01:00
fn status_after_sync() {
::env_logger::try_init().ok();
2016-02-04 19:30:31 +01:00
let mut net = TestNet::new(3);
2016-12-06 19:23:15 +01:00
net.peer(1).chain.add_blocks(1000, EachBlockWith::Uncle);
net.peer(2).chain.add_blocks(1000, EachBlockWith::Uncle);
2016-02-04 19:30:31 +01:00
net.sync();
let status = net.peer(0).sync.read().status();
2016-02-04 19:30:31 +01:00
assert_eq!(status.state, SyncState::Idle);
}
#[test]
2016-02-04 20:03:14 +01:00
fn takes_few_steps() {
2016-02-04 19:30:31 +01:00
let mut net = TestNet::new(3);
2016-12-06 19:23:15 +01:00
net.peer(1).chain.add_blocks(100, EachBlockWith::Uncle);
net.peer(2).chain.add_blocks(100, EachBlockWith::Uncle);
2016-02-04 19:30:31 +01:00
let total_steps = net.sync();
2016-05-16 14:41:41 +02:00
assert!(total_steps < 20);
2016-02-04 19:30:31 +01:00
}
#[test]
2016-02-04 20:03:14 +01:00
fn empty_blocks() {
::env_logger::try_init().ok();
2016-02-04 19:30:31 +01:00
let mut net = TestNet::new(3);
for n in 0..200 {
let with = if n % 2 == 0 {
EachBlockWith::Nothing
} else {
EachBlockWith::Uncle
};
2016-12-06 19:23:15 +01:00
net.peer(1).chain.add_blocks(5, with.clone());
net.peer(2).chain.add_blocks(5, with);
2016-02-04 19:30:31 +01:00
}
net.sync();
assert!(net.peer(0).chain.block(BlockId::Number(1000)).is_some());
assert_eq!(
*net.peer(0).chain.blocks.read(),
*net.peer(1).chain.blocks.read()
);
2016-02-04 19:30:31 +01:00
}
#[test]
2016-02-04 20:03:14 +01:00
fn forked() {
::env_logger::try_init().ok();
2016-02-04 19:30:31 +01:00
let mut net = TestNet::new(3);
2016-12-06 19:23:15 +01:00
net.peer(0).chain.add_blocks(30, EachBlockWith::Uncle);
net.peer(1).chain.add_blocks(30, EachBlockWith::Uncle);
net.peer(2).chain.add_blocks(30, EachBlockWith::Uncle);
net.peer(0).chain.add_blocks(10, EachBlockWith::Nothing); //fork
net.peer(1).chain.add_blocks(20, EachBlockWith::Uncle);
net.peer(2).chain.add_blocks(20, EachBlockWith::Uncle);
net.peer(1).chain.add_blocks(10, EachBlockWith::Uncle); //fork between 1 and 2
net.peer(2).chain.add_blocks(1, EachBlockWith::Nothing);
2016-02-04 19:30:31 +01:00
// peer 1 has the best chain of 601 blocks
let peer1_chain = net.peer(1).chain.numbers.read().clone();
2016-02-04 19:30:31 +01:00
net.sync();
assert_eq!(
*net.peer(0).chain.difficulty.read(),
*net.peer(1).chain.difficulty.read()
);
assert_eq!(&*net.peer(0).chain.numbers.read(), &peer1_chain);
assert_eq!(&*net.peer(1).chain.numbers.read(), &peer1_chain);
assert_eq!(&*net.peer(2).chain.numbers.read(), &peer1_chain);
2016-02-04 19:30:31 +01:00
}
#[test]
fn forked_with_misbehaving_peer() {
::env_logger::try_init().ok();
let mut net = TestNet::new(3);
2020-08-05 06:08:03 +02:00
let mut alt_spec = ::ethcore::spec::Spec::new_test();
alt_spec.extra_data = b"fork".to_vec();
// peer 0 is on a totally different chain with higher total difficulty
net.peer_mut(0).chain = Arc::new(TestBlockChainClient::new_with_spec(alt_spec));
2016-12-06 19:23:15 +01:00
net.peer(0).chain.add_blocks(50, EachBlockWith::Nothing);
net.peer(1).chain.add_blocks(10, EachBlockWith::Nothing);
net.peer(2).chain.add_blocks(10, EachBlockWith::Nothing);
2020-08-05 06:08:03 +02:00
2016-12-06 19:23:15 +01:00
net.peer(1).chain.add_blocks(10, EachBlockWith::Nothing);
net.peer(2).chain.add_blocks(20, EachBlockWith::Uncle);
// peer 1 should sync to peer 2, others should not change
let peer0_chain = net.peer(0).chain.numbers.read().clone();
let peer2_chain = net.peer(2).chain.numbers.read().clone();
net.sync();
assert_eq!(&*net.peer(0).chain.numbers.read(), &peer0_chain);
assert_eq!(&*net.peer(1).chain.numbers.read(), &peer2_chain);
assert_eq!(&*net.peer(2).chain.numbers.read(), &peer2_chain);
}
2016-07-27 21:38:22 +02:00
#[test]
fn net_hard_fork() {
::env_logger::try_init().ok();
2016-07-27 21:38:22 +02:00
let ref_client = TestBlockChainClient::new();
ref_client.add_blocks(50, EachBlockWith::Uncle);
{
let mut net = TestNet::new_with_fork(
2,
Some((50, ref_client.block_hash(BlockId::Number(50)).unwrap())),
);
2016-12-06 19:23:15 +01:00
net.peer(0).chain.add_blocks(100, EachBlockWith::Uncle);
2016-07-27 21:38:22 +02:00
net.sync();
assert_eq!(net.peer(1).chain.chain_info().best_block_number, 100);
}
{
let mut net = TestNet::new_with_fork(
2,
Some((50, ref_client.block_hash(BlockId::Number(50)).unwrap())),
);
2016-12-06 19:23:15 +01:00
net.peer(0).chain.add_blocks(100, EachBlockWith::Nothing);
2016-07-27 21:38:22 +02:00
net.sync();
assert_eq!(net.peer(1).chain.chain_info().best_block_number, 0);
}
}
2016-02-04 19:30:31 +01:00
#[test]
2016-02-04 20:03:14 +01:00
fn restart() {
::env_logger::try_init().ok();
2016-02-04 19:30:31 +01:00
let mut net = TestNet::new(3);
2016-12-06 19:23:15 +01:00
net.peer(1).chain.add_blocks(1000, EachBlockWith::Uncle);
net.peer(2).chain.add_blocks(1000, EachBlockWith::Uncle);
2016-02-04 19:30:31 +01:00
net.sync();
2016-02-04 19:30:31 +01:00
// make sure that sync has actually happened
assert!(net.peer(0).chain.chain_info().best_block_number > 100);
net.restart_peer(0);
let status = net.peer(0).sync.read().status();
assert_eq!(status.state, SyncState::Idle);
2016-02-04 19:30:31 +01:00
}
#[test]
2016-02-04 20:03:14 +01:00
fn status_empty() {
2016-02-04 19:30:31 +01:00
let net = TestNet::new(2);
assert_eq!(net.peer(0).sync.read().status().state, SyncState::Idle);
let mut config = SyncConfig::default();
config.warp_sync = WarpSync::Enabled;
let net = TestNet::new_with_config(2, config);
assert_eq!(
net.peer(0).sync.read().status().state,
SyncState::WaitingPeers
);
2016-02-05 18:34:08 +01:00
}
#[test]
fn status_packet() {
let mut net = TestNet::new(2);
2016-12-06 19:23:15 +01:00
net.peer(0).chain.add_blocks(100, EachBlockWith::Uncle);
net.peer(1).chain.add_blocks(1, EachBlockWith::Uncle);
2016-02-05 18:34:08 +01:00
net.start();
net.sync_step_peer(0);
2016-12-06 19:23:15 +01:00
assert_eq!(1, net.peer(0).queue.read().len());
assert_eq!(0x00, net.peer(0).queue.read()[0].packet_id);
2016-02-05 18:34:08 +01:00
}
#[test]
fn propagate_hashes() {
2016-02-12 13:07:02 +01:00
let mut net = TestNet::new(6);
2016-12-06 19:23:15 +01:00
net.peer(1).chain.add_blocks(10, EachBlockWith::Uncle);
2016-02-05 18:34:08 +01:00
net.sync();
2020-08-05 06:08:03 +02:00
2016-12-06 19:23:15 +01:00
net.peer(0).chain.add_blocks(10, EachBlockWith::Uncle);
2016-02-12 13:07:02 +01:00
net.sync();
net.trigger_chain_new_blocks(0); //first event just sets the marker
net.trigger_chain_new_blocks(0);
2020-08-05 06:08:03 +02:00
// 5 peers with NewHahses, 4 with blocks
2016-12-06 19:23:15 +01:00
assert_eq!(9, net.peer(0).queue.read().len());
2016-02-12 13:07:02 +01:00
let mut hashes = 0;
let mut blocks = 0;
2016-12-06 19:23:15 +01:00
for i in 0..net.peer(0).queue.read().len() {
if net.peer(0).queue.read()[i].packet_id == 0x1 {
2016-02-12 13:07:02 +01:00
hashes += 1;
}
2016-12-06 19:23:15 +01:00
if net.peer(0).queue.read()[i].packet_id == 0x7 {
2016-02-12 13:07:02 +01:00
blocks += 1;
}
}
assert_eq!(blocks, 4);
assert_eq!(hashes, 5);
2016-02-07 01:00:43 +01:00
}
#[test]
fn propagate_blocks() {
2016-05-29 00:38:10 +02:00
let mut net = TestNet::new(20);
2016-12-06 19:23:15 +01:00
net.peer(1).chain.add_blocks(10, EachBlockWith::Uncle);
2016-02-07 01:00:43 +01:00
net.sync();
2020-08-05 06:08:03 +02:00
2016-12-06 19:23:15 +01:00
net.peer(0).chain.add_blocks(10, EachBlockWith::Uncle);
net.trigger_chain_new_blocks(0); //first event just sets the marker
net.trigger_chain_new_blocks(0);
2020-08-05 06:08:03 +02:00
2016-12-06 19:23:15 +01:00
assert!(!net.peer(0).queue.read().is_empty());
2016-02-06 21:04:58 +01:00
// NEW_BLOCK_PACKET
2016-12-06 19:23:15 +01:00
let blocks = net
.peer(0)
.queue
.read()
.iter()
.filter(|p| p.packet_id == 0x7)
.count();
2016-05-29 00:38:10 +02:00
assert!(blocks > 0);
}
#[test]
fn restart_on_malformed_block() {
::env_logger::try_init().ok();
let mut net = TestNet::new(2);
net.peer(1).chain.add_blocks(5, EachBlockWith::Nothing);
net.peer(1)
.chain
.add_block(EachBlockWith::Nothing, |mut header| {
header
.set_extra_data(b"This extra data is way too long to be considered valid".to_vec());
header
});
2016-05-16 14:41:41 +02:00
net.sync_steps(20);
2020-08-05 06:08:03 +02:00
// This gets accepted just fine since the TestBlockChainClient performs no validation.
// Probably remove this test?
assert_eq!(net.peer(0).chain.chain_info().best_block_number, 6);
2016-05-16 14:41:41 +02:00
}
#[test]
fn reject_on_broken_chain() {
let mut net = TestNet::new(2);
net.peer(1).chain.add_blocks(10, EachBlockWith::Nothing);
2016-12-06 19:23:15 +01:00
net.peer(1).chain.corrupt_block_parent(6);
net.sync_steps(20);
assert_eq!(net.peer(0).chain.chain_info().best_block_number, 0);
}
2016-11-28 16:30:36 +01:00
#[test]
fn disconnect_on_unrelated_chain() {
::env_logger::try_init().ok();
2016-11-28 16:30:36 +01:00
let mut net = TestNet::new(2);
net.peer(0).chain.set_history(Some(20));
net.peer(1).chain.set_history(Some(20));
net.restart_peer(0);
net.restart_peer(1);
net.peer(0).chain.add_blocks(500, EachBlockWith::Uncle);
net.peer(1).chain.add_blocks(300, EachBlockWith::Nothing);
2016-11-28 16:30:36 +01:00
net.sync();
assert_eq!(net.disconnect_events, vec![(0, 0)]);
}