Skip to content

Commit

Permalink
Adding unconfirmed_set class.
Browse files Browse the repository at this point in the history
# Conflicts:
#	nano/secure/CMakeLists.txt
  • Loading branch information
clemahieu committed Sep 30, 2024
1 parent 6b5ad49 commit e73f929
Show file tree
Hide file tree
Showing 29 changed files with 1,006 additions and 260 deletions.
1 change: 1 addition & 0 deletions nano/core_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ add_executable(
timer.cpp
uint256_union.cpp
unchecked_map.cpp
unconfirmed_set.cpp
utility.cpp
vote_cache.cpp
vote_processor.cpp
Expand Down
20 changes: 18 additions & 2 deletions nano/core_test/bootstrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ TEST (bulk_pull, end_not_owned)
nano::test::system system (1);
nano::keypair key2;
system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv);
ASSERT_NE (nullptr, system.wallet (0)->send_action (nano::dev::genesis_key.pub, key2.pub, 100));
auto block = system.wallet (0)->send_action (nano::dev::genesis_key.pub, key2.pub, 100);
ASSERT_NE (nullptr, block);
{
auto transaction = system.nodes[0]->ledger.tx_begin_write ();
system.nodes[0]->ledger.confirm (transaction, block->hash ());
}
nano::block_hash latest (system.nodes[0]->latest (nano::dev::genesis_key.pub));
nano::block_builder builder;
auto open = builder
Expand Down Expand Up @@ -128,6 +133,10 @@ TEST (bulk_pull, ascending_one_hash)
.build ();
node.work_generate_blocking (*block1);
ASSERT_EQ (nano::block_status::progress, node.process (block1));
{
auto transaction = node.ledger.tx_begin_write ();
node.ledger.confirm (transaction, block1->hash ());
}
auto socket = std::make_shared<nano::transport::tcp_socket> (node, nano::transport::socket_endpoint::server);
auto connection = std::make_shared<nano::transport::tcp_server> (socket, system.nodes[0]);
auto req = std::make_unique<nano::bulk_pull> (nano::dev::network_params.network);
Expand Down Expand Up @@ -160,6 +169,10 @@ TEST (bulk_pull, ascending_two_account)
.build ();
node.work_generate_blocking (*block1);
ASSERT_EQ (nano::block_status::progress, node.process (block1));
{
auto transaction = node.ledger.tx_begin_write ();
node.ledger.confirm (transaction, block1->hash ());
}
auto socket = std::make_shared<nano::transport::tcp_socket> (node, nano::transport::socket_endpoint::server);
auto connection = std::make_shared<nano::transport::tcp_server> (socket, system.nodes[0]);
auto req = std::make_unique<nano::bulk_pull> (nano::dev::network_params.network);
Expand Down Expand Up @@ -263,7 +276,10 @@ TEST (bulk_pull, count_limit)
.work (*system.work.generate (send1->hash ()))
.build ();
ASSERT_EQ (nano::block_status::progress, node0->process (receive1));

{
auto transaction = node0->ledger.tx_begin_write ();
node0->ledger.confirm (transaction, receive1->hash ());
}
auto connection (std::make_shared<nano::transport::tcp_server> (std::make_shared<nano::transport::tcp_socket> (*node0, nano::transport::socket_endpoint::server), node0));
auto req = std::make_unique<nano::bulk_pull> (nano::dev::network_params.network);
req->start = receive1->hash ();
Expand Down
63 changes: 47 additions & 16 deletions nano/core_test/ledger.cpp

Large diffs are not rendered by default.

