Skip to content

Commit 5eb9781

Browse files
author
MacroFake
committed
Merge bitcoin/bitcoin#25971: refactor: Use std::string for thread and index names
26cf9ea scripted-diff: rename pszThread to thread_name (stickies-v) 200d84d refactor: use std::string for index names (stickies-v) 97f5b20 refactor: use std::string for thread names (stickies-v) Pull request description: As a follow-up to bitcoin/bitcoin#25967 (comment), this PR changes the return type of [`BaseIndex::GetName()`](https://github.com/bitcoin/bitcoin/blob/fa5c224d444802dabec5841009e029b9754c92f1/src/index/base.h#L120) to `const std::string&` instead of `const char*`. The first commit is not essential for this change, but since the code is touched and index names are commonly used to specify thread names, I've made the same update there. No behaviour change, just refactoring to further phase out C-style strings. Note: `util::ThreadRename()` used to take an rvalue ref, but since it then passes this to `SetInternalName()` by value, I don't think there's any benefit to having both an rvalue and lvalue ref function so I just changed it into lvalue ref. Not 100% sure I'm missing something? ACKs for top commit: MarcoFalke: review ACK 26cf9ea only change is new scripted-diff 😀 hebasto: ACK 26cf9ea, I have reviewed the code and it looks OK. w0xlt: reACK bitcoin/bitcoin@26cf9ea Tree-SHA512: 44a03ebf2bb86ca1411a36222a575217cdba8ee3a3c985e74d74c934516f002b27336147fa22f59eda7dac21204a93951563317005d475da95b23c427014d77b
2 parents 08785aa + 26cf9ea commit 5eb9781

File tree

13 files changed

+27
-26
lines changed

13 files changed

+27
-26
lines changed

src/index/base.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
#include <validation.h> // For g_chainman
1919
#include <warnings.h>
2020

21+
#include <string>
22+
#include <utility>
23+
2124
using node::ReadBlockFromDisk;
2225

2326
constexpr uint8_t DB_BEST_BLOCK{'B'};
@@ -62,8 +65,8 @@ void BaseIndex::DB::WriteBestBlock(CDBBatch& batch, const CBlockLocator& locator
6265
batch.Write(DB_BEST_BLOCK, locator);
6366
}
6467

65-
BaseIndex::BaseIndex(std::unique_ptr<interfaces::Chain> chain)
66-
: m_chain{std::move(chain)} {}
68+
BaseIndex::BaseIndex(std::unique_ptr<interfaces::Chain> chain, std::string name)
69+
: m_chain{std::move(chain)}, m_name{std::move(name)} {}
6770

6871
BaseIndex::~BaseIndex()
6972
{

src/index/base.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <threadinterrupt.h>
1111
#include <validationinterface.h>
1212

13+
#include <string>
14+
1315
class CBlock;
1416
class CBlockIndex;
1517
class Chainstate;
@@ -95,6 +97,7 @@ class BaseIndex : public CValidationInterface
9597
protected:
9698
std::unique_ptr<interfaces::Chain> m_chain;
9799
Chainstate* m_chainstate{nullptr};
100+
const std::string m_name;
98101

99102
void BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) override;
100103

@@ -117,13 +120,13 @@ class BaseIndex : public CValidationInterface
117120
virtual DB& GetDB() const = 0;
118121

119122
/// Get the name of the index for display in logs.
120-
virtual const char* GetName() const = 0;
123+
const std::string& GetName() const LIFETIMEBOUND { return m_name; }
121124

122125
/// Update the internal best block index as well as the prune lock.
123126
void SetBestBlockIndex(const CBlockIndex* block);
124127

125128
public:
126-
BaseIndex(std::unique_ptr<interfaces::Chain> chain);
129+
BaseIndex(std::unique_ptr<interfaces::Chain> chain, std::string name);
127130
/// Destructor interrupts sync thread if running and blocks until it exits.
128131
virtual ~BaseIndex();
129132

src/index/blockfilterindex.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,15 @@ static std::map<BlockFilterType, BlockFilterIndex> g_filter_indexes;
9797

9898
BlockFilterIndex::BlockFilterIndex(std::unique_ptr<interfaces::Chain> chain, BlockFilterType filter_type,
9999
size_t n_cache_size, bool f_memory, bool f_wipe)
100-
: BaseIndex(std::move(chain)), m_filter_type(filter_type)
100+
: BaseIndex(std::move(chain), BlockFilterTypeName(filter_type) + " block filter index")
101+
, m_filter_type(filter_type)
101102
{
102103
const std::string& filter_name = BlockFilterTypeName(filter_type);
103104
if (filter_name.empty()) throw std::invalid_argument("unknown filter_type");
104105

105106
fs::path path = gArgs.GetDataDirNet() / "indexes" / "blockfilter" / fs::u8path(filter_name);
106107
fs::create_directories(path);
107108

108-
m_name = filter_name + " block filter index";
109109
m_db = std::make_unique<BaseIndex::DB>(path / "db", n_cache_size, f_memory, f_wipe);
110110
m_filter_fileseq = std::make_unique<FlatFileSeq>(std::move(path), "fltr", FLTR_FILE_CHUNK_SIZE);
111111
}

