Skip to content

Commit

Permalink
Merge dashpay#6370: backport: merge bitcoin#23499, bitcoin#23832, bit…
Browse files Browse the repository at this point in the history
…coin#24136, bitcoin#24265, bitcoin#23819, bitcoin#23595, bitcoin#24486, bitcoin#15423, bitcoin#24784, bitcoin#24956, bitcoin#25016, bitcoin#25060 (auxiliary backports: part 21)

0232003 merge bitcoin#25060: add LIFETIMEBOUND to GetFirstStoredBlock()::start_time (Kittywhiskers Van Gogh)
cfda9e5 merge bitcoin#25016: GetFirstStoredBlock() and getblockchaininfo follow-ups (Kittywhiskers Van Gogh)
99d028e merge bitcoin#24956: Call CHECK_NONFATAL only once where needed (Kittywhiskers Van Gogh)
87b556d refactor: use loop to iterate through `SoftForkDescPushBack` args (Kittywhiskers Van Gogh)
80615b8 merge bitcoin#24784: deduplicate integer serialization in RollingBloom benchmark (Kittywhiskers Van Gogh)
092a11e merge bitcoin#15423: Query Tor for correct -onion configuration (Kittywhiskers Van Gogh)
74dceb3 merge bitcoin#24486: dedup sqlite blob binding (Kittywhiskers Van Gogh)
aa945c2 merge bitcoin#23595: Add ParseHex<std::byte>() helper (Kittywhiskers Van Gogh)
c2defe7 merge bitcoin#23819: don't serialize block hash twice (Kittywhiskers Van Gogh)
9a8d6e1 merge bitcoin#24265: Drop StripRedundantLastElementsOfPath() function (Kittywhiskers Van Gogh)
2d299f1 merge bitcoin#24136: Extract CTxIn::MAX_SEQUENCE_NONFINAL constant, rework BIP 65/68/112 docs (Kittywhiskers Van Gogh)
9aacee7 merge bitcoin#23832: Changes time variables from int to chrono (Kittywhiskers Van Gogh)
06bf649 merge bitcoin#23499: Add interfaces::Node::broadCastTransaction method (Kittywhiskers Van Gogh)

Pull request description:

  ## Breaking Changes

  None expected.

  ## Checklist

  - [x] I have performed a self-review of my own code
  - [x] I have commented my code, particularly in hard-to-understand areas **(note: N/A)**
  - [x] I have added or updated relevant unit/integration/functional/e2e tests
  - [x] I have made corresponding changes to the documentation **(note: N/A)**
  - [x] I have assigned this pull request to a milestone _(for repository code-owners and collaborators only)_

ACKs for top commit:
  UdjinM6:
    utACK 0232003

Tree-SHA512: b633e1caac95e565d43e28cef7c915f65952fbabdf55624090bc587c75b84fc04474bb6cd8625219ba828da172391c3d2d1aa6db998927312055c82caaad7ba0
  • Loading branch information
PastaPastaPasta committed Oct 30, 2024
2 parents 88a8e7a + 0232003 commit 5dbe58c
Show file tree
Hide file tree
Showing 32 changed files with 350 additions and 179 deletions.
2 changes: 1 addition & 1 deletion src/index/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ bool BaseIndex::Init()
if (!m_best_block_index) {
// index is not built yet
// make sure we have all block data back to the genesis
prune_violation = GetFirstStoredBlock(active_chain.Tip()) != active_chain.Genesis();
prune_violation = m_chainstate->m_blockman.GetFirstStoredBlock(*active_chain.Tip()) != active_chain.Genesis();
}
// in case the index has a best block set and is not fully synced
// check if we have the required blocks to continue building the index
Expand Down
2 changes: 1 addition & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1470,7 +1470,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
LogPrintf("Using at most %i automatic connections (%i file descriptors available)\n", nMaxConnections, nFD);

