Skip to content

Commit

Permalink
Prevent SPV closing sockets in use (#1398)
Browse files Browse the repository at this point in the history
* Prevent closing of sockets in use

* Restore time include

* Restore previous behaviour

Co-authored-by: Prasanna Loganathar <[email protected]>
  • Loading branch information
Bushstar and prasannavl authored Aug 2, 2022
1 parent 8d44594 commit 2247f0f
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 278 deletions.
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

0 comments on commit 2247f0f

Please sign in to comment.