83 Commits
Author SHA1 Message Date
Andrew Tothandl0rinc cae6d895f8 fuzz: add target for CoinsViewOverlay
Co-authored-by: l0rinc <pap.lorinc@gmail.com>
2026-02-12 21:31:23 -05:00
Andrew Tothandl0rinc 86eda88c8e fuzz: move backend mutating block to end of coins_view
Refactor TestCoinsView() to move code that directly modifies
backend_coins_view to the end of the function.
This prepares for a CoinsViewOverlay fuzz target that asserts
the backend_coins_view is not mutated by any methods before
BatchWrite is called.

Co-authored-by: l0rinc <pap.lorinc@gmail.com>
2026-02-12 21:31:23 -05:00
Andrew Tothandl0rinc 89824fb27b fuzz: pass coins_view_cache to TestCoinsView in coins_view
Refactor TestCoinsView() to accept the cache as a parameter instead of
creating it internally. This prepares for adding a CoinsViewOverlay
fuzz target that needs to pass in a different cache type.

This is a non-functional change.

Co-authored-by: l0rinc <pap.lorinc@gmail.com>
2026-02-12 21:31:23 -05:00
Andrew Tothandl0rinc 73e99a5966 coins: don't mutate main cache when connecting block
Use `CoinsViewOverlay` when connecting blocks in `ConnectTip`.

Add a new integration test to verify that using
CoinsViewOverlay does not mutate the main cache
during validation for an invalid block.

Co-authored-by: l0rinc <pap.lorinc@gmail.com>
2026-02-12 21:31:23 -05:00
Andrew Tothandl0rinc 67c0d1798e coins: introduce CoinsViewOverlay
Introduce `CoinsViewOverlay`, a `CCoinsViewCache` subclass that reads
coins without mutating the underlying cache via `FetchCoin()`.

Use `PeekCoin()` to look up a Coin through a stack of `CCoinsViewCache` layers without populating parent caches. This prevents the main cache from caching inputs pulled from disk for a block that has not yet been fully validated. Once `Flush()` is called on the view, these inputs will be added as spent to `coinsCache` in the main cache via `BatchWrite()`.

This is the foundation for async input fetching, where worker threads must not
mutate shared state.

Co-authored-by: l0rinc <pap.lorinc@gmail.com>
2026-02-12 21:31:23 -05:00
69b01af0eb coins: add PeekCoin()
Introduce a helper to look up a Coin through a stack of CCoinsViewCache layers without populating parent caches.

This is useful for ephemeral views (e.g. during ConnectBlock) that want to avoid polluting CoinsTip() when validating invalid blocks.

Co-authored-by: l0rinc <pap.lorinc@gmail.com>
Co-authored-by: Pieter Wuille <pieter@wuille.net>
Co-authored-by: Ryan Ofsky <ryan@ofsky.org>
2026-02-12 21:31:23 -05:00
Andrew Toth 2a1d0db799 doc: Mention private broadcast RPCs in release notes 2026-02-11 19:46:22 -05:00
Andrew Tothandl0rinc c3378be10b test: Cover abortprivatebroadcast in p2p_private_broadcast
Co-authored-by: l0rinc <pap.lorinc@gmail.com>
2026-02-11 19:46:22 -05:00
Andrew Tothandl0rinc 557260ca14 rpc: Add abortprivatebroadcast
Co-authored-by: l0rinc <pap.lorinc@gmail.com>
2026-02-11 19:46:19 -05:00
Andrew Tothandl0rinc 15dff452eb test: Cover getprivatebroadcastinfo in p2p_private_broadcast
Co-authored-by: l0rinc <pap.lorinc@gmail.com>
2026-02-11 19:46:16 -05:00
Andrew TothandVasil Dimov 996f20c18a rpc: Add getprivatebroadcastinfo
Co-authored-by: Vasil Dimov <vd@freebsd.org>
2026-02-11 19:46:12 -05:00
5e64982541 net: Add PrivateBroadcast::GetBroadcastInfo
Co-authored-by: Daniela Brozzoni <danielabrozzoni@protonmail.com>
Co-authored-by: l0rinc <pap.lorinc@gmail.com>
2026-02-11 19:46:12 -05:00
Andrew Toth 573bb542be net: Store recipient node address in private broadcast 2026-02-10 09:44:52 -05:00
Andrew Toth 3e0fd0e4dd refactor: rename will_reuse_cache to reallocate_cache
More accurately reflects the purpose of the parameter, since
we will keep reusing the cache but don't want to reallocate it.
2026-01-29 09:47:10 -05:00
Andrew Tothandl0rinc 44b4ee194d validation: reuse same CCoinsViewCache for every ConnectBlock call
Add m_connect_block_view to ChainState's CoinsViews.
Call CreateResetGuard inside ConnectTip to ensure the view
is Reset after each block, avoiding repeated memory allocations.

