2017-06-01 01:52:43 +03:00
|
|
|
// Copyright 2016 The Grin Developers
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
//! Utilities to check the status of all the outputs we have stored in
|
|
|
|
//! the wallet storage and update them.
|
|
|
|
|
|
|
|
use api;
|
2017-09-22 19:44:12 +03:00
|
|
|
use extkey::ExtendedKey;
|
2017-06-01 01:52:43 +03:00
|
|
|
use secp::{self, pedersen};
|
2017-09-22 19:44:12 +03:00
|
|
|
use types::*;
|
2017-06-01 01:52:43 +03:00
|
|
|
use util;
|
|
|
|
|
2017-09-22 19:44:12 +03:00
|
|
|
|
2017-09-29 21:44:25 +03:00
|
|
|
fn refresh_output(out: &mut OutputData, api_out: Option<api::Output>, tip: &api::Tip) {
|
2017-09-22 19:44:12 +03:00
|
|
|
if let Some(api_out) = api_out {
|
|
|
|
out.height = api_out.height;
|
|
|
|
out.lock_height = api_out.lock_height;
|
|
|
|
|
2017-09-24 07:40:31 +03:00
|
|
|
if out.status == OutputStatus::Locked {
|
|
|
|
// leave it Locked locally for now
|
|
|
|
} else if api_out.lock_height >= tip.height {
|
2017-09-22 19:44:12 +03:00
|
|
|
out.status = OutputStatus::Immature;
|
|
|
|
} else {
|
|
|
|
out.status = OutputStatus::Unspent;
|
|
|
|
}
|
2017-09-29 21:44:25 +03:00
|
|
|
} else if vec![OutputStatus::Unspent, OutputStatus::Locked].contains(&out.status) {
|
2017-09-22 19:44:12 +03:00
|
|
|
out.status = OutputStatus::Spent;
|
|
|
|
}
|
|
|
|
}
|
2017-06-01 01:52:43 +03:00
|
|
|
|
2017-06-06 23:18:16 +03:00
|
|
|
/// Goes through the list of outputs that haven't been spent yet and check
|
|
|
|
/// with a node whether their status has changed.
|
2017-09-29 21:44:25 +03:00
|
|
|
pub fn refresh_outputs(config: &WalletConfig, ext_key: &ExtendedKey) -> Result<(), Error> {
|
2017-06-01 01:52:43 +03:00
|
|
|
let secp = secp::Secp256k1::with_caps(secp::ContextFlag::Commit);
|
2017-09-22 19:44:12 +03:00
|
|
|
let tip = get_tip(config)?;
|
2017-06-01 01:52:43 +03:00
|
|
|
|
2017-09-22 19:44:12 +03:00
|
|
|
WalletData::with_wallet(&config.data_file_dir, |wallet_data| {
|
2017-06-15 07:42:58 +03:00
|
|
|
// check each output that's not spent
|
2017-09-29 21:44:25 +03:00
|
|
|
for mut out in wallet_data.outputs.iter_mut().filter(|out| {
|
|
|
|
out.status != OutputStatus::Spent
|
|
|
|
})
|
|
|
|
{
|
2017-08-10 03:54:10 +03:00
|
|
|
|
2017-09-22 19:44:12 +03:00
|
|
|
// 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();
|
2017-06-15 07:42:58 +03:00
|
|
|
|
2017-09-22 19:44:12 +03:00
|
|
|
match get_output_by_commitment(config, commitment) {
|
|
|
|
Ok(api_out) => refresh_output(&mut out, api_out, &tip),
|
|
|
|
Err(_) => {
|
2017-09-29 21:44:25 +03:00
|
|
|
// TODO find error with connection and return
|
|
|
|
// error!("Error contacting server node at {}. Is it running?",
|
|
|
|
// config.check_node_api_http_addr);
|
2017-06-13 02:41:27 +03:00
|
|
|
}
|
2017-06-01 01:52:43 +03:00
|
|
|
}
|
|
|
|
}
|
2017-09-22 19:44:12 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_tip(config: &WalletConfig) -> Result<api::Tip, Error> {
|
2017-09-24 07:40:31 +03:00
|
|
|
let url = format!("{}/v1/chain/1", config.check_node_api_http_addr);
|
2017-09-29 21:44:25 +03:00
|
|
|
api::client::get::<api::Tip>(url.as_str()).map_err(|e| Error::Node(e))
|
2017-06-01 01:52:43 +03:00
|
|
|
}
|
|
|
|
|
2017-09-29 21:44:25 +03:00
|
|
|
// queries a reachable node for a given output, checking whether it's been
|
|
|
|
// confirmed
|
2017-09-22 19:44:12 +03:00
|
|
|
fn get_output_by_commitment(
|
|
|
|
config: &WalletConfig,
|
2017-09-29 21:44:25 +03:00
|
|
|
commit: pedersen::Commitment,
|
2017-09-22 19:44:12 +03:00
|
|
|
) -> 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)),
|
|
|
|
}
|
2017-06-01 01:52:43 +03:00
|
|
|
}
|