22 changes: 19 additions & 3 deletions nano/core_test/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ TEST (node, send_single)
system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv);
system.wallet (1)->insert_adhoc (key2.prv);
ASSERT_NE (nullptr, system.wallet (0)->send_action (nano::dev::genesis_key.pub, key2.pub, system.nodes[0]->config.receive_minimum.number ()));
ASSERT_EQ (std::numeric_limits<nano::uint128_t>::max () - system.nodes[0]->config.receive_minimum.number (), system.nodes[0]->balance (nano::dev::genesis_key.pub));
ASSERT_TIMELY_EQ (5s, std::numeric_limits<nano::uint128_t>::max () - system.nodes[0]->config.receive_minimum.number (), system.nodes[0]->balance (nano::dev::genesis_key.pub));
ASSERT_TRUE (system.nodes[0]->balance (key2.pub).is_zero ());
ASSERT_TIMELY (10s, !system.nodes[0]->balance (key2.pub).is_zero ());
}
Expand All @@ -162,7 +162,7 @@ TEST (node, send_single_observing_peer)
system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv);
system.wallet (1)->insert_adhoc (key2.prv);
ASSERT_NE (nullptr, system.wallet (0)->send_action (nano::dev::genesis_key.pub, key2.pub, system.nodes[0]->config.receive_minimum.number ()));
ASSERT_EQ (std::numeric_limits<nano::uint128_t>::max () - system.nodes[0]->config.receive_minimum.number (), system.nodes[0]->balance (nano::dev::genesis_key.pub));
ASSERT_TIMELY_EQ (5s, std::numeric_limits<nano::uint128_t>::max () - system.nodes[0]->config.receive_minimum.number (), system.nodes[0]->balance (nano::dev::genesis_key.pub));
ASSERT_TRUE (system.nodes[0]->balance (key2.pub).is_zero ());
ASSERT_TIMELY (10s, std::all_of (system.nodes.begin (), system.nodes.end (), [&] (std::shared_ptr<nano::node> const & node_a) { return !node_a->balance (key2.pub).is_zero (); }));
}
Expand Down Expand Up @@ -197,6 +197,22 @@ TEST (node, send_out_of_order)
node1.process_active (send3);
node1.process_active (send2);
node1.process_active (send1);
ASSERT_TIMELY (5s, std::all_of (system.nodes.begin (), system.nodes.end (), [&] (std::shared_ptr<nano::node> const & node_a) {
auto blocks = { send1, send2, send3 };
return std::all_of (blocks.begin (), blocks.end (), [&] (std::shared_ptr<nano::block> block) {
// return node_a->ledger.unconfirmed (block->hash ());
return false;
});
}));
{
auto transaction = node1.ledger.tx_begin_write ();
node1.ledger.confirm (transaction, send3->hash ());
}
auto & node2 = *system.nodes[1];
{
auto transaction = node2.ledger.tx_begin_write ();
node2.ledger.confirm (transaction, send3->hash ());
}
ASSERT_TIMELY (10s, std::all_of (system.nodes.begin (), system.nodes.end (), [&] (std::shared_ptr<nano::node> const & node_a) { return node_a->balance (nano::dev::genesis_key.pub) == nano::dev::constants.genesis_amount - node1.config.receive_minimum.number () * 3; }));
}

Expand Down Expand Up @@ -3807,4 +3823,4 @@ TEST (node, local_block_broadcast)
node1.network.merge_peer (node2.network.endpoint ());
ASSERT_TIMELY (5s, node1.network.find_node_id (node2.get_node_id ()));
ASSERT_TIMELY (10s, node2.block (send1->hash ()));
}
}
141 changes: 141 additions & 0 deletions nano/core_test/unconfirmed_set.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#include <nano/lib/blockbuilders.hpp>
#include <nano/secure/common.hpp>
#include <nano/store/rocksdb/unconfirmed_set.hpp>

#include <gtest/gtest.h>

TEST (unconfirmed_set, construction)
{
nano::store::unconfirmed_set set;
}

TEST (unconfirmed_set, account_not_exists)
{
nano::store::unconfirmed_set set;
auto tx = set.tx_begin_write ();
nano::account account{ 42 };
ASSERT_FALSE (set.account.exists (tx, account));
ASSERT_FALSE (set.account.get (tx, account).has_value ());
}

TEST (unconfirmed_set, account_exists)
{
nano::store::unconfirmed_set set;
auto tx = set.tx_begin_write ();
nano::account account{ 42 };
nano::account_info info{ 17, 18, 19, 20, 21, 22, nano::epoch::epoch_2 };
set.account.put (tx, account, info);
ASSERT_TRUE (set.account.exists (tx, account));
auto value = set.account.get (tx, account);
ASSERT_TRUE (value.has_value ());
ASSERT_EQ (info, value.value ());
set.account.del (tx, account);
ASSERT_FALSE (set.account.exists (tx, account));
ASSERT_FALSE (set.account.get (tx, account).has_value ());
}

