Skip to content

feat: chain source rest sync - #526

Merged
tnull merged 3 commits into
lightningdevkit:mainfrom
enigbe:2025-04-bitcoind-rest-sync-v2
Jul 1, 2025
Merged

feat: chain source rest sync#526
tnull merged 3 commits into
lightningdevkit:mainfrom
enigbe:2025-04-bitcoind-rest-sync-v2

Conversation

@enigbe

@enigbeenigbe commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

What this PR does

This PR introduces chain source synchronization with Bitcoin Core's REST API. To do so, it explicitly distinguishes between the bitcoind_rpc_client and the newly introduced bitcoind_sync_client that can sync either via the RPC or REST APIs.

BitcoindRpcClient is maintained and the distinction created because the REST interface exposed by Bitcoin Core is not as robust as the RPC interface. This makes it impossible to send POST request to broadcast a transaction for example.

Related Issues

Fixes#496

cc @tnull@joostjager

@ldk-reviews-bot

ldk-reviews-bot commented Apr 18, 2025

Copy link
Copy Markdown

👋 Thanks for assigning @tnull as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

@enigbeenigbe changed the title feat: chain source rest sync with bitcoindfeat: chain source rest syncApr 18, 2025
@tnull
tnull requested review from tnull and removed request for joostjagerApril 18, 2025 10:47

@tnulltnull 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.

Thanks for looking into this! Before I get into more detailed review, I want to note two things on the chosen approach that stood out to me from a first look:

  1. I'm not quite convinced we should rename the BitcoindRpc client. Wouldn't it be cleaner to just add a (granted, rather duplicative) BitcoindRest variant side-by-side, and then in a second step maybe see how we can DRY up the code. Mushing both variants together seems to result in a rather confusing API and code structure.
  2. I'm a bit confused why we'd now still have RPC credentials on set_chain_source_bitcoind (and elsewhere in downstream from that), even if we want to use the REST backend? Is there any operation that still requires RPC access? Can't we just rely on REST alone?

@enigbe

enigbe commented Apr 19, 2025

Copy link
Copy Markdown
ContributorAuthor

Thanks for the early feedback.

  1. I'm not quite convinced we should rename the BitcoindRpc client. Wouldn't it be cleaner to just add a (granted, rather duplicative) BitcoindRest variant side-by-side, and then in a second step maybe see how we can DRY up the code. Mushing both variants together seems to result in a rather confusing API and code structure.
  2. I'm a bit confused why we'd now still have RPC credentials on set_chain_source_bitcoind (and elsewhere in downstream from that), even if we want to use the REST backend? Is there any operation that still requires RPC access? Can't we just rely on REST alone?

I understand your concerns about the name and API changes I have introduced but here is a bit of context.

Adding a ChainSource::BitcondRest variant was my initial approach too because I had erroneously thought Bitcoin Core's REST interface exposed (more or less) the same APIs as the RPC interface. However, the REST interface exposes a handful of APIs only for GET requests. It is not possible to broadcast transactions with a pure REST variant as we do with the BitcoindRpcClient, There is no REST API to sendrawtransaction.
For that particular use case, given that we'd need a handle on an RPC client regardless, I thought it best to:

  • Keep the BitcoindRpcClient as is.
  • Introduce a configurable REST/RPC sync client. With this, users can decide if syncing should happen either via the REST or RPC APIs.
  • Rename ChainSource::BitcoindRpc to ChainSource::Bitcoind as the latter leaves no ambiguity about the possibility of a REST variant.

@tnull

Copy link
Copy Markdown
Collaborator

I understand your concerns about the name and API changes I have introduced but here is a bit of context.

Adding a ChainSource::BitcondRest variant was my initial approach too because I had erroneously thought Bitcoin Core's REST interface exposed (more or less) the same APIs as the RPC interface. However, the REST interface exposes a handful of APIs only for GET requests. It is not possible to broadcast transactions with a pure REST variant as we do with the BitcoindRpcClient, There is no REST API to sendrawtransaction. For that particular use case, given that we'd need a handle on an RPC client regardless, I thought it best to:

* Keep the `BitcoindRpcClient` as is.
* Introduce a configurable REST/RPC sync client. With this, users can decide if syncing should happen either via the REST or RPC APIs.
* Rename `ChainSource::BitcoindRpc` to `ChainSource::Bitcoind` as the latter leaves no ambiguity about the possibility of a REST variant.

Aaah, okay, then the proposed approach makes more sense. I think it's still a bit awkward to have the mandatory RPC credentials on the set_chain_source_bitcoind method and addtionally have the BitcoindSyncConfig::Rpc variant.

While it then makes sense to reuse the client internally, it might be simpler to just keep the original set_chain_source_bitcoind_rpc method and just add a set_chain_source_bitcoind_rest variant, that also features the mandatory RPC credentials?

@Camillarhi

Copy link
Copy Markdown
Contributor

Hi @enigbe,

I noticed your CI is failing. I had a similar issue, but it was resolved after rebasing onto the latest dev branch (which includes changes from #524).

Could you try rebasing and see if that fixes the problem?

Thanks

@chuksyschuksys left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looking into this PR has helped me gain a good understanding of how chain sourcing works. I think introducing BitcoindSyncClient is a good idea as the user can still carry out syncing operations using the REST API while the RPC Client is still maintained to carry out operations not supported by the REST API. I have just a few suggestions with regards to error handling.

Comment threadsrc/builder.rs Outdated
Comment threadsrc/builder.rs Outdated
Comment threadsrc/builder.rs Outdated
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 9b07570 to a28d28fCompareMay 7, 2025 08:28
@tnull

Copy link
Copy Markdown
Collaborator

@enigbe Please let me know when this is ready for a second look.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Thanks for the feedback @tnull. I revisited this last week and addressed concerns raised here.

Aaah, okay, then the proposed approach makes more sense. I think it's still a bit awkward to have the mandatory RPC credentials on the set_chain_source_bitcoind method and addtionally have the BitcoindSyncConfig::Rpc variant.

This has been removed and we just keep a config for REST sync.

While it then makes sense to reuse the client internally, it might be simpler to just keep the original set_chain_source_bitcoind_rpc method and just add a set_chain_source_bitcoind_rest variant, that also features the mandatory RPC credentials?

Agreed! This seems like a cleaner split. I've added a set_chain_source_bitcoind_rest variant accordingly. However, I think it'd be potentially confusing/awkward for callers to still pass RPC parameters to the REST function. I opted for set_chain_source_bitcoind_rest to take only REST parameters and clarified comments on usage and dependency on the RPC variant.

If you'd still prefer we pass RPC parameters as a required input for the REST variant, happy to adjust.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Hi @enigbe,

I noticed your CI is failing. I had a similar issue, but it was resolved after rebasing onto the latest dev branch (which includes changes from #524).

Could you try rebasing and see if that fixes the problem?

Thanks

Thanks for looking at this. I've rebased and have just the CLN integration tests failing. I tried reproducing the failing test locally but couldn't. I should give it another go today.

@tnull

Copy link
Copy Markdown
Collaborator

Thanks for looking at this. I've rebased and have just the CLN integration tests failing. I tried reproducing the failing test locally but couldn't. I should give it another go today.

Yes, I think this is unrelated, so feel free to ignore the flaky CI in this PR. Fixing it is really an unrelated task.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Looking into this PR has helped me gain a good understanding of how chain sourcing works. I think introducing BitcoindSyncClient is a good idea as the user can still carry out syncing operations using the REST API while the RPC Client is still maintained to carry out operations not supported by the REST API. I have just a few suggestions with regards to error handling.

Thanks for the review. Added the suggestions recommended for error handling.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

@enigbe Please let me know when this is ready for a second look.

This is ready @tnull.

@tnulltnull 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.

Hmm, tbh. I still dislike the current refactorings as they make the codepaths unnecessarily confusing and duplicative. Why keep a BitcoindRpcClient around side-by-side with the new BitcoindSyncClient that could also be an RPC client? If you maintain that merging in the REST codepaths with the RPC client is the way to go, why not extend the enum BitcoindSyncClient variants so that one would only hold the RPC client and the other the RPC and REST clients, which would allow us to add an fn rpc_client accessor that we can use everywhere, i.e., hence would allow us to drop the bitcoind_rpc_client field?

Comment threadsrc/builder.rs
Comment threadsrc/builder.rs Outdated
rpc_port: u16,
rpc_user: String,
rpc_password: String,
sync_client_config: Option<BitcoindRestSyncClientConfig>,

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.

Mhh, let's name this rest_client_config then.

Comment threadsrc/chain/mod.rs Outdated
}
}

