Skip to content

Commit

Permalink
[wip] finish is_available and update_is_available implementations
Browse files Browse the repository at this point in the history
Signed-off-by: ozkanonur <work@onurozkan.dev>
  • Loading branch information
onur-ozkan committed Apr 26, 2023
1 parent 09ca1b8 commit 6b6c4ed
Show file tree
Hide file tree
Showing 20 changed files with 71 additions and 32 deletions.
9 changes: 6 additions & 3 deletions mm2src/coins/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ use std::convert::TryFrom;
use std::ops::Deref;
#[cfg(not(target_arch = "wasm32"))] use std::path::PathBuf;
use std::str::FromStr;
use std::sync::atomic::{AtomicU64, Ordering as AtomicOrdering};
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering as AtomicOrdering};
use std::sync::{Arc, Mutex};
use web3::types::{Action as TraceAction, BlockId, BlockNumber, Bytes, CallRequest, FilterBuilder, Log, Trace,
TraceFilterBuilder, Transaction as Web3Transaction, TransactionId, U64};
Expand Down Expand Up @@ -463,6 +463,8 @@ pub struct EthCoinImpl {
/// This spawner is used to spawn coin's related futures that should be aborted on coin deactivation
/// and on [`MmArc::stop`].
pub abortable_system: AbortableQueue,
/// A flag used for controlling the parent coin's mode(active/passive).
is_available: AtomicBool,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -4428,9 +4430,9 @@ impl MmCoin for EthCoin {
};
}

fn is_available(&self) -> bool { todo!() }
fn is_available(&self) -> bool { self.is_available.load(AtomicOrdering::SeqCst) }

fn passive_it(&self) { todo!() }
fn update_is_available(&self, to: bool) { self.is_available.store(to, AtomicOrdering::SeqCst); }
}

pub trait TryToAddress {
Expand Down Expand Up @@ -4976,6 +4978,7 @@ pub async fn eth_coin_from_conf_and_request(
nonce_lock,
erc20_tokens_infos: Default::default(),
abortable_system,
is_available: AtomicBool::new(true),
};
Ok(EthCoin(Arc::new(coin)))
}
Expand Down
10 changes: 10 additions & 0 deletions mm2src/coins/eth/eth_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ fn eth_coin_for_test(
nonce_lock: new_nonce_lock(),
erc20_tokens_infos: Default::default(),
abortable_system: AbortableQueue::default(),
is_available: AtomicBool::new(true),
}));
(ctx, eth_coin)
}
Expand Down Expand Up @@ -251,6 +252,7 @@ fn send_and_refund_erc20_payment() {
nonce_lock: new_nonce_lock(),
erc20_tokens_infos: Default::default(),
abortable_system: AbortableQueue::default(),
is_available: AtomicBool::new(true),
}));
let maker_payment_args = SendPaymentArgs {
time_lock_duration: 0,
Expand Down Expand Up @@ -322,6 +324,7 @@ fn send_and_refund_eth_payment() {
nonce_lock: new_nonce_lock(),
erc20_tokens_infos: Default::default(),
abortable_system: AbortableQueue::default(),
is_available: AtomicBool::new(true),
}));
let send_maker_payment_args = SendPaymentArgs {
time_lock_duration: 0,
Expand Down Expand Up @@ -411,6 +414,7 @@ fn test_nonce_several_urls() {
nonce_lock: new_nonce_lock(),
erc20_tokens_infos: Default::default(),
abortable_system: AbortableQueue::default(),
is_available: AtomicBool::new(true),
}));

log!("My address {:?}", coin.my_address);
Expand Down Expand Up @@ -463,6 +467,7 @@ fn test_wait_for_payment_spend_timeout() {
nonce_lock: new_nonce_lock(),
erc20_tokens_infos: Default::default(),
abortable_system: AbortableQueue::default(),
is_available: AtomicBool::new(true),
};

let coin = EthCoin(Arc::new(coin));
Expand Down Expand Up @@ -534,6 +539,7 @@ fn test_search_for_swap_tx_spend_was_spent() {
nonce_lock: new_nonce_lock(),
erc20_tokens_infos: Default::default(),
abortable_system: AbortableQueue::default(),
is_available: AtomicBool::new(true),
}));

// raw transaction bytes of https://etherscan.io/tx/0x2814718945e90fe4301e2a74eaaa46b4fdbdba1536e1d94e3b0bd665b2dd091d
Expand Down Expand Up @@ -647,6 +653,7 @@ fn test_search_for_swap_tx_spend_was_refunded() {
nonce_lock: new_nonce_lock(),
erc20_tokens_infos: Default::default(),
abortable_system: AbortableQueue::default(),
is_available: AtomicBool::new(true),
}));

