Skip to content

Commit

Permalink
Add functions to allow pending_key objects to be in containers.
Browse files Browse the repository at this point in the history
  • Loading branch information
clemahieu committed Mar 14, 2024
1 parent da4e9eb commit d5c0b02
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
14 changes: 14 additions & 0 deletions nano/core_test/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <nano/lib/thread_pool.hpp>
#include <nano/lib/timer.hpp>
#include <nano/lib/utility.hpp>
#include <nano/secure/pending_info.hpp>
#include <nano/secure/utility.hpp>

#include <gtest/gtest.h>
Expand Down Expand Up @@ -346,3 +347,16 @@ TEST (relaxed_atomic_integral, many_threads)
// Check values
ASSERT_EQ (0, atomic);
}

TEST (pending_key, sorting)
{
nano::pending_key one{ 1, 2 };
nano::pending_key two{ 1, 3 };
nano::pending_key three{ 2, 1 };
ASSERT_LT (one, two);
ASSERT_LT (one, three);
ASSERT_LT (two, three);
nano::pending_key one_same{ 1, 2 };
ASSERT_EQ (std::hash<nano::pending_key>{}(one), std::hash<nano::pending_key>{}(one_same));
ASSERT_NE (std::hash<nano::pending_key>{}(one), std::hash<nano::pending_key>{}(two));
}
5 changes: 5 additions & 0 deletions nano/secure/pending_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,8 @@ nano::account const & nano::pending_key::key () const
{
return account;
}

bool nano::pending_key::operator< (nano::pending_key const & other_a) const
{
return account == other_a.account ? hash < other_a.hash : account < other_a.account;
}
13 changes: 13 additions & 0 deletions nano/secure/pending_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,21 @@ class pending_key final
pending_key (nano::account const &, nano::block_hash const &);
bool deserialize (nano::stream &);
bool operator== (nano::pending_key const &) const;
bool operator< (nano::pending_key const &) const;
nano::account const & key () const;
nano::account account{};
nano::block_hash hash{ 0 };
};
} // namespace nano

namespace std
{
template <>
struct hash<::nano::pending_key>
{
size_t operator() (::nano::pending_key const & data_a) const
{
return hash<::nano::uint512_union>{}({ ::nano::uint256_union{ data_a.account.number () }, data_a.hash });
}
};
}

0 comments on commit d5c0b02

Please sign in to comment.