Skip to content

Commit cefa2eb

Browse files
Merge pull request dashpay#5663 from vijaydasmp/bp23_2
backport: Merge bitcoin#(partial) 21940,(partial) 22232,22505,22407,22510,22383,13533,22528,22538,22139
2 parents 63b9071 + 0845e19 commit cefa2eb

25 files changed

+176
-120
lines changed

contrib/guix/README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ We achieve bootstrappability by using Guix as a functional package manager.
1111

1212
# Requirements
1313

14-
Conservatively, a x86_64 machine with:
14+
Conservatively, you will need an x86_64 machine with:
1515

1616
- 16GB of free disk space on the partition that /gnu/store will reside in
1717
- 8GB of free disk space **per platform triple** you're planning on building
@@ -437,9 +437,8 @@ In the extraordinarily rare case where you messed up your Guix installation in
437437
an irreversible way, you may want to completely purge Guix from your system and
438438
start over.
439439

440-
1. Uninstall Guix itself according to the way you installed it. (e.g. `sudo apt
441-
purge guix` for Ubuntu packaging, `sudo make uninstall` for
442-
built-from-source).
440+
1. Uninstall Guix itself according to the way you installed it (e.g. `sudo apt
441+
purge guix` for Ubuntu packaging, `sudo make uninstall` for a build from source).
443442
2. Remove all build users and groups
444443

445444
You may check for relevant users and groups using:

doc/release-notes-22407.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Updated RPC
2+
--------
3+
4+
- `getblockchaininfo` now returns a new `time` field, that provides the chain tip time.

src/addrman.cpp

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ CAddrInfo* CAddrMan::Create(const CAddress& addr, const CNetAddr& addrSource, in
144144
return &mapInfo[nId];
145145
}
146146

147-
void CAddrMan::SwapRandom(unsigned int nRndPos1, unsigned int nRndPos2)
147+
void CAddrMan::SwapRandom(unsigned int nRndPos1, unsigned int nRndPos2) const
148148
{
149149
AssertLockHeld(cs);
150150

@@ -156,11 +156,13 @@ void CAddrMan::SwapRandom(unsigned int nRndPos1, unsigned int nRndPos2)
156156
int nId1 = vRandom[nRndPos1];
157157
int nId2 = vRandom[nRndPos2];
158158

159-
assert(mapInfo.count(nId1) == 1);
160-
assert(mapInfo.count(nId2) == 1);
159+
const auto it_1{mapInfo.find(nId1)};
160+
const auto it_2{mapInfo.find(nId2)};
161+
assert(it_1 != mapInfo.end());
162+
assert(it_2 != mapInfo.end());
161163

162-
mapInfo[nId1].nRandomPos = nRndPos2;
163-
mapInfo[nId2].nRandomPos = nRndPos1;
164+
it_1->second.nRandomPos = nRndPos2;
165+
it_2->second.nRandomPos = nRndPos1;
164166

165167
vRandom[nRndPos1] = nId2;
166168
vRandom[nRndPos2] = nId1;
@@ -425,7 +427,7 @@ void CAddrMan::Attempt_(const CService& addr, bool fCountFailure, int64_t nTime)
425427
}
426428
}
427429