// raw transaction bytes of https://etherscan.io/tx/0x02c261dcb1c8615c029b9abc712712b80ef8c1ef20d2cbcdd9bde859e7913476
Expand Down Expand Up @@ -1320,6 +1327,7 @@ fn test_message_hash() {
nonce_lock: new_nonce_lock(),
erc20_tokens_infos: Default::default(),
abortable_system: AbortableQueue::default(),
is_available: AtomicBool::new(true),
}));

let message_hash = coin.sign_message_hash("test").unwrap();
Expand Down Expand Up @@ -1365,6 +1373,7 @@ fn test_sign_verify_message() {
nonce_lock: new_nonce_lock(),
erc20_tokens_infos: Default::default(),
abortable_system: AbortableQueue::default(),
is_available: AtomicBool::new(true),
}));

let message = "test";
Expand Down Expand Up @@ -1417,6 +1426,7 @@ fn test_eth_extract_secret() {
nonce_lock: new_nonce_lock(),
erc20_tokens_infos: Default::default(),
abortable_system: AbortableQueue::default(),
is_available: AtomicBool::new(true),
}));

// raw transaction bytes of https://ropsten.etherscan.io/tx/0xcb7c14d3ff309996d582400369393b6fa42314c52245115d4a3f77f072c36da9
Expand Down
2 changes: 2 additions & 0 deletions mm2src/coins/eth/v2_activation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ impl EthCoin {
nonce_lock: self.nonce_lock.clone(),
erc20_tokens_infos: Default::default(),
abortable_system,
is_available: AtomicBool::new(true),
};

Ok(EthCoin(Arc::new(token)))
Expand Down Expand Up @@ -311,6 +312,7 @@ pub async fn eth_coin_from_conf_and_request_v2(
nonce_lock,
erc20_tokens_infos: Default::default(),
abortable_system,
is_available: AtomicBool::new(true),
};

Ok(EthCoin(Arc::new(coin)))
Expand Down
8 changes: 5 additions & 3 deletions mm2src/coins/lightning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use bitcrypto::ChecksumType;
use bitcrypto::{dhash256, ripemd160};
use common::custom_futures::repeatable::{Ready, Retry};
use common::executor::{AbortableSystem, AbortedError, Timer};
use common::log::{error, info, LogOnError, LogState};
use common::log::{error, info, warn, LogOnError, LogState};
use common::{async_blocking, get_local_duration_since_epoch, log, now_ms, PagingOptionsEnum};
use db_common::sqlite::rusqlite::Error as SqlError;
use futures::{FutureExt, TryFutureExt};
Expand Down Expand Up @@ -1428,7 +1428,9 @@ impl MmCoin for LightningCoin {

fn on_token_deactivated(&self, _ticker: &str) {}

fn is_available(&self) -> bool { todo!() }
fn is_available(&self) -> bool { true }

fn passive_it(&self) { todo!() }
fn update_is_available(&self, _to: bool) {
warn!("`update_is_available` is ineffective for lightning protocol");
}
}
4 changes: 2 additions & 2 deletions mm2src/coins/lp_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2237,7 +2237,7 @@ pub trait MmCoin:
fn on_disabled(&self) -> Result<(), AbortedError>;

/// For Handling the removal/deactivation of token on platform coin deactivation.
fn on_token_deactivated(&self, _ticker: &str);
fn on_token_deactivated(&self, ticker: &str);

/// Gets the current state of the parent coin wheter
/// it's available for the external requests or not.
Expand All @@ -2250,7 +2250,7 @@ pub trait MmCoin:
/// that have child tokens enabled.
///
/// Noneffective for child tokens.
fn passive_it(&self);
fn update_is_available(&self, to: bool);
}

/// The coin futures spawner. It's used to spawn futures that can be aborted immediately or after a timeout
Expand Down
5 changes: 3 additions & 2 deletions mm2src/coins/qrc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ use std::collections::{HashMap, HashSet};
use std::ops::{Deref, Neg};
#[cfg(not(target_arch = "wasm32"))] use std::path::PathBuf;
use std::str::FromStr;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use utxo_signer::with_key_pair::{sign_tx, UtxoSignWithKeyPairError};

