Commit Graph

257 Commits

Author SHA1 Message Date
Calvin Kim
a254998bc5 blockchain: change reorg utxo cache behavior
The assumption in the previous code was incorrect in that we were
assuming that the chainLock is held throughout the entire chain reorg.
This is not the case since the chainLock is let go of during the
callback to the subscribers.

Because of this, we need to ensure that the utxo set is consistent on
each block disconnect. To achieve this, additional flushes are added
during block disconnects.

Also the utxocache is no longer avoided during block connects and when
we're checking for the validity of the block connects and disconnects as
we can just use the cache instead of trying to avoid it.
2024-03-06 02:40:24 +09:00
Calvin Kim
5a91ea23ca blockchain_test, fullblocktests: add test to check for utxo
existance/non-existance

New test instance BlockDisconnectExpectUTXO tests that a utxo
exists/doesn't exist after a specific block has been disconnected.
2024-02-21 19:09:03 +09:00
Olaoluwa Osuntokun
13152b35e1
Merge pull request #2089 from kcalvinalvin/2024-01-03-add-last-flush-time-on-initconsistentstate
blockchain: set the lastflushtime when setting the lastflushhash
2024-01-23 20:25:55 -08:00
yyforyongyu
ef54c49df4
multi: map btcd mempool acceptance errors to bitcoind's testmempoolaccept
This commit creates a `RejectReasonMap` to map the errors returned from
`btcd` to bitcoind's `testmempoolaccept` so the `RejectReason` is
unified at the RPC level. To make sure the map keys are unique, the
error strings are modified in `btcd`.
2024-01-15 17:22:41 +08:00
Thabokani
a4df044cfd
Fix some typos (#2085)
* Dockerfile: fix typo

* blockchain: fix typos

* sample-btcd.conf: fix typos

* server: fix typos

* txscript: fix typos
2024-01-03 16:36:49 -05:00
Calvin Kim
e307ad122f blockchain: set the lastflushtime when setting the lastflushhash
On startup when the headers-first mode is off, when receiving the first
block, the periodic flush will trigger.  The lastflushtime wasn't set
which resulted in the flush being triggered on the first block on
restart.
2024-01-03 14:24:36 +09:00
Oliver Gugger
7644d14078
blockchain: fix compilation issues with 32bit systems 2023-12-22 10:14:43 +01:00
Olaoluwa Osuntokun
c3c3545f9b
multi: update main package to chainhash/v1.1.0, use optimized dsha256
In this commit, we update the top-level btcd package to use the latest
version of btcutil and also the chainhash package. With this version
bump, we can now use the new optimized dsha256 routine where applicable.

With this commit, I've covered most of the areas we'll hash an entire
transaction/block/header, but we may want to optimize some other areas
further, in particular, the witness sighash calc.
2023-12-19 15:01:55 -08:00
Calvin Kim
87a81f14fc blockchain: address nit comments 2023-12-16 16:53:17 +09:00
Calvin Kim
26b2e9d9de blockchain: add test for InitConsistentState 2023-12-16 16:53:17 +09:00
Calvin Kim
ebc93a34ce blockchain: flush the utxo cache on prune if needed
If the prune will delete block past the last flush hash of the
utxocache, the cache will need to be flushed first to avoid a case
where the utxocache is irrecoverable.  The newly added code adds this
flush logic to connectBlock.
2023-12-16 16:53:17 +09:00
Calvin Kim
dd37dfa80b blockchain: add flushNeededAfterPrune
flushNeededAfterPrune returns true if the utxocache needs to be flushed
after the pruning of the given slice of block hashes.  For the utxo
cache to be recoverable while pruning is enabled, we need to make sure
that there exists blocks since the last utxo cache flush.  If there are
blocks that are deleted after the last utxo cache flush, the utxo set is
irrecoverable.  The added method provides a way to tell if a flush is
needed.
2023-12-16 16:53:17 +09:00
Calvin Kim
16cd44f0e6 blockchain, netsync, main, cmd/addblock: Use utxocache
This change is part of the effort to add utxocache support to btcd.

utxo cache is now used by the BlockChain struct.  By default it's used
and the minimum cache is set to 250MiB.  The change made helps speed up
block/tx validation as the cache allows for much faster lookup of utxos.
The initial block download in particular is improved as the db i/o
bottleneck is remedied by the cache.
2023-12-16 16:53:17 +09:00
Calvin Kim
3c11e48dd2 blockchain: Add utxocache
The implemented utxocache implements connectTransactions just like
utxoviewpoint and can be used as a drop in replacement for
connectTransactions.

One thing to note is that unlike the utxoViewpoint, the utxocache
immediately deletes the spent entry from the cache.  This means that the
utxocache is unfit for functions like checkConnectBlock where you expect
the entry to still exist but be marked as spent.

disconnectTransactions is purposely not implemented as using the cache
during reorganizations may leave the utxo state inconsistent if there is
an unexpected shutdown.  The utxoViewpoint will still have to be used
for reorganizations.
2023-12-16 16:53:04 +09:00
Calvin Kim
053ef330f2 blockchain: Refactor fetchInputUtxos
This change is part of the effort to add utxocache support to btcd.

fetchInputUtxos had mainly 2 functions:
1: Figure out which outpoints to fetch
2: Call fetchUtxosMain to fetch those outpoints

Functionality for (1) is refactored out to fetchInputsToFetch.  This is
done to allow fetchInputUtxos to use the cache to fetch the outpoints
as well in a later commit.
2023-12-16 16:36:45 +09:00
Calvin Kim
bcd8f547fe blockchain: Require utxoBucket in dbFetchUtxoEntry
This change is part of the effort to add utxocache support to btcd.

Require the caller to pass in the utxoBucket as the caller may be
fetching many utxos in one loop.  Having the caller pass it in removes
the need for dbFetchUtxoEntry to grab the bucket on every single fetch.
2023-12-16 16:36:45 +09:00
Calvin Kim
953d62afa7 blockchain: Return early on nil utxo view in dbPutUtxoView
This change is part of the effort to add utxocache support to btcd.

connectBlock may have an empty utxoviewpoint as the block verification
process may be using the utxo cache directly.  In that case, a nil utxo
viewpoint will be passed in.  Just return early on a nil utxoviewpoint.
2023-12-16 16:36:45 +09:00
Calvin Kim
d86e79eb79 blockchain: Refactor dbPutUtxoView
This change is part of the effort to add utxocache support to btcd.

dbPutUtxoView handled putting and deleting new/spent utxos from the
database.  These two functinalities are refactored to their own
functions: dbDeleteUtxoEntry and dbPutUtxoEntry.

Refactoring these out allows the cache to call these two functions
directly instead of having to create a view and saving that view to
disk.
2023-12-16 16:36:45 +09:00
Calvin Kim
fc65744134 blockchain: Add utxoStateConsistency read and write functions
This change is part of the effort to add utxocache support to btcd.

The utxoStateConsistency indicates what the last block that the utxo
cache got flush at.  This is useful for recovery purposes as if the node
is unexpectdly shut down, we know which block to start rebuilding the
utxo state from.
2023-12-16 16:36:45 +09:00
Calvin Kim
27cf70216f blockchain: Add memoryUsage() method on UtxoEntry
This change is part of the effort to add utxocache support to btcd.

Getting the memory usage of an entry is very useful for the utxo cache
as we need to know how much memory all the cached entries are using to
guarantee a cache usage limit for the end user.
2023-12-16 16:36:45 +09:00
Calvin Kim
35c42688d9 blockchain: Add tfFresh to txoFlags
This change is part of the effort to add utxocache support to btcd.

The fresh flag indicates that the entry is fresh and that the parent
view (the database) hasn't yet seen the entry.  This is very useful as
a performance optimization for the utxo cache as if a fresh entry is
spent, we can simply remove it from the cache and don't bother trying to
delete it from the database.
2023-12-16 16:36:45 +09:00
Calvin Kim
1f2dfa2d6e blockchain: Add mapslice
This change is part of the effort to add utxocache support to btcd.

mapslice allows the caller to allocate a fixed amount of memory for the
utxo cache maps without the mapslice going over that fixed amount of
memory.  This is useful as we can have variable sizes (1GB, 1.1GB, 2.3GB,
etc) while guaranteeing a memory limit.
2023-12-16 16:36:43 +09:00
Calvin Kim
e318551538 blockchain: Add sizehelper
This change is part of the effort to add utxocache support to btcd.

sizehelper introduces code for 2 main things:
    1: Calculating how many entries to allocate for a map given a size
       in bytes.
    2: Calculating how much a map takes up in memory given the entries
       were allocated for the map.

These functionality are useful for allocating maps so that they'll be
allocating below a certain number of bytes.  Since go maps will always
allocate in powers of B (where B is the bucket size for the given map),
it may allocate too much memory.  For example, for a map that can store
8GB of entries, the map will grow to be 16GB once the map is full and
the caller puts an extra entry onto the map.

If we want to give a memory guarantee to the user, we can either:
    1: Limit the cache size to fixed sizes (4GB, 8GB, ...).
    2: Allocate a slice of maps.

The sizehelper code helps with (2).
2023-12-13 23:48:54 +09:00
Olaoluwa Osuntokun
ac068e7f61
Merge pull request #1688 from kcalvinalvin/add-blockindex-parentskips
blockchain: Add ancestor optimization to finding Ancestor
2023-12-06 16:27:05 -08:00
xiaolou86
4171854739 Fix typos 2023-11-20 12:04:31 -05:00
Calvin Kim
f396b3d3d9 blockchain: better Ancestor with skiplists
On startup, Ancestor call was taking a lot of time when the node was
loading the blockindex onto memory. This change speeds up the Ancestor
function significantly and speeds up the node during startup.

On testnet3 at blockheight ~2,500,000, the startup was around 30seconds
on current main and was 5 seconds with this change. Below is a benchstat
result showing the significant speedup.

goos: darwin
goarch: arm64
pkg: github.com/utreexo/utreexod/blockchain
           │     old.txt      │               new.txt                │
           │      sec/op      │    sec/op     vs base                │
Ancestor-8   120819.301µ ± 5%   7.013µ ± 19%  -99.99% (p=0.000 n=10)

           │  old.txt   │            new.txt             │
           │    B/op    │    B/op     vs base            │
Ancestor-8   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=10) ¹
¹ all samples are equal

           │  old.txt   │            new.txt             │
           │ allocs/op  │ allocs/op   vs base            │
