Input nonce expiry based on max ledger sequence no. (#197)

This commit is contained in:
Chalith Desaman
2020-12-17 17:19:27 +05:30
committed by GitHub
parent 229af3e294
commit 21482a3f8a
3 changed files with 9 additions and 8 deletions

View File

@@ -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<std::string, std::string, uint64_t>(nonce, sig, (util::get_epoch_milliseconds() + TTL)));
nonce_map.emplace(pubkey, std::tuple<std::string, std::string, uint64_t>(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;
}

View File

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

View File

@@ -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.");