Expand Down Expand Up @@ -1464,9 +1465,9 @@ impl MmCoin for Qrc20Coin {

fn on_token_deactivated(&self, _ticker: &str) {}

fn is_available(&self) -> bool { todo!() }
fn is_available(&self) -> bool { self.as_ref().is_available.load(Ordering::SeqCst) }

fn passive_it(&self) { todo!() }
fn update_is_available(&self, to: bool) { self.as_ref().is_available.store(to, Ordering::SeqCst); }
}

pub fn qrc20_swap_id(time_lock: u32, secret_hash: &[u8]) -> Vec<u8> {
Expand Down
8 changes: 6 additions & 2 deletions mm2src/coins/solana.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use solana_sdk::{pubkey::Pubkey,
signature::{Keypair, Signer}};
use std::collections::HashMap;
use std::str::FromStr;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Mutex;
use std::{convert::TryFrom, fmt::Debug, ops::Deref, sync::Arc};

Expand Down Expand Up @@ -207,6 +208,7 @@ pub async fn solana_coin_with_policy(
decimals,
spl_tokens_infos,
abortable_system,
is_available: AtomicBool::new(true),
}));
Ok(solana_coin)
}
Expand All @@ -222,6 +224,8 @@ pub struct SolanaCoinImpl {
/// This spawner is used to spawn coin's related futures that should be aborted on coin deactivation
/// and on [`MmArc::stop`].
pub abortable_system: AbortableQueue,
/// A flag used for controlling the parent coin's mode(active/passive).
is_available: AtomicBool,
}

#[derive(Clone)]
Expand Down Expand Up @@ -769,7 +773,7 @@ impl MmCoin for SolanaCoin {

fn on_token_deactivated(&self, _ticker: &str) {}

fn is_available(&self) -> bool { todo!() }
fn is_available(&self) -> bool { self.is_available.load(Ordering::SeqCst) }

fn passive_it(&self) { todo!() }
fn update_is_available(&self, to: bool) { self.is_available.store(to, Ordering::SeqCst); }
}
6 changes: 4 additions & 2 deletions mm2src/coins/solana/spl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,9 @@ impl MmCoin for SplToken {

fn on_token_deactivated(&self, _ticker: &str) {}

fn is_available(&self) -> bool { todo!() }
fn is_available(&self) -> bool { true }

fn passive_it(&self) { todo!() }
fn update_is_available(&self, _to: bool) {
warn!("child token {} can't be passive", self.ticker());
}
}
7 changes: 4 additions & 3 deletions mm2src/coins/tendermint/tendermint_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ impl RpcCommonOps for TendermintCoin {
}

pub struct TendermintCoinImpl {
is_available: AtomicBool,
ticker: String,
/// As seconds
avg_blocktime: u8,
Expand All @@ -233,6 +232,8 @@ pub struct TendermintCoinImpl {
pub(crate) history_sync_state: Mutex<HistorySyncState>,
client: TendermintRpcClient,
chain_registry_name: Option<String>,
/// A flag used for controlling the parent coin's mode(active/passive).
is_available: AtomicBool,
}

#[derive(Clone)]
Expand Down Expand Up @@ -504,7 +505,6 @@ impl TendermintCoin {
})?;

Ok(TendermintCoin(Arc::new(TendermintCoinImpl {
is_available: AtomicBool::new(true),
ticker,
account_id,
account_prefix: protocol_info.account_prefix,
Expand All @@ -519,6 +519,7 @@ impl TendermintCoin {
history_sync_state: Mutex::new(history_sync_state),
client: TendermintRpcClient(AsyncMutex::new(client_impl)),
chain_registry_name: protocol_info.chain_registry_name,
is_available: AtomicBool::new(true),
})))
}

Expand Down Expand Up @@ -2045,7 +2046,7 @@ impl MmCoin for TendermintCoin {

fn is_available(&self) -> bool { self.is_available.load(Ordering::SeqCst) }

fn passive_it(&self) { self.is_available.store(false, Ordering::SeqCst); }
fn update_is_available(&self, to: bool) { self.is_available.store(to, Ordering::SeqCst); }
}

