Skip to content

Commit

Permalink
Merge pull request #190 from pastelnetwork/tickets-validations-improv
Browse files Browse the repository at this point in the history
  • Loading branch information
a-ok123 authored Aug 9, 2023
2 parents d20cf4e + 52f2ba5 commit 5c175a2
Show file tree
Hide file tree
Showing 18 changed files with 104 additions and 36 deletions.
13 changes: 11 additions & 2 deletions src/mnode/mnode-sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ void CMasternodeSync::Reset()
nTimeAssetSyncStarted = GetTime();
nTimeLastBumped = GetTime();
nTimeLastFailure = 0;
nTimeLastProcess = 0;
}

void CMasternodeSync::BumpAssetLastTime(const std::string &strMethodName, const std::string &strFuncName)
Expand Down Expand Up @@ -194,8 +195,7 @@ void CMasternodeSync::ProcessTick()
return;

// reset the sync process if the last call to this function was more than 60 minutes ago (client was in sleep mode)
static int64_t nTimeLastProcess = GetTime();
if (GetTime() - nTimeLastProcess > 60*60)
if (nTimeLastProcess + (60*60) < GetTime())
{
LogFnPrintf("WARNING: no actions for too long, restarting sync...");
Reset();
Expand All @@ -217,6 +217,15 @@ void CMasternodeSync::ProcessTick()
return;
}

// reset sync status if it stuck in Initial state for too long - 15 minutes
if (syncState == MasternodeSyncState::Initial && IsBlockchainSynced() && nTimeAssetSyncStarted + (15*60) > GetTime())
{
LogFnPrintf("WARNING: stuck in Initial for too long, restarting sync...");
Reset();
SwitchToNextAsset();
return;
}

if (IsSynced())
return;

Expand Down
4 changes: 4 additions & 0 deletions src/mnode/mnode-sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class CMasternodeSync
// ... or failed
int64_t nTimeLastFailure;

int64_t nTimeLastProcess;


void Fail();
void ClearFulfilledRequests();

Expand All @@ -65,6 +68,7 @@ class CMasternodeSync
}

bool IsFailed() const noexcept { return syncState == MasternodeSyncState::Failed; }
bool IsInitial() const noexcept { return syncState == MasternodeSyncState::Initial; }
bool IsBlockchainSynced() const noexcept { return syncState > MasternodeSyncState::Waiting; }
bool IsMasternodeListSynced() const noexcept { return syncState > MasternodeSyncState::List; }
bool IsWinnersListSynced() const noexcept { return syncState > MasternodeSyncState::Winners; }
Expand Down
41 changes: 39 additions & 2 deletions src/mnode/ticket-processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -639,8 +639,14 @@ unique_ptr<CPastelTicket> CPastelTicketProcessor::GetTicket(const uint256 &txid)
if (data.nTicketHeight == numeric_limits<uint32_t>::max())
{
// if ticket block height is still not defined - lookup it up in mapBlockIndex by hash
if (mapBlockIndex.count(data.hashBlock) != 0)
data.nTicketHeight = mapBlockIndex[data.hashBlock]->nHeight;
const auto mi = mapBlockIndex.find(data.hashBlock);
if (mi != mapBlockIndex.cend() && mi->second) {
const auto pindex = mi->second;
if (chainActive.Contains(pindex))
{
data.nTicketHeight = pindex->nHeight;
}
}
}

// create Pastel ticket by id
Expand Down Expand Up @@ -1891,3 +1897,34 @@ shared_ptr<ITxMemPoolTracker> CPastelTicketProcessor::GetTxMemPoolTracker()
TicketTxMemPoolTracker = make_shared<CTicketTxMemPoolTracker>();
return TicketTxMemPoolTracker;
}

