Added confirmation printout / prompt to begin spend for demo purposes.
This commit is contained in:
parent
2e3c6ea008
commit
d7f2b49701
|
@ -14,6 +14,7 @@ dependencies = [
|
|||
"rust-crypto 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"strason 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"thread-scoped 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"whiteread 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
|
@ -422,6 +423,11 @@ dependencies = [
|
|||
"rand 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thread-scoped"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.1.34"
|
||||
|
|
|
@ -17,3 +17,4 @@ serde = "0.6.6"
|
|||
strason = "0.3.3"
|
||||
whiteread = "0.2.0"
|
||||
rust-crypto = "^0.2"
|
||||
thread-scoped = "1.0.1"
|
|
@ -100,7 +100,8 @@ pub fn solve_sudoku(client: &mut jsonrpc::client::Client,
|
|||
}
|
||||
|
||||
pub fn poll_for_payment(client: &mut jsonrpc::client::Client,
|
||||
p2sh: &str
|
||||
p2sh: &str,
|
||||
old_confirmations: &mut isize
|
||||
) -> Option<(String, usize)>
|
||||
{
|
||||
let request = client.build_request("listtransactions".to_string(), vec!["*".into(), 100.into(), 0.into(), true.into()]);
|
||||
|
@ -128,7 +129,9 @@ pub fn poll_for_payment(client: &mut jsonrpc::client::Client,
|
|||
}
|
||||
}
|
||||
|
||||
if found && confirmations > 0 {
|
||||
if found {
|
||||
*old_confirmations = confirmations;
|
||||
|
||||
return Some((txid.unwrap().into(), vout.unwrap().parse().unwrap()));
|
||||
}
|
||||
}
|
||||
|
|
39
src/main.rs
39
src/main.rs
|
@ -4,6 +4,7 @@ extern crate whiteread;
|
|||
extern crate libc;
|
||||
extern crate bincode;
|
||||
extern crate rand;
|
||||
extern crate thread_scoped;
|
||||
extern crate hex;
|
||||
extern crate serde;
|
||||
extern crate clap;
|
||||
|
@ -25,7 +26,10 @@ use hex::{ToHex, FromHex};
|
|||
use clap::{App, Arg, SubCommand};
|
||||
use crypto::sha2::Sha256;
|
||||
use rand::{Rng,thread_rng};
|
||||
use std::sync::mpsc::channel;
|
||||
use thread_scoped::scoped;
|
||||
use crypto::digest::Digest;
|
||||
use std::sync::{Arc,Mutex};
|
||||
|
||||
mod sudoku;
|
||||
mod ffi;
|
||||
|
@ -283,11 +287,44 @@ fn handle_server(stream: &mut TcpStream, ctx: &Context, n: usize, rpc: &mut json
|
|||
|
||||
serialize_into(stream, &solving_pubkey, Infinite);
|
||||
|
||||
{
|
||||
let (tx, rx) = channel();
|
||||
let rx = Arc::new(Mutex::new(rx));
|
||||
|
||||
unsafe {
|
||||
let derp = scoped(|| {
|
||||
let mut old_confirmations: isize = -1;
|
||||
|
||||
loop {
|
||||
let mut confirmations: isize = -1;
|
||||
bitcoin::poll_for_payment(rpc, &p2sh, &mut confirmations);
|
||||
|
||||
if confirmations != old_confirmations {
|
||||
old_confirmations = confirmations;
|
||||
|
||||
println!("I have {} confirmations.", confirmations);
|
||||
}
|
||||
|
||||
std::thread::sleep(std::time::Duration::from_secs(1));
|
||||
|
||||
if (rx.lock().unwrap().try_recv().is_ok()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
prompt::<String>("Press enter when ready to spend.\n");
|
||||
tx.send(());
|
||||
|
||||
drop(derp);
|
||||
}
|
||||
}
|
||||
|
||||
let mut txid;
|
||||
let mut vout;
|
||||
|
||||
loop {
|
||||
if let Some((_txid, _vout)) = bitcoin::poll_for_payment(rpc, &p2sh) {
|
||||
if let Some((_txid, _vout)) = bitcoin::poll_for_payment(rpc, &p2sh, &mut 0) {
|
||||
txid = _txid;
|
||||
vout = _vout;
|
||||
break;
|
||||
|
|
|
@ -21,10 +21,13 @@ pub fn print_sudoku(dim: usize, grid: &[u8]) {
|
|||
println!("");
|
||||
}
|
||||
|
||||
pub fn prompt<T: whiteread::White>(prompt: &str) -> T {
|
||||
pub fn prompt<T: whiteread::White>(prompt: &str) -> Option<T> {
|
||||
print!("{}", prompt);
|
||||
io::stdout().flush().unwrap();
|
||||
parse_line().unwrap()
|
||||
match parse_line() {
|
||||
Err(_) => None,
|
||||
Ok(t) => Some(t)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_sudoku_from_stdin(dimension: usize) -> Vec<u8> {
|
||||
|
|
Loading…
Reference in New Issue