Co-authored-by: l0rinc <pap.lorinc@gmail.com>
2026-01-29 09:47:10 -05:00
8fb6043231 coins: introduce CCoinsViewCache::ResetGuard
CCoinsViewCache::CreateResetGuard returns a guard that calls
Reset on the cache when the guard goes out of scope.
This RAII pattern ensures the cache is always properly reset
when it leaves current scope.

Co-authored-by: l0rinc <pap.lorinc@gmail.com>
Co-authored-by: sedited <seb.kung@gmail.com>
2026-01-29 09:47:05 -05:00
Andrew Tothandl0rinc 041758f5ed coins: use hashBlock setter internally for CCoinsViewCache methods
Co-authored-by: l0rinc <pap.lorinc@gmail.com>
2026-01-29 09:47:05 -05:00
8dd9200fc9 coins: add Reset on CCoinsViewCache
Add a Reset() method to CCoinsViewCache that clears cacheCoins,
cachedCoinsUsage, and hashBlock without flushing to the base view.

Co-authored-by: l0rinc <pap.lorinc@gmail.com>
Co-authored-by: sedited <seb.kung@gmail.com>
2026-01-29 09:46:35 -05:00
Andrew TothandLőrinc 2ee7f9b259 coins: assume GetCoin only returns unspent coins
`CCoinsViewCache::FetchCoin()` had special handling for a spent `Coin` returned by the parent view.
Production parents (`CCoinsViewCache` and `CCoinsViewDB`) do not return spent coins, so this path is unreachable.

Replace it with an `Assume(!coin.IsSpent())`, drop outdated documentation about spent+FRESH cache entries, and simplify `SanityCheck()` to assert the remaining possible state invariants.
This is safe because it does not change behavior for valid backends and will fail fast if the `GetCoin()` contract is violated.

Co-authored-by: Lőrinc <pap.lorinc@gmail.com>
2026-01-11 21:32:52 +01:00
Andrew TothandLőrinc eec551aaf1 fuzz: keep coinscache_sim backend free of spent coins
`CoinsViewBottom` roughly simulates a memory-backed `CCoinsViewDB`, which never stores spent coins.

Stop returning spent coins from `GetCoin()`, erase spent entries in `BatchWrite()`, and tighten comparisons to expect `std::nullopt` when the simulator has no coin.

Co-authored-by: Lőrinc <pap.lorinc@gmail.com>
2026-01-06 12:16:46 +01:00
Andrew TothandLőrinc 3e4155fcef test: do not return spent coins from CCoinsViewTest::GetCoin
Production `GetCoin()` implementations only return unspent coins.

Update the `CCoinsView` test backend to match that contract, so tests stop exercising cache states that cannot occur with `CCoinsViewCache` or `CCoinsViewDB`.

