Add functions to allow pending_key objects to be in containers. (#4489)

This commit is contained in:
clemahieu 2024-03-14 15:00:18 +00:00 committed by GitHub
commit 7523172634
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 32 additions and 0 deletions

View file

@ -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>
@ -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));
}

View file

@ -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;
}

View file

@ -28,8 +28,21 @@ public:
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 });
}
};
}