Skip to content

Improve elements memory usage #1190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/block_proof.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
bool CheckChallenge(const CBlockHeader& block, const CBlockIndex& indexLast, const Consensus::Params& params)
{
if (g_signed_blocks) {
return block.proof.challenge == indexLast.proof.challenge;
return block.proof.challenge == indexLast.get_proof().challenge;
} else {
return block.nBits == GetNextWorkRequired(&indexLast, &block, params);
}
Expand Down
76 changes: 61 additions & 15 deletions src/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,51 @@ class CBlockIndex
uint32_t nTime{0};
uint32_t nBits{0};
uint32_t nNonce{0};
CProof proof{};

protected:
std::optional<CProof> proof{};
// Dynamic federation fields
DynaFedParams dynafed_params{};
CScriptWitness m_signblock_witness{};
std::optional<DynaFedParams> m_dynafed_params{};
std::optional<CScriptWitness> m_signblock_witness{};

bool m_trimmed{false};

friend class CBlockTreeDB;

public:

// Irrevocably remove blocksigning and dynafed-related stuff from this
// in-memory copy of the block header.
void trim() {
assert_untrimmed();
m_trimmed = true;
proof = std::nullopt;
m_dynafed_params = std::nullopt;
m_signblock_witness = std::nullopt;
}

bool trimmed() const {
return m_trimmed;
}

void assert_untrimmed() const {
assert(!m_trimmed);
}

const CProof& get_proof() const {
assert_untrimmed();
return proof.value();
}

const DynaFedParams& dynafed_params() const {
assert_untrimmed();
return m_dynafed_params.value();
}

const CScriptWitness& signblock_witness() const {
assert_untrimmed();
return m_signblock_witness.value();
}

//! (memory only) Sequential id assigned to distinguish order in which blocks are received.
int32_t nSequenceId{0};
Expand All @@ -214,7 +255,7 @@ class CBlockIndex
nBits{block.nBits},
nNonce{block.nNonce},
proof{block.proof},
dynafed_params{block.m_dynafed_params},
m_dynafed_params{block.m_dynafed_params},
m_signblock_witness{block.m_signblock_witness}
{
}
Expand All @@ -239,6 +280,7 @@ class CBlockIndex

CBlockHeader GetBlockHeader() const
{
assert_untrimmed();
CBlockHeader block;
block.nVersion = nVersion;
if (pprev)
Expand All @@ -250,9 +292,9 @@ class CBlockIndex
}
block.nBits = nBits;
block.nNonce = nNonce;
block.proof = proof;
block.m_dynafed_params = dynafed_params;
block.m_signblock_witness = m_signblock_witness;
block.proof = proof.value();
block.m_dynafed_params = m_dynafed_params.value();
block.m_signblock_witness = m_signblock_witness.value();
return block;
}

Expand Down Expand Up @@ -366,12 +408,12 @@ class CDiskBlockIndex : public CBlockIndex
nVersion = ~CBlockHeader::DYNAFED_HF_MASK & nVersion;
return is_dyna;
} else {
return !dynafed_params.IsNull();
return !dynafed_params().IsNull();
}
}
bool RemoveDynaFedMaskOnSerialize(bool for_read) const {
assert(!for_read);
return !dynafed_params.IsNull();
return !dynafed_params().IsNull();
}

