From 2d9b60b4a2c9ea399dd3e97da1ea1e92e628fdcc Mon Sep 17 00:00:00 2001 From: theohax Date: Tue, 20 Apr 2021 21:55:03 +0300 Subject: [PATCH] More unit test modernizing --- nano/core_test/block.cpp | 153 ++++++++++++++++++++---------- nano/core_test/network_filter.cpp | 23 ++++- nano/core_test/websocket.cpp | 107 +++++++++++++++++++-- nano/core_test/work_watcher.cpp | 24 ++++- 4 files changed, 245 insertions(+), 62 deletions(-) diff --git a/nano/core_test/block.cpp b/nano/core_test/block.cpp index 88b8766d31..df98c3a84f 100644 --- a/nano/core_test/block.cpp +++ b/nano/core_test/block.cpp @@ -319,16 +319,25 @@ TEST (state_block, serialization) { nano::keypair key1; nano::keypair key2; - nano::state_block block1 (key1.pub, 1, key2.pub, 2, 4, key1.prv, key1.pub, 5); - ASSERT_EQ (key1.pub, block1.hashables.account); - ASSERT_EQ (nano::block_hash (1), block1.previous ()); - ASSERT_EQ (key2.pub, block1.hashables.representative); - ASSERT_EQ (nano::amount (2), block1.hashables.balance); - ASSERT_EQ (nano::uint256_union (4), block1.hashables.link); + nano::state_block_builder builder; + auto block1 = builder + .account (key1.pub) + .previous (1) + .representative (key2.pub) + .balance (2) + .link (4) + .sign (key1.prv, key1.pub) + .work (5) + .build_shared (); + ASSERT_EQ (key1.pub, block1->hashables.account); + ASSERT_EQ (nano::block_hash (1), block1->previous ()); + ASSERT_EQ (key2.pub, block1->hashables.representative); + ASSERT_EQ (nano::amount (2), block1->hashables.balance); + ASSERT_EQ (nano::uint256_union (4), block1->hashables.link); std::vector bytes; { nano::vectorstream stream (bytes); - block1.serialize (stream); + block1->serialize (stream); } ASSERT_EQ (0x5, bytes[215]); // Ensure work is serialized big-endian ASSERT_EQ (nano::state_block::size, bytes.size ()); @@ -336,7 +345,7 @@ TEST (state_block, serialization) nano::bufferstream stream (bytes.data (), bytes.size ()); nano::state_block block2 (error1, stream); ASSERT_FALSE (error1); - ASSERT_EQ (block1, block2); + ASSERT_EQ (*block1, block2); block2.hashables.account.clear (); block2.hashables.previous.clear (); block2.hashables.representative.clear (); @@ -346,16 +355,16 @@ TEST (state_block, serialization) block2.work = 0; nano::bufferstream stream2 (bytes.data (), bytes.size ()); ASSERT_FALSE (block2.deserialize (stream2)); - ASSERT_EQ (block1, block2); + ASSERT_EQ (*block1, block2); std::string json; - block1.serialize_json (json); + block1->serialize_json (json); std::stringstream body (json); boost::property_tree::ptree tree; boost::property_tree::read_json (body, tree); bool error2 (false); nano::state_block block3 (error2, tree); ASSERT_FALSE (error2); - ASSERT_EQ (block1, block3); + ASSERT_EQ (*block1, block3); block3.hashables.account.clear (); block3.hashables.previous.clear (); block3.hashables.representative.clear (); @@ -364,45 +373,54 @@ TEST (state_block, serialization) block3.signature.clear (); block3.work = 0; ASSERT_FALSE (block3.deserialize_json (tree)); - ASSERT_EQ (block1, block3); + ASSERT_EQ (*block1, block3); } TEST (state_block, hashing) { nano::keypair key; - nano::state_block block (key.pub, 0, key.pub, 0, 0, key.prv, key.pub, 0); - auto hash (block.hash ()); - ASSERT_EQ (hash, block.hash ()); // check cache works - block.hashables.account.bytes[0] ^= 0x1; - block.refresh (); - ASSERT_NE (hash, block.hash ()); - block.hashables.account.bytes[0] ^= 0x1; - block.refresh (); - ASSERT_EQ (hash, block.hash ()); - block.hashables.previous.bytes[0] ^= 0x1; - block.refresh (); - ASSERT_NE (hash, block.hash ()); - block.hashables.previous.bytes[0] ^= 0x1; - block.refresh (); - ASSERT_EQ (hash, block.hash ()); - block.hashables.representative.bytes[0] ^= 0x1; - block.refresh (); - ASSERT_NE (hash, block.hash ()); - block.hashables.representative.bytes[0] ^= 0x1; - block.refresh (); - ASSERT_EQ (hash, block.hash ()); - block.hashables.balance.bytes[0] ^= 0x1; - block.refresh (); - ASSERT_NE (hash, block.hash ()); - block.hashables.balance.bytes[0] ^= 0x1; - block.refresh (); - ASSERT_EQ (hash, block.hash ()); - block.hashables.link.bytes[0] ^= 0x1; - block.refresh (); - ASSERT_NE (hash, block.hash ()); - block.hashables.link.bytes[0] ^= 0x1; - block.refresh (); - ASSERT_EQ (hash, block.hash ()); + nano::state_block_builder builder; + auto block = builder + .account (key.pub) + .previous (0) + .representative (key.pub) + .balance (0) + .link (0) + .sign (key.prv, key.pub) + .work (0) + .build_shared (); + auto hash (block->hash ()); + ASSERT_EQ (hash, block->hash ()); // check cache works + block->hashables.account.bytes[0] ^= 0x1; + block->refresh (); + ASSERT_NE (hash, block->hash ()); + block->hashables.account.bytes[0] ^= 0x1; + block->refresh (); + ASSERT_EQ (hash, block->hash ()); + block->hashables.previous.bytes[0] ^= 0x1; + block->refresh (); + ASSERT_NE (hash, block->hash ()); + block->hashables.previous.bytes[0] ^= 0x1; + block->refresh (); + ASSERT_EQ (hash, block->hash ()); + block->hashables.representative.bytes[0] ^= 0x1; + block->refresh (); + ASSERT_NE (hash, block->hash ()); + block->hashables.representative.bytes[0] ^= 0x1; + block->refresh (); + ASSERT_EQ (hash, block->hash ()); + block->hashables.balance.bytes[0] ^= 0x1; + block->refresh (); + ASSERT_NE (hash, block->hash ()); + block->hashables.balance.bytes[0] ^= 0x1; + block->refresh (); + ASSERT_EQ (hash, block->hash ()); + block->hashables.link.bytes[0] ^= 0x1; + block->refresh (); + ASSERT_NE (hash, block->hash ()); + block->hashables.link.bytes[0] ^= 0x1; + block->refresh (); + ASSERT_EQ (hash, block->hash ()); } TEST (blocks, work_version) @@ -423,7 +441,16 @@ TEST (block_uniquer, null) TEST (block_uniquer, single) { nano::keypair key; - auto block1 (std::make_shared (0, 0, 0, 0, 0, key.prv, key.pub, 0)); + nano::state_block_builder builder; + auto block1 = builder + .account (0) + .previous (0) + .representative (0) + .balance (0) + .link (0) + .sign (key.prv, key.pub) + .work (0) + .build_shared (); auto block2 (std::make_shared (*block1)); ASSERT_NE (block1, block2); ASSERT_EQ (*block1, *block2); @@ -441,8 +468,27 @@ TEST (block_uniquer, single) TEST (block_uniquer, cleanup) { nano::keypair key; - auto block1 (std::make_shared (0, 0, 0, 0, 0, key.prv, key.pub, 0)); - auto block2 (std::make_shared (0, 0, 0, 0, 0, key.prv, key.pub, 1)); + nano::state_block_builder builder; + auto block1 = builder + .account (0) + .previous (0) + .representative (0) + .balance (0) + .link (0) + .sign (key.prv, key.pub) + .work (0) + .build_shared (); + auto block2 = builder + .make_block () + .account (0) + .previous (0) + .representative (0) + .balance (0) + .link (0) + .sign (key.prv, key.pub) + .work (1) + .build_shared (); + nano::block_uniquer uniquer; auto block3 (uniquer.unique (block1)); auto block4 (uniquer.unique (block2)); @@ -482,8 +528,17 @@ TEST (block_builder, zeroed_state_block) { nano::block_builder builder; nano::keypair key; + nano::state_block_builder state_builder; // Make sure manually- and builder constructed all-zero blocks have equal hashes, and check signature. - auto zero_block_manual (std::make_shared (0, 0, 0, 0, 0, key.prv, key.pub, 0)); + auto zero_block_manual = state_builder + .account (0) + .previous (0) + .representative (0) + .balance (0) + .link (0) + .sign (key.prv, key.pub) + .work (0) + .build_shared (); auto zero_block_build = builder.state ().zero ().sign (key.prv, key.pub).build (); ASSERT_TRUE (zero_block_manual->hash () == zero_block_build->hash ()); ASSERT_FALSE (nano::validate_message (key.pub, zero_block_build->hash (), zero_block_build->signature)); diff --git a/nano/core_test/network_filter.cpp b/nano/core_test/network_filter.cpp index 5770d83ff4..1d2d2fa4bd 100644 --- a/nano/core_test/network_filter.cpp +++ b/nano/core_test/network_filter.cpp @@ -37,7 +37,17 @@ TEST (network_filter, unit) { one_block (genesis.open, true); } - auto new_block (std::make_shared (nano::dev_genesis_key.pub, genesis.open->hash (), nano::dev_genesis_key.pub, nano::genesis_amount - 10 * nano::xrb_ratio, nano::public_key (), nano::dev_genesis_key.prv, nano::dev_genesis_key.pub, 0)); + nano::state_block_builder builder; + auto new_block = builder + .account (nano::dev_genesis_key.pub) + .previous (genesis.open->hash ()) + .representative (nano::dev_genesis_key.pub) + .balance (nano::genesis_amount - 10 * nano::xrb_ratio) + .link (nano::public_key ()) + .sign (nano::dev_genesis_key.prv, nano::dev_genesis_key.pub) + .work (0) + .build_shared (); + one_block (new_block, false); for (int i = 0; i < 10; ++i) { @@ -55,9 +65,18 @@ TEST (network_filter, many) nano::genesis genesis; nano::network_filter filter (4); nano::keypair key1; + nano::state_block_builder builder; for (int i = 0; i < 100; ++i) { - auto block (std::make_shared (nano::dev_genesis_key.pub, genesis.open->hash (), nano::dev_genesis_key.pub, nano::genesis_amount - i * 10 * nano::xrb_ratio, key1.pub, nano::dev_genesis_key.prv, nano::dev_genesis_key.pub, 0)); + auto block = builder + .account (nano::dev_genesis_key.pub) + .previous (genesis.open->hash ()) + .representative (nano::dev_genesis_key.pub) + .balance (nano::genesis_amount - i * 10 * nano::xrb_ratio) + .link (key1.pub) + .sign (nano::dev_genesis_key.prv, nano::dev_genesis_key.pub) + .work (0) + .build_shared (); nano::publish message (block); auto bytes (message.to_bytes ()); diff --git a/nano/core_test/websocket.cpp b/nano/core_test/websocket.cpp index b7fec3e16d..1d0535e41d 100644 --- a/nano/core_test/websocket.cpp +++ b/nano/core_test/websocket.cpp @@ -167,9 +167,19 @@ TEST (websocket, confirmation) // Quick confirm a state block { + nano::state_block_builder builder; nano::block_hash previous (node1->latest (nano::dev_genesis_key.pub)); balance -= send_amount; - auto send (std::make_shared (nano::dev_genesis_key.pub, previous, nano::dev_genesis_key.pub, balance, key.pub, nano::dev_genesis_key.prv, nano::dev_genesis_key.pub, *system.work.generate (previous))); + auto send = builder + .account (nano::dev_genesis_key.pub) + .previous (previous) + .representative (nano::dev_genesis_key.pub) + .balance (balance) + .link (key.pub) + .sign (nano::dev_genesis_key.prv, nano::dev_genesis_key.pub) + .work (*system.work.generate (previous)) + .build_shared (); + node1->process_active (send); } @@ -251,7 +261,17 @@ TEST (websocket, confirmation_options) nano::block_hash previous (node1->latest (nano::dev_genesis_key.pub)); { balance -= send_amount; - auto send (std::make_shared (nano::dev_genesis_key.pub, previous, nano::dev_genesis_key.pub, balance, key.pub, nano::dev_genesis_key.prv, nano::dev_genesis_key.pub, *system.work.generate (previous))); + nano::state_block_builder builder; + auto send = builder + .account (nano::dev_genesis_key.pub) + .previous (previous) + .representative (nano::dev_genesis_key.pub) + .balance (balance) + .link (key.pub) + .sign (nano::dev_genesis_key.prv, nano::dev_genesis_key.pub) + .work (*system.work.generate (previous)) + .build_shared (); + node1->process_active (send); previous = send->hash (); } @@ -274,7 +294,17 @@ TEST (websocket, confirmation_options) // Quick-confirm another block { balance -= send_amount; - auto send (std::make_shared (nano::dev_genesis_key.pub, previous, nano::dev_genesis_key.pub, balance, key.pub, nano::dev_genesis_key.prv, nano::dev_genesis_key.pub, *system.work.generate (previous))); + nano::state_block_builder builder; + auto send = builder + .account (nano::dev_genesis_key.pub) + .previous (previous) + .representative (nano::dev_genesis_key.pub) + .balance (balance) + .link (key.pub) + .sign (nano::dev_genesis_key.prv, nano::dev_genesis_key.pub) + .work (*system.work.generate (previous)) + .build_shared (); + node1->process_active (send); previous = send->hash (); } @@ -363,8 +393,18 @@ TEST (websocket, confirmation_options_votes) auto send_amount = node1->config.online_weight_minimum.number () + 1; nano::block_hash previous (node1->latest (nano::dev_genesis_key.pub)); { + nano::state_block_builder builder; balance -= send_amount; - auto send (std::make_shared (nano::dev_genesis_key.pub, previous, nano::dev_genesis_key.pub, balance, key.pub, nano::dev_genesis_key.prv, nano::dev_genesis_key.pub, *system.work.generate (previous))); + auto send = builder + .account (nano::dev_genesis_key.pub) + .previous (previous) + .representative (nano::dev_genesis_key.pub) + .balance (balance) + .link (key.pub) + .sign (nano::dev_genesis_key.prv, nano::dev_genesis_key.pub) + .work (*system.work.generate (previous)) + .build_shared (); + node1->process_active (send); previous = send->hash (); } @@ -453,8 +493,18 @@ TEST (websocket, confirmation_options_update) system.wallet (0)->insert_adhoc (nano::dev_genesis_key.prv); nano::genesis genesis; nano::keypair key; + nano::state_block_builder builder; auto previous (node1->latest (nano::dev_genesis_key.pub)); - auto send (std::make_shared (nano::dev_genesis_key.pub, previous, nano::dev_genesis_key.pub, nano::genesis_amount - nano::Gxrb_ratio, key.pub, nano::dev_genesis_key.prv, nano::dev_genesis_key.pub, *system.work.generate (previous))); + auto send = builder + .account (nano::dev_genesis_key.pub) + .previous (previous) + .representative (nano::dev_genesis_key.pub) + .balance (nano::genesis_amount - nano::Gxrb_ratio) + .link (key.pub) + .sign (nano::dev_genesis_key.prv, nano::dev_genesis_key.pub) + .work (*system.work.generate (previous)) + .build_shared (); + node1->process_active (send); // Wait for delete acknowledgement @@ -462,7 +512,17 @@ TEST (websocket, confirmation_options_update) // Confirm another block previous = send->hash (); - auto send2 (std::make_shared (nano::dev_genesis_key.pub, previous, nano::dev_genesis_key.pub, nano::genesis_amount - 2 * nano::Gxrb_ratio, key.pub, nano::dev_genesis_key.prv, nano::dev_genesis_key.pub, *system.work.generate (previous))); + auto send2 = builder + .make_block () + .account (nano::dev_genesis_key.pub) + .previous (previous) + .representative (nano::dev_genesis_key.pub) + .balance (nano::genesis_amount - 2 * nano::Gxrb_ratio) + .link (key.pub) + .sign (nano::dev_genesis_key.prv, nano::dev_genesis_key.pub) + .work (*system.work.generate (previous)) + .build_shared (); + node1->process_active (send2); ASSERT_TIMELY (5s, future.wait_for (0s) == std::future_status::ready); @@ -492,9 +552,19 @@ TEST (websocket, vote) // Quick-confirm a block nano::keypair key; + nano::state_block_builder builder; system.wallet (0)->insert_adhoc (nano::dev_genesis_key.prv); nano::block_hash previous (node1->latest (nano::dev_genesis_key.pub)); - auto send (std::make_shared (nano::dev_genesis_key.pub, previous, nano::dev_genesis_key.pub, nano::genesis_amount - (node1->online_reps.delta () + 1), key.pub, nano::dev_genesis_key.prv, nano::dev_genesis_key.pub, *system.work.generate (previous))); + auto send = builder + .account (nano::dev_genesis_key.pub) + .previous (previous) + .representative (nano::dev_genesis_key.pub) + .balance (nano::genesis_amount - (node1->online_reps.delta () + 1)) + .link (key.pub) + .sign (nano::dev_genesis_key.prv, nano::dev_genesis_key.pub) + .work (*system.work.generate (previous)) + .build_shared (); + node1->process_active (send); ASSERT_TIMELY (5s, future.wait_for (0s) == std::future_status::ready); @@ -581,13 +651,22 @@ TEST (websocket, vote_options_representatives) // Quick-confirm a block nano::keypair key; + nano::state_block_builder builder; auto balance = nano::genesis_amount; system.wallet (0)->insert_adhoc (nano::dev_genesis_key.prv); auto send_amount = node1->online_reps.delta () + 1; auto confirm_block = [&] () { nano::block_hash previous (node1->latest (nano::dev_genesis_key.pub)); balance -= send_amount; - auto send (std::make_shared (nano::dev_genesis_key.pub, previous, nano::dev_genesis_key.pub, balance, key.pub, nano::dev_genesis_key.prv, nano::dev_genesis_key.pub, *system.work.generate (previous))); + auto send = builder + .account (nano::dev_genesis_key.pub) + .previous (previous) + .representative (nano::dev_genesis_key.pub) + .balance (balance) + .link (key.pub) + .sign (nano::dev_genesis_key.prv, nano::dev_genesis_key.pub) + .work (*system.work.generate (previous)) + .build_shared (); node1->process_active (send); }; confirm_block (); @@ -901,9 +980,19 @@ TEST (websocket, new_unconfirmed_block) ASSERT_TIMELY (5s, ack_ready); + nano::state_block_builder builder; // Process a new block nano::genesis genesis; - auto send1 (std::make_shared (nano::dev_genesis_key.pub, genesis.hash (), nano::dev_genesis_key.pub, nano::genesis_amount - 1, nano::dev_genesis_key.pub, nano::dev_genesis_key.prv, nano::dev_genesis_key.pub, *system.work.generate (genesis.hash ()))); + auto send1 = builder + .account (nano::dev_genesis_key.pub) + .previous (genesis.hash ()) + .representative (nano::dev_genesis_key.pub) + .balance (nano::genesis_amount - 1) + .link (nano::dev_genesis_key.pub) + .sign (nano::dev_genesis_key.prv, nano::dev_genesis_key.pub) + .work (*system.work.generate (genesis.hash ())) + .build_shared (); + ASSERT_EQ (nano::process_result::progress, node1->process_local (send1).code); ASSERT_TIMELY (5s, future.wait_for (0s) == std::future_status::ready); diff --git a/nano/core_test/work_watcher.cpp b/nano/core_test/work_watcher.cpp index 49770df17f..79fe4438ae 100644 --- a/nano/core_test/work_watcher.cpp +++ b/nano/core_test/work_watcher.cpp @@ -124,6 +124,7 @@ TEST (work_watcher, removed_after_lose) { nano::system system; nano::node_config node_config (nano::get_available_port (), system.logging); + nano::state_block_builder builder; node_config.enable_voting = false; node_config.work_watcher_period = 1s; auto & node = *system.add_node (node_config); @@ -133,7 +134,16 @@ TEST (work_watcher, removed_after_lose) auto const block1 (wallet.send_action (nano::dev_genesis_key.pub, key.pub, 100)); ASSERT_TRUE (node.wallets.watcher->is_watched (block1->qualified_root ())); nano::genesis genesis; - auto fork1 (std::make_shared (nano::dev_genesis_key.pub, genesis.hash (), nano::dev_genesis_key.pub, nano::genesis_amount - nano::xrb_ratio, nano::dev_genesis_key.pub, nano::dev_genesis_key.prv, nano::dev_genesis_key.pub, *system.work.generate (genesis.hash ()))); + auto fork1 = builder + .account (nano::dev_genesis_key.pub) + .previous (genesis.hash ()) + .representative (nano::dev_genesis_key.pub) + .balance (nano::genesis_amount - nano::xrb_ratio) + .link (nano::dev_genesis_key.pub) + .sign (nano::dev_genesis_key.prv, nano::dev_genesis_key.pub) + .work (*system.work.generate (genesis.hash ())) + .build_shared (); + node.process_active (fork1); node.block_processor.flush (); auto vote (std::make_shared (nano::dev_genesis_key.pub, nano::dev_genesis_key.prv, 0, fork1)); @@ -147,6 +157,7 @@ TEST (work_watcher, generation_disabled) { nano::system system; nano::node_config node_config (nano::get_available_port (), system.logging); + nano::state_block_builder builder; node_config.enable_voting = false; node_config.work_watcher_period = 1s; node_config.work_threads = 0; @@ -157,7 +168,16 @@ TEST (work_watcher, generation_disabled) nano::work_pool pool (std::numeric_limits::max ()); nano::genesis genesis; nano::keypair key; - auto block (std::make_shared (nano::dev_genesis_key.pub, genesis.hash (), nano::dev_genesis_key.pub, nano::genesis_amount - nano::Mxrb_ratio, key.pub, nano::dev_genesis_key.prv, nano::dev_genesis_key.pub, *pool.generate (genesis.hash ()))); + auto block = builder + .account (nano::dev_genesis_key.pub) + .previous (genesis.hash ()) + .representative (nano::dev_genesis_key.pub) + .balance (nano::genesis_amount - nano::Mxrb_ratio) + .link (key.pub) + .sign (nano::dev_genesis_key.prv, nano::dev_genesis_key.pub) + .work (*pool.generate (genesis.hash ())) + .build_shared (); + auto difficulty (block->difficulty ()); node.wallets.watcher->add (block); ASSERT_FALSE (node.process_local (block).code != nano::process_result::progress);