Co-authored-by: Lőrinc <pap.lorinc@gmail.com>
2026-01-06 12:16:46 +01:00
Andrew Toth 99d012ec80 refactor: return reference instead of pointer
The return value of BlockManager::GetFirstBlock must always be non-null. This
can be inferred by the implementation, which has an assertion that the return
value is not null. A raw pointer should only be returned if the result may be
null. In this case a reference is more appropriate.
2025-11-13 09:57:42 -05:00
Andrew Toth f743e6c5dd refactor: add missing LIFETIMEBOUND annotation for parameter
The BlockManager::GetFirstBlock lower_block parameter can have its lifetime
extended by the return parameter. In the case where lower_block is returned,
its lifetime will be bound to the return value. A LIFETIMEBOUND annotation is
appropriate here.
2025-11-13 09:57:42 -05:00
Andrew Toth 141117f5e8 refactor: remove incorrect LIFETIMEBOUND annotations
The return value of CheckBlockDataAvailability does not extend the lifetime of
the input parameters, nor does BlockManager instance retain references to the
parameters. The LIFETIMEBOUND annotations are misleading here since the lifetime
of the parameters are not extended past the method call.
2025-11-13 09:37:55 -05:00
Andrew Tothandmaflcko dda5228e02 test: set par=2 in default config for functional test framework
Depending on the host machine, a default `par` value can spawn up to 15 script verification threads for each node.
Running the functional test suite with default `par` can exhaust file descriptors or hit other resource limits when many threads are spawned.
These threads are mostly idle and the same code paths are executed with a value of `par=2`.
Limit this to 2 for functional tests that do not override the default option.

Co-authored-by: maflcko <6399679+maflcko@users.noreply.github.com>
2025-09-27 16:31:01 -04:00
Andrew Toth c1e554d3e5 refactor: consolidate 3 separate locks into one block
The main lock needs to be taken 3 separate times.
It simplifies readability to take it once, and is more efficient.
2025-05-04 15:00:50 -04:00
Andrew Toth 41479ed1d2 test: add test for periodic flush inside ActivateBestChain 2025-05-04 15:00:50 -04:00
Andrew Tothandl0rinc 84820561dc validation: periodically flush dbcache during reindex-chainstate
Move the periodic flush inside the outer loop of ActivateBestChain.
For very long activations, such as with reindex-chainstate, this
calls periodic flushes so progress can be saved to disk.

Co-Authored-By: l0rinc <pap.lorinc@gmail.com>
2025-05-04 13:23:14 -04:00
e976bd3045 validation: add randomness to periodic write interval
Co-Authored-By: Pieter Wuille <pieter@wuille.net>
Co-Authored-By: l0rinc <pap.lorinc@gmail.com>
2025-04-30 18:35:03 -04:00
Andrew Tothandl0rinc 2e2f410681 refactor: replace m_last_write with m_next_write
Co-Authored-By: l0rinc <pap.lorinc@gmail.com>
2025-04-30 18:33:43 -04:00
Andrew Toth b557fa7a17 refactor: rename fDoFullFlush to should_write 2025-04-30 18:32:51 -04:00
Andrew Toth d73bd9fbe4 validation: write chainstate to disk every hour
Remove the 24 hour periodic flush interval and
write the chainstate along with blocks and block
index every hour
2025-04-30 18:32:41 -04:00
Andrew Toth 0ad7d7abdb test: chainstate write test for periodic chainstate flush 2024-12-28 10:14:01 -05:00
Andrew Toth 589db872e1 validation: don't erase coins cache on prune flushes 2024-08-05 22:34:35 -04:00
Andrew TothandPieter Wuille 05cf4e1875 coins: move Sync logic to CoinsViewCacheCursor
Erase spent cache entries and clear flags of unspent
entries inside the BatchWrite loop, instead of an
additional loop after BatchWrite.

Co-Authored-By: Pieter Wuille <pieter@wuille.net>
2024-08-05 22:34:35 -04:00
Andrew Tothandl0rinc 7825b8b9ae coins: pass linked list of flagged entries to BatchWrite
BatchWrite now iterates through the linked
list of flagged entries instead of the entire
coinsCache map.

Co-Authored-By: l0rinc <pap.lorinc@gmail.com>
2024-08-05 19:43:56 -04:00
Andrew Tothandl0rinc a14edada8a test: add cache entry linked list tests
Co-Authored-By: l0rinc <pap.lorinc@gmail.com>
2024-08-05 19:43:56 -04:00
24ce37cb86 coins: track flagged cache entries in linked list
No visible behavior change. This commit tracks the flagged
entries internally but the list is not iterated by anything.