Ancestor-8   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=10) ¹
¹ all samples are equal
2023-11-17 16:55:58 +09:00
Olaoluwa Osuntokun
f7e9fba086
Merge pull request #1918 from kcalvinalvin/2022-11-06-implement-getchaintips
blockchain, btcjson: Implement getchaintips rpc call
2023-11-14 17:16:15 -08:00
eugene
1a615550b7
blockchain: export CheckSerializedHeight, reduce allocs for cb height
parsing

Some callers only want to check the coinbase height rather than be given
the height.
2023-10-30 12:06:10 -04:00
Olaoluwa Osuntokun
ec401d00a1
Merge pull request #1971 from kcalvinalvin/add-pruning
main, wire, blockchain, indexers, ffldb: Add pruning
2023-08-23 15:59:37 -07:00
Calvin Kim
e27fcac9cd blockchain/indexers: add functions to report init status
This change is part of the effort to add pruning support to btcd.

The added *Initialized() functions to each of the indexers allow for
callers to check if each of the indexes have been created.  It's
useful for ux improvements where we force the user to manually drop
indexes that aren't compatible with pruning when the user enables
pruning.
2023-08-23 00:46:53 +09:00
Calvin Kim
57ec43fedc blockchain: Add pruning support
This change is part of the effort to add pruning support to btcd.