SERIALIZE_METHODS(CDiskBlockIndex, obj)
Expand All @@ -394,7 +436,7 @@ class CDiskBlockIndex : public CBlockIndex
READWRITE(obj.nVersion);
} else {
int32_t nVersion = obj.nVersion;
if (!obj.dynafed_params.IsNull()) {
if (!obj.dynafed_params().IsNull()) {
nVersion |= CBlockHeader::DYNAFED_HF_MASK;
}
READWRITE(nVersion);
Expand All @@ -406,11 +448,14 @@ class CDiskBlockIndex : public CBlockIndex
READWRITE(obj.nTime);
// For compatibility with elements 0.14 based chains
if (g_signed_blocks) {
SER_READ(obj, obj.m_dynafed_params = DynaFedParams());
SER_READ(obj, obj.m_signblock_witness = CScriptWitness());
SER_READ(obj, obj.proof = CProof());
if (is_dyna) {
READWRITE(obj.dynafed_params);
READWRITE(obj.m_signblock_witness.stack);
READWRITE(obj.m_dynafed_params.value());
READWRITE(obj.m_signblock_witness.value().stack);
} else {
READWRITE(obj.proof);
READWRITE(obj.proof.value());
}
} else {
READWRITE(obj.nBits);
Expand All @@ -420,6 +465,7 @@ class CDiskBlockIndex : public CBlockIndex

uint256 GetBlockHash() const
{
assert_untrimmed();
CBlockHeader block;
block.nVersion = nVersion;
block.hashPrevBlock = hashPrev;
Expand All @@ -430,8 +476,8 @@ class CDiskBlockIndex : public CBlockIndex
}
block.nBits = nBits;
block.nNonce = nNonce;
block.proof = proof;
block.m_dynafed_params = dynafed_params;
block.proof = proof.value();
block.m_dynafed_params = m_dynafed_params.value();
return block.GetHash();
}

Expand Down
10 changes: 5 additions & 5 deletions src/dynafed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ bool NextBlockIsParameterTransition(const CBlockIndex* pindexPrev, const Consens
for (int32_t height = next_height - 1; height >= (int32_t)(next_height - consensus.dynamic_epoch_length); --height) {
const CBlockIndex* p_epoch_walk = pindexPrev->GetAncestor(height);
assert(p_epoch_walk);
const DynaFedParamEntry& proposal = p_epoch_walk->dynafed_params.m_proposed;
const DynaFedParamEntry& proposal = p_epoch_walk->dynafed_params().m_proposed;
const uint256 proposal_root = proposal.CalculateRoot();
vote_tally[proposal_root]++;
// Short-circuit once 4/5 threshold is reached
Expand Down Expand Up @@ -56,13 +56,13 @@ DynaFedParamEntry ComputeNextBlockFullCurrentParameters(const CBlockIndex* pinde
// may be pre-dynafed params
const CBlockIndex* p_epoch_start = pindexPrev->GetAncestor(epoch_start_height);
assert(p_epoch_start);
if (p_epoch_start->dynafed_params.IsNull()) {
if (p_epoch_start->dynafed_params().IsNull()) {
// We need to construct the "full" current parameters of pre-dynafed
// consensus

// Convert signblockscript to P2WSH
uint256 signblock_witness_program;
CSHA256().Write(p_epoch_start->proof.challenge.data(), p_epoch_start->proof.challenge.size()).Finalize(signblock_witness_program.begin());
CSHA256().Write(p_epoch_start->get_proof().challenge.data(), p_epoch_start->get_proof().challenge.size()).Finalize(signblock_witness_program.begin());
CScript p2wsh_signblock_script = CScript() << OP_0 << ToByteVector(signblock_witness_program);

// Make P2SH-P2WSH-ness of non-dynafed fedpegscript explicit
Expand All @@ -75,7 +75,7 @@ DynaFedParamEntry ComputeNextBlockFullCurrentParameters(const CBlockIndex* pinde
// Put them in winning proposal
winning_proposal = DynaFedParamEntry(p2wsh_signblock_script, consensus.max_block_signature_size, sh_wsh_fedpeg_program, consensus.fedpegScript, consensus.first_extension_space);
} else {
winning_proposal = p_epoch_start->dynafed_params.m_current;
winning_proposal = p_epoch_start->dynafed_params().m_current;
}
return winning_proposal;
}
Expand All @@ -93,7 +93,7 @@ DynaFedParamEntry ComputeNextBlockCurrentParameters(const CBlockIndex* pindexPre

// Return appropriate format based on epoch age or if we *just* activated
// dynafed via BIP9
if (epoch_age == 0 || pindexPrev->dynafed_params.IsNull()) {
if (epoch_age == 0 || pindexPrev->dynafed_params().IsNull()) {
return entry;
} else {
return DynaFedParamEntry(entry.m_signblockscript, entry.m_signblock_witness_limit, entry.CalculateExtraRoot());
Expand Down
25 changes: 24 additions & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ void SetupServerArgs(ArgsManager& argsman)
hidden_args.emplace_back("-sysperms");
#endif
argsman.AddArg("-txindex", strprintf("Maintain a full transaction index, used by the getrawtransaction rpc call (default: %u)", DEFAULT_TXINDEX), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
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);
argsman.AddArg("-blockfilterindex=<type>",
strprintf("Maintain an index of compact filters by block (default: %s, values: %s).", DEFAULT_BLOCKFILTERINDEX, ListBlockFilterTypes()) +
" If <type> is not supplied or if <type> = 1, indexes for all known types are enabled.",
Expand Down Expand Up @@ -979,6 +980,23 @@ bool AppInitParameterInteraction(const ArgsManager& args)
fPruneMode = true;
}

if (args.IsArgSet("-trim_headers")) {
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");
fTrimHeaders = true;
// This calculation is driven by GetValidFedpegScripts in pegins.cpp, which walks the chain
// back to current epoch start, and then an additional total_valid_epochs on top of that.
// We add one epoch here for the current partial epoch, and then another one for good luck.
// NB: If we're non-dynafed, then:
// - total_valid_epochs = 1
// - dynamic_epoch_length = std::numeric_limits<uint32_t>::max()
// So this will work out to an unhelpfully-large number. XXX: Is this a problem?
nMustKeepFullHeaders = (chainparams.GetConsensus().total_valid_epochs + 2) * (chainparams.GetConsensus().dynamic_epoch_length);
// This is the number of headers we can have in flight downloading at a time, beyond the
// set of blocks we've already validated. Capping this is necessary to keep memory usage
// bounded during IBD.
}
nHeaderDownloadBuffer = chainparams.GetConsensus().dynamic_epoch_length * 2;

nConnectTimeout = args.GetArg("-timeout", DEFAULT_CONNECT_TIMEOUT);
if (nConnectTimeout <= 0) {
nConnectTimeout = DEFAULT_CONNECT_TIMEOUT;
Expand Down Expand Up @@ -1690,7 +1708,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)

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

if (fTrimHeaders) {
LogPrintf("Unsetting NODE_NETWORK_LIMITED on header trim mode\n");
nLocalServices = ServiceFlags(nLocalServices & ~NODE_NETWORK_LIMITED);
}

if (DeploymentEnabled(chainparams.GetConsensus(), Consensus::DEPLOYMENT_SEGWIT)) {
// Advertise witness capabilities.
// The option to not set NODE_WITNESS is only used in the tests and should be removed.
Expand Down
2 changes: 1 addition & 1 deletion src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

void ResetChallenge(CBlockHeader& block, const CBlockIndex& indexLast, const Consensus::Params& params)
{
block.proof.challenge = indexLast.proof.challenge;
block.proof.challenge = indexLast.get_proof().challenge;
}

void ResetProof(CBlockHeader& block)
Expand Down
3 changes: 3 additions & 0 deletions src/node/blockstorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ std::atomic_bool fReindex(false);
bool fHavePruned = false;
bool fPruneMode = false;
uint64_t nPruneTarget = 0;
bool fTrimHeaders = false;
uint64_t nMustKeepFullHeaders = std::numeric_limits<uint64_t>::max();
uint64_t nHeaderDownloadBuffer = std::numeric_limits<uint64_t>::max();

// TODO make namespace {
RecursiveMutex cs_LastBlockFile;
Expand Down
7 changes: 7 additions & 0 deletions src/node/blockstorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ extern bool fHavePruned;
extern bool fPruneMode;
/** Number of MiB of block files that we're trying to stay below. */
extern uint64_t nPruneTarget;
/** True if we're running in -trim_headers mode. */
extern bool fTrimHeaders;
/** Minimum number of full untrimmed headers to keep, for blocks we have. */
extern uint64_t nMustKeepFullHeaders;
/** Target number of headers to download beyond the blocks we have. */
// XXX: this currently only operates when in header trim mode, but it's really independent of that.
extern uint64_t nHeaderDownloadBuffer;

//! Check whether the block associated with this index entry is pruned or not.
bool IsBlockPruned(const CBlockIndex* pblockindex);
Expand Down
4 changes: 2 additions & 2 deletions src/pegins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,8 @@ std::vector<std::pair<CScript, CScript>> GetValidFedpegScripts(const CBlockIndex
break;
}

if (!p_epoch_start->dynafed_params.IsNull()) {
fedpegscripts.push_back(std::make_pair(p_epoch_start->dynafed_params.m_current.m_fedpeg_program, p_epoch_start->dynafed_params.m_current.m_fedpegscript));
if (!p_epoch_start->dynafed_params().IsNull()) {
fedpegscripts.push_back(std::make_pair(p_epoch_start->dynafed_params().m_current.m_fedpeg_program, p_epoch_start->dynafed_params().m_current.m_fedpegscript));
} else {
fedpegscripts.push_back(std::make_pair(GetScriptForDestination(ScriptHash(GetScriptForDestination(WitnessV0ScriptHash(params.fedpegScript)))), params.fedpegScript));
}
Expand Down
12 changes: 6 additions & 6 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,13 @@ UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex
result.pushKV("difficulty", GetDifficulty(blockindex));
result.pushKV("chainwork", blockindex->nChainWork.GetHex());
} else {
if (blockindex->dynafed_params.IsNull()) {
result.pushKV("signblock_witness_asm", ScriptToAsmStr(blockindex->proof.solution));
result.pushKV("signblock_witness_hex", HexStr(blockindex->proof.solution));
result.pushKV("signblock_challenge", HexStr(blockindex->proof.challenge));
if (blockindex->dynafed_params().IsNull()) {
result.pushKV("signblock_witness_asm", ScriptToAsmStr(blockindex->get_proof().solution));
result.pushKV("signblock_witness_hex", HexStr(blockindex->get_proof().solution));
result.pushKV("signblock_challenge", HexStr(blockindex->get_proof().challenge));
} else {
result.pushKV("signblock_witness_hex", EncodeHexScriptWitness(blockindex->m_signblock_witness));
result.pushKV("dynamic_parameters", dynaParamsToJSON(blockindex->dynafed_params));
result.pushKV("signblock_witness_hex", EncodeHexScriptWitness(blockindex->signblock_witness()));
result.pushKV("dynamic_parameters", dynaParamsToJSON(blockindex->dynafed_params()));
}
}
result.pushKV("nTx", (uint64_t)blockindex->nTx);
Expand Down
Loading