Skip to content

Commit

Permalink
Port validation
Browse files Browse the repository at this point in the history
  • Loading branch information
timemarkovqtum committed Mar 20, 2024
1 parent 2def8e8 commit 93fb9d9
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 30 deletions.
14 changes: 7 additions & 7 deletions src/pos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,10 @@ bool CheckBlockInputPubKeyMatchesOutputPubKey(const CBlock& block, CCoinsViewCac
return true;
}

bool CheckRecoveredPubKeyFromBlockSignature(CBlockIndex* pindexPrev, const CBlockHeader& block, CCoinsViewCache& view, CChain& chain) {
bool CheckRecoveredPubKeyFromBlockSignature(CBlockIndex* pindexPrev, const CBlockHeader& block, CCoinsViewCache& view, Chainstate& chainstate) {
Coin coinPrev;
if(!view.GetCoin(block.prevoutStake, coinPrev)){
if(!GetSpentCoinFromMainChain(pindexPrev, block.prevoutStake, &coinPrev, chain)) {
if(!GetSpentCoinFromMainChain(pindexPrev, block.prevoutStake, &coinPrev, chainstate)) {
return error("CheckRecoveredPubKeyFromBlockSignature(): Could not find %s and it was not at the tip", block.prevoutStake.hash.GetHex());
}
}
Expand Down Expand Up @@ -394,21 +394,21 @@ bool CheckRecoveredPubKeyFromBlockSignature(CBlockIndex* pindexPrev, const CBloc
return false;
}

bool CheckKernel(CBlockIndex* pindexPrev, unsigned int nBits, uint32_t nTimeBlock, const COutPoint& prevout, CCoinsViewCache& view, CChain& chain)
bool CheckKernel(CBlockIndex* pindexPrev, unsigned int nBits, uint32_t nTimeBlock, const COutPoint& prevout, CCoinsViewCache& view, Chainstate& chainstate)
{
std::map<COutPoint, CStakeCache> tmp;
return CheckKernel(pindexPrev, nBits, nTimeBlock, prevout, view, tmp, chain);
return CheckKernel(pindexPrev, nBits, nTimeBlock, prevout, view, tmp, chainstate);
}

bool CheckKernel(CBlockIndex* pindexPrev, unsigned int nBits, uint32_t nTimeBlock, const COutPoint& prevout, CCoinsViewCache& view, const std::map<COutPoint, CStakeCache>& cache, CChain& chain)
bool CheckKernel(CBlockIndex* pindexPrev, unsigned int nBits, uint32_t nTimeBlock, const COutPoint& prevout, CCoinsViewCache& view, const std::map<COutPoint, CStakeCache>& cache, Chainstate& chainstate)
{
uint256 hashProofOfStake, targetProofOfStake;
auto it=cache.find(prevout);
if(it == cache.end()) {
//not found in cache (shouldn't happen during staking, only during verification which does not use cache)
Coin coinPrev;
if(!view.GetCoin(prevout, coinPrev)){
if(!GetSpentCoinFromMainChain(pindexPrev, prevout, &coinPrev, chain)) {
if(!GetSpentCoinFromMainChain(pindexPrev, prevout, &coinPrev, chainstate)) {
return error("CheckKernel(): Could not find coin and it was not at the tip");
}
}
Expand All @@ -434,7 +434,7 @@ bool CheckKernel(CBlockIndex* pindexPrev, unsigned int nBits, uint32_t nTimeBloc
if(CheckStakeKernelHash(pindexPrev, nBits, stake.blockFromTime, stake.amount, prevout,
nTimeBlock, hashProofOfStake, targetProofOfStake)){
//Cache could potentially cause false positive stakes in the event of deep reorgs, so check without cache also
return CheckKernel(pindexPrev, nBits, nTimeBlock, prevout, view, chain);
return CheckKernel(pindexPrev, nBits, nTimeBlock, prevout, view, chainstate);
}
}
return false;
Expand Down
6 changes: 3 additions & 3 deletions src/pos.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ inline bool CheckCoinStakeTimestamp(uint32_t nTimeBlock, int nHeight, const Cons
bool CheckBlockInputPubKeyMatchesOutputPubKey(const CBlock& block, CCoinsViewCache& view, bool delegateOutputExist);

// Recover the pubkey and check that it matches the prevoutStake's scriptPubKey.
bool CheckRecoveredPubKeyFromBlockSignature(CBlockIndex* pindexPrev, const CBlockHeader& block, CCoinsViewCache& view, CChain& chain);
bool CheckRecoveredPubKeyFromBlockSignature(CBlockIndex* pindexPrev, const CBlockHeader& block, CCoinsViewCache& view, Chainstate& chainstate);

// Wrapper around CheckStakeKernelHash()
// Also checks existence of kernel input and min age
// Convenient for searching a kernel
bool CheckKernel(CBlockIndex* pindexPrev, unsigned int nBits, uint32_t nTimeBlock, const COutPoint& prevout, CCoinsViewCache& view, CChain& chain);
bool CheckKernel(CBlockIndex* pindexPrev, unsigned int nBits, uint32_t nTimeBlock, const COutPoint& prevout, CCoinsViewCache& view, const std::map<COutPoint, CStakeCache>& cache, CChain& chain);
bool CheckKernel(CBlockIndex* pindexPrev, unsigned int nBits, uint32_t nTimeBlock, const COutPoint& prevout, CCoinsViewCache& view, Chainstate& chainstate);
bool CheckKernel(CBlockIndex* pindexPrev, unsigned int nBits, uint32_t nTimeBlock, const COutPoint& prevout, CCoinsViewCache& view, const std::map<COutPoint, CStakeCache>& cache, Chainstate& chainstate);
bool CheckKernelCache(CBlockIndex* pindexPrev, unsigned int nBits, uint32_t nTimeBlock, const COutPoint& prevout, const std::map<COutPoint, CStakeCache>& cache, uint256& hashProofOfStake);

unsigned int GetStakeMaxCombineInputs();
Expand Down
42 changes: 42 additions & 0 deletions src/txmempool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,48 @@ std::vector<CTxMemPool::txiter> CTxMemPool::GatherClusters(const std::vector<uin
/////////////////////////////////////////////////////// // qtum
void CTxMemPool::addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewCache &view)
{
LOCK(cs);
const CTransaction& tx = entry.GetTx();
std::vector<CMempoolAddressDeltaKey> inserted;

uint256 txhash = tx.GetHash();
for (unsigned int j = 0; j < tx.vin.size(); j++) {
const CTxIn input = tx.vin[j];
const CTxOut &prevout = view.GetOutputFor(input);

CTxDestination dest;
if (ExtractDestination(input.prevout, prevout.scriptPubKey, dest)) {
valtype bytesID(std::visit(DataVisitor(), dest));
if(bytesID.empty()) {
continue;
}
valtype addressBytes(32);
std::copy(bytesID.begin(), bytesID.end(), addressBytes.begin());
CMempoolAddressDeltaKey key(dest.index(), uint256(addressBytes), txhash, j, 1);
CMempoolAddressDelta delta(entry.GetTime().count(), prevout.nValue * -1, input.prevout.hash, input.prevout.n);
mapAddress.insert(std::make_pair(key, delta));
inserted.push_back(key);
}
}

for (unsigned int k = 0; k < tx.vout.size(); k++) {
const CTxOut &out = tx.vout[k];

CTxDestination dest;
if (ExtractDestination({tx.GetHash(), k}, out.scriptPubKey, dest)) {
valtype bytesID(std::visit(DataVisitor(), dest));
if(bytesID.empty()) {
continue;
}
valtype addressBytes(32);
std::copy(bytesID.begin(), bytesID.end(), addressBytes.begin());
CMempoolAddressDeltaKey key(dest.index(), uint256(addressBytes), txhash, k, 0);
mapAddress.insert(std::make_pair(key, CMempoolAddressDelta(entry.GetTime().count(), out.nValue)));
inserted.push_back(key);
}
}

mapAddressInserted.insert(std::make_pair(txhash, inserted));
}

bool CTxMemPool::getAddressIndex(std::vector<std::pair<uint256, int> > &addresses, std::vector<std::pair<CMempoolAddressDeltaKey, CMempoolAddressDelta> > &results)
Expand Down
Loading

0 comments on commit 93fb9d9

Please sign in to comment.