Skip to content

Commit

Permalink
Scx1332/eventsfixed (#53)
Browse files Browse the repository at this point in the history
* Add PaymentRuntime::status with POC implementation

* infer token address in PaymentDriver::get_token_balance

* start_payment_engine -> PaymentRuntime::new

* Add StatusProperty::NoGas

* Calculate token_addr in PaymentDriver::transfer

* chore: clippy

* chore: fmt

* Add DriverEventContent::StatusChanged

This enables an event-driven way to check the entire status without
polling.

* 1.72 cargo fmt

* events fixed

* Missing import

---------

Co-authored-by: Kamil Koczurek <[email protected]>
  • Loading branch information
scx1332 and kamirr authored Sep 13, 2023
1 parent ee0798b commit bd3b739
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 25 deletions.
20 changes: 15 additions & 5 deletions crates/erc20_payment_lib/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,17 @@ pub enum TransactionFailedReason {
Unknown,
}

#[derive(Debug, Clone, Serialize)]
pub struct TransactionFinishedInfo {
pub token_transfer_dao: TokenTransferDao,
pub tx_dao: TxDao,
}

#[derive(Debug, Clone, Serialize)]
#[allow(clippy::large_enum_variant)]
pub enum DriverEventContent {
TransactionConfirmed(TxDao),
TransferFinished(TokenTransferDao),
TransferFinished(TransactionFinishedInfo),
ApproveFinished(AllowanceDao),
TransactionStuck(TransactionStuckReason),
TransactionFailed(TransactionFailedReason),
Expand Down Expand Up @@ -296,10 +302,14 @@ impl StatusTracker {
},
)
}
DriverEventContent::TransferFinished(token_transfer) => Self::clear_issues(
status2.lock().await.deref_mut(),
token_transfer.chain_id,
),

DriverEventContent::TransferFinished(transaction_finished_info) => {
Self::clear_issues(
status2.lock().await.deref_mut(),
transaction_finished_info.token_transfer_dao.chain_id,
)
}

_ => false,
};

Expand Down
9 changes: 7 additions & 2 deletions crates/erc20_payment_lib/src/sender/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use crate::sender::process::{process_transaction, ProcessTransactionResult};

use crate::utils::ConversionError;

