Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent SPV closing sockets in use #1398

Merged
merged 5 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 6 additions & 24 deletions src/masternodes/anchors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ void CAnchorAuthIndex::PruneOlderThan(THeight height)
}

CAnchorIndex::CAnchorIndex(size_t nCacheSize, bool fMemory, bool fWipe)
: db(new CDBWrapper(GetDataDir() / "anchors", nCacheSize, fMemory, fWipe))
: db(std::make_unique<CDBWrapper>(GetDataDir() / "anchors", nCacheSize, fMemory, fWipe))
{
}

Expand Down Expand Up @@ -522,8 +522,7 @@ void CAnchorIndex::CheckPendingAnchors()
continue;
}

const auto timestamp = spv::pspv->ReadTxTimestamp(rec.txHash);
const auto blockHeight = spv::pspv->ReadTxBlockHeight(rec.txHash);
const auto [blockHeight, timestamp] = spv::pspv->ReadTxHeightTime(rec.txHash);
const auto blockHash = panchors->ReadBlockHash(rec.btcHeight);

// Do not delete, TX time still pending. If block height is set to max we cannot trust the timestamp.
Expand Down Expand Up @@ -669,15 +668,10 @@ bool CAnchorIndex::ActivateBestAnchor(bool forced)
return top != oldTop;
}

bool CAnchorIndex::AddToAnchorPending(CAnchor const & anchor, uint256 const & btcTxHash, THeight btcBlockHeight, bool overwrite)
bool CAnchorIndex::AddToAnchorPending(const AnchorRec& rec)
{
AssertLockHeld(cs_main);

AnchorRec rec{ anchor, btcTxHash, btcBlockHeight };
if (overwrite) {
DeletePendingByBtcTx(btcTxHash);
}

return db->Write(std::make_pair(DB_PENDING, rec.txHash), rec);
}

Expand All @@ -688,20 +682,13 @@ bool CAnchorIndex::GetPendingByBtcTx(uint256 const & txHash, AnchorRec & rec) co
return db->Read(std::make_pair(DB_PENDING, txHash), rec);
}

bool CAnchorIndex::DeletePendingByBtcTx(uint256 const & btcTxHash)
void CAnchorIndex::DeletePendingByBtcTx(uint256 const & btcTxHash)
{
AssertLockHeld(cs_main);

AnchorRec pending;

if (GetPendingByBtcTx(btcTxHash, pending)) {
if (db->Exists(std::make_pair(DB_PENDING, btcTxHash))) {
db->Erase(std::make_pair(DB_PENDING, btcTxHash));
}
return true;
if (db->Exists(std::make_pair(DB_PENDING, btcTxHash))) {
db->Erase(std::make_pair(DB_PENDING, btcTxHash));
}

return false;
}

bool CAnchorIndex::WriteBlock(const uint32_t height, const uint256& blockHash)
Expand Down Expand Up @@ -729,11 +716,6 @@ bool CAnchorIndex::DbExists(const uint256 & hash) const
return db->Exists(std::make_pair(DB_ANCHORS, hash));
}

bool CAnchorIndex::DbRead(uint256 const & hash, AnchorRec & rec) const
{
return db->Read(std::make_pair(DB_ANCHORS, hash), rec);
}

bool CAnchorIndex::DbWrite(AnchorRec const & rec)
{
return db->Write(std::make_pair(DB_ANCHORS, rec.txHash), rec);
Expand Down
7 changes: 3 additions & 4 deletions src/masternodes/anchors.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class CAnchorAuthIndex
class CAnchorIndex
{
private:
std::shared_ptr<CDBWrapper> db;
std::unique_ptr<CDBWrapper> db;
std::unique_ptr<CDBBatch> batch;
public:
using Signature = std::vector<unsigned char>;
Expand Down Expand Up @@ -247,9 +247,9 @@ class CAnchorIndex
void UpdateLastHeight(uint32_t height);

// Post-fork anchor pending, requires chain context to validate. Some pending may be bogus, intentional or not.
bool AddToAnchorPending(CAnchor const & anchor, uint256 const & btcTxHash, THeight btcBlockHeight, bool overwrite = false);
bool AddToAnchorPending(const AnchorRec& rec);
bool GetPendingByBtcTx(uint256 const & txHash, AnchorRec & rec) const;
bool DeletePendingByBtcTx(uint256 const & btcTxHash);
void DeletePendingByBtcTx(uint256 const & btcTxHash);
void ForEachPending(std::function<void (const uint256 &, AnchorRec &)> callback);

// Used to apply chain context to post-fork anchors which get added to pending.
Expand Down Expand Up @@ -297,7 +297,6 @@ class CAnchorIndex
}

bool DbExists(uint256 const & hash) const;
bool DbRead(uint256 const & hash, AnchorRec & anchor) const;
bool DbWrite(AnchorRec const & anchor);
bool DbErase(uint256 const & hash);
};
Expand Down
Loading