A field for pruning is added and the BlockChain struct is now able to be
configured with pruning.  Prune is called on every block connect and
the prune target field is passed to PruneBlocks func.  There's no check
to keep the latest 288 blocks to abide by the NODE_NETWORK_LIMITED rule.
That'll have to be enforced by the caller creating the BlockChain
struct.
2023-08-22 15:48:55 +09:00
Olaoluwa Osuntokun
0aaa7c5e7b
Merge pull request #1979 from kcalvinalvin/merkle-calc-fast
blockchain, integration, mining, main: Rolling merkle root calculation
2023-08-10 15:05:40 -07:00
Conner Fromknecht
ecfbb7e5d8 blockchain, rpctest, mining, main: replace usage of BuildMerkleTreeStore with CalcMerkleRoot 2023-08-04 00:02:26 +09:00
Conner Fromknecht
025fa65c93 blockchain: Add CalcMerkleRoot
CalcMerkleRoot uses the rolling merkle root algorithm to calculate the
merkle root commitment inside the Bitcoin block header.  It allocates
significantly less memory than the BuildMerkleTreeStore function that's
currently in use (99.9% in an average block with 2000 txs).
2023-08-04 00:01:10 +09:00
Conner Fromknecht
2d23f94002 blockchain: Add benchmarks for Merkle root calculation 2023-08-04 00:00:51 +09:00
Conner Fromknecht
2bb6824067 blockchain: Add RollingMerkleTree
RollingMerkleTree is a much more memory efficient way of calculating the
merkle root of a tx commitment inside the bitcoin block header.  The
current way of calculating the merkle root allocates 2*N elements. With
the RollingMerkleTree, we are able to reduce the memory allocated to
log2(N).

