Skip to content

Commit a3757b2

Browse files
committed
WIP: impl swiftsync in ChainState
1 parent 0b00908 commit a3757b2

File tree

8 files changed

+62
-11
lines changed

8 files changed

+62
-11
lines changed

src/init.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <bitcoin-build-config.h> // IWYU pragma: keep
77

8+
#include <filesystem>
89
#include <init.h>
910

1011
#include <kernel/checks.h>
@@ -479,6 +480,7 @@ void SetupServerArgs(ArgsManager& argsman, bool can_listen_ipc)
479480
argsman.AddArg("-alertnotify=<cmd>", "Execute command when an alert is raised (%s in cmd is replaced by message)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
480481
#endif
481482
argsman.AddArg("-assumevalid=<hex>", strprintf("If this block is in the chain assume that it and its ancestors are valid and potentially skip their script verification (0 to verify all, default: %s, testnet3: %s, testnet4: %s, signet: %s)", defaultChainParams->GetConsensus().defaultAssumeValid.GetHex(), testnetChainParams->GetConsensus().defaultAssumeValid.GetHex(), testnet4ChainParams->GetConsensus().defaultAssumeValid.GetHex(), signetChainParams->GetConsensus().defaultAssumeValid.GetHex()), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
483+
argsman.AddArg("-utxohints=<file>", "Specify hints of the UTXO set with a hintfile.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
482484
argsman.AddArg("-blocksdir=<dir>", "Specify directory to hold blocks subdirectory for *.dat files (default: <datadir>)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
483485
argsman.AddArg("-blocksxor",
484486
strprintf("Whether an XOR-key applies to blocksdir *.dat files. "
@@ -1089,6 +1091,13 @@ bool AppInitParameterInteraction(const ArgsManager& args)
10891091
}
10901092
}
10911093

1094+
if (args.IsArgSet("-utxohints")) {
1095+
fs::path file_path = fs::absolute(args.GetPathArg("-utxohints"));
1096+
if (!fs::exists(file_path)) {
1097+
return InitError(Untranslated("Provided UTXO hints file doesn't exist"));
1098+
}
1099+
}
1100+
10921101
// Also report errors from parsing before daemonization
10931102
{
10941103
kernel::Notifications notifications{};

src/kernel/chainstatemanager_opts.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef BITCOIN_KERNEL_CHAINSTATEMANAGER_OPTS_H
66
#define BITCOIN_KERNEL_CHAINSTATEMANAGER_OPTS_H
77

8+
#include "util/fs.h"
89
#include <kernel/notifications_interface.h>
910

1011
#include <arith_uint256.h>
@@ -15,7 +16,6 @@
1516
#include <util/time.h>
1617

1718
#include <cstdint>
18-
#include <functional>
1919
#include <optional>
2020

2121
class CChainParams;
@@ -32,6 +32,7 @@ namespace kernel {
3232
*/
3333
struct ChainstateManagerOpts {
3434
const CChainParams& chainparams;
35+
std::optional<fs::path> utxo_hints{};
3536
fs::path datadir;
3637
std::optional<int32_t> check_block_index{};
3738
//! If set, it will override the minimum work we will assume exists on some valid chain.

src/node/chainstatemanager_args.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ util::Result<void> ApplyArgsManOptions(const ArgsManager& args, ChainstateManage
6969
opts.signature_cache_bytes = clamped_size_each;
7070
}
7171

72+
if (args.IsArgSet("-utxohints")) {
73+
opts.utxo_hints = fs::absolute(args.GetPathArg("-ibdboosterfile"));
74+
}
75+
7276
return {};
7377
}
7478
} // namespace node

src/rpc/blockchain.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
#include <script/descriptor.h>
4040
#include <serialize.h>
4141
#include <streams.h>
42-
#include <swiftsync.h>
4342
#include <sync.h>
4443
#include <tinyformat.h>
4544
#include <txdb.h>

src/swiftsync.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ Hintfile::Hintfile(AutoFile& file) : m_file(file.release())
4141
m_file >> m_stop_hash;
4242
}
4343

44-
Hintfile Hintfile::FromExisting(AutoFile& file)
45-
{
46-
return Hintfile(file);
47-
}
48-
4944
Hintfile::Hintfile(AutoFile& file, const uint256& stop_hash, const uint32_t& stop_height) : m_file(file.release())
5045
{
5146
m_file << stop_height;
@@ -94,3 +89,10 @@ bool Hintfile::WriteNextBlock(const std::vector<uint64_t>& unspent_offsets)
9489
}
9590
return m_file.Commit();
9691
}
92+
93+
Context::Context() = default;
94+
95+
void Context::LoadHints(AutoFile& file)
96+
{
97+
m_hintfile.emplace(file);
98+
}

src/swiftsync.h

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <cstdint>
22
#include <hash.h>
3+
#include <optional>
34
#include <primitives/transaction.h>
45
#include <streams.h>
56
#include <uint256.h>
@@ -51,12 +52,10 @@ class Hintfile
5152
AutoFile m_file;
5253
uint256 m_stop_hash;
5354
uint32_t m_stop_height;
54-
Hintfile(AutoFile& file);
5555
Hintfile(AutoFile& file, const uint256& stop_hash, const uint32_t& stop_height);
5656

5757
public:
58-
// Construct a hint file and immediately read the stop hash and height.
59-
static Hintfile FromExisting(AutoFile& file);
58+
Hintfile(AutoFile& file);
6059
// Construct a hint file and write the stop hash and height.
6160
static Hintfile CreateNew(AutoFile& file, const uint256& stop_hash, const uint32_t& stop_height);
6261
BlockUnspentHints ReadNextBlock();
@@ -65,4 +64,25 @@ class Hintfile
6564
uint256 StopHash() { return m_stop_hash; };
6665
uint32_t StopHeight() { return m_stop_height; };
6766
};
68-
}
67+
68+
class Context
69+
{
70+
private:
71+
std::optional<Hintfile> m_hintfile{};
72+
Aggregate m_agg{};
73+
bool m_connecting_to_genesis{false};
74+
75+
public:
76+
Context();
77+
bool IsStartBlockGenesis() { return m_connecting_to_genesis; };
78+
bool IsActive() { return m_connecting_to_genesis && m_hintfile.has_value(); };
79+
void LoadHints(AutoFile& file);
80+
void StartBlockIsGenesis() { m_connecting_to_genesis = true; };
81+
uint256 StopHash() { return m_hintfile->StopHash(); };
82+
uint32_t StopHeight() { return m_hintfile->StopHeight(); };
83+
BlockUnspentHints ReadNextBlock() { return m_hintfile->ReadNextBlock(); };
84+
void Add(const COutPoint& outpoint) { m_agg.Add(outpoint); };
85+
void Spend(const COutPoint& outpoint) { m_agg.Spend(outpoint); };
86+
bool EmptySet() { return m_agg.IsZero(); };
87+
};
88+
} // namespace swiftsync

