-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Mine block before and after onboarding app
It appears broadcasting the funding transaction fails every once in a while as the output transaction has already been spent. This leads to the following bug #873. Once fixed it should not be required to mine blocks to make that test pass all the time.
- Loading branch information
Showing
4 changed files
with
98 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use anyhow::bail; | ||
use anyhow::Result; | ||
use reqwest::Client; | ||
use reqwest::Response; | ||
use serde::Deserialize; | ||
use std::time::Duration; | ||
|
||
/// A wrapper over the bitcoind HTTP API | ||
/// | ||
/// It does not aim to be complete, functionality will be added as needed | ||
pub struct Bitcoind { | ||
client: Client, | ||
host: String, | ||
} | ||
|
||
impl Bitcoind { | ||
pub fn new(client: Client) -> Self { | ||
let host = "http://localhost:8080/bitcoin".to_string(); | ||
Self { client, host } | ||
} | ||
|
||
/// Instructs `bitcoind` to generate to address. | ||
pub async fn mine(&self, n: u16) -> Result<()> { | ||
#[derive(Deserialize, Debug)] | ||
struct BitcoindResponse { | ||
result: String, | ||
} | ||
|
||
let response: BitcoindResponse = self | ||
.client | ||
.post(&self.host) | ||
.body(r#"{"jsonrpc": "1.0", "method": "getnewaddress", "params": []}"#.to_string()) | ||
.send() | ||
.await? | ||
.json() | ||
.await?; | ||
|
||
self.client | ||
.post(&self.host) | ||
.body(format!( | ||
r#"{{"jsonrpc": "1.0", "method": "generatetoaddress", "params": [{}, "{}"]}}"#, | ||
n, response.result | ||
)) | ||
.send() | ||
.await?; | ||
|
||
// For the mined blocks to be picked up by the subsequent wallet syncs | ||
tokio::time::sleep(Duration::from_secs(5)).await; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn post(&self, endpoint: &str, body: Option<String>) -> Result<Response> { | ||
let mut builder = self.client.post(endpoint.to_string()); | ||
if let Some(body) = body { | ||
builder = builder.body(body); | ||
} | ||
let response = builder.send().await?; | ||
|
||
if !response.status().is_success() { | ||
bail!(response.text().await?) | ||
} | ||
Ok(response) | ||
} | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod app; | ||
pub mod bitcoind; | ||
pub mod coordinator; | ||
pub mod fund; | ||
pub mod http; | ||
|
This file contains 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