-
Notifications
You must be signed in to change notification settings - Fork 4
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
coordinator: get_spend_transaction #31
base: master
Are you sure you want to change the base?
Changes from all commits
55bd888
f985b2e
269d299
69710c6
81fc75d
1375fc5
e819759
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ authors = ["Antoine Poinsot <[email protected]>"] | |
edition = "2018" | ||
|
||
[dependencies] | ||
bitcoinconsensus = "0.19.0-2" | ||
revault_tx = { version = "0.5", features = ["use-serde"] } | ||
revault_net = "0.3" | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use std::net::SocketAddr; | ||
|
||
use revault_net::{ | ||
message::coordinator::{GetSpendTx, SpendTx}, | ||
noise::{PublicKey, SecretKey}, | ||
transport::KKTransport, | ||
}; | ||
|
||
use revault_tx::bitcoin::{OutPoint, Transaction}; | ||
|
||
const COORDINATOR_CLIENT_RETRIES: usize = 3; | ||
|
||
pub struct CoordinatorClient { | ||
host: SocketAddr, | ||
our_noise_secret_key: SecretKey, | ||
pub_key: PublicKey, | ||
/// How many times the client will try again | ||
/// to send a request to coordinator upon failure | ||
retries: usize, | ||
} | ||
|
||
impl CoordinatorClient { | ||
pub fn new(our_noise_secret_key: SecretKey, host: SocketAddr, pub_key: PublicKey) -> Self { | ||
Self { | ||
host, | ||
our_noise_secret_key, | ||
pub_key, | ||
retries: COORDINATOR_CLIENT_RETRIES, | ||
} | ||
} | ||
|
||
/// Wrapper to retry a request sent to coordinator upon IO failure | ||
/// according to the configured number of retries. | ||
fn retry<T, R: Fn() -> Result<T, revault_net::Error>>( | ||
&self, | ||
request: R, | ||
) -> Result<T, revault_net::Error> { | ||
let mut error: Option<revault_net::Error> = None; | ||
for _ in 0..self.retries { | ||
match request() { | ||
Ok(res) => return Ok(res), | ||
Err(e) => error = Some(e), | ||
} | ||
log::debug!( | ||
"Error while communicating with coordinator: {}, retrying", | ||
error.as_ref().expect("An error must have happened"), | ||
); | ||
std::thread::sleep(std::time::Duration::from_secs(3)); | ||
} | ||
Err(error.expect("An error must have happened")) | ||
} | ||
|
||
fn send_req<T>(&self, req: &revault_net::message::Request) -> Result<T, revault_net::Error> | ||
where | ||
T: serde::de::DeserializeOwned, | ||
{ | ||
log::debug!( | ||
"Sending request to Coordinator: '{}'", | ||
serde_json::to_string(req).unwrap(), | ||
); | ||
let mut transport = | ||
KKTransport::connect(self.host, &self.our_noise_secret_key, &self.pub_key)?; | ||
transport.send_req(&req) | ||
} | ||
|
||
/// Get Spend transaction spending the vault with the given deposit outpoint. | ||
/// Beware that the spend transaction may be invalid and needs to be verified against | ||
/// libbitcoinconsensus. | ||
Comment on lines
+66
to
+68
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. What is the purpose of checking the Spend transaction against libbitcoinconsensus? 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. I may have misunderstood libbitcoinconsensus usage, but it was to check that the sigs and the tx are valid and can pass the btc contensus and that the coordinator did not trick us with a fake spend that will never pass the mempool 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.
But if they committed to an invalid Spend.. It's their problem? It will be unspendable through the manager path so no harm can be done (assuming cosigning servers aren't compromised, which Spend things always do). They will have to Cancel all the Unvaults. |
||
pub fn get_spend_transaction( | ||
&self, | ||
deposit_outpoint: OutPoint, | ||
) -> Result<Option<Transaction>, revault_net::Error> { | ||
let resp: SpendTx = self.retry(|| { | ||
let msg = GetSpendTx { deposit_outpoint }; | ||
self.send_req(&msg.into()) | ||
})?; | ||
log::debug!( | ||
"Got from Coordinator: '{}'", | ||
serde_json::to_string(&resp).unwrap() | ||
); | ||
Ok(resp.spend_tx) | ||
} | ||
} |
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.
or
base64
?