This results in significant memory savings (99.9% in an average block),
allowing for a faster block verification.
2023-08-03 23:56:53 +09:00
Calvin Kim
3ba9feeeee blockchain, btcutil/bloom: BuildMerkleTreeStore returns chainhash.Hash
BuildMerkleTreeStore used to return a pointer, but it is changed to
return a chainhash.Hash directly.  This allows the compiler to make
optimizations in some cases and avoids a memory allocation.
2023-08-03 15:23:17 +09:00
Calvin Kim
46bfb784a6 blockchain: Add ChainTips() method to BlockChain
ChainTips method allows for callers to get all the chain tips the node
is aware of. This is useful for supporting the getchaintips rpc call.
2023-07-16 16:03:45 +09:00
Calvin Kim
b84f084ce7 blockchain: Add InactiveTips() to blockindex
InactiveTips() returns all the tips of the branches of the blockchain
tree that are not in the best chain. This function is useful for
supporting the getchaintips rpc call.
2023-07-16 15:03:33 +09:00
eugene
ee6c0e1962
blockchain: export CheckBlockHeaderSanity as a library function 2023-06-29 14:45:32 -04:00
eugene
00d0edd5c4
blockchain: intro HeaderCtx, ChainCtx + refactor CheckBlockHeaderContext
This change will allow an external program to provide its own HeaderCtx
and ChainCtx and be able to perform contextual block header checks.
2023-06-29 14:45:30 -04:00
cui fliter
e160bb6922 multi: remove repetitive the
Signed-off-by: cui fliter <imcusg@gmail.com>
2023-06-26 15:40:51 +08:00
Oliver Gugger
599d3619e8
blockchain+chaincfg: disable retargeting for regtest
This commit emulates the behavior of Bitcoin Core introduced in
https://github.com/bitcoin/bitcoin/pull/6853 that disables retargeting
of the required proof of work for regtest.
2023-06-22 09:25:40 +02:00
Calvin Kim
ba5407615d multi: Run gofmt on the entire repository
The doc formatting changes introduced in the recent go version is
increasing the diff for all of the new commits.  Formatting it all in
this commit will help the readability of future PRs by reducing the
diff.
2023-06-21 22:31:09 +09:00
Calvin Kim
5ede256f66 blockchain: Add benchmark for using a map vs a slice
Benchmark added to compare the performance of a map vs a slice when
fetching utxos.  The benchmark shows roughly 25% performance improvement
when using slices instead of a map.
2023-05-05 22:54:35 +09:00
Calvin Kim
cfb39f790f blockchain: Use slices when fetching utxos
Maps have a higher overhead than slices.  As slices can be used instead
of maps, we avoid the overhead of making a map.
2023-05-05 22:54:33 +09:00
Olaoluwa Osuntokun
f523d4ccaa
wire: remove erroneous witness size check in wire parsing
In this commit, we fix a bug that would cause nodes to be unable to
parse a given block from the wire. The block would be properly accepted
if fed in via other mechanisms.

The issue here is that the old checks for the maximum witness size,
circa segwit v0 where placed in the wire package _as well_ as the tx
engine. This check should only be in the engine, since it's properly
gated by other related scrip validation flags.

The fix itself is simple: limit witnesses only based on the maximum
block size in bytes, or ~4MB.
2022-10-09 18:09:02 -07:00
Olaoluwa Osuntokun
36e67158b2
blockchain: demote BIP 9 log statements
In this commit, we demote a series of log statements added while
debugging the modified BIP 9 state machine. These are rather spammy on
mainnet, so we demote the transition logs (moving to a new state) to
debug, and the remaining log (when we're still in started to trace).
2022-03-17 17:57:37 -07:00
Olaoluwa Osuntokun
6ab97a3dd8
blockchain: fix IsSpeedy() bug, add more logging in BIP 9 state machine 2022-03-15 18:23:39 -07:00
Olaoluwa Osuntokun
f7f7bb33c1
blockchain/indexers: add P2TR support to the addrindex 2022-03-15 18:23:37 -07:00