// Warn about relative -datadir path.
if (args.IsArgSet("-datadir") && !fs::PathFromString(args.GetArg("-datadir", "")).is_absolute()) {
if (args.IsArgSet("-datadir") && !args.GetPathArg("-datadir").is_absolute()) {
LogPrintf("Warning: relative datadir option '%s' specified, which will be interpreted relative to the " /* Continued */
"current working directory '%s'. This is fragile, because if Dash Core is started in the future "
"from a different location, it will be unable to locate the current data files. There could "
Expand Down
4 changes: 4 additions & 0 deletions src/interfaces/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class UniValue;
class Proxy;
struct bilingual_str;
enum class SynchronizationState;
enum class TransactionError;
struct CNodeStateStats;
struct NodeContext;

Expand Down Expand Up @@ -264,6 +265,9 @@ class Node
//! Get unspent outputs associated with a transaction.
virtual bool getUnspentOutput(const COutPoint& output, Coin& coin) = 0;

//! Broadcast transaction.
virtual TransactionError broadcastTransaction(CTransactionRef tx, CAmount max_tx_fee, bilingual_str& err_string) = 0;

//! Get wallet loader.
virtual WalletLoader& walletLoader() = 0;

Expand Down
4 changes: 2 additions & 2 deletions src/key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,11 @@ bool CExtKey::Derive(CExtKey &out, unsigned int _nChild) const {
return key.Derive(out.key, out.chaincode, _nChild, chaincode);
}

void CExtKey::SetSeed(Span<const uint8_t> seed)
void CExtKey::SetSeed(Span<const std::byte> seed)
{
static const unsigned char hashkey[] = {'B','i','t','c','o','i','n',' ','s','e','e','d'};
std::vector<unsigned char, secure_allocator<unsigned char>> vout(64);
CHMAC_SHA512{hashkey, sizeof(hashkey)}.Write(seed.data(), seed.size()).Finalize(vout.data());
CHMAC_SHA512{hashkey, sizeof(hashkey)}.Write(UCharCast(seed.data()), seed.size()).Finalize(vout.data());
key.Set(vout.data(), vout.data() + 32, true);
memcpy(chaincode.begin(), vout.data() + 32, 32);
nDepth = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/key.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class CKey

//! Simple read-only vector-like interface.
unsigned int size() const { return (fValid ? keydata.size() : 0); }
const unsigned char* data() const { return keydata.data(); }
const std::byte* data() const { return reinterpret_cast<const std::byte*>(keydata.data()); }
const unsigned char* begin() const { return keydata.data(); }
const unsigned char* end() const { return keydata.data() + size(); }

Expand Down Expand Up @@ -188,7 +188,7 @@ struct CExtKey {
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
bool Derive(CExtKey& out, unsigned int nChild) const;
CExtPubKey Neuter() const;
void SetSeed(Span<const uint8_t> seed);
void SetSeed(Span<const std::byte> seed);
};

/** Initialize the elliptic curve support. May not be called twice without calling ECC_Stop first. */
Expand Down
50 changes: 25 additions & 25 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ static constexpr int32_t MAX_PEER_OBJECT_IN_FLIGHT = 100;
/** Maximum number of announced objects from a peer */
static constexpr int32_t MAX_PEER_OBJECT_ANNOUNCEMENTS = 2 * MAX_INV_SZ;
/** How many microseconds to delay requesting transactions from inbound peers */
static constexpr std::chrono::microseconds INBOUND_PEER_TX_DELAY{std::chrono::seconds{2}};
/** How long to wait (in microseconds) before downloading a transaction from an additional peer */
static constexpr std::chrono::microseconds GETDATA_TX_INTERVAL{std::chrono::seconds{60}};
/** Maximum delay (in microseconds) for transaction requests to avoid biasing some peers over others. */
static constexpr std::chrono::microseconds MAX_GETDATA_RANDOM_DELAY{std::chrono::seconds{2}};
static constexpr auto INBOUND_PEER_TX_DELAY{2s};
/** How long to wait before downloading a transaction from an additional peer */
static constexpr auto GETDATA_TX_INTERVAL{60s};
/** Maximum delay for transaction requests to avoid biasing some peers over others. */
static constexpr auto MAX_GETDATA_RANDOM_DELAY{2s};
/** How long to wait (expiry * factor microseconds) before expiring an in-flight getdata request to a peer */
static constexpr int64_t TX_EXPIRY_INTERVAL_FACTOR = 10;
static_assert(INBOUND_PEER_TX_DELAY >= MAX_GETDATA_RANDOM_DELAY,
Expand Down Expand Up @@ -112,7 +112,7 @@ static constexpr auto STALE_CHECK_INTERVAL{150s}; // 2.5 minutes (~block interva
/** How frequently to check for extra outbound peers and disconnect */
static constexpr auto EXTRA_PEER_CHECK_INTERVAL{45s};
/** Minimum time an outbound-peer-eviction candidate must be connected for, in order to evict */
static constexpr std::chrono::seconds MINIMUM_CONNECT_TIME{30};
static constexpr auto MINIMUM_CONNECT_TIME{30s};
/** SHA256("main address relay")[0:8] */
static constexpr uint64_t RANDOMIZER_ID_ADDRESS_RELAY = 0x3cac0035b5866b90ULL;
/// Age after which a stale block will no longer be served if requested as
Expand All @@ -122,7 +122,7 @@ static constexpr int STALE_RELAY_AGE_LIMIT = 30 * 24 * 60 * 60;
/// limiting block relay. Set to one week, denominated in seconds.
static constexpr int HISTORICAL_BLOCK_AGE = 7 * 24 * 60 * 60;
/** Time between pings automatically sent out for latency probing and keepalive */
static constexpr std::chrono::minutes PING_INTERVAL{2};
static constexpr auto PING_INTERVAL{2min};
/** The maximum number of entries in a locator */
static const unsigned int MAX_LOCATOR_SZ = 101;
/** Number of blocks that can be requested at any given time from a single peer. */
Expand Down Expand Up @@ -153,17 +153,17 @@ static const int MAX_UNCONNECTING_HEADERS = 10;
/** Minimum blocks required to signal NODE_NETWORK_LIMITED */
static const unsigned int NODE_NETWORK_LIMITED_MIN_BLOCKS = 288;
/** Average delay between local address broadcasts */
static constexpr auto AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL = 24h;
static constexpr auto AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL{24h};
/** Average delay between peer address broadcasts */
static constexpr auto AVG_ADDRESS_BROADCAST_INTERVAL = 30s;
static constexpr auto AVG_ADDRESS_BROADCAST_INTERVAL{30s};
/** Average delay between trickled inventory transmissions for inbound peers.
* Blocks and peers with NetPermissionFlags::NoBan permission bypass this. */
static constexpr auto INBOUND_INVENTORY_BROADCAST_INTERVAL = 5s;
static constexpr auto INBOUND_INVENTORY_BROADCAST_INTERVAL{5s};
/** Average delay between trickled inventory transmissions for outbound peers.
* Use a smaller delay as there is less privacy concern for them.
* Blocks and peers with NetPermissionFlags::NoBan permission bypass this.
* Masternode outbound peers get half this delay. */
static constexpr auto OUTBOUND_INVENTORY_BROADCAST_INTERVAL = 2s;
static constexpr auto OUTBOUND_INVENTORY_BROADCAST_INTERVAL{2s};
/** Maximum rate of inventory items to send per second.
* Limits the impact of low-fee transaction floods.
* We have 4 times smaller block times in Dash, so we need to push 4 times more invs per 1MB. */
Expand Down Expand Up @@ -459,7 +459,7 @@ struct CNodeState {
* - its chain tip has at least as much work as ours
*
* CHAIN_SYNC_TIMEOUT: if a peer's best known block has less work than our tip,
* set a timeout CHAIN_SYNC_TIMEOUT seconds in the future:
* set a timeout CHAIN_SYNC_TIMEOUT in the future:
* - If at timeout their best known block now has more work than our tip
* when the timeout was set, then either reset the timeout or clear it
* (after comparing against our current tip's work)
Expand Down Expand Up @@ -1479,11 +1479,11 @@ std::chrono::microseconds GetObjectInterval(int invType)
switch(invType)
{
case MSG_QUORUM_RECOVERED_SIG:
return std::chrono::seconds{15};
return 15s;
case MSG_CLSIG:
return std::chrono::seconds{5};
return 5s;
case MSG_ISDLOCK:
return std::chrono::seconds{10};
return 10s;
default:
return GETDATA_TX_INTERVAL;
}
Expand Down Expand Up @@ -1606,7 +1606,7 @@ void PeerManagerImpl::ReattemptInitialBroadcast(CScheduler& scheduler)

// Schedule next run for 10-15 minutes in the future.
// We add randomness on every cycle to avoid the possibility of P2P fingerprinting.
const std::chrono::milliseconds delta = std::chrono::minutes{10} + GetRandMillis(std::chrono::minutes{5});
const std::chrono::milliseconds delta = 10min + GetRandMillis(5min);
scheduler.scheduleFromNow([&] { ReattemptInitialBroadcast(scheduler); }, delta);
}

Expand Down Expand Up @@ -1709,7 +1709,7 @@ bool PeerManagerImpl::GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) c
// since pingtime does not update until the ping is complete, which might take a while.
// So, if a ping is taking an unusually long time in flight,
// the caller can immediately detect that this is happening.
std::chrono::microseconds ping_wait{0};
auto ping_wait{0us};
if ((0 != peer->m_ping_nonce_sent) && (0 != peer->m_ping_start.load().count())) {
ping_wait = GetTime<std::chrono::microseconds>() - peer->m_ping_start.load();
}
Expand Down Expand Up @@ -1955,7 +1955,7 @@ void PeerManagerImpl::StartScheduledTasks(CScheduler& scheduler)
scheduler.scheduleEvery([this] { this->CheckForStaleTipAndEvictPeers(); }, std::chrono::seconds{EXTRA_PEER_CHECK_INTERVAL});

// schedule next run for 10-15 minutes in the future
const std::chrono::milliseconds delta = std::chrono::minutes{10} + GetRandMillis(std::chrono::minutes{5});
const std::chrono::milliseconds delta = 10min + GetRandMillis(5min);
scheduler.scheduleFromNow([&] { ReattemptInitialBroadcast(scheduler); }, delta);
}

Expand Down Expand Up @@ -2566,10 +2566,10 @@ void PeerManagerImpl::ProcessGetData(CNode& pfrom, Peer& peer, const std::atomic
std::vector<CInv> vNotFound;
const CNetMsgMaker msgMaker(pfrom.GetCommonVersion());

const std::chrono::seconds now = GetTime<std::chrono::seconds>();
const auto now{GetTime<std::chrono::seconds>()};
// Get last mempool request time
const std::chrono::seconds mempool_req = tx_relay != nullptr ? tx_relay->m_last_mempool_req.load()
: std::chrono::seconds::min();
const auto mempool_req = tx_relay != nullptr ? tx_relay->m_last_mempool_req.load()
: std::chrono::seconds::min();

// Process as many TX items from the front of the getdata queue as
// possible, since they're common and it's efficient to batch process
Expand Down Expand Up @@ -3868,7 +3868,7 @@ void PeerManagerImpl::ProcessMessage(
int64_t nSince = nNow - 10 * 60;

// Update/increment addr rate limiting bucket.
const auto current_time = GetTime<std::chrono::microseconds>();
const auto current_time{GetTime<std::chrono::microseconds>()};
if (peer->m_addr_token_bucket < MAX_ADDR_PROCESSING_TOKEN_BUCKET) {
// Don't increment bucket if it's already full
const auto time_diff = std::max(current_time - peer->m_addr_token_timestamp, 0us);
Expand Down Expand Up @@ -3963,7 +3963,7 @@ void PeerManagerImpl::ProcessMessage(

LOCK(cs_main);

const auto current_time = GetTime<std::chrono::microseconds>();
const auto current_time{GetTime<std::chrono::microseconds>()};
uint256* best_block{nullptr};

for (CInv& inv : vInv) {
Expand Down Expand Up @@ -4401,7 +4401,7 @@ void PeerManagerImpl::ProcessMessage(
}
}
if (!fRejectedParents) {
const auto current_time = GetTime<std::chrono::microseconds>();
const auto current_time{GetTime<std::chrono::microseconds>()};

for (const uint256& parent_txid : unique_parents) {
CInv _inv(MSG_TX, parent_txid);
Expand Down Expand Up @@ -5659,7 +5659,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
// If we get here, the outgoing message serialization version is set and can't change.
const CNetMsgMaker msgMaker(pto->GetCommonVersion());

const auto current_time = GetTime<std::chrono::microseconds>();
const auto current_time{GetTime<std::chrono::microseconds>()};

if (pto->IsAddrFetchConn() && current_time - pto->m_connected > 10 * AVG_ADDRESS_BROADCAST_INTERVAL) {
LogPrint(BCLog::NET_NETCONN, "addrfetch connection timeout; disconnecting peer=%d\n", pto->GetId());
Expand Down
6 changes: 3 additions & 3 deletions src/node/blockstorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,10 +421,10 @@ bool BlockManager::IsBlockPruned(const CBlockIndex* pblockindex)
return (m_have_pruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0);
}

const CBlockIndex* GetFirstStoredBlock(const CBlockIndex* start_block) {
const CBlockIndex* BlockManager::GetFirstStoredBlock(const CBlockIndex& start_block)
{
AssertLockHeld(::cs_main);
assert(start_block);
const CBlockIndex* last_block = start_block;
const CBlockIndex* last_block = &start_block;
while (last_block->pprev && (last_block->pprev->nStatus & BLOCK_HAVE_DATA)) {
last_block = last_block->pprev;
}
Expand Down
9 changes: 5 additions & 4 deletions src/node/blockstorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
#ifndef BITCOIN_NODE_BLOCKSTORAGE_H
#define BITCOIN_NODE_BLOCKSTORAGE_H

#include <attributes.h>
#include <chain.h>
#include <fs.h>
#include <protocol.h> // For CMessageHeader::MessageStartChars
#include <protocol.h>
#include <sync.h>
#include <txdb.h>

Expand Down Expand Up @@ -196,6 +197,9 @@ class BlockManager
//! Returns last CBlockIndex* that is a checkpoint
const CBlockIndex* GetLastCheckpoint(const CCheckpointData& data) EXCLUSIVE_LOCKS_REQUIRED(cs_main);

//! Find the first block that is not pruned
const CBlockIndex* GetFirstStoredBlock(const CBlockIndex& start_block LIFETIMEBOUND) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);

/** True if any block files have ever been pruned. */
bool m_have_pruned = false;

Expand All @@ -206,9 +210,6 @@ class BlockManager
void UpdatePruneLock(const std::string& name, const PruneLockInfo& lock_info) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
};

//! Find the first block that is not pruned
const CBlockIndex* GetFirstStoredBlock(const CBlockIndex* start_block) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);

void CleanupBlockRevFiles();

/** Open a block file (blk?????.dat) */
Expand Down
4 changes: 4 additions & 0 deletions src/node/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,10 @@ class NodeImpl : public Node
LOCK(::cs_main);
return chainman().ActiveChainstate().CoinsTip().GetCoin(output, coin);
}
TransactionError broadcastTransaction(CTransactionRef tx, CAmount max_tx_fee, bilingual_str& err_string) override
{
return BroadcastTransaction(*m_context, std::move(tx), err_string, max_tx_fee, /*relay=*/ true, /*wait_callback=*/ false);
}
WalletLoader& walletLoader() override
{
return *Assert(m_context->wallet_loader);
Expand Down
36 changes: 28 additions & 8 deletions src/primitives/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,45 @@ class CTxIn
CScript scriptSig;
uint32_t nSequence;

/* Setting nSequence to this value for every input in a transaction
* disables nLockTime. */
/**
* Setting nSequence to this value for every input in a transaction
* disables nLockTime/IsFinalTx().
* It fails OP_CHECKLOCKTIMEVERIFY/CheckLockTime() for any input that has
* it set (BIP 65).
* It has SEQUENCE_LOCKTIME_DISABLE_FLAG set (BIP 68/112).
*/
static const uint32_t SEQUENCE_FINAL = 0xffffffff;
/**
* This is the maximum sequence number that enables both nLockTime and
* OP_CHECKLOCKTIMEVERIFY (BIP 65).
* It has SEQUENCE_LOCKTIME_DISABLE_FLAG set (BIP 68/112).
*/
static const uint32_t MAX_SEQUENCE_NONFINAL{SEQUENCE_FINAL - 1};

/* Below flags apply in the context of BIP 68*/
/* If this flag set, CTxIn::nSequence is NOT interpreted as a
* relative lock-time. */
// Below flags apply in the context of BIP 68. BIP 68 requires the tx
// version to be set to 2, or higher.
/**
* If this flag is set, CTxIn::nSequence is NOT interpreted as a
* relative lock-time.
* It skips SequenceLocks() for any input that has it set (BIP 68).
* It fails OP_CHECKSEQUENCEVERIFY/CheckSequence() for any input that has
* it set (BIP 112).
*/
static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG = (1U << 31);

/* If CTxIn::nSequence encodes a relative lock-time and this flag
/**
* If CTxIn::nSequence encodes a relative lock-time and this flag
* is set, the relative lock-time has units of 512 seconds,
* otherwise it specifies blocks with a granularity of 1. */
static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG = (1 << 22);

/* If CTxIn::nSequence encodes a relative lock-time, this mask is
/**
* If CTxIn::nSequence encodes a relative lock-time, this mask is
* applied to extract that lock-time from the sequence field. */
static const uint32_t SEQUENCE_LOCKTIME_MASK = 0x0000ffff;

/* In order to use the same number of bits to encode roughly the
/**
* In order to use the same number of bits to encode roughly the
* same wall-clock duration, and because blocks are naturally
* limited to occur every 600s on average, the minimum granularity
* for time-based relative lock-time is fixed at 512 seconds.
Expand Down
4 changes: 2 additions & 2 deletions src/qt/psbtoperationsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ void PSBTOperationsDialog::broadcastTransaction()

CTransactionRef tx = MakeTransactionRef(mtx);
bilingual_str err_string;
TransactionError error = BroadcastTransaction(
*m_client_model->node().context(), tx, err_string, DEFAULT_MAX_RAW_TX_FEE_RATE.GetFeePerK(), /* relay */ true, /* await_callback */ false);
TransactionError error =
m_client_model->node().broadcastTransaction(tx, DEFAULT_MAX_RAW_TX_FEE_RATE.GetFeePerK(), err_string);

if (error == TransactionError::OK) {
showStatus(tr("Transaction broadcast successfully! Transaction ID: %1")
Expand Down
Loading

0 comments on commit 5dbe58c

Please sign in to comment.