Reformat the source code
This commit is contained in:
@@ -14,251 +14,288 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::sync::Arc;
|
||||
use ethcore::client::{TestBlockChainClient, BlockChainClient, BlockId, EachBlockWith, ChainInfo, BlockInfo};
|
||||
use chain::{SyncState};
|
||||
use super::helpers::*;
|
||||
use {SyncConfig, WarpSync};
|
||||
use chain::SyncState;
|
||||
use ethcore::client::{
|
||||
BlockChainClient, BlockId, BlockInfo, ChainInfo, EachBlockWith, TestBlockChainClient,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
use SyncConfig;
|
||||
use WarpSync;
|
||||
|
||||
#[test]
|
||||
fn two_peers() {
|
||||
::env_logger::try_init().ok();
|
||||
let mut net = TestNet::new(3);
|
||||
net.peer(1).chain.add_blocks(1000, EachBlockWith::Uncle);
|
||||
net.peer(2).chain.add_blocks(1000, EachBlockWith::Uncle);
|
||||
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());
|
||||
::env_logger::try_init().ok();
|
||||
let mut net = TestNet::new(3);
|
||||
net.peer(1).chain.add_blocks(1000, EachBlockWith::Uncle);
|
||||
net.peer(2).chain.add_blocks(1000, EachBlockWith::Uncle);
|
||||
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()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn long_chain() {
|
||||
::env_logger::try_init().ok();
|
||||
let mut net = TestNet::new(2);
|
||||
net.peer(1).chain.add_blocks(50000, EachBlockWith::Nothing);
|
||||
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());
|
||||
::env_logger::try_init().ok();
|
||||
let mut net = TestNet::new(2);
|
||||
net.peer(1).chain.add_blocks(50000, EachBlockWith::Nothing);
|
||||
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()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn status_after_sync() {
|
||||
::env_logger::try_init().ok();
|
||||
let mut net = TestNet::new(3);
|
||||
net.peer(1).chain.add_blocks(1000, EachBlockWith::Uncle);
|
||||
net.peer(2).chain.add_blocks(1000, EachBlockWith::Uncle);
|
||||
net.sync();
|
||||
let status = net.peer(0).sync.read().status();
|
||||
assert_eq!(status.state, SyncState::Idle);
|
||||
::env_logger::try_init().ok();
|
||||
let mut net = TestNet::new(3);
|
||||
net.peer(1).chain.add_blocks(1000, EachBlockWith::Uncle);
|
||||
net.peer(2).chain.add_blocks(1000, EachBlockWith::Uncle);
|
||||
net.sync();
|
||||
let status = net.peer(0).sync.read().status();
|
||||
assert_eq!(status.state, SyncState::Idle);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn takes_few_steps() {
|
||||
let mut net = TestNet::new(3);
|
||||
net.peer(1).chain.add_blocks(100, EachBlockWith::Uncle);
|
||||
net.peer(2).chain.add_blocks(100, EachBlockWith::Uncle);
|
||||
let total_steps = net.sync();
|
||||
assert!(total_steps < 20);
|
||||
let mut net = TestNet::new(3);
|
||||
net.peer(1).chain.add_blocks(100, EachBlockWith::Uncle);
|
||||
net.peer(2).chain.add_blocks(100, EachBlockWith::Uncle);
|
||||
let total_steps = net.sync();
|
||||
assert!(total_steps < 20);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_blocks() {
|
||||
::env_logger::try_init().ok();
|
||||
let mut net = TestNet::new(3);
|
||||
for n in 0..200 {
|
||||
let with = if n % 2 == 0 { EachBlockWith::Nothing } else { EachBlockWith::Uncle };
|
||||
net.peer(1).chain.add_blocks(5, with.clone());
|
||||
net.peer(2).chain.add_blocks(5, with);
|
||||
}
|
||||
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());
|
||||
::env_logger::try_init().ok();
|
||||
let mut net = TestNet::new(3);
|
||||
for n in 0..200 {
|
||||
let with = if n % 2 == 0 {
|
||||
EachBlockWith::Nothing
|
||||
} else {
|
||||
EachBlockWith::Uncle
|
||||
};
|
||||
net.peer(1).chain.add_blocks(5, with.clone());
|
||||
net.peer(2).chain.add_blocks(5, with);
|
||||
}
|
||||
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()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn forked() {
|
||||
::env_logger::try_init().ok();
|
||||
let mut net = TestNet::new(3);
|
||||
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);
|
||||
// peer 1 has the best chain of 601 blocks
|
||||
let peer1_chain = net.peer(1).chain.numbers.read().clone();
|
||||
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);
|
||||
::env_logger::try_init().ok();
|
||||
let mut net = TestNet::new(3);
|
||||
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);
|
||||
// peer 1 has the best chain of 601 blocks
|
||||
let peer1_chain = net.peer(1).chain.numbers.read().clone();
|
||||
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);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn forked_with_misbehaving_peer() {
|
||||
::env_logger::try_init().ok();
|
||||
let mut net = TestNet::new(3);
|
||||
::env_logger::try_init().ok();
|
||||
let mut net = TestNet::new(3);
|
||||
|
||||
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));
|
||||
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);
|
||||
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));
|
||||
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);
|
||||
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn net_hard_fork() {
|
||||
::env_logger::try_init().ok();
|
||||
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())));
|
||||
net.peer(0).chain.add_blocks(100, EachBlockWith::Uncle);
|
||||
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())));
|
||||
net.peer(0).chain.add_blocks(100, EachBlockWith::Nothing);
|
||||
net.sync();
|
||||
assert_eq!(net.peer(1).chain.chain_info().best_block_number, 0);
|
||||
}
|
||||
::env_logger::try_init().ok();
|
||||
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())),
|
||||
);
|
||||
net.peer(0).chain.add_blocks(100, EachBlockWith::Uncle);
|
||||
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())),
|
||||
);
|
||||
net.peer(0).chain.add_blocks(100, EachBlockWith::Nothing);
|
||||
net.sync();
|
||||
assert_eq!(net.peer(1).chain.chain_info().best_block_number, 0);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn restart() {
|
||||
::env_logger::try_init().ok();
|
||||
let mut net = TestNet::new(3);
|
||||
net.peer(1).chain.add_blocks(1000, EachBlockWith::Uncle);
|
||||
net.peer(2).chain.add_blocks(1000, EachBlockWith::Uncle);
|
||||
::env_logger::try_init().ok();
|
||||
let mut net = TestNet::new(3);
|
||||
net.peer(1).chain.add_blocks(1000, EachBlockWith::Uncle);
|
||||
net.peer(2).chain.add_blocks(1000, EachBlockWith::Uncle);
|
||||
|
||||
net.sync();
|
||||
net.sync();
|
||||
|
||||
// make sure that sync has actually happened
|
||||
assert!(net.peer(0).chain.chain_info().best_block_number > 100);
|
||||
net.restart_peer(0);
|
||||
// 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);
|
||||
let status = net.peer(0).sync.read().status();
|
||||
assert_eq!(status.state, SyncState::Idle);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn status_empty() {
|
||||
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);
|
||||
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
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn status_packet() {
|
||||
let mut net = TestNet::new(2);
|
||||
net.peer(0).chain.add_blocks(100, EachBlockWith::Uncle);
|
||||
net.peer(1).chain.add_blocks(1, EachBlockWith::Uncle);
|
||||
let mut net = TestNet::new(2);
|
||||
net.peer(0).chain.add_blocks(100, EachBlockWith::Uncle);
|
||||
net.peer(1).chain.add_blocks(1, EachBlockWith::Uncle);
|
||||
|
||||
net.start();
|
||||
net.start();
|
||||
|
||||
net.sync_step_peer(0);
|
||||
net.sync_step_peer(0);
|
||||
|
||||
assert_eq!(1, net.peer(0).queue.read().len());
|
||||
assert_eq!(0x00, net.peer(0).queue.read()[0].packet_id);
|
||||
assert_eq!(1, net.peer(0).queue.read().len());
|
||||
assert_eq!(0x00, net.peer(0).queue.read()[0].packet_id);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn propagate_hashes() {
|
||||
let mut net = TestNet::new(6);
|
||||
net.peer(1).chain.add_blocks(10, EachBlockWith::Uncle);
|
||||
net.sync();
|
||||
let mut net = TestNet::new(6);
|
||||
net.peer(1).chain.add_blocks(10, EachBlockWith::Uncle);
|
||||
net.sync();
|
||||
|
||||
net.peer(0).chain.add_blocks(10, EachBlockWith::Uncle);
|
||||
net.sync();
|
||||
net.trigger_chain_new_blocks(0); //first event just sets the marker
|
||||
net.trigger_chain_new_blocks(0);
|
||||
net.peer(0).chain.add_blocks(10, EachBlockWith::Uncle);
|
||||
net.sync();
|
||||
net.trigger_chain_new_blocks(0); //first event just sets the marker
|
||||
net.trigger_chain_new_blocks(0);
|
||||
|
||||
// 5 peers with NewHahses, 4 with blocks
|
||||
assert_eq!(9, net.peer(0).queue.read().len());
|
||||
let mut hashes = 0;
|
||||
let mut blocks = 0;
|
||||
for i in 0..net.peer(0).queue.read().len() {
|
||||
if net.peer(0).queue.read()[i].packet_id == 0x1 {
|
||||
hashes += 1;
|
||||
}
|
||||
if net.peer(0).queue.read()[i].packet_id == 0x7 {
|
||||
blocks += 1;
|
||||
}
|
||||
}
|
||||
assert_eq!(blocks, 4);
|
||||
assert_eq!(hashes, 5);
|
||||
// 5 peers with NewHahses, 4 with blocks
|
||||
assert_eq!(9, net.peer(0).queue.read().len());
|
||||
let mut hashes = 0;
|
||||
let mut blocks = 0;
|
||||
for i in 0..net.peer(0).queue.read().len() {
|
||||
if net.peer(0).queue.read()[i].packet_id == 0x1 {
|
||||
hashes += 1;
|
||||
}
|
||||
if net.peer(0).queue.read()[i].packet_id == 0x7 {
|
||||
blocks += 1;
|
||||
}
|
||||
}
|
||||
assert_eq!(blocks, 4);
|
||||
assert_eq!(hashes, 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn propagate_blocks() {
|
||||
let mut net = TestNet::new(20);
|
||||
net.peer(1).chain.add_blocks(10, EachBlockWith::Uncle);
|
||||
net.sync();
|
||||
let mut net = TestNet::new(20);
|
||||
net.peer(1).chain.add_blocks(10, EachBlockWith::Uncle);
|
||||
net.sync();
|
||||
|
||||
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);
|
||||
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);
|
||||
|
||||
assert!(!net.peer(0).queue.read().is_empty());
|
||||
// NEW_BLOCK_PACKET
|
||||
let blocks = net.peer(0).queue.read().iter().filter(|p| p.packet_id == 0x7).count();
|
||||
assert!(blocks > 0);
|
||||
assert!(!net.peer(0).queue.read().is_empty());
|
||||
// NEW_BLOCK_PACKET
|
||||
let blocks = net
|
||||
.peer(0)
|
||||
.queue
|
||||
.read()
|
||||
.iter()
|
||||
.filter(|p| p.packet_id == 0x7)
|
||||
.count();
|
||||
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
|
||||
});
|
||||
net.sync_steps(20);
|
||||
::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
|
||||
});
|
||||
net.sync_steps(20);
|
||||
|
||||
// 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);
|
||||
// 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);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reject_on_broken_chain() {
|
||||
let mut net = TestNet::new(2);
|
||||
net.peer(1).chain.add_blocks(10, EachBlockWith::Nothing);
|
||||
net.peer(1).chain.corrupt_block_parent(6);
|
||||
net.sync_steps(20);
|
||||
let mut net = TestNet::new(2);
|
||||
net.peer(1).chain.add_blocks(10, EachBlockWith::Nothing);
|
||||
net.peer(1).chain.corrupt_block_parent(6);
|
||||
net.sync_steps(20);
|
||||
|
||||
assert_eq!(net.peer(0).chain.chain_info().best_block_number, 0);
|
||||
assert_eq!(net.peer(0).chain.chain_info().best_block_number, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn disconnect_on_unrelated_chain() {
|
||||
::env_logger::try_init().ok();
|
||||
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);
|
||||
net.sync();
|
||||
assert_eq!(net.disconnect_events, vec![(0, 0)]);
|
||||
::env_logger::try_init().ok();
|
||||
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);
|
||||
net.sync();
|
||||
assert_eq!(net.disconnect_events, vec![(0, 0)]);
|
||||
}
|
||||
|
||||
@@ -14,111 +14,161 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::sync::Arc;
|
||||
use hash::keccak;
|
||||
use ethereum_types::{U256, Address};
|
||||
use io::{IoHandler, IoChannel};
|
||||
use ethcore::client::{ChainInfo, ClientIoMessage};
|
||||
use ethcore::engines;
|
||||
use ethcore::spec::Spec;
|
||||
use ethcore::miner::{self, MinerService};
|
||||
use ethkey::{KeyPair, Secret};
|
||||
use types::transaction::{Action, PendingTransaction, Transaction};
|
||||
use super::helpers::*;
|
||||
use ethcore::{
|
||||
client::{ChainInfo, ClientIoMessage},
|
||||
engines,
|
||||
miner::{self, MinerService},
|
||||
spec::Spec,
|
||||
};
|
||||
use ethereum_types::{Address, U256};
|
||||
use ethkey::{KeyPair, Secret};
|
||||
use hash::keccak;
|
||||
use io::{IoChannel, IoHandler};
|
||||
use std::sync::Arc;
|
||||
use types::transaction::{Action, PendingTransaction, Transaction};
|
||||
use SyncConfig;
|
||||
|
||||
fn new_tx(secret: &Secret, nonce: U256, chain_id: u64) -> PendingTransaction {
|
||||
let signed = Transaction {
|
||||
nonce: nonce.into(),
|
||||
gas_price: 0.into(),
|
||||
gas: 21000.into(),
|
||||
action: Action::Call(Address::default()),
|
||||
value: 0.into(),
|
||||
data: Vec::new(),
|
||||
}.sign(secret, Some(chain_id));
|
||||
PendingTransaction::new(signed, None)
|
||||
let signed = Transaction {
|
||||
nonce: nonce.into(),
|
||||
gas_price: 0.into(),
|
||||
gas: 21000.into(),
|
||||
action: Action::Call(Address::default()),
|
||||
value: 0.into(),
|
||||
data: Vec::new(),
|
||||
}
|
||||
.sign(secret, Some(chain_id));
|
||||
PendingTransaction::new(signed, None)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn authority_round() {
|
||||
let s0 = KeyPair::from_secret_slice(&keccak("1")).unwrap();
|
||||
let s1 = KeyPair::from_secret_slice(&keccak("0")).unwrap();
|
||||
let s0 = KeyPair::from_secret_slice(&keccak("1")).unwrap();
|
||||
let s1 = KeyPair::from_secret_slice(&keccak("0")).unwrap();
|
||||
|
||||
let chain_id = Spec::new_test_round().chain_id();
|
||||
let mut net = TestNet::with_spec(2, SyncConfig::default(), Spec::new_test_round);
|
||||
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()));
|
||||
// Push transaction to both clients. Only one of them gets lucky to produce a block.
|
||||
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())));
|
||||
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 _);
|
||||
net.peer(0).chain.set_io_channel(IoChannel::to_handler(Arc::downgrade(&io_handler1)));
|
||||
net.peer(1).chain.set_io_channel(IoChannel::to_handler(Arc::downgrade(&io_handler0)));
|
||||
// exchange statuses
|
||||
net.sync();
|
||||
// Trigger block proposal
|
||||
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();
|
||||
// Sync a block
|
||||
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);
|
||||
let chain_id = Spec::new_test_round().chain_id();
|
||||
let mut net = TestNet::with_spec(2, SyncConfig::default(), Spec::new_test_round);
|
||||
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()));
|
||||
// Push transaction to both clients. Only one of them gets lucky to produce a block.
|
||||
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(),
|
||||
)));
|
||||
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 _);
|
||||
net.peer(0)
|
||||
.chain
|
||||
.set_io_channel(IoChannel::to_handler(Arc::downgrade(&io_handler1)));
|
||||
net.peer(1)
|
||||
.chain
|
||||
.set_io_channel(IoChannel::to_handler(Arc::downgrade(&io_handler0)));
|
||||
// exchange statuses
|
||||
net.sync();
|
||||
// Trigger block proposal
|
||||
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();
|
||||
// Sync a block
|
||||
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);
|
||||
|
||||
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();
|
||||
// Move to next proposer step.
|
||||
net.peer(0).chain.engine().step();
|
||||
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);
|
||||
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();
|
||||
// Move to next proposer step.
|
||||
net.peer(0).chain.engine().step();
|
||||
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);
|
||||
|
||||
// Fork the network with equal height.
|
||||
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();
|
||||
// Let both nodes build one block.
|
||||
net.peer(0).chain.engine().step();
|
||||
let early_hash = net.peer(0).chain.chain_info().best_block_hash;
|
||||
net.peer(1).chain.engine().step();
|
||||
net.peer(0).chain.engine().step();
|
||||
net.peer(1).chain.engine().step();
|
||||
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);
|
||||
// Reorg to the chain with earlier view.
|
||||
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);
|
||||
assert_eq!(ci1.best_block_hash, early_hash);
|
||||
// Fork the network with equal height.
|
||||
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();
|
||||
// Let both nodes build one block.
|
||||
net.peer(0).chain.engine().step();
|
||||
let early_hash = net.peer(0).chain.chain_info().best_block_hash;
|
||||
net.peer(1).chain.engine().step();
|
||||
net.peer(0).chain.engine().step();
|
||||
net.peer(1).chain.engine().step();
|
||||
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);
|
||||
// Reorg to the chain with earlier view.
|
||||
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);
|
||||
assert_eq!(ci1.best_block_hash, early_hash);
|
||||
|
||||
// Selfish miner
|
||||
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();
|
||||
// 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();
|
||||
net.peer(1).miner.import_own_transaction(&*net.peer(1).chain, new_tx(s1.secret(), 4.into(), chain_id)).unwrap();
|
||||
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);
|
||||
// Selfish miner
|
||||
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();
|
||||
// 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();
|
||||
net.peer(1)
|
||||
.miner
|
||||
.import_own_transaction(&*net.peer(1).chain, new_tx(s1.secret(), 4.into(), chain_id))
|
||||
.unwrap();
|
||||
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);
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -14,11 +14,11 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
pub mod helpers;
|
||||
pub mod snapshot;
|
||||
mod chain;
|
||||
mod consensus;
|
||||
pub mod helpers;
|
||||
mod private;
|
||||
pub mod snapshot;
|
||||
|
||||
#[cfg(feature = "ipc")]
|
||||
mod rpc;
|
||||
|
||||
@@ -14,141 +14,187 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::sync::Arc;
|
||||
use hash::keccak;
|
||||
use io::{IoHandler, IoChannel};
|
||||
use types::transaction::{Transaction, Action};
|
||||
use types::ids::BlockId;
|
||||
use ethcore::CreateContractAddress;
|
||||
use ethcore::client::{ClientIoMessage, BlockChainClient};
|
||||
use ethcore::executive::{contract_address};
|
||||
use ethcore::engines;
|
||||
use ethcore::miner::{self, MinerService};
|
||||
use ethcore::spec::Spec;
|
||||
use ethcore::test_helpers::{push_block_with_transactions};
|
||||
use ethcore_private_tx::{Provider, ProviderConfig, NoopEncryptor, Importer, SignedPrivateTransaction, StoringKeyProvider};
|
||||
use ethcore::{
|
||||
client::{BlockChainClient, ClientIoMessage},
|
||||
engines,
|
||||
executive::contract_address,
|
||||
miner::{self, MinerService},
|
||||
spec::Spec,
|
||||
test_helpers::push_block_with_transactions,
|
||||
CreateContractAddress,
|
||||
};
|
||||
use ethcore_private_tx::{
|
||||
Importer, NoopEncryptor, Provider, ProviderConfig, SignedPrivateTransaction, StoringKeyProvider,
|
||||
};
|
||||
use ethkey::KeyPair;
|
||||
use tests::helpers::{TestNet, TestIoHandler};
|
||||
use rustc_hex::FromHex;
|
||||
use hash::keccak;
|
||||
use io::{IoChannel, IoHandler};
|
||||
use rlp::Rlp;
|
||||
use rustc_hex::FromHex;
|
||||
use std::sync::Arc;
|
||||
use tests::helpers::{TestIoHandler, TestNet};
|
||||
use types::{
|
||||
ids::BlockId,
|
||||
transaction::{Action, Transaction},
|
||||
};
|
||||
use SyncConfig;
|
||||
|
||||
fn seal_spec() -> Spec {
|
||||
let spec_data = include_str!("../res/private_spec.json");
|
||||
Spec::load(&::std::env::temp_dir(), spec_data.as_bytes()).unwrap()
|
||||
let spec_data = include_str!("../res/private_spec.json");
|
||||
Spec::load(&::std::env::temp_dir(), spec_data.as_bytes()).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn send_private_transaction() {
|
||||
// Setup two clients
|
||||
let s0 = KeyPair::from_secret_slice(&keccak("1")).unwrap();
|
||||
let s1 = KeyPair::from_secret_slice(&keccak("0")).unwrap();
|
||||
// Setup two clients
|
||||
let s0 = KeyPair::from_secret_slice(&keccak("1")).unwrap();
|
||||
let s1 = KeyPair::from_secret_slice(&keccak("0")).unwrap();
|
||||
|
||||
let signer = Arc::new(ethcore_private_tx::KeyPairSigner(vec![s0.clone(), s1.clone()]));
|
||||
let signer = Arc::new(ethcore_private_tx::KeyPairSigner(vec![
|
||||
s0.clone(),
|
||||
s1.clone(),
|
||||
]));
|
||||
|
||||
let mut net = TestNet::with_spec(2, SyncConfig::default(), seal_spec);
|
||||
let client0 = net.peer(0).chain.clone();
|
||||
let client1 = net.peer(1).chain.clone();
|
||||
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()));
|
||||
let mut net = TestNet::with_spec(2, SyncConfig::default(), seal_spec);
|
||||
let client0 = net.peer(0).chain.clone();
|
||||
let client1 = net.peer(1).chain.clone();
|
||||
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()));
|
||||
|
||||
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())));
|
||||
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 _);
|
||||
net.peer(0).chain.set_io_channel(IoChannel::to_handler(Arc::downgrade(&io_handler0)));
|
||||
net.peer(1).chain.set_io_channel(IoChannel::to_handler(Arc::downgrade(&io_handler1)));
|
||||
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(),
|
||||
)));
|
||||
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 _);
|
||||
net.peer(0)
|
||||
.chain
|
||||
.set_io_channel(IoChannel::to_handler(Arc::downgrade(&io_handler0)));
|
||||
net.peer(1)
|
||||
.chain
|
||||
.set_io_channel(IoChannel::to_handler(Arc::downgrade(&io_handler1)));
|
||||
|
||||
let (address, _) = contract_address(CreateContractAddress::FromSenderAndNonce, &s0.address(), &0.into(), &[]);
|
||||
let chain_id = client0.signing_chain_id();
|
||||
let (address, _) = contract_address(
|
||||
CreateContractAddress::FromSenderAndNonce,
|
||||
&s0.address(),
|
||||
&0.into(),
|
||||
&[],
|
||||
);
|
||||
let chain_id = client0.signing_chain_id();
|
||||
|
||||
// Exhange statuses
|
||||
net.sync();
|
||||
// Exhange statuses
|
||||
net.sync();
|
||||
|
||||
// Setup private providers
|
||||
let validator_config = ProviderConfig{
|
||||
validator_accounts: vec![s1.address()],
|
||||
signer_account: None,
|
||||
};
|
||||
// Setup private providers
|
||||
let validator_config = ProviderConfig {
|
||||
validator_accounts: vec![s1.address()],
|
||||
signer_account: None,
|
||||
};
|
||||
|
||||
let signer_config = ProviderConfig{
|
||||
validator_accounts: Vec::new(),
|
||||
signer_account: Some(s0.address()),
|
||||
};
|
||||
let signer_config = ProviderConfig {
|
||||
validator_accounts: Vec::new(),
|
||||
signer_account: Some(s0.address()),
|
||||
};
|
||||
|
||||
let private_keys = Arc::new(StoringKeyProvider::default());
|
||||
let private_keys = Arc::new(StoringKeyProvider::default());
|
||||
|
||||
let pm0 = Arc::new(Provider::new(
|
||||
client0.clone(),
|
||||
net.peer(0).miner.clone(),
|
||||
signer.clone(),
|
||||
Box::new(NoopEncryptor::default()),
|
||||
signer_config,
|
||||
IoChannel::to_handler(Arc::downgrade(&io_handler0)),
|
||||
private_keys.clone(),
|
||||
));
|
||||
pm0.add_notify(net.peers[0].clone());
|
||||
let pm0 = Arc::new(Provider::new(
|
||||
client0.clone(),
|
||||
net.peer(0).miner.clone(),
|
||||
signer.clone(),
|
||||
Box::new(NoopEncryptor::default()),
|
||||
signer_config,
|
||||
IoChannel::to_handler(Arc::downgrade(&io_handler0)),
|
||||
private_keys.clone(),
|
||||
));
|
||||
pm0.add_notify(net.peers[0].clone());
|
||||
|
||||
let pm1 = Arc::new(Provider::new(
|
||||
client1.clone(),
|
||||
net.peer(1).miner.clone(),
|
||||
signer.clone(),
|
||||
Box::new(NoopEncryptor::default()),
|
||||
validator_config,
|
||||
IoChannel::to_handler(Arc::downgrade(&io_handler1)),
|
||||
private_keys.clone(),
|
||||
));
|
||||
pm1.add_notify(net.peers[1].clone());
|
||||
let pm1 = Arc::new(Provider::new(
|
||||
client1.clone(),
|
||||
net.peer(1).miner.clone(),
|
||||
signer.clone(),
|
||||
Box::new(NoopEncryptor::default()),
|
||||
validator_config,
|
||||
IoChannel::to_handler(Arc::downgrade(&io_handler1)),
|
||||
private_keys.clone(),
|
||||
));
|
||||
pm1.add_notify(net.peers[1].clone());
|
||||
|
||||
// Create and deploy contract
|
||||
let private_contract_test = "6060604052341561000f57600080fd5b60d88061001d6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680630c55699c146046578063bc64b76d14607457600080fd5b3415605057600080fd5b60566098565b60405180826000191660001916815260200191505060405180910390f35b3415607e57600080fd5b6096600480803560001916906020019091905050609e565b005b60005481565b8060008160001916905550505600a165627a7a723058206acbdf4b15ca4c2d43e1b1879b830451a34f1e9d02ff1f2f394d8d857e79d2080029".from_hex().unwrap();
|
||||
let mut private_create_tx = Transaction::default();
|
||||
private_create_tx.action = Action::Create;
|
||||
private_create_tx.data = private_contract_test;
|
||||
private_create_tx.gas = 200000.into();
|
||||
let private_create_tx_signed = private_create_tx.sign(&s0.secret(), None);
|
||||
let validators = vec![s1.address()];
|
||||
let (public_tx, _) = pm0.public_creation_transaction(BlockId::Latest, &private_create_tx_signed, &validators, 0.into()).unwrap();
|
||||
let public_tx = public_tx.sign(&s0.secret(), chain_id);
|
||||
// Create and deploy contract
|
||||
let private_contract_test = "6060604052341561000f57600080fd5b60d88061001d6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680630c55699c146046578063bc64b76d14607457600080fd5b3415605057600080fd5b60566098565b60405180826000191660001916815260200191505060405180910390f35b3415607e57600080fd5b6096600480803560001916906020019091905050609e565b005b60005481565b8060008160001916905550505600a165627a7a723058206acbdf4b15ca4c2d43e1b1879b830451a34f1e9d02ff1f2f394d8d857e79d2080029".from_hex().unwrap();
|
||||
let mut private_create_tx = Transaction::default();
|
||||
private_create_tx.action = Action::Create;
|
||||
private_create_tx.data = private_contract_test;
|
||||
private_create_tx.gas = 200000.into();
|
||||
let private_create_tx_signed = private_create_tx.sign(&s0.secret(), None);
|
||||
let validators = vec![s1.address()];
|
||||
let (public_tx, _) = pm0
|
||||
.public_creation_transaction(
|
||||
BlockId::Latest,
|
||||
&private_create_tx_signed,
|
||||
&validators,
|
||||
0.into(),
|
||||
)
|
||||
.unwrap();
|
||||
let public_tx = public_tx.sign(&s0.secret(), chain_id);
|
||||
|
||||
let public_tx_copy = public_tx.clone();
|
||||
push_block_with_transactions(&client0, &[public_tx]);
|
||||
push_block_with_transactions(&client1, &[public_tx_copy]);
|
||||
let public_tx_copy = public_tx.clone();
|
||||
push_block_with_transactions(&client0, &[public_tx]);
|
||||
push_block_with_transactions(&client1, &[public_tx_copy]);
|
||||
|
||||
net.sync();
|
||||
net.sync();
|
||||
|
||||
//Create private transaction for modifying state
|
||||
let mut private_tx = Transaction::default();
|
||||
private_tx.action = Action::Call(address.clone());
|
||||
private_tx.data = "bc64b76d2a00000000000000000000000000000000000000000000000000000000000000".from_hex().unwrap(); //setX(42)
|
||||
private_tx.gas = 120000.into();
|
||||
private_tx.nonce = 1.into();
|
||||
let private_tx = private_tx.sign(&s0.secret(), None);
|
||||
assert!(pm0.create_private_transaction(private_tx).is_ok());
|
||||
//Create private transaction for modifying state
|
||||
let mut private_tx = Transaction::default();
|
||||
private_tx.action = Action::Call(address.clone());
|
||||
private_tx.data = "bc64b76d2a00000000000000000000000000000000000000000000000000000000000000"
|
||||
.from_hex()
|
||||
.unwrap(); //setX(42)
|
||||
private_tx.gas = 120000.into();
|
||||
private_tx.nonce = 1.into();
|
||||
let private_tx = private_tx.sign(&s0.secret(), None);
|
||||
assert!(pm0.create_private_transaction(private_tx).is_ok());
|
||||
|
||||
//send private transaction message to validator
|
||||
net.sync();
|
||||
//send private transaction message to validator
|
||||
net.sync();
|
||||
|
||||
let validator_handler = net.peer(1).private_tx_handler.clone();
|
||||
let received_private_transactions = validator_handler.txs.lock().clone();
|
||||
assert_eq!(received_private_transactions.len(), 1);
|
||||
let validator_handler = net.peer(1).private_tx_handler.clone();
|
||||
let received_private_transactions = validator_handler.txs.lock().clone();
|
||||
assert_eq!(received_private_transactions.len(), 1);
|
||||
|
||||
//process received private transaction message
|
||||
let private_transaction = received_private_transactions[0].clone();
|
||||
assert!(pm1.import_private_transaction(&private_transaction).is_ok());
|
||||
//process received private transaction message
|
||||
let private_transaction = received_private_transactions[0].clone();
|
||||
assert!(pm1.import_private_transaction(&private_transaction).is_ok());
|
||||
|
||||
//send signed response
|
||||
net.sync();
|
||||
//send signed response
|
||||
net.sync();
|
||||
|
||||
let sender_handler = net.peer(0).private_tx_handler.clone();
|
||||
let received_signed_private_transactions = sender_handler.signed_txs.lock().clone();
|
||||
assert_eq!(received_signed_private_transactions.len(), 1);
|
||||
let sender_handler = net.peer(0).private_tx_handler.clone();
|
||||
let received_signed_private_transactions = sender_handler.signed_txs.lock().clone();
|
||||
assert_eq!(received_signed_private_transactions.len(), 1);
|
||||
|
||||
//process signed response
|
||||
let signed_private_transaction = received_signed_private_transactions[0].clone();
|
||||
assert!(pm0.import_signed_private_transaction(&signed_private_transaction).is_ok());
|
||||
let signature: SignedPrivateTransaction = Rlp::new(&signed_private_transaction).as_val().unwrap();
|
||||
assert!(pm0.process_signature(&signature).is_ok());
|
||||
let local_transactions = net.peer(0).miner.local_transactions();
|
||||
assert_eq!(local_transactions.len(), 1);
|
||||
//process signed response
|
||||
let signed_private_transaction = received_signed_private_transactions[0].clone();
|
||||
assert!(pm0
|
||||
.import_signed_private_transaction(&signed_private_transaction)
|
||||
.is_ok());
|
||||
let signature: SignedPrivateTransaction =
|
||||
Rlp::new(&signed_private_transaction).as_val().unwrap();
|
||||
assert!(pm0.process_signature(&signature).is_ok());
|
||||
let local_transactions = net.peer(0).miner.local_transactions();
|
||||
assert_eq!(local_transactions.len(), 1);
|
||||
}
|
||||
|
||||
@@ -15,15 +15,15 @@
|
||||
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use super::super::NetworkConfiguration;
|
||||
use ipc::binary::{deserialize, serialize};
|
||||
use network::NetworkConfiguration as BasicNetworkConfiguration;
|
||||
use std::convert::From;
|
||||
use ipc::binary::{serialize, deserialize};
|
||||
|
||||
#[test]
|
||||
fn network_settings_serialize() {
|
||||
let net_cfg = NetworkConfiguration::from(BasicNetworkConfiguration::new_local());
|
||||
let serialized = serialize(&net_cfg).unwrap();
|
||||
let deserialized = deserialize::<NetworkConfiguration>(&serialized).unwrap();
|
||||
let net_cfg = NetworkConfiguration::from(BasicNetworkConfiguration::new_local());
|
||||
let serialized = serialize(&net_cfg).unwrap();
|
||||
let deserialized = deserialize::<NetworkConfiguration>(&serialized).unwrap();
|
||||
|
||||
assert_eq!(net_cfg.udp_port, deserialized.udp_port);
|
||||
assert_eq!(net_cfg.udp_port, deserialized.udp_port);
|
||||
}
|
||||
|
||||
@@ -14,145 +14,203 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use hash::keccak;
|
||||
use ethereum_types::H256;
|
||||
use parking_lot::Mutex;
|
||||
use bytes::Bytes;
|
||||
use ethcore::snapshot::{SnapshotService, ManifestData, RestorationStatus};
|
||||
use ethcore::client::EachBlockWith;
|
||||
use types::BlockNumber;
|
||||
use super::helpers::*;
|
||||
use {SyncConfig, WarpSync};
|
||||
use bytes::Bytes;
|
||||
use ethcore::{
|
||||
client::EachBlockWith,
|
||||
snapshot::{ManifestData, RestorationStatus, SnapshotService},
|
||||
};
|
||||
use ethereum_types::H256;
|
||||
use hash::keccak;
|
||||
use parking_lot::Mutex;
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
use types::BlockNumber;
|
||||
use SyncConfig;
|
||||
use WarpSync;
|
||||
|
||||
pub struct TestSnapshotService {
|
||||
manifest: Option<ManifestData>,
|
||||
chunks: HashMap<H256, Bytes>,
|
||||
manifest: Option<ManifestData>,
|
||||
chunks: HashMap<H256, Bytes>,
|
||||
|
||||
restoration_manifest: Mutex<Option<ManifestData>>,
|
||||
state_restoration_chunks: Mutex<HashMap<H256, Bytes>>,
|
||||
block_restoration_chunks: Mutex<HashMap<H256, Bytes>>,
|
||||
restoration_manifest: Mutex<Option<ManifestData>>,
|
||||
state_restoration_chunks: Mutex<HashMap<H256, Bytes>>,
|
||||
block_restoration_chunks: Mutex<HashMap<H256, Bytes>>,
|
||||
}
|
||||
|
||||
impl TestSnapshotService {
|
||||
pub fn new() -> TestSnapshotService {
|
||||
TestSnapshotService {
|
||||
manifest: None,
|
||||
chunks: HashMap::new(),
|
||||
restoration_manifest: Mutex::new(None),
|
||||
state_restoration_chunks: Mutex::new(HashMap::new()),
|
||||
block_restoration_chunks: Mutex::new(HashMap::new()),
|
||||
}
|
||||
}
|
||||
pub fn new() -> TestSnapshotService {
|
||||
TestSnapshotService {
|
||||
manifest: None,
|
||||
chunks: HashMap::new(),
|
||||
restoration_manifest: Mutex::new(None),
|
||||
state_restoration_chunks: Mutex::new(HashMap::new()),
|
||||
block_restoration_chunks: Mutex::new(HashMap::new()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_with_snapshot(num_chunks: usize, block_hash: H256, block_number: BlockNumber) -> TestSnapshotService {
|
||||
let num_state_chunks = num_chunks / 2;
|
||||
let num_block_chunks = num_chunks - num_state_chunks;
|
||||
let state_chunks: Vec<Bytes> = (0..num_state_chunks).map(|_| H256::random().to_vec()).collect();
|
||||
let block_chunks: Vec<Bytes> = (0..num_block_chunks).map(|_| H256::random().to_vec()).collect();
|
||||
let manifest = ManifestData {
|
||||
version: 2,
|
||||
state_hashes: state_chunks.iter().map(|data| keccak(data)).collect(),
|
||||
block_hashes: block_chunks.iter().map(|data| keccak(data)).collect(),
|
||||
state_root: H256::new(),
|
||||
block_number: block_number,
|
||||
block_hash: block_hash,
|
||||
};
|
||||
let mut chunks: HashMap<H256, Bytes> = state_chunks.into_iter().map(|data| (keccak(&data), data)).collect();
|
||||
chunks.extend(block_chunks.into_iter().map(|data| (keccak(&data), data)));
|
||||
TestSnapshotService {
|
||||
manifest: Some(manifest),
|
||||
chunks: chunks,
|
||||
restoration_manifest: Mutex::new(None),
|
||||
state_restoration_chunks: Mutex::new(HashMap::new()),
|
||||
block_restoration_chunks: Mutex::new(HashMap::new()),
|
||||
}
|
||||
}
|
||||
pub fn new_with_snapshot(
|
||||
num_chunks: usize,
|
||||
block_hash: H256,
|
||||
block_number: BlockNumber,
|
||||
) -> TestSnapshotService {
|
||||
let num_state_chunks = num_chunks / 2;
|
||||
let num_block_chunks = num_chunks - num_state_chunks;
|
||||
let state_chunks: Vec<Bytes> = (0..num_state_chunks)
|
||||
.map(|_| H256::random().to_vec())
|
||||
.collect();
|
||||
let block_chunks: Vec<Bytes> = (0..num_block_chunks)
|
||||
.map(|_| H256::random().to_vec())
|
||||
.collect();
|
||||
let manifest = ManifestData {
|
||||
version: 2,
|
||||
state_hashes: state_chunks.iter().map(|data| keccak(data)).collect(),
|
||||
block_hashes: block_chunks.iter().map(|data| keccak(data)).collect(),
|
||||
state_root: H256::new(),
|
||||
block_number: block_number,
|
||||
block_hash: block_hash,
|
||||
};
|
||||
let mut chunks: HashMap<H256, Bytes> = state_chunks
|
||||
.into_iter()
|
||||
.map(|data| (keccak(&data), data))
|
||||
.collect();
|
||||
chunks.extend(block_chunks.into_iter().map(|data| (keccak(&data), data)));
|
||||
TestSnapshotService {
|
||||
manifest: Some(manifest),
|
||||
chunks: chunks,
|
||||
restoration_manifest: Mutex::new(None),
|
||||
state_restoration_chunks: Mutex::new(HashMap::new()),
|
||||
block_restoration_chunks: Mutex::new(HashMap::new()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SnapshotService for TestSnapshotService {
|
||||
fn manifest(&self) -> Option<ManifestData> {
|
||||
self.manifest.as_ref().cloned()
|
||||
}
|
||||
fn manifest(&self) -> Option<ManifestData> {
|
||||
self.manifest.as_ref().cloned()
|
||||
}
|
||||
|
||||
fn supported_versions(&self) -> Option<(u64, u64)> {
|
||||
Some((1, 2))
|
||||
}
|
||||
fn supported_versions(&self) -> Option<(u64, u64)> {
|
||||
Some((1, 2))
|
||||
}
|
||||
|
||||
fn completed_chunks(&self) -> Option<Vec<H256>> {
|
||||
Some(vec![])
|
||||
}
|
||||
fn completed_chunks(&self) -> Option<Vec<H256>> {
|
||||
Some(vec![])
|
||||
}
|
||||
|
||||
fn chunk(&self, hash: H256) -> Option<Bytes> {
|
||||
self.chunks.get(&hash).cloned()
|
||||
}
|
||||
fn chunk(&self, hash: H256) -> Option<Bytes> {
|
||||
self.chunks.get(&hash).cloned()
|
||||
}
|
||||
|
||||
fn status(&self) -> RestorationStatus {
|
||||
match *self.restoration_manifest.lock() {
|
||||
Some(ref manifest) if self.state_restoration_chunks.lock().len() == manifest.state_hashes.len() &&
|
||||
self.block_restoration_chunks.lock().len() == manifest.block_hashes.len() => RestorationStatus::Inactive,
|
||||
Some(ref manifest) => RestorationStatus::Ongoing {
|
||||
state_chunks: manifest.state_hashes.len() as u32,
|
||||
block_chunks: manifest.block_hashes.len() as u32,
|
||||
state_chunks_done: self.state_restoration_chunks.lock().len() as u32,
|
||||
block_chunks_done: self.block_restoration_chunks.lock().len() as u32,
|
||||
},
|
||||
None => RestorationStatus::Inactive,
|
||||
}
|
||||
}
|
||||
fn status(&self) -> RestorationStatus {
|
||||
match *self.restoration_manifest.lock() {
|
||||
Some(ref manifest)
|
||||
if self.state_restoration_chunks.lock().len() == manifest.state_hashes.len()
|
||||
&& self.block_restoration_chunks.lock().len()
|
||||
== manifest.block_hashes.len() =>
|
||||
{
|
||||
RestorationStatus::Inactive
|
||||
}
|
||||
Some(ref manifest) => RestorationStatus::Ongoing {
|
||||
state_chunks: manifest.state_hashes.len() as u32,
|
||||
block_chunks: manifest.block_hashes.len() as u32,
|
||||
state_chunks_done: self.state_restoration_chunks.lock().len() as u32,
|
||||
block_chunks_done: self.block_restoration_chunks.lock().len() as u32,
|
||||
},
|
||||
None => RestorationStatus::Inactive,
|
||||
}
|
||||
}
|
||||
|
||||
fn begin_restore(&self, manifest: ManifestData) {
|
||||
let mut restoration_manifest = self.restoration_manifest.lock();
|
||||
fn begin_restore(&self, manifest: ManifestData) {
|
||||
let mut restoration_manifest = self.restoration_manifest.lock();
|
||||
|
||||
if let Some(ref c_manifest) = *restoration_manifest {
|
||||
if c_manifest.state_root == manifest.state_root {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if let Some(ref c_manifest) = *restoration_manifest {
|
||||
if c_manifest.state_root == manifest.state_root {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
*restoration_manifest = Some(manifest);
|
||||
self.state_restoration_chunks.lock().clear();
|
||||
self.block_restoration_chunks.lock().clear();
|
||||
}
|
||||
*restoration_manifest = Some(manifest);
|
||||
self.state_restoration_chunks.lock().clear();
|
||||
self.block_restoration_chunks.lock().clear();
|
||||
}
|
||||
|
||||
fn abort_restore(&self) {
|
||||
*self.restoration_manifest.lock() = None;
|
||||
self.state_restoration_chunks.lock().clear();
|
||||
self.block_restoration_chunks.lock().clear();
|
||||
}
|
||||
fn abort_restore(&self) {
|
||||
*self.restoration_manifest.lock() = None;
|
||||
self.state_restoration_chunks.lock().clear();
|
||||
self.block_restoration_chunks.lock().clear();
|
||||
}
|
||||
|
||||
fn abort_snapshot(&self) {}
|
||||
fn abort_snapshot(&self) {}
|
||||
|
||||
fn restore_state_chunk(&self, hash: H256, chunk: Bytes) {
|
||||
if self.restoration_manifest.lock().as_ref().map_or(false, |m| m.state_hashes.iter().any(|h| h == &hash)) {
|
||||
self.state_restoration_chunks.lock().insert(hash, chunk);
|
||||
}
|
||||
}
|
||||
fn restore_state_chunk(&self, hash: H256, chunk: Bytes) {
|
||||
if self
|
||||
.restoration_manifest
|
||||
.lock()
|
||||
.as_ref()
|
||||
.map_or(false, |m| m.state_hashes.iter().any(|h| h == &hash))
|
||||
{
|
||||
self.state_restoration_chunks.lock().insert(hash, chunk);
|
||||
}
|
||||
}
|
||||
|
||||
fn restore_block_chunk(&self, hash: H256, chunk: Bytes) {
|
||||
if self.restoration_manifest.lock().as_ref().map_or(false, |m| m.block_hashes.iter().any(|h| h == &hash)) {
|
||||
self.block_restoration_chunks.lock().insert(hash, chunk);
|
||||
}
|
||||
}
|
||||
fn restore_block_chunk(&self, hash: H256, chunk: Bytes) {
|
||||
if self
|
||||
.restoration_manifest
|
||||
.lock()
|
||||
.as_ref()
|
||||
.map_or(false, |m| m.block_hashes.iter().any(|h| h == &hash))
|
||||
{
|
||||
self.block_restoration_chunks.lock().insert(hash, chunk);
|
||||
}
|
||||
}
|
||||
|
||||
fn shutdown(&self) {
|
||||
self.abort_restore();
|
||||
}
|
||||
fn shutdown(&self) {
|
||||
self.abort_restore();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn snapshot_sync() {
|
||||
::env_logger::try_init().ok();
|
||||
let mut config = SyncConfig::default();
|
||||
config.warp_sync = WarpSync::Enabled;
|
||||
let mut net = TestNet::new_with_config(5, config);
|
||||
let snapshot_service = Arc::new(TestSnapshotService::new_with_snapshot(16, H256::new(), 500000));
|
||||
for i in 0..4 {
|
||||
net.peer_mut(i).snapshot_service = snapshot_service.clone();
|
||||
net.peer(i).chain.add_blocks(1, EachBlockWith::Nothing);
|
||||
}
|
||||
net.sync_steps(50);
|
||||
assert_eq!(net.peer(4).snapshot_service.state_restoration_chunks.lock().len(), net.peer(0).snapshot_service.manifest.as_ref().unwrap().state_hashes.len());
|
||||
assert_eq!(net.peer(4).snapshot_service.block_restoration_chunks.lock().len(), net.peer(0).snapshot_service.manifest.as_ref().unwrap().block_hashes.len());
|
||||
::env_logger::try_init().ok();
|
||||
let mut config = SyncConfig::default();
|
||||
config.warp_sync = WarpSync::Enabled;
|
||||
let mut net = TestNet::new_with_config(5, config);
|
||||
let snapshot_service = Arc::new(TestSnapshotService::new_with_snapshot(
|
||||
16,
|
||||
H256::new(),
|
||||
500000,
|
||||
));
|
||||
for i in 0..4 {
|
||||
net.peer_mut(i).snapshot_service = snapshot_service.clone();
|
||||
net.peer(i).chain.add_blocks(1, EachBlockWith::Nothing);
|
||||
}
|
||||
net.sync_steps(50);
|
||||
assert_eq!(
|
||||
net.peer(4)
|
||||
.snapshot_service
|
||||
.state_restoration_chunks
|
||||
.lock()
|
||||
.len(),
|
||||
net.peer(0)
|
||||
.snapshot_service
|
||||
.manifest
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.state_hashes
|
||||
.len()
|
||||
);
|
||||
assert_eq!(
|
||||
net.peer(4)
|
||||
.snapshot_service
|
||||
.block_restoration_chunks
|
||||
.lock()
|
||||
.len(),
|
||||
net.peer(0)
|
||||
.snapshot_service
|
||||
.manifest
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.block_hashes
|
||||
.len()
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user