Skip to content
Open
1 change: 1 addition & 0 deletions share/rpcauth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ positional arguments:

optional arguments:
-h, --help show this help message and exit
-j, --json output data in json format
```
12 changes: 9 additions & 3 deletions share/rpcauth/rpcauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from getpass import getpass
from secrets import token_hex, token_urlsafe
import hmac
import json

def generate_salt(size):
"""Create size byte hex salt"""
Expand All @@ -24,6 +25,7 @@ def main():
parser = ArgumentParser(description='Create login credentials for a JSON-RPC user')
parser.add_argument('username', help='the username for authentication')
parser.add_argument('password', help='leave empty to generate a random password or specify "-" to prompt for password', nargs='?')
parser.add_argument("-j", "--json", help="output to json instead of plain-text", action='store_true')
args = parser.parse_args()

if not args.password:
Expand All @@ -35,9 +37,13 @@ def main():
salt = generate_salt(16)
password_hmac = password_to_hmac(salt, args.password)

print('String to be appended to dash.conf:')
print(f'rpcauth={args.username}:{salt}${password_hmac}')
print(f'Your password:\n{args.password}')
if args.json:
odict={'username':args.username, 'password':args.password, 'rpcauth':f'{args.username}:{salt}${password_hmac}'}
print(json.dumps(odict))
else:
print('String to be appended to dash.conf:')
print(f'rpcauth={args.username}:{salt}${password_hmac}')
print(f'Your password:\n{args.password}')

if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions src/Makefile.bench.include
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ bench_bench_dash_SOURCES = \
bench/merkle_root.cpp \
bench/nanobench.cpp \
bench/nanobench.h \
bench/parse_hex.cpp \
bench/peer_eviction.cpp \
bench/poly1305.cpp \
bench/pool.cpp \
Expand Down
14 changes: 9 additions & 5 deletions src/addrdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ bool SerializeDB(Stream& stream, const Data& data)
hashwriter << Params().MessageStart() << data;
stream << hashwriter.GetHash();
} catch (const std::exception& e) {
return error("%s: Serialize or I/O error - %s", __func__, e.what());
LogError("%s: Serialize or I/O error - %s\n", __func__, e.what());
return false;
}

return true;
Expand All @@ -59,7 +60,8 @@ bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data
if (fileout.IsNull()) {
fileout.fclose();
remove(pathTmp);
return error("%s: Failed to open file %s", __func__, fs::PathToString(pathTmp));
LogError("%s: Failed to open file %s\n", __func__, fs::PathToString(pathTmp));
return false;
}

// Serialize
Expand All @@ -71,14 +73,16 @@ bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data
if (!FileCommit(fileout.Get())) {
fileout.fclose();
remove(pathTmp);
return error("%s: Failed to flush file %s", __func__, fs::PathToString(pathTmp));
LogError("%s: Failed to flush file %s\n", __func__, fs::PathToString(pathTmp));
return false;
}
fileout.fclose();

// replace existing file, if any, with new file
if (!RenameOver(pathTmp, path)) {
remove(pathTmp);
return error("%s: Rename-into-place failed", __func__);
LogError("%s: Rename-into-place failed\n", __func__);
return false;
}

return true;
Expand Down Expand Up @@ -136,7 +140,7 @@ bool CBanDB::Write(const banmap_t& banSet)
}

for (const auto& err : errors) {
error("%s", err);
LogError("%s\n", err);
}
return false;
}
Expand Down
36 changes: 36 additions & 0 deletions src/bench/parse_hex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2024- The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <bench/bench.h>
#include <random.h>
#include <stddef.h>
#include <util/strencodings.h>
#include <cassert>
#include <optional>
#include <vector>

std::string generateHexString(size_t length) {
const auto hex_digits = "0123456789ABCDEF";
FastRandomContext rng(/*fDeterministic=*/true);

std::string data;
while (data.size() < length) {
auto digit = hex_digits[rng.randbits(4)];
data.push_back(digit);
}
return data;
}

static void HexParse(benchmark::Bench& bench)
{
auto data = generateHexString(130); // Generates 678B0EDA0A1FD30904D5A65E3568DB82DB2D918B0AD8DEA18A63FECCB877D07CAD1495C7157584D877420EF38B8DA473A6348B4F51811AC13C786B962BEE5668F9 by default

bench.batch(data.size()).unit("base16").run([&] {
auto result = TryParseHex(data);
assert(result != std::nullopt); // make sure we're measuring the successful case
ankerl::nanobench::doNotOptimizeAway(result);
});
}

BENCHMARK(HexParse, benchmark::PriorityLevel::HIGH);
9 changes: 6 additions & 3 deletions src/flatfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,18 @@ bool FlatFileSeq::Flush(const FlatFilePos& pos, bool finalize)
{
FILE* file = Open(FlatFilePos(pos.nFile, 0)); // Avoid fseek to nPos
if (!file) {
return error("%s: failed to open file %d", __func__, pos.nFile);
LogError("%s: failed to open file %d\n", __func__, pos.nFile);
return false;
}
if (finalize && !TruncateFile(file, pos.nPos)) {
fclose(file);
return error("%s: failed to truncate file %d", __func__, pos.nFile);
LogError("%s: failed to truncate file %d\n", __func__, pos.nFile);
return false;
}
if (!FileCommit(file)) {
fclose(file);
return error("%s: failed to commit file %d", __func__, pos.nFile);
LogError("%s: failed to commit file %d\n", __func__, pos.nFile);
return false;
}
DirectoryCommit(m_dir);

Expand Down
3 changes: 2 additions & 1 deletion src/index/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ bool BaseIndex::Commit()
{
CDBBatch batch(GetDB());
if (!CommitInternal(batch) || !GetDB().WriteBatch(batch)) {
return error("%s: Failed to commit latest %s state", __func__, GetName());
LogError("%s: Failed to commit latest %s state\n", __func__, GetName());
return false;
}
return true;
}
Expand Down
38 changes: 26 additions & 12 deletions src/index/blockfilterindex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@ bool BlockFilterIndex::Init()
// indicate database corruption or a disk failure, and starting the index would cause
// further corruption.
if (m_db->Exists(DB_FILTER_POS)) {
return error("%s: Cannot read current %s state; index may be corrupted",
LogError("%s: Cannot read current %s state; index may be corrupted\n",
__func__, GetName());
return false;
}

// If the DB_FILTER_POS is not set, then initialize to the first location.
Expand All @@ -169,10 +170,12 @@ bool BlockFilterIndex::CommitInternal(CDBBatch& batch)
// Flush current filter file to disk.
AutoFile file{m_filter_fileseq->Open(pos)};
if (file.IsNull()) {
return error("%s: Failed to open filter file %d", __func__, pos.nFile);
LogError("%s: Failed to open filter file %d\n", __func__, pos.nFile);
return false;
}
if (!FileCommit(file.Get())) {
return error("%s: Failed to commit filter file %d", __func__, pos.nFile);
LogError("%s: Failed to commit filter file %d\n", __func__, pos.nFile);
return false;
}

batch.Write(DB_FILTER_POS, pos);
Expand All @@ -191,11 +194,15 @@ bool BlockFilterIndex::ReadFilterFromDisk(const FlatFilePos& pos, const uint256&
std::vector<uint8_t> encoded_filter;
try {
filein >> block_hash >> encoded_filter;
if (Hash(encoded_filter) != hash) return error("Checksum mismatch in filter decode.");
if (Hash(encoded_filter) != hash) {
LogError("Checksum mismatch in filter decode.\n");
return false;
}
filter = BlockFilter(GetFilterType(), block_hash, std::move(encoded_filter), /*skip_decode_check=*/true);
}
catch (const std::exception& e) {
return error("%s: Failed to deserialize block filter from disk: %s", __func__, e.what());
LogError("%s: Failed to deserialize block filter from disk: %s\n", __func__, e.what());
return false;
}

return true;
Expand Down Expand Up @@ -264,8 +271,9 @@ bool BlockFilterIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex

uint256 expected_block_hash = pindex->pprev->GetBlockHash();
if (read_out.first != expected_block_hash) {
return error("%s: previous block header belongs to unexpected block %s; expected %s",
LogError("%s: previous block header belongs to unexpected block %s; expected %s\n",
__func__, read_out.first.ToString(), expected_block_hash.ToString());
return false;
}

prev_header = read_out.second.header;
Expand Down Expand Up @@ -299,14 +307,16 @@ bool BlockFilterIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex

for (int height = start_height; height <= stop_height; ++height) {
if (!db_it.GetKey(key) || key.height != height) {
return error("%s: unexpected key in %s: expected (%c, %d)",
LogError("%s: unexpected key in %s: expected (%c, %d)\n",
__func__, index_name, DB_BLOCK_HEIGHT, height);
return false;
}

std::pair<uint256, DBVal> value;
if (!db_it.GetValue(value)) {
return error("%s: unable to read value in %s at key (%c, %d)",
LogError("%s: unable to read value in %s at key (%c, %d)\n",
__func__, index_name, DB_BLOCK_HEIGHT, height);
return false;
}

batch.Write(DBHashKey(value.first), std::move(value.second));
Expand Down Expand Up @@ -361,11 +371,13 @@ static bool LookupRange(CDBWrapper& db, const std::string& index_name, int start
const CBlockIndex* stop_index, std::vector<DBVal>& results)
{
if (start_height < 0) {
return error("%s: start height (%d) is negative", __func__, start_height);
LogError("%s: start height (%d) is negative\n", __func__, start_height);
return false;
}
if (start_height > stop_index->nHeight) {
return error("%s: start height (%d) is greater than stop height (%d)",
LogError("%s: start height (%d) is greater than stop height (%d)\n",
__func__, start_height, stop_index->nHeight);
return false;
}

size_t results_size = static_cast<size_t>(stop_index->nHeight - start_height + 1);
Expand All @@ -381,8 +393,9 @@ static bool LookupRange(CDBWrapper& db, const std::string& index_name, int start

size_t i = static_cast<size_t>(height - start_height);
if (!db_it->GetValue(values[i])) {
return error("%s: unable to read value in %s at key (%c, %d)",
LogError("%s: unable to read value in %s at key (%c, %d)\n",
__func__, index_name, DB_BLOCK_HEIGHT, height);
return false;
}

db_it->Next();
Expand All @@ -404,8 +417,9 @@ static bool LookupRange(CDBWrapper& db, const std::string& index_name, int start
}

if (!db.Read(DBHashKey(block_hash), results[i])) {
return error("%s: unable to read value in %s at key (%c, %s)",
LogError("%s: unable to read value in %s at key (%c, %s)\n",
__func__, index_name, DB_BLOCK_HASH, block_hash.ToString());
return false;
}
}

Expand Down
24 changes: 16 additions & 8 deletions src/index/coinstatsindex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ bool CoinStatsIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)
read_out.first.ToString(), expected_block_hash.ToString());

if (!m_db->Read(DBHashKey(expected_block_hash), read_out)) {
return error("%s: previous block header not found; expected %s",
LogError("%s: previous block header not found; expected %s\n",
__func__, expected_block_hash.ToString());
return false;
}
}

Expand Down Expand Up @@ -241,14 +242,16 @@ bool CoinStatsIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)

for (int height = start_height; height <= stop_height; ++height) {
if (!db_it.GetKey(key) || key.height != height) {
return error("%s: unexpected key in %s: expected (%c, %d)",
LogError("%s: unexpected key in %s: expected (%c, %d)\n",
__func__, index_name, DB_BLOCK_HEIGHT, height);
return false;
}

std::pair<uint256, DBVal> value;
if (!db_it.GetValue(value)) {
return error("%s: unable to read value in %s at key (%c, %d)",
LogError("%s: unable to read value in %s at key (%c, %d)\n",
__func__, index_name, DB_BLOCK_HEIGHT, height);
return false;
}

batch.Write(DBHashKey(value.first), std::move(value.second));
Expand Down Expand Up @@ -283,8 +286,9 @@ bool CoinStatsIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* n
CBlock block;

if (!ReadBlockFromDisk(block, iter_tip, consensus_params)) {
return error("%s: Failed to read block %s from disk",
LogError("%s: Failed to read block %s from disk\n",
__func__, iter_tip->GetBlockHash().ToString());
return false;
}

if (!ReverseBlock(block, iter_tip)) {
Expand Down Expand Up @@ -351,8 +355,9 @@ bool CoinStatsIndex::Init()
// exist. Any other errors indicate database corruption or a disk
// failure, and starting the index would cause further corruption.
if (m_db->Exists(DB_MUHASH)) {
return error("%s: Cannot read current %s state; index may be corrupted",
LogError("%s: Cannot read current %s state; index may be corrupted\n",
__func__, GetName());
return false;
}
}

Expand All @@ -363,14 +368,16 @@ bool CoinStatsIndex::Init()
if (pindex) {
DBVal entry;
if (!LookUpOne(*m_db, pindex, entry)) {
return error("%s: Cannot read current %s state; index may be corrupted",
LogError("%s: Cannot read current %s state; index may be corrupted\n",
__func__, GetName());
return false;
}
uint256 out;
m_muhash.Finalize(out);
if (entry.muhash != out) {
return error("%s: Cannot read current %s state; index may be corrupted",
LogError("%s: Cannot read current %s state; index may be corrupted\n",
__func__, GetName());
return false;
}
m_transaction_output_count = entry.transaction_output_count;
m_bogo_size = entry.bogo_size;
Expand Down Expand Up @@ -422,8 +429,9 @@ bool CoinStatsIndex::ReverseBlock(const CBlock& block, const CBlockIndex* pindex
read_out.first.ToString(), expected_block_hash.ToString());

if (!m_db->Read(DBHashKey(expected_block_hash), read_out)) {
return error("%s: previous block header not found; expected %s",
LogError("%s: previous block header not found; expected %s\n",
__func__, expected_block_hash.ToString());
return false;
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/index/txindex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,24 @@ bool TxIndex::FindTx(const uint256& tx_hash, uint256& block_hash, CTransactionRe

AutoFile file{OpenBlockFile(postx, true)};
if (file.IsNull()) {
return error("%s: OpenBlockFile failed", __func__);
LogError("%s: OpenBlockFile failed\n", __func__);
return false;
}
CBlockHeader header;
try {
file >> header;
if (fseek(file.Get(), postx.nTxOffset, SEEK_CUR)) {
return error("%s: fseek(...) failed", __func__);
LogError("%s: fseek(...) failed\n", __func__);
return false;
}
file >> tx;
} catch (const std::exception& e) {
return error("%s: Deserialize or I/O error - %s", __func__, e.what());
LogError("%s: Deserialize or I/O error - %s\n", __func__, e.what());
return false;
}
if (tx->GetHash() != tx_hash) {
return error("%s: txid mismatch", __func__);
LogError("%s: txid mismatch\n", __func__);
return false;
}
block_hash = header.GetHash();
return true;
Expand Down
3 changes: 2 additions & 1 deletion src/kernel/coinstats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ static bool ComputeUTXOStats(CCoinsView* view, CCoinsStats& stats, T hash_obj, c
outputs[key.n] = std::move(coin);
stats.coins_count++;
} else {
return error("%s: unable to read value", __func__);
LogError("%s: unable to read value\n", __func__);
return false;
}
pcursor->Next();
}
Expand Down
Loading
Loading