428-
CAddrInfo CAddrMan::Select_(bool newOnly)
430+
CAddrInfo CAddrMan::Select_(bool newOnly) const
429431
{
430432
AssertLockHeld(cs);
431433

@@ -448,8 +450,9 @@ CAddrInfo CAddrMan::Select_(bool newOnly)
448450
nKBucketPos = (nKBucketPos + insecure_rand.randbits(ADDRMAN_BUCKET_SIZE_LOG2)) % ADDRMAN_BUCKET_SIZE;
449451
}
450452
int nId = vvTried[nKBucket][nKBucketPos];
451-
assert(mapInfo.count(nId) == 1);
452-
CAddrInfo& info = mapInfo[nId];
453+
const auto it_found{mapInfo.find(nId)};
454+
assert(it_found != mapInfo.end());
455+
const CAddrInfo& info{it_found->second};
453456
if (insecure_rand.randbits(30) < fChanceFactor * info.GetChance() * (1 << 30))
454457
return info;
455458
fChanceFactor *= 1.2;
@@ -465,8 +468,9 @@ CAddrInfo CAddrMan::Select_(bool newOnly)
465468
nUBucketPos = (nUBucketPos + insecure_rand.randbits(ADDRMAN_BUCKET_SIZE_LOG2)) % ADDRMAN_BUCKET_SIZE;
466469
}
467470
int nId = vvNew[nUBucket][nUBucketPos];
468-
assert(mapInfo.count(nId) == 1);
469-
CAddrInfo& info = mapInfo[nId];
471+
const auto it_found{mapInfo.find(nId)};
472+
assert(it_found != mapInfo.end());
473+
const CAddrInfo& info{it_found->second};
470474
if (insecure_rand.randbits(30) < fChanceFactor * info.GetChance() * (1 << 30))
471475
return info;
472476
fChanceFactor *= 1.2;
@@ -517,15 +521,15 @@ int CAddrMan::Check_()
517521

518522
for (int n = 0; n < ADDRMAN_TRIED_BUCKET_COUNT; n++) {
519523
for (int i = 0; i < ADDRMAN_BUCKET_SIZE; i++) {
520-
if (vvTried[n][i] != -1) {
521-
if (!setTried.count(vvTried[n][i]))
522-
return -11;
523-
if (mapInfo[vvTried[n][i]].GetTriedBucket(nKey, m_asmap) != n)
524-
return -17;
525-
if (mapInfo[vvTried[n][i]].GetBucketPosition(nKey, false, n) != i)
526-
return -18;
527-
setTried.erase(vvTried[n][i]);
528-
}
524+
if (vvTried[n][i] != -1) {
525+
if (!setTried.count(vvTried[n][i]))
526+
return -11;
527+
if (mapInfo[vvTried[n][i]].GetTriedBucket(nKey, m_asmap) != n)
528+
return -17;
529+
if (mapInfo[vvTried[n][i]].GetBucketPosition(nKey, false, n) != i)
530+
return -18;
531+
setTried.erase(vvTried[n][i]);
532+
}
529533
}
530534
}
531535

@@ -553,7 +557,7 @@ int CAddrMan::Check_()
553557
}
554558
#endif
555559

