Skip to content

Commit 7c55557

Browse files
committed
Add a flag for header-trimming; clear P2P network feature bits when enabled.
1 parent 68eb086 commit 7c55557

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

src/init.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ void SetupServerArgs(ArgsManager& argsman)
425425
hidden_args.emplace_back("-sysperms");
426426
#endif
427427
argsman.AddArg("-txindex", strprintf("Maintain a full transaction index, used by the getrawtransaction rpc call (default: %u)", DEFAULT_TXINDEX), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
428+
argsman.AddArg("-trim_headers", strprintf("Trim old headers in memory, removing blocksigning and dynafed-related fields. Saves memory, but blocks us from serving blocks or headers to peers, and removes trimmed fields from some JSON RPC outputs. (default: false)"), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
428429
argsman.AddArg("-blockfilterindex=<type>",
429430
strprintf("Maintain an index of compact filters by block (default: %s, values: %s).", DEFAULT_BLOCKFILTERINDEX, ListBlockFilterTypes()) +
430431
" If <type> is not supplied or if <type> = 1, indexes for all known types are enabled.",
@@ -979,6 +980,23 @@ bool AppInitParameterInteraction(const ArgsManager& args)
979980
fPruneMode = true;
980981
}
981982

983+
if (args.IsArgSet("-trim_headers")) {
984+
LogPrintf("Configured for header-trimming mode. This will reduce memory usage substantially, but we will be unable to serve as a full P2P peer, and certain header fields may be missing from JSON RPC output.\n");
985+
fTrimHeaders = true;
986+
// This calculation is driven by GetValidFedpegScripts in pegins.cpp, which walks the chain
987+
// back to current epoch start, and then an additional total_valid_epochs on top of that.
988+
// We add one epoch here for the current partial epoch, and then another one for good luck.
989+
// NB: If we're non-dynafed, then:
990+
// - total_valid_epochs = 1
991+
// - dynamic_epoch_length = std::numeric_limits<uint32_t>::max()
992+
// So this will work out to an unhelpfully-large number. XXX: Is this a problem?
993+
nMustKeepFullHeaders = (chainparams.GetConsensus().total_valid_epochs + 2) * (chainparams.GetConsensus().dynamic_epoch_length);
994+
// This is the number of headers we can have in flight downloading at a time, beyond the
995+
// set of blocks we've already validated. Capping this is necessary to keep memory usage
996+
// bounded during IBD.
997+
}
998+
nHeaderDownloadBuffer = chainparams.GetConsensus().dynamic_epoch_length * 2;
999+
9821000
nConnectTimeout = args.GetArg("-timeout", DEFAULT_CONNECT_TIMEOUT);
9831001
if (nConnectTimeout <= 0) {
9841002
nConnectTimeout = DEFAULT_CONNECT_TIMEOUT;
@@ -1690,7 +1708,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
16901708

16911709
// if pruning, unset the service bit and perform the initial blockstore prune
16921710
// after any wallet rescanning has taken place.
1693-
if (fPruneMode) {
1711+
if (fPruneMode || fTrimHeaders) {
16941712
LogPrintf("Unsetting NODE_NETWORK on prune mode\n");
16951713
nLocalServices = ServiceFlags(nLocalServices & ~NODE_NETWORK);
16961714
if (!fReindex) {
@@ -1702,6 +1720,11 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
17021720
}
17031721
}
17041722

1723+
if (fTrimHeaders) {
1724+
LogPrintf("Unsetting NODE_NETWORK_LIMITED on header trim mode\n");
1725+
nLocalServices = ServiceFlags(nLocalServices & ~NODE_NETWORK_LIMITED);
1726+
}
1727+
17051728
if (DeploymentEnabled(chainparams.GetConsensus(), Consensus::DEPLOYMENT_SEGWIT)) {
17061729
// Advertise witness capabilities.
17071730
// The option to not set NODE_WITNESS is only used in the tests and should be removed.

src/node/blockstorage.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ std::atomic_bool fReindex(false);
2525
bool fHavePruned = false;
2626
bool fPruneMode = false;
2727
uint64_t nPruneTarget = 0;
28+
bool fTrimHeaders = false;
29+
uint64_t nMustKeepFullHeaders = std::numeric_limits<uint64_t>::max();
30+
uint64_t nHeaderDownloadBuffer = std::numeric_limits<uint64_t>::max();
2831

2932
// TODO make namespace {
3033
RecursiveMutex cs_LastBlockFile;

src/node/blockstorage.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ extern bool fHavePruned;
4444
extern bool fPruneMode;
4545
/** Number of MiB of block files that we're trying to stay below. */
4646
extern uint64_t nPruneTarget;
47+
/** True if we're running in -trim_headers mode. */
48+
extern bool fTrimHeaders;
49+
/** Minimum number of full untrimmed headers to keep, for blocks we have. */
50+
extern uint64_t nMustKeepFullHeaders;
51+
/** Target number of headers to download beyond the blocks we have. */
52+
// XXX: this currently only operates when in header trim mode, but it's really independent of that.
53+
extern uint64_t nHeaderDownloadBuffer;
4754

4855
//! Check whether the block associated with this index entry is pruned or not.
4956
bool IsBlockPruned(const CBlockIndex* pblockindex);

0 commit comments

Comments
 (0)