use crate::runtime::{send_driver_event, DriverEvent, DriverEventContent, SharedState};
use crate::runtime::{
send_driver_event, DriverEvent, DriverEventContent, SharedState, TransactionFinishedInfo,
};
use crate::sender::batching::{gather_transactions_post, gather_transactions_pre};
use crate::sender::process_allowance;
use crate::setup::PaymentSetup;
Expand Down Expand Up @@ -89,7 +91,10 @@ pub async fn update_token_transfer_result(
for token_transfer in token_transfers {
send_driver_event(
&event_sender,
DriverEventContent::TransferFinished(token_transfer),
DriverEventContent::TransferFinished(TransactionFinishedInfo {
token_transfer_dao: token_transfer,
tx_dao: tx.clone(),
}),
)
.await;
}
Expand Down
4 changes: 2 additions & 2 deletions crates/erc20_payment_lib_test/src/durabily2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ pub async fn test_durability2(generate_count: u64, gen_interval_secs: f64, trans
log::debug!("Received message: {:?}", msg);

match msg.content {
TransferFinished(transfer_dao) => {
TransferFinished(transfer_finished) => {
transfer_finished_message_count += 1;
fee_paid += U256::from_dec_str(&transfer_dao.fee_paid.expect("fee paid should be set")).expect("fee paid should be a valid U256");
fee_paid += U256::from_dec_str(&transfer_finished.token_transfer_dao.fee_paid.expect("fee paid should be set")).expect("fee paid should be a valid U256");
}
ApproveFinished(allowance_dao) => {
approve_contract_message_count += 1;
Expand Down
4 changes: 2 additions & 2 deletions crates/erc20_payment_lib_test/src/multi_erc20_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ pub async fn test_durability(generate_count: u64, gen_interval_secs: f64, transf
log::debug!("Received message: {:?}", msg);

match msg.content {
TransferFinished(transfer_dao) => {
TransferFinished(transfer_finished) => {
transfer_finished_message_count += 1;
fee_paid += U256::from_dec_str(&transfer_dao.fee_paid.expect("fee paid should be set")).expect("fee paid should be a valid U256");
fee_paid += U256::from_dec_str(&transfer_finished.token_transfer_dao.fee_paid.expect("fee paid should be set")).expect("fee paid should be a valid U256");
}
ApproveFinished(allowance_dao) => {
approve_contract_message_count += 1;
Expand Down
4 changes: 2 additions & 2 deletions tests/docker_01_basic/single_erc20_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ async fn test_erc20_transfer() -> Result<(), anyhow::Error> {
log::info!("Received message: {:?}", msg);

match msg.content {
TransferFinished(transfer_dao) => {
TransferFinished(transfer_finished) => {
transfer_finished_message_count += 1;
fee_paid += U256::from_dec_str(&transfer_dao.fee_paid.expect("fee paid should be set")).expect("fee paid should be a valid U256");
fee_paid += U256::from_dec_str(&transfer_finished.token_transfer_dao.fee_paid.expect("fee paid should be set")).expect("fee paid should be a valid U256");
}
ApproveFinished(allowance_dao) => {
approve_contract_message_count += 1;
Expand Down
4 changes: 2 additions & 2 deletions tests/docker_01_basic/single_gas_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ async fn test_gas_transfer() -> Result<(), anyhow::Error> {
log::info!("Received message: {:?}", msg);

match msg.content {
TransferFinished(transfer_dao) => {
TransferFinished(transfer_finished) => {
transfer_finished_message_count += 1;
fee_paid += U256::from_dec_str(&transfer_dao.fee_paid.expect("fee paid should be set")).expect("fee paid should be a valid U256");
fee_paid += U256::from_dec_str(&transfer_finished.token_transfer_dao.fee_paid.expect("fee paid should be set")).expect("fee paid should be a valid U256");
},
TransactionConfirmed(tx_dao) => {
assert_eq!(tx_dao.gas_limit, Some(21000));
Expand Down
2 changes: 2 additions & 0 deletions tests/docker_02_errors/insufficient_gas.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use erc20_payment_lib::config::AdditionalOptions;
use erc20_payment_lib::db::ops::insert_token_transfer;
use erc20_payment_lib::misc::load_private_keys;

use erc20_payment_lib::runtime::DriverEventContent::{StatusChanged, TransactionStuck};

use erc20_payment_lib::runtime::{DriverEvent, PaymentRuntime, TransactionStuckReason};
use erc20_payment_lib::signer::PrivateKeySigner;
use erc20_payment_lib::transaction::create_token_transfer;
Expand Down
4 changes: 2 additions & 2 deletions tests/docker_02_errors/transfer_stuck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ async fn test_transfer_stuck() -> Result<(), anyhow::Error> {
log::info!("Received message: {:?}", msg);

match msg.content {
TransferFinished(transfer_dao) => {
TransferFinished(transfer_finished) => {
transfer_finished_message_count += 1;
fee_paid += U256::from_dec_str(&transfer_dao.fee_paid.expect("fee paid should be set")).expect("fee paid should be a valid U256");
fee_paid += U256::from_dec_str(&transfer_finished.token_transfer_dao.fee_paid.expect("fee paid should be set")).expect("fee paid should be a valid U256");
}
TransactionStuck(reason) => {
match reason {
Expand Down
4 changes: 2 additions & 2 deletions tests/docker_02_errors/transfer_stuck_and_replaced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ async fn test_transfer_stuck_and_replaced(scenario: Scenarios) -> Result<(), any
log::info!("Received message: {:?}", msg);

match msg.content {
TransferFinished(transfer_dao) => {
TransferFinished(transfer_finished) => {
transfer_finished_message_count += 1;
fee_paid += U256::from_dec_str(&transfer_dao.fee_paid.expect("fee paid should be set")).expect("fee paid should be a valid U256");
fee_paid += U256::from_dec_str(&transfer_finished.token_transfer_dao.fee_paid.expect("fee paid should be set")).expect("fee paid should be a valid U256");
}
TransactionStuck(reason) => {
match reason {
Expand Down
4 changes: 2 additions & 2 deletions tests/docker_02_errors/wrong_chain_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ async fn test_wrong_chain_id() -> Result<(), anyhow::Error> {
log::info!("Received message: {:?}", msg);

match msg.content {
TransferFinished(transfer_dao) => {
TransferFinished(transfer_finished) => {
transfer_finished_message_count += 1;
fee_paid += U256::from_dec_str(&transfer_dao.fee_paid.expect("fee paid should be set")).expect("fee paid should be a valid U256");
fee_paid += U256::from_dec_str(&transfer_finished.token_transfer_dao.fee_paid.expect("fee paid should be set")).expect("fee paid should be a valid U256");
}
TransactionStuck(reason) => {
//todo - dont ignore it check if proper status
Expand Down
4 changes: 2 additions & 2 deletions tests/docker_03_problems/single_transfer_with_problems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ async fn test_gas_transfer(error_probability: f64) -> Result<(), anyhow::Error>
log::info!("Received message: {:?}", msg);

match msg.content {
TransferFinished(transfer_dao) => {
TransferFinished(transfer_finished) => {
transfer_finished_message_count += 1;
fee_paid += U256::from_dec_str(&transfer_dao.fee_paid.expect("fee paid should be set")).expect("fee paid should be a valid U256");
fee_paid += U256::from_dec_str(&transfer_finished.token_transfer_dao.fee_paid.expect("fee paid should be set")).expect("fee paid should be a valid U256");
}
TransactionFailed(reason) => {
#[allow(clippy::match_single_binding)]
Expand Down
4 changes: 2 additions & 2 deletions tests/docker_04_multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ async fn test_multi_erc20_transfer(payment_count: usize, use_direct_method: bool
log::info!("Received message: {:?}", msg);

match msg.content {
TransferFinished(transfer_dao) => {
TransferFinished(transfer_finished) => {
transfer_finished_message_count += 1;
fee_paid += U256::from_dec_str(&transfer_dao.fee_paid.expect("fee paid should be set")).expect("fee paid should be a valid U256");
fee_paid += U256::from_dec_str(&transfer_finished.token_transfer_dao.fee_paid.expect("fee paid should be set")).expect("fee paid should be a valid U256");
}
ApproveFinished(allowance_dao) => {
approve_contract_message_count += 1;
Expand Down

0 comments on commit bd3b739

Please sign in to comment.