Skip to content
This repository has been archived by the owner on Jan 14, 2020. It is now read-only.

Commit

Permalink
Add check_version function to Foreign API (mimblewimble#87)
Browse files Browse the repository at this point in the history
* move api deser types into separate types mod

* rustfmt

* missing types file

* make all exports from libwallet more explicit

* rustfmt

* add version check function to foreign api

* rustfmt

* change check_version return value to result, for consistency
  • Loading branch information
yeastplume committed Apr 24, 2019
1 parent bff07eb commit d774272
Show file tree
Hide file tree
Showing 36 changed files with 342 additions and 256 deletions.
33 changes: 25 additions & 8 deletions api/src/foreign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@

use crate::keychain::Keychain;
use crate::libwallet::api_impl::foreign;
use crate::libwallet::slate::Slate;
use crate::libwallet::types::{BlockFees, CbData, NodeClient, WalletBackend};
use crate::libwallet::Error;
use crate::libwallet::{BlockFees, CbData, Error, NodeClient, Slate, VersionInfo, WalletBackend};
use crate::util::Mutex;
use std::marker::PhantomData;
use std::sync::Arc;
Expand Down Expand Up @@ -91,7 +89,7 @@ where
/// use api::Foreign;
/// use config::WalletConfig;
/// use impls::{HTTPNodeClient, LMDBBackend};
/// use libwallet::types::WalletBackend;
/// use libwallet::WalletBackend;
///
/// let mut wallet_config = WalletConfig::default();
/// # let dir = tempdir().map_err(|e| format!("{:#?}", e)).unwrap();
Expand Down Expand Up @@ -125,6 +123,26 @@ where
}
}

/// Return the version capabilities of the running ForeignApi Node
/// # Arguments
/// None
/// # Returns
/// * [`VersionInfo`](../grin_wallet_libwallet/api_impl/types/struct.VersionInfo.html)
/// # Example
/// Set up as in [`new`](struct.Foreign.html#method.new) method above.
/// ```
/// # grin_wallet_api::doctest_helper_setup_doc_env_foreign!(wallet, wallet_config);
///
/// let mut api_foreign = Foreign::new(wallet.clone());
///
/// let version_info = api_foreign.check_version();
/// // check and proceed accordingly
/// ```

pub fn check_version(&self) -> Result<VersionInfo, Error> {
Ok(foreign::check_version())
}