bool CPastelTicketProcessor::FindTicketTransaction(const std::string& existing_ticket_txid, uint32_t existing_ticket_block_height,
const std::string& new_ticket_txid, uint32_t new_ticket_block_height,
bool bPreReg, std::string &message) {
bool bFound= true;
const uint256 txid = uint256S(existing_ticket_txid);
const auto pTicket = CPastelTicketProcessor::GetTicket(txid);
if (pTicket) {
if (pTicket->GetBlock() == numeric_limits<uint32_t>::max()) {
CTransaction tx;
if (mempool.lookup(txid, tx)) {
message = strprintf("%sfound in mempool. ", message);
} else {
bFound = false;
message = strprintf("%sfound in stale block. ", message);
}
} else {
message = strprintf("%salready exists in blockchain. ", message);
}
} else {
bFound = false;
message = strprintf("%sfound in Ticket DB, but not in blockchain. ", message);
}
message = strprintf("%s [%sfound ticket block=%u, txid=%s]", message,
bPreReg ? "" : strprintf("this ticket block=%u txid=%s; ", new_ticket_block_height, new_ticket_txid),
existing_ticket_block_height, existing_ticket_txid);
if (!bFound) {
LogFnPrintf("WARNING: %s", message);
}
return bFound;
}
4 changes: 4 additions & 0 deletions src/mnode/ticket-processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ class CPastelTicketProcessor
// search for NFT registration tickets, calls functor for each matching ticket
void SearchForNFTs(const search_thumbids_t &p, std::function<size_t(const CPastelTicket *, const nlohmann::json &)> &fnMatchFound) const;

static bool FindTicketTransaction(const std::string& existing_ticket_txid, uint32_t existing_ticket_block_height,
const std::string& new_ticket_txid, uint32_t new_ticket_block_height,
bool bPreReg, std::string &message);

