Skip to content

Adds Batch client connection and async RPC calls to the bitcoind client#1

Open
Vib-UX wants to merge 8 commits into
masterfrom
BatchClient
Open

Adds Batch client connection and async RPC calls to the bitcoind client#1
Vib-UX wants to merge 8 commits into
masterfrom
BatchClient

Conversation

@Vib-UX

@Vib-UX Vib-UX commented Apr 13, 2022

Copy link
Copy Markdown
Owner

PR serves in relation to fixing issue 5041

With this PR we enable batch client connection and async RPC calls to the bitcoind client providing testcoverages for the same. This helps us in batching the required requests needed for speeding up operations by reducing round trips.

Pull Request Checklist

Testing

  • Your PR passes all CI checks.
  • Tests covering the positive and negative (error paths) are included.
  • Bug fixes contain tests triggering the bug to prevent regressions.

Code Style and Documentation

📝 Please see our Contribution Guidelines for further guidance.

@guggero guggero left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks pretty good! Mostly style nits, functionality looks to there 👍

Comment thread chain/bitcoind_client.go Outdated
Comment thread chain/bitcoind_client.go Outdated
Comment thread chain/bitcoind_conn.go Outdated
Comment thread chain/bitcoind_client.go Outdated
@Vib-UX Vib-UX self-assigned this Apr 15, 2022
@Vib-UX Vib-UX added the enhancement New feature or request label Apr 15, 2022
@Vib-UX Vib-UX requested a review from guggero April 18, 2022 05:40
@guggero guggero removed their request for review May 5, 2022 13:31

@bhandras bhandras left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice work @Vib-UX ⚡ One general comment I can add is to please add some more detail to your commit messages. In general a good commit message is similar to what's describe in this doc:https://github.com/lightningnetwork/lnd/blob/master/docs/code_contribution_guidelines.md#model-git-commit-messages

Obviously it's not a requirement, but a good practice. At a minimum add some short description about what your commit changes, eg. packagename: adding a new shiny func to achieve some goals.

Looking forward for some test coverage as well :)

Comment thread chain/bitcoind_client.go Outdated
Comment thread chain/bitcoind_conn.go Outdated
Comment thread chain/bitcoind_conn.go Outdated
Comment thread chain/bitcoind_client.go Outdated
Comment thread chain/bitcoind_client.go Outdated
Comment thread chain/bitcoind_client.go Outdated
Comment thread chain/bitcoind_conn.go Outdated
Comment thread chain/bitcoind_conn.go Outdated
Comment thread chain/bitcoind_conn.go Outdated
Comment thread chain/bitcoind_conn.go Outdated
@Vib-UX Vib-UX force-pushed the BatchClient branch 2 times, most recently from ef5c3f5 to 1a97548 Compare May 10, 2022 04:59
@Vib-UX Vib-UX force-pushed the BatchClient branch 4 times, most recently from a457655 to 7045f44 Compare June 1, 2022 20:07
Comment thread chain/bitcoind_client.go Outdated
Comment thread chain/bitcoind_client.go Outdated
Comment thread chain/bitcoind_client.go Outdated
Comment thread chain/bitcoind_conn.go Outdated
Comment thread chain/bitcoind_client.go
Comment thread chain/bitcoind_conn.go Outdated
Comment thread chain/bitcoind_conn.go

@bhandras bhandras left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great work! Getting very close. RescanBlocks() needs some more fixing but other than that almost good to go.

Comment thread chain/bitcoind_client.go
Comment thread chain/bitcoind_conn.go Outdated
Comment thread chain/bitcoind_conn.go
Comment thread chain/bitcoind_client.go Outdated
Comment thread chain/bitcoind_client.go Outdated
Comment thread chain/bitcoind_client.go Outdated
Comment thread chain/bitcoind_client.go Outdated
Comment thread chain/bitcoind_client.go Outdated
Comment thread chain/bitcoind_client.go Outdated
Comment thread chain/bitcoind_client.go
@Vib-UX Vib-UX force-pushed the BatchClient branch 2 times, most recently from 760fa75 to a13d267 Compare July 17, 2022 11:31
@Vib-UX Vib-UX force-pushed the BatchClient branch 4 times, most recently from 8aecdbc to 3b15834 Compare July 29, 2022 18:06

@bhandras bhandras left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM, just a few nits remain, great work @Vib-UX 🎉

