Skip to content

Commit

Permalink
Merge pull request #4794 from pwojcikdev/bucketing
Browse files Browse the repository at this point in the history
Bucketing component
  • Loading branch information
pwojcikdev authored Nov 27, 2024
2 parents 238a7fa + 6392763 commit 7855c82
Show file tree
Hide file tree
Showing 31 changed files with 639 additions and 125 deletions.
2 changes: 2 additions & 0 deletions nano/core_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ add_executable(
blockprocessor.cpp
bootstrap.cpp
bootstrap_server.cpp
bucketing.cpp
cli.cpp
confirmation_solicitor.cpp
confirming_set.cpp
Expand All @@ -26,6 +27,7 @@ add_executable(
ipc.cpp
ledger.cpp
ledger_confirm.cpp
ledger_priority.cpp
locks.cpp
logging.cpp
message.cpp
Expand Down
1 change: 1 addition & 0 deletions nano/core_test/active_elections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ TEST (inactive_votes_cache, election_start)
node.vote_processor.vote (vote2, std::make_shared<nano::transport::inproc::channel> (node, node));
// Only election for send1 should start, other blocks are missing dependencies and don't have enough final weight
ASSERT_TIMELY_EQ (5s, 1, node.active.size ());
ASSERT_TRUE (node.vote_router.contains (send1->hash ()));
ASSERT_TRUE (node.vote_router.active (send1->hash ()));

// Confirm elections with weight quorum
Expand Down
55 changes: 55 additions & 0 deletions nano/core_test/bucketing.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <nano/node/bucketing.hpp>

#include <gtest/gtest.h>

#include <algorithm>

TEST (bucketing, construction)
{
nano::bucketing bucketing;
ASSERT_EQ (63, bucketing.size ());
}

TEST (bucketing, zero_index)
{
nano::bucketing bucketing;
ASSERT_EQ (0, bucketing.bucket_index (0));
}

TEST (bucketing, raw_index)
{
nano::bucketing bucketing;
ASSERT_EQ (0, bucketing.bucket_index (nano::raw_ratio));
}

TEST (bucketing, nano_index)
{
nano::bucketing bucketing;
ASSERT_EQ (14, bucketing.bucket_index (nano::nano_ratio));
}

TEST (bucketing, Knano_index)
{
nano::bucketing bucketing;
ASSERT_EQ (49, bucketing.bucket_index (nano::Knano_ratio));
}

TEST (bucketing, max_index)
{
nano::bucketing bucketing;
ASSERT_EQ (62, bucketing.bucket_index (std::numeric_limits<nano::amount::underlying_type>::max ()));
}

TEST (bucketing, indices)
{
nano::bucketing bucketing;
auto indices = bucketing.bucket_indices ();
ASSERT_EQ (63, indices.size ());
ASSERT_EQ (indices.size (), bucketing.size ());

// Check that the indices are in ascending order
ASSERT_TRUE (std::adjacent_find (indices.begin (), indices.end (), [] (auto const & lhs, auto const & rhs) {
return lhs >= rhs;
})
== indices.end ());
}
10 changes: 8 additions & 2 deletions nano/core_test/election_scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,7 @@ TEST (election_scheduler_bucket, construction)
auto & node = *system.add_node ();

nano::scheduler::priority_bucket_config bucket_config;
nano::scheduler::bucket bucket{ nano::Knano_ratio, bucket_config, node.active, node.stats };
ASSERT_EQ (nano::Knano_ratio, bucket.minimum_balance);
nano::scheduler::bucket bucket{ 0, bucket_config, node.active, node.stats };
ASSERT_TRUE (bucket.empty ());
ASSERT_EQ (0, bucket.size ());
}
Expand All @@ -269,7 +268,9 @@ TEST (election_scheduler_bucket, insert_one)

nano::scheduler::priority_bucket_config bucket_config;
nano::scheduler::bucket bucket{ 0, bucket_config, node.active, node.stats };
ASSERT_FALSE (bucket.contains (block0 ()->hash ()));
ASSERT_TRUE (bucket.push (1000, block0 ()));
ASSERT_TRUE (bucket.contains (block0 ()->hash ()));
ASSERT_FALSE (bucket.empty ());
ASSERT_EQ (1, bucket.size ());
auto blocks = bucket.blocks ();
Expand Down Expand Up @@ -320,10 +321,15 @@ TEST (election_scheduler_bucket, max_blocks)
};
nano::scheduler::bucket bucket{ 0, bucket_config, node.active, node.stats };
ASSERT_TRUE (bucket.push (2000, block0 ()));
ASSERT_TRUE (bucket.contains (block0 ()->hash ()));
ASSERT_TRUE (bucket.push (900, block1 ()));
ASSERT_TRUE (bucket.contains (block1 ()->hash ()));
ASSERT_FALSE (bucket.push (3000, block2 ()));
ASSERT_FALSE (bucket.contains (block2 ()->hash ()));
ASSERT_TRUE (bucket.push (1001, block3 ())); // Evicts 2000
ASSERT_FALSE (bucket.contains (block0 ()->hash ()));
ASSERT_TRUE (bucket.push (1000, block0 ())); // Evicts 1001
ASSERT_FALSE (bucket.contains (block3 ()->hash ()));
ASSERT_EQ (2, bucket.size ());
auto blocks = bucket.blocks ();
ASSERT_EQ (2, blocks.size ());
Expand Down
Loading

0 comments on commit 7855c82

Please sign in to comment.