Wallet now supports coinbase maturity (#130)

This commit is contained in:
AntiochP
2017-09-22 12:44:12 -04:00
committed by Ignotus Peverell
parent 139af79509
commit dbc4e10cec
8 changed files with 182 additions and 69 deletions
+56 -35
View File
@@ -16,57 +16,78 @@
//! the wallet storage and update them.
use api;
use core::core::Output;
use extkey::ExtendedKey;
use secp::{self, pedersen};
use types::*;
use util;
use extkey::ExtendedKey;
use types::{WalletConfig, OutputStatus, WalletData};
fn refresh_output(
out: &mut OutputData,
api_out: Option<api::Output>,
tip: &api::Tip,
) {
if let Some(api_out) = api_out {
out.height = api_out.height;
out.lock_height = api_out.lock_height;
if api_out.lock_height > tip.height {
out.status = OutputStatus::Immature;
} else {
out.status = OutputStatus::Unspent;
}
} else if out.status == OutputStatus::Unspent {
out.status = OutputStatus::Spent;
}
}
/// Goes through the list of outputs that haven't been spent yet and check
/// with a node whether their status has changed.
pub fn refresh_outputs(config: &WalletConfig, ext_key: &ExtendedKey) {
pub fn refresh_outputs(config: &WalletConfig, ext_key: &ExtendedKey) -> Result<(), Error>{
let secp = secp::Secp256k1::with_caps(secp::ContextFlag::Commit);
let tip = get_tip(config)?;
// operate within a lock on wallet data
let _ = WalletData::with_wallet(&config.data_file_dir, |wallet_data| {
WalletData::with_wallet(&config.data_file_dir, |wallet_data| {
// check each output that's not spent
for out in &mut wallet_data.outputs {
if out.status != OutputStatus::Spent {
// figure out the commitment
let key = ext_key.derive(&secp, out.n_child).unwrap();
let commitment = secp.commit(out.value, key.key).unwrap();
for mut out in wallet_data.outputs
.iter_mut()
.filter(|out| out.status != OutputStatus::Spent) {
// TODO check the pool for unconfirmed
// figure out the commitment
// TODO check the pool for unconfirmed
let key = ext_key.derive(&secp, out.n_child).unwrap();
let commitment = secp.commit(out.value, key.key).unwrap();
let out_res = get_output_by_commitment(config, commitment);
if out_res.is_ok() {
// output is known, it's a new utxo
out.status = OutputStatus::Unspent;
} else if out.status == OutputStatus::Unspent {
// a UTXO we can't find anymore has been spent
if let Err(api::Error::NotFound) = out_res {
out.status = OutputStatus::Spent;
}
} else {
match get_output_by_commitment(config, commitment) {
Ok(api_out) => refresh_output(&mut out, api_out, &tip),
Err(_) => {
//TODO find error with connection and return
//error!("Error contacting server node at {}. Is it running?", config.check_node_api_http_addr);
}
}
}
});
})
}
// queries a reachable node for a given output, checking whether it's been
// confirmed
fn get_output_by_commitment(config: &WalletConfig,
commit: pedersen::Commitment)
-> Result<Output, api::Error> {
let url = format!("{}/v1/chain/utxo/{}",
config.check_node_api_http_addr,
util::to_hex(commit.as_ref().to_vec()));
api::client::get::<Output>(url.as_str())
fn get_tip(config: &WalletConfig) -> Result<api::Tip, Error> {
let url = format!("{}/v1/chain", config.check_node_api_http_addr);
api::client::get::<api::Tip>(url.as_str())
.map_err(|e| Error::Node(e))
}
// queries a reachable node for a given output, checking whether it's been confirmed
fn get_output_by_commitment(
config: &WalletConfig,
commit: pedersen::Commitment
) -> Result<Option<api::Output>, Error> {
let url = format!(
"{}/v1/chain/utxo/{}",
config.check_node_api_http_addr,
util::to_hex(commit.as_ref().to_vec())
);
match api::client::get::<api::Output>(url.as_str()) {
Ok(out) => Ok(Some(out)),
Err(api::Error::NotFound) => Ok(None),
Err(e) => Err(Error::Node(e)),
}
}
+4
View File
@@ -169,6 +169,8 @@ fn receive_coinbase(config: &WalletConfig, ext_key: &ExtendedKey, amount: u64) -
n_child: coinbase_key.n_child,
value: amount,
status: OutputStatus::Unconfirmed,
height: 0,
lock_height: 0,
});
debug!("Using child {} for a new coinbase output.",
coinbase_key.n_child);
@@ -207,6 +209,8 @@ fn receive_transaction(config: &WalletConfig,
n_child: out_key.n_child,
value: amount,
status: OutputStatus::Unconfirmed,
height: 0,
lock_height: 0,
});
debug!("Using child {} for a new transaction output.",
+5 -1
View File
@@ -28,7 +28,7 @@ use api;
/// UTXOs. The destination can be "stdout" (for command line) or a URL to the
/// recipients wallet receiver (to be implemented).
pub fn issue_send_tx(config: &WalletConfig, ext_key: &ExtendedKey, amount: u64, dest: String) -> Result<(), Error> {
checker::refresh_outputs(&config, ext_key);
let _ = checker::refresh_outputs(&config, ext_key);
let (tx, blind_sum) = build_send_tx(config, ext_key, amount)?;
let json_tx = partial_tx_to_json(amount, blind_sum, tx);
@@ -82,6 +82,8 @@ fn build_send_tx(config: &WalletConfig, ext_key: &ExtendedKey, amount: u64) -> R
n_child: change_key.n_child,
value: change as u64,
status: OutputStatus::Unconfirmed,
height: 0,
lock_height: 0,
});
for mut coin in coins {
coin.lock();
@@ -118,6 +120,8 @@ mod test {
n_child: out_key.n_child,
value: 5,
status: OutputStatus::Unconfirmed,
height: 0,
lock_height: 0,
};
let (tx, _) = transaction(vec![output(coin.value, out_key.key)]).unwrap();
+4
View File
@@ -108,6 +108,7 @@ impl Default for WalletConfig {
pub enum OutputStatus {
Unconfirmed,
Unspent,
Immature,
Locked,
Spent,
}
@@ -125,6 +126,9 @@ pub struct OutputData {
pub value: u64,
/// Current status of the output
pub status: OutputStatus,
/// Height of the output
pub height: u64,
pub lock_height: u64,
}
impl OutputData {