From 752317263499aca6ec54119c308267df33173f73 Mon Sep 17 00:00:00 2001 From: clemahieu Date: Thu, 14 Mar 2024 15:00:18 +0000 Subject: [PATCH] Add functions to allow pending_key objects to be in containers. (#4489) --- nano/core_test/utility.cpp | 14 ++++++++++++++ nano/secure/pending_info.cpp | 5 +++++ nano/secure/pending_info.hpp | 13 +++++++++++++ 3 files changed, 32 insertions(+) diff --git a/nano/core_test/utility.cpp b/nano/core_test/utility.cpp index 1bd6a50c2..730714dc8 100644 --- a/nano/core_test/utility.cpp +++ b/nano/core_test/utility.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -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{}(one), std::hash{}(one_same)); + ASSERT_NE (std::hash{}(one), std::hash{}(two)); +} diff --git a/nano/secure/pending_info.cpp b/nano/secure/pending_info.cpp index 24aa14b3a..637f4adf7 100644 --- a/nano/secure/pending_info.cpp +++ b/nano/secure/pending_info.cpp @@ -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; +} diff --git a/nano/secure/pending_info.hpp b/nano/secure/pending_info.hpp index 4e6946039..584337e95 100644 --- a/nano/secure/pending_info.hpp +++ b/nano/secure/pending_info.hpp @@ -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 }); + } +}; +}