pub(crate) enum BitcoindSyncClient {

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.

I'm not sure why this was added here vs bitcoind_rpc.rs.

@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch 3 times, most recently from 953bc1e to 1196080CompareMay 26, 2025 10:00
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Please excuse the delay on this. I have addressed concerns from the last review. Investigating a flaky test but it's ready for another look.

Let me know what you think when you can review.

@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 1196080 to d56e7a7CompareMay 26, 2025 12:19

@tnulltnull 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.

Excuse the considerable delay here, I have to admit this fell off my radar for a bit. Sorry! (feel free to re-request review if a PR is ready or it's been pending for a while)

Generally I like the updated approach much better from what I see. However, due to the huge code move and some commits that revert prior commits it's almost impossible to just review the parts of the code that actually changed (i.e., neither going commit-by-commit nor full-diff really works). Could you do me favor and clean up the commit history a bit? Also, I'm not conviced we gain much by the huge code move to common.rs/mod.rs if we don't actually split out REST and RPC types. Can we maybe revert that and keep everything in one file for now? Or, do the code move as an entirely separate commit in the beginning or end.

Thank you!

Comment threadbindings/ldk_node.udl Outdated
BackgroundSyncConfig? background_sync_config;
};

[Enum]

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.

nit: Seems this was first added here, then dropped in a later commit?

Comment threadsrc/chain/bitcoind/common.rs Outdated

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.

Can we please do the code moves in a prefactor commit? This would make it immediately clear that nothing changed when reviewing via git diff --color-moved.

And to be honest I'm not entirely sure why we move all that code at all, if we just end up with the implementation for both rpc and rest in mod.rs?

Comment threadsrc/chain/bitcoind/mod.rs Outdated

pub mod common;

pub enum BitcoindApiClient {

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.

Why not just BitcoindClient?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

This has been renamed.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Thanks for the feedback.

Excuse the considerable delay here, I have to admit this fell off my radar for a bit. Sorry! (feel free to re-request review if a PR is ready or it's been pending for a while)

Noted. I'll re-request going forward.

Generally I like the updated approach much better from what I see. However, due to the huge code move and some commits that revert prior commits it's almost impossible to just review the parts of the code that actually changed (i.e., neither going commit-by-commit nor full-diff really works). Could you do me favor and clean up the commit history a bit?

I'll clean up the commit history to make review much easier. Apologies.

Also, I'm not conviced we gain much by the huge code move to common.rs/mod.rs if we don't actually split out REST and RPC types. Can we maybe revert that and keep everything in one file for now? Or, do the code move as an entirely separate commit in the beginning or end.

You're right wrt common.rs. It is a leftover from when I separated REST and RPC types. Since we're not doing that split, I'll revert back to keeping everything in a single file to keep things simpler.

I'll have this cleaned up and ready shortly.

@tnulltnull 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.

This also needs a rebase now.

@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch 2 times, most recently from 9e88e4f to 4f34ec3CompareJune 11, 2025 16:45
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Hi @tnull

I've cleaned up the history and rebased. It is ready for another look.

@tnulltnull 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.

Excuse the delay here.

As far as I can tell the changes look mostly good, but the commit scheme still is very confusing to me and makes this very hard to review. Why are we first adding a _rest variant with a lot of copy/paste code, only to then merge them, whilst at the same time adding everything to a new file?

If you think this should all end up living in bitcoind.rs, please first make a commit moving the file to its new location (so that git understands it's the same file), and then make the changes in following commits. This would allow to see what actually was added and what changed in reference to the original code.

Comment threadsrc/builder.rs
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 4f34ec3 to c01b934CompareJune 20, 2025 14:55
@enigbe

enigbe commented Jun 20, 2025

Copy link
Copy Markdown
ContributorAuthor

Hi @tnull,

Why are we first adding a _rest variant with a lot of copy/paste code, only to then merge them, whilst at the same time adding everything to a new file?

I thought this might be helpful for other reviewers to see the justification in unifying the clients. Nonetheless, I've removed that commit so it should be as you'd prefer.

If you think this should all end up living in bitcoind.rs, please first make a commit moving the file to its new location (so that git understands it's the same file), and then make the changes in following commits. This would allow to see what actually was added and what changed in reference to the original code

I have added a prefactor commit to address the file change/rename.

Apologies for the confusion here. This should be much cleaner now.

@tnull
tnull self-requested a review June 23, 2025 12:13

@tnulltnull 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.

Thanks for the commit history cleanup, this is indeed now more reviewable. It still includes a bunch of unnecessary changes/refactorings that bloat the current changeset. It would be preferable not to include these (such as moving code blocks, switching up which types to import and which not, etc).

Moreover, I have to say I'm not super stoked about putting all the logic behind macros, as they are almost inaccessible to tooling (compiler checks, rustfmt, code completion, etc). Can we try to avoid them by prefactors that move the code to methods/closures or to helper methods that take whatever data these methods are operating on? If we do this, would be great to keep a sane git history.

All that said, functionally the code looks good to me.

Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from c01b934 to 925d214CompareJuly 1, 2025 07:24
enigbe added 2 commits July 1, 2025 08:43
Since we unify RPC and REST sync clients in the
following commit, we rename bitcoind_rpc to bitcoind
as this appropriately captures the unified client.
In the next commit, we'll introduce a unified bitcoind
client with two variant - RPC and REST. In preparation
to support chain syncing via the REST interface, we
refactor some methods into helper functions specific
to the interface client.
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 925d214 to ec53d60CompareJuly 1, 2025 07:44
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Hi @tnull,

This is ready for another look. As recommended, I have added:

  • prefector commit where the previously introduced macros are refactored into helper methods in preparation for REST support.
  • couple of fix-ups to address unwanted code move and type imports.

@tnulltnull 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.

Cool, thank you, much better without the macros!

Feel free to squash the fixups!

This commit:
- Adds support for syncing data via Bitcoin Core's
REST interface.
- Unifies the REST and RPC Bitcoin Core API clients
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from ec53d60 to 36e4c7fCompareJuly 1, 2025 10:18
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Squashed the fixups.

@tnull
tnull merged commit 0509e99 into lightningdevkit:mainJul 1, 2025
Sign up for freeto 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.

Bitcoind REST sync

5 participants

@enigbe@ldk-reviews-bot@tnull@Camillarhi@chuksys
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
feat: chain source rest sync by enigbe · Pull Request #526 · lightningdevkit/ldk-node · GitHub
Skip to content

feat: chain source rest sync - #526

Merged
tnull merged 3 commits into
lightningdevkit:mainfrom
enigbe:2025-04-bitcoind-rest-sync-v2
Jul 1, 2025
Merged

feat: chain source rest sync#526
tnull merged 3 commits into
lightningdevkit:mainfrom
enigbe:2025-04-bitcoind-rest-sync-v2

Conversation

@enigbe

@enigbeenigbe commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

What this PR does

This PR introduces chain source synchronization with Bitcoin Core's REST API. To do so, it explicitly distinguishes between the bitcoind_rpc_client and the newly introduced bitcoind_sync_client that can sync either via the RPC or REST APIs.

BitcoindRpcClient is maintained and the distinction created because the REST interface exposed by Bitcoin Core is not as robust as the RPC interface. This makes it impossible to send POST request to broadcast a transaction for example.

Related Issues

Fixes#496

cc @tnull@joostjager

@ldk-reviews-bot

ldk-reviews-bot commented Apr 18, 2025

Copy link
Copy Markdown

👋 Thanks for assigning @tnull as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

@enigbeenigbe changed the title feat: chain source rest sync with bitcoindfeat: chain source rest syncApr 18, 2025
@tnull
tnull requested review from tnull and removed request for joostjagerApril 18, 2025 10:47

@tnulltnull 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.

Thanks for looking into this! Before I get into more detailed review, I want to note two things on the chosen approach that stood out to me from a first look:

  1. I'm not quite convinced we should rename the BitcoindRpc client. Wouldn't it be cleaner to just add a (granted, rather duplicative) BitcoindRest variant side-by-side, and then in a second step maybe see how we can DRY up the code. Mushing both variants together seems to result in a rather confusing API and code structure.
  2. I'm a bit confused why we'd now still have RPC credentials on set_chain_source_bitcoind (and elsewhere in downstream from that), even if we want to use the REST backend? Is there any operation that still requires RPC access? Can't we just rely on REST alone?

@enigbe

enigbe commented Apr 19, 2025

Copy link
Copy Markdown
ContributorAuthor

Thanks for the early feedback.

  1. I'm not quite convinced we should rename the BitcoindRpc client. Wouldn't it be cleaner to just add a (granted, rather duplicative) BitcoindRest variant side-by-side, and then in a second step maybe see how we can DRY up the code. Mushing both variants together seems to result in a rather confusing API and code structure.
  2. I'm a bit confused why we'd now still have RPC credentials on set_chain_source_bitcoind (and elsewhere in downstream from that), even if we want to use the REST backend? Is there any operation that still requires RPC access? Can't we just rely on REST alone?

I understand your concerns about the name and API changes I have introduced but here is a bit of context.

Adding a ChainSource::BitcondRest variant was my initial approach too because I had erroneously thought Bitcoin Core's REST interface exposed (more or less) the same APIs as the RPC interface. However, the REST interface exposes a handful of APIs only for GET requests. It is not possible to broadcast transactions with a pure REST variant as we do with the BitcoindRpcClient, There is no REST API to sendrawtransaction.
For that particular use case, given that we'd need a handle on an RPC client regardless, I thought it best to:

  • Keep the BitcoindRpcClient as is.
  • Introduce a configurable REST/RPC sync client. With this, users can decide if syncing should happen either via the REST or RPC APIs.
  • Rename ChainSource::BitcoindRpc to ChainSource::Bitcoind as the latter leaves no ambiguity about the possibility of a REST variant.

@tnull

Copy link
Copy Markdown
Collaborator

I understand your concerns about the name and API changes I have introduced but here is a bit of context.

Adding a ChainSource::BitcondRest variant was my initial approach too because I had erroneously thought Bitcoin Core's REST interface exposed (more or less) the same APIs as the RPC interface. However, the REST interface exposes a handful of APIs only for GET requests. It is not possible to broadcast transactions with a pure REST variant as we do with the BitcoindRpcClient, There is no REST API to sendrawtransaction. For that particular use case, given that we'd need a handle on an RPC client regardless, I thought it best to:

* Keep the `BitcoindRpcClient` as is.
* Introduce a configurable REST/RPC sync client. With this, users can decide if syncing should happen either via the REST or RPC APIs.
* Rename `ChainSource::BitcoindRpc` to `ChainSource::Bitcoind` as the latter leaves no ambiguity about the possibility of a REST variant.

Aaah, okay, then the proposed approach makes more sense. I think it's still a bit awkward to have the mandatory RPC credentials on the set_chain_source_bitcoind method and addtionally have the BitcoindSyncConfig::Rpc variant.

While it then makes sense to reuse the client internally, it might be simpler to just keep the original set_chain_source_bitcoind_rpc method and just add a set_chain_source_bitcoind_rest variant, that also features the mandatory RPC credentials?

@Camillarhi

Copy link
Copy Markdown
Contributor

Hi @enigbe,

I noticed your CI is failing. I had a similar issue, but it was resolved after rebasing onto the latest dev branch (which includes changes from #524).

Could you try rebasing and see if that fixes the problem?

Thanks

@chuksyschuksys left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looking into this PR has helped me gain a good understanding of how chain sourcing works. I think introducing BitcoindSyncClient is a good idea as the user can still carry out syncing operations using the REST API while the RPC Client is still maintained to carry out operations not supported by the REST API. I have just a few suggestions with regards to error handling.

Comment threadsrc/builder.rs Outdated
Comment threadsrc/builder.rs Outdated
Comment threadsrc/builder.rs Outdated
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 9b07570 to a28d28fCompareMay 7, 2025 08:28
@tnull

Copy link
Copy Markdown
Collaborator

@enigbe Please let me know when this is ready for a second look.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Thanks for the feedback @tnull. I revisited this last week and addressed concerns raised here.

Aaah, okay, then the proposed approach makes more sense. I think it's still a bit awkward to have the mandatory RPC credentials on the set_chain_source_bitcoind method and addtionally have the BitcoindSyncConfig::Rpc variant.

This has been removed and we just keep a config for REST sync.

While it then makes sense to reuse the client internally, it might be simpler to just keep the original set_chain_source_bitcoind_rpc method and just add a set_chain_source_bitcoind_rest variant, that also features the mandatory RPC credentials?

Agreed! This seems like a cleaner split. I've added a set_chain_source_bitcoind_rest variant accordingly. However, I think it'd be potentially confusing/awkward for callers to still pass RPC parameters to the REST function. I opted for set_chain_source_bitcoind_rest to take only REST parameters and clarified comments on usage and dependency on the RPC variant.

If you'd still prefer we pass RPC parameters as a required input for the REST variant, happy to adjust.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Hi @enigbe,

I noticed your CI is failing. I had a similar issue, but it was resolved after rebasing onto the latest dev branch (which includes changes from #524).

Could you try rebasing and see if that fixes the problem?

Thanks

Thanks for looking at this. I've rebased and have just the CLN integration tests failing. I tried reproducing the failing test locally but couldn't. I should give it another go today.

@tnull

Copy link
Copy Markdown
Collaborator

Thanks for looking at this. I've rebased and have just the CLN integration tests failing. I tried reproducing the failing test locally but couldn't. I should give it another go today.

Yes, I think this is unrelated, so feel free to ignore the flaky CI in this PR. Fixing it is really an unrelated task.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Looking into this PR has helped me gain a good understanding of how chain sourcing works. I think introducing BitcoindSyncClient is a good idea as the user can still carry out syncing operations using the REST API while the RPC Client is still maintained to carry out operations not supported by the REST API. I have just a few suggestions with regards to error handling.

Thanks for the review. Added the suggestions recommended for error handling.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

@enigbe Please let me know when this is ready for a second look.

This is ready @tnull.

@tnulltnull 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.

Hmm, tbh. I still dislike the current refactorings as they make the codepaths unnecessarily confusing and duplicative. Why keep a BitcoindRpcClient around side-by-side with the new BitcoindSyncClient that could also be an RPC client? If you maintain that merging in the REST codepaths with the RPC client is the way to go, why not extend the enum BitcoindSyncClient variants so that one would only hold the RPC client and the other the RPC and REST clients, which would allow us to add an fn rpc_client accessor that we can use everywhere, i.e., hence would allow us to drop the bitcoind_rpc_client field?

Comment threadsrc/builder.rs
Comment threadsrc/builder.rs Outdated
rpc_port: u16,
rpc_user: String,
rpc_password: String,
sync_client_config: Option<BitcoindRestSyncClientConfig>,

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.

Mhh, let's name this rest_client_config then.

Comment threadsrc/chain/mod.rs Outdated
}
}

pub(crate) enum BitcoindSyncClient {

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.

I'm not sure why this was added here vs bitcoind_rpc.rs.

@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch 3 times, most recently from 953bc1e to 1196080CompareMay 26, 2025 10:00
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Please excuse the delay on this. I have addressed concerns from the last review. Investigating a flaky test but it's ready for another look.

Let me know what you think when you can review.

@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 1196080 to d56e7a7CompareMay 26, 2025 12:19

@tnulltnull 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.

Excuse the considerable delay here, I have to admit this fell off my radar for a bit. Sorry! (feel free to re-request review if a PR is ready or it's been pending for a while)

Generally I like the updated approach much better from what I see. However, due to the huge code move and some commits that revert prior commits it's almost impossible to just review the parts of the code that actually changed (i.e., neither going commit-by-commit nor full-diff really works). Could you do me favor and clean up the commit history a bit? Also, I'm not conviced we gain much by the huge code move to common.rs/mod.rs if we don't actually split out REST and RPC types. Can we maybe revert that and keep everything in one file for now? Or, do the code move as an entirely separate commit in the beginning or end.

Thank you!

Comment threadbindings/ldk_node.udl Outdated
BackgroundSyncConfig? background_sync_config;
};

[Enum]

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.

nit: Seems this was first added here, then dropped in a later commit?

Comment threadsrc/chain/bitcoind/common.rs Outdated

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.

Can we please do the code moves in a prefactor commit? This would make it immediately clear that nothing changed when reviewing via git diff --color-moved.

And to be honest I'm not entirely sure why we move all that code at all, if we just end up with the implementation for both rpc and rest in mod.rs?

Comment threadsrc/chain/bitcoind/mod.rs Outdated

pub mod common;

pub enum BitcoindApiClient {

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.

Why not just BitcoindClient?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

This has been renamed.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Thanks for the feedback.

Excuse the considerable delay here, I have to admit this fell off my radar for a bit. Sorry! (feel free to re-request review if a PR is ready or it's been pending for a while)

Noted. I'll re-request going forward.

Generally I like the updated approach much better from what I see. However, due to the huge code move and some commits that revert prior commits it's almost impossible to just review the parts of the code that actually changed (i.e., neither going commit-by-commit nor full-diff really works). Could you do me favor and clean up the commit history a bit?

I'll clean up the commit history to make review much easier. Apologies.

Also, I'm not conviced we gain much by the huge code move to common.rs/mod.rs if we don't actually split out REST and RPC types. Can we maybe revert that and keep everything in one file for now? Or, do the code move as an entirely separate commit in the beginning or end.

You're right wrt common.rs. It is a leftover from when I separated REST and RPC types. Since we're not doing that split, I'll revert back to keeping everything in a single file to keep things simpler.

I'll have this cleaned up and ready shortly.

@tnulltnull 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.

This also needs a rebase now.

@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch 2 times, most recently from 9e88e4f to 4f34ec3CompareJune 11, 2025 16:45
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Hi @tnull

I've cleaned up the history and rebased. It is ready for another look.

@tnulltnull 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.

Excuse the delay here.

As far as I can tell the changes look mostly good, but the commit scheme still is very confusing to me and makes this very hard to review. Why are we first adding a _rest variant with a lot of copy/paste code, only to then merge them, whilst at the same time adding everything to a new file?

If you think this should all end up living in bitcoind.rs, please first make a commit moving the file to its new location (so that git understands it's the same file), and then make the changes in following commits. This would allow to see what actually was added and what changed in reference to the original code.

Comment threadsrc/builder.rs
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 4f34ec3 to c01b934CompareJune 20, 2025 14:55
@enigbe

enigbe commented Jun 20, 2025

Copy link
Copy Markdown
ContributorAuthor

Hi @tnull,

Why are we first adding a _rest variant with a lot of copy/paste code, only to then merge them, whilst at the same time adding everything to a new file?

I thought this might be helpful for other reviewers to see the justification in unifying the clients. Nonetheless, I've removed that commit so it should be as you'd prefer.

If you think this should all end up living in bitcoind.rs, please first make a commit moving the file to its new location (so that git understands it's the same file), and then make the changes in following commits. This would allow to see what actually was added and what changed in reference to the original code

I have added a prefactor commit to address the file change/rename.

Apologies for the confusion here. This should be much cleaner now.

@tnull
tnull self-requested a review June 23, 2025 12:13

@tnulltnull 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.

Thanks for the commit history cleanup, this is indeed now more reviewable. It still includes a bunch of unnecessary changes/refactorings that bloat the current changeset. It would be preferable not to include these (such as moving code blocks, switching up which types to import and which not, etc).

Moreover, I have to say I'm not super stoked about putting all the logic behind macros, as they are almost inaccessible to tooling (compiler checks, rustfmt, code completion, etc). Can we try to avoid them by prefactors that move the code to methods/closures or to helper methods that take whatever data these methods are operating on? If we do this, would be great to keep a sane git history.

All that said, functionally the code looks good to me.

Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from c01b934 to 925d214CompareJuly 1, 2025 07:24
enigbe added 2 commits July 1, 2025 08:43
Since we unify RPC and REST sync clients in the
following commit, we rename bitcoind_rpc to bitcoind
as this appropriately captures the unified client.
In the next commit, we'll introduce a unified bitcoind
client with two variant - RPC and REST. In preparation
to support chain syncing via the REST interface, we
refactor some methods into helper functions specific
to the interface client.
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 925d214 to ec53d60CompareJuly 1, 2025 07:44
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Hi @tnull,

This is ready for another look. As recommended, I have added:

  • prefector commit where the previously introduced macros are refactored into helper methods in preparation for REST support.
  • couple of fix-ups to address unwanted code move and type imports.

@tnulltnull 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.

Cool, thank you, much better without the macros!

Feel free to squash the fixups!

This commit:
- Adds support for syncing data via Bitcoin Core's
REST interface.
- Unifies the REST and RPC Bitcoin Core API clients
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from ec53d60 to 36e4c7fCompareJuly 1, 2025 10:18
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Squashed the fixups.

@tnull
tnull merged commit 0509e99 into lightningdevkit:mainJul 1, 2025
Sign up for freeto 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.

Bitcoind REST sync

5 participants

@enigbe@ldk-reviews-bot@tnull@Camillarhi@chuksys
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' feat: chain source rest sync by enigbe · Pull Request #526 · lightningdevkit/ldk-node · GitHub
Skip to content

feat: chain source rest sync - #526

Merged
tnull merged 3 commits into
lightningdevkit:mainfrom
enigbe:2025-04-bitcoind-rest-sync-v2
Jul 1, 2025
Merged

feat: chain source rest sync#526
tnull merged 3 commits into
lightningdevkit:mainfrom
enigbe:2025-04-bitcoind-rest-sync-v2

Conversation

@enigbe

@enigbeenigbe commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

What this PR does

This PR introduces chain source synchronization with Bitcoin Core's REST API. To do so, it explicitly distinguishes between the bitcoind_rpc_client and the newly introduced bitcoind_sync_client that can sync either via the RPC or REST APIs.

BitcoindRpcClient is maintained and the distinction created because the REST interface exposed by Bitcoin Core is not as robust as the RPC interface. This makes it impossible to send POST request to broadcast a transaction for example.

Related Issues

Fixes#496

cc @tnull@joostjager

@ldk-reviews-bot

ldk-reviews-bot commented Apr 18, 2025

Copy link
Copy Markdown

👋 Thanks for assigning @tnull as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

@enigbeenigbe changed the title feat: chain source rest sync with bitcoindfeat: chain source rest syncApr 18, 2025
@tnull
tnull requested review from tnull and removed request for joostjagerApril 18, 2025 10:47

@tnulltnull 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.

Thanks for looking into this! Before I get into more detailed review, I want to note two things on the chosen approach that stood out to me from a first look:

  1. I'm not quite convinced we should rename the BitcoindRpc client. Wouldn't it be cleaner to just add a (granted, rather duplicative) BitcoindRest variant side-by-side, and then in a second step maybe see how we can DRY up the code. Mushing both variants together seems to result in a rather confusing API and code structure.
  2. I'm a bit confused why we'd now still have RPC credentials on set_chain_source_bitcoind (and elsewhere in downstream from that), even if we want to use the REST backend? Is there any operation that still requires RPC access? Can't we just rely on REST alone?

@enigbe

enigbe commented Apr 19, 2025

Copy link
Copy Markdown
ContributorAuthor

Thanks for the early feedback.

  1. I'm not quite convinced we should rename the BitcoindRpc client. Wouldn't it be cleaner to just add a (granted, rather duplicative) BitcoindRest variant side-by-side, and then in a second step maybe see how we can DRY up the code. Mushing both variants together seems to result in a rather confusing API and code structure.
  2. I'm a bit confused why we'd now still have RPC credentials on set_chain_source_bitcoind (and elsewhere in downstream from that), even if we want to use the REST backend? Is there any operation that still requires RPC access? Can't we just rely on REST alone?

I understand your concerns about the name and API changes I have introduced but here is a bit of context.

Adding a ChainSource::BitcondRest variant was my initial approach too because I had erroneously thought Bitcoin Core's REST interface exposed (more or less) the same APIs as the RPC interface. However, the REST interface exposes a handful of APIs only for GET requests. It is not possible to broadcast transactions with a pure REST variant as we do with the BitcoindRpcClient, There is no REST API to sendrawtransaction.
For that particular use case, given that we'd need a handle on an RPC client regardless, I thought it best to:

  • Keep the BitcoindRpcClient as is.
  • Introduce a configurable REST/RPC sync client. With this, users can decide if syncing should happen either via the REST or RPC APIs.
  • Rename ChainSource::BitcoindRpc to ChainSource::Bitcoind as the latter leaves no ambiguity about the possibility of a REST variant.

@tnull

Copy link
Copy Markdown
Collaborator

I understand your concerns about the name and API changes I have introduced but here is a bit of context.

Adding a ChainSource::BitcondRest variant was my initial approach too because I had erroneously thought Bitcoin Core's REST interface exposed (more or less) the same APIs as the RPC interface. However, the REST interface exposes a handful of APIs only for GET requests. It is not possible to broadcast transactions with a pure REST variant as we do with the BitcoindRpcClient, There is no REST API to sendrawtransaction. For that particular use case, given that we'd need a handle on an RPC client regardless, I thought it best to:

* Keep the `BitcoindRpcClient` as is.
* Introduce a configurable REST/RPC sync client. With this, users can decide if syncing should happen either via the REST or RPC APIs.
* Rename `ChainSource::BitcoindRpc` to `ChainSource::Bitcoind` as the latter leaves no ambiguity about the possibility of a REST variant.

Aaah, okay, then the proposed approach makes more sense. I think it's still a bit awkward to have the mandatory RPC credentials on the set_chain_source_bitcoind method and addtionally have the BitcoindSyncConfig::Rpc variant.

While it then makes sense to reuse the client internally, it might be simpler to just keep the original set_chain_source_bitcoind_rpc method and just add a set_chain_source_bitcoind_rest variant, that also features the mandatory RPC credentials?

@Camillarhi

Copy link
Copy Markdown
Contributor

Hi @enigbe,

I noticed your CI is failing. I had a similar issue, but it was resolved after rebasing onto the latest dev branch (which includes changes from #524).

Could you try rebasing and see if that fixes the problem?

Thanks

@chuksyschuksys left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looking into this PR has helped me gain a good understanding of how chain sourcing works. I think introducing BitcoindSyncClient is a good idea as the user can still carry out syncing operations using the REST API while the RPC Client is still maintained to carry out operations not supported by the REST API. I have just a few suggestions with regards to error handling.

Comment threadsrc/builder.rs Outdated
Comment threadsrc/builder.rs Outdated
Comment threadsrc/builder.rs Outdated
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 9b07570 to a28d28fCompareMay 7, 2025 08:28
@tnull

Copy link
Copy Markdown
Collaborator

@enigbe Please let me know when this is ready for a second look.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Thanks for the feedback @tnull. I revisited this last week and addressed concerns raised here.

Aaah, okay, then the proposed approach makes more sense. I think it's still a bit awkward to have the mandatory RPC credentials on the set_chain_source_bitcoind method and addtionally have the BitcoindSyncConfig::Rpc variant.

This has been removed and we just keep a config for REST sync.

While it then makes sense to reuse the client internally, it might be simpler to just keep the original set_chain_source_bitcoind_rpc method and just add a set_chain_source_bitcoind_rest variant, that also features the mandatory RPC credentials?

Agreed! This seems like a cleaner split. I've added a set_chain_source_bitcoind_rest variant accordingly. However, I think it'd be potentially confusing/awkward for callers to still pass RPC parameters to the REST function. I opted for set_chain_source_bitcoind_rest to take only REST parameters and clarified comments on usage and dependency on the RPC variant.

If you'd still prefer we pass RPC parameters as a required input for the REST variant, happy to adjust.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Hi @enigbe,

I noticed your CI is failing. I had a similar issue, but it was resolved after rebasing onto the latest dev branch (which includes changes from #524).

Could you try rebasing and see if that fixes the problem?

Thanks

Thanks for looking at this. I've rebased and have just the CLN integration tests failing. I tried reproducing the failing test locally but couldn't. I should give it another go today.

@tnull

Copy link
Copy Markdown
Collaborator

Thanks for looking at this. I've rebased and have just the CLN integration tests failing. I tried reproducing the failing test locally but couldn't. I should give it another go today.

Yes, I think this is unrelated, so feel free to ignore the flaky CI in this PR. Fixing it is really an unrelated task.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Looking into this PR has helped me gain a good understanding of how chain sourcing works. I think introducing BitcoindSyncClient is a good idea as the user can still carry out syncing operations using the REST API while the RPC Client is still maintained to carry out operations not supported by the REST API. I have just a few suggestions with regards to error handling.

Thanks for the review. Added the suggestions recommended for error handling.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

@enigbe Please let me know when this is ready for a second look.

This is ready @tnull.

@tnulltnull 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.

Hmm, tbh. I still dislike the current refactorings as they make the codepaths unnecessarily confusing and duplicative. Why keep a BitcoindRpcClient around side-by-side with the new BitcoindSyncClient that could also be an RPC client? If you maintain that merging in the REST codepaths with the RPC client is the way to go, why not extend the enum BitcoindSyncClient variants so that one would only hold the RPC client and the other the RPC and REST clients, which would allow us to add an fn rpc_client accessor that we can use everywhere, i.e., hence would allow us to drop the bitcoind_rpc_client field?

Comment threadsrc/builder.rs
Comment threadsrc/builder.rs Outdated
rpc_port: u16,
rpc_user: String,
rpc_password: String,
sync_client_config: Option<BitcoindRestSyncClientConfig>,

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.

Mhh, let's name this rest_client_config then.

Comment threadsrc/chain/mod.rs Outdated
}
}

pub(crate) enum BitcoindSyncClient {

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.

I'm not sure why this was added here vs bitcoind_rpc.rs.

@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch 3 times, most recently from 953bc1e to 1196080CompareMay 26, 2025 10:00
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Please excuse the delay on this. I have addressed concerns from the last review. Investigating a flaky test but it's ready for another look.

Let me know what you think when you can review.

@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 1196080 to d56e7a7CompareMay 26, 2025 12:19

@tnulltnull 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.

Excuse the considerable delay here, I have to admit this fell off my radar for a bit. Sorry! (feel free to re-request review if a PR is ready or it's been pending for a while)

Generally I like the updated approach much better from what I see. However, due to the huge code move and some commits that revert prior commits it's almost impossible to just review the parts of the code that actually changed (i.e., neither going commit-by-commit nor full-diff really works). Could you do me favor and clean up the commit history a bit? Also, I'm not conviced we gain much by the huge code move to common.rs/mod.rs if we don't actually split out REST and RPC types. Can we maybe revert that and keep everything in one file for now? Or, do the code move as an entirely separate commit in the beginning or end.

Thank you!

Comment threadbindings/ldk_node.udl Outdated
BackgroundSyncConfig? background_sync_config;
};

[Enum]

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.

nit: Seems this was first added here, then dropped in a later commit?

Comment threadsrc/chain/bitcoind/common.rs Outdated

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.

Can we please do the code moves in a prefactor commit? This would make it immediately clear that nothing changed when reviewing via git diff --color-moved.

And to be honest I'm not entirely sure why we move all that code at all, if we just end up with the implementation for both rpc and rest in mod.rs?

Comment threadsrc/chain/bitcoind/mod.rs Outdated

pub mod common;

pub enum BitcoindApiClient {

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.

Why not just BitcoindClient?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

This has been renamed.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Thanks for the feedback.

Excuse the considerable delay here, I have to admit this fell off my radar for a bit. Sorry! (feel free to re-request review if a PR is ready or it's been pending for a while)

Noted. I'll re-request going forward.

Generally I like the updated approach much better from what I see. However, due to the huge code move and some commits that revert prior commits it's almost impossible to just review the parts of the code that actually changed (i.e., neither going commit-by-commit nor full-diff really works). Could you do me favor and clean up the commit history a bit?

I'll clean up the commit history to make review much easier. Apologies.

Also, I'm not conviced we gain much by the huge code move to common.rs/mod.rs if we don't actually split out REST and RPC types. Can we maybe revert that and keep everything in one file for now? Or, do the code move as an entirely separate commit in the beginning or end.

You're right wrt common.rs. It is a leftover from when I separated REST and RPC types. Since we're not doing that split, I'll revert back to keeping everything in a single file to keep things simpler.

I'll have this cleaned up and ready shortly.

@tnulltnull 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.

This also needs a rebase now.

@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch 2 times, most recently from 9e88e4f to 4f34ec3CompareJune 11, 2025 16:45
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Hi @tnull

I've cleaned up the history and rebased. It is ready for another look.

@tnulltnull 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.

Excuse the delay here.

As far as I can tell the changes look mostly good, but the commit scheme still is very confusing to me and makes this very hard to review. Why are we first adding a _rest variant with a lot of copy/paste code, only to then merge them, whilst at the same time adding everything to a new file?

If you think this should all end up living in bitcoind.rs, please first make a commit moving the file to its new location (so that git understands it's the same file), and then make the changes in following commits. This would allow to see what actually was added and what changed in reference to the original code.

Comment threadsrc/builder.rs
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 4f34ec3 to c01b934CompareJune 20, 2025 14:55
@enigbe

enigbe commented Jun 20, 2025

Copy link
Copy Markdown
ContributorAuthor

Hi @tnull,

Why are we first adding a _rest variant with a lot of copy/paste code, only to then merge them, whilst at the same time adding everything to a new file?

I thought this might be helpful for other reviewers to see the justification in unifying the clients. Nonetheless, I've removed that commit so it should be as you'd prefer.

If you think this should all end up living in bitcoind.rs, please first make a commit moving the file to its new location (so that git understands it's the same file), and then make the changes in following commits. This would allow to see what actually was added and what changed in reference to the original code

I have added a prefactor commit to address the file change/rename.

Apologies for the confusion here. This should be much cleaner now.

@tnull
tnull self-requested a review June 23, 2025 12:13

@tnulltnull 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.

Thanks for the commit history cleanup, this is indeed now more reviewable. It still includes a bunch of unnecessary changes/refactorings that bloat the current changeset. It would be preferable not to include these (such as moving code blocks, switching up which types to import and which not, etc).

Moreover, I have to say I'm not super stoked about putting all the logic behind macros, as they are almost inaccessible to tooling (compiler checks, rustfmt, code completion, etc). Can we try to avoid them by prefactors that move the code to methods/closures or to helper methods that take whatever data these methods are operating on? If we do this, would be great to keep a sane git history.

All that said, functionally the code looks good to me.

Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from c01b934 to 925d214CompareJuly 1, 2025 07:24
enigbe added 2 commits July 1, 2025 08:43
Since we unify RPC and REST sync clients in the
following commit, we rename bitcoind_rpc to bitcoind
as this appropriately captures the unified client.
In the next commit, we'll introduce a unified bitcoind
client with two variant - RPC and REST. In preparation
to support chain syncing via the REST interface, we
refactor some methods into helper functions specific
to the interface client.
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 925d214 to ec53d60CompareJuly 1, 2025 07:44
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Hi @tnull,

This is ready for another look. As recommended, I have added:

  • prefector commit where the previously introduced macros are refactored into helper methods in preparation for REST support.
  • couple of fix-ups to address unwanted code move and type imports.

@tnulltnull 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.

Cool, thank you, much better without the macros!

Feel free to squash the fixups!

This commit:
- Adds support for syncing data via Bitcoin Core's
REST interface.
- Unifies the REST and RPC Bitcoin Core API clients
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from ec53d60 to 36e4c7fCompareJuly 1, 2025 10:18
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Squashed the fixups.

@tnull
tnull merged commit 0509e99 into lightningdevkit:mainJul 1, 2025
Sign up for freeto 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.

Bitcoind REST sync

5 participants

@enigbe@ldk-reviews-bot@tnull@Camillarhi@chuksys
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' feat: chain source rest sync by enigbe · Pull Request #526 · lightningdevkit/ldk-node · GitHub
Skip to content

feat: chain source rest sync - #526

Merged
tnull merged 3 commits into
lightningdevkit:mainfrom
enigbe:2025-04-bitcoind-rest-sync-v2
Jul 1, 2025
Merged

feat: chain source rest sync#526
tnull merged 3 commits into
lightningdevkit:mainfrom
enigbe:2025-04-bitcoind-rest-sync-v2

Conversation

@enigbe

@enigbeenigbe commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

What this PR does

This PR introduces chain source synchronization with Bitcoin Core's REST API. To do so, it explicitly distinguishes between the bitcoind_rpc_client and the newly introduced bitcoind_sync_client that can sync either via the RPC or REST APIs.

BitcoindRpcClient is maintained and the distinction created because the REST interface exposed by Bitcoin Core is not as robust as the RPC interface. This makes it impossible to send POST request to broadcast a transaction for example.

Related Issues

Fixes#496

cc @tnull@joostjager

@ldk-reviews-bot

ldk-reviews-bot commented Apr 18, 2025

Copy link
Copy Markdown

👋 Thanks for assigning @tnull as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

@enigbeenigbe changed the title feat: chain source rest sync with bitcoindfeat: chain source rest syncApr 18, 2025
@tnull
tnull requested review from tnull and removed request for joostjagerApril 18, 2025 10:47

@tnulltnull 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.

Thanks for looking into this! Before I get into more detailed review, I want to note two things on the chosen approach that stood out to me from a first look:

  1. I'm not quite convinced we should rename the BitcoindRpc client. Wouldn't it be cleaner to just add a (granted, rather duplicative) BitcoindRest variant side-by-side, and then in a second step maybe see how we can DRY up the code. Mushing both variants together seems to result in a rather confusing API and code structure.
  2. I'm a bit confused why we'd now still have RPC credentials on set_chain_source_bitcoind (and elsewhere in downstream from that), even if we want to use the REST backend? Is there any operation that still requires RPC access? Can't we just rely on REST alone?

@enigbe

enigbe commented Apr 19, 2025

Copy link
Copy Markdown
ContributorAuthor

Thanks for the early feedback.

  1. I'm not quite convinced we should rename the BitcoindRpc client. Wouldn't it be cleaner to just add a (granted, rather duplicative) BitcoindRest variant side-by-side, and then in a second step maybe see how we can DRY up the code. Mushing both variants together seems to result in a rather confusing API and code structure.
  2. I'm a bit confused why we'd now still have RPC credentials on set_chain_source_bitcoind (and elsewhere in downstream from that), even if we want to use the REST backend? Is there any operation that still requires RPC access? Can't we just rely on REST alone?

I understand your concerns about the name and API changes I have introduced but here is a bit of context.

Adding a ChainSource::BitcondRest variant was my initial approach too because I had erroneously thought Bitcoin Core's REST interface exposed (more or less) the same APIs as the RPC interface. However, the REST interface exposes a handful of APIs only for GET requests. It is not possible to broadcast transactions with a pure REST variant as we do with the BitcoindRpcClient, There is no REST API to sendrawtransaction.
For that particular use case, given that we'd need a handle on an RPC client regardless, I thought it best to:

  • Keep the BitcoindRpcClient as is.
  • Introduce a configurable REST/RPC sync client. With this, users can decide if syncing should happen either via the REST or RPC APIs.
  • Rename ChainSource::BitcoindRpc to ChainSource::Bitcoind as the latter leaves no ambiguity about the possibility of a REST variant.

@tnull

Copy link
Copy Markdown
Collaborator

I understand your concerns about the name and API changes I have introduced but here is a bit of context.

Adding a ChainSource::BitcondRest variant was my initial approach too because I had erroneously thought Bitcoin Core's REST interface exposed (more or less) the same APIs as the RPC interface. However, the REST interface exposes a handful of APIs only for GET requests. It is not possible to broadcast transactions with a pure REST variant as we do with the BitcoindRpcClient, There is no REST API to sendrawtransaction. For that particular use case, given that we'd need a handle on an RPC client regardless, I thought it best to:

* Keep the `BitcoindRpcClient` as is.
* Introduce a configurable REST/RPC sync client. With this, users can decide if syncing should happen either via the REST or RPC APIs.
* Rename `ChainSource::BitcoindRpc` to `ChainSource::Bitcoind` as the latter leaves no ambiguity about the possibility of a REST variant.

Aaah, okay, then the proposed approach makes more sense. I think it's still a bit awkward to have the mandatory RPC credentials on the set_chain_source_bitcoind method and addtionally have the BitcoindSyncConfig::Rpc variant.

While it then makes sense to reuse the client internally, it might be simpler to just keep the original set_chain_source_bitcoind_rpc method and just add a set_chain_source_bitcoind_rest variant, that also features the mandatory RPC credentials?

@Camillarhi

Copy link
Copy Markdown
Contributor

Hi @enigbe,

I noticed your CI is failing. I had a similar issue, but it was resolved after rebasing onto the latest dev branch (which includes changes from #524).

Could you try rebasing and see if that fixes the problem?

Thanks

@chuksyschuksys left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looking into this PR has helped me gain a good understanding of how chain sourcing works. I think introducing BitcoindSyncClient is a good idea as the user can still carry out syncing operations using the REST API while the RPC Client is still maintained to carry out operations not supported by the REST API. I have just a few suggestions with regards to error handling.

Comment threadsrc/builder.rs Outdated
Comment threadsrc/builder.rs Outdated
Comment threadsrc/builder.rs Outdated
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 9b07570 to a28d28fCompareMay 7, 2025 08:28
@tnull

Copy link
Copy Markdown
Collaborator

@enigbe Please let me know when this is ready for a second look.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Thanks for the feedback @tnull. I revisited this last week and addressed concerns raised here.

Aaah, okay, then the proposed approach makes more sense. I think it's still a bit awkward to have the mandatory RPC credentials on the set_chain_source_bitcoind method and addtionally have the BitcoindSyncConfig::Rpc variant.

This has been removed and we just keep a config for REST sync.

While it then makes sense to reuse the client internally, it might be simpler to just keep the original set_chain_source_bitcoind_rpc method and just add a set_chain_source_bitcoind_rest variant, that also features the mandatory RPC credentials?

Agreed! This seems like a cleaner split. I've added a set_chain_source_bitcoind_rest variant accordingly. However, I think it'd be potentially confusing/awkward for callers to still pass RPC parameters to the REST function. I opted for set_chain_source_bitcoind_rest to take only REST parameters and clarified comments on usage and dependency on the RPC variant.

If you'd still prefer we pass RPC parameters as a required input for the REST variant, happy to adjust.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Hi @enigbe,

I noticed your CI is failing. I had a similar issue, but it was resolved after rebasing onto the latest dev branch (which includes changes from #524).

Could you try rebasing and see if that fixes the problem?

Thanks

Thanks for looking at this. I've rebased and have just the CLN integration tests failing. I tried reproducing the failing test locally but couldn't. I should give it another go today.

@tnull

Copy link
Copy Markdown
Collaborator

Thanks for looking at this. I've rebased and have just the CLN integration tests failing. I tried reproducing the failing test locally but couldn't. I should give it another go today.

Yes, I think this is unrelated, so feel free to ignore the flaky CI in this PR. Fixing it is really an unrelated task.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Looking into this PR has helped me gain a good understanding of how chain sourcing works. I think introducing BitcoindSyncClient is a good idea as the user can still carry out syncing operations using the REST API while the RPC Client is still maintained to carry out operations not supported by the REST API. I have just a few suggestions with regards to error handling.

Thanks for the review. Added the suggestions recommended for error handling.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

@enigbe Please let me know when this is ready for a second look.

This is ready @tnull.

@tnulltnull 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.

Hmm, tbh. I still dislike the current refactorings as they make the codepaths unnecessarily confusing and duplicative. Why keep a BitcoindRpcClient around side-by-side with the new BitcoindSyncClient that could also be an RPC client? If you maintain that merging in the REST codepaths with the RPC client is the way to go, why not extend the enum BitcoindSyncClient variants so that one would only hold the RPC client and the other the RPC and REST clients, which would allow us to add an fn rpc_client accessor that we can use everywhere, i.e., hence would allow us to drop the bitcoind_rpc_client field?

Comment threadsrc/builder.rs
Comment threadsrc/builder.rs Outdated
rpc_port: u16,
rpc_user: String,
rpc_password: String,
sync_client_config: Option<BitcoindRestSyncClientConfig>,

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.

Mhh, let's name this rest_client_config then.

Comment threadsrc/chain/mod.rs Outdated
}
}

pub(crate) enum BitcoindSyncClient {

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.

I'm not sure why this was added here vs bitcoind_rpc.rs.

@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch 3 times, most recently from 953bc1e to 1196080CompareMay 26, 2025 10:00
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Please excuse the delay on this. I have addressed concerns from the last review. Investigating a flaky test but it's ready for another look.

Let me know what you think when you can review.

@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 1196080 to d56e7a7CompareMay 26, 2025 12:19

@tnulltnull 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.

Excuse the considerable delay here, I have to admit this fell off my radar for a bit. Sorry! (feel free to re-request review if a PR is ready or it's been pending for a while)

Generally I like the updated approach much better from what I see. However, due to the huge code move and some commits that revert prior commits it's almost impossible to just review the parts of the code that actually changed (i.e., neither going commit-by-commit nor full-diff really works). Could you do me favor and clean up the commit history a bit? Also, I'm not conviced we gain much by the huge code move to common.rs/mod.rs if we don't actually split out REST and RPC types. Can we maybe revert that and keep everything in one file for now? Or, do the code move as an entirely separate commit in the beginning or end.

Thank you!

Comment threadbindings/ldk_node.udl Outdated
BackgroundSyncConfig? background_sync_config;
};

[Enum]

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.

nit: Seems this was first added here, then dropped in a later commit?

Comment threadsrc/chain/bitcoind/common.rs Outdated

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.

Can we please do the code moves in a prefactor commit? This would make it immediately clear that nothing changed when reviewing via git diff --color-moved.

And to be honest I'm not entirely sure why we move all that code at all, if we just end up with the implementation for both rpc and rest in mod.rs?

Comment threadsrc/chain/bitcoind/mod.rs Outdated

pub mod common;

pub enum BitcoindApiClient {

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.

Why not just BitcoindClient?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

This has been renamed.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Thanks for the feedback.

Excuse the considerable delay here, I have to admit this fell off my radar for a bit. Sorry! (feel free to re-request review if a PR is ready or it's been pending for a while)

Noted. I'll re-request going forward.

Generally I like the updated approach much better from what I see. However, due to the huge code move and some commits that revert prior commits it's almost impossible to just review the parts of the code that actually changed (i.e., neither going commit-by-commit nor full-diff really works). Could you do me favor and clean up the commit history a bit?

I'll clean up the commit history to make review much easier. Apologies.

Also, I'm not conviced we gain much by the huge code move to common.rs/mod.rs if we don't actually split out REST and RPC types. Can we maybe revert that and keep everything in one file for now? Or, do the code move as an entirely separate commit in the beginning or end.

You're right wrt common.rs. It is a leftover from when I separated REST and RPC types. Since we're not doing that split, I'll revert back to keeping everything in a single file to keep things simpler.

I'll have this cleaned up and ready shortly.

@tnulltnull 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.

This also needs a rebase now.

@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch 2 times, most recently from 9e88e4f to 4f34ec3CompareJune 11, 2025 16:45
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Hi @tnull

I've cleaned up the history and rebased. It is ready for another look.

@tnulltnull 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.

Excuse the delay here.

As far as I can tell the changes look mostly good, but the commit scheme still is very confusing to me and makes this very hard to review. Why are we first adding a _rest variant with a lot of copy/paste code, only to then merge them, whilst at the same time adding everything to a new file?

If you think this should all end up living in bitcoind.rs, please first make a commit moving the file to its new location (so that git understands it's the same file), and then make the changes in following commits. This would allow to see what actually was added and what changed in reference to the original code.

Comment threadsrc/builder.rs
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 4f34ec3 to c01b934CompareJune 20, 2025 14:55
@enigbe

enigbe commented Jun 20, 2025

Copy link
Copy Markdown
ContributorAuthor

Hi @tnull,

Why are we first adding a _rest variant with a lot of copy/paste code, only to then merge them, whilst at the same time adding everything to a new file?

I thought this might be helpful for other reviewers to see the justification in unifying the clients. Nonetheless, I've removed that commit so it should be as you'd prefer.

If you think this should all end up living in bitcoind.rs, please first make a commit moving the file to its new location (so that git understands it's the same file), and then make the changes in following commits. This would allow to see what actually was added and what changed in reference to the original code

I have added a prefactor commit to address the file change/rename.

Apologies for the confusion here. This should be much cleaner now.

@tnull
tnull self-requested a review June 23, 2025 12:13

@tnulltnull 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.

Thanks for the commit history cleanup, this is indeed now more reviewable. It still includes a bunch of unnecessary changes/refactorings that bloat the current changeset. It would be preferable not to include these (such as moving code blocks, switching up which types to import and which not, etc).

Moreover, I have to say I'm not super stoked about putting all the logic behind macros, as they are almost inaccessible to tooling (compiler checks, rustfmt, code completion, etc). Can we try to avoid them by prefactors that move the code to methods/closures or to helper methods that take whatever data these methods are operating on? If we do this, would be great to keep a sane git history.

All that said, functionally the code looks good to me.

Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from c01b934 to 925d214CompareJuly 1, 2025 07:24
enigbe added 2 commits July 1, 2025 08:43
Since we unify RPC and REST sync clients in the
following commit, we rename bitcoind_rpc to bitcoind
as this appropriately captures the unified client.
In the next commit, we'll introduce a unified bitcoind
client with two variant - RPC and REST. In preparation
to support chain syncing via the REST interface, we
refactor some methods into helper functions specific
to the interface client.
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 925d214 to ec53d60CompareJuly 1, 2025 07:44
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Hi @tnull,

This is ready for another look. As recommended, I have added:

  • prefector commit where the previously introduced macros are refactored into helper methods in preparation for REST support.
  • couple of fix-ups to address unwanted code move and type imports.

@tnulltnull 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.

Cool, thank you, much better without the macros!

Feel free to squash the fixups!

This commit:
- Adds support for syncing data via Bitcoin Core's
REST interface.
- Unifies the REST and RPC Bitcoin Core API clients
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from ec53d60 to 36e4c7fCompareJuly 1, 2025 10:18
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Squashed the fixups.

@tnull
tnull merged commit 0509e99 into lightningdevkit:mainJul 1, 2025
Sign up for freeto 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.

Bitcoind REST sync

5 participants

@enigbe@ldk-reviews-bot@tnull@Camillarhi@chuksys
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' feat: chain source rest sync by enigbe · Pull Request #526 · lightningdevkit/ldk-node · GitHub
Skip to content

feat: chain source rest sync - #526

Merged
tnull merged 3 commits into
lightningdevkit:mainfrom
enigbe:2025-04-bitcoind-rest-sync-v2
Jul 1, 2025
Merged

feat: chain source rest sync#526
tnull merged 3 commits into
lightningdevkit:mainfrom
enigbe:2025-04-bitcoind-rest-sync-v2

Conversation

@enigbe

@enigbeenigbe commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

What this PR does

This PR introduces chain source synchronization with Bitcoin Core's REST API. To do so, it explicitly distinguishes between the bitcoind_rpc_client and the newly introduced bitcoind_sync_client that can sync either via the RPC or REST APIs.

BitcoindRpcClient is maintained and the distinction created because the REST interface exposed by Bitcoin Core is not as robust as the RPC interface. This makes it impossible to send POST request to broadcast a transaction for example.

Related Issues

Fixes#496

cc @tnull@joostjager

@ldk-reviews-bot

ldk-reviews-bot commented Apr 18, 2025

Copy link
Copy Markdown

👋 Thanks for assigning @tnull as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

@enigbeenigbe changed the title feat: chain source rest sync with bitcoindfeat: chain source rest syncApr 18, 2025
@tnull
tnull requested review from tnull and removed request for joostjagerApril 18, 2025 10:47

@tnulltnull 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.

Thanks for looking into this! Before I get into more detailed review, I want to note two things on the chosen approach that stood out to me from a first look:

  1. I'm not quite convinced we should rename the BitcoindRpc client. Wouldn't it be cleaner to just add a (granted, rather duplicative) BitcoindRest variant side-by-side, and then in a second step maybe see how we can DRY up the code. Mushing both variants together seems to result in a rather confusing API and code structure.
  2. I'm a bit confused why we'd now still have RPC credentials on set_chain_source_bitcoind (and elsewhere in downstream from that), even if we want to use the REST backend? Is there any operation that still requires RPC access? Can't we just rely on REST alone?

@enigbe

enigbe commented Apr 19, 2025

Copy link
Copy Markdown
ContributorAuthor

Thanks for the early feedback.

  1. I'm not quite convinced we should rename the BitcoindRpc client. Wouldn't it be cleaner to just add a (granted, rather duplicative) BitcoindRest variant side-by-side, and then in a second step maybe see how we can DRY up the code. Mushing both variants together seems to result in a rather confusing API and code structure.
  2. I'm a bit confused why we'd now still have RPC credentials on set_chain_source_bitcoind (and elsewhere in downstream from that), even if we want to use the REST backend? Is there any operation that still requires RPC access? Can't we just rely on REST alone?

I understand your concerns about the name and API changes I have introduced but here is a bit of context.

Adding a ChainSource::BitcondRest variant was my initial approach too because I had erroneously thought Bitcoin Core's REST interface exposed (more or less) the same APIs as the RPC interface. However, the REST interface exposes a handful of APIs only for GET requests. It is not possible to broadcast transactions with a pure REST variant as we do with the BitcoindRpcClient, There is no REST API to sendrawtransaction.
For that particular use case, given that we'd need a handle on an RPC client regardless, I thought it best to:

  • Keep the BitcoindRpcClient as is.
  • Introduce a configurable REST/RPC sync client. With this, users can decide if syncing should happen either via the REST or RPC APIs.
  • Rename ChainSource::BitcoindRpc to ChainSource::Bitcoind as the latter leaves no ambiguity about the possibility of a REST variant.

@tnull

Copy link
Copy Markdown
Collaborator

I understand your concerns about the name and API changes I have introduced but here is a bit of context.

Adding a ChainSource::BitcondRest variant was my initial approach too because I had erroneously thought Bitcoin Core's REST interface exposed (more or less) the same APIs as the RPC interface. However, the REST interface exposes a handful of APIs only for GET requests. It is not possible to broadcast transactions with a pure REST variant as we do with the BitcoindRpcClient, There is no REST API to sendrawtransaction. For that particular use case, given that we'd need a handle on an RPC client regardless, I thought it best to:

* Keep the `BitcoindRpcClient` as is.
* Introduce a configurable REST/RPC sync client. With this, users can decide if syncing should happen either via the REST or RPC APIs.
* Rename `ChainSource::BitcoindRpc` to `ChainSource::Bitcoind` as the latter leaves no ambiguity about the possibility of a REST variant.

Aaah, okay, then the proposed approach makes more sense. I think it's still a bit awkward to have the mandatory RPC credentials on the set_chain_source_bitcoind method and addtionally have the BitcoindSyncConfig::Rpc variant.

While it then makes sense to reuse the client internally, it might be simpler to just keep the original set_chain_source_bitcoind_rpc method and just add a set_chain_source_bitcoind_rest variant, that also features the mandatory RPC credentials?

@Camillarhi

Copy link
Copy Markdown
Contributor

Hi @enigbe,

I noticed your CI is failing. I had a similar issue, but it was resolved after rebasing onto the latest dev branch (which includes changes from #524).

Could you try rebasing and see if that fixes the problem?

Thanks

@chuksyschuksys left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looking into this PR has helped me gain a good understanding of how chain sourcing works. I think introducing BitcoindSyncClient is a good idea as the user can still carry out syncing operations using the REST API while the RPC Client is still maintained to carry out operations not supported by the REST API. I have just a few suggestions with regards to error handling.

Comment threadsrc/builder.rs Outdated
Comment threadsrc/builder.rs Outdated
Comment threadsrc/builder.rs Outdated
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 9b07570 to a28d28fCompareMay 7, 2025 08:28
@tnull

Copy link
Copy Markdown
Collaborator

@enigbe Please let me know when this is ready for a second look.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Thanks for the feedback @tnull. I revisited this last week and addressed concerns raised here.

Aaah, okay, then the proposed approach makes more sense. I think it's still a bit awkward to have the mandatory RPC credentials on the set_chain_source_bitcoind method and addtionally have the BitcoindSyncConfig::Rpc variant.

This has been removed and we just keep a config for REST sync.

While it then makes sense to reuse the client internally, it might be simpler to just keep the original set_chain_source_bitcoind_rpc method and just add a set_chain_source_bitcoind_rest variant, that also features the mandatory RPC credentials?

Agreed! This seems like a cleaner split. I've added a set_chain_source_bitcoind_rest variant accordingly. However, I think it'd be potentially confusing/awkward for callers to still pass RPC parameters to the REST function. I opted for set_chain_source_bitcoind_rest to take only REST parameters and clarified comments on usage and dependency on the RPC variant.

If you'd still prefer we pass RPC parameters as a required input for the REST variant, happy to adjust.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Hi @enigbe,

I noticed your CI is failing. I had a similar issue, but it was resolved after rebasing onto the latest dev branch (which includes changes from #524).

Could you try rebasing and see if that fixes the problem?

Thanks

Thanks for looking at this. I've rebased and have just the CLN integration tests failing. I tried reproducing the failing test locally but couldn't. I should give it another go today.

@tnull

Copy link
Copy Markdown
Collaborator

Thanks for looking at this. I've rebased and have just the CLN integration tests failing. I tried reproducing the failing test locally but couldn't. I should give it another go today.

Yes, I think this is unrelated, so feel free to ignore the flaky CI in this PR. Fixing it is really an unrelated task.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Looking into this PR has helped me gain a good understanding of how chain sourcing works. I think introducing BitcoindSyncClient is a good idea as the user can still carry out syncing operations using the REST API while the RPC Client is still maintained to carry out operations not supported by the REST API. I have just a few suggestions with regards to error handling.

Thanks for the review. Added the suggestions recommended for error handling.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

@enigbe Please let me know when this is ready for a second look.

This is ready @tnull.

@tnulltnull 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.

Hmm, tbh. I still dislike the current refactorings as they make the codepaths unnecessarily confusing and duplicative. Why keep a BitcoindRpcClient around side-by-side with the new BitcoindSyncClient that could also be an RPC client? If you maintain that merging in the REST codepaths with the RPC client is the way to go, why not extend the enum BitcoindSyncClient variants so that one would only hold the RPC client and the other the RPC and REST clients, which would allow us to add an fn rpc_client accessor that we can use everywhere, i.e., hence would allow us to drop the bitcoind_rpc_client field?

Comment threadsrc/builder.rs
Comment threadsrc/builder.rs Outdated
rpc_port: u16,
rpc_user: String,
rpc_password: String,
sync_client_config: Option<BitcoindRestSyncClientConfig>,

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.

Mhh, let's name this rest_client_config then.

Comment threadsrc/chain/mod.rs Outdated
}
}

pub(crate) enum BitcoindSyncClient {

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.

I'm not sure why this was added here vs bitcoind_rpc.rs.

@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch 3 times, most recently from 953bc1e to 1196080CompareMay 26, 2025 10:00
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Please excuse the delay on this. I have addressed concerns from the last review. Investigating a flaky test but it's ready for another look.

Let me know what you think when you can review.

@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 1196080 to d56e7a7CompareMay 26, 2025 12:19

@tnulltnull 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.

Excuse the considerable delay here, I have to admit this fell off my radar for a bit. Sorry! (feel free to re-request review if a PR is ready or it's been pending for a while)

Generally I like the updated approach much better from what I see. However, due to the huge code move and some commits that revert prior commits it's almost impossible to just review the parts of the code that actually changed (i.e., neither going commit-by-commit nor full-diff really works). Could you do me favor and clean up the commit history a bit? Also, I'm not conviced we gain much by the huge code move to common.rs/mod.rs if we don't actually split out REST and RPC types. Can we maybe revert that and keep everything in one file for now? Or, do the code move as an entirely separate commit in the beginning or end.

Thank you!

Comment threadbindings/ldk_node.udl Outdated
BackgroundSyncConfig? background_sync_config;
};

[Enum]

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.

nit: Seems this was first added here, then dropped in a later commit?

Comment threadsrc/chain/bitcoind/common.rs Outdated

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.

Can we please do the code moves in a prefactor commit? This would make it immediately clear that nothing changed when reviewing via git diff --color-moved.

And to be honest I'm not entirely sure why we move all that code at all, if we just end up with the implementation for both rpc and rest in mod.rs?

Comment threadsrc/chain/bitcoind/mod.rs Outdated

pub mod common;

pub enum BitcoindApiClient {

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.

Why not just BitcoindClient?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

This has been renamed.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Thanks for the feedback.

Excuse the considerable delay here, I have to admit this fell off my radar for a bit. Sorry! (feel free to re-request review if a PR is ready or it's been pending for a while)

Noted. I'll re-request going forward.

Generally I like the updated approach much better from what I see. However, due to the huge code move and some commits that revert prior commits it's almost impossible to just review the parts of the code that actually changed (i.e., neither going commit-by-commit nor full-diff really works). Could you do me favor and clean up the commit history a bit?

I'll clean up the commit history to make review much easier. Apologies.

Also, I'm not conviced we gain much by the huge code move to common.rs/mod.rs if we don't actually split out REST and RPC types. Can we maybe revert that and keep everything in one file for now? Or, do the code move as an entirely separate commit in the beginning or end.

You're right wrt common.rs. It is a leftover from when I separated REST and RPC types. Since we're not doing that split, I'll revert back to keeping everything in a single file to keep things simpler.

I'll have this cleaned up and ready shortly.

@tnulltnull 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.

This also needs a rebase now.

@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch 2 times, most recently from 9e88e4f to 4f34ec3CompareJune 11, 2025 16:45
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Hi @tnull

I've cleaned up the history and rebased. It is ready for another look.

@tnulltnull 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.

Excuse the delay here.

As far as I can tell the changes look mostly good, but the commit scheme still is very confusing to me and makes this very hard to review. Why are we first adding a _rest variant with a lot of copy/paste code, only to then merge them, whilst at the same time adding everything to a new file?

If you think this should all end up living in bitcoind.rs, please first make a commit moving the file to its new location (so that git understands it's the same file), and then make the changes in following commits. This would allow to see what actually was added and what changed in reference to the original code.

Comment threadsrc/builder.rs
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 4f34ec3 to c01b934CompareJune 20, 2025 14:55
@enigbe

enigbe commented Jun 20, 2025

Copy link
Copy Markdown
ContributorAuthor

Hi @tnull,

Why are we first adding a _rest variant with a lot of copy/paste code, only to then merge them, whilst at the same time adding everything to a new file?

I thought this might be helpful for other reviewers to see the justification in unifying the clients. Nonetheless, I've removed that commit so it should be as you'd prefer.

If you think this should all end up living in bitcoind.rs, please first make a commit moving the file to its new location (so that git understands it's the same file), and then make the changes in following commits. This would allow to see what actually was added and what changed in reference to the original code

I have added a prefactor commit to address the file change/rename.

Apologies for the confusion here. This should be much cleaner now.

@tnull
tnull self-requested a review June 23, 2025 12:13

@tnulltnull 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.

Thanks for the commit history cleanup, this is indeed now more reviewable. It still includes a bunch of unnecessary changes/refactorings that bloat the current changeset. It would be preferable not to include these (such as moving code blocks, switching up which types to import and which not, etc).

Moreover, I have to say I'm not super stoked about putting all the logic behind macros, as they are almost inaccessible to tooling (compiler checks, rustfmt, code completion, etc). Can we try to avoid them by prefactors that move the code to methods/closures or to helper methods that take whatever data these methods are operating on? If we do this, would be great to keep a sane git history.

All that said, functionally the code looks good to me.

Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from c01b934 to 925d214CompareJuly 1, 2025 07:24
enigbe added 2 commits July 1, 2025 08:43
Since we unify RPC and REST sync clients in the
following commit, we rename bitcoind_rpc to bitcoind
as this appropriately captures the unified client.
In the next commit, we'll introduce a unified bitcoind
client with two variant - RPC and REST. In preparation
to support chain syncing via the REST interface, we
refactor some methods into helper functions specific
to the interface client.
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 925d214 to ec53d60CompareJuly 1, 2025 07:44
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Hi @tnull,

This is ready for another look. As recommended, I have added:

  • prefector commit where the previously introduced macros are refactored into helper methods in preparation for REST support.
  • couple of fix-ups to address unwanted code move and type imports.

@tnulltnull 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.

Cool, thank you, much better without the macros!

Feel free to squash the fixups!

This commit:
- Adds support for syncing data via Bitcoin Core's
REST interface.
- Unifies the REST and RPC Bitcoin Core API clients
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from ec53d60 to 36e4c7fCompareJuly 1, 2025 10:18
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Squashed the fixups.

@tnull
tnull merged commit 0509e99 into lightningdevkit:mainJul 1, 2025
Sign up for freeto 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.

Bitcoind REST sync

5 participants

@enigbe@ldk-reviews-bot@tnull@Camillarhi@chuksys
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' feat: chain source rest sync by enigbe · Pull Request #526 · lightningdevkit/ldk-node · GitHub
Skip to content

feat: chain source rest sync - #526

Merged
tnull merged 3 commits into
lightningdevkit:mainfrom
enigbe:2025-04-bitcoind-rest-sync-v2
Jul 1, 2025
Merged

feat: chain source rest sync#526
tnull merged 3 commits into
lightningdevkit:mainfrom
enigbe:2025-04-bitcoind-rest-sync-v2

Conversation

@enigbe

@enigbeenigbe commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

What this PR does

This PR introduces chain source synchronization with Bitcoin Core's REST API. To do so, it explicitly distinguishes between the bitcoind_rpc_client and the newly introduced bitcoind_sync_client that can sync either via the RPC or REST APIs.

BitcoindRpcClient is maintained and the distinction created because the REST interface exposed by Bitcoin Core is not as robust as the RPC interface. This makes it impossible to send POST request to broadcast a transaction for example.

Related Issues

Fixes#496

cc @tnull@joostjager

@ldk-reviews-bot

ldk-reviews-bot commented Apr 18, 2025

Copy link
Copy Markdown

👋 Thanks for assigning @tnull as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

@enigbeenigbe changed the title feat: chain source rest sync with bitcoindfeat: chain source rest syncApr 18, 2025
@tnull
tnull requested review from tnull and removed request for joostjagerApril 18, 2025 10:47

@tnulltnull 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.

Thanks for looking into this! Before I get into more detailed review, I want to note two things on the chosen approach that stood out to me from a first look:

  1. I'm not quite convinced we should rename the BitcoindRpc client. Wouldn't it be cleaner to just add a (granted, rather duplicative) BitcoindRest variant side-by-side, and then in a second step maybe see how we can DRY up the code. Mushing both variants together seems to result in a rather confusing API and code structure.
  2. I'm a bit confused why we'd now still have RPC credentials on set_chain_source_bitcoind (and elsewhere in downstream from that), even if we want to use the REST backend? Is there any operation that still requires RPC access? Can't we just rely on REST alone?

@enigbe

enigbe commented Apr 19, 2025

Copy link
Copy Markdown
ContributorAuthor

Thanks for the early feedback.

  1. I'm not quite convinced we should rename the BitcoindRpc client. Wouldn't it be cleaner to just add a (granted, rather duplicative) BitcoindRest variant side-by-side, and then in a second step maybe see how we can DRY up the code. Mushing both variants together seems to result in a rather confusing API and code structure.
  2. I'm a bit confused why we'd now still have RPC credentials on set_chain_source_bitcoind (and elsewhere in downstream from that), even if we want to use the REST backend? Is there any operation that still requires RPC access? Can't we just rely on REST alone?

I understand your concerns about the name and API changes I have introduced but here is a bit of context.

Adding a ChainSource::BitcondRest variant was my initial approach too because I had erroneously thought Bitcoin Core's REST interface exposed (more or less) the same APIs as the RPC interface. However, the REST interface exposes a handful of APIs only for GET requests. It is not possible to broadcast transactions with a pure REST variant as we do with the BitcoindRpcClient, There is no REST API to sendrawtransaction.
For that particular use case, given that we'd need a handle on an RPC client regardless, I thought it best to:

  • Keep the BitcoindRpcClient as is.
  • Introduce a configurable REST/RPC sync client. With this, users can decide if syncing should happen either via the REST or RPC APIs.
  • Rename ChainSource::BitcoindRpc to ChainSource::Bitcoind as the latter leaves no ambiguity about the possibility of a REST variant.

@tnull

Copy link
Copy Markdown
Collaborator

I understand your concerns about the name and API changes I have introduced but here is a bit of context.

Adding a ChainSource::BitcondRest variant was my initial approach too because I had erroneously thought Bitcoin Core's REST interface exposed (more or less) the same APIs as the RPC interface. However, the REST interface exposes a handful of APIs only for GET requests. It is not possible to broadcast transactions with a pure REST variant as we do with the BitcoindRpcClient, There is no REST API to sendrawtransaction. For that particular use case, given that we'd need a handle on an RPC client regardless, I thought it best to:

* Keep the `BitcoindRpcClient` as is.
* Introduce a configurable REST/RPC sync client. With this, users can decide if syncing should happen either via the REST or RPC APIs.
* Rename `ChainSource::BitcoindRpc` to `ChainSource::Bitcoind` as the latter leaves no ambiguity about the possibility of a REST variant.

Aaah, okay, then the proposed approach makes more sense. I think it's still a bit awkward to have the mandatory RPC credentials on the set_chain_source_bitcoind method and addtionally have the BitcoindSyncConfig::Rpc variant.

While it then makes sense to reuse the client internally, it might be simpler to just keep the original set_chain_source_bitcoind_rpc method and just add a set_chain_source_bitcoind_rest variant, that also features the mandatory RPC credentials?

@Camillarhi

Copy link
Copy Markdown
Contributor

Hi @enigbe,

I noticed your CI is failing. I had a similar issue, but it was resolved after rebasing onto the latest dev branch (which includes changes from #524).

Could you try rebasing and see if that fixes the problem?

Thanks

@chuksyschuksys left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looking into this PR has helped me gain a good understanding of how chain sourcing works. I think introducing BitcoindSyncClient is a good idea as the user can still carry out syncing operations using the REST API while the RPC Client is still maintained to carry out operations not supported by the REST API. I have just a few suggestions with regards to error handling.

Comment threadsrc/builder.rs Outdated
Comment threadsrc/builder.rs Outdated
Comment threadsrc/builder.rs Outdated
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 9b07570 to a28d28fCompareMay 7, 2025 08:28
@tnull

Copy link
Copy Markdown
Collaborator

@enigbe Please let me know when this is ready for a second look.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Thanks for the feedback @tnull. I revisited this last week and addressed concerns raised here.

Aaah, okay, then the proposed approach makes more sense. I think it's still a bit awkward to have the mandatory RPC credentials on the set_chain_source_bitcoind method and addtionally have the BitcoindSyncConfig::Rpc variant.

This has been removed and we just keep a config for REST sync.

While it then makes sense to reuse the client internally, it might be simpler to just keep the original set_chain_source_bitcoind_rpc method and just add a set_chain_source_bitcoind_rest variant, that also features the mandatory RPC credentials?

Agreed! This seems like a cleaner split. I've added a set_chain_source_bitcoind_rest variant accordingly. However, I think it'd be potentially confusing/awkward for callers to still pass RPC parameters to the REST function. I opted for set_chain_source_bitcoind_rest to take only REST parameters and clarified comments on usage and dependency on the RPC variant.

If you'd still prefer we pass RPC parameters as a required input for the REST variant, happy to adjust.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Hi @enigbe,

I noticed your CI is failing. I had a similar issue, but it was resolved after rebasing onto the latest dev branch (which includes changes from #524).

Could you try rebasing and see if that fixes the problem?

Thanks

Thanks for looking at this. I've rebased and have just the CLN integration tests failing. I tried reproducing the failing test locally but couldn't. I should give it another go today.

@tnull

Copy link
Copy Markdown
Collaborator

Thanks for looking at this. I've rebased and have just the CLN integration tests failing. I tried reproducing the failing test locally but couldn't. I should give it another go today.

Yes, I think this is unrelated, so feel free to ignore the flaky CI in this PR. Fixing it is really an unrelated task.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Looking into this PR has helped me gain a good understanding of how chain sourcing works. I think introducing BitcoindSyncClient is a good idea as the user can still carry out syncing operations using the REST API while the RPC Client is still maintained to carry out operations not supported by the REST API. I have just a few suggestions with regards to error handling.

Thanks for the review. Added the suggestions recommended for error handling.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

@enigbe Please let me know when this is ready for a second look.

This is ready @tnull.

@tnulltnull 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.

Hmm, tbh. I still dislike the current refactorings as they make the codepaths unnecessarily confusing and duplicative. Why keep a BitcoindRpcClient around side-by-side with the new BitcoindSyncClient that could also be an RPC client? If you maintain that merging in the REST codepaths with the RPC client is the way to go, why not extend the enum BitcoindSyncClient variants so that one would only hold the RPC client and the other the RPC and REST clients, which would allow us to add an fn rpc_client accessor that we can use everywhere, i.e., hence would allow us to drop the bitcoind_rpc_client field?

Comment threadsrc/builder.rs
Comment threadsrc/builder.rs Outdated
rpc_port: u16,
rpc_user: String,
rpc_password: String,
sync_client_config: Option<BitcoindRestSyncClientConfig>,

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.

Mhh, let's name this rest_client_config then.

Comment threadsrc/chain/mod.rs Outdated
}
}

pub(crate) enum BitcoindSyncClient {

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.

I'm not sure why this was added here vs bitcoind_rpc.rs.

@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch 3 times, most recently from 953bc1e to 1196080CompareMay 26, 2025 10:00
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Please excuse the delay on this. I have addressed concerns from the last review. Investigating a flaky test but it's ready for another look.

Let me know what you think when you can review.

@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 1196080 to d56e7a7CompareMay 26, 2025 12:19

@tnulltnull 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.

Excuse the considerable delay here, I have to admit this fell off my radar for a bit. Sorry! (feel free to re-request review if a PR is ready or it's been pending for a while)

Generally I like the updated approach much better from what I see. However, due to the huge code move and some commits that revert prior commits it's almost impossible to just review the parts of the code that actually changed (i.e., neither going commit-by-commit nor full-diff really works). Could you do me favor and clean up the commit history a bit? Also, I'm not conviced we gain much by the huge code move to common.rs/mod.rs if we don't actually split out REST and RPC types. Can we maybe revert that and keep everything in one file for now? Or, do the code move as an entirely separate commit in the beginning or end.

Thank you!

Comment threadbindings/ldk_node.udl Outdated
BackgroundSyncConfig? background_sync_config;
};

[Enum]

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.

nit: Seems this was first added here, then dropped in a later commit?

Comment threadsrc/chain/bitcoind/common.rs Outdated

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.

Can we please do the code moves in a prefactor commit? This would make it immediately clear that nothing changed when reviewing via git diff --color-moved.

And to be honest I'm not entirely sure why we move all that code at all, if we just end up with the implementation for both rpc and rest in mod.rs?

Comment threadsrc/chain/bitcoind/mod.rs Outdated

pub mod common;

pub enum BitcoindApiClient {

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.

Why not just BitcoindClient?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

This has been renamed.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Thanks for the feedback.

Excuse the considerable delay here, I have to admit this fell off my radar for a bit. Sorry! (feel free to re-request review if a PR is ready or it's been pending for a while)

Noted. I'll re-request going forward.

Generally I like the updated approach much better from what I see. However, due to the huge code move and some commits that revert prior commits it's almost impossible to just review the parts of the code that actually changed (i.e., neither going commit-by-commit nor full-diff really works). Could you do me favor and clean up the commit history a bit?

I'll clean up the commit history to make review much easier. Apologies.

Also, I'm not conviced we gain much by the huge code move to common.rs/mod.rs if we don't actually split out REST and RPC types. Can we maybe revert that and keep everything in one file for now? Or, do the code move as an entirely separate commit in the beginning or end.

You're right wrt common.rs. It is a leftover from when I separated REST and RPC types. Since we're not doing that split, I'll revert back to keeping everything in a single file to keep things simpler.

I'll have this cleaned up and ready shortly.

@tnulltnull 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.

This also needs a rebase now.

@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch 2 times, most recently from 9e88e4f to 4f34ec3CompareJune 11, 2025 16:45
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Hi @tnull

I've cleaned up the history and rebased. It is ready for another look.

@tnulltnull 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.

Excuse the delay here.

As far as I can tell the changes look mostly good, but the commit scheme still is very confusing to me and makes this very hard to review. Why are we first adding a _rest variant with a lot of copy/paste code, only to then merge them, whilst at the same time adding everything to a new file?

If you think this should all end up living in bitcoind.rs, please first make a commit moving the file to its new location (so that git understands it's the same file), and then make the changes in following commits. This would allow to see what actually was added and what changed in reference to the original code.

Comment threadsrc/builder.rs
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 4f34ec3 to c01b934CompareJune 20, 2025 14:55
@enigbe

enigbe commented Jun 20, 2025

Copy link
Copy Markdown
ContributorAuthor

Hi @tnull,

Why are we first adding a _rest variant with a lot of copy/paste code, only to then merge them, whilst at the same time adding everything to a new file?

I thought this might be helpful for other reviewers to see the justification in unifying the clients. Nonetheless, I've removed that commit so it should be as you'd prefer.

If you think this should all end up living in bitcoind.rs, please first make a commit moving the file to its new location (so that git understands it's the same file), and then make the changes in following commits. This would allow to see what actually was added and what changed in reference to the original code

I have added a prefactor commit to address the file change/rename.

Apologies for the confusion here. This should be much cleaner now.

@tnull
tnull self-requested a review June 23, 2025 12:13

@tnulltnull 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.

Thanks for the commit history cleanup, this is indeed now more reviewable. It still includes a bunch of unnecessary changes/refactorings that bloat the current changeset. It would be preferable not to include these (such as moving code blocks, switching up which types to import and which not, etc).

Moreover, I have to say I'm not super stoked about putting all the logic behind macros, as they are almost inaccessible to tooling (compiler checks, rustfmt, code completion, etc). Can we try to avoid them by prefactors that move the code to methods/closures or to helper methods that take whatever data these methods are operating on? If we do this, would be great to keep a sane git history.

All that said, functionally the code looks good to me.

Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from c01b934 to 925d214CompareJuly 1, 2025 07:24
enigbe added 2 commits July 1, 2025 08:43
Since we unify RPC and REST sync clients in the
following commit, we rename bitcoind_rpc to bitcoind
as this appropriately captures the unified client.
In the next commit, we'll introduce a unified bitcoind
client with two variant - RPC and REST. In preparation
to support chain syncing via the REST interface, we
refactor some methods into helper functions specific
to the interface client.
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 925d214 to ec53d60CompareJuly 1, 2025 07:44
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Hi @tnull,

This is ready for another look. As recommended, I have added:

  • prefector commit where the previously introduced macros are refactored into helper methods in preparation for REST support.
  • couple of fix-ups to address unwanted code move and type imports.

@tnulltnull 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.

Cool, thank you, much better without the macros!

Feel free to squash the fixups!

This commit:
- Adds support for syncing data via Bitcoin Core's
REST interface.
- Unifies the REST and RPC Bitcoin Core API clients
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from ec53d60 to 36e4c7fCompareJuly 1, 2025 10:18
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Squashed the fixups.

@tnull
tnull merged commit 0509e99 into lightningdevkit:mainJul 1, 2025
Sign up for freeto 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.

Bitcoind REST sync

5 participants

@enigbe@ldk-reviews-bot@tnull@Camillarhi@chuksys
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' feat: chain source rest sync by enigbe · Pull Request #526 · lightningdevkit/ldk-node · GitHub
Skip to content

feat: chain source rest sync - #526

Merged
tnull merged 3 commits into
lightningdevkit:mainfrom
enigbe:2025-04-bitcoind-rest-sync-v2
Jul 1, 2025
Merged

feat: chain source rest sync#526
tnull merged 3 commits into
lightningdevkit:mainfrom
enigbe:2025-04-bitcoind-rest-sync-v2

Conversation

@enigbe

@enigbeenigbe commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

What this PR does

This PR introduces chain source synchronization with Bitcoin Core's REST API. To do so, it explicitly distinguishes between the bitcoind_rpc_client and the newly introduced bitcoind_sync_client that can sync either via the RPC or REST APIs.

BitcoindRpcClient is maintained and the distinction created because the REST interface exposed by Bitcoin Core is not as robust as the RPC interface. This makes it impossible to send POST request to broadcast a transaction for example.

Related Issues

Fixes#496

cc @tnull@joostjager

@ldk-reviews-bot

ldk-reviews-bot commented Apr 18, 2025

Copy link
Copy Markdown

👋 Thanks for assigning @tnull as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

@enigbeenigbe changed the title feat: chain source rest sync with bitcoindfeat: chain source rest syncApr 18, 2025
@tnull
tnull requested review from tnull and removed request for joostjagerApril 18, 2025 10:47

@tnulltnull 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.

Thanks for looking into this! Before I get into more detailed review, I want to note two things on the chosen approach that stood out to me from a first look:

  1. I'm not quite convinced we should rename the BitcoindRpc client. Wouldn't it be cleaner to just add a (granted, rather duplicative) BitcoindRest variant side-by-side, and then in a second step maybe see how we can DRY up the code. Mushing both variants together seems to result in a rather confusing API and code structure.
  2. I'm a bit confused why we'd now still have RPC credentials on set_chain_source_bitcoind (and elsewhere in downstream from that), even if we want to use the REST backend? Is there any operation that still requires RPC access? Can't we just rely on REST alone?

@enigbe

enigbe commented Apr 19, 2025

Copy link
Copy Markdown
ContributorAuthor

Thanks for the early feedback.

  1. I'm not quite convinced we should rename the BitcoindRpc client. Wouldn't it be cleaner to just add a (granted, rather duplicative) BitcoindRest variant side-by-side, and then in a second step maybe see how we can DRY up the code. Mushing both variants together seems to result in a rather confusing API and code structure.
  2. I'm a bit confused why we'd now still have RPC credentials on set_chain_source_bitcoind (and elsewhere in downstream from that), even if we want to use the REST backend? Is there any operation that still requires RPC access? Can't we just rely on REST alone?

I understand your concerns about the name and API changes I have introduced but here is a bit of context.

Adding a ChainSource::BitcondRest variant was my initial approach too because I had erroneously thought Bitcoin Core's REST interface exposed (more or less) the same APIs as the RPC interface. However, the REST interface exposes a handful of APIs only for GET requests. It is not possible to broadcast transactions with a pure REST variant as we do with the BitcoindRpcClient, There is no REST API to sendrawtransaction.
For that particular use case, given that we'd need a handle on an RPC client regardless, I thought it best to:

  • Keep the BitcoindRpcClient as is.
  • Introduce a configurable REST/RPC sync client. With this, users can decide if syncing should happen either via the REST or RPC APIs.
  • Rename ChainSource::BitcoindRpc to ChainSource::Bitcoind as the latter leaves no ambiguity about the possibility of a REST variant.

@tnull

Copy link
Copy Markdown
Collaborator

I understand your concerns about the name and API changes I have introduced but here is a bit of context.

Adding a ChainSource::BitcondRest variant was my initial approach too because I had erroneously thought Bitcoin Core's REST interface exposed (more or less) the same APIs as the RPC interface. However, the REST interface exposes a handful of APIs only for GET requests. It is not possible to broadcast transactions with a pure REST variant as we do with the BitcoindRpcClient, There is no REST API to sendrawtransaction. For that particular use case, given that we'd need a handle on an RPC client regardless, I thought it best to:

* Keep the `BitcoindRpcClient` as is.
* Introduce a configurable REST/RPC sync client. With this, users can decide if syncing should happen either via the REST or RPC APIs.
* Rename `ChainSource::BitcoindRpc` to `ChainSource::Bitcoind` as the latter leaves no ambiguity about the possibility of a REST variant.

Aaah, okay, then the proposed approach makes more sense. I think it's still a bit awkward to have the mandatory RPC credentials on the set_chain_source_bitcoind method and addtionally have the BitcoindSyncConfig::Rpc variant.

While it then makes sense to reuse the client internally, it might be simpler to just keep the original set_chain_source_bitcoind_rpc method and just add a set_chain_source_bitcoind_rest variant, that also features the mandatory RPC credentials?

@Camillarhi

Copy link
Copy Markdown
Contributor

Hi @enigbe,

I noticed your CI is failing. I had a similar issue, but it was resolved after rebasing onto the latest dev branch (which includes changes from #524).

Could you try rebasing and see if that fixes the problem?

Thanks

@chuksyschuksys left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looking into this PR has helped me gain a good understanding of how chain sourcing works. I think introducing BitcoindSyncClient is a good idea as the user can still carry out syncing operations using the REST API while the RPC Client is still maintained to carry out operations not supported by the REST API. I have just a few suggestions with regards to error handling.

Comment threadsrc/builder.rs Outdated
Comment threadsrc/builder.rs Outdated
Comment threadsrc/builder.rs Outdated
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 9b07570 to a28d28fCompareMay 7, 2025 08:28
@tnull

Copy link
Copy Markdown
Collaborator

@enigbe Please let me know when this is ready for a second look.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Thanks for the feedback @tnull. I revisited this last week and addressed concerns raised here.

Aaah, okay, then the proposed approach makes more sense. I think it's still a bit awkward to have the mandatory RPC credentials on the set_chain_source_bitcoind method and addtionally have the BitcoindSyncConfig::Rpc variant.

This has been removed and we just keep a config for REST sync.

While it then makes sense to reuse the client internally, it might be simpler to just keep the original set_chain_source_bitcoind_rpc method and just add a set_chain_source_bitcoind_rest variant, that also features the mandatory RPC credentials?

Agreed! This seems like a cleaner split. I've added a set_chain_source_bitcoind_rest variant accordingly. However, I think it'd be potentially confusing/awkward for callers to still pass RPC parameters to the REST function. I opted for set_chain_source_bitcoind_rest to take only REST parameters and clarified comments on usage and dependency on the RPC variant.

If you'd still prefer we pass RPC parameters as a required input for the REST variant, happy to adjust.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Hi @enigbe,

I noticed your CI is failing. I had a similar issue, but it was resolved after rebasing onto the latest dev branch (which includes changes from #524).

Could you try rebasing and see if that fixes the problem?

Thanks

Thanks for looking at this. I've rebased and have just the CLN integration tests failing. I tried reproducing the failing test locally but couldn't. I should give it another go today.

@tnull

Copy link
Copy Markdown
Collaborator

Thanks for looking at this. I've rebased and have just the CLN integration tests failing. I tried reproducing the failing test locally but couldn't. I should give it another go today.

Yes, I think this is unrelated, so feel free to ignore the flaky CI in this PR. Fixing it is really an unrelated task.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Looking into this PR has helped me gain a good understanding of how chain sourcing works. I think introducing BitcoindSyncClient is a good idea as the user can still carry out syncing operations using the REST API while the RPC Client is still maintained to carry out operations not supported by the REST API. I have just a few suggestions with regards to error handling.

Thanks for the review. Added the suggestions recommended for error handling.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

@enigbe Please let me know when this is ready for a second look.

This is ready @tnull.

@tnulltnull 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.

Hmm, tbh. I still dislike the current refactorings as they make the codepaths unnecessarily confusing and duplicative. Why keep a BitcoindRpcClient around side-by-side with the new BitcoindSyncClient that could also be an RPC client? If you maintain that merging in the REST codepaths with the RPC client is the way to go, why not extend the enum BitcoindSyncClient variants so that one would only hold the RPC client and the other the RPC and REST clients, which would allow us to add an fn rpc_client accessor that we can use everywhere, i.e., hence would allow us to drop the bitcoind_rpc_client field?

Comment threadsrc/builder.rs
Comment threadsrc/builder.rs Outdated
rpc_port: u16,
rpc_user: String,
rpc_password: String,
sync_client_config: Option<BitcoindRestSyncClientConfig>,

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.

Mhh, let's name this rest_client_config then.

Comment threadsrc/chain/mod.rs Outdated
}
}

pub(crate) enum BitcoindSyncClient {

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.

I'm not sure why this was added here vs bitcoind_rpc.rs.

@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch 3 times, most recently from 953bc1e to 1196080CompareMay 26, 2025 10:00
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Please excuse the delay on this. I have addressed concerns from the last review. Investigating a flaky test but it's ready for another look.

Let me know what you think when you can review.

@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 1196080 to d56e7a7CompareMay 26, 2025 12:19

@tnulltnull 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.

Excuse the considerable delay here, I have to admit this fell off my radar for a bit. Sorry! (feel free to re-request review if a PR is ready or it's been pending for a while)

Generally I like the updated approach much better from what I see. However, due to the huge code move and some commits that revert prior commits it's almost impossible to just review the parts of the code that actually changed (i.e., neither going commit-by-commit nor full-diff really works). Could you do me favor and clean up the commit history a bit? Also, I'm not conviced we gain much by the huge code move to common.rs/mod.rs if we don't actually split out REST and RPC types. Can we maybe revert that and keep everything in one file for now? Or, do the code move as an entirely separate commit in the beginning or end.

Thank you!

Comment threadbindings/ldk_node.udl Outdated
BackgroundSyncConfig? background_sync_config;
};

[Enum]

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.

nit: Seems this was first added here, then dropped in a later commit?

Comment threadsrc/chain/bitcoind/common.rs Outdated

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.

Can we please do the code moves in a prefactor commit? This would make it immediately clear that nothing changed when reviewing via git diff --color-moved.

And to be honest I'm not entirely sure why we move all that code at all, if we just end up with the implementation for both rpc and rest in mod.rs?

Comment threadsrc/chain/bitcoind/mod.rs Outdated

pub mod common;

pub enum BitcoindApiClient {

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.

Why not just BitcoindClient?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

This has been renamed.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Thanks for the feedback.

Excuse the considerable delay here, I have to admit this fell off my radar for a bit. Sorry! (feel free to re-request review if a PR is ready or it's been pending for a while)

Noted. I'll re-request going forward.

Generally I like the updated approach much better from what I see. However, due to the huge code move and some commits that revert prior commits it's almost impossible to just review the parts of the code that actually changed (i.e., neither going commit-by-commit nor full-diff really works). Could you do me favor and clean up the commit history a bit?

I'll clean up the commit history to make review much easier. Apologies.

Also, I'm not conviced we gain much by the huge code move to common.rs/mod.rs if we don't actually split out REST and RPC types. Can we maybe revert that and keep everything in one file for now? Or, do the code move as an entirely separate commit in the beginning or end.

You're right wrt common.rs. It is a leftover from when I separated REST and RPC types. Since we're not doing that split, I'll revert back to keeping everything in a single file to keep things simpler.

I'll have this cleaned up and ready shortly.

@tnulltnull 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.

This also needs a rebase now.

@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch 2 times, most recently from 9e88e4f to 4f34ec3CompareJune 11, 2025 16:45
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Hi @tnull

I've cleaned up the history and rebased. It is ready for another look.

@tnulltnull 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.

Excuse the delay here.

As far as I can tell the changes look mostly good, but the commit scheme still is very confusing to me and makes this very hard to review. Why are we first adding a _rest variant with a lot of copy/paste code, only to then merge them, whilst at the same time adding everything to a new file?

If you think this should all end up living in bitcoind.rs, please first make a commit moving the file to its new location (so that git understands it's the same file), and then make the changes in following commits. This would allow to see what actually was added and what changed in reference to the original code.

Comment threadsrc/builder.rs
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 4f34ec3 to c01b934CompareJune 20, 2025 14:55
@enigbe

enigbe commented Jun 20, 2025

Copy link
Copy Markdown
ContributorAuthor

Hi @tnull,

Why are we first adding a _rest variant with a lot of copy/paste code, only to then merge them, whilst at the same time adding everything to a new file?

I thought this might be helpful for other reviewers to see the justification in unifying the clients. Nonetheless, I've removed that commit so it should be as you'd prefer.

If you think this should all end up living in bitcoind.rs, please first make a commit moving the file to its new location (so that git understands it's the same file), and then make the changes in following commits. This would allow to see what actually was added and what changed in reference to the original code

I have added a prefactor commit to address the file change/rename.

Apologies for the confusion here. This should be much cleaner now.

@tnull
tnull self-requested a review June 23, 2025 12:13

@tnulltnull 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.

Thanks for the commit history cleanup, this is indeed now more reviewable. It still includes a bunch of unnecessary changes/refactorings that bloat the current changeset. It would be preferable not to include these (such as moving code blocks, switching up which types to import and which not, etc).

Moreover, I have to say I'm not super stoked about putting all the logic behind macros, as they are almost inaccessible to tooling (compiler checks, rustfmt, code completion, etc). Can we try to avoid them by prefactors that move the code to methods/closures or to helper methods that take whatever data these methods are operating on? If we do this, would be great to keep a sane git history.

All that said, functionally the code looks good to me.

Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from c01b934 to 925d214CompareJuly 1, 2025 07:24
enigbe added 2 commits July 1, 2025 08:43
Since we unify RPC and REST sync clients in the
following commit, we rename bitcoind_rpc to bitcoind
as this appropriately captures the unified client.
In the next commit, we'll introduce a unified bitcoind
client with two variant - RPC and REST. In preparation
to support chain syncing via the REST interface, we
refactor some methods into helper functions specific
to the interface client.
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 925d214 to ec53d60CompareJuly 1, 2025 07:44
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Hi @tnull,

This is ready for another look. As recommended, I have added:

  • prefector commit where the previously introduced macros are refactored into helper methods in preparation for REST support.
  • couple of fix-ups to address unwanted code move and type imports.

@tnulltnull 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.

Cool, thank you, much better without the macros!

Feel free to squash the fixups!

This commit:
- Adds support for syncing data via Bitcoin Core's
REST interface.
- Unifies the REST and RPC Bitcoin Core API clients
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from ec53d60 to 36e4c7fCompareJuly 1, 2025 10:18
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Squashed the fixups.

@tnull
tnull merged commit 0509e99 into lightningdevkit:mainJul 1, 2025
Sign up for freeto 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.

Bitcoind REST sync

5 participants

@enigbe@ldk-reviews-bot@tnull@Camillarhi@chuksys
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })(); feat: chain source rest sync by enigbe · Pull Request #526 · lightningdevkit/ldk-node · GitHub
Skip to content

feat: chain source rest sync - #526

Merged
tnull merged 3 commits into
lightningdevkit:mainfrom
enigbe:2025-04-bitcoind-rest-sync-v2
Jul 1, 2025
Merged

feat: chain source rest sync#526
tnull merged 3 commits into
lightningdevkit:mainfrom
enigbe:2025-04-bitcoind-rest-sync-v2

Conversation

@enigbe

@enigbeenigbe commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

What this PR does

This PR introduces chain source synchronization with Bitcoin Core's REST API. To do so, it explicitly distinguishes between the bitcoind_rpc_client and the newly introduced bitcoind_sync_client that can sync either via the RPC or REST APIs.

BitcoindRpcClient is maintained and the distinction created because the REST interface exposed by Bitcoin Core is not as robust as the RPC interface. This makes it impossible to send POST request to broadcast a transaction for example.

Related Issues

Fixes#496

cc @tnull@joostjager

@ldk-reviews-bot

ldk-reviews-bot commented Apr 18, 2025

Copy link
Copy Markdown

👋 Thanks for assigning @tnull as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

@enigbeenigbe changed the title feat: chain source rest sync with bitcoindfeat: chain source rest syncApr 18, 2025
@tnull
tnull requested review from tnull and removed request for joostjagerApril 18, 2025 10:47

@tnulltnull 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.

Thanks for looking into this! Before I get into more detailed review, I want to note two things on the chosen approach that stood out to me from a first look:

  1. I'm not quite convinced we should rename the BitcoindRpc client. Wouldn't it be cleaner to just add a (granted, rather duplicative) BitcoindRest variant side-by-side, and then in a second step maybe see how we can DRY up the code. Mushing both variants together seems to result in a rather confusing API and code structure.
  2. I'm a bit confused why we'd now still have RPC credentials on set_chain_source_bitcoind (and elsewhere in downstream from that), even if we want to use the REST backend? Is there any operation that still requires RPC access? Can't we just rely on REST alone?

@enigbe

enigbe commented Apr 19, 2025

Copy link
Copy Markdown
ContributorAuthor

Thanks for the early feedback.

  1. I'm not quite convinced we should rename the BitcoindRpc client. Wouldn't it be cleaner to just add a (granted, rather duplicative) BitcoindRest variant side-by-side, and then in a second step maybe see how we can DRY up the code. Mushing both variants together seems to result in a rather confusing API and code structure.
  2. I'm a bit confused why we'd now still have RPC credentials on set_chain_source_bitcoind (and elsewhere in downstream from that), even if we want to use the REST backend? Is there any operation that still requires RPC access? Can't we just rely on REST alone?

I understand your concerns about the name and API changes I have introduced but here is a bit of context.

Adding a ChainSource::BitcondRest variant was my initial approach too because I had erroneously thought Bitcoin Core's REST interface exposed (more or less) the same APIs as the RPC interface. However, the REST interface exposes a handful of APIs only for GET requests. It is not possible to broadcast transactions with a pure REST variant as we do with the BitcoindRpcClient, There is no REST API to sendrawtransaction.
For that particular use case, given that we'd need a handle on an RPC client regardless, I thought it best to:

  • Keep the BitcoindRpcClient as is.
  • Introduce a configurable REST/RPC sync client. With this, users can decide if syncing should happen either via the REST or RPC APIs.
  • Rename ChainSource::BitcoindRpc to ChainSource::Bitcoind as the latter leaves no ambiguity about the possibility of a REST variant.

@tnull

Copy link
Copy Markdown
Collaborator

I understand your concerns about the name and API changes I have introduced but here is a bit of context.

Adding a ChainSource::BitcondRest variant was my initial approach too because I had erroneously thought Bitcoin Core's REST interface exposed (more or less) the same APIs as the RPC interface. However, the REST interface exposes a handful of APIs only for GET requests. It is not possible to broadcast transactions with a pure REST variant as we do with the BitcoindRpcClient, There is no REST API to sendrawtransaction. For that particular use case, given that we'd need a handle on an RPC client regardless, I thought it best to:

* Keep the `BitcoindRpcClient` as is.
* Introduce a configurable REST/RPC sync client. With this, users can decide if syncing should happen either via the REST or RPC APIs.
* Rename `ChainSource::BitcoindRpc` to `ChainSource::Bitcoind` as the latter leaves no ambiguity about the possibility of a REST variant.

Aaah, okay, then the proposed approach makes more sense. I think it's still a bit awkward to have the mandatory RPC credentials on the set_chain_source_bitcoind method and addtionally have the BitcoindSyncConfig::Rpc variant.

While it then makes sense to reuse the client internally, it might be simpler to just keep the original set_chain_source_bitcoind_rpc method and just add a set_chain_source_bitcoind_rest variant, that also features the mandatory RPC credentials?

@Camillarhi

Copy link
Copy Markdown
Contributor

Hi @enigbe,

I noticed your CI is failing. I had a similar issue, but it was resolved after rebasing onto the latest dev branch (which includes changes from #524).

Could you try rebasing and see if that fixes the problem?

Thanks

@chuksyschuksys left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looking into this PR has helped me gain a good understanding of how chain sourcing works. I think introducing BitcoindSyncClient is a good idea as the user can still carry out syncing operations using the REST API while the RPC Client is still maintained to carry out operations not supported by the REST API. I have just a few suggestions with regards to error handling.

Comment threadsrc/builder.rs Outdated
Comment threadsrc/builder.rs Outdated
Comment threadsrc/builder.rs Outdated
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 9b07570 to a28d28fCompareMay 7, 2025 08:28
@tnull

Copy link
Copy Markdown
Collaborator

@enigbe Please let me know when this is ready for a second look.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Thanks for the feedback @tnull. I revisited this last week and addressed concerns raised here.

Aaah, okay, then the proposed approach makes more sense. I think it's still a bit awkward to have the mandatory RPC credentials on the set_chain_source_bitcoind method and addtionally have the BitcoindSyncConfig::Rpc variant.

This has been removed and we just keep a config for REST sync.

While it then makes sense to reuse the client internally, it might be simpler to just keep the original set_chain_source_bitcoind_rpc method and just add a set_chain_source_bitcoind_rest variant, that also features the mandatory RPC credentials?

Agreed! This seems like a cleaner split. I've added a set_chain_source_bitcoind_rest variant accordingly. However, I think it'd be potentially confusing/awkward for callers to still pass RPC parameters to the REST function. I opted for set_chain_source_bitcoind_rest to take only REST parameters and clarified comments on usage and dependency on the RPC variant.

If you'd still prefer we pass RPC parameters as a required input for the REST variant, happy to adjust.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Hi @enigbe,

I noticed your CI is failing. I had a similar issue, but it was resolved after rebasing onto the latest dev branch (which includes changes from #524).

Could you try rebasing and see if that fixes the problem?

Thanks

Thanks for looking at this. I've rebased and have just the CLN integration tests failing. I tried reproducing the failing test locally but couldn't. I should give it another go today.

@tnull

Copy link
Copy Markdown
Collaborator

Thanks for looking at this. I've rebased and have just the CLN integration tests failing. I tried reproducing the failing test locally but couldn't. I should give it another go today.

Yes, I think this is unrelated, so feel free to ignore the flaky CI in this PR. Fixing it is really an unrelated task.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Looking into this PR has helped me gain a good understanding of how chain sourcing works. I think introducing BitcoindSyncClient is a good idea as the user can still carry out syncing operations using the REST API while the RPC Client is still maintained to carry out operations not supported by the REST API. I have just a few suggestions with regards to error handling.

Thanks for the review. Added the suggestions recommended for error handling.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

@enigbe Please let me know when this is ready for a second look.

This is ready @tnull.

@tnulltnull 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.

Hmm, tbh. I still dislike the current refactorings as they make the codepaths unnecessarily confusing and duplicative. Why keep a BitcoindRpcClient around side-by-side with the new BitcoindSyncClient that could also be an RPC client? If you maintain that merging in the REST codepaths with the RPC client is the way to go, why not extend the enum BitcoindSyncClient variants so that one would only hold the RPC client and the other the RPC and REST clients, which would allow us to add an fn rpc_client accessor that we can use everywhere, i.e., hence would allow us to drop the bitcoind_rpc_client field?

Comment threadsrc/builder.rs
Comment threadsrc/builder.rs Outdated
rpc_port: u16,
rpc_user: String,
rpc_password: String,
sync_client_config: Option<BitcoindRestSyncClientConfig>,

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.

Mhh, let's name this rest_client_config then.

Comment threadsrc/chain/mod.rs Outdated
}
}

pub(crate) enum BitcoindSyncClient {

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.

I'm not sure why this was added here vs bitcoind_rpc.rs.

@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch 3 times, most recently from 953bc1e to 1196080CompareMay 26, 2025 10:00
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Please excuse the delay on this. I have addressed concerns from the last review. Investigating a flaky test but it's ready for another look.

Let me know what you think when you can review.

@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 1196080 to d56e7a7CompareMay 26, 2025 12:19

@tnulltnull 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.

Excuse the considerable delay here, I have to admit this fell off my radar for a bit. Sorry! (feel free to re-request review if a PR is ready or it's been pending for a while)

Generally I like the updated approach much better from what I see. However, due to the huge code move and some commits that revert prior commits it's almost impossible to just review the parts of the code that actually changed (i.e., neither going commit-by-commit nor full-diff really works). Could you do me favor and clean up the commit history a bit? Also, I'm not conviced we gain much by the huge code move to common.rs/mod.rs if we don't actually split out REST and RPC types. Can we maybe revert that and keep everything in one file for now? Or, do the code move as an entirely separate commit in the beginning or end.

Thank you!

Comment threadbindings/ldk_node.udl Outdated
BackgroundSyncConfig? background_sync_config;
};

[Enum]

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.

nit: Seems this was first added here, then dropped in a later commit?

Comment threadsrc/chain/bitcoind/common.rs Outdated

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.

Can we please do the code moves in a prefactor commit? This would make it immediately clear that nothing changed when reviewing via git diff --color-moved.

And to be honest I'm not entirely sure why we move all that code at all, if we just end up with the implementation for both rpc and rest in mod.rs?

Comment threadsrc/chain/bitcoind/mod.rs Outdated

pub mod common;

pub enum BitcoindApiClient {

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.

Why not just BitcoindClient?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

This has been renamed.

@enigbe

Copy link
Copy Markdown
ContributorAuthor

Thanks for the feedback.

Excuse the considerable delay here, I have to admit this fell off my radar for a bit. Sorry! (feel free to re-request review if a PR is ready or it's been pending for a while)

Noted. I'll re-request going forward.

Generally I like the updated approach much better from what I see. However, due to the huge code move and some commits that revert prior commits it's almost impossible to just review the parts of the code that actually changed (i.e., neither going commit-by-commit nor full-diff really works). Could you do me favor and clean up the commit history a bit?

I'll clean up the commit history to make review much easier. Apologies.

Also, I'm not conviced we gain much by the huge code move to common.rs/mod.rs if we don't actually split out REST and RPC types. Can we maybe revert that and keep everything in one file for now? Or, do the code move as an entirely separate commit in the beginning or end.

You're right wrt common.rs. It is a leftover from when I separated REST and RPC types. Since we're not doing that split, I'll revert back to keeping everything in a single file to keep things simpler.

I'll have this cleaned up and ready shortly.

@tnulltnull 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.

This also needs a rebase now.

@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch 2 times, most recently from 9e88e4f to 4f34ec3CompareJune 11, 2025 16:45
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Hi @tnull

I've cleaned up the history and rebased. It is ready for another look.

@tnulltnull 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.

Excuse the delay here.

As far as I can tell the changes look mostly good, but the commit scheme still is very confusing to me and makes this very hard to review. Why are we first adding a _rest variant with a lot of copy/paste code, only to then merge them, whilst at the same time adding everything to a new file?

If you think this should all end up living in bitcoind.rs, please first make a commit moving the file to its new location (so that git understands it's the same file), and then make the changes in following commits. This would allow to see what actually was added and what changed in reference to the original code.

Comment threadsrc/builder.rs
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 4f34ec3 to c01b934CompareJune 20, 2025 14:55
@enigbe

enigbe commented Jun 20, 2025

Copy link
Copy Markdown
ContributorAuthor

Hi @tnull,

Why are we first adding a _rest variant with a lot of copy/paste code, only to then merge them, whilst at the same time adding everything to a new file?

I thought this might be helpful for other reviewers to see the justification in unifying the clients. Nonetheless, I've removed that commit so it should be as you'd prefer.

If you think this should all end up living in bitcoind.rs, please first make a commit moving the file to its new location (so that git understands it's the same file), and then make the changes in following commits. This would allow to see what actually was added and what changed in reference to the original code

I have added a prefactor commit to address the file change/rename.

Apologies for the confusion here. This should be much cleaner now.

@tnull
tnull self-requested a review June 23, 2025 12:13

@tnulltnull 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.

Thanks for the commit history cleanup, this is indeed now more reviewable. It still includes a bunch of unnecessary changes/refactorings that bloat the current changeset. It would be preferable not to include these (such as moving code blocks, switching up which types to import and which not, etc).

Moreover, I have to say I'm not super stoked about putting all the logic behind macros, as they are almost inaccessible to tooling (compiler checks, rustfmt, code completion, etc). Can we try to avoid them by prefactors that move the code to methods/closures or to helper methods that take whatever data these methods are operating on? If we do this, would be great to keep a sane git history.

All that said, functionally the code looks good to me.

Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
Comment threadsrc/chain/bitcoind.rs Outdated
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from c01b934 to 925d214CompareJuly 1, 2025 07:24
enigbe added 2 commits July 1, 2025 08:43
Since we unify RPC and REST sync clients in the
following commit, we rename bitcoind_rpc to bitcoind
as this appropriately captures the unified client.
In the next commit, we'll introduce a unified bitcoind
client with two variant - RPC and REST. In preparation
to support chain syncing via the REST interface, we
refactor some methods into helper functions specific
to the interface client.
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from 925d214 to ec53d60CompareJuly 1, 2025 07:44
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Hi @tnull,

This is ready for another look. As recommended, I have added:

  • prefector commit where the previously introduced macros are refactored into helper methods in preparation for REST support.
  • couple of fix-ups to address unwanted code move and type imports.

@tnulltnull 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.

Cool, thank you, much better without the macros!

Feel free to squash the fixups!

This commit:
- Adds support for syncing data via Bitcoin Core's
REST interface.
- Unifies the REST and RPC Bitcoin Core API clients
@enigbe
enigbeforce-pushed the 2025-04-bitcoind-rest-sync-v2 branch from ec53d60 to 36e4c7fCompareJuly 1, 2025 10:18
@enigbe

Copy link
Copy Markdown
ContributorAuthor

Squashed the fixups.

@tnull
tnull merged commit 0509e99 into lightningdevkit:mainJul 1, 2025
Sign up for freeto 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.

Bitcoind REST sync

5 participants

@enigbe@ldk-reviews-bot@tnull@Camillarhi@chuksys