Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add code example for each supported backend #526

Merged
merged 2 commits into from
Nov 22, 2022

Conversation

w0xlt
Copy link
Contributor

@w0xlt w0xlt commented Jan 12, 2022

This PR adds code example for connecting to Esplora, Electrum Server, Neutrino and Bitcoin Core.
Also shows how to retrieve balance, sign and broadcast transactions.

To test:

cd examples/backend/
cargo run electrum
cargo run esplora
cargo run neutrino
cargo run rpc_core

@w0xlt w0xlt changed the title Add code example for every supported backend Add code example for each supported backend Jan 12, 2022
Copy link
Contributor

@rajarshimaitra rajarshimaitra left a comment

Choose a reason for hiding this comment

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

Concept ACK on adding more example to the project. But I think its possible without adding a new cargo project in the workspace.

Can't we just have running examples like this #519?

@notmandatory
Copy link
Member

Hi, please rebase to pickup changes in #596. Thanks!

@w0xlt
Copy link
Contributor Author

w0xlt commented Jul 26, 2022

@notmandatory sorry for the delay.

I force pushed a new version that adds examples just for Electrum and Esplora, as there are already examples for Compact Filter and RPC backends.

I also updated the code to 0.20.1 and addressed @rajarshimaitra's suggestion not to add a new cargo project.

@w0xlt
Copy link
Contributor Author

w0xlt commented Jul 27, 2022

The new push added the documentation suggested in #685, explaining how to use use-esplora-reqwest.

Copy link
Contributor

@rajarshimaitra rajarshimaitra left a comment

Choose a reason for hiding this comment

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

Concept ACK.. Few more fixes needs to be done..

Cargo.toml Outdated Show resolved Hide resolved
examples/esplora_backend.rs Outdated Show resolved Hide resolved
examples/electrum_backend.rs Outdated Show resolved Hide resolved
@w0xlt w0xlt force-pushed the add_examples branch 2 times, most recently from 8f1ac3d to 400cbad Compare September 1, 2022 19:51
Copy link
Contributor

@rajarshimaitra rajarshimaitra left a comment

Choose a reason for hiding this comment

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

Code looks good.. It need few more fixes to work.. Sorry if I reviewed prematurely.

examples/electrum_backend.rs Outdated Show resolved Hide resolved
examples/esplora_backend.rs Outdated Show resolved Hide resolved
examples/electrum_backend.rs Outdated Show resolved Hide resolved
Cargo.toml Outdated Show resolved Hide resolved
examples/electrum_backend.rs Outdated Show resolved Hide resolved
examples/esplora_backend.rs Outdated Show resolved Hide resolved
@w0xlt w0xlt force-pushed the add_examples branch 2 times, most recently from a9c426e to 95e9b7f Compare September 4, 2022 04:40
Copy link
Contributor

@rajarshimaitra rajarshimaitra left a comment

Choose a reason for hiding this comment

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

tACK 95e9b7f

Code loos good.. Thanks for the quick update.. One last comment..

Cargo.toml Outdated Show resolved Hide resolved
examples/electrum_backend.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@rajarshimaitra rajarshimaitra left a comment

Choose a reason for hiding this comment

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

ACK f13d5e6

One last fix to pass the tests..

examples/electrum_backend.rs Outdated Show resolved Hide resolved
@w0xlt w0xlt force-pushed the add_examples branch 5 times, most recently from 07f6747 to c7e1247 Compare September 5, 2022 14:04
Copy link
Contributor

@rajarshimaitra rajarshimaitra left a comment

Choose a reason for hiding this comment

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

tACK c7e1247

LGTM.. One suggestion maybe for a future PR.

/// This can be run with `cargo run --features="use-esplora-reqwest, reqwest-default-tls" --example esplora_backend`
/// in the root folder.
///
/// Note: The configuration above uses asynchronous HTTP calls.
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry I have missed this before. Nothing major, feel free to ignore for this PR.

Even though the calls are async in reqwest they are still blocking in our impl, using the await_or_block macro like this,

Ok(await_or_block!(self.url_client._get_tx(txid))?)

So to actually make the wallet calls async in the example you need to use the async-interface feature too.. In that case you will have await in your wallets calls in the example..

This PR is good to go on its own.. Maybe as a future PR an idea.

  • Transform this to a blocking example using ureq.
  • Create another fully async example using reqwest.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That would be great. But, as mentioned above, the command cargo run --features="use-esplora-reqwest, reqwest-default-tls, async-interface" --example esplora_backend results in error.

I think the problem is here (src/lib:210)

#[cfg(all(feature = "async-interface", feature = "electrum"))]
compile_error!(
    "Features async-interface and electrum are mutually exclusive and cannot be enabled together"
);

Even there is no electrum in the features, it seems this is enabled by default. Is there a way to disable it ?

Copy link
Member

Choose a reason for hiding this comment

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

You can disable the default features (which are "key-value-db", "electrum") by adding --no-default-features, eg:

cargo run --no-default-features --features=use-esplora-reqwest,reqwest-default-tls,async-interface --example esplora_backend

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@notmandatory this worked. Thanks.

@rajarshimaitra I created two files examples/esplora_backend_synchronous.rs and examples/esplora_backend_asynchronous.rs in a2b6a40.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know why CI / Rust fmt (pull_request) failed. I ran cargo fmt --all before committing.

Copy link
Contributor

@rajarshimaitra rajarshimaitra left a comment

Choose a reason for hiding this comment

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

ACK a2b6a40

Thanks for the split.. This now looks complete.. Few more nits..

examples/esplora_backend_asynchronous.rs Outdated Show resolved Hide resolved
examples/esplora_backend_asynchronous.rs Outdated Show resolved Hide resolved
examples/esplora_backend_synchronous.rs Outdated Show resolved Hide resolved
Cargo.toml Outdated Show resolved Hide resolved
examples/electrum_backend.rs Outdated Show resolved Hide resolved
@rajarshimaitra
Copy link
Contributor

I don't know why CI / Rust fmt (pull_request) failed. I ran cargo fmt --all before committing.

It was some temporary problem with rust update server.. I restarted the tests and it passed..

Copy link
Contributor

@rajarshimaitra rajarshimaitra left a comment

Choose a reason for hiding this comment

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

tACK e50fcb6

@w0xlt
Copy link
Contributor Author

w0xlt commented Nov 1, 2022

Rebased.
Is any additional action required ?

Copy link
Contributor

@rajarshimaitra rajarshimaitra left a comment

Choose a reason for hiding this comment

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

Sorry took me some time to get back to this.. Thanks for pushing this through..

tACK f99a6b9

All looks good to me, and great work separating the blocking and async example separate. This is good to go, no more changes should be required..

@notmandatory notmandatory merged commit 1c95ca3 into bitcoindevkit:master Nov 22, 2022
Copy link

@22f-cy 22f-cy left a comment

Choose a reason for hiding this comment

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

if [ "$PWD" = "$HOME" ]; then

@w0xlt w0xlt deleted the add_examples branch November 30, 2022 04:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

4 participants