headerssync: Make HeadersSyncState more flexible and move constants

Move calculated constants from the top of src/headerssync.cpp into src/kernel/chainparams.cpp.

Instead of being hardcoded to mainnet parameters, HeadersSyncState can now vary depending on chain or test. (This means we can reset TARGET_BLOCKS back to the nice round number of 15'000).

Signet and testnets got new HeadersSyncParams constants through temporarily altering headerssync-params.py with corresponding GENESIS_TIME and MINCHAINWORK_HEADERS (based off defaultAssumeValid block height comments, corresponding to nMinimumChainWork). Regtest doesn't have a default assume valid block height, so the values are copied from Testnet 4. Since the constants only affect memory usage, and have very low impact unless dealing with a largely malicious chain, it's not that critical to keep updating them for non-mainnet chains.

GENESIS_TIMEs (UTC):
Testnet3: 1296688602 = datetime(2011, 2, 2)
Testnet4: 1714777860 = datetime(2024, 5, 3)
Signet: 1598918400 = datetime(2020, 9, 1)
This commit is contained in:
Hodlinator
2025-09-04 09:17:00 +02:00
parent 8fd1c2893e
commit cc5dda1de3
10 changed files with 99 additions and 39 deletions

View File

@@ -195,6 +195,12 @@ public:
.tx_count = 1235299397,
.dTxRate = 5.456290459519495,
};
// Generated by headerssync-params.py on 2025-09-01.
m_headers_sync_params = HeadersSyncParams{
.commitment_period = 632,
.redownload_buffer_size = 15009, // 15009/632 = ~23.7 commitments
};
}
};
@@ -292,6 +298,12 @@ public:
.tx_count = 508468699,
.dTxRate = 7.172978845985714,
};
// Generated by headerssync-params.py on 2025-09-03.
m_headers_sync_params = HeadersSyncParams{
.commitment_period = 628,
.redownload_buffer_size = 13460, // 13460/628 = ~21.4 commitments
};
}
};
@@ -393,6 +405,12 @@ public:
.tx_count = 11414302,
.dTxRate = 0.2842619757327476,
};
// Generated by headerssync-params.py on 2025-09-03.
m_headers_sync_params = HeadersSyncParams{
.commitment_period = 275,
.redownload_buffer_size = 7017, // 7017/275 = ~25.5 commitments
};
}
};
@@ -506,6 +524,12 @@ public:
fDefaultConsistencyChecks = false;
m_is_mockable_chain = false;
// Generated by headerssync-params.py on 2025-09-03.
m_headers_sync_params = HeadersSyncParams{
.commitment_period = 390,
.redownload_buffer_size = 9584, // 9584/390 = ~24.6 commitments
};
}
};
@@ -636,6 +660,12 @@ public:
base58Prefixes[EXT_SECRET_KEY] = {0x04, 0x35, 0x83, 0x94};
bech32_hrp = "bcrt";
// Copied from Testnet4.
m_headers_sync_params = HeadersSyncParams{
.commitment_period = 275,
.redownload_buffer_size = 7017, // 7017/275 = ~25.5 commitments
};
}
};

View File

@@ -61,6 +61,15 @@ struct ChainTxData {
double dTxRate; //!< estimated number of transactions per second after that timestamp
};
//! Configuration for headers sync memory usage.
struct HeadersSyncParams {
//! Distance in blocks between header commitments.
size_t commitment_period{0};
//! Minimum number of validated headers to accumulate in the redownload
//! buffer before feeding them into the permanent block index.
size_t redownload_buffer_size{0};
};
/**
* CChainParams defines various tweakable parameters of a given instance of the
* Bitcoin system.
@@ -106,6 +115,7 @@ public:
const std::vector<unsigned char>& Base58Prefix(Base58Type type) const { return base58Prefixes[type]; }
const std::string& Bech32HRP() const { return bech32_hrp; }
const std::vector<uint8_t>& FixedSeeds() const { return vFixedSeeds; }
const HeadersSyncParams& HeadersSync() const { return m_headers_sync_params; }
std::optional<AssumeutxoData> AssumeutxoForHeight(int height) const
{
@@ -170,6 +180,7 @@ protected:
bool m_is_mockable_chain;
std::vector<AssumeutxoData> m_assumeutxo_data;
ChainTxData chainTxData;
HeadersSyncParams m_headers_sync_params;
};
std::optional<ChainType> GetNetworkForMagic(const MessageStartChars& pchMessageStart);