src/index/blockfilterindex.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class BlockFilterIndex final : public BaseIndex
2626
{
2727
private:
2828
BlockFilterType m_filter_type;
29-
std::string m_name;
3029
std::unique_ptr<BaseIndex::DB> m_db;
3130

3231
FlatFilePos m_next_filter_pos;
@@ -52,8 +51,6 @@ class BlockFilterIndex final : public BaseIndex
5251

5352
BaseIndex::DB& GetDB() const LIFETIMEBOUND override { return *m_db; }
5453

55-
const char* GetName() const LIFETIMEBOUND override { return m_name.c_str(); }
56-
5754
public:
5855
/** Constructs the index, which becomes available to be queried. */
5956
explicit BlockFilterIndex(std::unique_ptr<interfaces::Chain> chain, BlockFilterType filter_type,

src/index/coinstatsindex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ struct DBHashKey {
105105
std::unique_ptr<CoinStatsIndex> g_coin_stats_index;
106106

107107
CoinStatsIndex::CoinStatsIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory, bool f_wipe)
108-
: BaseIndex(std::move(chain))
108+
: BaseIndex(std::move(chain), "coinstatsindex")
109109
{
110110
fs::path path{gArgs.GetDataDirNet() / "indexes" / "coinstats"};
111111
fs::create_directories(path);

src/index/coinstatsindex.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ struct CCoinsStats;
2020
class CoinStatsIndex final : public BaseIndex
2121
{
2222
private:
23-
std::string m_name;
2423
std::unique_ptr<BaseIndex::DB> m_db;
2524

2625
MuHash3072 m_muhash;
@@ -52,8 +51,6 @@ class CoinStatsIndex final : public BaseIndex
5251

5352
BaseIndex::DB& GetDB() const override { return *m_db; }
5453

55-
const char* GetName() const override { return "coinstatsindex"; }
56-
5754
public:
5855
// Constructs the index, which becomes available to be queried.
5956
explicit CoinStatsIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory = false, bool f_wipe = false);

src/index/txindex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ bool TxIndex::DB::WriteTxs(const std::vector<std::pair<uint256, CDiskTxPos>>& v_
4949
}
5050

5151
TxIndex::TxIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory, bool f_wipe)
52-
: BaseIndex(std::move(chain)), m_db(std::make_unique<TxIndex::DB>(n_cache_size, f_memory, f_wipe))
52+
: BaseIndex(std::move(chain), "txindex"), m_db(std::make_unique<TxIndex::DB>(n_cache_size, f_memory, f_wipe))
5353
{}
5454

5555
TxIndex::~TxIndex() = default;

src/index/txindex.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ class TxIndex final : public BaseIndex
2727

2828
BaseIndex::DB& GetDB() const override;
2929

30-
const char* GetName() const override { return "txindex"; }
31-
3230
public:
3331
/// Constructs the index, which becomes available to be queried.
3432
explicit TxIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory = false, bool f_wipe = false);

src/qt/guiutil.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ void PrintSlotException(
982982
std::string description = sender->metaObject()->className();
983983
description += "->";
984984
description += receiver->metaObject()->className();
985-
PrintExceptionContinue(exception, description.c_str());
985+
PrintExceptionContinue(exception, description);
986986
}
987987

988988
void ShowModalDialogAsynchronously(QDialog* dialog)

src/util/system.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ std::string HelpMessageOpt(const std::string &option, const std::string &message
831831
std::string("\n\n");
832832
}
833833

834-
static std::string FormatException(const std::exception* pex, const char* pszThread)
834+
static std::string FormatException(const std::exception* pex, std::string_view thread_name)
835835
{
836836
#ifdef WIN32
837837
char pszModule[MAX_PATH] = "";
@@ -841,15 +841,15 @@ static std::string FormatException(const std::exception* pex, const char* pszThr
841841
#endif
842842
if (pex)
843843
return strprintf(
844-
"EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread);
844+
"EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, thread_name);
845845
else
846846
return strprintf(
847-
"UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread);
847+
"UNKNOWN EXCEPTION \n%s in %s \n", pszModule, thread_name);
848848
}
849849

850-
void PrintExceptionContinue(const std::exception* pex, const char* pszThread)
850+
void PrintExceptionContinue(const std::exception* pex, std::string_view thread_name)
851851
{
852-
std::string message = FormatException(pex, pszThread);
852+
std::string message = FormatException(pex, thread_name);
853853
LogPrintf("\n\n************************\n%s\n", message);
854854
tfm::format(std::cerr, "\n\n************************\n%s\n", message);
855855
}

0 commit comments

Comments
 (0)