mirror of
https://github.com/mimblewimble/grin.git
synced 2025-01-21 11:31:08 +03:00
6c8c483172
* change keychain to use bip32 paths * convert keychain to use bip32 * change identifier to be serialisation of 4-level bip32 path * wallet changes compiling, pass parent key into all wallet functions * rustfmt * fix tests in chain * rustfmt * core tests passing * rustfmt * pool tests * rustfmt * fixing wallet tests * rustfmt * remove file wallet * wallet tests compiling * rustfmt * remove db_migrate * successful tx exchange test using BIP32 paths * rustfmt * fix wallet derivation paths to m/0/0/0 * wallet test fixed again, working with default path * rustfmt * fix server tests * rustfmt * make parent_id a trait on walletbackend * rustfmt * add ability for wallet to switch between multiple named accounts, and tests (not complete) * rustfmt * account switching tests in place and passing * rustfmt * compile and test with latest libsecp changes * added public key sum to calculated e for aggsig * rustfmt * Update secp to 26 * bulletproof bip32 path integration * rustfmt * wallet restore updated with bip32 paths, also restores accounts * rustfmt * rustfmt * remove old extkey * remove old extkey * rustfmt * add wallet account commands * rustfmt * update wallet documentation * rustfmt * merge from master * update libsecp tag * merge from upstream and fix server test * rustfmt * rustfmt * merge from master * update latest libsecp merge * fix commitment to zero value generation
264 lines
8.9 KiB
Rust
264 lines
8.9 KiB
Rust
// 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.
|
|
|
|
//! tests differing accounts in the same wallet
|
|
extern crate grin_chain as chain;
|
|
extern crate grin_core as core;
|
|
extern crate grin_keychain as keychain;
|
|
extern crate grin_store as store;
|
|
extern crate grin_util as util;
|
|
extern crate grin_wallet as wallet;
|
|
extern crate rand;
|
|
#[macro_use]
|
|
extern crate slog;
|
|
extern crate chrono;
|
|
extern crate serde;
|
|
extern crate uuid;
|
|
|
|
mod common;
|
|
use common::testclient::{LocalWalletClient, WalletProxy};
|
|
|
|
use std::fs;
|
|
use std::thread;
|
|
use std::time::Duration;
|
|
|
|
use core::global;
|
|
use core::global::ChainTypes;
|
|
use keychain::{ExtKeychain, Keychain};
|
|
use util::LOGGER;
|
|
use wallet::libwallet;
|
|
|
|
fn clean_output_dir(test_dir: &str) {
|
|
let _ = fs::remove_dir_all(test_dir);
|
|
}
|
|
|
|
fn setup(test_dir: &str) {
|
|
util::init_test_logger();
|
|
clean_output_dir(test_dir);
|
|
global::set_mining_mode(ChainTypes::AutomatedTesting);
|
|
}
|
|
|
|
/// Various tests on accounts within the same wallet
|
|
fn accounts_test_impl(test_dir: &str) -> Result<(), libwallet::Error> {
|
|
setup(test_dir);
|
|
// Create a new proxy to simulate server and wallet responses
|
|
let mut wallet_proxy: WalletProxy<LocalWalletClient, ExtKeychain> = WalletProxy::new(test_dir);
|
|
let chain = wallet_proxy.chain.clone();
|
|
|
|
// Create a new wallet test client, and set its queues to communicate with the
|
|
// proxy
|
|
let client = LocalWalletClient::new("wallet1", wallet_proxy.tx.clone());
|
|
let wallet1 = common::create_wallet(&format!("{}/wallet1", test_dir), client.clone());
|
|
wallet_proxy.add_wallet("wallet1", client.get_send_instance(), wallet1.clone());
|
|
|
|
// define recipient wallet, add to proxy
|
|
let wallet2 = common::create_wallet(&format!("{}/wallet2", test_dir), client.clone());
|
|
let client = LocalWalletClient::new("wallet2", wallet_proxy.tx.clone());
|
|
wallet_proxy.add_wallet("wallet2", client.get_send_instance(), wallet2.clone());
|
|
|
|
// Set the wallet proxy listener running
|
|
thread::spawn(move || {
|
|
if let Err(e) = wallet_proxy.run() {
|
|
error!(LOGGER, "Wallet Proxy error: {}", e);
|
|
}
|
|
});
|
|
|
|
// few values to keep things shorter
|
|
let reward = core::consensus::REWARD;
|
|
let cm = global::coinbase_maturity(0); // assume all testing precedes soft fork height
|
|
|
|
// test default accounts exist
|
|
wallet::controller::owner_single_use(wallet1.clone(), |api| {
|
|
let accounts = api.accounts()?;
|
|
assert_eq!(accounts[0].label, "default");
|
|
assert_eq!(accounts[0].path, ExtKeychain::derive_key_id(2, 0, 0, 0, 0));
|
|
Ok(())
|
|
})?;
|
|
|
|
// add some accounts
|
|
wallet::controller::owner_single_use(wallet1.clone(), |api| {
|
|
let new_path = api.new_account_path("account1").unwrap();
|
|
assert_eq!(new_path, ExtKeychain::derive_key_id(2, 1, 0, 0, 0));
|
|
let new_path = api.new_account_path("account2").unwrap();
|
|
assert_eq!(new_path, ExtKeychain::derive_key_id(2, 2, 0, 0, 0));
|
|
let new_path = api.new_account_path("account3").unwrap();
|
|
assert_eq!(new_path, ExtKeychain::derive_key_id(2, 3, 0, 0, 0));
|
|
// trying to add same label again should fail
|
|
let res = api.new_account_path("account1");
|
|
assert!(res.is_err());
|
|
Ok(())
|
|
})?;
|
|
|
|
// add account to wallet 2
|
|
wallet::controller::owner_single_use(wallet2.clone(), |api| {
|
|
let new_path = api.new_account_path("listener_account").unwrap();
|
|
assert_eq!(new_path, ExtKeychain::derive_key_id(2, 1, 0, 0, 0));
|
|
Ok(())
|
|
})?;
|
|
|
|
// Default wallet 2 to listen on that account
|
|
{
|
|
let mut w = wallet2.lock().unwrap();
|
|
w.set_parent_key_id_by_name("listener_account")?;
|
|
}
|
|
|
|
// Mine into two different accounts in the same wallet
|
|
{
|
|
let mut w = wallet1.lock().unwrap();
|
|
w.set_parent_key_id_by_name("account1")?;
|
|
assert_eq!(w.parent_key_id(), ExtKeychain::derive_key_id(2, 1, 0, 0, 0));
|
|
}
|
|
let _ = common::award_blocks_to_wallet(&chain, wallet1.clone(), 7);
|
|
|
|
{
|
|
let mut w = wallet1.lock().unwrap();
|
|
w.set_parent_key_id_by_name("account2")?;
|
|
assert_eq!(w.parent_key_id(), ExtKeychain::derive_key_id(2, 2, 0, 0, 0));
|
|
}
|
|
let _ = common::award_blocks_to_wallet(&chain, wallet1.clone(), 5);
|
|
|
|
// Should have 5 in account1 (5 spendable), 5 in account (2 spendable)
|
|
wallet::controller::owner_single_use(wallet1.clone(), |api| {
|
|
let (wallet1_refreshed, wallet1_info) = api.retrieve_summary_info(true)?;
|
|
assert!(wallet1_refreshed);
|
|
assert_eq!(wallet1_info.last_confirmed_height, 12);
|
|
assert_eq!(wallet1_info.total, 5 * reward);
|
|
assert_eq!(wallet1_info.amount_currently_spendable, (5 - cm) * reward);
|
|
// check tx log as well
|
|
let (_, txs) = api.retrieve_txs(true, None)?;
|
|
assert_eq!(txs.len(), 5);
|
|
Ok(())
|
|
})?;
|
|
// now check second account
|
|
{
|
|
let mut w = wallet1.lock().unwrap();
|
|
w.set_parent_key_id_by_name("account1")?;
|
|
}
|
|
wallet::controller::owner_single_use(wallet1.clone(), |api| {
|
|
// check last confirmed height on this account is different from above (should be 0)
|
|
let (_, wallet1_info) = api.retrieve_summary_info(false)?;
|
|
assert_eq!(wallet1_info.last_confirmed_height, 0);
|
|
let (wallet1_refreshed, wallet1_info) = api.retrieve_summary_info(true)?;
|
|
assert!(wallet1_refreshed);
|
|
assert_eq!(wallet1_info.last_confirmed_height, 12);
|
|
assert_eq!(wallet1_info.total, 7 * reward);
|
|
assert_eq!(wallet1_info.amount_currently_spendable, 7 * reward);
|
|
// check tx log as well
|
|
let (_, txs) = api.retrieve_txs(true, None)?;
|
|
assert_eq!(txs.len(), 7);
|
|
Ok(())
|
|
})?;
|
|
|
|
// should be nothing in default account
|
|
{
|
|
let mut w = wallet1.lock().unwrap();
|
|
w.set_parent_key_id_by_name("default")?;
|
|
}
|
|
wallet::controller::owner_single_use(wallet1.clone(), |api| {
|
|
let (_, wallet1_info) = api.retrieve_summary_info(false)?;
|
|
assert_eq!(wallet1_info.last_confirmed_height, 0);
|
|
let (wallet1_refreshed, wallet1_info) = api.retrieve_summary_info(true)?;
|
|
assert!(wallet1_refreshed);
|
|
assert_eq!(wallet1_info.last_confirmed_height, 12);
|
|
assert_eq!(wallet1_info.total, 0,);
|
|
assert_eq!(wallet1_info.amount_currently_spendable, 0,);
|
|
// check tx log as well
|
|
let (_, txs) = api.retrieve_txs(true, None)?;
|
|
assert_eq!(txs.len(), 0);
|
|
Ok(())
|
|
})?;
|
|
|
|
// Send a tx to another wallet
|
|
{
|
|
let mut w = wallet1.lock().unwrap();
|
|
w.set_parent_key_id_by_name("account1")?;
|
|
}
|
|
|
|
wallet::controller::owner_single_use(wallet1.clone(), |api| {
|
|
let slate = api.issue_send_tx(
|
|
reward, // amount
|
|
2, // minimum confirmations
|
|
"wallet2", // dest
|
|
500, // max outputs
|
|
1, // num change outputs
|
|
true, // select all outputs
|
|
)?;
|
|
api.post_tx(&slate, false)?;
|
|
Ok(())
|
|
})?;
|
|
|
|
wallet::controller::owner_single_use(wallet1.clone(), |api| {
|
|
let (wallet1_refreshed, wallet1_info) = api.retrieve_summary_info(true)?;
|
|
assert!(wallet1_refreshed);
|
|
assert_eq!(wallet1_info.last_confirmed_height, 13);
|
|
let (_, txs) = api.retrieve_txs(true, None)?;
|
|
assert_eq!(txs.len(), 9);
|
|
Ok(())
|
|
})?;
|
|
|
|
// other account should be untouched
|
|
{
|
|
let mut w = wallet1.lock().unwrap();
|
|
w.set_parent_key_id_by_name("account2")?;
|
|
}
|
|
wallet::controller::owner_single_use(wallet1.clone(), |api| {
|
|
let (_, wallet1_info) = api.retrieve_summary_info(false)?;
|
|
assert_eq!(wallet1_info.last_confirmed_height, 12);
|
|
let (_, wallet1_info) = api.retrieve_summary_info(true)?;
|
|
assert_eq!(wallet1_info.last_confirmed_height, 13);
|
|
let (_, txs) = api.retrieve_txs(true, None)?;
|
|
println!("{:?}", txs);
|
|
assert_eq!(txs.len(), 5);
|
|
Ok(())
|
|
})?;
|
|
|
|
// wallet 2 should only have this tx on the listener account
|
|
wallet::controller::owner_single_use(wallet2.clone(), |api| {
|
|
let (wallet2_refreshed, wallet2_info) = api.retrieve_summary_info(true)?;
|
|
assert!(wallet2_refreshed);
|
|
assert_eq!(wallet2_info.last_confirmed_height, 13);
|
|
let (_, txs) = api.retrieve_txs(true, None)?;
|
|
assert_eq!(txs.len(), 1);
|
|
Ok(())
|
|
})?;
|
|
// Default account on wallet 2 should be untouched
|
|
{
|
|
let mut w = wallet2.lock().unwrap();
|
|
w.set_parent_key_id_by_name("default")?;
|
|
}
|
|
wallet::controller::owner_single_use(wallet2.clone(), |api| {
|
|
let (_, wallet2_info) = api.retrieve_summary_info(false)?;
|
|
assert_eq!(wallet2_info.last_confirmed_height, 0);
|
|
let (wallet2_refreshed, wallet2_info) = api.retrieve_summary_info(true)?;
|
|
assert!(wallet2_refreshed);
|
|
assert_eq!(wallet2_info.last_confirmed_height, 13);
|
|
assert_eq!(wallet2_info.total, 0,);
|
|
assert_eq!(wallet2_info.amount_currently_spendable, 0,);
|
|
// check tx log as well
|
|
let (_, txs) = api.retrieve_txs(true, None)?;
|
|
assert_eq!(txs.len(), 0);
|
|
Ok(())
|
|
})?;
|
|
|
|
// let logging finish
|
|
thread::sleep(Duration::from_millis(200));
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn accounts() {
|
|
let test_dir = "test_output/accounts";
|
|
if let Err(e) = accounts_test_impl(test_dir) {
|
|
panic!("Libwallet Error: {} - {}", e, e.backtrace().unwrap());
|
|
}
|
|
}
|