impl MarketCoinOps for TendermintCoin {
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/tendermint/tendermint_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ impl MmCoin for TendermintToken {

fn is_available(&self) -> bool { true }

fn passive_it(&self) {
fn update_is_available(&self, _to: bool) {
warn!("child token {} can't be passive", self.ticker());
}
}
8 changes: 5 additions & 3 deletions mm2src/coins/test_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{coin_errors::MyAddressError, BalanceFut, CanRefundHtlc, CheckIfMyPay
VerificationResult, WatcherOps, WatcherSearchForSwapTxSpendInput, WatcherValidatePaymentInput,
WatcherValidateTakerFeeInput, WithdrawFut, WithdrawRequest};
use async_trait::async_trait;
use common::executor::AbortedError;
use common::{executor::AbortedError, log::warn};
use futures01::Future;
use keys::KeyPair;
use mm2_core::mm_ctx::MmArc;
Expand Down Expand Up @@ -360,7 +360,9 @@ impl MmCoin for TestCoin {

fn on_token_deactivated(&self, _ticker: &str) { () }

fn is_available(&self) -> bool { todo!() }
fn is_available(&self) -> bool { true }

fn passive_it(&self) { todo!() }
fn update_is_available(&self, _to: bool) {
warn!("`update_is_available` is ineffective for test coin");
}
}
2 changes: 2 additions & 0 deletions mm2src/coins/utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,8 @@ pub struct UtxoCoinFields {
/// This abortable system is used to spawn coin's related futures that should be aborted on coin deactivation
/// and on [`MmArc::stop`].
pub abortable_system: AbortableQueue,
/// A flag used for controlling the parent coin's mode(active/passive).
pub(crate) is_available: AtomicBool,
}

#[derive(Debug, Display)]
Expand Down
5 changes: 3 additions & 2 deletions mm2src/coins/utxo/bch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use mm2_metrics::MetricsArc;
use mm2_number::MmNumber;
use serde_json::{self as json, Value as Json};
use serialization::{deserialize, CoinVariant};
use std::sync::atomic::Ordering;
use std::sync::MutexGuard;

pub type BchUnspentMap = HashMap<Address, BchUnspents>;
Expand Down Expand Up @@ -1286,9 +1287,9 @@ impl MmCoin for BchCoin {
};
}

fn is_available(&self) -> bool { todo!() }
fn is_available(&self) -> bool { self.as_ref().is_available.load(Ordering::SeqCst) }

fn passive_it(&self) { todo!() }
fn update_is_available(&self, to: bool) { self.as_ref().is_available.store(to, Ordering::SeqCst); }
}

impl CoinWithDerivationMethod for BchCoin {
Expand Down
5 changes: 3 additions & 2 deletions mm2src/coins/utxo/qtum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use mm2_metrics::MetricsArc;
use mm2_number::MmNumber;
use serde::Serialize;
use serialization::CoinVariant;
use std::sync::atomic::Ordering;
use utxo_signer::UtxoSignerOps;

#[derive(Debug, Display)]
Expand Down Expand Up @@ -972,9 +973,9 @@ impl MmCoin for QtumCoin {

fn on_token_deactivated(&self, _ticker: &str) {}

fn is_available(&self) -> bool { todo!() }
fn is_available(&self) -> bool { self.as_ref().is_available.load(Ordering::SeqCst) }

fn passive_it(&self) { todo!() }
fn update_is_available(&self, to: bool) { self.as_ref().is_available.store(to, Ordering::SeqCst); }
}

#[async_trait]
Expand Down
6 changes: 4 additions & 2 deletions mm2src/coins/utxo/slp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1873,9 +1873,11 @@ impl MmCoin for SlpToken {

fn on_token_deactivated(&self, _ticker: &str) {}

fn is_available(&self) -> bool { todo!() }
fn is_available(&self) -> bool { true }

fn passive_it(&self) { todo!() }
fn update_is_available(&self, _to: bool) {
warn!("child token {} can't be passive", self.ticker());
}
}

#[async_trait]
Expand Down
3 changes: 3 additions & 0 deletions mm2src/coins/utxo/utxo_builder/utxo_coin_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use serde_json::{self as json, Value as Json};
use spv_validation::conf::SPVConf;
use spv_validation::helpers_validation::SPVError;
use spv_validation::storage::{BlockHeaderStorageError, BlockHeaderStorageOps};
use std::sync::atomic::AtomicBool;
use std::sync::{Arc, Mutex, Weak};

cfg_native! {
Expand Down Expand Up @@ -225,6 +226,7 @@ where
block_headers_status_notifier,
block_headers_status_watcher,
abortable_system,
is_available: AtomicBool::new(true),
};
Ok(coin)
}
Expand Down Expand Up @@ -298,6 +300,7 @@ pub trait UtxoFieldsWithHardwareWalletBuilder: UtxoCoinBuilderCommonOps {
block_headers_status_notifier,
block_headers_status_watcher,
abortable_system,
is_available: AtomicBool::new(true),
};
Ok(coin)
}
Expand Down
Loading

0 comments on commit 6b6c4ed

Please sign in to comment.