-
Notifications
You must be signed in to change notification settings - Fork 68
feat(client): add async and blocking clients to submit txs package #114
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
Open
acidbunny21
wants to merge
1
commit into
bitcoindevkit:master
Choose a base branch
from
acidbunny21:submit-tx-pkg-clients
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+211
−32
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -16,6 +16,7 @@ use std::convert::TryFrom; | |||||
| use std::str::FromStr; | ||||||
| use std::thread; | ||||||
|
|
||||||
| use bitcoin::consensus::encode::serialize_hex; | ||||||
| #[allow(unused_imports)] | ||||||
| use log::{debug, error, info, trace}; | ||||||
|
|
||||||
|
|
@@ -31,8 +32,8 @@ use bitcoin::{ | |||||
|
|
||||||
| use crate::api::AddressStats; | ||||||
| use crate::{ | ||||||
| BlockStatus, BlockSummary, Builder, Error, MerkleProof, OutputStatus, Tx, TxStatus, Utxo, | ||||||
| BASE_BACKOFF_MILLIS, RETRYABLE_ERROR_CODES, | ||||||
| BlockStatus, BlockSummary, Builder, Error, MerkleProof, OutputStatus, SubmitPackageResult, Tx, | ||||||
| TxStatus, Utxo, BASE_BACKOFF_MILLIS, RETRYABLE_ERROR_CODES, | ||||||
| }; | ||||||
|
|
||||||
| #[derive(Debug, Clone)] | ||||||
|
|
@@ -88,6 +89,24 @@ impl BlockingClient { | |||||
| Ok(request) | ||||||
| } | ||||||
|
|
||||||
| fn post_request<T>(&self, path: &str, body: T) -> Result<Request, Error> | ||||||
| where | ||||||
| T: Into<Vec<u8>>, | ||||||
| { | ||||||
| let mut request = minreq::post(format!("{}{}", self.url, path)).with_body(body); | ||||||
|
|
||||||
| if let Some(proxy) = &self.proxy { | ||||||
| let proxy = Proxy::new(proxy.as_str())?; | ||||||
| request = request.with_proxy(proxy); | ||||||
| } | ||||||
|
|
||||||
| if let Some(timeout) = &self.timeout { | ||||||
| request = request.with_timeout(*timeout); | ||||||
| } | ||||||
|
|
||||||
| Ok(request) | ||||||
| } | ||||||
|
|
||||||
| fn get_opt_response<T: Decodable>(&self, path: &str) -> Result<Option<T>, Error> { | ||||||
| match self.get_with_retry(path) { | ||||||
| Ok(resp) if is_status_not_found(resp.status_code) => Ok(None), | ||||||
|
|
@@ -267,21 +286,63 @@ impl BlockingClient { | |||||
| } | ||||||
|
|
||||||
| /// Broadcast a [`Transaction`] to Esplora | ||||||
| pub fn broadcast(&self, transaction: &Transaction) -> Result<(), Error> { | ||||||
| let mut request = minreq::post(format!("{}/tx", self.url)).with_body( | ||||||
| pub fn broadcast(&self, transaction: &Transaction) -> Result<Txid, Error> { | ||||||
| let request = self.post_request( | ||||||
| "tx", | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| serialize(transaction) | ||||||
| .to_lower_hex_string() | ||||||
| .as_bytes() | ||||||
| .to_vec(), | ||||||
| ); | ||||||
| )?; | ||||||
|
|
||||||
| if let Some(proxy) = &self.proxy { | ||||||
| let proxy = Proxy::new(proxy.as_str())?; | ||||||
| request = request.with_proxy(proxy); | ||||||
| match request.send() { | ||||||
| Ok(resp) if !is_status_ok(resp.status_code) => { | ||||||
| let status = u16::try_from(resp.status_code).map_err(Error::StatusCode)?; | ||||||
| let message = resp.as_str().unwrap_or_default().to_string(); | ||||||
| Err(Error::HttpResponse { status, message }) | ||||||
| } | ||||||
| Ok(resp) => { | ||||||
| let txid = | ||||||
| Txid::from_str(resp.as_str().unwrap_or_default()).map_err(Error::HexToArray)?; | ||||||
| Ok(txid) | ||||||
| } | ||||||
| Err(e) => Err(Error::Minreq(e)), | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| if let Some(timeout) = &self.timeout { | ||||||
| request = request.with_timeout(*timeout); | ||||||
| /// Broadcast a package of [`Transaction`] to Esplora | ||||||
| /// | ||||||
| /// if `maxfeerate` is provided, any transaction whose | ||||||
| /// fee is higher will be rejected | ||||||
| /// | ||||||
| /// if `maxburnamount` is provided, any transaction | ||||||
| /// with higher provably unspendable outputs amount | ||||||
| /// will be rejected | ||||||
| pub fn submit_package( | ||||||
| &self, | ||||||
| transactions: &[Transaction], | ||||||
| maxfeerate: Option<f64>, | ||||||
| maxburnamount: Option<f64>, | ||||||
| ) -> Result<SubmitPackageResult, Error> { | ||||||
| let serialized_txs = transactions | ||||||
| .iter() | ||||||
| .map(|tx| serialize_hex(&tx)) | ||||||
| .collect::<Vec<_>>(); | ||||||
|
|
||||||
| let mut request = self.post_request( | ||||||
| "txs/package", | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| serde_json::to_string(&serialized_txs) | ||||||
| .unwrap() | ||||||
| .as_bytes() | ||||||
| .to_vec(), | ||||||
| )?; | ||||||
|
|
||||||
| if let Some(maxfeerate) = maxfeerate { | ||||||
| request = request.with_param("maxfeerate", maxfeerate.to_string()) | ||||||
| } | ||||||
|
|
||||||
| if let Some(maxburnamount) = maxburnamount { | ||||||
| request = request.with_param("maxburnamount", maxburnamount.to_string()) | ||||||
| } | ||||||
|
|
||||||
| match request.send() { | ||||||
|
|
@@ -290,7 +351,7 @@ impl BlockingClient { | |||||
| let message = resp.as_str().unwrap_or_default().to_string(); | ||||||
| Err(Error::HttpResponse { status, message }) | ||||||
| } | ||||||
| Ok(_resp) => Ok(()), | ||||||
| Ok(resp) => Ok(resp.json::<SubmitPackageResult>().map_err(Error::Minreq)?), | ||||||
| Err(e) => Err(Error::Minreq(e)), | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we know if the variants here are finite? Do we see a chance to parse this into an
enum?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, that'd be best.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i agree that would be best, but as I can see here, there is no enum defined for that field. I'd be happy to update that part as soon as it is upgraded to one there