aboutsummaryrefslogtreecommitdiff
path: root/src/blockencodings.h
diff options
context:
space:
mode:
authorAngusP <angus@toaster.cc>2024-06-12 22:09:15 +0100
committerAngusP <angus@toaster.cc>2024-06-19 22:56:30 +0100
commit55eea003af24169c883e1761beb997e151845225 (patch)
treed6cc5564dbea79a85029c8d94658e15502cf993b /src/blockencodings.h
parent4c99301220ab44e98d0d0e1cc8d774d96a25b7aa (diff)
test: Make blockencodings_tests deterministic
refactor: CBlockHeaderAndShortTxIDs constructor now always takes an explicit nonce. test: Make blockencodings_tests deterministic using fixed seed providing deterministic CBlockHeaderAndShortTxID nonces and dummy transaction IDs. Fixes very rare flaky test failures, where the ShortIDs of test transactions collide, leading to `READ_STATUS_FAILED` from PartiallyDownloadedBlock::InitData and/or `IsTxAvailable` giving `false` when the transaction should actually be available. * Use a new `FastRandomContext` with a fixed seed in each test, to ensure 'random' uint256s used as fake prevouts are deterministic, so in-turn test txids and short IDs are deterministic and don't collide causing very rare but flaky test failures. * Add new test-only/internal initializer for `CBlockHeaderAndShortTxIDs` that takes a specified nonce to further ensure determinism and avoid rare but undesireable short ID collisions. In a test context this nonce is set to a fixed known-good value. Normally it is random, as previously. Flaky test failures can be reproduced with: ```patch diff --git a/src/blockencodings.cpp b/src/blockencodings.cpp index 695e8d806a..64d635a97a 100644 --- a/src/blockencodings.cpp +++ b/src/blockencodings.cpp @@ -44,7 +44,8 @@ void CBlockHeaderAndShortTxIDs::FillShortTxIDSelector() const { uint64_t CBlockHeaderAndShortTxIDs::GetShortID(const Wtxid& wtxid) const { static_assert(SHORTTXIDS_LENGTH == 6, "shorttxids calculation assumes 6-byte shorttxids"); - return SipHashUint256(shorttxidk0, shorttxidk1, wtxid) & 0xffffffffffffL; + // return SipHashUint256(shorttxidk0, shorttxidk1, wtxid) & 0xffffffffffffL; + return SipHashUint256(shorttxidk0, shorttxidk1, wtxid) & 0x0f; } ``` to increase the likelihood of a short ID collision; and running ```shell set -e; n=0; while (( n++ < 5000 )); do src/test/test_bitcoin --run_test=blockencodings_tests; done ```
Diffstat (limited to 'src/blockencodings.h')
-rw-r--r--src/blockencodings.h9
1 files changed, 7 insertions, 2 deletions
diff --git a/src/blockencodings.h b/src/blockencodings.h
index 01594db527..bc1d08ba5a 100644
--- a/src/blockencodings.h
+++ b/src/blockencodings.h
@@ -106,10 +106,15 @@ public:
CBlockHeader header;
- // Dummy for deserialization
+ /**
+ * Dummy for deserialization
+ */
CBlockHeaderAndShortTxIDs() {}
- CBlockHeaderAndShortTxIDs(const CBlock& block);
+ /**
+ * @param[in] nonce This should be randomly generated, and is used for the siphash secret key
+ */
+ CBlockHeaderAndShortTxIDs(const CBlock& block, const uint64_t nonce);
uint64_t GetShortID(const Wtxid& wtxid) const;