From 4e62b5201087eb5c191ddbf1eaf581f12a3d4b96 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Wed, 27 Jan 2016 04:24:32 +0400 Subject: [PATCH 01/11] blockchain cache tests --- src/blockchain.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/blockchain.rs b/src/blockchain.rs index 3bd31688a..16e75b08d 100644 --- a/src/blockchain.rs +++ b/src/blockchain.rs @@ -628,6 +628,25 @@ mod tests { use util::hash::*; use blockchain::*; + fn get_populated_bc() -> BlockChain { + let genesis = "f901fcf901f7a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a07dba07d6b448a186e9612e5f737d1c909dce473e53199901a302c00646d523c1a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000080832fefd8808454c98c8142a059262c330941f3fe2a34d16d6e3c7b30d2ceb37c6a0e9a994c494ee1a61d2410885aa4c8bf8e56e264c0c0".from_hex().unwrap(); + let b1 = "f90261f901f9a05716670833ec874362d65fea27a7cd35af5897d275b31a44944113111e4e96d2a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0cb52de543653d86ccd13ba3ddf8b052525b04231c6884a4db3188a184681d878a0e78628dd45a1f8dc495594d83b76c588a3ee67463260f8b7d4a42f574aeab29aa0e9244cf7503b79c03d3a099e07a80d2dbc77bb0b502d8a89d51ac0d68dd31313b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000001832fefd882520884562791e580a051b3ecba4e3f2b49c11d42dd0851ec514b1be3138080f72a2b6e83868275d98f8877671f479c414b47f862f86080018304cb2f94095e7baea6a6c7c4c2dfeb977efac326af552d870a801ca09e2709d7ec9bbe6b1bbbf0b2088828d14cd5e8642a1fee22dc74bfa89761a7f9a04bd8813dee4be989accdb708b1c2e325a7e9c695a8024e30e89d6c644e424747c0".from_hex().unwrap(); + let b2 = "f902ccf901f9a0437e51676ff10756fcfee5edd9159fa41dbcb1b2c592850450371cbecd54ee4fa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0c70a5dc56146e5ef025e4e5726a6373c6f12fd2f6784093a19ead0a7d17fb292a040645cbce4fd399e7bb9160b4c30c40d7ee616a030d4e18ef0ed3b02bdb65911a086e608555f63628417032a011d107b36427af37d153f0da02ce3f90fdd5e8c08b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302004002832fefd882c0e384562791e880a0e3cc39ff775cc0a32f175995b92e84b729e5c9a3563ff899e3555b908bc21d75887c3cde283f4846a6f8cdf8cb01018304cb2f8080b87e6060604052606e8060106000396000f360606040526000357c010000000000000000000000000000000000000000000000000000000090048063c0406226146037576035565b005b60406004506056565b6040518082815260200191505060405180910390f35b6000600560006000508190555060059050606b565b90561ba05258615c63503c0a600d6994b12ea5750d45b3c69668e2a371b4fbfb9eeff6b8a0a11be762bc90491231274a2945be35a43f23c27775b1ff24dd521702fe15f73ec0".from_hex().unwrap(); + let b3a = "f90261f901f9a036fde1253128666fcb95a5956da14a73489e988bb72738717ec1d31e1cee781aa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a05fb2b4bfdef7b314451cb138a534d225c922fc0e5fbe25e451142732c3e25c25a09dc4b1357c0b7b8108f8a098f4f9a1a274957bc9ebc22a9ae67ae81739e5b19ca007c6fdfa8eea7e86b81f5b0fc0f78f90cc19f4aa60d323151e0cac660199e9a1b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302008003832fefd882524d84562791eb80a074861666bd346c025889745c793b91ab9cd1e2ca19b5cf3c50d04d135b0a4d2b8809fe9587ea4cdc04f862f86002018304cb2f94ec0e71ad0a90ffe1909d27dac207f7680abba42d01801ba06fd84874d36d5de9e8e48978c03619b53a96b7ae0a4cd1ac118f103098b44801a00572596974dd7df4f9f69bd7456585618c568d8434ef6453391b89281ce12ae1c0".from_hex().unwrap(); + let b3b = "f90265f901f9a036fde1253128666fcb95a5956da14a73489e988bb72738717ec1d31e1cee781aa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0ab87dc338bfd6f662b1cd90bc0c9e40a1b2146a095312393c9e13ce3a5008b09a0e609b7a7d4b8a2403ec1268627ecd98783627246e8f1b26addb3ff504f76a054a0592fabf92476512952db3a69a2481a42912e668a1ee28c4c322e703bb665f8beb90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302008003832fefd882a1f084562791ee80a0fe7098fa7e4ac5d637eea81fb23f8f78346826dbab430068dd9a249d0afa99818853e1a6b201ae3545f866f86402018304cb2f94ec0e71ad0a90ffe1909d27dac207f7680abba42d0284c04062261ca06edc9ce8e7da4cc34067beb325dcad59e5655a164a5100a50bc3eb681b12c716a0abf9053d5de65b1be81fe50d327b84de685efbeecea34e7b747180a6c6023e44c0".from_hex().unwrap(); + + let mut dir = env::temp_dir(); + dir.push(H32::random().hex()); + + let bc = BlockChain::new(&genesis, &dir); + bc.insert_block(&b1); + bc.insert_block(&b2); + bc.insert_block(&b3a); + bc.insert_block(&b3b); + + bc + } + #[test] fn valid_tests_extra32() { let genesis = "f901fcf901f7a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0925002c3260b44e44c3edebad1cc442142b03020209df1ab8bb86752edbd2cd7a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000080832fefd8808454c98c8142a0363659b251bf8b819179874c8cce7b9b983d7f3704cbb58a3b334431f7032871889032d09c281e1236c0c0".from_hex().unwrap(); @@ -773,4 +792,11 @@ mod tests { assert_eq!(bc.best_block_hash(), b1_hash); } } + + #[test] + fn blocks_get_garbage_collected() { + let bc = get_populated_bc(); + bc.squeeze_to_fit(CacheSize { blocks: 0, block_logs: 0, transaction_addresses: 0, block_details: 0, blocks_blooms: 0 } ); + assert_eq!(bc.cache_size().blocks, 0); + } } From 8d8782056a3e5d4166f55c1f007767ca26e9f1b5 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Wed, 27 Jan 2016 13:22:51 +0400 Subject: [PATCH 02/11] failing test for sqeeze to fit code --- src/blockchain.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/blockchain.rs b/src/blockchain.rs index dcc348440..3f55f3ea1 100644 --- a/src/blockchain.rs +++ b/src/blockchain.rs @@ -803,5 +803,8 @@ mod tests { let bc = get_populated_bc(); bc.squeeze_to_fit(CacheSize { blocks: 0, block_logs: 0, transaction_addresses: 0, block_details: 0, blocks_blooms: 0 } ); assert_eq!(bc.cache_size().blocks, 0); + assert_eq!(bc.cache_size().block_details, 0); + assert_eq!(bc.cache_size().block_logs, 0); + assert_eq!(bc.cache_size().blocks_blooms, 0); } } From b4263c1755a63d55f2cd60e3484458728f778f02 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Wed, 27 Jan 2016 13:38:47 +0400 Subject: [PATCH 03/11] squeeze/garbage --- src/blockchain.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/blockchain.rs b/src/blockchain.rs index 3f55f3ea1..40abb1c8d 100644 --- a/src/blockchain.rs +++ b/src/blockchain.rs @@ -799,12 +799,20 @@ mod tests { } #[test] - fn blocks_get_garbage_collected() { + fn can_be_squeezed() { let bc = get_populated_bc(); bc.squeeze_to_fit(CacheSize { blocks: 0, block_logs: 0, transaction_addresses: 0, block_details: 0, blocks_blooms: 0 } ); - assert_eq!(bc.cache_size().blocks, 0); - assert_eq!(bc.cache_size().block_details, 0); + assert_eq!(bc.cache_size().blocks, 1624); + assert_eq!(bc.cache_size().block_details, 3712); assert_eq!(bc.cache_size().block_logs, 0); assert_eq!(bc.cache_size().blocks_blooms, 0); } + + #[test] + fn can_collect_garbage() { + let bc = get_populated_bc(); + bc.collect_garbage(false); + assert_eq!(bc.cache_size().blocks, 1624); + + } } From d2229f519ddfd8ec0cfdcd69b6f2fe9f73647dc5 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Wed, 27 Jan 2016 14:50:39 +0400 Subject: [PATCH 04/11] test helpers initial --- src/tests/helpers.rs | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/tests/helpers.rs diff --git a/src/tests/helpers.rs b/src/tests/helpers.rs new file mode 100644 index 000000000..e69de29bb From dddbf589451a61daeecd122d4e83e6ae3b9e3165 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Wed, 27 Jan 2016 14:50:48 +0400 Subject: [PATCH 05/11] test helpers initial --- src/tests/client.rs | 23 +++++-------------- src/tests/helpers.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++ src/tests/mod.rs | 1 + 3 files changed, 61 insertions(+), 17 deletions(-) diff --git a/src/tests/client.rs b/src/tests/client.rs index eb58699b4..57ab6e577 100644 --- a/src/tests/client.rs +++ b/src/tests/client.rs @@ -3,18 +3,7 @@ use std::env; use super::test_common::*; use std::path::PathBuf; use spec::*; - -#[cfg(test)] -fn get_random_temp_dir() -> PathBuf { - let mut dir = env::temp_dir(); - dir.push(H32::random().hex()); - dir -} - -#[cfg(test)] -fn get_test_spec() -> Spec { - Spec::new_test() -} +use super::helpers::*; #[cfg(test)] fn get_good_dummy_block() -> Bytes { @@ -57,7 +46,7 @@ fn create_test_block(header: &Header) -> Bytes { #[cfg(test)] fn get_test_client_with_blocks(blocks: Vec) -> Arc { - let client = Client::new(get_test_spec(), &get_random_temp_dir(), IoChannel::disconnected()).unwrap(); + let client = Client::new(get_test_spec(), &get_random_path(), IoChannel::disconnected()).unwrap(); for block in &blocks { if let Err(_) = client.import_block(block.clone()) { panic!("panic importing block which is well-formed"); @@ -71,20 +60,20 @@ fn get_test_client_with_blocks(blocks: Vec) -> Arc { #[test] fn created() { - let client_result = Client::new(get_test_spec(), &get_random_temp_dir(), IoChannel::disconnected()); + let client_result = Client::new(get_test_spec(), &get_random_path(), IoChannel::disconnected()); assert!(client_result.is_ok()); } #[test] fn imports_from_empty() { - let client = Client::new(get_test_spec(), &get_random_temp_dir(), IoChannel::disconnected()).unwrap(); + let client = Client::new(get_test_spec(), &get_random_path(), IoChannel::disconnected()).unwrap(); client.import_verified_blocks(&IoChannel::disconnected()); client.flush_queue(); } #[test] fn imports_good_block() { - let client = Client::new(get_test_spec(), &get_random_temp_dir(), IoChannel::disconnected()).unwrap(); + let client = Client::new(get_test_spec(), &get_random_path(), IoChannel::disconnected()).unwrap(); let good_block = get_good_dummy_block(); if let Err(_) = client.import_block(good_block) { panic!("error importing block being good by definition"); @@ -98,7 +87,7 @@ fn imports_good_block() { #[test] fn query_none_block() { - let client = Client::new(get_test_spec(), &get_random_temp_dir(), IoChannel::disconnected()).unwrap(); + let client = Client::new(get_test_spec(), &get_random_path(), IoChannel::disconnected()).unwrap(); let non_existant = client.block_header_at(188); assert!(non_existant.is_none()); diff --git a/src/tests/helpers.rs b/src/tests/helpers.rs index e69de29bb..4cbd36692 100644 --- a/src/tests/helpers.rs +++ b/src/tests/helpers.rs @@ -0,0 +1,54 @@ +use client::{BlockChainClient,Client}; +use std::env; +use super::test_common::*; +use std::path::PathBuf; +use spec::*; +use std::fs::{create_dir_all}; + +#[cfg(test)] +const FIXED_TEMP_DIR_NAME: &'static str = "parity-temp"; + + +#[cfg(test)] +pub fn get_tests_temp_dir() -> PathBuf { + let mut dir = env::temp_dir(); + dir.push(FIXED_TEMP_DIR_NAME); + if let Err(_) = create_dir_all(&dir) { + panic!("failed to create test dir!"); + } + dir +} + +#[cfg(test)] +pub fn get_random_path() -> PathBuf { + let mut dir = get_tests_temp_dir(); + dir.push(H32::random().hex()); + dir +} + + +#[cfg(test)] +pub fn get_test_spec() -> Spec { + Spec::new_test() +} + +#[cfg(test)] +pub fn generate_dummy_client(block_number: usize) { + let client = Client::new(get_test_spec(), &get_random_path(), IoChannel::disconnected()).unwrap(); + + let mut rolling_hash = test_engine.spec().genesis_header().hash(); + let mut rolling_state = test_engine.spec().genesis_header().state_root; + let mut rolling_block_number = 1; + + for _ in 0..block_number { + let mut header = Header::new(); + + header.gas_limit = decode(test_engine.spec().engine_params.get("minGasLimit").unwrap()); + header.difficulty = decode(test_engine.spec().engine_params.get("minimumDifficulty").unwrap()); + header.timestamp = 40; + header.number = rolling_block_number; + header.parent_hash = test_engine.spec().genesis_header().hash(); + header.state_root = test_engine.spec().genesis_header().state_root; + } + +} \ No newline at end of file diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 290c2e293..17da72d77 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -6,3 +6,4 @@ mod executive; mod state; mod client; mod chain; +mod helpers; \ No newline at end of file From 38164d6b21323b78f49ddfb1201fe5084a1bda41 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Wed, 27 Jan 2016 16:23:24 +0400 Subject: [PATCH 06/11] extra tests --- src/blockchain.rs | 36 ------------------------------------ src/client.rs | 12 ++++++++++++ src/tests/client.rs | 37 ++++++++++++++++++++++++++----------- src/tests/helpers.rs | 37 ++++++++++++++++++++++++++++++++----- 4 files changed, 70 insertions(+), 52 deletions(-) diff --git a/src/blockchain.rs b/src/blockchain.rs index 40abb1c8d..a0b554bcc 100644 --- a/src/blockchain.rs +++ b/src/blockchain.rs @@ -633,25 +633,6 @@ mod tests { use util::hash::*; use blockchain::*; - fn get_populated_bc() -> BlockChain { - let genesis = "f901fcf901f7a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a07dba07d6b448a186e9612e5f737d1c909dce473e53199901a302c00646d523c1a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000080832fefd8808454c98c8142a059262c330941f3fe2a34d16d6e3c7b30d2ceb37c6a0e9a994c494ee1a61d2410885aa4c8bf8e56e264c0c0".from_hex().unwrap(); - let b1 = "f90261f901f9a05716670833ec874362d65fea27a7cd35af5897d275b31a44944113111e4e96d2a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0cb52de543653d86ccd13ba3ddf8b052525b04231c6884a4db3188a184681d878a0e78628dd45a1f8dc495594d83b76c588a3ee67463260f8b7d4a42f574aeab29aa0e9244cf7503b79c03d3a099e07a80d2dbc77bb0b502d8a89d51ac0d68dd31313b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000001832fefd882520884562791e580a051b3ecba4e3f2b49c11d42dd0851ec514b1be3138080f72a2b6e83868275d98f8877671f479c414b47f862f86080018304cb2f94095e7baea6a6c7c4c2dfeb977efac326af552d870a801ca09e2709d7ec9bbe6b1bbbf0b2088828d14cd5e8642a1fee22dc74bfa89761a7f9a04bd8813dee4be989accdb708b1c2e325a7e9c695a8024e30e89d6c644e424747c0".from_hex().unwrap(); - let b2 = "f902ccf901f9a0437e51676ff10756fcfee5edd9159fa41dbcb1b2c592850450371cbecd54ee4fa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0c70a5dc56146e5ef025e4e5726a6373c6f12fd2f6784093a19ead0a7d17fb292a040645cbce4fd399e7bb9160b4c30c40d7ee616a030d4e18ef0ed3b02bdb65911a086e608555f63628417032a011d107b36427af37d153f0da02ce3f90fdd5e8c08b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302004002832fefd882c0e384562791e880a0e3cc39ff775cc0a32f175995b92e84b729e5c9a3563ff899e3555b908bc21d75887c3cde283f4846a6f8cdf8cb01018304cb2f8080b87e6060604052606e8060106000396000f360606040526000357c010000000000000000000000000000000000000000000000000000000090048063c0406226146037576035565b005b60406004506056565b6040518082815260200191505060405180910390f35b6000600560006000508190555060059050606b565b90561ba05258615c63503c0a600d6994b12ea5750d45b3c69668e2a371b4fbfb9eeff6b8a0a11be762bc90491231274a2945be35a43f23c27775b1ff24dd521702fe15f73ec0".from_hex().unwrap(); - let b3a = "f90261f901f9a036fde1253128666fcb95a5956da14a73489e988bb72738717ec1d31e1cee781aa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a05fb2b4bfdef7b314451cb138a534d225c922fc0e5fbe25e451142732c3e25c25a09dc4b1357c0b7b8108f8a098f4f9a1a274957bc9ebc22a9ae67ae81739e5b19ca007c6fdfa8eea7e86b81f5b0fc0f78f90cc19f4aa60d323151e0cac660199e9a1b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302008003832fefd882524d84562791eb80a074861666bd346c025889745c793b91ab9cd1e2ca19b5cf3c50d04d135b0a4d2b8809fe9587ea4cdc04f862f86002018304cb2f94ec0e71ad0a90ffe1909d27dac207f7680abba42d01801ba06fd84874d36d5de9e8e48978c03619b53a96b7ae0a4cd1ac118f103098b44801a00572596974dd7df4f9f69bd7456585618c568d8434ef6453391b89281ce12ae1c0".from_hex().unwrap(); - let b3b = "f90265f901f9a036fde1253128666fcb95a5956da14a73489e988bb72738717ec1d31e1cee781aa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0ab87dc338bfd6f662b1cd90bc0c9e40a1b2146a095312393c9e13ce3a5008b09a0e609b7a7d4b8a2403ec1268627ecd98783627246e8f1b26addb3ff504f76a054a0592fabf92476512952db3a69a2481a42912e668a1ee28c4c322e703bb665f8beb90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302008003832fefd882a1f084562791ee80a0fe7098fa7e4ac5d637eea81fb23f8f78346826dbab430068dd9a249d0afa99818853e1a6b201ae3545f866f86402018304cb2f94ec0e71ad0a90ffe1909d27dac207f7680abba42d0284c04062261ca06edc9ce8e7da4cc34067beb325dcad59e5655a164a5100a50bc3eb681b12c716a0abf9053d5de65b1be81fe50d327b84de685efbeecea34e7b747180a6c6023e44c0".from_hex().unwrap(); - - let mut dir = env::temp_dir(); - dir.push(H32::random().hex()); - - let bc = BlockChain::new(&genesis, &dir); - bc.insert_block(&b1); - bc.insert_block(&b2); - bc.insert_block(&b3a); - bc.insert_block(&b3b); - - bc - } - #[test] fn valid_tests_extra32() { let genesis = "f901fcf901f7a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0925002c3260b44e44c3edebad1cc442142b03020209df1ab8bb86752edbd2cd7a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000080832fefd8808454c98c8142a0363659b251bf8b819179874c8cce7b9b983d7f3704cbb58a3b334431f7032871889032d09c281e1236c0c0".from_hex().unwrap(); @@ -798,21 +779,4 @@ mod tests { } } - #[test] - fn can_be_squeezed() { - let bc = get_populated_bc(); - bc.squeeze_to_fit(CacheSize { blocks: 0, block_logs: 0, transaction_addresses: 0, block_details: 0, blocks_blooms: 0 } ); - assert_eq!(bc.cache_size().blocks, 1624); - assert_eq!(bc.cache_size().block_details, 3712); - assert_eq!(bc.cache_size().block_logs, 0); - assert_eq!(bc.cache_size().blocks_blooms, 0); - } - - #[test] - fn can_collect_garbage() { - let bc = get_populated_bc(); - bc.collect_garbage(false); - assert_eq!(bc.cache_size().blocks, 1624); - - } } diff --git a/src/client.rs b/src/client.rs index a83ff554e..28b5ada88 100644 --- a/src/client.rs +++ b/src/client.rs @@ -292,6 +292,18 @@ impl Client { pub fn tick(&self) { self.chain.read().unwrap().collect_garbage(false); } + + /// Minimizes cache used by the client. + pub fn minimize_cache(&self) { + self.chain.read().unwrap().squeeze_to_fit( + CacheSize { + blocks: 0, + block_logs: 0, + transaction_addresses: 0, + block_details: 0, + blocks_blooms: 0 + }); + } } impl BlockChainClient for Client { diff --git a/src/tests/client.rs b/src/tests/client.rs index 57ab6e577..2b8a97551 100644 --- a/src/tests/client.rs +++ b/src/tests/client.rs @@ -1,8 +1,5 @@ use client::{BlockChainClient,Client}; -use std::env; use super::test_common::*; -use std::path::PathBuf; -use spec::*; use super::helpers::*; #[cfg(test)] @@ -35,14 +32,6 @@ fn get_bad_state_dummy_block() -> Bytes { create_test_block(&block_header) } -#[cfg(test)] -fn create_test_block(header: &Header) -> Bytes { - let mut rlp = RlpStream::new_list(3); - rlp.append(header); - rlp.append_raw(&rlp::EMPTY_LIST_RLP, 1); - rlp.append_raw(&rlp::EMPTY_LIST_RLP, 1); - rlp.out() -} #[cfg(test)] fn get_test_client_with_blocks(blocks: Vec) -> Arc { @@ -108,4 +97,30 @@ fn returns_chain_info() { let block = BlockView::new(&dummy_block); let info = client.chain_info(); assert_eq!(info.best_block_hash, block.header().hash()); +} + +#[test] +fn imports_block_sequence() { + let client = generate_dummy_client(6); + let block = client.block_header_at(5).unwrap(); + + assert!(!block.is_empty()); +} + + +#[test] +fn can_have_cash_minimized() { + let client = generate_dummy_client(20); + client.minimize_cache(); + assert!(client.cache_info().blocks < 2048); + assert!(client.cache_info().block_details < 4096); + assert_eq!(client.cache_info().block_logs, 0); + assert_eq!(client.cache_info().blocks_blooms, 0); +} + +#[test] +fn can_collect_garbage() { + let client = generate_dummy_client(100); + client.tick(); + assert!(client.cache_info().blocks < 100 * 1024); } \ No newline at end of file diff --git a/src/tests/helpers.rs b/src/tests/helpers.rs index 4cbd36692..7f9b8ba42 100644 --- a/src/tests/helpers.rs +++ b/src/tests/helpers.rs @@ -32,23 +32,50 @@ pub fn get_test_spec() -> Spec { Spec::new_test() } + #[cfg(test)] -pub fn generate_dummy_client(block_number: usize) { +pub fn create_test_block(header: &Header) -> Bytes { + let mut rlp = RlpStream::new_list(3); + rlp.append(header); + rlp.append_raw(&rlp::EMPTY_LIST_RLP, 1); + rlp.append_raw(&rlp::EMPTY_LIST_RLP, 1); + rlp.out() +} + +#[cfg(test)] +pub fn generate_dummy_client(block_number: usize) -> Arc { let client = Client::new(get_test_spec(), &get_random_path(), IoChannel::disconnected()).unwrap(); + let test_spec = get_test_spec(); + let test_engine = test_spec.to_engine().unwrap(); + let state_root = test_engine.spec().genesis_header().state_root; let mut rolling_hash = test_engine.spec().genesis_header().hash(); - let mut rolling_state = test_engine.spec().genesis_header().state_root; let mut rolling_block_number = 1; + let mut rolling_timestamp = 40; for _ in 0..block_number { let mut header = Header::new(); header.gas_limit = decode(test_engine.spec().engine_params.get("minGasLimit").unwrap()); header.difficulty = decode(test_engine.spec().engine_params.get("minimumDifficulty").unwrap()); - header.timestamp = 40; + header.timestamp = rolling_timestamp; header.number = rolling_block_number; - header.parent_hash = test_engine.spec().genesis_header().hash(); - header.state_root = test_engine.spec().genesis_header().state_root; + header.parent_hash = rolling_hash; + header.state_root = state_root.clone(); + + rolling_hash = header.hash(); + rolling_block_number = rolling_block_number + 1; + rolling_timestamp = rolling_timestamp + 10; + + if let Err(_) = client.import_block(create_test_block(&header)) { + panic!("error importing block which is valid by definition"); + } + } + client.flush_queue(); + client.import_verified_blocks(&IoChannel::disconnected()); + + client + } \ No newline at end of file From 1897d55a791e7ea77e6840d4eb6437bf6a8cc99f Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Wed, 27 Jan 2016 16:41:41 +0400 Subject: [PATCH 07/11] removed unused cfg options --- src/tests/client.rs | 3 --- src/tests/helpers.rs | 7 +------ 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/src/tests/client.rs b/src/tests/client.rs index 2b8a97551..92bf31c89 100644 --- a/src/tests/client.rs +++ b/src/tests/client.rs @@ -2,7 +2,6 @@ use client::{BlockChainClient,Client}; use super::test_common::*; use super::helpers::*; -#[cfg(test)] fn get_good_dummy_block() -> Bytes { let mut block_header = Header::new(); let test_spec = get_test_spec(); @@ -17,7 +16,6 @@ fn get_good_dummy_block() -> Bytes { create_test_block(&block_header) } -#[cfg(test)] fn get_bad_state_dummy_block() -> Bytes { let mut block_header = Header::new(); let test_spec = get_test_spec(); @@ -33,7 +31,6 @@ fn get_bad_state_dummy_block() -> Bytes { } -#[cfg(test)] fn get_test_client_with_blocks(blocks: Vec) -> Arc { let client = Client::new(get_test_spec(), &get_random_path(), IoChannel::disconnected()).unwrap(); for block in &blocks { diff --git a/src/tests/helpers.rs b/src/tests/helpers.rs index 7f9b8ba42..4aa2ae8fc 100644 --- a/src/tests/helpers.rs +++ b/src/tests/helpers.rs @@ -5,11 +5,10 @@ use std::path::PathBuf; use spec::*; use std::fs::{create_dir_all}; -#[cfg(test)] + const FIXED_TEMP_DIR_NAME: &'static str = "parity-temp"; -#[cfg(test)] pub fn get_tests_temp_dir() -> PathBuf { let mut dir = env::temp_dir(); dir.push(FIXED_TEMP_DIR_NAME); @@ -19,7 +18,6 @@ pub fn get_tests_temp_dir() -> PathBuf { dir } -#[cfg(test)] pub fn get_random_path() -> PathBuf { let mut dir = get_tests_temp_dir(); dir.push(H32::random().hex()); @@ -27,13 +25,11 @@ pub fn get_random_path() -> PathBuf { } -#[cfg(test)] pub fn get_test_spec() -> Spec { Spec::new_test() } -#[cfg(test)] pub fn create_test_block(header: &Header) -> Bytes { let mut rlp = RlpStream::new_list(3); rlp.append(header); @@ -42,7 +38,6 @@ pub fn create_test_block(header: &Header) -> Bytes { rlp.out() } -#[cfg(test)] pub fn generate_dummy_client(block_number: usize) -> Arc { let client = Client::new(get_test_spec(), &get_random_path(), IoChannel::disconnected()).unwrap(); From d2cd6f69cbb1bb25c16d84988419c57f09093eed Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Wed, 27 Jan 2016 17:21:54 +0400 Subject: [PATCH 08/11] added temp directory struct with drop --- src/tests/client.rs | 15 ++++++++++----- src/tests/helpers.rs | 42 ++++++++++++++++++++++++++---------------- 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/tests/client.rs b/src/tests/client.rs index 92bf31c89..f6887cb9c 100644 --- a/src/tests/client.rs +++ b/src/tests/client.rs @@ -32,7 +32,8 @@ fn get_bad_state_dummy_block() -> Bytes { fn get_test_client_with_blocks(blocks: Vec) -> Arc { - let client = Client::new(get_test_spec(), &get_random_path(), IoChannel::disconnected()).unwrap(); + let dir = RandomTempPath::new(); + let client = Client::new(get_test_spec(), dir.as_path(), IoChannel::disconnected()).unwrap(); for block in &blocks { if let Err(_) = client.import_block(block.clone()) { panic!("panic importing block which is well-formed"); @@ -46,20 +47,23 @@ fn get_test_client_with_blocks(blocks: Vec) -> Arc { #[test] fn created() { - let client_result = Client::new(get_test_spec(), &get_random_path(), IoChannel::disconnected()); + let dir = RandomTempPath::new(); + let client_result = Client::new(get_test_spec(), dir.as_path(), IoChannel::disconnected()); assert!(client_result.is_ok()); } #[test] fn imports_from_empty() { - let client = Client::new(get_test_spec(), &get_random_path(), IoChannel::disconnected()).unwrap(); + let dir = RandomTempPath::new(); + let client = Client::new(get_test_spec(), dir.as_path(), IoChannel::disconnected()).unwrap(); client.import_verified_blocks(&IoChannel::disconnected()); client.flush_queue(); } #[test] fn imports_good_block() { - let client = Client::new(get_test_spec(), &get_random_path(), IoChannel::disconnected()).unwrap(); + let dir = RandomTempPath::new(); + let client = Client::new(get_test_spec(), dir.as_path(), IoChannel::disconnected()).unwrap(); let good_block = get_good_dummy_block(); if let Err(_) = client.import_block(good_block) { panic!("error importing block being good by definition"); @@ -73,7 +77,8 @@ fn imports_good_block() { #[test] fn query_none_block() { - let client = Client::new(get_test_spec(), &get_random_path(), IoChannel::disconnected()).unwrap(); + let dir = RandomTempPath::new(); + let client = Client::new(get_test_spec(), dir.as_path(), IoChannel::disconnected()).unwrap(); let non_existant = client.block_header_at(188); assert!(non_existant.is_none()); diff --git a/src/tests/helpers.rs b/src/tests/helpers.rs index 4aa2ae8fc..e7c717f42 100644 --- a/src/tests/helpers.rs +++ b/src/tests/helpers.rs @@ -3,25 +3,33 @@ use std::env; use super::test_common::*; use std::path::PathBuf; use spec::*; -use std::fs::{create_dir_all}; +use std::fs::{remove_dir_all}; -const FIXED_TEMP_DIR_NAME: &'static str = "parity-temp"; - - -pub fn get_tests_temp_dir() -> PathBuf { - let mut dir = env::temp_dir(); - dir.push(FIXED_TEMP_DIR_NAME); - if let Err(_) = create_dir_all(&dir) { - panic!("failed to create test dir!"); - } - dir +pub struct RandomTempPath { + path: PathBuf } -pub fn get_random_path() -> PathBuf { - let mut dir = get_tests_temp_dir(); - dir.push(H32::random().hex()); - dir +impl RandomTempPath { + pub fn new() -> RandomTempPath { + let mut dir = env::temp_dir(); + dir.push(H32::random().hex()); + RandomTempPath { + path: dir.clone() + } + } + + pub fn as_path(&self) -> &PathBuf { + &self.path + } +} + +impl Drop for RandomTempPath { + fn drop(&mut self) { + if let Err(e) = remove_dir_all(self.as_path()) { + panic!("failed to remove temp directory, probably something failed to destroyed ({})", e); + } + } } @@ -39,7 +47,9 @@ pub fn create_test_block(header: &Header) -> Bytes { } pub fn generate_dummy_client(block_number: usize) -> Arc { - let client = Client::new(get_test_spec(), &get_random_path(), IoChannel::disconnected()).unwrap(); + let dir = RandomTempPath::new(); + + let client = Client::new(get_test_spec(), dir.as_path(), IoChannel::disconnected()).unwrap(); let test_spec = get_test_spec(); let test_engine = test_spec.to_engine().unwrap(); From f1edf627ae18a7ad54098373cae8df8facf1581f Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Wed, 27 Jan 2016 19:01:33 +0400 Subject: [PATCH 09/11] fixed notes --- src/blockchain.rs | 1 - src/client.rs | 11 ----------- src/tests/client.rs | 11 ----------- 3 files changed, 23 deletions(-) diff --git a/src/blockchain.rs b/src/blockchain.rs index a0b554bcc..da9ee04c2 100644 --- a/src/blockchain.rs +++ b/src/blockchain.rs @@ -778,5 +778,4 @@ mod tests { assert_eq!(bc.best_block_hash(), b1_hash); } } - } diff --git a/src/client.rs b/src/client.rs index 28b5ada88..9aa9854b6 100644 --- a/src/client.rs +++ b/src/client.rs @@ -293,17 +293,6 @@ impl Client { self.chain.read().unwrap().collect_garbage(false); } - /// Minimizes cache used by the client. - pub fn minimize_cache(&self) { - self.chain.read().unwrap().squeeze_to_fit( - CacheSize { - blocks: 0, - block_logs: 0, - transaction_addresses: 0, - block_details: 0, - blocks_blooms: 0 - }); - } } impl BlockChainClient for Client { diff --git a/src/tests/client.rs b/src/tests/client.rs index f6887cb9c..f6d603f43 100644 --- a/src/tests/client.rs +++ b/src/tests/client.rs @@ -109,17 +109,6 @@ fn imports_block_sequence() { assert!(!block.is_empty()); } - -#[test] -fn can_have_cash_minimized() { - let client = generate_dummy_client(20); - client.minimize_cache(); - assert!(client.cache_info().blocks < 2048); - assert!(client.cache_info().block_details < 4096); - assert_eq!(client.cache_info().block_logs, 0); - assert_eq!(client.cache_info().blocks_blooms, 0); -} - #[test] fn can_collect_garbage() { let client = generate_dummy_client(100); From 0312c08494e6779829c42393a3ed96b5f8dfa454 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Wed, 27 Jan 2016 19:07:35 +0400 Subject: [PATCH 10/11] spaces fix --- src/client.rs | 1 - src/tests/helpers.rs | 6 ------ 2 files changed, 7 deletions(-) diff --git a/src/client.rs b/src/client.rs index 9aa9854b6..a83ff554e 100644 --- a/src/client.rs +++ b/src/client.rs @@ -292,7 +292,6 @@ impl Client { pub fn tick(&self) { self.chain.read().unwrap().collect_garbage(false); } - } impl BlockChainClient for Client { diff --git a/src/tests/helpers.rs b/src/tests/helpers.rs index e7c717f42..a566392cc 100644 --- a/src/tests/helpers.rs +++ b/src/tests/helpers.rs @@ -32,12 +32,10 @@ impl Drop for RandomTempPath { } } - pub fn get_test_spec() -> Spec { Spec::new_test() } - pub fn create_test_block(header: &Header) -> Bytes { let mut rlp = RlpStream::new_list(3); rlp.append(header); @@ -50,7 +48,6 @@ pub fn generate_dummy_client(block_number: usize) -> Arc { let dir = RandomTempPath::new(); let client = Client::new(get_test_spec(), dir.as_path(), IoChannel::disconnected()).unwrap(); - let test_spec = get_test_spec(); let test_engine = test_spec.to_engine().unwrap(); let state_root = test_engine.spec().genesis_header().state_root; @@ -77,10 +74,7 @@ pub fn generate_dummy_client(block_number: usize) -> Arc { } } - client.flush_queue(); client.import_verified_blocks(&IoChannel::disconnected()); - client - } \ No newline at end of file From f7a0f1056803fe6ef819040a236d04872f5aa332 Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 27 Jan 2016 16:58:53 +0100 Subject: [PATCH 11/11] missing methods --- src/sync/tests.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/sync/tests.rs b/src/sync/tests.rs index 50d6efab2..646933122 100644 --- a/src/sync/tests.rs +++ b/src/sync/tests.rs @@ -51,6 +51,10 @@ impl TestBlockChainClient { } impl BlockChainClient for TestBlockChainClient { + fn block_total_difficulty(&self, _h: &H256) -> Option { + unimplemented!(); + } + fn block_header(&self, h: &H256) -> Option { self.blocks.read().unwrap().get(h).map(|r| Rlp::new(r).at(0).as_raw().to_vec()) @@ -76,6 +80,10 @@ impl BlockChainClient for TestBlockChainClient { } } + fn block_total_difficulty_at(&self, _number: BlockNumber) -> Option { + unimplemented!(); + } + fn block_header_at(&self, n: BlockNumber) -> Option { self.numbers.read().unwrap().get(&(n as usize)).and_then(|h| self.block_header(h)) }