Skip to content

Commit

Permalink
adjusting iapyx after newest changes and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
dkijania committed Oct 19, 2020
1 parent c2b7d4f commit aa6ed7c
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 27 deletions.
4 changes: 2 additions & 2 deletions testing/iapyx/src/cli/args/interactive/command.rs
Expand Up @@ -50,7 +50,7 @@ impl IapyxCommand {
if let Some(controller) = model.controller.as_mut() {
let fragment_ids = controller
.pending_transactions()
.keys()
.iter()
.map(|x| x.to_string())
.collect::<Vec<String>>();
println!("===================");
Expand Down Expand Up @@ -258,7 +258,7 @@ impl Convert {
"Conversion transactions ids: [{:?}]",
controller
.pending_transactions()
.keys()
.iter()
.cloned()
.collect::<Vec<FragmentId>>()
);
Expand Down
3 changes: 2 additions & 1 deletion testing/iapyx/src/cli/args/load/mod.rs
Expand Up @@ -106,9 +106,10 @@ impl IapyxLoadCommand {
std::time::Duration::from_secs(duration),
self.pace,
self.build_monitor(),
0
)
} else if let Some(count) = self.count {
Configuration::requests_per_thread(self.threads, count, self.pace, self.build_monitor())
Configuration::requests_per_thread(self.threads, count, self.pace, self.build_monitor(), 0)
} else {
return Err(IapyxLoadCommandError::NoStrategyDefined);
};
Expand Down
1 change: 1 addition & 0 deletions testing/iapyx/src/cli/args/mod.rs
@@ -1,2 +1,3 @@
pub mod interactive;
pub mod load;
pub mod proxy;
45 changes: 45 additions & 0 deletions testing/iapyx/src/cli/args/proxy/mod.rs
@@ -0,0 +1,45 @@
use structopt::StructOpt;
use thiserror::Error;
use crate::backend::ProxyServerError;
use crate::backend::ProxyServerStub;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use std::path::PathBuf;
use jortestkit::file;

#[derive(Error, Debug)]
pub enum IapyxProxyCommandError {
#[error("proxy error")]
ProxyError(#[from] crate::backend::ProxyServerError),
}

#[derive(StructOpt, Debug)]
pub struct IapyxProxyCommand {

#[structopt(short = "a", long = "address", default_value = "127.0.0.1:8000")]
pub address: String,

#[structopt(short = "v", long = "vit-address", default_value = "127.0.0.1:3030")]
pub vit_address: String,

#[structopt(short = "n", long = "node-address", default_value = "127.0.0.1:8080")]
pub node_address: String,

#[structopt(short = "b", long = "block0")]
pub block0_path: PathBuf

}

impl IapyxProxyCommand {
pub fn build(&self) -> Result<ProxyServerStub, IapyxProxyCommandError> {
let proxy_address = self.address.clone();
let vit_address = self.vit_address.clone();
let node_address = self.node_address.clone();
let block0_path = self.block0_path.clone();

Ok(ProxyServerStub::new(proxy_address,vit_address,node_address,file::get_file_as_byte_vec(&block0_path))?)
}
}


54 changes: 46 additions & 8 deletions testing/iapyx/src/controller.rs
Expand Up @@ -9,6 +9,7 @@ use std::collections::HashMap;
use thiserror::Error;
use wallet::{AccountId, Settings};
use wallet_core::{Choice, Conversion, Proposal, Value};
use std::collections::HashSet;

pub struct Controller {
backend: WalletBackend,
Expand All @@ -17,6 +18,7 @@ pub struct Controller {
}

impl Controller {

pub fn generate(
proxy_address: String,
words_length: Type,
Expand All @@ -31,13 +33,12 @@ impl Controller {
})
}

pub fn recover(
proxy_address: String,

pub fn recover_with_backend(
backend: WalletBackend,
mnemonics: &str,
password: &[u8],
backend_settings: RestSettings,
) -> Result<Self, ControllerError> {
let backend = WalletBackend::new(proxy_address, backend_settings);
let settings = backend.settings()?;
Ok(Self {
backend,
Expand All @@ -46,6 +47,16 @@ impl Controller {
})
}

pub fn recover(
proxy_address: String,
mnemonics: &str,
password: &[u8],
backend_settings: RestSettings,
) -> Result<Self, ControllerError> {
let backend = WalletBackend::new(proxy_address, backend_settings);
Self::recover_with_backend(backend,mnemonics,password)
}

pub fn switch_backend(&mut self, proxy_address: String, backend_settings: RestSettings) {
self.backend = WalletBackend::new(proxy_address, backend_settings);
}
Expand Down Expand Up @@ -89,8 +100,8 @@ impl Controller {
self.wallet.confirm_transaction(id)
}

pub fn pending_transactions(&self) -> &HashMap<FragmentId, Vec<Input>> {
&self.wallet.pending_transactions()
pub fn pending_transactions(&self) -> HashSet<FragmentId> {
self.wallet.pending_transactions()
}

pub fn wait_for_pending_transactions(
Expand All @@ -99,7 +110,7 @@ impl Controller {
) -> Result<(), ControllerError> {
let mut limit = 60;
loop {
let ids: Vec<FragmentId> = self.pending_transactions().keys().cloned().collect();
let ids: Vec<FragmentId> = self.pending_transactions().iter().cloned().collect();

if limit <= 0 {
return Err(ControllerError::TransactionsWerePendingForTooLong { fragments: ids });
Expand Down Expand Up @@ -152,6 +163,28 @@ impl Controller {
self.backend.account_state(self.id()).map_err(Into::into)
}

pub fn vote_for(&mut self,
vote_plan_id: String,
proposal_index: u32,
choice: u8,
) -> Result<FragmentId, ControllerError> {

let proposals = self.get_proposals()?;
let proposal = proposals.iter()
.filter(|x| x.chain_voteplan_id == vote_plan_id && x.chain_proposal_index == proposal_index as i64)
.next()
.ok_or(ControllerError::CannotFindProposal{
vote_plan_name: vote_plan_id.to_string(),
proposal_index
})?;

let transaction =
self.wallet
.vote(self.settings.clone(), &proposal.clone().into(), Choice::new(choice))?;
Ok(self.backend.send_fragment(transaction.to_vec())?)
}


pub fn vote(
&mut self,
proposal: &VitProposal,
Expand Down Expand Up @@ -202,9 +235,14 @@ impl Controller {
#[derive(Debug, Error)]
pub enum ControllerError {
#[error("wallet error")]
WalletError(#[from] crate::wallet::WalletError),
WalletError(#[from] crate::wallet::Error),
#[error("backend error")]
BackendError(#[from] crate::backend::WalletBackendError),
#[error("cannot find proposal: voteplan({vote_plan_name}) index({proposal_index})")]
CannotFindProposal{
vote_plan_name: String,
proposal_index: u32
},
#[error("transactions with ids [{fragments:?}] were pending for too long")]
TransactionsWerePendingForTooLong { fragments: Vec<FragmentId> },
}
2 changes: 1 addition & 1 deletion testing/iapyx/src/data.rs
Expand Up @@ -79,7 +79,7 @@ pub struct Proposer {
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
pub struct Proposal {
#[serde(alias = "internalId")]
pub internal_id: String,
pub internal_id: u32,
#[serde(alias = "proposalId")]
pub proposal_id: String,
// #[serde(alias = "category")]
Expand Down
4 changes: 2 additions & 2 deletions testing/iapyx/src/lib.rs
Expand Up @@ -8,8 +8,8 @@ mod load;
pub mod utils;
mod wallet;

pub use crate::wallet::{Wallet, WalletError};
pub use crate::wallet::{Wallet, Error as WalletError};
pub use backend::WalletBackend;
pub use controller::Controller;
pub use controller::{Controller,ControllerError};
pub use data::{Fund, Proposal, SimpleVoteStatus, Voteplan};
pub use load::{MultiController, VoteStatusProvider, WalletRequestGen};
6 changes: 5 additions & 1 deletion testing/iapyx/src/load/multi_controller.rs
Expand Up @@ -8,6 +8,10 @@ use std::iter;
use thiserror::Error;
use wallet::Settings;
use wallet_core::{Choice, Value};


unsafe impl Send for Wallet {}

pub struct MultiController {
backend: WalletBackend,
wallets: Vec<Wallet>,
Expand Down Expand Up @@ -131,7 +135,7 @@ impl MultiController {
#[derive(Debug, Error)]
pub enum MultiControllerError {
#[error("wallet error")]
WalletError(#[from] crate::wallet::WalletError),
WalletError(#[from] crate::wallet::Error),
#[error("wallet error")]
BackendError(#[from] crate::backend::WalletBackendError),
}
39 changes: 27 additions & 12 deletions testing/iapyx/src/wallet.rs
Expand Up @@ -9,28 +9,43 @@ use chain_impl_mockchain::{
};
use hdkeygen::account::AccountId;
use jormungandr_lib::interfaces::AccountIdentifier;
use std::collections::HashMap;
use std::collections::{HashMap,HashSet};
use std::str::FromStr;
use wallet::Settings;
use wallet_core::Conversion;
use wallet_core::Wallet as Inner;
pub use wallet_core::{Error as WalletError, Proposal};
use wallet_core::{Proposal};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum Error {
#[error("cannot recover from mnemonics: {0}")]
CannotRecover(String),
#[error("cannot retrieve funds: {0}")]
CannotRetrieveFunds(String),
#[error("backend error")]
BackendError(#[from] crate::backend::WalletBackendError),
#[error("cannot send vote")]
CannotSendVote(String),
}



pub struct Wallet {
proposals: Vec<Proposal>,
inner: Inner,
}

impl Wallet {
pub fn generate(words_length: Type) -> Result<Self, WalletError> {
pub fn generate(words_length: Type) -> Result<Self, Error> {
let entropy = Entropy::generate(words_length, rand::random);
let mnemonics = entropy.to_mnemonics().to_string(&dictionary::ENGLISH);
Self::recover(&mnemonics, b"iapyx")
}

pub fn recover(mnemonics: &str, password: &[u8]) -> Result<Self, WalletError> {
pub fn recover(mnemonics: &str, password: &[u8]) -> Result<Self, Error> {
Ok(Self {
inner: Inner::recover(mnemonics, password)?,
inner: Inner::recover(mnemonics, password).map_err(|e| Error::CannotRecover(e.to_string()))?,
proposals: vec![],
})
}
Expand All @@ -43,8 +58,8 @@ impl Wallet {
self.inner.id()
}

pub fn retrieve_funds(&mut self, block0_bytes: &[u8]) -> Result<wallet::Settings, WalletError> {
self.inner.retrieve_funds(block0_bytes)
pub fn retrieve_funds(&mut self, block0_bytes: &[u8]) -> Result<wallet::Settings, Error> {
self.inner.retrieve_funds(block0_bytes).map_err(|e| Error::CannotRetrieveFunds(e.to_string()))
}

pub fn convert(&mut self, settings: Settings) -> Conversion {
Expand All @@ -65,7 +80,7 @@ impl Wallet {
}

pub fn confirm_all_transactions(&mut self) {
for (id, _) in self.pending_transactions().clone() {
for id in self.pending_transactions().clone() {
self.confirm_transaction(id)
}
}
Expand All @@ -74,8 +89,8 @@ impl Wallet {
self.inner.confirm_transaction(id);
}

pub fn pending_transactions(&self) -> &HashMap<FragmentId, Vec<Input>> {
&self.inner.pending_transactions()
pub fn pending_transactions(&self) -> HashSet<FragmentId> {
self.inner.pending_transactions().clone()
}

pub fn remove_pending_transaction(&mut self, id: &FragmentId) -> Option<Vec<Input>> {
Expand All @@ -95,8 +110,8 @@ impl Wallet {
settings: Settings,
proposal: &Proposal,
choice: Choice,
) -> Result<Box<[u8]>, WalletError> {
self.inner.vote(settings, proposal, choice)
) -> Result<Box<[u8]>, Error> {
self.inner.vote(settings, proposal, choice).map_err(|e| Error::CannotSendVote(e.to_string()))
}

pub fn set_proposals(&mut self, proposals: Vec<Proposal>) {
Expand Down

0 comments on commit aa6ed7c

Please sign in to comment.