Co-Authored-By: Pieter Wuille <pieter@wuille.net>
Co-Authored-By: l0rinc <pap.lorinc@gmail.com>
2024-08-05 19:43:56 -04:00
Andrew Toth 58b7ed156d coins: call ClearFlags in CCoinsCacheEntry destructor
No behavior change. Prepares for flags adding CCoinsCacheEntrys
to a linked list which need to be removed on destruction.
2024-08-02 00:29:42 -04:00
Andrew Toth 8bd3959fea refactor: require self and sentinel parameters for AddFlags
No behavior change. Prepares for adding the CoinsCachePairs to
a linked list when flagged.
2024-08-02 00:29:42 -04:00
Andrew Toth 75f36d241d refactor: add CoinsCachePair alias 2024-08-01 23:36:00 -04:00
Andrew Toth f08faeade2 refactor: move flags to private uint8_t and rename to m_flags
No behavior change. This prepares to add CCoinsCacheEntrys
to a doubly linked list when a flag is added.
2024-08-01 23:36:00 -04:00
Andrew TothandMartin Leitner-Ankerl 4e4fb4cbab refactor: disallow setting flags in CCoinsCacheEntry constructors
No behavior change because any entries that are added in EmplaceCoinInternalDANGER
have DIRTY assigned to them after, and if they
are not inserted then they will not be
modified as before.
This prepares moving the cache entry
flags field to private access.

Co-Authored-By: Martin Leitner-Ankerl <martin.ankerl@gmail.com>
2024-08-01 23:36:00 -04:00
Andrew Toth 8737c0cefa refactor: encapsulate flags setting with AddFlags and ClearFlags
No behavior change. This prepares moving the cache entry
flags field to private access.
2024-08-01 23:36:00 -04:00
Andrew Toth 9715d3bf1e refactor: encapsulate flags get access for all other checks
No behavior change. This prepares moving the cache entry
flags field to private access.
2024-08-01 23:36:00 -04:00
Andrew Tothandl0rinc df34a94e57 refactor: encapsulate flags access for dirty and fresh checks
No behavior change. This prepares moving the cache entry flags field
to private access.

