Commit graph

3973 commits

Author SHA1 Message Date
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
Conner Fromknecht
8c4da83890
wire/msgblock+msgtx: user block-level script slab 2023-12-15 16:38:02 -08:00
Conner Fromknecht
d7396dc13d
wire/msgtx: use tx-level script slab 2023-12-15 16:37:57 -08:00
Conner Fromknecht
2e6eefcab1
wire/msgtx: remove unused read/writeOutPoint 2023-12-15 16:37:54 -08:00
Conner Fromknecht
4cc4f76c55
wire/msgtx: remove unused readScript 2023-12-15 16:37:51 -08:00
Conner Fromknecht
e0fa866890
wire/msgtx: remove unused readTxIn 2023-12-15 16:37:49 -08:00
Conner Fromknecht
f0184e58ba
wire/msgtx: remove unused writeTxIn 2023-12-15 16:37:46 -08:00
Conner Fromknecht
da89ed68ff
wire/msgtx: remove unused writeTxWitness 2023-12-15 16:37:44 -08:00
Conner Fromknecht
80ae5d342c
wire/msgpong: remove usage of read/writeElement 2023-12-15 16:37:41 -08:00
Conner Fromknecht
3698f2deab
wire/msgping: remove usage for read/writeElement 2023-12-15 16:37:39 -08:00
Conner Fromknecht
7207967135
wire/msgmerkleblock: optimize serialization by reusing small buffers 2023-12-15 16:37:36 -08:00
Conner Fromknecht
8bf07cc0bf
wire/netaddress: add optimiezed read/writeNetAddressBuf 2023-12-15 16:37:34 -08:00
Conner Fromknecht
dc4fbb04b3
wire/msgreject: optimize serialization by reusing small buffers 2023-12-15 16:37:31 -08:00
Conner Fromknecht
57daac345e
wire/common: add optimized writeVarStrBuf an readVarStrBuf 2023-12-15 16:37:29 -08:00
Conner Fromknecht
1cd5e02838
wire/invvect: remove unused readInvVect and writeInvVect 2023-12-15 16:37:26 -08:00
Conner Fromknecht
efcf964be2
wire/msgnotfound: optimize serialization by reusing small buffers 2023-12-15 16:37:24 -08:00
Conner Fromknecht
834febbb86
wire/msgcfilter: optimize serialization by reusing small buffers 2023-12-15 16:37:21 -08:00
Conner Fromknecht
ddeba60026
wiree/msggetcfilters: optimize serialization by reusing small buffers 2023-12-15 16:37:19 -08:00
Conner Fromknecht
d6594daa49
wire/msggetdata: optimize serialization by reusing small buffers 2023-12-15 16:37:16 -08:00
Conner Fromknecht
2383a04977
wire/msginv: optimize by reusing small buffers 2023-12-15 16:37:13 -08:00
Conner Fromknecht
f37f4750dc
wire/msgcfcheckpt: optimize serialization by reusing small buffers 2023-12-15 16:37:09 -08:00
Conner Fromknecht
1990555eee
wire/msggetcfcheckpt: optimize by removing read/writeElement 2023-12-15 16:37:02 -08:00
Conner Fromknecht
1c525dbd0e
wire/msgcfheaders: optimize encode/decode by using one small buffer 2023-12-15 16:36:56 -08:00
Conner Fromknecht
d042fe0586
wire/msggetcfheaders: use single small buffer for encode/decode 2023-12-15 16:36:54 -08:00
Conner Fromknecht
83675cb393
wire/msgheaders: optimize serialization by reusing small buffers 2023-12-15 16:36:51 -08:00
Conner Fromknecht
c0d35e6d92
wire/msggetheaders: optimize serialization by reusing small buffers 2023-12-15 16:36:49 -08:00
Conner Fromknecht
d8e08170bd
wire/msgblock: optimize DeserializeTxLoc by reusing small buffers 2023-12-15 16:36:46 -08:00
Conner Fromknecht
ee1f8077d8
wire/msgblock: use only one small buffer per block encode/decode 2023-12-15 16:36:44 -08:00
Conner Fromknecht
4ebc651689
wire/msggetblocks: optimize by reusing small buffer 2023-12-15 16:36:41 -08:00
Conner Fromknecht
674c2202ea
wire/invvect: add optimized readInvVectBuf and writeInvVectBuf 2023-12-15 16:36:38 -08:00
Conner Fromknecht
3cee06eaeb
wire/blockheader: introduce optimized writeBlockHeaderBuf 2023-12-15 16:36:36 -08:00
Conner Fromknecht
aa769e3da6
wire/bench_test: introduce optimized readBlockHeaderBuf 2023-12-15 16:36:33 -08:00
Conner Fromknecht
0cf8c19d5a
wire/msgtx: use readScriptBuf in txn serialization 2023-12-15 16:36:31 -08:00
Conner Fromknecht
3a91303a1a
wire/msgtx: use writeTxWitnessBuf in txn serialization 2023-12-15 16:36:28 -08:00
Conner Fromknecht
3bfd0c6cb3
wire/msgtx: introduce optimized writeTxWitnessBuf 2023-12-15 16:36:26 -08:00
Conner Fromknecht
24d42177a2
wire/msgtx: use readTxOutBuf in txn serialization 2023-12-15 16:36:23 -08:00
Conner Fromknecht
aebc74398a
wire/msgtx: introduce optimized readTxOutBuf 2023-12-15 16:36:20 -08:00
Conner Fromknecht
48d31e5926
wire/msgtx: use WriteTxOutBuf in txn serialization 2023-12-15 16:36:18 -08:00
Conner Fromknecht
7c8844f56f
wire/msgtx: introduce optimized WriteTxOutBuf 2023-12-15 16:36:15 -08:00
Conner Fromknecht
607eea193f
wire/msgtx: use readTxInBuf in txn serialization 2023-12-15 16:36:13 -08:00
Conner Fromknecht
6f4a7a142a
wire/msgtx: introduce optimized readTxInBuf 2023-12-15 16:36:10 -08:00