Commit graph

3837 commits

Author SHA1 Message Date
Olaoluwa Osuntokun
0d666ff7ce
Merge pull request #2075 from Roasbeef/btcutil-psbt-update-dec-2023
multi: update main package and btcutil to chainhash/v1.1.0, use optimized dsha256
2023-12-19 15:17:57 -08:00
Olaoluwa Osuntokun
19008edd0f
txscript: use a single shared scratch buffer in segwit sighash calc
We used to use a lot of small buffers for serialization, but now we'll
use one buffer large enough, and slice into it when needed.

``
name                  old time/op    new time/op    delta
CalcWitnessSigHash-8    31.5µs ± 0%    29.2µs ± 0%   -7.05%  (p=0.000 n=10+10)

name                  old alloc/op   new alloc/op   delta
CalcWitnessSigHash-8    19.9kB ± 0%    18.5kB ± 0%   -7.14%  (p=0.000 n=10+10)

name                  old allocs/op  new allocs/op  delta
CalcWitnessSigHash-8       801 ± 0%       445 ± 0%  -44.44%  (p=0.000 n=10+10)
```
2023-12-19 15:02:05 -08:00
Olaoluwa Osuntokun
046a70121a
txscript: use DoubleHashRaw for segwit sighash single calc
We can write direly into the hash writer vs serializing into a buffer,
then writing that into the hash writer.
2023-12-19 15:02:00 -08:00
Olaoluwa Osuntokun
adfb641a36
txscript: use DoubleHashRaw to write directly crypto.Hash for segwit sighash
In this commit, we optimize the sighash calc further by writing directly
into the buffer used for serialization by the sha256.New() instance
rather than to an intermediate buffer, which is then write to the hash
buffer.
2023-12-19 15:01:58 -08: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
Olaoluwa Osuntokun
4126760706
btcutil/psbt: update to btcutil btcutil/v1.1.4 2023-12-19 15:01:52 -08:00
Afanti
b2f340d9d9
fix: enhance code comments (#2074)
* fix: enhance code comments
2023-12-19 10:40:37 -05:00
Olaoluwa Osuntokun
4c2ce6daed
Merge pull request #2072 from Roasbeef/chainhash-v-1-1-0-btcutil
btcutil: update to chaincfg/chainhash/v1.1.0
2023-12-18 16:36:33 -08:00
Olaoluwa Osuntokun
941e42c781
Merge pull request #1955 from kcalvinalvin/utxocache-original
blockchain, cmd, netsync, main: Add utxocache
2023-12-18 16:35:17 -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
84bdd0180e ffldb: change export_test.go to export.go
The testing function in export_test.go is changed to just export.go so
that callers outside the ffldb package will be able to call the
function.

The main use for this is so that the prune code can be triggered from
the blockchain package.  This allows testing code to have less than
1.5GB worth of blocks to trigger the prune.
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
d387d162f3 database/ffldb: make PruneBlocks atomic
PruneBlocks used to delete files immediately before the database
transaction finished.  By making the prune atomic, we can guarantee that
the database flush will happen before the utxo cache is flushed,
ensuring that the utxo cache is never in an irrecoverable state.
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
Olaoluwa Osuntokun
28a816f050
btcutil/psbt: update to chaincfg/chainhash/v1.1.0 + btcutil/v1.1.3
We also remove the replace directives in place.
2023-12-15 14:55:10 -08:00
Olaoluwa Osuntokun
7745cbb679
btcutl/gcs: update filter logic to use new DoubleHashRaw 2023-12-15 14:42:35 -08:00
Olaoluwa Osuntokun
e22513c237
btcutil: update to chaincfg/chainhash/v1.1.0 2023-12-15 14:42:29 -08:00
Olaoluwa Osuntokun
96c9fd8078
Merge pull request #1978 from kcalvinalvin/memory-efficient-txhash
chainhash, wire, btcutil, main: Memory efficient txhash
2023-12-15 14:18:05 -08: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
Oliver Gugger
4f72645a8a make: add install and release-install goals
To simplify building the release-grade (stripped and
reproducible) binaries from source, we add the install and
release-install make goals. Running either of the commands will create
binaries in the $GOPATH/bin directories.
The main difference between the two goals is that the release-install
will not contain any local paths and no debug information.
2023-12-11 09:16:27 -05:00
Oliver Gugger
36637f9ff9 git: add binaries to .gitignore file 2023-12-11 09:16:27 -05:00
Oliver Gugger
c9cda53709 multi: remove use of GO111MODULE
The use of the GO111MODULE environment variable doesn't have any effect
anymore and hasn't for a couple of versions. The default was set to "on"
a while back, so we can remove that variable everywhere.
2023-12-11 09:16:27 -05:00
Olaoluwa Osuntokun
55ac06b142
Merge pull request #2035 from kcalvinalvin/2023-09-25-change-is-sync-candidate-behavior
wire, netsync: change isSyncCandidate behavior
2023-12-08 16:28:18 -08:00
Peter Todd
909106575e Update petertodd seed DNS to .net
Also, add mainnet seed.
2023-12-08 09:58:00 -05: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
John C. Vernaleo
7520523a9a Correct comments in singature.go 2023-11-29 13:20:44 -05:00
wydengyre
a4236c5010 schnorr: simplify some signing math
We reuse the Bytes() function rather than duplicating its logic.
2023-11-29 09:44:44 -05:00
Evan Tedesco
6ea604df63 Fix typos on signature.go 2023-11-28 16:44:19 -05: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
Calvin Kim
b4992febff netsync: change isSyncCandidate behavior to include pruned nodes
isSyncCandidate is now changed to return true even if the peer is a
pruned node if and only if our chaintip is within 288 blocks of the
peer.

Rationale:
Pruned nodes that signal NODE_NETWORK_LIMITED MUST serve 288 blocks from
their chaintip.  If our chaintip is within that range, this peer can be
a sync candidate even if they aren't an archival node.
2023-11-07 11:04:34 +09:00
Calvin Kim
a09e7b224a wire: add HasFlag method 2023-11-07 11:04:34 +09:00
Calvin Kim
375f79dcd0 chainhash: Add DoubleHashRaw
DoubleHashRaw provides a simple function for doing double hashes.  Since
it uses the digest instead of making the caller allocate a byte slice, it
can be more memory efficient vs other double hash functions.
2023-11-07 08:27:02 +09:00
ClaytonNorthey92
d15dd71087 added tests for GetBestBlockHashAsync
now testing that GetBestBlockHashAsync sends the getbestblockhash command via websocket connection and that the channel returned can be used to send the response when it is received
2023-11-06 15:56:24 -05:00
ClaytonNorthey92
d988b86027 Added test for client.GetChainTxStatsAsync() in rpcclient. This sets up a test websocket server to run the tests. Also, ensure these are run within a timeout, since they rely on concurrency 2023-11-02 16:16:11 -04:00
ClaytonNorthey92
208800c5f4 Added tests for FutureGetBestBlockHashResult.Receive 2023-11-02 15:17:27 -04:00
0xEclair
0e795fbbfb fix: default forward port 2023-11-01 16:51:20 -04:00
Olaoluwa Osuntokun
e16cdb6bcf
Merge pull request #2051 from Crypt-iQ/txscript_alloc_cb_change
blockchain: export CheckSerializedHeight
2023-10-31 13:02:24 -07:00