Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add and use support for pulling tx without PSBT wrappers #6983

Merged
merged 15 commits into from Jan 16, 2024

Conversation

jgriffiths
Copy link
Contributor

@jgriffiths jgriffiths commented Jan 9, 2024

Includes #6882 which is rebased and I reckon is ready to merge.

Supersedes #6975, @vincenzopalazzo @grubles your review/testing would be appreciated.

Fixes #6975
Fixes #6955

@cdecker
Copy link
Member

cdecker commented Jan 9, 2024

Thanks @jgriffiths this looks great, and I'm glad to see some of the utilities we open-coded in CLN making their way back into libwally 👍

I'll wait for @grubles to confirm that this works, and then merge it. In the meantime I think your script change in the last commit could give us a speed boost and so I'm benchmarking a blockchain sync with and without this change to quantify that.

Copy link
Collaborator

@vincenzopalazzo vincenzopalazzo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK fe96d3a

This looks good to me and my node is syncing with the liquid chain. It is not difficult to get a pegin transaction while syncing, so currently there is no crash.

Thanks for push this work on the final line

@grubles
Copy link
Contributor

grubles commented Jan 9, 2024

ACK fe96d3a

Builds and syncs with --network=liquid successfully.

@jgriffiths
Copy link
Contributor Author

jgriffiths commented Jan 9, 2024

I'll wait for @grubles to confirm that this works, and then merge it.

Awesome, thanks @cdecker

I'm glad to see some of the utilities we open-coded in CLN making their way back into libwally 👍

Worth noting that nothing has moved from cln to wally. The removed code was either added to wally for other purposes or was already in wally (but not exposed through its api,) and was exposed for 1.0.0.

I say this is worth mentioning because there is code in cln that should move to wally, I'm thinking particularly of the taproot code outside of the keypath spending support gsanders and I worked to merge last year. Also the previous iteration of the wally update PR removed a large amount of code relating to witness serialization but I had to pull it out as no-one helped me figure out how it was upsetting tal. Unfortunately I don't have time to work on this exclusively, but I would be happy to work with someone to get code into wally where it can help others who need e.g. taproot support.

In the meantime I think your script change in the last commit could give us a speed boost and so I'm benchmarking a blockchain sync with and without this change to quantify that.

That change and your alloc removal will help, but note that your changes will be lost in the noise of the rest of that PR. In order of effect, the changes in this PR that will improve speed should be:

  1. Building wally and libsecp in release mode, which enables asm hash impls amongst other optimizations
  2. Not building PSBTs for every tx in each block and elsewhere (multiple dozens of allocations per tx)
  3. Not re-serializing the tx to redundantly re-compute its txid (allocates, serializes and multi-hashes every tx redundantly)

If block processing in particular is a concern, one easy win I noticed but didn't make is to remove the top level tal_count calls from the block tx iteration for loops by computing the count outside the loop first, particularly in topology.c. I don't believe the compiler will optimize out these calls, and they perform redundant verification each time. The double nested loops for fees in particular are probably dominated by wasted tal overhead.

edit: I've made the above change to avoid the tal overhead, and also added early-exit for pegin, fee and cant-possibly-be-p2wsh outputs.

@jgriffiths jgriffiths force-pushed the pull_tx_only branch 2 times, most recently from 9062395 to 5f0712a Compare January 10, 2024 20:57
@vincenzopalazzo vincenzopalazzo added this to the v24.02 milestone Jan 14, 2024
@cdecker
Copy link
Member

cdecker commented Jan 15, 2024

Worth noting that nothing has moved from cln to wally. The removed code was either added to wally for other purposes or was already in wally (but not exposed through its api,) and was exposed for 1.0.0.

Oh yeah, I meant more as in the use-cases we needed in CLN seem to be shared, rather than the code. Thanks for the detailed answer too, it'd certainly be useful to consolidate on wally for anything that accesses or manipulates TXs.

I also finally managed to get benchmarks working, which was strange since the bitcoind backend was slower than CLN could process them :-)

Awesome find on that redundant tal_count and fee computation issue 👍

@jgriffiths
Copy link
Contributor Author

bitcoind backend was slower than CLN could process them :-)

Probably no point optimising further for now then :)

I suspect the rpc overhead is what make this true. The absolute fastest way to do initial sync if the core block files are available would be to use an extremely cut-down block parser based on e.g. https://github.com/rustyrussell/bitcoin-iterate. With an interface that just returns each tx in the block as a pointer + length, the block txs could be constructed directly from the memmapped bytes without rpc overhead and without bin<>hex conversion. At the expense of having more code to maintain you could leave some of the parsing code intact and pass the filter criteria into the call so that filtered txs are skipped completely without even creating them.

Rename the offending functions from wally_foo to cln_wally_foo.

For the sake of a minimal diff, only calls which conflict with wally
v1.0.0 have been changed. However it is bad form to use the wally_
function namespace; the remaining such calls should also be renamed.

Changelog-None

Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
Also removes incorrect/redundant configure flags when building.

Changelog-Changed: Update libwally to 1.0.0

Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
As of wally v1.0.0, Elements/Liquid support is enabled and part of the
library ABI by default.

Changelog-None

Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
Wally release builds are significantly faster than debug builds. Plus we
pass down our build flags to libsecp, which means release builds have
been disabling the asm optimisations for both libraries.

Changelog-Changed: Enable optimizations for libwally/libsecp256k1-zkp

Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
Changelog-None

Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
Changelog-None

Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
Changelog-None

Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
Input cloning has not been exposed yet; I'll add that to wally in a
future release.

Changelog-None

Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
There are many contexts in which it doesn't make sense to create a PSBT
wrapping the tx. There are also tx features that are not supported in
wallys PSBT implementation yet (such as pegins), which fail if a PSBT
wrapper is created, even though such features are unlikely to ever be
used in txs that cln will create/manipulate.

Allow these cases to be explicit that they only want the tx. This avoids
hitting such errors, is clearer on the caller side, and is more efficient.

Changelog-None

Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
Changelog-None

Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
Changelog-None

Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
…way again

Changelog-None

Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
…ock txs

Changelog-None

Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
- Avoid overhead from tal checks when iterating block txs
- Skip pegin txs as well as coinbase txs while iterating
- Early-exit if the txout cannot possibly be p2wsh
- Don't re-calculate the txid when we already have it
- Don't allocate a script for non-policy asset outputs
- Don't copy txids for non-interesting UTXOs

Note the below -Changed line covers the previous wally and PSBT commits
which also provide general block processing speedups.

Changelog-Changed: core: Processing blocks should now be faster

Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
Removes the tal_count checking overhead when iterating constant arrays.
Separated from the previous commit to make review easier.

Changelog-None

Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
@cdecker cdecker merged commit 43e302c into ElementsProject:master Jan 16, 2024
34 of 38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

crash with --network=liquid
4 participants