src/validation.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,6 +1987,13 @@ void Chainstate::InitCoinsCache(size_t cache_size_bytes)
19871987
m_coins_views->InitCache();
19881988
}
19891989

1990+
void Chainstate::AddUtxoHints(const fs::path& path)
1991+
{
1992+
FILE* file{fsbridge::fopen(path, "rb")};
1993+
AutoFile afile{file};
1994+
m_swiftsync_ctx.LoadHints(afile);
1995+
}
1996+
19901997
// Note that though this is marked const, we may end up modifying `m_cached_finished_ibd`, which
19911998
// is a performance-related implementation detail. This function must be marked
19921999
// `const` so that `CValidationInterface` clients (which are given a `const Chainstate*`)
@@ -2417,6 +2424,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
24172424
// Special case for the genesis block, skipping connection of its transactions
24182425
// (its coinbase is unspendable)
24192426
if (block_hash == params.GetConsensus().hashGenesisBlock) {
2427+
m_swiftsync_ctx.StartBlockIsGenesis();
24202428
if (!fJustCheck)
24212429
view.SetBestBlock(pindex->GetBlockHash());
24222430
return true;
@@ -5668,6 +5676,9 @@ Chainstate& ChainstateManager::InitializeChainstate(CTxMemPool* mempool)
56685676
assert(!m_active_chainstate);
56695677

56705678
m_ibd_chainstate = std::make_unique<Chainstate>(mempool, m_blockman, *this);
5679+
if (m_options.utxo_hints) {
5680+
m_ibd_chainstate->AddUtxoHints(m_options.utxo_hints.value());
5681+
}
56715682
m_active_chainstate = m_ibd_chainstate.get();
56725683
return *m_active_chainstate;
56735684
}

src/validation.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <script/sigcache.h>
2626
#include <script/verify_flags.h>
2727
#include <sync.h>
28+
#include <swiftsync.h>
2829
#include <txdb.h>
2930
#include <txmempool.h>
3031
#include <uint256.h>
@@ -563,6 +564,8 @@ class Chainstate
563564

564565
std::optional<const char*> m_last_script_check_reason_logged GUARDED_BY(::cs_main){};
565566

567+
swiftsync::Context m_swiftsync_ctx {};
568+
566569
public:
567570
//! Reference to a BlockManager instance which itself is shared across all
568571
//! Chainstate instances.
@@ -792,6 +795,8 @@ class Chainstate
792795

793796
std::string ToString() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
794797

798+
void AddUtxoHints(const fs::path& path);
799+
795800
//! Indirection necessary to make lock annotations work with an optional mempool.
796801
RecursiveMutex* MempoolMutex() const LOCK_RETURNED(m_mempool->cs)
797802
{

0 commit comments

Comments
 (0)