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

GH-672: review 3 #314

Closed
wants to merge 11 commits into from
Prev Previous commit
Next Next commit
GH-672-with-agent: could be called roughly complete; will go with ref…
…actoring and knocking off some todos left behind me
  • Loading branch information
Bert authored and Bert committed Jun 30, 2023
commit e89875aedb5614a49bb60e8c5ce8293a42358d8a
38 changes: 23 additions & 15 deletions node/src/accountant/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1430,10 +1430,6 @@ mod tests {
let account_1 = make_payable_account(44_444);
let account_2 = make_payable_account(333_333);
let system = System::new("test");
let consuming_wallet_balances = ConsumingWalletBalances {
transaction_fee_currency_in_minor_units: U256::from(u32::MAX),
masq_tokens_in_minor_units: U256::from(u32::MAX),
};
let arbitrary_id_stamp = ArbitraryIdStamp::new();
let agent = PayablePaymentsAgentMock::default().set_arbitrary_id_stamp(arbitrary_id_stamp);
let agent_boxed = Box::new(agent) as Box<dyn PayablePaymentsAgent>;
Expand Down Expand Up @@ -1515,7 +1511,7 @@ mod tests {
};
let mut agent = PayablePaymentsAgentWeb3::new(78910);
let persistent_config = PersistentConfigurationMock::default().gas_price_result(Ok(123));
let _ = agent.consult_desired_fee_per_computed_unit(&persistent_config);
let _ = agent.consult_required_fee_per_computed_unit(&persistent_config);
let boxed_agent = Box::new(agent);
let adjusted_payments_instructions = OutboundPaymentsInstructions {
checked_accounts: vec![adjusted_account_1.clone(), adjusted_account_2.clone()],
Expand All @@ -1534,10 +1530,6 @@ mod tests {
subject.logger = Logger::new(test_name);
let subject_addr = subject.start();
let system = System::new("test");
let consuming_wallet_balances = ConsumingWalletBalances {
transaction_fee_currency_in_minor_units: U256::from(u32::MAX),
masq_tokens_in_minor_units: U256::from(150_000_000_000_u64),
};
let arbitrary_id_stamp = ArbitraryIdStamp::new();
let agent = PayablePaymentsAgentMock::default().set_arbitrary_id_stamp(arbitrary_id_stamp);
let payable_payments_setup_msg = PayablePaymentsSetupMsg {
Expand Down Expand Up @@ -2317,8 +2309,8 @@ mod tests {
let payable_dao =
PayableDaoMock::default().non_pending_payables_result(qualified_payables.clone());
let (blockchain_bridge, _, blockchain_bridge_recordings_arc) = make_recorder();
let blockchain_bridge =
blockchain_bridge.system_stop_conditions(match_every_type_id!(PayablePaymentsSetupMsg));
let blockchain_bridge = blockchain_bridge
.system_stop_conditions(match_every_type_id!(InitialPayablePaymentsSetupMsg));
let system =
System::new("scan_for_payable_message_triggers_payment_for_balances_over_the_curve");
let peer_actors = peer_actors_builder()
Expand Down Expand Up @@ -2356,8 +2348,8 @@ mod tests {
let (blockchain_bridge, _, blockchain_bridge_recording) = make_recorder();
let blockchain_bridge_addr = blockchain_bridge
.system_stop_conditions(match_every_type_id!(
PayablePaymentsSetupMsg,
PayablePaymentsSetupMsg
InitialPayablePaymentsSetupMsg,
InitialPayablePaymentsSetupMsg
))
.start();
let pps_for_blockchain_bridge_sub = blockchain_bridge_addr.clone().recipient();
Expand Down Expand Up @@ -3124,6 +3116,7 @@ mod tests {
#[test]
fn pending_transaction_is_registered_and_monitored_until_it_gets_confirmed_or_canceled() {
init_test_logging();
let set_up_consuming_balances_params_arc = Arc::new(Mutex::new(vec![]));
let mark_pending_payable_params_arc = Arc::new(Mutex::new(vec![]));
let transactions_confirmed_params_arc = Arc::new(Mutex::new(vec![]));
let get_transaction_receipt_params_arc = Arc::new(Mutex::new(vec![]));
Expand Down Expand Up @@ -3162,9 +3155,14 @@ mod tests {
let transaction_receipt_tx_2_third_round = TransactionReceipt::default();
let mut transaction_receipt_tx_2_fourth_round = TransactionReceipt::default();
transaction_receipt_tx_2_fourth_round.status = Some(U64::from(1)); // confirmed
let agent = PayablePaymentsAgentMock::default()
.set_up_consuming_wallet_balances_params(&set_up_consuming_balances_params_arc);
let transaction_fee_balance = U256::from(444_555_666_777_u64);
let token_balance = U256::from(111_111_111_111_111_111_u64);
let blockchain_interface = BlockchainInterfaceMock::default()
.get_gas_balance_result(Ok(U256::from(u128::MAX)))
.get_token_balance_result(Ok(U256::from(u128::MAX)))
.get_transaction_fee_balance_result(Ok(transaction_fee_balance))
.get_token_balance_result(Ok(token_balance))
.mobilize_payable_payments_agent_result(Box::new(agent))
.get_transaction_count_result(Ok(web3::types::U256::from(1)))
.get_transaction_count_result(Ok(web3::types::U256::from(2)))
// because we cannot have both, resolution on the high level and also of what's inside blockchain interface,
Expand Down Expand Up @@ -3367,6 +3365,16 @@ mod tests {
pending_tx_hash_2,
]
);
let set_up_consuming_wallet_balances_params =
set_up_consuming_balances_params_arc.lock().unwrap();
let expected_consuming_wallet_balances = ConsumingWalletBalances {
transaction_fee_balance_in_minor_units: transaction_fee_balance,
masq_token_balance_in_minor_units: token_balance,
};
assert_eq!(
*set_up_consuming_wallet_balances_params,
vec![expected_consuming_wallet_balances]
);
let update_fingerprints_params = update_fingerprint_params_arc.lock().unwrap();
assert_eq!(
*update_fingerprints_params,
Expand Down
16 changes: 8 additions & 8 deletions node/src/accountant/payment_adjuster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ mod tests {
let mut payable_2 = make_payable_account(222);
payable_2.balance_wei = 200_000_000;
let consuming_wallet_balances = ConsumingWalletBalances {
transaction_fee_currency_in_minor_units: U256::from(1_001_000_000_000_u64),
masq_tokens_in_minor_units: U256::from(301_000_000),
transaction_fee_balance_in_minor_units: U256::from(1_001_000_000_000_u64),
masq_token_balance_in_minor_units: U256::from(301_000_000),
};
let mut agent_for_enough = PayablePaymentsAgentMock::default()
let agent_for_enough = PayablePaymentsAgentMock::default()
.consuming_wallet_balances_result(Some(consuming_wallet_balances))
.estimated_transaction_fee_result(1_000_000_000_000_000);
.estimated_transaction_fee_total_result(1_000_000_000_000_000);
let non_required = PayablePaymentsSetupMsg {
qualified_payables: vec![payable_1.clone(), payable_2.clone()],
agent: Box::new(agent_for_enough),
Expand All @@ -107,12 +107,12 @@ mod tests {
subject.search_for_indispensable_adjustment(&non_required, &logger);

let consuming_wallet_balances = ConsumingWalletBalances {
transaction_fee_currency_in_minor_units: U256::from(999_000_000_000_u64),
masq_tokens_in_minor_units: U256::from(299_000_000),
transaction_fee_balance_in_minor_units: U256::from(999_000_000_000_u64),
masq_token_balance_in_minor_units: U256::from(299_000_000),
};
let mut agent_for_insufficient = PayablePaymentsAgentMock::default()
let agent_for_insufficient = PayablePaymentsAgentMock::default()
.consuming_wallet_balances_result(Some(consuming_wallet_balances))
.estimated_transaction_fee_result(1_000_000_000_000_000);
.estimated_transaction_fee_total_result(1_000_000_000_000_000);
let should_require = PayablePaymentsSetupMsg {
qualified_payables: vec![payable_1, payable_2],
agent: Box::new(agent_for_insufficient),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ use web3::types::U256;

pub trait PayablePaymentsAgent: Send {
//e.g. Cardano does not require user's own choice of price
fn consult_desired_fee_per_computed_unit(
fn consult_required_fee_per_computed_unit(
&mut self,
persistent_config: &dyn PersistentConfiguration,
) -> Result<(), PersistentConfigError>;
fn set_up_pending_transaction_id(&mut self, id: U256);
fn set_up_consuming_wallet_balances(&mut self, balances: ConsumingWalletBalances);
fn estimated_transaction_fee(&self, number_of_transactions: usize) -> u128;
fn estimated_transaction_fee_total(&self, number_of_transactions: usize) -> u128;
fn consuming_wallet_balances(&self) -> Option<ConsumingWalletBalances>;
fn desired_fee_per_computed_unit(&self) -> Option<u64>;
fn required_fee_per_computed_unit(&self) -> Option<u64>;
fn pending_transaction_id(&self) -> Option<U256>;
fn debug(&self) -> String;
fn duplicate(&self) -> Box<dyn PayablePaymentsAgent>;
Expand Down Expand Up @@ -61,8 +61,6 @@ mod tests {
use crate::accountant::test_utils::{
assert_on_cloneable_agent_objects, PayablePaymentsAgentMock,
};
use crate::sub_lib::blockchain_bridge::ConsumingWalletBalances;
use crate::test_utils::persistent_configuration_mock::PersistentConfigurationMock;
use crate::test_utils::unshared_test_utils::arbitrary_id_stamp::ArbitraryIdStamp;
use web3::types::U256;

Expand Down
33 changes: 14 additions & 19 deletions node/src/accountant/scanners/payable_payments_agent_web3.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) 2019, MASQ (https://masq.ai) and/or its affiliates. All rights reserved.

use crate::accountant::scanners::payable_payments_agent_abstract_layer::PayablePaymentsAgent;
use crate::blockchain::blockchain_interface::{BlockchainError, BlockchainInterface};
use crate::db_config::persistent_configuration::{PersistentConfigError, PersistentConfiguration};
use crate::sub_lib::blockchain_bridge::ConsumingWalletBalances;
use web3::types::U256;
Expand All @@ -16,7 +15,7 @@ pub struct PayablePaymentsAgentWeb3 {
}

impl PayablePaymentsAgent for PayablePaymentsAgentWeb3 {
fn consult_desired_fee_per_computed_unit(
fn consult_required_fee_per_computed_unit(
&mut self,
persistent_config: &dyn PersistentConfiguration,
) -> Result<(), PersistentConfigError> {
Expand All @@ -33,7 +32,7 @@ impl PayablePaymentsAgent for PayablePaymentsAgentWeb3 {
self.consuming_wallet_balance_opt.replace(balances);
}

fn estimated_transaction_fee(&self, number_of_transactions: usize) -> u128 {
fn estimated_transaction_fee_total(&self, number_of_transactions: usize) -> u128 {
((self.upmost_added_gas_margin + self.gas_limit_const_part) * number_of_transactions as u64)
as u128
* self
Expand All @@ -45,7 +44,7 @@ impl PayablePaymentsAgent for PayablePaymentsAgentWeb3 {
self.consuming_wallet_balance_opt
}

fn desired_fee_per_computed_unit(&self) -> Option<u64> {
fn required_fee_per_computed_unit(&self) -> Option<u64> {
self.desired_fee_per_computed_unit_gwei_opt
}

Expand Down Expand Up @@ -84,12 +83,8 @@ mod tests {
use crate::accountant::scanners::payable_payments_agent_web3::{
PayablePaymentsAgentWeb3, WEB3_MAXIMAL_GAS_LIMIT_MARGIN,
};
use crate::accountant::test_utils::{
assert_on_cloneable_agent_objects, PayablePaymentsAgentMock,
};
use crate::db_config::persistent_configuration::{
PersistentConfigError, PersistentConfigurationReal,
};
use crate::accountant::test_utils::assert_on_cloneable_agent_objects;
use crate::db_config::persistent_configuration::PersistentConfigError;
use crate::sub_lib::blockchain_bridge::ConsumingWalletBalances;
use crate::test_utils::persistent_configuration_mock::PersistentConfigurationMock;
use web3::types::U256;
Expand Down Expand Up @@ -118,10 +113,10 @@ mod tests {
let persistent_config = PersistentConfigurationMock::default().gas_price_result(Ok(130));
let mut subject = PayablePaymentsAgentWeb3::new(12345);

let result = subject.consult_desired_fee_per_computed_unit(&persistent_config);
let result = subject.consult_required_fee_per_computed_unit(&persistent_config);

assert_eq!(result, Ok(()));
assert_eq!(subject.desired_fee_per_computed_unit(), Some(130))
assert_eq!(subject.required_fee_per_computed_unit(), Some(130))
}

#[test]
Expand All @@ -130,7 +125,7 @@ mod tests {
.gas_price_result(Err(PersistentConfigError::TransactionError));
let mut subject = PayablePaymentsAgentWeb3::new(12345);

let result = subject.consult_desired_fee_per_computed_unit(&persistent_config);
let result = subject.consult_required_fee_per_computed_unit(&persistent_config);

assert_eq!(result, Err(PersistentConfigError::TransactionError));
}
Expand All @@ -148,8 +143,8 @@ mod tests {
fn set_and_get_for_consuming_wallet_balances_works() {
let mut subject = PayablePaymentsAgentWeb3::new(12345);
let consuming_wallet_balances = ConsumingWalletBalances {
transaction_fee_currency_in_minor_units: U256::from(45_000),
masq_tokens_in_minor_units: U256::from(30_000),
transaction_fee_balance_in_minor_units: U256::from(45_000),
masq_token_balance_in_minor_units: U256::from(30_000),
};

subject.set_up_consuming_wallet_balances(consuming_wallet_balances.clone());
Expand All @@ -167,19 +162,19 @@ mod tests {
.gas_price_result(Ok(122))
.gas_price_result(Ok(550));
one_agent
.consult_desired_fee_per_computed_unit(&persistent_config)
.consult_required_fee_per_computed_unit(&persistent_config)
.unwrap();
let mut second_agent = PayablePaymentsAgentWeb3::new(444);
second_agent
.consult_desired_fee_per_computed_unit(&persistent_config)
.consult_required_fee_per_computed_unit(&persistent_config)
.unwrap();

assert_eq!(
one_agent.estimated_transaction_fee(7),
one_agent.estimated_transaction_fee_total(7),
(7 * (11_111 + WEB3_MAXIMAL_GAS_LIMIT_MARGIN)) as u128 * 122
);
assert_eq!(
second_agent.estimated_transaction_fee(3),
second_agent.estimated_transaction_fee_total(3),
(3 * (444 + WEB3_MAXIMAL_GAS_LIMIT_MARGIN)) as u128 * 550
)
}
Expand Down
29 changes: 8 additions & 21 deletions node/src/accountant/scanners/payable_payments_setup_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use crate::accountant::database_access_objects::payable_dao::PayableAccount;
use crate::accountant::scanners::payable_payments_agent_abstract_layer::PayablePaymentsAgent;
use crate::accountant::{ResponseSkeleton, SkeletonOptHolder};
use crate::sub_lib::blockchain_bridge::ConsumingWalletBalances;
use actix::Message;
use std::fmt::Debug;

Expand All @@ -22,7 +21,7 @@ impl SkeletonOptHolder for InitialPayablePaymentsSetupMsg {

#[derive(Debug, Clone, Message)]
pub struct PayablePaymentsSetupMsg {
//this field should stay private for anybody outside Accountant
// this field should stay private for anybody outside Accountant
pub(in crate::accountant) qualified_payables: Vec<PayableAccount>,
pub agent: Box<dyn PayablePaymentsAgent>,
pub response_skeleton_opt: Option<ResponseSkeleton>,
Expand All @@ -40,6 +39,8 @@ impl PartialEq for PayablePaymentsSetupMsg {
}
}

// this allows you to construct the PayablePaymentsSetupMsg even outside Accountant
// (while some parts stay privet)
impl
From<(
InitialPayablePaymentsSetupMsg,
Expand All @@ -52,25 +53,11 @@ impl
Box<dyn PayablePaymentsAgent>,
),
) -> Self {
todo!()
// PayablePaymentsSetup {
// qualified_payables: previous_msg.qualified_payables,
// this_stage_data_opt: Some(this_stage_data),
// response_skeleton_opt: previous_msg.response_skeleton_opt,
// }
}
}

impl From<(PayablePaymentsSetupMsg, Box<dyn PayablePaymentsAgent>)> for PayablePaymentsSetupMsg {
fn from(
(previous_msg, agent): (PayablePaymentsSetupMsg, Box<dyn PayablePaymentsAgent>),
) -> Self {
todo!()
// PayablePaymentsSetup {
// qualified_payables: previous_msg.qualified_payables,
// this_stage_data_opt: Some(this_stage_data),
// response_skeleton_opt: previous_msg.response_skeleton_opt,
// }
PayablePaymentsSetupMsg {
qualified_payables: initial_msg.qualified_payables,
agent,
response_skeleton_opt: initial_msg.response_skeleton_opt,
}
}
}

Expand Down
Loading