Electrum integration for Bitcoin chain#3406
Merged
tomaszslabon merged 54 commits intomainfrom Dec 9, 2022
Merged
Conversation
The intention of the function is to return the most recent block that was mined (added to the chain) - also called a "tip". Replacing `Current` with `Latest` seems accurate and should reduce the confusion, as `current` may suggest the block that is currently being mined.
For Bitcoin it seems more common to use the name `block height` over the `block number`.
We need a `String()` function to serialize Hash automatically in the log messages to a string. By default it will convert the Hash to a hex in the InternalByteOrder. This change required the previous `String` function to be renamed to `Hex`.
The function can be used to reverse the bytes order in the slice.
Electrum client supports TCP and SSL connections to the Electrum server. This enumeration will be used to handle the protocol types.
b8e5ee1 to
ea88f63
Compare
The function initializes connection to a remote Electrum server. TCP and SSL types of connections are supported. Most of the servers run electrum protocol 1.4 version, hence we use it for verification. If a server runs another version of the protocol a warning will be issued. A connection to an Electrum server may be dropped if no requests are submitted, hence we implement a keep alive loop that will ping the server to keep the connection alive.
ea88f63 to
c51f12f
Compare
The function queries electrum server for a block header for a block at the given height and then returns it in the format expected by the bitcoin.Chain interface.
The function queries electrum server for a blockchain tip and returns its' block height.
The function submits a serialized transaction to the chain.
The function queries electrum for a details of the specific transaction and converts it in a format expected by the bitcoin.Chain interface. The simpliest solution is to use `GetTransaction` function to query electrum as it returns the transaction details in verbose format. Unfortunatelly it's not compatible with Esplora/Electrs implementation of ElectrumX server. (see: Blockstream/electrs#36) As a workaround to cover integration with Esplora/Electrs we use `GetRawTransaction` and deserialize the transaction.
The function returns number of confirmations for the given transaction. As Esplora/Electrs doesn't support verbose transactions details in the response, that would contain the confirmations number we need to workaround it. (See: Blockstream/electrs#36) The workaround: 1. Get the raw transaction 2. Deserialize the raw transaction 3. Find transaction block height by finding it in a history of transactions for the output script included in the transaction. 4. Get the latest block height 5. Calculate number of confirmations by subtracting the transaction block height from the latest block height and adding one.
The tests validate integration with a public electrum servers.
Mostly added TODOs and improved error handling.
The function was added to utils, so we can use it here.
3 tasks
The function passes a context that should be used within it.
To reduce confusion we name the property `parentCtx`.
The `BitcoinConfig` struct is a container for configuration of different implementations of the bitcoin integration. We cannot place it in `electrum` package, as the package is a specific implementation. The struct cannot be located insige the `pkg/bitcoin` package due to a cyclic dependencies. We move it out to the main `config` package as it seems the most clean approach. We can regiter more implementations (e.g. Electrs).
The flags reflect all the configuration properties for electrum. We introduced a custom flag for Protocol enum.
The implementation can be used in the maintainer already.
b42431c to
a4c21e8
Compare
tomaszslabon
reviewed
Dec 2, 2022
tomaszslabon
reviewed
Dec 2, 2022
The config properties reflect the default config of the client.
Wrappers defined in keep-common library require a parent context to be passed to correctly initialize the context with timeout. The change was introduced in keep-network/keep-common#109
tomaszslabon
approved these changes
Dec 9, 2022
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR implements Bitcoin package integration with Electrum API.
There are a couple of servers implementations supporting the Electrum API:
A list of publicly exposed Electrum protocol servers for Bitcoin can be found here:
We added integration tests to verify the implementation works correctly with the most popular server implementations. To run the integration tests execute:
There are a couple of TODOs in the code regarding the improvements that will be handled in follow-up PRs.
Refs #3384
Refs #3369
Refs #3395