Co-Authored-By: l0rinc <pap.lorinc@gmail.com>
2024-08-01 23:36:00 -04:00
Andrew Toth 75d27fefc7 net: reduce LOCK(cs_main) scope in ProcessGetBlockData
This also changes behavior if ReadBlockFromDisk or
ReadRawBlockFromDisk fails. Previously, the node would crash
due to an assert. This has been replaced with logging the error,
disconnecting the peer, and returning early.
2024-05-04 15:38:39 -04:00
Andrew Toth 613a45cd4b net: reduce LOCK(cs_main) scope in GETBLOCKTXN
Also adds a static assertion that MAX_BLOCKTXN_DEPTH <= MIN_BLOCKS_TO_KEEP
2024-05-04 15:33:36 -04:00
Andrew Toth 4a6d1d1e3b validation: don't clear cache on periodic flush 2024-03-27 14:06:22 -04:00
Andrew Toth e710cefd57 rest: read raw block in rest_block and deserialize for json
Note that for speed this commit also removes the proof of work and
signet signature checks before returning the block in getblock.
It is assumed if a block is stored it will be valid.
2024-03-12 12:48:04 -04:00
Andrew Toth 95ce0783a6 rpc: read raw block in getblock and deserialize for verbosity > 0
Note that for speed this commit also removes the proof of work and
signet signature checks before returning the block in getblock.
It is assumed if a block is stored it will be valid.
2024-03-12 12:47:17 -04:00
Andrew Toth 0865ab8712 test: check more details on zmq raw block response 2024-03-12 12:47:01 -04:00
Andrew Toth 38265cc14e zmq: read raw block with ReadRawBlockFromDisk 2024-03-12 12:46:46 -04:00
Andrew Toth da338aada7 blockstorage: check nPos in ReadRawBlockFromDisk before seeking back
ReadRawBlockFromDisk assumes a non-null pos that has an nPos >= 8.
This simple check makes the function safer to call in the future,
so callers don't need to worry about causing UB if the pos is null.
2024-03-12 12:46:07 -04:00
Andrew Toth eb5d78c649 doc: document preference for list-initialization 2024-02-23 13:19:19 -05:00
Andrew Toth 1c4b9cbe90 bench: add readblock benchmark 2023-11-26 13:24:05 -05:00
Andrew Toth 0e21b56a44 assumeutxo: catch and log fs::remove error instead of two exist checks 2023-01-05 17:35:14 -05:00
Andrew Toth 3141eab9c6 test: add functional test for ScanAndUnlinkAlreadyPrunedFiles 2023-01-05 17:04:28 -05:00
Andrew Toth e252909e56 test: add unit test for ScanAndUnlinkAlreadyPrunedFiles 2022-12-20 12:25:54 -05:00
Andrew Toth 77557dda4a prune: scan and unlink already pruned block files on startup 2022-12-20 12:25:36 -05:00
Andrew Toth d7f61e7d59 rpc: reduce LOCK(cs_main) scope in gettxoutproof 2022-12-06 15:07:04 -05:00
Andrew Toth 4d92b5aaba rpc: reduce LOCK(cs_main) scope in GetUndoChecked and getblockstats 2022-12-06 15:07:04 -05:00
Andrew Toth efd82aec8a rpc: reduce LOCK(cs_main) scope in blockToJSON 2022-12-06 15:07:04 -05:00
Andrew Toth f00808e932 rpc: reduce LOCK(cs_main) scope in GetBlockChecked and getblock 2022-12-06 15:07:04 -05:00
Andrew Toth 7d253c943f zmq: remove LOCK(cs_main) from NotifyBlock 2022-12-06 15:07:04 -05:00
Andrew Toth c75e3d2772 rest: reduce LOCK(cs_main) scope in rest_block 2022-12-06 15:07:04 -05:00
Andrew Toth f5ff3d773c rpc: add missing lock around chainman.ActiveTip() 2022-10-26 11:46:39 -04:00
Andrew Toth 1ff5d61dfd doc: add mempool/contents rest verbose and mempool_sequence args 2022-10-05 09:25:07 -04:00
Andrew Toth 52a31dccc9 tests: mempool/contents verbose and mempool_sequence query params tests 2022-10-05 09:25:07 -04:00
Andrew Toth a518fff0f2 rest: add verbose and mempool_sequence query params for mempool/contents 2022-10-05 09:25:07 -04:00
Andrew Toth 1dcba996d3 Coinbase receivedby rpcs release notes 2021-12-07 10:49:07 -05:00
Andrew Toth b5696750a9 Test including coinbase transactions in receivedby wallet rpcs 2021-12-07 10:48:37 -05:00
Andrew Toth bce20c34d6 Include coinbase transactions in receivedby wallet rpcs 2021-12-06 12:30:58 -05:00
Andrew Toth a1d5b12ec0 Merge getreceivedby tally into GetReceived function 2020-03-27 14:26:00 -04:00
Andrew Toth 7524b6479c Add tests for generateblock 2020-03-27 12:09:31 -04:00
Andrew Toth dcc8332543 Add generateblock rpc 2020-03-27 11:00:10 -04:00
Andrew Toth c7ec9a1888 Add missing supported rpcs to doc/descriptors.md 2020-01-30 18:34:25 -05:00
Andrew Toth 85016e52f6 [rpc] Fix broken bitcoin-cli examples 2019-10-14 13:09:52 -04:00
andrewtoth 1b602f6fed remove extraneous scope 2019-04-27 10:16:33 -04:00
andrewtoth 6ad372a973 wallet: log on rescan completion 2019-04-27 10:15:56 -04:00
andrewtoth 1a0139cbaf trivial: correct parameter name in comments 2019-02-10 17:17:32 -05:00
andrewtoth f3f6dde56e Test coinbase category in wallet rpcs 2018-12-01 13:00:38 -05:00
andrewtoth e982f0b682 Add all category options to wallet rpc help 2018-12-01 13:00:00 -05:00