2018-05-16 15:18:09 +03:00
|
|
|
// Copyright 2018 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.
|
|
|
|
|
|
|
|
//! Common functions to facilitate wallet, walletlib and transaction testing
|
2018-06-14 15:16:14 +03:00
|
|
|
use std::collections::HashMap;
|
2018-06-21 04:30:22 +03:00
|
|
|
use std::collections::hash_map::Entry;
|
2018-05-16 15:18:09 +03:00
|
|
|
|
|
|
|
extern crate grin_api as api;
|
|
|
|
extern crate grin_chain as chain;
|
|
|
|
extern crate grin_core as core;
|
|
|
|
extern crate grin_keychain as keychain;
|
|
|
|
extern crate grin_wallet as wallet;
|
|
|
|
extern crate time;
|
|
|
|
|
|
|
|
use chain::Chain;
|
|
|
|
use core::core::hash::Hashed;
|
2018-05-21 18:28:11 +03:00
|
|
|
use core::core::{Output, OutputFeatures, OutputIdentifier, Transaction, TxKernel};
|
2018-05-16 15:18:09 +03:00
|
|
|
use core::{consensus, global, pow};
|
2018-06-08 08:21:54 +03:00
|
|
|
use keychain::ExtKeychain;
|
2018-06-22 16:04:51 +03:00
|
|
|
use wallet::WalletConfig;
|
2018-06-22 16:05:03 +03:00
|
|
|
use wallet::file_wallet::FileWallet;
|
2018-06-06 17:36:29 +03:00
|
|
|
use wallet::libwallet::internal::updater;
|
2018-06-29 04:56:07 +03:00
|
|
|
use wallet::libwallet::types::{BlockFees, BlockIdentifier, OutputStatus,
|
2018-06-14 15:16:14 +03:00
|
|
|
WalletBackend};
|
2018-06-01 17:06:59 +03:00
|
|
|
use wallet::libwallet::{Error, ErrorKind};
|
2018-05-16 15:18:09 +03:00
|
|
|
|
2018-06-07 17:04:21 +03:00
|
|
|
use util;
|
2018-05-16 15:18:09 +03:00
|
|
|
use util::secp::pedersen;
|
|
|
|
|
2018-05-21 18:28:11 +03:00
|
|
|
/// Mostly for testing, refreshes output state against a local chain instance
|
|
|
|
/// instead of via an http API call
|
2018-06-08 08:21:54 +03:00
|
|
|
pub fn refresh_output_state_local<T, K>(wallet: &mut T, chain: &chain::Chain) -> Result<(), Error>
|
|
|
|
where
|
|
|
|
T: WalletBackend<K>,
|
|
|
|
K: keychain::Keychain,
|
|
|
|
{
|
2018-06-01 17:06:59 +03:00
|
|
|
let wallet_outputs = updater::map_wallet_outputs(wallet)?;
|
2018-05-24 18:27:26 +03:00
|
|
|
let chain_outputs: Vec<Option<api::Output>> = wallet_outputs
|
2018-05-16 15:18:09 +03:00
|
|
|
.keys()
|
|
|
|
.map(|k| match get_output_local(chain, &k) {
|
2018-05-24 18:27:26 +03:00
|
|
|
Err(_) => None,
|
|
|
|
Ok(k) => Some(k),
|
2018-05-16 15:18:09 +03:00
|
|
|
})
|
|
|
|
.collect();
|
2018-06-07 17:04:21 +03:00
|
|
|
let mut api_outputs: HashMap<pedersen::Commitment, String> = HashMap::new();
|
2018-05-16 15:18:09 +03:00
|
|
|
for out in chain_outputs {
|
2018-05-24 18:27:26 +03:00
|
|
|
match out {
|
|
|
|
Some(o) => {
|
2018-06-07 17:04:21 +03:00
|
|
|
api_outputs.insert(o.commit.commit(), util::to_hex(o.commit.to_vec()));
|
2018-05-24 18:27:26 +03:00
|
|
|
}
|
|
|
|
None => {}
|
|
|
|
}
|
2018-05-16 15:18:09 +03:00
|
|
|
}
|
2018-06-14 19:02:05 +03:00
|
|
|
let height = chain.head().unwrap().height;
|
|
|
|
updater::apply_api_outputs(wallet, &wallet_outputs, &api_outputs, height)?;
|
2018-05-16 15:18:09 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the spendable wallet balance from the local chain
|
2018-05-21 18:28:11 +03:00
|
|
|
/// (0:total, 1:amount_awaiting_confirmation, 2:confirmed but locked,
|
|
|
|
/// 3:currently_spendable, 4:locked total) TODO: Should be a wallet lib
|
|
|
|
/// function with nicer return values
|
2018-06-08 08:21:54 +03:00
|
|
|
pub fn get_wallet_balances<T, K>(
|
2018-05-30 19:48:32 +03:00
|
|
|
wallet: &mut T,
|
2018-05-16 15:18:09 +03:00
|
|
|
height: u64,
|
2018-06-08 08:21:54 +03:00
|
|
|
) -> Result<(u64, u64, u64, u64, u64), Error>
|
|
|
|
where
|
|
|
|
T: WalletBackend<K>,
|
|
|
|
K: keychain::Keychain,
|
|
|
|
{
|
2018-06-22 16:04:51 +03:00
|
|
|
let mut unspent_total = 0;
|
|
|
|
let mut unspent_but_locked_total = 0;
|
|
|
|
let mut unconfirmed_total = 0;
|
|
|
|
let mut locked_total = 0;
|
|
|
|
let keychain = wallet.keychain().clone();
|
2018-06-22 16:05:03 +03:00
|
|
|
for out in wallet
|
|
|
|
.iter()
|
2018-06-22 16:04:51 +03:00
|
|
|
.filter(|out| out.root_key_id == keychain.root_key_id())
|
|
|
|
{
|
|
|
|
if out.status == OutputStatus::Unspent {
|
|
|
|
unspent_total += out.value;
|
|
|
|
if out.lock_height > height {
|
|
|
|
unspent_but_locked_total += out.value;
|
2018-05-16 15:18:09 +03:00
|
|
|
}
|
|
|
|
}
|
2018-06-22 16:04:51 +03:00
|
|
|
if out.status == OutputStatus::Unconfirmed && !out.is_coinbase {
|
|
|
|
unconfirmed_total += out.value;
|
|
|
|
}
|
|
|
|
if out.status == OutputStatus::Locked {
|
|
|
|
locked_total += out.value;
|
|
|
|
}
|
|
|
|
}
|
2018-05-16 15:18:09 +03:00
|
|
|
|
2018-06-22 16:04:51 +03:00
|
|
|
Ok((
|
|
|
|
unspent_total + unconfirmed_total, //total
|
|
|
|
unconfirmed_total, //amount_awaiting_confirmation
|
|
|
|
unspent_but_locked_total, // confirmed but locked
|
|
|
|
unspent_total - unspent_but_locked_total, // currently spendable
|
|
|
|
locked_total, // locked total
|
|
|
|
))
|
2018-05-16 15:18:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get an output from the chain locally and present it back as an API output
|
|
|
|
fn get_output_local(
|
|
|
|
chain: &chain::Chain,
|
|
|
|
commit: &pedersen::Commitment,
|
|
|
|
) -> Result<api::Output, Error> {
|
|
|
|
let outputs = [
|
|
|
|
OutputIdentifier::new(OutputFeatures::DEFAULT_OUTPUT, commit),
|
|
|
|
OutputIdentifier::new(OutputFeatures::COINBASE_OUTPUT, commit),
|
|
|
|
];
|
|
|
|
|
|
|
|
for x in outputs.iter() {
|
|
|
|
if let Ok(_) = chain.is_unspent(&x) {
|
|
|
|
return Ok(api::Output::new(&commit));
|
|
|
|
}
|
|
|
|
}
|
2018-06-01 17:06:59 +03:00
|
|
|
Err(ErrorKind::GenericError(
|
|
|
|
"Can't get output from local instance of chain",
|
|
|
|
))?
|
2018-05-16 15:18:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a block with a given reward to the chain and mines it
|
|
|
|
pub fn add_block_with_reward(chain: &Chain, txs: Vec<&Transaction>, reward: (Output, TxKernel)) {
|
|
|
|
let prev = chain.head_header().unwrap();
|
|
|
|
let difficulty = consensus::next_difficulty(chain.difficulty_iter()).unwrap();
|
2018-05-30 23:57:13 +03:00
|
|
|
let mut b = core::core::Block::new(
|
|
|
|
&prev,
|
|
|
|
txs.into_iter().cloned().collect(),
|
|
|
|
difficulty.clone(),
|
|
|
|
reward,
|
|
|
|
).unwrap();
|
2018-05-16 15:18:09 +03:00
|
|
|
b.header.timestamp = prev.timestamp + time::Duration::seconds(60);
|
2018-06-22 11:33:42 +03:00
|
|
|
chain.set_txhashset_roots(&mut b, false).unwrap();
|
2018-05-16 15:18:09 +03:00
|
|
|
pow::pow_size(
|
|
|
|
&mut b.header,
|
|
|
|
difficulty,
|
|
|
|
global::proofsize(),
|
|
|
|
global::sizeshift(),
|
|
|
|
).unwrap();
|
|
|
|
chain.process_block(b, chain::Options::MINE).unwrap();
|
|
|
|
chain.validate(false).unwrap();
|
|
|
|
}
|
|
|
|
|
2018-05-21 18:28:11 +03:00
|
|
|
/// adds a reward output to a wallet, includes that reward in a block, mines
|
|
|
|
/// the block and adds it to the chain, with option transactions included.
|
2018-05-16 15:18:09 +03:00
|
|
|
/// Helpful for building up precise wallet balances for testing.
|
2018-06-08 08:21:54 +03:00
|
|
|
pub fn award_block_to_wallet<T, K>(chain: &Chain, txs: Vec<&Transaction>, wallet: &mut T)
|
|
|
|
where
|
|
|
|
T: WalletBackend<K>,
|
|
|
|
K: keychain::Keychain,
|
|
|
|
{
|
2018-05-16 15:18:09 +03:00
|
|
|
let prev = chain.head_header().unwrap();
|
|
|
|
let fee_amt = txs.iter().map(|tx| tx.fee()).sum();
|
|
|
|
let fees = BlockFees {
|
|
|
|
fees: fee_amt,
|
|
|
|
key_id: None,
|
|
|
|
height: prev.height + 1,
|
|
|
|
};
|
2018-06-06 17:36:29 +03:00
|
|
|
let coinbase_tx = wallet::libwallet::internal::updater::receive_coinbase(wallet, &fees);
|
2018-05-16 15:18:09 +03:00
|
|
|
let (coinbase_tx, fees) = match coinbase_tx {
|
|
|
|
Ok(t) => ((t.0, t.1), t.2),
|
|
|
|
Err(e) => {
|
|
|
|
panic!("Unable to create block reward: {:?}", e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
add_block_with_reward(chain, txs, coinbase_tx.clone());
|
2018-06-29 04:56:07 +03:00
|
|
|
let output = wallet.get(&fees.key_id.unwrap()).unwrap();
|
2018-06-22 16:04:51 +03:00
|
|
|
let mut batch = wallet.batch().unwrap();
|
|
|
|
batch.save(output).unwrap();
|
|
|
|
batch.commit().unwrap();
|
2018-05-16 15:18:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// adds many block rewards to a wallet, no transactions
|
2018-06-08 08:21:54 +03:00
|
|
|
pub fn award_blocks_to_wallet<T, K>(chain: &Chain, wallet: &mut T, num_rewards: usize)
|
|
|
|
where
|
|
|
|
T: WalletBackend<K>,
|
|
|
|
K: keychain::Keychain,
|
|
|
|
{
|
2018-05-16 15:18:09 +03:00
|
|
|
for _ in 0..num_rewards {
|
|
|
|
award_block_to_wallet(chain, vec![], wallet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new wallet in a particular directory
|
2018-06-08 08:21:54 +03:00
|
|
|
pub fn create_wallet(dir: &str) -> FileWallet<ExtKeychain> {
|
2018-05-16 15:18:09 +03:00
|
|
|
let mut wallet_config = WalletConfig::default();
|
|
|
|
wallet_config.data_file_dir = String::from(dir);
|
2018-06-06 17:36:29 +03:00
|
|
|
wallet::WalletSeed::init_file(&wallet_config).expect("Failed to create wallet seed file.");
|
|
|
|
let mut wallet = FileWallet::new(wallet_config.clone(), "")
|
|
|
|
.unwrap_or_else(|e| panic!("Error creating wallet: {:?} Config: {:?}", e, wallet_config));
|
|
|
|
wallet.open_with_credentials().unwrap_or_else(|e| {
|
|
|
|
panic!(
|
|
|
|
"Error initializing wallet: {:?} Config: {:?}",
|
|
|
|
e, wallet_config
|
|
|
|
)
|
|
|
|
});
|
|
|
|
wallet
|
2018-05-16 15:18:09 +03:00
|
|
|
}
|