Skip to content

Commit

Permalink
Port validation
Browse files Browse the repository at this point in the history
  • Loading branch information
timemarkovqtum committed Jul 12, 2024
1 parent 1e5d033 commit a3bfc16
Show file tree
Hide file tree
Showing 6 changed files with 734 additions and 21 deletions.
5 changes: 5 additions & 0 deletions src/node/caches.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <common/args.h>
#include <index/txindex.h>
#include <txdb.h>
#include <validation.h>

namespace node {
CacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes)
Expand All @@ -16,6 +17,10 @@ CacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes)
nTotalCache = std::min(nTotalCache, nMaxDbCache << 20); // total cache cannot be greater than nMaxDbcache
CacheSizes sizes;
sizes.block_tree_db = std::min(nTotalCache / 8, nMaxBlockDBCache << 20);
if (args.GetBoolArg("-addrindex", DEFAULT_ADDRINDEX)) {
// enable 3/4 of the cache if addressindex and/or spentindex is enabled
sizes.block_tree_db = nTotalCache * 3 / 4;
}
nTotalCache -= sizes.block_tree_db;
sizes.tx_index = std::min(nTotalCache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? nMaxTxIndexCache << 20 : 0);
nTotalCache -= sizes.tx_index;
Expand Down
20 changes: 18 additions & 2 deletions src/node/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef t
const MempoolAcceptResult result = node.chainman->ProcessTransaction(tx, /*test_accept=*/ true);
if (result.m_result_type != MempoolAcceptResult::ResultType::VALID) {
return HandleATMPError(result.m_state, err_string);
} else if (result.m_base_fees.value() > max_tx_fee) {
} else if (!tx->HasCreateOrCall() && result.m_base_fees.value() > max_tx_fee) {
return TransactionError::MAX_FEE_EXCEEDED;
}
}
Expand Down Expand Up @@ -122,7 +122,7 @@ TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef t
return TransactionError::OK;
}

CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMemPool* const mempool, const uint256& hash, uint256& hashBlock, const BlockManager& blockman)
CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMemPool* const mempool, const uint256& hash, uint256& hashBlock, const BlockManager& blockman, Chainstate* chainstate)
{
if (mempool && !block_index) {
CTransactionRef ptx = mempool->get(hash);
Expand Down Expand Up @@ -152,6 +152,22 @@ CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMe
}
}
}
if (chainstate) { // use coin database to locate block that contains transaction, and scan it
CBlockIndex* pindexSlow = nullptr;
const Coin& coin = AccessByTxid(chainstate->CoinsTip(), Txid::FromUint256(hash));
if (!coin.IsSpent()) pindexSlow = chainstate->m_chain[coin.nHeight];
if (pindexSlow) {
CBlock block;
if (blockman.ReadBlockFromDisk(block, *pindexSlow)) {
for (const auto& tx : block.vtx) {
if (tx->GetHash() == hash) {
hashBlock = pindexSlow->GetBlockHash();
return tx;
}
}
}
}
}
return nullptr;
}
} // namespace node
5 changes: 3 additions & 2 deletions src/node/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class CTxMemPool;
namespace Consensus {
struct Params;
}
class Chainstate;

namespace node {
class BlockManager;
Expand All @@ -24,7 +25,7 @@ struct NodeContext;
* By default, a transaction with a fee rate higher than this will be rejected
* by these RPCs and the GUI. This can be overridden with the maxfeerate argument.
*/
static const CFeeRate DEFAULT_MAX_RAW_TX_FEE_RATE{COIN / 10};
static const CFeeRate DEFAULT_MAX_RAW_TX_FEE_RATE{1 * COIN};

/**
* Submit a transaction to the mempool and (optionally) relay it to all P2P peers.
Expand Down Expand Up @@ -57,7 +58,7 @@ static const CFeeRate DEFAULT_MAX_RAW_TX_FEE_RATE{COIN / 10};
* @param[out] hashBlock The block hash, if the tx was found via -txindex or block_index
* @returns The tx if found, otherwise nullptr
*/
CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMemPool* const mempool, const uint256& hash, uint256& hashBlock, const BlockManager& blockman);
CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMemPool* const mempool, const uint256& hash, uint256& hashBlock, const BlockManager& blockman, Chainstate* chainstate = nullptr);
} // namespace node

#endif // BITCOIN_NODE_TRANSACTION_H
12 changes: 9 additions & 3 deletions src/timedata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <sync.h>
#include <tinyformat.h>
#include <util/translation.h>
#include <util/time.h>
#include <warnings.h>

static GlobalMutex g_timeoffset_mutex;
Expand Down Expand Up @@ -80,17 +81,17 @@ void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample)
nTimeOffset = 0;

if (!g_warning_emitted) {
// If nobody has a time different than ours but within 5 minutes of ours, give a warning
// If nobody has a time different than ours but within 16 seconds of ours, give a warning
bool fMatch = false;
for (const int64_t nOffset : vSorted) {
if (nOffset != 0 && nOffset > -5 * 60 && nOffset < 5 * 60) fMatch = true;
if (nOffset != 0 && nOffset > -16 && nOffset < 16) fMatch = true;
}

if (!fMatch) {
g_warning_emitted = true;
bilingual_str strMessage = strprintf(_("Please check that your computer's date and time are correct! If your clock is wrong, %s will not work properly."), PACKAGE_NAME);
SetMiscWarning(strMessage);
uiInterface.ThreadSafeMessageBox(strMessage, "", CClientUIInterface::MSG_WARNING);
uiInterface.ThreadSafeMessageBox(strMessage, "", CClientUIInterface::ICON_WARNING);
}
}
}
Expand All @@ -114,3 +115,8 @@ void TestOnlyResetTimeData()
g_time_offsets = CMedianFilter<int64_t>{BITCOIN_TIMEDATA_MAX_SAMPLES, 0};
g_warning_emitted = false;
}

int64_t GetAdjustedTimeSeconds()
{
return TicksSinceEpoch<std::chrono::seconds>(NodeClock::now());
}
4 changes: 3 additions & 1 deletion src/timedata.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <cstdint>
#include <vector>

static const int64_t DEFAULT_MAX_TIME_ADJUSTMENT = 70 * 60;
static const int64_t DEFAULT_MAX_TIME_ADJUSTMENT = 0;

class CNetAddr;

Expand Down Expand Up @@ -79,4 +79,6 @@ void AddTimeData(const CNetAddr& ip, int64_t nTime);
*/
void TestOnlyResetTimeData();

int64_t GetAdjustedTimeSeconds();

#endif // BITCOIN_TIMEDATA_H
Loading

0 comments on commit a3bfc16

Please sign in to comment.