Skip to content

Commit

Permalink
Add library status (#50)
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

* Adjust error tests to ignore StatusChanged events

---------

Co-authored-by: scx1332 <[email protected]>
  • Loading branch information
kamirr and scx1332 authored Sep 7, 2023
1 parent 469e15a commit ee0798b
Show file tree
Hide file tree
Showing 14 changed files with 284 additions and 101 deletions.
313 changes: 245 additions & 68 deletions crates/erc20_payment_lib/src/runtime.rs

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions crates/erc20_payment_lib/src/sender/batching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,7 @@ pub async fn gather_transactions_batch(
send_driver_event(
&event_sender,
DriverEventContent::TransactionFailed(TransactionFailedReason::InvalidChainId(
format!(
"No setup found for chain id when gathering transfers: {}",
token_transfer.chain_id
),
token_transfer.chain_id,
)),
)
.await;
Expand Down
2 changes: 1 addition & 1 deletion crates/erc20_payment_lib/src/sender/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub async fn process_transaction(
send_driver_event(
&event_sender,
DriverEventContent::TransactionFailed(TransactionFailedReason::InvalidChainId(
format!("No setup found for chain id: {chain_id}"),
chain_id,
)),
)
.await;
Expand Down
5 changes: 3 additions & 2 deletions crates/erc20_payment_lib_test/src/durabily2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use erc20_payment_lib::db::ops::get_transfer_stats;
use erc20_payment_lib::error::PaymentError;
use erc20_payment_lib::misc::load_private_keys;
use erc20_payment_lib::runtime::DriverEventContent::*;
use erc20_payment_lib::runtime::{start_payment_engine, DriverEvent};
use erc20_payment_lib::runtime::{DriverEvent, PaymentRuntime};
use erc20_payment_lib::signer::PrivateKeySigner;
use erc20_payment_lib::utils::u256_to_rust_dec;
use erc20_payment_lib_extra::{generate_test_payments, GenerateOptions};
Expand Down Expand Up @@ -49,6 +49,7 @@ pub async fn test_durability2(generate_count: u64, gen_interval_secs: f64, trans
TransactionConfirmed(_tx_dao) => {
tx_confirmed_message_count += 1;
}
StatusChanged(_) => { }
_ => {
//maybe remove this if caused too much hassle to maintain
panic!("Unexpected message: {:?}", msg);
Expand Down Expand Up @@ -108,7 +109,7 @@ pub async fn test_durability2(generate_count: u64, gen_interval_secs: f64, trans
let jh = tokio::spawn(
async move {
tokio::time::sleep(Duration::from_secs(1)).await;
let sp = start_payment_engine(
let sp = PaymentRuntime::new(
&private_keys,
Path::new(""),
config.clone(),
Expand Down
5 changes: 3 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 @@ -7,7 +7,7 @@ use erc20_payment_lib::db::ops::get_transfer_stats;
use erc20_payment_lib::error::PaymentError;
use erc20_payment_lib::misc::load_private_keys;
use erc20_payment_lib::runtime::DriverEventContent::*;
use erc20_payment_lib::runtime::{start_payment_engine, DriverEvent};
use erc20_payment_lib::runtime::{DriverEvent, PaymentRuntime};
use erc20_payment_lib::signer::PrivateKeySigner;
use erc20_payment_lib::utils::u256_to_rust_dec;
use erc20_payment_lib_extra::{generate_test_payments, GenerateOptions};
Expand Down Expand Up @@ -49,6 +49,7 @@ pub async fn test_durability(generate_count: u64, gen_interval_secs: f64, transf
TransactionConfirmed(_tx_dao) => {
tx_confirmed_message_count += 1;
}
StatusChanged(_) => { }
_ => {
//maybe remove this if caused too much hassle to maintain
panic!("Unexpected message: {:?}", msg);
Expand Down Expand Up @@ -108,7 +109,7 @@ pub async fn test_durability(generate_count: u64, gen_interval_secs: f64, transf
async move {
tokio::time::sleep(Duration::from_secs(1)).await;
let signer = PrivateKeySigner::new(private_keys.clone());
let sp = start_payment_engine(
let sp = PaymentRuntime::new(
&private_keys,
Path::new(""),
config.clone(),
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use erc20_payment_lib::{
config, err_custom_create, err_from,
error::*,
misc::{display_private_keys, load_private_keys},
runtime::start_payment_engine,
runtime::PaymentRuntime,
};
use std::env;

Expand Down Expand Up @@ -129,7 +129,7 @@ async fn main_internal() -> Result<(), PaymentError> {
contract_use_unpacked_method: false,
};

let sp = start_payment_engine(
let sp = PaymentRuntime::new(
&private_keys,
&db_filename,
config,
Expand Down
5 changes: 3 additions & 2 deletions tests/docker_01_basic/single_erc20_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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::*;
use erc20_payment_lib::runtime::{start_payment_engine, DriverEvent};
use erc20_payment_lib::runtime::{DriverEvent, PaymentRuntime};
use erc20_payment_lib::signer::PrivateKeySigner;
use erc20_payment_lib::transaction::create_token_transfer;
use erc20_payment_lib::utils::u256_to_rust_dec;
Expand Down Expand Up @@ -45,6 +45,7 @@ async fn test_erc20_transfer() -> Result<(), anyhow::Error> {
TransactionConfirmed(_tx_dao) => {
tx_confirmed_message_count += 1;
},
StatusChanged(_) => { }
_ => {
//maybe remove this if caused too much hassle to maintain
panic!("Unexpected message: {:?}", msg);
Expand Down Expand Up @@ -79,7 +80,7 @@ async fn test_erc20_transfer() -> Result<(), anyhow::Error> {

// *** TEST RUN ***

let sp = start_payment_engine(
let sp = PaymentRuntime::new(
&private_keys.0,
std::path::Path::new(""),
config.clone(),
Expand Down
5 changes: 3 additions & 2 deletions tests/docker_01_basic/single_gas_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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::*;
use erc20_payment_lib::runtime::{start_payment_engine, DriverEvent};
use erc20_payment_lib::runtime::{DriverEvent, PaymentRuntime};
use erc20_payment_lib::signer::PrivateKeySigner;
use erc20_payment_lib::transaction::create_token_transfer;
use erc20_payment_lib::utils::u256_to_rust_dec;
Expand Down Expand Up @@ -41,6 +41,7 @@ async fn test_gas_transfer() -> Result<(), anyhow::Error> {
assert_eq!(tx_dao.gas_limit, Some(21000));
tx_confirmed_message_count += 1;
},
StatusChanged(_) => { }
_ => {
//maybe remove this if caused too much hassle to maintain
panic!("Unexpected message: {:?}", msg);
Expand Down Expand Up @@ -74,7 +75,7 @@ async fn test_gas_transfer() -> Result<(), anyhow::Error> {

// *** TEST RUN ***

let sp = start_payment_engine(
let sp = PaymentRuntime::new(
&private_keys.0,
std::path::Path::new(""),
config.clone(),
Expand Down
7 changes: 4 additions & 3 deletions tests/docker_02_errors/insufficient_gas.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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::TransactionStuck;
use erc20_payment_lib::runtime::{start_payment_engine, DriverEvent, TransactionStuckReason};
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;
use erc20_payment_lib_test::*;
Expand Down Expand Up @@ -46,6 +46,7 @@ async fn test_insufficient_gas() -> Result<(), anyhow::Error> {
}
}
}
StatusChanged(_) => { }
_ => {
//maybe remove this if caused too much hassle to maintain
panic!("Unexpected message: {:?}", msg);
Expand Down Expand Up @@ -80,7 +81,7 @@ async fn test_insufficient_gas() -> Result<(), anyhow::Error> {

// *** TEST RUN ***

let sp = start_payment_engine(
let sp = PaymentRuntime::new(
&private_keys.0,
std::path::Path::new(""),
config.clone(),
Expand Down
6 changes: 3 additions & 3 deletions tests/docker_02_errors/transfer_stuck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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::*;
use erc20_payment_lib::runtime::{start_payment_engine, DriverEvent, TransactionStuckReason};
use erc20_payment_lib::runtime::{DriverEvent, PaymentRuntime, TransactionStuckReason};
use erc20_payment_lib::signer::PrivateKeySigner;
use erc20_payment_lib::transaction::create_token_transfer;
use erc20_payment_lib_test::*;
Expand Down Expand Up @@ -47,7 +47,7 @@ async fn test_transfer_stuck() -> Result<(), anyhow::Error> {
}
}
}
TransactionConfirmed(_) => {
TransactionConfirmed(_) | StatusChanged(_) => {

}
_ => {
Expand Down Expand Up @@ -85,7 +85,7 @@ async fn test_transfer_stuck() -> Result<(), anyhow::Error> {

// *** TEST RUN ***

let sp = start_payment_engine(
let sp = PaymentRuntime::new(
&private_keys.0,
std::path::Path::new(""),
config.clone(),
Expand Down
9 changes: 5 additions & 4 deletions tests/docker_02_errors/transfer_stuck_and_replaced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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::*;
use erc20_payment_lib::runtime::{start_payment_engine, DriverEvent, TransactionStuckReason};
use erc20_payment_lib::runtime::{DriverEvent, PaymentRuntime, TransactionStuckReason};
use erc20_payment_lib::signer::PrivateKeySigner;
use erc20_payment_lib::transaction::create_token_transfer;
use erc20_payment_lib_test::*;
Expand Down Expand Up @@ -69,6 +69,7 @@ async fn test_transfer_stuck_and_replaced(scenario: Scenarios) -> Result<(), any
}
}
}
StatusChanged(_) => { }
_ => {
//maybe remove this if caused too much hassle to maintain
panic!("Unexpected message: {:?}", msg);
Expand Down Expand Up @@ -107,7 +108,7 @@ async fn test_transfer_stuck_and_replaced(scenario: Scenarios) -> Result<(), any

// *** TEST RUN ***

let sp = start_payment_engine(
let sp = PaymentRuntime::new(
&private_keys.0,
std::path::Path::new(""),
config.clone(),
Expand Down Expand Up @@ -136,7 +137,7 @@ async fn test_transfer_stuck_and_replaced(scenario: Scenarios) -> Result<(), any
Scenarios::FirstTransactionDone => Duration::from_secs(80),
};

let sp = start_payment_engine(
let sp = PaymentRuntime::new(
&private_keys.0,
std::path::Path::new(""),
config.clone(),
Expand Down Expand Up @@ -173,7 +174,7 @@ async fn test_transfer_stuck_and_replaced(scenario: Scenarios) -> Result<(), any
Scenarios::PreLastTransactionDone => Duration::from_secs(35),
Scenarios::FirstTransactionDone => Duration::from_secs(0),
};
let sp = start_payment_engine(
let sp = PaymentRuntime::new(
&private_keys.0,
std::path::Path::new(""),
config.clone(),
Expand Down
9 changes: 5 additions & 4 deletions tests/docker_02_errors/wrong_chain_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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::*;
use erc20_payment_lib::runtime::{start_payment_engine, DriverEvent, TransactionFailedReason};
use erc20_payment_lib::runtime::{DriverEvent, PaymentRuntime, TransactionFailedReason};
use erc20_payment_lib::signer::PrivateKeySigner;
use erc20_payment_lib::transaction::create_token_transfer;
use erc20_payment_lib::utils::u256_to_rust_dec;
Expand Down Expand Up @@ -41,8 +41,8 @@ async fn test_wrong_chain_id() -> Result<(), anyhow::Error> {
},
TransactionFailed(reason) => {
match reason {
TransactionFailedReason::InvalidChainId(msg) => {
log::info!("Invalid chain id: {msg}");
TransactionFailedReason::InvalidChainId(chain_id) => {
log::info!("Invalid chain id: {chain_id}");
tx_invalid_chain_id_message_count += 1;
},
_ => {
Expand All @@ -54,6 +54,7 @@ async fn test_wrong_chain_id() -> Result<(), anyhow::Error> {
TransactionConfirmed(_tx_dao) => {
tx_confirmed_message_count += 1;
},
StatusChanged(_) => { },
_ => {
//maybe remove this if caused too much hassle to maintain
panic!("Unexpected message: {:?}", msg);
Expand Down Expand Up @@ -89,7 +90,7 @@ async fn test_wrong_chain_id() -> Result<(), anyhow::Error> {

// *** TEST RUN ***

let sp = start_payment_engine(
let sp = PaymentRuntime::new(
&private_keys.0,
std::path::Path::new(""),
config.clone(),
Expand Down
5 changes: 3 additions & 2 deletions tests/docker_03_problems/single_transfer_with_problems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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::*;
use erc20_payment_lib::runtime::{start_payment_engine, DriverEvent, TransactionStuckReason};
use erc20_payment_lib::runtime::{DriverEvent, PaymentRuntime, TransactionStuckReason};
use erc20_payment_lib::signer::PrivateKeySigner;
use erc20_payment_lib::transaction::create_token_transfer;
use erc20_payment_lib::utils::u256_to_rust_dec;
Expand Down Expand Up @@ -62,6 +62,7 @@ async fn test_gas_transfer(error_probability: f64) -> Result<(), anyhow::Error>
TransactionConfirmed(_tx_dao) => {
tx_confirmed_message_count += 1;
},
StatusChanged(_) => { },
_ => {
//maybe remove this if caused too much hassle to maintain
panic!("Unexpected message: {:?}", msg);
Expand Down Expand Up @@ -131,7 +132,7 @@ async fn test_gas_transfer(error_probability: f64) -> Result<(), anyhow::Error>

// *** TEST RUN ***

let sp = start_payment_engine(
let sp = PaymentRuntime::new(
&private_keys.0,
std::path::Path::new(""),
config.clone(),
Expand Down
5 changes: 3 additions & 2 deletions tests/docker_04_multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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::*;
use erc20_payment_lib::runtime::{start_payment_engine, DriverEvent};
use erc20_payment_lib::runtime::{DriverEvent, PaymentRuntime};
use erc20_payment_lib::signer::PrivateKeySigner;
use erc20_payment_lib::transaction::create_token_transfer;
use erc20_payment_lib::utils::u256_to_rust_dec;
Expand Down Expand Up @@ -60,6 +60,7 @@ async fn test_multi_erc20_transfer(payment_count: usize, use_direct_method: bool

tx_confirmed_message_count += 1;
},
StatusChanged(_) => { }
_ => {
//maybe remove this if caused too much hassle to maintain
panic!("Unexpected message: {:?}", msg);
Expand Down Expand Up @@ -120,7 +121,7 @@ async fn test_multi_erc20_transfer(payment_count: usize, use_direct_method: bool

// *** TEST RUN ***

let sp = start_payment_engine(
let sp = PaymentRuntime::new(
&private_keys.0,
std::path::Path::new(""),
config.clone(),
Expand Down

0 comments on commit ee0798b

Please sign in to comment.