2018-03-02 23:47:27 +03:00
|
|
|
// Copyright 2018 The Grin Developers
|
2017-06-01 01:52:43 +03:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2018-03-04 03:19:54 +03:00
|
|
|
use failure::ResultExt;
|
2018-06-26 20:24:40 +03:00
|
|
|
use std::collections::HashMap;
|
2017-10-25 20:57:48 +03:00
|
|
|
|
2018-06-06 17:36:29 +03:00
|
|
|
use core::consensus::reward;
|
|
|
|
use core::core::{Output, TxKernel};
|
2018-06-14 15:16:14 +03:00
|
|
|
use core::{global, ser};
|
2018-06-08 08:21:54 +03:00
|
|
|
use keychain::{Identifier, Keychain};
|
2018-06-06 17:36:29 +03:00
|
|
|
use libtx::reward;
|
2018-06-14 19:02:05 +03:00
|
|
|
use libwallet;
|
2018-06-01 17:06:59 +03:00
|
|
|
use libwallet::error::{Error, ErrorKind};
|
2018-06-06 17:36:29 +03:00
|
|
|
use libwallet::internal::keys;
|
2018-06-26 20:24:40 +03:00
|
|
|
use libwallet::types::{
|
|
|
|
BlockFees, CbData, OutputData, OutputStatus, WalletBackend, WalletClient, WalletInfo,
|
|
|
|
};
|
2018-06-13 23:58:45 +03:00
|
|
|
use util::secp::pedersen;
|
2018-06-14 15:16:14 +03:00
|
|
|
use util::{self, LOGGER};
|
2017-06-01 01:52:43 +03:00
|
|
|
|
2018-06-06 17:36:29 +03:00
|
|
|
/// Retrieve all of the outputs (doesn't attempt to update from node)
|
2018-06-08 08:21:54 +03:00
|
|
|
pub fn retrieve_outputs<T, K>(wallet: &mut T, show_spent: bool) -> Result<Vec<OutputData>, Error>
|
|
|
|
where
|
|
|
|
T: WalletBackend<K>,
|
|
|
|
K: Keychain,
|
|
|
|
{
|
2018-06-06 17:36:29 +03:00
|
|
|
let root_key_id = wallet.keychain().clone().root_key_id();
|
|
|
|
|
|
|
|
// just read the wallet here, no need for a write lock
|
2018-06-22 11:08:06 +03:00
|
|
|
let mut outputs = wallet
|
|
|
|
.iter()
|
|
|
|
.filter(|out| out.root_key_id == root_key_id)
|
|
|
|
.filter(|out| {
|
|
|
|
if show_spent {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
out.status != OutputStatus::Spent
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
outputs.sort_by_key(|out| out.n_child);
|
2018-06-06 17:36:29 +03:00
|
|
|
Ok(outputs)
|
|
|
|
}
|
|
|
|
|
2018-06-01 17:06:59 +03:00
|
|
|
/// Refreshes the outputs in a wallet with the latest information
|
|
|
|
/// from a node
|
2018-06-08 08:21:54 +03:00
|
|
|
pub fn refresh_outputs<T, K>(wallet: &mut T) -> Result<(), Error>
|
2018-05-30 19:48:32 +03:00
|
|
|
where
|
2018-06-08 08:21:54 +03:00
|
|
|
T: WalletBackend<K> + WalletClient,
|
|
|
|
K: Keychain,
|
2018-05-30 19:48:32 +03:00
|
|
|
{
|
2018-06-26 20:24:40 +03:00
|
|
|
let height = wallet.get_chain_height()?;
|
2018-06-07 17:04:21 +03:00
|
|
|
refresh_output_state(wallet, height)?;
|
2018-06-22 11:08:06 +03:00
|
|
|
Ok(())
|
2017-09-22 19:44:12 +03:00
|
|
|
}
|
|
|
|
|
2018-05-16 15:18:09 +03:00
|
|
|
/// build a local map of wallet outputs keyed by commit
|
|
|
|
/// and a list of outputs we want to query the node for
|
2018-06-08 08:21:54 +03:00
|
|
|
pub fn map_wallet_outputs<T, K>(
|
2018-05-30 19:48:32 +03:00
|
|
|
wallet: &mut T,
|
|
|
|
) -> Result<HashMap<pedersen::Commitment, Identifier>, Error>
|
|
|
|
where
|
2018-06-08 08:21:54 +03:00
|
|
|
T: WalletBackend<K>,
|
|
|
|
K: Keychain,
|
2018-05-30 19:48:32 +03:00
|
|
|
{
|
2018-01-17 06:03:40 +03:00
|
|
|
let mut wallet_outputs: HashMap<pedersen::Commitment, Identifier> = HashMap::new();
|
2018-06-22 11:08:06 +03:00
|
|
|
let keychain = wallet.keychain().clone();
|
|
|
|
let root_key_id = keychain.root_key_id().clone();
|
|
|
|
let unspents = wallet
|
|
|
|
.iter()
|
|
|
|
.filter(|x| x.root_key_id == root_key_id && x.status != OutputStatus::Spent);
|
|
|
|
for out in unspents {
|
|
|
|
let commit = keychain.commit_with_key_index(out.value, out.n_child)?;
|
|
|
|
wallet_outputs.insert(commit, out.key_id.clone());
|
|
|
|
}
|
2018-05-16 15:18:09 +03:00
|
|
|
Ok(wallet_outputs)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Apply refreshed API output data to the wallet
|
2018-06-08 08:21:54 +03:00
|
|
|
pub fn apply_api_outputs<T, K>(
|
2018-05-30 19:48:32 +03:00
|
|
|
wallet: &mut T,
|
2018-05-16 15:18:09 +03:00
|
|
|
wallet_outputs: &HashMap<pedersen::Commitment, Identifier>,
|
2018-06-07 17:04:21 +03:00
|
|
|
api_outputs: &HashMap<pedersen::Commitment, String>,
|
2018-06-14 19:02:05 +03:00
|
|
|
height: u64,
|
|
|
|
) -> Result<(), libwallet::Error>
|
2018-05-30 19:48:32 +03:00
|
|
|
where
|
2018-06-08 08:21:54 +03:00
|
|
|
T: WalletBackend<K>,
|
|
|
|
K: Keychain,
|
2018-05-30 19:48:32 +03:00
|
|
|
{
|
2018-05-24 03:14:34 +03:00
|
|
|
// now for each commit, find the output in the wallet and the corresponding
|
|
|
|
// api output (if it exists) and refresh it in-place in the wallet.
|
2018-05-16 15:18:09 +03:00
|
|
|
// Note: minimizing the time we spend holding the wallet lock.
|
2018-06-22 11:08:06 +03:00
|
|
|
{
|
|
|
|
let mut batch = wallet.batch()?;
|
|
|
|
for (commit, id) in wallet_outputs.iter() {
|
|
|
|
if let Ok(mut output) = batch.get(id) {
|
2018-05-16 15:18:09 +03:00
|
|
|
match api_outputs.get(&commit) {
|
2018-06-22 11:08:06 +03:00
|
|
|
Some(_) => output.mark_unspent(),
|
|
|
|
None => output.mark_spent(),
|
2018-05-16 15:18:09 +03:00
|
|
|
};
|
2018-06-27 18:57:40 +03:00
|
|
|
batch.save(output)?;
|
2018-05-16 15:18:09 +03:00
|
|
|
}
|
|
|
|
}
|
2018-06-27 18:57:40 +03:00
|
|
|
{
|
|
|
|
let details = batch.details();
|
|
|
|
details.last_confirmed_height = height;
|
|
|
|
}
|
2018-06-22 11:08:06 +03:00
|
|
|
batch.commit()?;
|
|
|
|
}
|
2018-06-14 19:02:05 +03:00
|
|
|
Ok(())
|
2018-05-16 15:18:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds a single api query to retrieve the latest output data from the node.
|
|
|
|
/// So we can refresh the local wallet outputs.
|
2018-06-08 08:21:54 +03:00
|
|
|
fn refresh_output_state<T, K>(wallet: &mut T, height: u64) -> Result<(), Error>
|
2018-05-30 19:48:32 +03:00
|
|
|
where
|
2018-06-08 08:21:54 +03:00
|
|
|
T: WalletBackend<K> + WalletClient,
|
|
|
|
K: Keychain,
|
2018-05-30 19:48:32 +03:00
|
|
|
{
|
2018-05-16 15:18:09 +03:00
|
|
|
debug!(LOGGER, "Refreshing wallet outputs");
|
2018-01-17 06:03:40 +03:00
|
|
|
|
2018-05-16 15:18:09 +03:00
|
|
|
// build a local map of wallet outputs keyed by commit
|
|
|
|
// and a list of outputs we want to query the node for
|
2018-05-30 19:48:32 +03:00
|
|
|
let wallet_outputs = map_wallet_outputs(wallet)?;
|
2018-05-24 03:14:34 +03:00
|
|
|
|
2018-06-07 17:04:21 +03:00
|
|
|
let wallet_output_keys = wallet_outputs.keys().map(|commit| commit.clone()).collect();
|
2018-05-24 03:14:34 +03:00
|
|
|
|
2018-06-26 20:24:40 +03:00
|
|
|
let api_outputs = wallet.get_outputs_from_node(wallet_output_keys)?;
|
2018-06-14 19:02:05 +03:00
|
|
|
apply_api_outputs(wallet, &wallet_outputs, &api_outputs, height)?;
|
2018-06-07 17:04:21 +03:00
|
|
|
clean_old_unconfirmed(wallet, height)?;
|
2018-05-16 15:18:09 +03:00
|
|
|
Ok(())
|
2018-01-17 06:03:40 +03:00
|
|
|
}
|
|
|
|
|
2018-06-08 08:21:54 +03:00
|
|
|
fn clean_old_unconfirmed<T, K>(wallet: &mut T, height: u64) -> Result<(), Error>
|
2018-05-30 19:48:32 +03:00
|
|
|
where
|
2018-06-08 08:21:54 +03:00
|
|
|
T: WalletBackend<K>,
|
|
|
|
K: Keychain,
|
2018-05-30 19:48:32 +03:00
|
|
|
{
|
2018-06-07 17:04:21 +03:00
|
|
|
if height < 500 {
|
2018-05-24 18:27:26 +03:00
|
|
|
return Ok(());
|
|
|
|
}
|
2018-06-22 11:08:06 +03:00
|
|
|
let mut ids_to_del = vec![];
|
|
|
|
for out in wallet.iter() {
|
|
|
|
if out.status == OutputStatus::Unconfirmed && out.height > 0 && out.height < height - 500 {
|
|
|
|
ids_to_del.push(out.key_id.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let mut batch = wallet.batch()?;
|
|
|
|
for id in ids_to_del {
|
|
|
|
batch.delete(&id);
|
|
|
|
}
|
|
|
|
batch.commit()?;
|
|
|
|
Ok(())
|
2018-05-24 03:14:34 +03:00
|
|
|
}
|
|
|
|
|
2018-06-13 19:03:34 +03:00
|
|
|
/// Retrieve summary info about the wallet
|
2018-06-13 23:58:45 +03:00
|
|
|
/// caller should refresh first if desired
|
2018-06-14 19:02:05 +03:00
|
|
|
pub fn retrieve_info<T, K>(wallet: &mut T) -> Result<WalletInfo, Error>
|
2018-06-01 17:06:59 +03:00
|
|
|
where
|
2018-06-08 08:21:54 +03:00
|
|
|
T: WalletBackend<K> + WalletClient,
|
|
|
|
K: Keychain,
|
2018-06-01 17:06:59 +03:00
|
|
|
{
|
2018-06-22 11:08:06 +03:00
|
|
|
let current_height = wallet.details().last_confirmed_height;
|
|
|
|
let keychain = wallet.keychain().clone();
|
|
|
|
let outputs = wallet
|
|
|
|
.iter()
|
|
|
|
.filter(|out| out.root_key_id == keychain.root_key_id());
|
|
|
|
|
|
|
|
let mut unspent_total = 0;
|
|
|
|
let mut immature_total = 0;
|
|
|
|
let mut unconfirmed_total = 0;
|
|
|
|
let mut locked_total = 0;
|
|
|
|
for out in outputs {
|
|
|
|
if out.status == OutputStatus::Unspent && out.lock_height <= current_height {
|
|
|
|
unspent_total += out.value;
|
|
|
|
}
|
|
|
|
if out.status == OutputStatus::Unspent && out.lock_height > current_height {
|
|
|
|
immature_total += out.value;
|
|
|
|
}
|
|
|
|
if out.status == OutputStatus::Unconfirmed && !out.is_coinbase {
|
|
|
|
unconfirmed_total += out.value;
|
2018-06-01 17:06:59 +03:00
|
|
|
}
|
2018-06-22 11:08:06 +03:00
|
|
|
if out.status == OutputStatus::Locked {
|
|
|
|
locked_total += out.value;
|
|
|
|
}
|
|
|
|
}
|
2018-06-01 17:06:59 +03:00
|
|
|
|
2018-06-22 11:08:06 +03:00
|
|
|
Ok(WalletInfo {
|
|
|
|
last_confirmed_height: current_height,
|
|
|
|
total: unspent_total + unconfirmed_total + immature_total,
|
|
|
|
amount_awaiting_confirmation: unconfirmed_total,
|
|
|
|
amount_immature: immature_total,
|
|
|
|
amount_locked: locked_total,
|
|
|
|
amount_currently_spendable: unspent_total,
|
|
|
|
})
|
2018-06-01 17:06:59 +03:00
|
|
|
}
|
2018-06-06 17:36:29 +03:00
|
|
|
|
|
|
|
/// Build a coinbase output and insert into wallet
|
2018-06-08 08:21:54 +03:00
|
|
|
pub fn build_coinbase<T, K>(wallet: &mut T, block_fees: &BlockFees) -> Result<CbData, Error>
|
2018-06-06 17:36:29 +03:00
|
|
|
where
|
2018-06-08 08:21:54 +03:00
|
|
|
T: WalletBackend<K>,
|
|
|
|
K: Keychain,
|
2018-06-06 17:36:29 +03:00
|
|
|
{
|
|
|
|
let (out, kern, block_fees) = receive_coinbase(wallet, block_fees).context(ErrorKind::Node)?;
|
|
|
|
|
|
|
|
let out_bin = ser::ser_vec(&out).context(ErrorKind::Node)?;
|
|
|
|
|
|
|
|
let kern_bin = ser::ser_vec(&kern).context(ErrorKind::Node)?;
|
|
|
|
|
|
|
|
let key_id_bin = match block_fees.key_id {
|
|
|
|
Some(key_id) => ser::ser_vec(&key_id).context(ErrorKind::Node)?,
|
|
|
|
None => vec![],
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(CbData {
|
|
|
|
output: util::to_hex(out_bin),
|
|
|
|
kernel: util::to_hex(kern_bin),
|
|
|
|
key_id: util::to_hex(key_id_bin),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: Split up the output creation and the wallet insertion
|
|
|
|
/// Build a coinbase output and the corresponding kernel
|
2018-06-08 08:21:54 +03:00
|
|
|
pub fn receive_coinbase<T, K>(
|
2018-06-06 17:36:29 +03:00
|
|
|
wallet: &mut T,
|
|
|
|
block_fees: &BlockFees,
|
|
|
|
) -> Result<(Output, TxKernel, BlockFees), Error>
|
|
|
|
where
|
2018-06-08 08:21:54 +03:00
|
|
|
T: WalletBackend<K>,
|
|
|
|
K: Keychain,
|
2018-06-06 17:36:29 +03:00
|
|
|
{
|
|
|
|
let root_key_id = wallet.keychain().root_key_id();
|
|
|
|
|
|
|
|
let height = block_fees.height;
|
|
|
|
let lock_height = height + global::coinbase_maturity();
|
2018-06-22 11:08:06 +03:00
|
|
|
let key_id = block_fees.key_id();
|
2018-06-06 17:36:29 +03:00
|
|
|
|
2018-06-22 11:08:06 +03:00
|
|
|
let (key_id, derivation) = match key_id {
|
|
|
|
Some(key_id) => keys::retrieve_existing_key(wallet, key_id)?,
|
|
|
|
None => keys::next_available_key(wallet)?,
|
|
|
|
};
|
2018-06-06 17:36:29 +03:00
|
|
|
|
2018-06-22 11:08:06 +03:00
|
|
|
{
|
|
|
|
// Now acquire the wallet lock and write the new output.
|
|
|
|
let mut batch = wallet.batch()?;
|
2018-06-29 04:56:07 +03:00
|
|
|
batch.save(OutputData {
|
|
|
|
root_key_id: root_key_id.clone(),
|
|
|
|
key_id: key_id.clone(),
|
|
|
|
n_child: derivation,
|
|
|
|
value: reward(block_fees.fees),
|
|
|
|
status: OutputStatus::Unconfirmed,
|
|
|
|
height: height,
|
|
|
|
lock_height: lock_height,
|
|
|
|
is_coinbase: true,
|
|
|
|
});
|
2018-06-22 11:08:06 +03:00
|
|
|
batch.commit()?;
|
|
|
|
}
|
2018-06-06 17:36:29 +03:00
|
|
|
|
|
|
|
debug!(
|
|
|
|
LOGGER,
|
|
|
|
"receive_coinbase: built candidate output - {:?}, {}",
|
|
|
|
key_id.clone(),
|
|
|
|
derivation,
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut block_fees = block_fees.clone();
|
|
|
|
block_fees.key_id = Some(key_id.clone());
|
|
|
|
|
|
|
|
debug!(LOGGER, "receive_coinbase: {:?}", block_fees);
|
|
|
|
|
|
|
|
let (out, kern) = reward::output(
|
2018-06-08 08:21:54 +03:00
|
|
|
wallet.keychain(),
|
2018-06-06 17:36:29 +03:00
|
|
|
&key_id,
|
|
|
|
block_fees.fees,
|
|
|
|
block_fees.height,
|
|
|
|
).unwrap();
|
|
|
|
/* .context(ErrorKind::Keychain)?; */
|
|
|
|
Ok((out, kern, block_fees))
|
|
|
|
}
|