556-
void CAddrMan::GetAddr_(std::vector<CAddress>& vAddr, size_t max_addresses, size_t max_pct, std::optional<Network> network)
560+
void CAddrMan::GetAddr_(std::vector<CAddress>& vAddr, size_t max_addresses, size_t max_pct, std::optional<Network> network) const
557561
{
558562
AssertLockHeld(cs);
559563

@@ -573,9 +577,10 @@ void CAddrMan::GetAddr_(std::vector<CAddress>& vAddr, size_t max_addresses, size
573577

574578
int nRndPos = insecure_rand.randrange(vRandom.size() - n) + n;
575579
SwapRandom(n, nRndPos);
576-
assert(mapInfo.count(vRandom[n]) == 1);
580+
const auto it{mapInfo.find(vRandom[n])};
581+
assert(it != mapInfo.end());
577582

578-
const CAddrInfo& ai = mapInfo[vRandom[n]];
583+
const CAddrInfo& ai{it->second};
579584

580585
// Filter by network (optional)
581586
if (network != std::nullopt && ai.GetNetClass() != network) continue;

src/addrman.h

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class CAddrInfo : public CAddress
5555
bool fInTried{false};
5656

5757
//! position in vRandom
58-
int nRandomPos{-1};
58+
mutable int nRandomPos{-1};
5959

6060
friend class CAddrMan;
6161

@@ -544,12 +544,12 @@ class CAddrMan
544544
}
545545

546546
//! Mark an entry as accessible.
547-
void Good(const CService &addr, bool test_before_evict = true, int64_t nTime = GetAdjustedTime())
547+
void Good(const CService &addr, int64_t nTime = GetAdjustedTime())
548548
EXCLUSIVE_LOCKS_REQUIRED(!cs)
549549
{
550550
LOCK(cs);
551551
Check();
552-
Good_(addr, test_before_evict, nTime);
552+
Good_(addr, /* test_before_evict */ true, nTime);
553553
Check();
554554
}
555555

@@ -587,7 +587,7 @@ class CAddrMan
587587
/**
588588
* Choose an address to connect to.
589589
*/
590-
CAddrInfo Select(bool newOnly = false)
590+
CAddrInfo Select(bool newOnly = false) const
591591
EXCLUSIVE_LOCKS_REQUIRED(!cs)
592592
{
593593
LOCK(cs);
@@ -604,7 +604,7 @@ class CAddrMan
604604
* @param[in] max_pct Maximum percentage of addresses to return (0 = all).
605605
* @param[in] network Select only addresses of this network (nullopt = all).
606606
*/
607-
std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network)
607+
std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network) const
608608
EXCLUSIVE_LOCKS_REQUIRED(!cs)
609609
{
610610
LOCK(cs);
@@ -650,12 +650,12 @@ class CAddrMan
650650
uint256 nKey;
651651

652652
//! Source of random numbers for randomization in inner loops
653-
FastRandomContext insecure_rand;
653+
mutable FastRandomContext insecure_rand GUARDED_BY(cs);
654654

655-
private:
656655
//! A mutex to protect the inner data structures.
657656
mutable Mutex cs;
658657

658+
private:
659659
//! Serialization versions.
660660
enum Format : uint8_t {
661661
V0_HISTORICAL = 0, //!< historic format, before commit e6b343d88
@@ -688,7 +688,9 @@ class CAddrMan
688688
std::map<CService, int> mapAddr GUARDED_BY(cs);
689689

690690
//! randomly-ordered vector of all nIds
691-
std::vector<int> vRandom GUARDED_BY(cs);
691+
//! This is mutable because it is unobservable outside the class, so any
692+
//! changes to it (even in const methods) are also unobservable.
693+
mutable std::vector<int> vRandom GUARDED_BY(cs);
692694

693695
// number of "tried" entries
694696
int nTried GUARDED_BY(cs);
@@ -718,7 +720,7 @@ class CAddrMan
718720
CAddrInfo* Create(const CAddress &addr, const CNetAddr &addrSource, int *pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
719721

720722
//! Swap two elements in vRandom.
721-
void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) EXCLUSIVE_LOCKS_REQUIRED(cs);
723+
void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) const EXCLUSIVE_LOCKS_REQUIRED(cs);
722724

723725
//! Move an entry from the "new" table(s) to the "tried" table
724726
void MakeTried(CAddrInfo& info, int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
@@ -739,7 +741,7 @@ class CAddrMan
739741
void Attempt_(const CService &addr, bool fCountFailure, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs);
740742

741743
//! Select an address to connect to, if newOnly is set to true, only the new table is selected from.
742-
CAddrInfo Select_(bool newOnly) EXCLUSIVE_LOCKS_REQUIRED(cs);
744+
CAddrInfo Select_(bool newOnly) const EXCLUSIVE_LOCKS_REQUIRED(cs);
743745

744746
//! See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
745747
void ResolveCollisions_() EXCLUSIVE_LOCKS_REQUIRED(cs);
@@ -748,7 +750,7 @@ class CAddrMan
748750
CAddrInfo SelectTriedCollision_() EXCLUSIVE_LOCKS_REQUIRED(cs);
749751

750752
//! Consistency check
751-
void Check()
753+
void Check() const
752754
EXCLUSIVE_LOCKS_REQUIRED(cs)
753755
{
754756
#ifdef DEBUG_ADDRMAN
@@ -762,7 +764,7 @@ class CAddrMan
762764

763765
#ifdef DEBUG_ADDRMAN
764766
//! Perform consistency check. Returns an error code or zero.
765-
int Check_() EXCLUSIVE_LOCKS_REQUIRED(cs);
767+
int Check_() const EXCLUSIVE_LOCKS_REQUIRED(cs);
766768
#endif
767769

768770
/**
@@ -773,7 +775,7 @@ class CAddrMan
773775
* @param[in] max_pct Maximum percentage of addresses to return (0 = all).
774776
* @param[in] network Select only addresses of this network (nullopt = all).
775777
*/
776-
void GetAddr_(std::vector<CAddress>& vAddr, size_t max_addresses, size_t max_pct, std::optional<Network> network) EXCLUSIVE_LOCKS_REQUIRED(cs);
778+
void GetAddr_(std::vector<CAddress>& vAddr, size_t max_addresses, size_t max_pct, std::optional<Network> network) const EXCLUSIVE_LOCKS_REQUIRED(cs);
777779

778780
/** We have successfully connected to this peer. Calling this function
779781
* updates the CAddress's nTime, which is used in our IsTerrible()

src/consensus/tx_verify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ unsigned int GetP2SHSigOpCount(const CTransaction& tx, const CCoinsViewCache& in
145145
return nSigOps;
146146
}
147147

148-
unsigned int GetTransactionSigOpCount(const CTransaction& tx, const CCoinsViewCache& inputs, int flags)
148+
unsigned int GetTransactionSigOpCount(const CTransaction& tx, const CCoinsViewCache& inputs, uint32_t flags)
149149
{
150150
unsigned int nSigOps = GetLegacySigOpCount(tx);
151151

src/consensus/tx_verify.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ unsigned int GetP2SHSigOpCount(const CTransaction& tx, const CCoinsViewCache& ma
5252
* @param[out] flags Script verification flags
5353
* @return Total signature operation count for a tx
5454
*/
55-
unsigned int GetTransactionSigOpCount(const CTransaction& tx, const CCoinsViewCache& inputs, int flags);
55+
unsigned int GetTransactionSigOpCount(const CTransaction& tx, const CCoinsViewCache& inputs, uint32_t flags);
5656

5757
/**
5858
* Check if transaction is final and can be included in a block with the

src/node/transaction.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
#include <consensus/validation.h>
7+
#include <index/txindex.h>
78
#include <net.h>
89
#include <net_processing.h>
10+
#include <node/blockstorage.h>
911
#include <txmempool.h>
1012
#include <validation.h>
1113
#include <validationinterface.h>
@@ -90,3 +92,38 @@ TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef t
9092

9193
return TransactionError::OK;
9294
}
95+
96+
CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMemPool* const mempool, const uint256& hash, const Consensus::Params& consensusParams, uint256& hashBlock)
97+
{
98+
LOCK(cs_main);
99+
100+
if (mempool && !block_index) {
101+
CTransactionRef ptx = mempool->get(hash);
102+
if (ptx) return ptx;
103+
}
104+
if (g_txindex) {
105+
CTransactionRef tx;
106+
uint256 block_hash;
107+
if (g_txindex->FindTx(hash, block_hash, tx)) {
108+
if (!block_index || block_index->GetBlockHash() == block_hash) {
109+
// Don't return the transaction if the provided block hash doesn't match.
110+
// The case where a transaction appears in multiple blocks (e.g. reorgs or
111+
// BIP30) is handled by the block lookup below.
112+
hashBlock = block_hash;
113+
return tx;
114+
}
115+
}
116+
}
117+
if (block_index) {
118+
CBlock block;
119+
if (ReadBlockFromDisk(block, block_index, consensusParams)) {
120+
for (const auto& tx : block.vtx) {
121+
if (tx->GetHash() == hash) {
122+
hashBlock = block_index->GetBlockHash();
123+
return tx;
124+
}
125+
}
126+
}
127+
}
128+
return nullptr;
129+
}

src/node/transaction.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
#include <primitives/transaction.h>
1111
#include <util/error.h>
1212

13+
class CBlockIndex;
14+
class CTxMemPool;
1315
struct NodeContext;
16+
namespace Consensus {
17+
struct Params;
18+
}
1419

1520
/** Maximum fee rate for sendrawtransaction and testmempoolaccept RPC calls.
1621
* Also used by the GUI when broadcasting a completed PSBT.
@@ -38,4 +43,19 @@ static const CFeeRate DEFAULT_MAX_RAW_TX_FEE_RATE{COIN / 10};
3843
*/
3944
[[nodiscard]] TransactionError BroadcastTransaction(NodeContext& node, CTransactionRef tx, std::string& err_string, const CAmount& highfee, bool relay, bool wait_callback, bool bypass_limits = false);
4045

46+
/**
47+
* Return transaction with a given hash.
48+
* If mempool is provided and block_index is not provided, check it first for the tx.
49+
* If -txindex is available, check it next for the tx.
50+
* Finally, if block_index is provided, check for tx by reading entire block from disk.
51+
*
52+
* @param[in] block_index The block to read from disk, or nullptr
53+
* @param[in] mempool If provided, check mempool for tx
54+
* @param[in] hash The txid
55+
* @param[in] consensusParams The params
56+
* @param[out] hashBlock The block hash, if the tx was found via -txindex or block_index
57+
* @returns The tx if found, otherwise nullptr
58+
*/
59+
CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMemPool* const mempool, const uint256& hash, const Consensus::Params& consensusParams, uint256& hashBlock);
60+
4161
#endif // BITCOIN_NODE_TRANSACTION_H

src/rpc/blockchain.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1662,7 +1662,8 @@ UniValue getblockchaininfo(const JSONRPCRequest& request)
16621662
{RPCResult::Type::NUM, "headers", "the current number of headers we have validated"},
16631663
{RPCResult::Type::STR, "bestblockhash", "the hash of the currently best block"},
16641664
{RPCResult::Type::NUM, "difficulty", "the current difficulty"},
1665-
{RPCResult::Type::NUM, "mediantime", "median time for the current best block"},
1665+
{RPCResult::Type::NUM_TIME, "time", "The block time expressed in " + UNIX_EPOCH_TIME},
1666+
{RPCResult::Type::NUM_TIME, "mediantime", "The median block time expressed in " + UNIX_EPOCH_TIME},
16661667
{RPCResult::Type::NUM, "verificationprogress", "estimate of verification progress [0..1]"},
16671668
{RPCResult::Type::BOOL, "initialblockdownload", "(debug information) estimate of whether this node is in Initial Block Download mode"},
16681669
{RPCResult::Type::STR_HEX, "chainwork", "total amount of work in active chain, in hexadecimal"},
@@ -1720,6 +1721,7 @@ UniValue getblockchaininfo(const JSONRPCRequest& request)
17201721
obj.pushKV("headers", pindexBestHeader ? pindexBestHeader->nHeight : -1);
17211722
obj.pushKV("bestblockhash", tip->GetBlockHash().GetHex());
17221723
obj.pushKV("difficulty", (double)GetDifficulty(tip));
1724+
obj.pushKV("time", (int64_t)tip->nTime);
17231725
obj.pushKV("mediantime", (int64_t)tip->GetMedianTimePast());
17241726
obj.pushKV("verificationprogress", GuessVerificationProgress(Params().TxData(), tip));
17251727
obj.pushKV("initialblockdownload", active_chainstate.IsInitialBlockDownload());

src/rpc/rawtransaction.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ static UniValue getrawtransaction(const JSONRPCRequest& request)
113113
RPCHelpMan{
114114
"getrawtransaction",
115115
"\nReturn the raw transaction data.\n"
116-
"\nBy default this function only works for mempool transactions. When called with a blockhash\n"
117-
"argument, getrawtransaction will return the transaction if the specified block is available and\n"
118-
"the transaction is found in that block. When called without a blockhash argument, getrawtransaction\n"
119-
"will return the transaction if it is in the mempool, or if -txindex is enabled and the transaction\n"
120-
"is in a block in the blockchain.\n"
116+
117+
"\nBy default, this call only returns a transaction if it is in the mempool. If -txindex is enabled\n"
118+
"and no blockhash argument is passed, it will return the transaction if it is in the mempool or any block.\n"
119+
"If a blockhash argument is passed, it will return the transaction if\n"
120+
"the specified block is available and the transaction is in that block.\n"
121121
"\nHint: Use gettransaction for wallet transactions.\n"
122122

123123
"\nIf verbose is 'true', returns an Object with information about 'txid'.\n"

0 commit comments

Comments
 (0)