#ifdef ENABLE_WALLET
static bool CreateP2FMSTransaction(const std::string& input_string, CMutableTransaction& tx_out,
const CAmount nPricePSL, const opt_string_t& sFundingAddress, std::string& error_ret);
Expand Down
2 changes: 1 addition & 1 deletion src/mnode/tickets/accept.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ string CAcceptTicket::ToJSON(const bool bDecodeProperties) const noexcept
const json jsonObj
{
{ "txid", m_txid },
{ "height", m_nBlock },
{ "height", static_cast<int32_t>(m_nBlock) },
{ "tx_info", get_txinfo_json() },
{ "ticket",
{
Expand Down
18 changes: 11 additions & 7 deletions src/mnode/tickets/action-act.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include <mnode/tickets/ticket-utils.h>
#ifdef ENABLE_WALLET
#include <wallet/wallet.h>
#include "tinyformat.h"

#endif // ENABLE_WALLET

using json = nlohmann::json;
Expand Down Expand Up @@ -47,7 +49,7 @@ string CActionActivateTicket::ToJSON(const bool bDecodeProperties) const noexcep
const json jsonObj
{
{ "txid", m_txid },
{ "height", m_nBlock },
{ "height", static_cast<int32_t>(m_nBlock)},
{ "tx_info", get_txinfo_json() },
{ "ticket",
{
Expand Down Expand Up @@ -129,12 +131,14 @@ ticket_validation_t CActionActivateTicket::IsValid(const TxOrigin txOrigin, cons
!existingTicket.IsBlock(m_nBlock) ||
!existingTicket.IsTxId(m_txid))
{
tv.errorMsg = strprintf(
"The Activation ticket for the Registration ticket with txid [%s] already exists [%sfound ticket block=%u, txid=%s]",
m_regTicketTxId,
bPreReg ? "" : strprintf("this ticket block=%u txid=%s; ", m_nBlock, m_txid),
existingTicket.m_nBlock, existingTicket.m_txid);
break;
std::string message = strprintf( "The Activation ticket for the Registration ticket with txid [%s] ", m_regTicketTxId);
bool bFound = CPastelTicketProcessor::FindTicketTransaction(existingTicket.m_txid, existingTicket.m_nBlock,
m_txid, m_nBlock,
bPreReg, message);
if (bFound) {
tv.errorMsg = message;
break;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/mnode/tickets/action-reg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ string CActionRegTicket::ToJSON(const bool bDecodeProperties) const noexcept
const json jsonObj
{
{ "txid", m_txid },
{ "height", m_nBlock},
{ "height", static_cast<int32_t>(m_nBlock)},
{ "tx_info", get_txinfo_json() },
{ "ticket",
{
Expand Down
16 changes: 9 additions & 7 deletions src/mnode/tickets/collection-act.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,14 @@ ticket_validation_t CollectionActivateTicket::IsValid(const TxOrigin txOrigin, c
!existingTicket.IsBlock(m_nBlock) ||
!existingTicket.IsTxId(m_txid))
{
tv.errorMsg = strprintf(
"The Activation ticket for the Collection Registration ticket with txid [%s] already exists [%sfound ticket block=%u, txid=%s]",
m_regTicketTxId,
bPreReg ? "" : strprintf("this ticket block=%u txid=%s; ", m_nBlock, m_txid),
existingTicket.m_nBlock, existingTicket.m_txid);
break;
std::string message = strprintf( "The Activation ticket for the Collection Registration ticket with txid [%s] ", m_regTicketTxId);
bool bFound = CPastelTicketProcessor::FindTicketTransaction(existingTicket.m_txid, existingTicket.m_nBlock,
m_txid, m_nBlock,
bPreReg, message);
if (bFound) {
tv.errorMsg = message;
break;
}
}
}

Expand Down Expand Up @@ -223,7 +225,7 @@ string CollectionActivateTicket::ToJSON(const bool bDecodeProperties) const noex
const json jsonObj
{
{ "txid", m_txid },
{ "height", m_nBlock },
{ "height", static_cast<int32_t>(m_nBlock) },
{ "tx_info", get_txinfo_json() },
{ "ticket",
{
Expand Down
2 changes: 1 addition & 1 deletion src/mnode/tickets/collection-reg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ string CollectionRegTicket::ToJSON(const bool bDecodeProperties) const noexcept
const json jsonObj
{
{ "txid", m_txid },
{ "height", m_nBlock },
{ "height", static_cast<int32_t>(m_nBlock) },
{ "tx_info", get_txinfo_json() },
{ "ticket",
{
Expand Down
4 changes: 2 additions & 2 deletions src/mnode/tickets/ethereum-address-change.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ using namespace std;
// CChangeEthereumAddressTicket ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
string CChangeEthereumAddressTicket::ToJSON(const bool bDecodeProperties) const noexcept
{
const json jsonObj =
const json jsonObj =
{
{ "txid", m_txid },
{ "height", m_nBlock },
{ "height", static_cast<int32_t>(m_nBlock) },
{ "tx_info", get_txinfo_json() },
{ "ticket",
{
Expand Down
16 changes: 9 additions & 7 deletions src/mnode/tickets/nft-act.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,14 @@ ticket_validation_t CNFTActivateTicket::IsValid(const TxOrigin txOrigin, const u
!existingTicket.IsBlock(m_nBlock) ||
!existingTicket.IsTxId(m_txid))
{
tv.errorMsg = strprintf(
"The Activation ticket for the Registration ticket with txid [%s] already exists [%sfound ticket block=%u, txid=%s]",
m_regTicketTxId,
bPreReg ? "" : strprintf("this ticket block=%u txid=%s; ", m_nBlock, m_txid),
existingTicket.m_nBlock, existingTicket.m_txid);
break;
std::string message = strprintf( "The Activation ticket for the Registration ticket with txid [%s] ", m_regTicketTxId);
bool bFound = CPastelTicketProcessor::FindTicketTransaction(existingTicket.m_txid, existingTicket.m_nBlock,
m_txid, m_nBlock,
bPreReg, message);
if (bFound) {
tv.errorMsg = message;
break;
}
}
}

Expand Down Expand Up @@ -262,7 +264,7 @@ string CNFTActivateTicket::ToJSON(const bool bDecodeProperties) const noexcept
const json jsonObj
{
{ "txid", m_txid },
{ "height", m_nBlock },
{ "height", static_cast<int32_t>(m_nBlock) },
{ "tx_info", get_txinfo_json() },
{ "ticket",
{
Expand Down
2 changes: 1 addition & 1 deletion src/mnode/tickets/nft-reg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ string CNFTRegTicket::ToJSON(const bool bDecodeProperties) const noexcept
const json jsonObj
{
{ "txid", m_txid },
{ "height", m_nBlock },
{ "height", static_cast<int32_t>(m_nBlock) },
{ "tx_info", get_txinfo_json() },
{ "ticket",
{
Expand Down
2 changes: 1 addition & 1 deletion src/mnode/tickets/nft-royalty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ string CNFTRoyaltyTicket::ToJSON(const bool bDecodeProperties) const noexcept
const json jsonObj
{
{ "txid", m_txid },
{ "height", m_nBlock },
{ "height", static_cast<int32_t>(m_nBlock) },
{ "tx_info", get_txinfo_json() },
{ "ticket",
{
Expand Down
2 changes: 1 addition & 1 deletion src/mnode/tickets/offer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ string COfferTicket::ToJSON(const bool bDecodeProperties) const noexcept
const json jsonObj
{
{ "txid", m_txid },
{ "height", m_nBlock },
{ "height", static_cast<int32_t>(m_nBlock) },
{ "tx_info", get_txinfo_json() },
{ "ticket",
{
Expand Down
2 changes: 1 addition & 1 deletion src/mnode/tickets/pastelid-reg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ string CPastelIDRegTicket::ToJSON(const bool bDecodeProperties) const noexcept
json jsonObj
{
{ "txid", m_txid },
{ "height", m_nBlock },
{ "height", static_cast<int32_t>(m_nBlock) },
{ "tx_info", get_txinfo_json() },
{ "ticket",
{
Expand Down
2 changes: 1 addition & 1 deletion src/mnode/tickets/transfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ string CTransferTicket::ToJSON(const bool bDecodeProperties) const noexcept
const json jsonObj
{
{ "txid", m_txid },
{ "height", m_nBlock },
{ "height", static_cast<int32_t>(m_nBlock) },
{ "tx_info", get_txinfo_json() },
{ "ticket",
{
Expand Down
2 changes: 1 addition & 1 deletion src/mnode/tickets/username-change.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ string CChangeUsernameTicket::ToJSON(const bool bDecodeProperties) const noexcep
const json jsonObj
{
{ "txid", m_txid },
{ "height", m_nBlock },
{ "height", static_cast<int32_t>(m_nBlock) },
{ "tx_info", get_txinfo_json() },
{ "ticket",
{
Expand Down
6 changes: 6 additions & 0 deletions src/sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ void PrintLockContention(const char* pszName, const char* pszFile, int nLine)
}
#endif /* DEBUG_LOCKCONTENTION */

#define ASSERT_ONLY_MAYBE_DEADLOCK 1

#ifdef DEBUG_LOCKORDER
//
// Early deadlock detection.
Expand Down Expand Up @@ -113,7 +115,11 @@ static void potential_deadlock_detected(const pair<void*, void*>& mismatch, cons
}
LogPrintf(" %s\n", lockLocation.ToString());
}
#ifdef ASSERT_ONLY_MAYBE_DEADLOCK
assert(onlyMaybeDeadlock);
#else
cout << "POTENTIAL DEADLOCK DETECTED" << endl;
#endif
}

static void push_lock(void* c, const CLockLocation& locklocation, bool fTry)
Expand Down

0 comments on commit 5c175a2

Please sign in to comment.