Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My/worker return values #59

Merged
merged 10 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ pub mod trusted_cli;

use crate::commands::Commands;
use clap::Parser;
use itp_node_api::api_client::Metadata;
use ita_stf::MerkleProofWithCodec;
use itp_node_api::metadata::Metadata;
use simplyr_lib::MarketOutput;
use sp_application_crypto::KeyTypeId;
use sp_core::{H160, H256};
use thiserror::Error;
Expand Down Expand Up @@ -109,6 +111,10 @@ pub enum CliResultOk {
// TODO should ideally be removed; or at least drastically less used
// We WANT all commands exposed by the cli to return something useful for the caller(ie instead of printing)
None,

Matches(MarketOutput),
PayAsBidOutput(Option<Vec<u8>>),
PayAsBidProofOutput(MerkleProofWithCodec<H256, Vec<u8>>),
}

#[derive(Debug, Error)]
Expand Down
36 changes: 19 additions & 17 deletions cli/src/trusted_base_cli/commands/get_market_results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::{
trusted_operation::perform_trusted_operation, Cli, CliResult, CliResultOk,
};

use crate::CliError;
use codec::Decode;
use ita_stf::{TrustedGetter, TrustedOperation};
use itp_stf_primitives::types::KeyPair;
Expand All @@ -32,19 +33,14 @@ pub struct GetMarketResultsCommand {

impl GetMarketResultsCommand {
pub(crate) fn run(&self, cli: &Cli, trusted_args: &TrustedCli) -> CliResult {
println!(
"{:?}",
// if we serialize with serde-json we can easily just pass it as
// an argument in the verify-proof command.
serde_json::to_string(&get_market_results(
cli,
trusted_args,
&self.account,
self.timestamp.clone(),
))
.unwrap()
);
Ok(CliResultOk::None)
let results = get_market_results(cli, trusted_args, &self.account, self.timestamp.clone());
match results {
Ok(res) => Ok(CliResultOk::Matches(res)),
Err(e) => {
log::error!("Error getting results: {}", e);
Err(CliError::TrustedOp { msg: "Error getting results".into() })
},
}
}
}

Expand All @@ -53,7 +49,7 @@ pub(crate) fn get_market_results(
trusted_args: &TrustedCli,
arg_who: &str,
timestamp: String,
) -> MarketOutput {
) -> Result<MarketOutput, CliError> {
debug!("arg_who = {:?}", arg_who);
let who = get_pair_from_str(trusted_args, arg_who);

Expand All @@ -65,11 +61,17 @@ pub(crate) fn get_market_results(

match res {
Some(market_results) => match MarketOutput::decode(&mut market_results.as_slice()) {
Ok(market_output) => market_output,
Err(err) => panic!("Error deserializing market results: {}", err),
Ok(market_output) => Ok(market_output),
Err(err) => {
log::error!("Error deserializing results: {}", err);
Err(CliError::TrustedOp {
msg: format!("Error deserializing market results: {}", err),
})
},
},
None => {
panic!("Results not found");
log::error!("Results not found");
Err(CliError::TrustedOp { msg: "Results not found".into() })
},
}
}
31 changes: 26 additions & 5 deletions cli/src/trusted_base_cli/commands/pay_as_bid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
trusted_cli::TrustedCli,
trusted_command_utils::{get_identifiers, get_pair_from_str},
trusted_operation::perform_trusted_operation,
Cli, CliResult, CliResultOk,
Cli, CliError, CliResult, CliResultOk,
};
use codec::Decode;
use ita_stf::{Index, TrustedCall, TrustedGetter, TrustedOperation};
Expand All @@ -37,12 +37,23 @@ pub struct PayAsBidCommand {

impl PayAsBidCommand {
pub(crate) fn run(&self, cli: &Cli, trusted_args: &TrustedCli) -> CliResult {
println!("Result: {:?}", pay_as_bid(cli, trusted_args, &self.account, &self.orders_string));
Ok(CliResultOk::None)
let results = pay_as_bid(cli, trusted_args, &self.account, &self.orders_string);
match results {
Ok(res) => Ok(CliResultOk::PayAsBidOutput(res)),
Err(e) => {
log::error!("Error: {}", e);
Err(CliError::TrustedOp { msg: "Error savings Orders".into() })
},
}
}
}

pub(crate) fn pay_as_bid(cli: &Cli, trusted_args: &TrustedCli, arg_who: &str, orders_string: &str) {
pub(crate) fn pay_as_bid(
cli: &Cli,
trusted_args: &TrustedCli,
arg_who: &str,
orders_string: &str,
) -> Result<Option<Vec<u8>>, CliError> {
debug!("arg_who = {:?}", arg_who);
let who = get_pair_from_str(trusted_args, arg_who);
let signer = get_pair_from_str(trusted_args, arg_who);
Expand All @@ -53,5 +64,15 @@ pub(crate) fn pay_as_bid(cli: &Cli, trusted_args: &TrustedCli, arg_who: &str, or
.sign(&KeyPair::Sr25519(Box::new(signer)), nonce, &mrenclave, &shard)
.into_trusted_operation(trusted_args.direct);

let _res = perform_trusted_operation(cli, trusted_args, &top);
let res = perform_trusted_operation(cli, trusted_args, &top);
match res {
Ok(opt) => match opt {
Some(_results) => Ok(Some(_results)),
None => Ok(None),
},
Err(err) => {
log::error!("Error in saving Orders: {}", err);
Err(CliError::TrustedOp { msg: format!("Error in saving Orders: {}", err) })
},
}
}
48 changes: 28 additions & 20 deletions cli/src/trusted_base_cli/commands/pay_as_bid_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use itp_stf_primitives::types::KeyPair;
use log::debug;
use sp_core::{Pair, H256};

use crate::CliError;
use codec;

#[derive(Parser)]
pub struct PayAsBidProofCommand {
/// AccountId in ss58check format
Expand All @@ -34,20 +34,23 @@ pub struct PayAsBidProofCommand {

impl PayAsBidProofCommand {
pub(crate) fn run(&self, cli: &Cli, trusted_args: &TrustedCli) -> CliResult {
println!(
"{:?}",
// if we serialize with serde-json we can easily just pass it as
// an argument in the verify-proof command.
serde_json::to_string(&pay_as_bid_proof(
cli,
trusted_args,
&self.account,
self.timestamp.clone(),
self.actor_id.clone()
))
.unwrap()
// if we serialize with serde-json we can easily just pass it as
// an argument in the verify-proof command.
let results = pay_as_bid_proof(
cli,
trusted_args,
&self.account,
self.timestamp.clone(),
self.actor_id.clone(),
);
Ok(CliResultOk::None)

match results {
Ok(res) => Ok(CliResultOk::PayAsBidProofOutput(res)),
Err(e) => {
log::error!("Error getting proof: {}", e);
Err(CliError::TrustedOp { msg: "Error getting proof".into() })
},
}
}
}

Expand All @@ -57,7 +60,7 @@ pub(crate) fn pay_as_bid_proof(
arg_who: &str,
timestamp: String,
actor_id: String,
) -> MerkleProofWithCodec<H256, Vec<u8>> {
) -> Result<MerkleProofWithCodec<H256, Vec<u8>>, CliError> {
debug!("arg_who = {:?}", arg_who);
let who = get_pair_from_str(trusted_args, arg_who);

Expand All @@ -69,13 +72,18 @@ pub(crate) fn pay_as_bid_proof(
let res = perform_trusted_operation(cli, trusted_args, &top).unwrap();

match res {
Some(value) => {
let proof: MerkleProofWithCodec<_, _> =
MerkleProofWithCodec::decode(&mut &value[..]).unwrap();
proof
Some(_proof) => match MerkleProofWithCodec::decode(&mut &_proof[..]) {
Ok(_proof) => Ok(_proof),
Err(err) => {
log::error!("Error deserializing results: {}", err);
Err(CliError::TrustedOp {
msg: format!("Error deserializing market results: {}", err),
})
},
},
None => {
panic!("Proof not found");
log::error!("Results not found");
Err(CliError::TrustedOp { msg: "Results not found".into() })
},
}
}
1 change: 0 additions & 1 deletion cli/src/trusted_base_cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ fn new_account(trusted_args: &TrustedCli) -> CliResult {
drop(store);
info!("new account {}", key.to_ss58check());
let key_str = key.to_ss58check();
println!("{}", key_str);
Copy link

@clangenb clangenb Sep 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this leads to the CI error. Has this been changed for a reason?

Copy link

@j-ti j-ti Sep 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we now give return values now, we did not need it, but we can reinsert it. I will do so an push it


Ok(CliResultOk::PubKeysBase58 { pubkeys_sr25519: Some(vec![key_str]), pubkeys_ed25519: None })
}
Expand Down
Loading