From 21482a3f8ab77b064e63c8d1972ca1dc41a2d078 Mon Sep 17 00:00:00 2001 From: Chalith Desaman Date: Thu, 17 Dec 2020 17:19:27 +0530 Subject: [PATCH] Input nonce expiry based on max ledger sequence no. (#197) --- src/usr/input_nonce_map.cpp | 11 ++++++----- src/usr/input_nonce_map.hpp | 2 +- src/usr/usr.cpp | 4 ++-- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/usr/input_nonce_map.cpp b/src/usr/input_nonce_map.cpp index e07ea767..f6bb22a5 100644 --- a/src/usr/input_nonce_map.cpp +++ b/src/usr/input_nonce_map.cpp @@ -1,5 +1,6 @@ #include "../pchheader.hpp" #include "../util/util.hpp" +#include "../ledger.hpp" #include "input_nonce_map.hpp" namespace usr @@ -14,7 +15,7 @@ namespace usr * 1 if nonce has expired. * 2 if message with same nonce/sig has already been submitted. */ - int input_nonce_map::check(const std::string &pubkey, const std::string &nonce, const std::string &sig, const bool no_add) + int input_nonce_map::check(const std::string &pubkey, const std::string &nonce, const std::string &sig, const uint64_t &max_lcl_seqno, const bool no_add) { int result = 0; @@ -24,20 +25,20 @@ namespace usr { result = 0; if (!no_add) - nonce_map.emplace(pubkey, std::tuple(nonce, sig, (util::get_epoch_milliseconds() + TTL))); + nonce_map.emplace(pubkey, std::tuple(nonce, sig, max_lcl_seqno)); } else { const std::string &existing_nonce = std::get<0>(itr->second); - const uint64_t expire_on = std::get<2>(itr->second); + const uint64_t expire_lcl_seqno = std::get<2>(itr->second); // Check if previous nonce has already expired or it is less than new nonce. - if (expire_on <= now || existing_nonce < nonce) + if (expire_lcl_seqno <= ledger::ctx.get_seq_no() || existing_nonce < nonce) { if (!no_add) { std::get<0>(itr->second) = nonce; - std::get<2>(itr->second) = now + TTL; + std::get<2>(itr->second) = max_lcl_seqno; } result = 0; } diff --git a/src/usr/input_nonce_map.hpp b/src/usr/input_nonce_map.hpp index e77d4793..dfd01bae 100644 --- a/src/usr/input_nonce_map.hpp +++ b/src/usr/input_nonce_map.hpp @@ -13,7 +13,7 @@ namespace usr void cleanup(); public: - int check(const std::string &pubkey, const std::string &nonce, const std::string &sig, const bool no_add = false); + int check(const std::string &pubkey, const std::string &nonce, const std::string &sig, const uint64_t &max_lcl_seqno, const bool no_add = false); }; } // namespace usr diff --git a/src/usr/usr.cpp b/src/usr/usr.cpp index de4bcfb6..dedd7e6c 100644 --- a/src/usr/usr.cpp +++ b/src/usr/usr.cpp @@ -158,7 +158,7 @@ namespace usr uint64_t max_lcl_seqno; if (parser.extract_input_container(input_data, nonce, max_lcl_seqno, input_container) != -1) { - const int nonce_status = nonce_map.check(user.pubkey, nonce, sig, true); + const int nonce_status = nonce_map.check(user.pubkey, nonce, sig, max_lcl_seqno, true); if (nonce_status == 0) { //Add to the submitted input list. @@ -321,7 +321,7 @@ namespace usr return msg::usrmsg::REASON_MAX_LEDGER_EXPIRED; } - const int nonce_status = nonce_map.check(user_pubkey, nonce, umsg.sig); + const int nonce_status = nonce_map.check(user_pubkey, nonce, umsg.sig, max_lcl_seqno); if (nonce_status > 0) { LOG_DEBUG << (nonce_status == 1 ? "User message nonce expired." : "User message with same nonce/sig already submitted.");