mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-03 09:56:38 -05:00
Use AutoFile where possible
This commit is contained in:
parent
6666803c89
commit
facc2fa7b8
17 changed files with 46 additions and 49 deletions
|
@ -131,7 +131,7 @@ bool BlockFilterIndex::CommitInternal(CDBBatch& batch)
|
||||||
const FlatFilePos& pos = m_next_filter_pos;
|
const FlatFilePos& pos = m_next_filter_pos;
|
||||||
|
|
||||||
// Flush current filter file to disk.
|
// Flush current filter file to disk.
|
||||||
CAutoFile file(m_filter_fileseq->Open(pos), SER_DISK, CLIENT_VERSION);
|
AutoFile file{m_filter_fileseq->Open(pos)};
|
||||||
if (file.IsNull()) {
|
if (file.IsNull()) {
|
||||||
return error("%s: Failed to open filter file %d", __func__, pos.nFile);
|
return error("%s: Failed to open filter file %d", __func__, pos.nFile);
|
||||||
}
|
}
|
||||||
|
@ -145,7 +145,7 @@ bool BlockFilterIndex::CommitInternal(CDBBatch& batch)
|
||||||
|
|
||||||
bool BlockFilterIndex::ReadFilterFromDisk(const FlatFilePos& pos, BlockFilter& filter) const
|
bool BlockFilterIndex::ReadFilterFromDisk(const FlatFilePos& pos, BlockFilter& filter) const
|
||||||
{
|
{
|
||||||
CAutoFile filein(m_filter_fileseq->Open(pos, true), SER_DISK, CLIENT_VERSION);
|
AutoFile filein{m_filter_fileseq->Open(pos, true)};
|
||||||
if (filein.IsNull()) {
|
if (filein.IsNull()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -173,7 +173,7 @@ size_t BlockFilterIndex::WriteFilterToDisk(FlatFilePos& pos, const BlockFilter&
|
||||||
|
|
||||||
// If writing the filter would overflow the file, flush and move to the next one.
|
// If writing the filter would overflow the file, flush and move to the next one.
|
||||||
if (pos.nPos + data_size > MAX_FLTR_FILE_SIZE) {
|
if (pos.nPos + data_size > MAX_FLTR_FILE_SIZE) {
|
||||||
CAutoFile last_file(m_filter_fileseq->Open(pos), SER_DISK, CLIENT_VERSION);
|
AutoFile last_file{m_filter_fileseq->Open(pos)};
|
||||||
if (last_file.IsNull()) {
|
if (last_file.IsNull()) {
|
||||||
LogPrintf("%s: Failed to open filter file %d\n", __func__, pos.nFile);
|
LogPrintf("%s: Failed to open filter file %d\n", __func__, pos.nFile);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -199,7 +199,7 @@ size_t BlockFilterIndex::WriteFilterToDisk(FlatFilePos& pos, const BlockFilter&
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
CAutoFile fileout(m_filter_fileseq->Open(pos), SER_DISK, CLIENT_VERSION);
|
AutoFile fileout{m_filter_fileseq->Open(pos)};
|
||||||
if (fileout.IsNull()) {
|
if (fileout.IsNull()) {
|
||||||
LogPrintf("%s: Failed to open filter file %d\n", __func__, pos.nFile);
|
LogPrintf("%s: Failed to open filter file %d\n", __func__, pos.nFile);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -3054,7 +3054,7 @@ void CaptureMessageToFile(const CAddress& addr,
|
||||||
fs::create_directories(base_path);
|
fs::create_directories(base_path);
|
||||||
|
|
||||||
fs::path path = base_path / (is_incoming ? "msgs_recv.dat" : "msgs_sent.dat");
|
fs::path path = base_path / (is_incoming ? "msgs_recv.dat" : "msgs_sent.dat");
|
||||||
CAutoFile f(fsbridge::fopen(path, "ab"), SER_DISK, CLIENT_VERSION);
|
AutoFile f{fsbridge::fopen(path, "ab")};
|
||||||
|
|
||||||
ser_writedata64(f, now.count());
|
ser_writedata64(f, now.count());
|
||||||
f.write(MakeByteSpan(msg_type));
|
f.write(MakeByteSpan(msg_type));
|
||||||
|
|
|
@ -161,13 +161,13 @@ public:
|
||||||
unsigned int GetMaxConfirms() const { return scale * confAvg.size(); }
|
unsigned int GetMaxConfirms() const { return scale * confAvg.size(); }
|
||||||
|
|
||||||
/** Write state of estimation data to a file*/
|
/** Write state of estimation data to a file*/
|
||||||
void Write(CAutoFile& fileout) const;
|
void Write(AutoFile& fileout) const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read saved state of estimation data from a file and replace all internal data structures and
|
* Read saved state of estimation data from a file and replace all internal data structures and
|
||||||
* variables with this state.
|
* variables with this state.
|
||||||
*/
|
*/
|
||||||
void Read(CAutoFile& filein, int nFileVersion, size_t numBuckets);
|
void Read(AutoFile& filein, int nFileVersion, size_t numBuckets);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -390,7 +390,7 @@ double TxConfirmStats::EstimateMedianVal(int confTarget, double sufficientTxVal,
|
||||||
return median;
|
return median;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TxConfirmStats::Write(CAutoFile& fileout) const
|
void TxConfirmStats::Write(AutoFile& fileout) const
|
||||||
{
|
{
|
||||||
fileout << Using<EncodedDoubleFormatter>(decay);
|
fileout << Using<EncodedDoubleFormatter>(decay);
|
||||||
fileout << scale;
|
fileout << scale;
|
||||||
|
@ -400,7 +400,7 @@ void TxConfirmStats::Write(CAutoFile& fileout) const
|
||||||
fileout << Using<VectorFormatter<VectorFormatter<EncodedDoubleFormatter>>>(failAvg);
|
fileout << Using<VectorFormatter<VectorFormatter<EncodedDoubleFormatter>>>(failAvg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TxConfirmStats::Read(CAutoFile& filein, int nFileVersion, size_t numBuckets)
|
void TxConfirmStats::Read(AutoFile& filein, int nFileVersion, size_t numBuckets)
|
||||||
{
|
{
|
||||||
// Read data file and do some very basic sanity checking
|
// Read data file and do some very basic sanity checking
|
||||||
// buckets and bucketMap are not updated yet, so don't access them
|
// buckets and bucketMap are not updated yet, so don't access them
|
||||||
|
@ -546,7 +546,7 @@ CBlockPolicyEstimator::CBlockPolicyEstimator(const fs::path& estimation_filepath
|
||||||
longStats = std::unique_ptr<TxConfirmStats>(new TxConfirmStats(buckets, bucketMap, LONG_BLOCK_PERIODS, LONG_DECAY, LONG_SCALE));
|
longStats = std::unique_ptr<TxConfirmStats>(new TxConfirmStats(buckets, bucketMap, LONG_BLOCK_PERIODS, LONG_DECAY, LONG_SCALE));
|
||||||
|
|
||||||
// If the fee estimation file is present, read recorded estimations
|
// If the fee estimation file is present, read recorded estimations
|
||||||
CAutoFile est_file(fsbridge::fopen(m_estimation_filepath, "rb"), SER_DISK, CLIENT_VERSION);
|
AutoFile est_file{fsbridge::fopen(m_estimation_filepath, "rb")};
|
||||||
if (est_file.IsNull() || !Read(est_file)) {
|
if (est_file.IsNull() || !Read(est_file)) {
|
||||||
LogPrintf("Failed to read fee estimates from %s. Continue anyway.\n", fs::PathToString(m_estimation_filepath));
|
LogPrintf("Failed to read fee estimates from %s. Continue anyway.\n", fs::PathToString(m_estimation_filepath));
|
||||||
}
|
}
|
||||||
|
@ -904,13 +904,13 @@ CFeeRate CBlockPolicyEstimator::estimateSmartFee(int confTarget, FeeCalculation
|
||||||
void CBlockPolicyEstimator::Flush() {
|
void CBlockPolicyEstimator::Flush() {
|
||||||
FlushUnconfirmed();
|
FlushUnconfirmed();
|
||||||
|
|
||||||
CAutoFile est_file(fsbridge::fopen(m_estimation_filepath, "wb"), SER_DISK, CLIENT_VERSION);
|
AutoFile est_file{fsbridge::fopen(m_estimation_filepath, "wb")};
|
||||||
if (est_file.IsNull() || !Write(est_file)) {
|
if (est_file.IsNull() || !Write(est_file)) {
|
||||||
LogPrintf("Failed to write fee estimates to %s. Continue anyway.\n", fs::PathToString(m_estimation_filepath));
|
LogPrintf("Failed to write fee estimates to %s. Continue anyway.\n", fs::PathToString(m_estimation_filepath));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CBlockPolicyEstimator::Write(CAutoFile& fileout) const
|
bool CBlockPolicyEstimator::Write(AutoFile& fileout) const
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
LOCK(m_cs_fee_estimator);
|
LOCK(m_cs_fee_estimator);
|
||||||
|
@ -935,7 +935,7 @@ bool CBlockPolicyEstimator::Write(CAutoFile& fileout) const
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CBlockPolicyEstimator::Read(CAutoFile& filein)
|
bool CBlockPolicyEstimator::Read(AutoFile& filein)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
LOCK(m_cs_fee_estimator);
|
LOCK(m_cs_fee_estimator);
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
class CAutoFile;
|
class AutoFile;
|
||||||
class CTxMemPoolEntry;
|
class CTxMemPoolEntry;
|
||||||
class TxConfirmStats;
|
class TxConfirmStats;
|
||||||
|
|
||||||
|
@ -220,11 +220,11 @@ public:
|
||||||
EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator);
|
EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator);
|
||||||
|
|
||||||
/** Write estimation data to a file */
|
/** Write estimation data to a file */
|
||||||
bool Write(CAutoFile& fileout) const
|
bool Write(AutoFile& fileout) const
|
||||||
EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator);
|
EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator);
|
||||||
|
|
||||||
/** Read estimation data from a file */
|
/** Read estimation data from a file */
|
||||||
bool Read(CAutoFile& filein)
|
bool Read(AutoFile& filein)
|
||||||
EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator);
|
EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator);
|
||||||
|
|
||||||
/** Empty mempool transactions on shutdown to record failure to confirm for txs still in mempool */
|
/** Empty mempool transactions on shutdown to record failure to confirm for txs still in mempool */
|
||||||
|
|
|
@ -2293,7 +2293,7 @@ static RPCHelpMan dumptxoutset()
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE* file{fsbridge::fopen(temppath, "wb")};
|
FILE* file{fsbridge::fopen(temppath, "wb")};
|
||||||
CAutoFile afile{file, SER_DISK, CLIENT_VERSION};
|
AutoFile afile{file};
|
||||||
if (afile.IsNull()) {
|
if (afile.IsNull()) {
|
||||||
throw JSONRPCError(
|
throw JSONRPCError(
|
||||||
RPC_INVALID_PARAMETER,
|
RPC_INVALID_PARAMETER,
|
||||||
|
@ -2314,7 +2314,7 @@ static RPCHelpMan dumptxoutset()
|
||||||
UniValue CreateUTXOSnapshot(
|
UniValue CreateUTXOSnapshot(
|
||||||
NodeContext& node,
|
NodeContext& node,
|
||||||
CChainState& chainstate,
|
CChainState& chainstate,
|
||||||
CAutoFile& afile,
|
AutoFile& afile,
|
||||||
const fs::path& path,
|
const fs::path& path,
|
||||||
const fs::path& temppath)
|
const fs::path& temppath)
|
||||||
{
|
{
|
||||||
|
|
|
@ -55,7 +55,7 @@ void CalculatePercentilesByWeight(CAmount result[NUM_GETBLOCKSTATS_PERCENTILES],
|
||||||
UniValue CreateUTXOSnapshot(
|
UniValue CreateUTXOSnapshot(
|
||||||
node::NodeContext& node,
|
node::NodeContext& node,
|
||||||
CChainState& chainstate,
|
CChainState& chainstate,
|
||||||
CAutoFile& afile,
|
AutoFile& afile,
|
||||||
const fs::path& path,
|
const fs::path& path,
|
||||||
const fs::path& tmppath);
|
const fs::path& tmppath);
|
||||||
|
|
||||||
|
|
|
@ -41,26 +41,26 @@ BOOST_AUTO_TEST_CASE(flatfile_open)
|
||||||
|
|
||||||
// Write first line to file.
|
// Write first line to file.
|
||||||
{
|
{
|
||||||
CAutoFile file(seq.Open(FlatFilePos(0, pos1)), SER_DISK, CLIENT_VERSION);
|
AutoFile file{seq.Open(FlatFilePos(0, pos1))};
|
||||||
file << LIMITED_STRING(line1, 256);
|
file << LIMITED_STRING(line1, 256);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attempt to append to file opened in read-only mode.
|
// Attempt to append to file opened in read-only mode.
|
||||||
{
|
{
|
||||||
CAutoFile file(seq.Open(FlatFilePos(0, pos2), true), SER_DISK, CLIENT_VERSION);
|
AutoFile file{seq.Open(FlatFilePos(0, pos2), true)};
|
||||||
BOOST_CHECK_THROW(file << LIMITED_STRING(line2, 256), std::ios_base::failure);
|
BOOST_CHECK_THROW(file << LIMITED_STRING(line2, 256), std::ios_base::failure);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Append second line to file.
|
// Append second line to file.
|
||||||
{
|
{
|
||||||
CAutoFile file(seq.Open(FlatFilePos(0, pos2)), SER_DISK, CLIENT_VERSION);
|
AutoFile file{seq.Open(FlatFilePos(0, pos2))};
|
||||||
file << LIMITED_STRING(line2, 256);
|
file << LIMITED_STRING(line2, 256);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read text from file in read-only mode.
|
// Read text from file in read-only mode.
|
||||||
{
|
{
|
||||||
std::string text;
|
std::string text;
|
||||||
CAutoFile file(seq.Open(FlatFilePos(0, pos1), true), SER_DISK, CLIENT_VERSION);
|
AutoFile file{seq.Open(FlatFilePos(0, pos1), true)};
|
||||||
|
|
||||||
file >> LIMITED_STRING(text, 256);
|
file >> LIMITED_STRING(text, 256);
|
||||||
BOOST_CHECK_EQUAL(text, line1);
|
BOOST_CHECK_EQUAL(text, line1);
|
||||||
|
@ -72,7 +72,7 @@ BOOST_AUTO_TEST_CASE(flatfile_open)
|
||||||
// Read text from file with position offset.
|
// Read text from file with position offset.
|
||||||
{
|
{
|
||||||
std::string text;
|
std::string text;
|
||||||
CAutoFile file(seq.Open(FlatFilePos(0, pos2)), SER_DISK, CLIENT_VERSION);
|
AutoFile file{seq.Open(FlatFilePos(0, pos2))};
|
||||||
|
|
||||||
file >> LIMITED_STRING(text, 256);
|
file >> LIMITED_STRING(text, 256);
|
||||||
BOOST_CHECK_EQUAL(text, line2);
|
BOOST_CHECK_EQUAL(text, line2);
|
||||||
|
@ -81,7 +81,7 @@ BOOST_AUTO_TEST_CASE(flatfile_open)
|
||||||
// Ensure another file in the sequence has no data.
|
// Ensure another file in the sequence has no data.
|
||||||
{
|
{
|
||||||
std::string text;
|
std::string text;
|
||||||
CAutoFile file(seq.Open(FlatFilePos(1, pos2)), SER_DISK, CLIENT_VERSION);
|
AutoFile file{seq.Open(FlatFilePos(1, pos2))};
|
||||||
BOOST_CHECK_THROW(file >> LIMITED_STRING(text, 256), std::ios_base::failure);
|
BOOST_CHECK_THROW(file >> LIMITED_STRING(text, 256), std::ios_base::failure);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ FUZZ_TARGET(autofile)
|
||||||
{
|
{
|
||||||
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
|
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
|
||||||
FuzzedAutoFileProvider fuzzed_auto_file_provider = ConsumeAutoFile(fuzzed_data_provider);
|
FuzzedAutoFileProvider fuzzed_auto_file_provider = ConsumeAutoFile(fuzzed_data_provider);
|
||||||
CAutoFile auto_file = fuzzed_auto_file_provider.open();
|
AutoFile auto_file{fuzzed_auto_file_provider.open()};
|
||||||
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
|
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
|
||||||
CallOneOf(
|
CallOneOf(
|
||||||
fuzzed_data_provider,
|
fuzzed_data_provider,
|
||||||
|
@ -53,8 +53,6 @@ FUZZ_TARGET(autofile)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
(void)auto_file.Get();
|
(void)auto_file.Get();
|
||||||
(void)auto_file.GetType();
|
|
||||||
(void)auto_file.GetVersion();
|
|
||||||
(void)auto_file.IsNull();
|
(void)auto_file.IsNull();
|
||||||
if (fuzzed_data_provider.ConsumeBool()) {
|
if (fuzzed_data_provider.ConsumeBool()) {
|
||||||
FILE* f = auto_file.release();
|
FILE* f = auto_file.release();
|
||||||
|
|
|
@ -76,7 +76,7 @@ FUZZ_TARGET_INIT(policy_estimator, initialize_policy_estimator)
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
FuzzedAutoFileProvider fuzzed_auto_file_provider = ConsumeAutoFile(fuzzed_data_provider);
|
FuzzedAutoFileProvider fuzzed_auto_file_provider = ConsumeAutoFile(fuzzed_data_provider);
|
||||||
CAutoFile fuzzed_auto_file = fuzzed_auto_file_provider.open();
|
AutoFile fuzzed_auto_file{fuzzed_auto_file_provider.open()};
|
||||||
block_policy_estimator.Write(fuzzed_auto_file);
|
block_policy_estimator.Write(fuzzed_auto_file);
|
||||||
block_policy_estimator.Read(fuzzed_auto_file);
|
block_policy_estimator.Read(fuzzed_auto_file);
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ FUZZ_TARGET_INIT(policy_estimator_io, initialize_policy_estimator_io)
|
||||||
{
|
{
|
||||||
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
|
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
|
||||||
FuzzedAutoFileProvider fuzzed_auto_file_provider = ConsumeAutoFile(fuzzed_data_provider);
|
FuzzedAutoFileProvider fuzzed_auto_file_provider = ConsumeAutoFile(fuzzed_data_provider);
|
||||||
CAutoFile fuzzed_auto_file = fuzzed_auto_file_provider.open();
|
AutoFile fuzzed_auto_file{fuzzed_auto_file_provider.open()};
|
||||||
// Re-using block_policy_estimator across runs to avoid costly creation of CBlockPolicyEstimator object.
|
// Re-using block_policy_estimator across runs to avoid costly creation of CBlockPolicyEstimator object.
|
||||||
static CBlockPolicyEstimator block_policy_estimator{FeeestPath(*g_setup->m_node.args)};
|
static CBlockPolicyEstimator block_policy_estimator{FeeestPath(*g_setup->m_node.args)};
|
||||||
if (block_policy_estimator.Read(fuzzed_auto_file)) {
|
if (block_policy_estimator.Read(fuzzed_auto_file)) {
|
||||||
|
|
|
@ -361,17 +361,16 @@ public:
|
||||||
|
|
||||||
class FuzzedAutoFileProvider
|
class FuzzedAutoFileProvider
|
||||||
{
|
{
|
||||||
FuzzedDataProvider& m_fuzzed_data_provider;
|
|
||||||
FuzzedFileProvider m_fuzzed_file_provider;
|
FuzzedFileProvider m_fuzzed_file_provider;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
FuzzedAutoFileProvider(FuzzedDataProvider& fuzzed_data_provider) : m_fuzzed_data_provider{fuzzed_data_provider}, m_fuzzed_file_provider{fuzzed_data_provider}
|
FuzzedAutoFileProvider(FuzzedDataProvider& fuzzed_data_provider) : m_fuzzed_file_provider{fuzzed_data_provider}
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
CAutoFile open()
|
AutoFile open()
|
||||||
{
|
{
|
||||||
return {m_fuzzed_file_provider.open(), m_fuzzed_data_provider.ConsumeIntegral<int>(), m_fuzzed_data_provider.ConsumeIntegral<int>()};
|
return AutoFile{m_fuzzed_file_provider.open()};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -39,13 +39,13 @@ FUZZ_TARGET_INIT(utxo_snapshot, initialize_chain)
|
||||||
Assert(!chainman.SnapshotBlockhash());
|
Assert(!chainman.SnapshotBlockhash());
|
||||||
|
|
||||||
{
|
{
|
||||||
CAutoFile outfile{fsbridge::fopen(snapshot_path, "wb"), SER_DISK, CLIENT_VERSION};
|
AutoFile outfile{fsbridge::fopen(snapshot_path, "wb")};
|
||||||
const auto file_data{ConsumeRandomLengthByteVector(fuzzed_data_provider)};
|
const auto file_data{ConsumeRandomLengthByteVector(fuzzed_data_provider)};
|
||||||
outfile << Span{file_data};
|
outfile << Span{file_data};
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto ActivateFuzzedSnapshot{[&] {
|
const auto ActivateFuzzedSnapshot{[&] {
|
||||||
CAutoFile infile{fsbridge::fopen(snapshot_path, "rb"), SER_DISK, CLIENT_VERSION};
|
AutoFile infile{fsbridge::fopen(snapshot_path, "rb")};
|
||||||
SnapshotMetadata metadata;
|
SnapshotMetadata metadata;
|
||||||
try {
|
try {
|
||||||
infile >> metadata;
|
infile >> metadata;
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
#include <boost/test/unit_test.hpp>
|
#include <boost/test/unit_test.hpp>
|
||||||
|
|
||||||
const auto NoMalleation = [](CAutoFile& file, node::SnapshotMetadata& meta){};
|
const auto NoMalleation = [](AutoFile& file, node::SnapshotMetadata& meta){};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create and activate a UTXO snapshot, optionally providing a function to
|
* Create and activate a UTXO snapshot, optionally providing a function to
|
||||||
|
@ -32,7 +32,7 @@ CreateAndActivateUTXOSnapshot(node::NodeContext& node, const fs::path root, F ma
|
||||||
WITH_LOCK(::cs_main, height = node.chainman->ActiveHeight());
|
WITH_LOCK(::cs_main, height = node.chainman->ActiveHeight());
|
||||||
fs::path snapshot_path = root / fs::u8path(tfm::format("test_snapshot.%d.dat", height));
|
fs::path snapshot_path = root / fs::u8path(tfm::format("test_snapshot.%d.dat", height));
|
||||||
FILE* outfile{fsbridge::fopen(snapshot_path, "wb")};
|
FILE* outfile{fsbridge::fopen(snapshot_path, "wb")};
|
||||||
CAutoFile auto_outfile{outfile, SER_DISK, CLIENT_VERSION};
|
AutoFile auto_outfile{outfile};
|
||||||
|
|
||||||
UniValue result = CreateUTXOSnapshot(
|
UniValue result = CreateUTXOSnapshot(
|
||||||
node, node.chainman->ActiveChainstate(), auto_outfile, snapshot_path, snapshot_path);
|
node, node.chainman->ActiveChainstate(), auto_outfile, snapshot_path, snapshot_path);
|
||||||
|
@ -42,7 +42,7 @@ CreateAndActivateUTXOSnapshot(node::NodeContext& node, const fs::path root, F ma
|
||||||
// Read the written snapshot in and then activate it.
|
// Read the written snapshot in and then activate it.
|
||||||
//
|
//
|
||||||
FILE* infile{fsbridge::fopen(snapshot_path, "rb")};
|
FILE* infile{fsbridge::fopen(snapshot_path, "rb")};
|
||||||
CAutoFile auto_infile{infile, SER_DISK, CLIENT_VERSION};
|
AutoFile auto_infile{infile};
|
||||||
node::SnapshotMetadata metadata;
|
node::SnapshotMetadata metadata;
|
||||||
auto_infile >> metadata;
|
auto_infile >> metadata;
|
||||||
|
|
||||||
|
|
|
@ -193,7 +193,7 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_activate_snapshot, TestChain100Setup)
|
||||||
|
|
||||||
// Should not load malleated snapshots
|
// Should not load malleated snapshots
|
||||||
BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
|
BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
|
||||||
m_node, m_path_root, [](CAutoFile& auto_infile, SnapshotMetadata& metadata) {
|
m_node, m_path_root, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
|
||||||
// A UTXO is missing but count is correct
|
// A UTXO is missing but count is correct
|
||||||
metadata.m_coins_count -= 1;
|
metadata.m_coins_count -= 1;
|
||||||
|
|
||||||
|
@ -204,22 +204,22 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_activate_snapshot, TestChain100Setup)
|
||||||
auto_infile >> coin;
|
auto_infile >> coin;
|
||||||
}));
|
}));
|
||||||
BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
|
BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
|
||||||
m_node, m_path_root, [](CAutoFile& auto_infile, SnapshotMetadata& metadata) {
|
m_node, m_path_root, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
|
||||||
// Coins count is larger than coins in file
|
// Coins count is larger than coins in file
|
||||||
metadata.m_coins_count += 1;
|
metadata.m_coins_count += 1;
|
||||||
}));
|
}));
|
||||||
BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
|
BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
|
||||||
m_node, m_path_root, [](CAutoFile& auto_infile, SnapshotMetadata& metadata) {
|
m_node, m_path_root, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
|
||||||
// Coins count is smaller than coins in file
|
// Coins count is smaller than coins in file
|
||||||
metadata.m_coins_count -= 1;
|
metadata.m_coins_count -= 1;
|
||||||
}));
|
}));
|
||||||
BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
|
BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
|
||||||
m_node, m_path_root, [](CAutoFile& auto_infile, SnapshotMetadata& metadata) {
|
m_node, m_path_root, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
|
||||||
// Wrong hash
|
// Wrong hash
|
||||||
metadata.m_base_blockhash = uint256::ZERO;
|
metadata.m_base_blockhash = uint256::ZERO;
|
||||||
}));
|
}));
|
||||||
BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
|
BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
|
||||||
m_node, m_path_root, [](CAutoFile& auto_infile, SnapshotMetadata& metadata) {
|
m_node, m_path_root, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
|
||||||
// Wrong hash
|
// Wrong hash
|
||||||
metadata.m_base_blockhash = uint256::ONE;
|
metadata.m_base_blockhash = uint256::ONE;
|
||||||
}));
|
}));
|
||||||
|
|
|
@ -195,7 +195,7 @@ std::vector<bool> DecodeAsmap(fs::path path)
|
||||||
{
|
{
|
||||||
std::vector<bool> bits;
|
std::vector<bool> bits;
|
||||||
FILE *filestr = fsbridge::fopen(path, "rb");
|
FILE *filestr = fsbridge::fopen(path, "rb");
|
||||||
CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
|
AutoFile file{filestr};
|
||||||
if (file.IsNull()) {
|
if (file.IsNull()) {
|
||||||
LogPrintf("Failed to open asmap file from disk\n");
|
LogPrintf("Failed to open asmap file from disk\n");
|
||||||
return bits;
|
return bits;
|
||||||
|
|
|
@ -4864,7 +4864,7 @@ const AssumeutxoData* ExpectedAssumeutxo(
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ChainstateManager::ActivateSnapshot(
|
bool ChainstateManager::ActivateSnapshot(
|
||||||
CAutoFile& coins_file,
|
AutoFile& coins_file,
|
||||||
const SnapshotMetadata& metadata,
|
const SnapshotMetadata& metadata,
|
||||||
bool in_memory)
|
bool in_memory)
|
||||||
{
|
{
|
||||||
|
@ -4959,7 +4959,7 @@ static void FlushSnapshotToDisk(CCoinsViewCache& coins_cache, bool snapshot_load
|
||||||
|
|
||||||
bool ChainstateManager::PopulateAndValidateSnapshot(
|
bool ChainstateManager::PopulateAndValidateSnapshot(
|
||||||
CChainState& snapshot_chainstate,
|
CChainState& snapshot_chainstate,
|
||||||
CAutoFile& coins_file,
|
AutoFile& coins_file,
|
||||||
const SnapshotMetadata& metadata)
|
const SnapshotMetadata& metadata)
|
||||||
{
|
{
|
||||||
// It's okay to release cs_main before we're done using `coins_cache` because we know
|
// It's okay to release cs_main before we're done using `coins_cache` because we know
|
||||||
|
|
|
@ -820,7 +820,7 @@ private:
|
||||||
//! Internal helper for ActivateSnapshot().
|
//! Internal helper for ActivateSnapshot().
|
||||||
[[nodiscard]] bool PopulateAndValidateSnapshot(
|
[[nodiscard]] bool PopulateAndValidateSnapshot(
|
||||||
CChainState& snapshot_chainstate,
|
CChainState& snapshot_chainstate,
|
||||||
CAutoFile& coins_file,
|
AutoFile& coins_file,
|
||||||
const node::SnapshotMetadata& metadata);
|
const node::SnapshotMetadata& metadata);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -909,7 +909,7 @@ public:
|
||||||
//! - Move the new chainstate to `m_snapshot_chainstate` and make it our
|
//! - Move the new chainstate to `m_snapshot_chainstate` and make it our
|
||||||
//! ChainstateActive().
|
//! ChainstateActive().
|
||||||
[[nodiscard]] bool ActivateSnapshot(
|
[[nodiscard]] bool ActivateSnapshot(
|
||||||
CAutoFile& coins_file, const node::SnapshotMetadata& metadata, bool in_memory);
|
AutoFile& coins_file, const node::SnapshotMetadata& metadata, bool in_memory);
|
||||||
|
|
||||||
//! The most-work chain.
|
//! The most-work chain.
|
||||||
CChainState& ActiveChainstate() const;
|
CChainState& ActiveChainstate() const;
|
||||||
|
|
Loading…
Add table
Reference in a new issue