/// Builds a new unconfirmed coinbase output in the wallet, generally for inclusion in a
/// potential new block's coinbase output during mining.
///
Expand All @@ -136,7 +154,7 @@ where
///
/// # Arguments
///
/// * `block_fees` - A [`BlockFees`](../grin_wallet_libwallet/types/struct.BlockFees.html)
/// * `block_fees` - A [`BlockFees`](../grin_wallet_libwallet/api_impl/types/struct.BlockFees.html)
/// struct, set up as follows:
///
/// `fees` - should contain the sum of all transaction fees included in the potential
Expand All @@ -149,7 +167,7 @@ where
/// id will be assigned
///
/// # Returns
/// * `Ok`([`cb_data`](../grin_wallet_libwallet/types/struct.CbData.html)`)` if successful. This
/// * `Ok`([`cb_data`](../grin_wallet_libwallet/api_impl/types/struct.CbData.html)`)` if successful. This
/// will contain the corresponding output, kernel and keyID used to create the coinbase output.
/// * or [`libwallet::Error`](../grin_wallet_libwallet/struct.Error.html) if an error is encountered.
///
Expand Down Expand Up @@ -305,7 +323,6 @@ macro_rules! doctest_helper_setup_doc_env_foreign {
use grin_wallet_libwallet as libwallet;
use grin_wallet_util::grin_keychain as keychain;
use grin_wallet_util::grin_util as util;
use libwallet::slate::Slate;

use keychain::ExtKeychain;
use tempfile::tempdir;
Expand All @@ -316,7 +333,7 @@ macro_rules! doctest_helper_setup_doc_env_foreign {
use api::Foreign;
use config::WalletConfig;
use impls::{HTTPNodeClient, LMDBBackend, WalletSeed};
use libwallet::types::{BlockFees, WalletBackend};
use libwallet::{BlockFees, Slate, WalletBackend};

let dir = tempdir().map_err(|e| format!("{:#?}", e)).unwrap();
let dir = dir
Expand Down
45 changes: 42 additions & 3 deletions api/src/foreign_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
//! JSON-RPC Stub generation for the Foreign API

use crate::keychain::Keychain;
use crate::libwallet::types::{BlockFees, CbData, InitTxArgs, NodeClient, WalletBackend};
use crate::libwallet::ErrorKind;
use crate::libwallet::{Slate, VersionedSlate};
use crate::libwallet::{
BlockFees, CbData, ErrorKind, InitTxArgs, NodeClient, Slate, VersionInfo, VersionedSlate,
WalletBackend,
};
use crate::Foreign;
use easy_jsonrpc;

Expand All @@ -27,6 +28,40 @@ use easy_jsonrpc;
/// * The endpoint only supports POST operations, with the json-rpc request as the body
#[easy_jsonrpc::rpc]
pub trait ForeignRpc {
/**
Networked version of [Foreign::check_version](struct.Foreign.html#method.check_version).
# Json rpc example
```
# grin_wallet_api::doctest_helper_json_rpc_foreign_assert_response!(
# r#"
{
"jsonrpc": "2.0",
"method": "check_version",
"id": 1,
"params": []
}
# "#
# ,
# r#"
{
"id": 1,
"jsonrpc": "2.0",
"result": {
"Ok": {
"default_slate_version": 2,
"foreign_api_version": 2
}
}
}
# "#
# , 0, false);
```
*/
fn check_version(&self) -> Result<VersionInfo, ErrorKind>;

/**
Networked version of [Foreign::build_coinbase](struct.Foreign.html#method.build_coinbase).
Expand Down Expand Up @@ -322,6 +357,10 @@ where
C: NodeClient,
K: Keychain,
{
fn check_version(&self) -> Result<VersionInfo, ErrorKind> {
Foreign::check_version(self).map_err(|e| e.kind())
}

fn build_coinbase(&self, block_fees: &BlockFees) -> Result<CbData, ErrorKind> {
Foreign::build_coinbase(self, block_fees).map_err(|e| e.kind())
}
Expand Down
12 changes: 5 additions & 7 deletions api/src/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ use crate::core::core::Transaction;
use crate::impls::{HTTPWalletCommAdapter, KeybaseWalletCommAdapter};
use crate::keychain::{Identifier, Keychain};
use crate::libwallet::api_impl::owner;
use crate::libwallet::slate::Slate;
use crate::libwallet::types::{
AcctPathMapping, InitTxArgs, NodeClient, NodeHeightResult, OutputCommitMapping, TxLogEntry,
WalletBackend, WalletInfo,
use crate::libwallet::{
AcctPathMapping, Error, ErrorKind, InitTxArgs, NodeClient, NodeHeightResult,
OutputCommitMapping, Slate, TxLogEntry, WalletBackend, WalletInfo,
};
use crate::libwallet::{Error, ErrorKind};

/// Main interface into all wallet API functions.
/// Wallet APIs are split into two seperate blocks of functionality
Expand Down Expand Up @@ -99,7 +97,7 @@ where
/// use api::Owner;
/// use config::WalletConfig;
/// use impls::{HTTPNodeClient, LMDBBackend};
/// use libwallet::types::WalletBackend;
/// use libwallet::WalletBackend;
///
/// let mut wallet_config = WalletConfig::default();
/// # let dir = tempdir().map_err(|e| format!("{:#?}", e)).unwrap();
Expand Down Expand Up @@ -1018,7 +1016,7 @@ macro_rules! doctest_helper_setup_doc_env {
use api::Owner;
use config::WalletConfig;
use impls::{HTTPNodeClient, LMDBBackend, WalletSeed};
use libwallet::types::{InitTxArgs, WalletBackend};
use libwallet::{InitTxArgs, WalletBackend};

let dir = tempdir().map_err(|e| format!("{:#?}", e)).unwrap();
let dir = dir
Expand Down
8 changes: 3 additions & 5 deletions api/src/owner_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ use uuid::Uuid;

use crate::core::core::Transaction;
use crate::keychain::{Identifier, Keychain};
use crate::libwallet::slate::Slate;
use crate::libwallet::types::{
AcctPathMapping, InitTxArgs, NodeClient, NodeHeightResult, OutputCommitMapping, TxLogEntry,
WalletBackend, WalletInfo,
use crate::libwallet::{
AcctPathMapping, ErrorKind, InitTxArgs, NodeClient, NodeHeightResult, OutputCommitMapping,
Slate, TxLogEntry, WalletBackend, WalletInfo,
};
use crate::libwallet::ErrorKind;
use crate::Owner;
use easy_jsonrpc;

Expand Down
2 changes: 1 addition & 1 deletion controller/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::impls::{
LMDBBackend, NullWalletCommAdapter,
};
use crate::impls::{HTTPNodeClient, WalletSeed};
use crate::libwallet::types::{InitTxArgs, NodeClient, WalletInst};
use crate::libwallet::{InitTxArgs, NodeClient, WalletInst};
use crate::{controller, display};

/// Arguments common to all wallet commands
Expand Down
8 changes: 3 additions & 5 deletions controller/src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ use crate::core::core;
use crate::core::core::Transaction;
use crate::impls::{FileWalletCommAdapter, HTTPWalletCommAdapter, KeybaseWalletCommAdapter};
use crate::keychain::Keychain;
use crate::libwallet::slate::Slate;
use crate::libwallet::types::{
CbData, InitTxArgs, NodeClient, OutputCommitMapping, SendTXArgs, TxLogEntry, WalletBackend,
WalletInfo,
use crate::libwallet::{
CbData, Error, ErrorKind, InitTxArgs, NodeClient, OutputCommitMapping, SendTXArgs, Slate,
TxLogEntry, WalletBackend, WalletInfo,
};
use crate::libwallet::{Error, ErrorKind};
use crate::util::to_base64;
use crate::util::Mutex;
use failure::ResultExt;
Expand Down
5 changes: 2 additions & 3 deletions controller/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@

use crate::core::core::{self, amount_to_hr_string};
use crate::core::global;
use crate::libwallet::types::{
AcctPathMapping, OutputCommitMapping, OutputStatus, TxLogEntry, WalletInfo,
use crate::libwallet::{
AcctPathMapping, Error, OutputCommitMapping, OutputStatus, TxLogEntry, WalletInfo,
};
use crate::libwallet::Error;
use crate::util;
use prettytable;
use std::io::prelude::Write;
Expand Down
2 changes: 1 addition & 1 deletion controller/tests/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use self::core::global::ChainTypes;
use self::keychain::{ExtKeychain, Keychain};
use grin_wallet_libwallet as libwallet;
use impls::test_framework::{self, LocalWalletClient, WalletProxy};
use libwallet::types::InitTxArgs;
use libwallet::InitTxArgs;
use std::fs;
use std::thread;
use std::time::Duration;
Expand Down
17 changes: 8 additions & 9 deletions controller/tests/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use self::keychain::ExtKeychain;
use grin_wallet_libwallet as libwallet;
use impls::test_framework::{self, LocalWalletClient, WalletProxy};
use impls::FileWalletCommAdapter;
use libwallet::types::{InitTxArgs, WalletInst};
use libwallet::{InitTxArgs, WalletInst};
use std::fs;
use std::thread;
use std::time::Duration;
Expand Down Expand Up @@ -123,7 +123,7 @@ fn check_repair_impl(test_dir: &str) -> Result<(), libwallet::Error> {
assert_eq!(wallet1_info.amount_currently_spendable, (bh - cm) * reward);
// check tx log as well
let (_, txs) = api.retrieve_txs(true, None, None)?;
let (c, _) = libwallet::types::TxLogEntry::sum_confirmed(&txs);
let (c, _) = libwallet::TxLogEntry::sum_confirmed(&txs);
assert_eq!(wallet1_info.total, c);
assert_eq!(txs.len(), bh as usize);
Ok(())
Expand All @@ -135,7 +135,7 @@ fn check_repair_impl(test_dir: &str) -> Result<(), libwallet::Error> {
w1_outputs_commits = api.retrieve_outputs(false, true, None)?.1;
Ok(())
})?;
let w1_outputs: Vec<libwallet::types::OutputData> =
let w1_outputs: Vec<libwallet::OutputData> =
w1_outputs_commits.into_iter().map(|m| m.output).collect();
{
let mut w = wallet1.lock();
Expand All @@ -145,7 +145,7 @@ fn check_repair_impl(test_dir: &str) -> Result<(), libwallet::Error> {
batch.delete(&w1_outputs[4].key_id, &None)?;
batch.delete(&w1_outputs[10].key_id, &None)?;
let mut accidental_spent = w1_outputs[13].clone();
accidental_spent.status = libwallet::types::OutputStatus::Spent;
accidental_spent.status = libwallet::OutputStatus::Spent;
batch.save(accidental_spent)?;
batch.commit()?;
}
Expand All @@ -156,7 +156,7 @@ fn check_repair_impl(test_dir: &str) -> Result<(), libwallet::Error> {
wallet::controller::owner_single_use(wallet1.clone(), |api| {
let (_, wallet1_info) = api.retrieve_summary_info(true, 1)?;
let (_, txs) = api.retrieve_txs(true, None, None)?;
let (c, _) = libwallet::types::TxLogEntry::sum_confirmed(&txs);
let (c, _) = libwallet::TxLogEntry::sum_confirmed(&txs);
assert!(wallet1_info.total != c);
Ok(())
})?;
Expand Down Expand Up @@ -223,10 +223,9 @@ fn check_repair_impl(test_dir: &str) -> Result<(), libwallet::Error> {

fn two_wallets_one_seed_impl(test_dir: &str) -> Result<(), libwallet::Error> {
setup(test_dir);
let seed_phrase =
"affair pistol cancel crush garment candy ancient flag work \
market crush dry stand focus mutual weapon offer ceiling rival turn team spring \
where swift";
let seed_phrase = "affair pistol cancel crush garment candy ancient flag work \
market crush dry stand focus mutual weapon offer ceiling rival turn team spring \
where swift";

// Create a new proxy to simulate server and wallet responses
let mut wallet_proxy: WalletProxy<LocalWalletClient, ExtKeychain> = WalletProxy::new(test_dir);
Expand Down
2 changes: 1 addition & 1 deletion controller/tests/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use std::fs;
use std::thread;
use std::time::Duration;

use grin_wallet_libwallet::types::InitTxArgs;
use grin_wallet_libwallet::InitTxArgs;

use serde_json;

Expand Down
3 changes: 1 addition & 2 deletions controller/tests/repost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ use grin_wallet_util::grin_util as util;
use self::core::global;
use self::core::global::ChainTypes;
use self::keychain::ExtKeychain;
use self::libwallet::slate::Slate;
use self::libwallet::types::InitTxArgs;
use self::libwallet::{InitTxArgs, Slate};
use impls::test_framework::{self, LocalWalletClient, WalletProxy};
use impls::FileWalletCommAdapter;
use std::fs;
Expand Down
11 changes: 5 additions & 6 deletions controller/tests/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ use grin_wallet_util::grin_util as util;
use self::core::global;
use self::core::global::ChainTypes;
use self::keychain::{ExtKeychain, Identifier, Keychain};
use self::libwallet::slate::Slate;
use self::libwallet::types::{AcctPathMapping, InitTxArgs};
use self::libwallet::{AcctPathMapping, InitTxArgs, Slate};
use impls::test_framework::{self, LocalWalletClient, WalletProxy};
use std::fs;
use std::sync::atomic::Ordering;
Expand Down Expand Up @@ -124,11 +123,11 @@ fn compare_wallet_restore(
}
});

let mut src_info: Option<libwallet::types::WalletInfo> = None;
let mut dest_info: Option<libwallet::types::WalletInfo> = None;
let mut src_info: Option<libwallet::WalletInfo> = None;
let mut dest_info: Option<libwallet::WalletInfo> = None;

let mut src_txs: Option<Vec<libwallet::types::TxLogEntry>> = None;
let mut dest_txs: Option<Vec<libwallet::types::TxLogEntry>> = None;
let mut src_txs: Option<Vec<libwallet::TxLogEntry>> = None;
let mut dest_txs: Option<Vec<libwallet::TxLogEntry>> = None;

let mut src_accts: Option<Vec<AcctPathMapping>> = None;
let mut dest_accts: Option<Vec<AcctPathMapping>> = None;
Expand Down
2 changes: 1 addition & 1 deletion controller/tests/self_send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use self::core::global::ChainTypes;
use self::keychain::ExtKeychain;
use grin_wallet_libwallet as libwallet;
use impls::test_framework::{self, LocalWalletClient, WalletProxy};
use libwallet::types::InitTxArgs;
use libwallet::InitTxArgs;
use std::fs;
use std::thread;
use std::time::Duration;
Expand Down
7 changes: 3 additions & 4 deletions controller/tests/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ use self::core::core::transaction;
use self::core::global;
use self::core::global::ChainTypes;
use self::keychain::ExtKeychain;
use self::libwallet::slate::Slate;
use self::libwallet::types::{InitTxArgs, OutputStatus};
use self::libwallet::{InitTxArgs, OutputStatus, Slate};
use impls::test_framework::{self, LocalWalletClient, WalletProxy};
use std::fs;
use std::thread;
Expand Down Expand Up @@ -75,7 +74,7 @@ fn basic_transaction_api(test_dir: &str) -> Result<(), libwallet::Error> {
// few values to keep things shorter
let reward = core::consensus::REWARD;
let cm = global::coinbase_maturity(); // assume all testing precedes soft fork height
// mine a few blocks
// mine a few blocks
let _ = test_framework::award_blocks_to_wallet(&chain, wallet1.clone(), 10, false);

// Check wallet 1 contents are as expected
Expand Down Expand Up @@ -379,7 +378,7 @@ fn tx_rollback(test_dir: &str) -> Result<(), libwallet::Error> {
// few values to keep things shorter
let reward = core::consensus::REWARD;
let cm = global::coinbase_maturity(); // assume all testing precedes soft fork height
// mine a few blocks
// mine a few blocks
let _ = test_framework::award_blocks_to_wallet(&chain, wallet1.clone(), 5, false);

let amount = 30_000_000_000;
Expand Down
3 changes: 1 addition & 2 deletions impls/src/adapters/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ use std::fs::File;
use std::io::{Read, Write};

use crate::config::WalletConfig;
use crate::libwallet::slate::Slate;
use crate::libwallet::Error;
use crate::libwallet::{Error, Slate};
use crate::WalletCommAdapter;
use std::collections::HashMap;

Expand Down
Loading

0 comments on commit d774272

Please sign in to comment.