TEST (unconfirmed_set, block_not_exists)
{
nano::store::unconfirmed_set set;
auto tx = set.tx_begin_write ();
nano::block_hash hash{ 42 };
ASSERT_FALSE (set.block.exists (tx, hash));
ASSERT_EQ (nullptr, set.block.get (tx, hash));
}

TEST (unconfirmed_set, block_exists)
{
nano::store::unconfirmed_set set;
auto tx = set.tx_begin_write ();
nano::state_block_builder builder;
auto block = builder.make_block ()
.account (17)
.representative (18)
.previous (19)
.balance (20)
.link (21)
.sign (nano::dev::genesis_key.prv, nano::dev::genesis_key.pub)
.work (22)
.build ();
nano::block_sideband sideband{ 23, 24, 25, 26, 27, nano::block_details{}, nano::epoch::epoch_2 };
block->sideband_set (sideband);
auto hash = block->hash ();
set.block.put (tx, hash, *block);
ASSERT_TRUE (set.block.exists (tx, hash));
auto block2 = set.block.get (tx, hash);
ASSERT_EQ (*block, *block2);
set.block.del (tx, hash);
ASSERT_FALSE (set.block.exists (tx, hash));
ASSERT_EQ (nullptr, set.block.get (tx, hash));
}

TEST (unconfirmed_set, receivable_not_exists)
{
nano::store::unconfirmed_set set;
auto tx = set.tx_begin_write ();
nano::pending_key key{ 42, 43 };
ASSERT_FALSE (set.receivable.exists (tx, key));
ASSERT_FALSE (set.receivable.get (tx, key).has_value ());
}

TEST (unconfirmed_set, receivable_exists)
{
nano::store::unconfirmed_set set;
auto tx = set.tx_begin_write ();
nano::pending_key key{ 42, 43 };
nano::pending_info value{ 44, 45, nano::epoch::epoch_2 };
set.receivable.put (tx, key, value);
ASSERT_TRUE (set.receivable.exists (tx, key));
auto value2 = set.receivable.get (tx, key);
ASSERT_TRUE (value2.has_value ());
ASSERT_EQ (value, value2.value ());
set.receivable.del (tx, key);
ASSERT_FALSE (set.receivable.exists (tx, key));
ASSERT_FALSE (set.receivable.get (tx, key).has_value ());
}

TEST (unconfirmed_set, received_not_exists)
{
nano::store::unconfirmed_set set;
auto tx = set.tx_begin_write ();
nano::pending_key key{ 42, 43 };
ASSERT_FALSE (set.received.exists (tx, key));
}

TEST (unconfirmed_set, received_exists)
{
nano::store::unconfirmed_set set;
auto tx = set.tx_begin_write ();
nano::pending_key key{ 42, 43 };
set.received.put (tx, key);
ASSERT_TRUE (set.received.exists (tx, key));
set.received.del (tx, key);
ASSERT_FALSE (set.received.exists (tx, key));
}

TEST (unconfirmed_set, successor_not_exists)
{
nano::store::unconfirmed_set set;
auto tx = set.tx_begin_write ();
nano::block_hash key{ 42 };
nano::block_hash value{ 43 };
ASSERT_FALSE (set.successor.exists (tx, key));
ASSERT_FALSE (set.successor.get (tx, key).has_value ());
}

TEST (unconfirmed_set, successor_exists)
{
nano::store::unconfirmed_set set;
auto tx = set.tx_begin_write ();
nano::block_hash key{ 42 };
nano::block_hash value{ 43 };
set.successor.put (tx, key, value);
ASSERT_TRUE (set.successor.exists (tx, key));
auto value2 = set.successor.get (tx, key);
ASSERT_TRUE (value2.has_value ());
ASSERT_EQ (value, value2.value ());
set.successor.del (tx, key);
ASSERT_FALSE (set.successor.exists (tx, key));
ASSERT_FALSE (set.successor.get (tx, key).has_value ());
}

5 changes: 5 additions & 0 deletions nano/secure/block_delta.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
#include <optional>
#include <utility>

namespace nano
{
class block;
}

namespace nano
{
class block_delta
Expand Down
Loading

0 comments on commit e73f929

Please sign in to comment.