2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2016-12-06 19:23:15 +01:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2016-12-06 19:23:15 +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.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2016-12-06 19:23:15 +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
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-12-06 19:23:15 +01:00
|
|
|
|
2017-07-29 21:56:42 +02:00
|
|
|
use std::sync::Arc;
|
2017-08-31 11:35:41 +02:00
|
|
|
use hash::keccak;
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::{U256, Address};
|
2018-04-09 16:14:33 +02:00
|
|
|
use io::{IoHandler, IoChannel};
|
|
|
|
use ethcore::client::{ChainInfo, ClientIoMessage};
|
2019-02-07 14:34:24 +01:00
|
|
|
use ethcore::engines;
|
2016-12-06 19:23:15 +01:00
|
|
|
use ethcore::spec::Spec;
|
2019-02-07 14:34:24 +01:00
|
|
|
use ethcore::miner::{self, MinerService};
|
2017-01-11 12:16:47 +01:00
|
|
|
use ethkey::{KeyPair, Secret};
|
2019-01-04 14:05:46 +01:00
|
|
|
use types::transaction::{Action, PendingTransaction, Transaction};
|
2016-12-06 19:23:15 +01:00
|
|
|
use super::helpers::*;
|
2018-01-10 13:35:18 +01:00
|
|
|
use SyncConfig;
|
2016-12-06 19:23:15 +01:00
|
|
|
|
2017-08-21 13:46:58 +02:00
|
|
|
fn new_tx(secret: &Secret, nonce: U256, chain_id: u64) -> PendingTransaction {
|
2016-12-15 19:10:33 +01:00
|
|
|
let signed = Transaction {
|
2016-12-12 17:24:48 +01:00
|
|
|
nonce: nonce.into(),
|
|
|
|
gas_price: 0.into(),
|
|
|
|
gas: 21000.into(),
|
|
|
|
action: Action::Call(Address::default()),
|
|
|
|
value: 0.into(),
|
|
|
|
data: Vec::new(),
|
2017-08-21 13:46:58 +02:00
|
|
|
}.sign(secret, Some(chain_id));
|
2016-12-15 19:10:33 +01:00
|
|
|
PendingTransaction::new(signed, None)
|
2016-12-12 17:24:48 +01:00
|
|
|
}
|
|
|
|
|
2016-12-06 19:23:15 +01:00
|
|
|
#[test]
|
2016-12-11 12:32:01 +01:00
|
|
|
fn authority_round() {
|
2017-08-31 11:35:41 +02:00
|
|
|
let s0 = KeyPair::from_secret_slice(&keccak("1")).unwrap();
|
|
|
|
let s1 = KeyPair::from_secret_slice(&keccak("0")).unwrap();
|
2017-01-18 18:49:50 +01:00
|
|
|
|
2017-08-21 13:46:58 +02:00
|
|
|
let chain_id = Spec::new_test_round().chain_id();
|
2019-02-07 14:34:24 +01:00
|
|
|
let mut net = TestNet::with_spec(2, SyncConfig::default(), Spec::new_test_round);
|
2018-04-09 16:14:33 +02:00
|
|
|
let io_handler0: Arc<IoHandler<ClientIoMessage>> = Arc::new(TestIoHandler::new(net.peer(0).chain.clone()));
|
|
|
|
let io_handler1: Arc<IoHandler<ClientIoMessage>> = Arc::new(TestIoHandler::new(net.peer(1).chain.clone()));
|
2016-12-11 12:32:01 +01:00
|
|
|
// Push transaction to both clients. Only one of them gets lucky to produce a block.
|
2019-02-07 14:34:24 +01:00
|
|
|
net.peer(0).miner.set_author(miner::Author::Sealer(engines::signer::from_keypair(s0.clone())));
|
|
|
|
net.peer(1).miner.set_author(miner::Author::Sealer(engines::signer::from_keypair(s1.clone())));
|
2017-09-05 17:54:05 +02:00
|
|
|
net.peer(0).chain.engine().register_client(Arc::downgrade(&net.peer(0).chain) as _);
|
|
|
|
net.peer(1).chain.engine().register_client(Arc::downgrade(&net.peer(1).chain) as _);
|
2016-12-11 12:32:01 +01:00
|
|
|
net.peer(0).chain.set_io_channel(IoChannel::to_handler(Arc::downgrade(&io_handler1)));
|
2016-12-12 17:24:48 +01:00
|
|
|
net.peer(1).chain.set_io_channel(IoChannel::to_handler(Arc::downgrade(&io_handler0)));
|
2016-12-11 12:32:01 +01:00
|
|
|
// exchange statuses
|
|
|
|
net.sync();
|
2016-12-12 17:24:48 +01:00
|
|
|
// Trigger block proposal
|
2018-04-13 17:34:27 +02:00
|
|
|
net.peer(0).miner.import_own_transaction(&*net.peer(0).chain, new_tx(s0.secret(), 0.into(), chain_id)).unwrap();
|
|
|
|
net.peer(1).miner.import_own_transaction(&*net.peer(1).chain, new_tx(s1.secret(), 0.into(), chain_id)).unwrap();
|
2016-12-12 17:24:48 +01:00
|
|
|
// Sync a block
|
2016-12-06 19:23:15 +01:00
|
|
|
net.sync();
|
|
|
|
assert_eq!(net.peer(0).chain.chain_info().best_block_number, 1);
|
|
|
|
assert_eq!(net.peer(1).chain.chain_info().best_block_number, 1);
|
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
net.peer(0).miner.import_own_transaction(&*net.peer(0).chain, new_tx(s0.secret(), 1.into(), chain_id)).unwrap();
|
|
|
|
net.peer(1).miner.import_own_transaction(&*net.peer(1).chain, new_tx(s1.secret(), 1.into(), chain_id)).unwrap();
|
2017-03-03 22:32:22 +01:00
|
|
|
// Move to next proposer step.
|
2016-12-12 17:24:48 +01:00
|
|
|
net.peer(0).chain.engine().step();
|
2016-12-06 19:23:15 +01:00
|
|
|
net.peer(1).chain.engine().step();
|
|
|
|
net.sync();
|
|
|
|
assert_eq!(net.peer(0).chain.chain_info().best_block_number, 2);
|
|
|
|
assert_eq!(net.peer(1).chain.chain_info().best_block_number, 2);
|
2016-12-12 17:24:48 +01:00
|
|
|
|
2017-03-03 22:32:22 +01:00
|
|
|
// Fork the network with equal height.
|
2018-04-13 17:34:27 +02:00
|
|
|
net.peer(0).miner.import_own_transaction(&*net.peer(0).chain, new_tx(s0.secret(), 2.into(), chain_id)).unwrap();
|
|
|
|
net.peer(1).miner.import_own_transaction(&*net.peer(1).chain, new_tx(s1.secret(), 2.into(), chain_id)).unwrap();
|
2017-03-03 22:32:22 +01:00
|
|
|
// Let both nodes build one block.
|
2016-12-12 17:24:48 +01:00
|
|
|
net.peer(0).chain.engine().step();
|
2017-03-03 22:32:22 +01:00
|
|
|
let early_hash = net.peer(0).chain.chain_info().best_block_hash;
|
2016-12-12 17:24:48 +01:00
|
|
|
net.peer(1).chain.engine().step();
|
|
|
|
net.peer(0).chain.engine().step();
|
|
|
|
net.peer(1).chain.engine().step();
|
2016-12-15 23:36:06 +01:00
|
|
|
let ci0 = net.peer(0).chain.chain_info();
|
|
|
|
let ci1 = net.peer(1).chain.chain_info();
|
|
|
|
assert_eq!(ci0.best_block_number, 3);
|
|
|
|
assert_eq!(ci1.best_block_number, 3);
|
|
|
|
assert!(ci0.best_block_hash != ci1.best_block_hash);
|
2017-03-03 22:32:22 +01:00
|
|
|
// Reorg to the chain with earlier view.
|
2016-12-12 17:24:48 +01:00
|
|
|
net.sync();
|
|
|
|
let ci0 = net.peer(0).chain.chain_info();
|
|
|
|
let ci1 = net.peer(1).chain.chain_info();
|
|
|
|
assert_eq!(ci0.best_block_number, 3);
|
|
|
|
assert_eq!(ci1.best_block_number, 3);
|
|
|
|
assert_eq!(ci0.best_block_hash, ci1.best_block_hash);
|
2017-03-03 22:32:22 +01:00
|
|
|
assert_eq!(ci1.best_block_hash, early_hash);
|
|
|
|
|
|
|
|
// Selfish miner
|
2018-04-13 17:34:27 +02:00
|
|
|
net.peer(0).miner.import_own_transaction(&*net.peer(0).chain, new_tx(s0.secret(), 3.into(), chain_id)).unwrap();
|
|
|
|
net.peer(1).miner.import_own_transaction(&*net.peer(1).chain, new_tx(s1.secret(), 3.into(), chain_id)).unwrap();
|
2017-03-03 22:32:22 +01:00
|
|
|
// Node 0 is an earlier primary.
|
|
|
|
net.peer(0).chain.engine().step();
|
|
|
|
assert_eq!(net.peer(0).chain.chain_info().best_block_number, 4);
|
|
|
|
net.peer(0).chain.engine().step();
|
|
|
|
net.peer(0).chain.engine().step();
|
|
|
|
net.peer(0).chain.engine().step();
|
|
|
|
assert_eq!(net.peer(0).chain.chain_info().best_block_number, 4);
|
|
|
|
// Node 1 makes 2 blocks, but is a later primary on the first one.
|
|
|
|
net.peer(1).chain.engine().step();
|
|
|
|
net.peer(1).chain.engine().step();
|
2018-04-13 17:34:27 +02:00
|
|
|
net.peer(1).miner.import_own_transaction(&*net.peer(1).chain, new_tx(s1.secret(), 4.into(), chain_id)).unwrap();
|
2017-03-03 22:32:22 +01:00
|
|
|
net.peer(1).chain.engine().step();
|
|
|
|
net.peer(1).chain.engine().step();
|
|
|
|
assert_eq!(net.peer(1).chain.chain_info().best_block_number, 5);
|
|
|
|
// Reorg to the longest chain one not ealier view one.
|
|
|
|
net.sync();
|
|
|
|
let ci0 = net.peer(0).chain.chain_info();
|
|
|
|
let ci1 = net.peer(1).chain.chain_info();
|
|
|
|
assert_eq!(ci0.best_block_number, 5);
|
|
|
|
assert_eq!(ci1.best_block_number, 5);
|
|
|
|
assert_eq!(ci0.best_block_hash, ci1.best_block_hash);
|
2016-12-06 19:23:15 +01:00
|
|
|
}
|