Comment thread chain/bitcoind_client.go Outdated
Comment thread chain/bitcoind_client.go Outdated
Comment thread chain/bitcoind_client.go
Comment thread chain/bitcoind_client.go Outdated
Comment thread chain/bitcoind_client_test.go Outdated
Comment thread chain/bitcoind_client_test.go Outdated
Comment thread chain/bitcoind_client_test.go
Comment thread chain/bitcoind_client_test.go Outdated
// TestBitcoindRescanBlocks ensures that we correctly retrieve the required details
// for the watched txn.
func TestBitcoindRescanBlocks(t *testing.T) {
testBitcoindRescanBlocks(t, true)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: can create a subtest here with:

t.Run(fmt.Sprintf("rpcpoll=true"), func(t *testing.T) { ... }) etc

Comment thread chain/bitcoind_client_test.go Outdated
Comment thread chain/bitcoind_client_test.go Outdated
@bhandras

bhandras commented Aug 2, 2022

Copy link
Copy Markdown

Actually one last minute comment that maybe needs more work: it is a bit unfortunate that we need to add back old code in order to test expectations, perhaps a better way would be to instead check the output of the new batched call since we know what the output should look like. Otherwise there'll always remain some unused code as part of the public interface that we'll never use.

Alternatively you can also rename the "Old" functions as "Sync", meaning they're non batched, slow sequential calls. Also what do you think @guggero ?

@Vib-UX

Vib-UX commented Aug 4, 2022

Copy link
Copy Markdown
Owner Author

Actually one last minute comment that maybe needs more work: it is a bit unfortunate that we need to add back old code in order to test expectations, perhaps a better way would be to instead check the output of the new batched call since we know what the output should look like. Otherwise there'll always remain some unused code as part of the public interface that we'll never use.

Alternatively you can also rename the "Old" functions as "Sync", meaning they're non batched, slow sequential calls. Also what do you think @guggero ?

btcwallet-testCoverageSpeedUp

The updated testcoverage removes dependency from the previous Sync functions making it redundant. It was earlier there to compare speedup.

@guggero

guggero commented Aug 5, 2022

Copy link
Copy Markdown
Collaborator

Alternatively you can also rename the "Old" functions as "Sync", meaning they're non batched, slow sequential calls. Also what do you think @guggero ?

Just jumping in to answer the question, I'm probably lacking most of the context of the previous discussion. But I'd generally not rename any existing functions, so other users (lnd isn't the only software that uses btcwallet AFAIK) don't need to change any code when updating. And using the same name as before for something that works differently is not a good idea either. So I'd keep everything existing as it is and add *Async methods for new stuff (which I believe is what's currently being done?).

@bhandras

bhandras commented Aug 5, 2022

Copy link
Copy Markdown

Alternatively you can also rename the "Old" functions as "Sync", meaning they're non batched, slow sequential calls. Also what do you think @guggero ?

Just jumping in to answer the question, I'm probably lacking most of the context of the previous discussion. But I'd generally not rename any existing functions, so other users (lnd isn't the only software that uses btcwallet AFAIK) don't need to change any code when updating. And using the same name as before for something that works differently is not a good idea either. So I'd keep everything existing as it is and add *Async methods for new stuff (which I believe is what's currently being done?).

Yeah maybe best to keep existing functionality and unrenamed. However since the new functionality is not exactly the same semantics as when using the "Async" versions of the RPC API, we could name them "Batched" perhaps to refer that it is doing batched calls under the hood.

@Vib-UX Vib-UX force-pushed the BatchClient branch 2 times, most recently from 556f769 to a6ba11e Compare August 6, 2022 02:23
@Vib-UX Vib-UX requested a review from guggero August 6, 2022 02:54
@Vib-UX

Vib-UX commented Aug 6, 2022

Copy link
Copy Markdown
Owner Author

rebase master : a6ba11e to 36bcc1e

@bhandras bhandras left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM, just a few nits remain, great work! 🎉 Make sure to add a detailed description when you open the pull request against the main repo! 🚀

Comment thread chain/bitcoind_conn.go Outdated
func (c *BitcoindConn) GetBlocksBatch(
hashes []*chainhash.Hash) ([]*wire.MsgBlock, error) {

batchRequest := make(

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: batchRequest could actually just be a slice, no need to key it with the hashes since we always iterate on hashes.

Comment thread chain/bitcoind_client.go

// requests map will store rpcclient.FutureGetBlockHeaderVerboseResult
// for each blockHash.
requests := make(

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: i think this also could just be an array since we always iterate on blockHashes.

Comment thread chain/bitcoind_client.go Outdated
// a FilterBlocksReponse for the first block containing a matching address.
// If no matches are found in the range of blocks requested, the returned
// response will be nil.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: extra nl

require.NoError(t, err)
msgBlocksStandard = append(msgBlocksStandard, msgBlock)
}
msgBlocksBatchClient, err := bitcoindClient.GetBlocksBatch(blockHashes)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: add an extra nl after the for block

Comment thread chain/bitcoind_client_test.go Outdated
// linearly with GetBlock() (store the result for each GetBlock() in a
// slice) further also try to retrieve blocks in batch with GetBlocksBatch().
// At last will compare the results from both which should be equal.
if _, err := miner.Client.Generate(5); err != nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: I think it'd be better to generate something like 2x15 + some blocks to test that we do create multiple batches correctly.

@guggero

guggero commented Aug 8, 2022

Copy link
Copy Markdown
Collaborator

I think it makes sense to open the PR against the main repo now. I'll happily do a review there. Is that okay for everyone?

@guggero guggero removed their request for review August 8, 2022 15:22
@bhandras

bhandras commented Aug 8, 2022

Copy link
Copy Markdown

I think it makes sense to open the PR against the main repo now. I'll happily do a review there. Is that okay for everyone?

Yeah, sure. Actually we also just agree with @Vib-UX to open it against upstream, since the PR is quite ready as is.

@Vib-UX Vib-UX changed the title Batch client Adds Batch client connection and async RPC calls to the bitcoind client Aug 8, 2022
@Vib-UX Vib-UX force-pushed the BatchClient branch 2 times, most recently from 759043b to 7f29aa8 Compare August 21, 2022 16:08
This commit adds a batch client connection and async RPC
calls to the bitcoind client which will enable batching multiple
requests speeding up operations by reducing round trips.
This will leverage the updated batch client to fetch multiple blocks in
one go which can speed up rescan and filter blocks.
Both RescanBlocksBatched() and FilterBlocksBatched() leverages GetBlocksBatch()
which works with the updated batch client for speed ups.
Testcoverage to ensure that GetBlocksBatch() which leverages batchAPI
works as expected with enhanced performance.
Testcoverage to ensure that RescanBlocksBatched() which leverages
updated batchAPI for scanning the chain works as expected with enhanced
performance.
Testcoverage to ensure that FilterBlocksBatched() which leverages
batchAPI for scanning the chain works as expected with enhanced
performance.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants