Skip to content
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
9f74dcf
implement stremaing infrastructure
onur-ozkan Aug 23, 2023
8e688c1
create basic client for SSE testing
onur-ozkan Aug 23, 2023
6c1998d
update examples README
onur-ozkan Aug 23, 2023
5ee822b
implement client-side event filtering
onur-ozkan Aug 23, 2023
b8e997a
add explanatory comments in sse module
onur-ozkan Aug 23, 2023
04df80c
update SSE client example documentation
onur-ozkan Aug 23, 2023
fae50e4
format wasm html example
onur-ozkan Aug 23, 2023
b8eca5e
stream network events
onur-ozkan Aug 24, 2023
be685af
manage event streaming dynamically
onur-ozkan Aug 28, 2023
e6ba1c3
leave perf improvement TODO note
onur-ozkan Aug 28, 2023
c58a7d5
avoid serializing events on http side
onur-ozkan Aug 29, 2023
79245d1
fix wasm error
onur-ozkan Aug 29, 2023
99f87c8
update SSE data type
onur-ozkan Aug 29, 2023
90996c5
implement `EventBehaviour`
onur-ozkan Aug 29, 2023
8445f35
save configuration in mm2 context
onur-ozkan Aug 29, 2023
7d727fe
update doc-comment of `stream_channel_controller` in `MmCtx`
onur-ozkan Aug 29, 2023
2e942af
optimize `mm2src/mm2_event_stream/src/lib.rs`
onur-ozkan Sep 11, 2023
c761a39
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into pu…
onur-ozkan Sep 13, 2023
6cd2c2c
cross-compatible tests for streaming channels
onur-ozkan Sep 14, 2023
a7a2064
enable wasm tests to be executed with wasm-pack
onur-ozkan Sep 15, 2023
f6056ea
implement WASM support for event streaming
onur-ozkan Sep 18, 2023
1e476d2
[WIP] concurrent balance loop controller
onur-ozkan Sep 21, 2023
945fece
add explanation comment lines
onur-ozkan Sep 21, 2023
2d68ca9
create `CoinBalanceEvent`
onur-ozkan Sep 22, 2023
61668ac
impl `handle_balance_stream` for `MmCoin` trait
onur-ozkan Sep 22, 2023
619bac8
update interval rule for coin balance event
onur-ozkan Sep 22, 2023
eaac0c1
save dev state
onur-ozkan Sep 25, 2023
212f4dd
save dev state
onur-ozkan Sep 25, 2023
188dade
WIP parse denoms from ws events
onur-ozkan Sep 25, 2023
f95568c
impl address handling and TLS support
onur-ozkan Sep 25, 2023
041aebb
denom to ticker conversion
onur-ozkan Sep 26, 2023
0f712e3
broadcast events when balance is changed
onur-ozkan Sep 26, 2023
633987a
create `http_uri_to_ws_address`
onur-ozkan Sep 26, 2023
eff5c9a
handle socket related panics/connection failures
onur-ozkan Sep 26, 2023
8a0577c
use komodo fork of websocket lib for WASM support
onur-ozkan Sep 26, 2023
8f478a4
get full compatibility on native + WASM sockets
onur-ozkan Sep 27, 2023
a2d6295
enable rustls feature
onur-ozkan Sep 27, 2023
e626950
broadcast network events only when data changes
onur-ozkan Sep 28, 2023
3758cdc
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into sh…
onur-ozkan Sep 29, 2023
6f668e2
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into sh…
onur-ozkan Oct 2, 2023
c3afd29
refactor balance stream impl design
onur-ozkan Oct 4, 2023
5464064
ref `active_ticker_and_decimals_from_denom` to be O(1)
onur-ozkan Oct 4, 2023
3c50310
replace plain json with `jsonrpc_core::MethodCall`
onur-ozkan Oct 4, 2023
35e58e4
explain how to examine incoming data from tendermint socket
onur-ozkan Oct 4, 2023
0e41ae7
update helper logs for Tendermint COIN_BALANCE event
onur-ozkan Oct 4, 2023
7e19003
fix minor notes
onur-ozkan Oct 5, 2023
799e4e8
upgrade from weak pointer for Tendermint to Mm context
onur-ozkan Oct 6, 2023
dbb2b22
handle event initializations
onur-ozkan Oct 9, 2023
d7bd0d7
remove `NotInitialized` variant in `EventInitStatus`
onur-ozkan Oct 9, 2023
7ccf765
return EventInitStatus::Failed if tx isn't present
onur-ozkan Oct 9, 2023
28048c8
Merge remote-tracking branch 'origin/dev' into shared-event-channels-…
shamardy Oct 27, 2023
b3d774c
update adex-cli cargo.lock
shamardy Oct 27, 2023
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
86 changes: 86 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions mm2src/coins/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ ethereum-types = { version = "0.13", default-features = false, features = ["std"
ethkey = { git = "https://github.com/KomodoPlatform/mm2-parity-ethereum.git" }
# Waiting for https://github.com/rust-lang/rust/issues/54725 to use on Stable.
#enum_dispatch = "0.1"
tokio-tungstenite-wasm = { git = "https://github.com/KomodoPlatform/tokio-tungstenite-wasm", rev = "d20abdb", features = ["rustls-tls-native-roots"]}
futures01 = { version = "0.1", package = "futures" }
futures-util = { version = "0.3", default-features = false, features = ["sink", "std"] }
# using select macro requires the crate to be named futures, compilation failed with futures03 name
futures = { version = "0.3", package = "futures", features = ["compat", "async-await"] }
group = "0.8.0"
Expand All @@ -63,6 +65,7 @@ lazy_static = "1.4"
libc = "0.2"
mm2_core = { path = "../mm2_core" }
mm2_err_handle = { path = "../mm2_err_handle" }
mm2_event_stream = { path = "../mm2_event_stream" }
mm2_git = { path = "../mm2_git" }
mm2_io = { path = "../mm2_io" }
mm2_metrics = { path = "../mm2_metrics" }
Expand Down
124 changes: 124 additions & 0 deletions mm2src/coins/coin_balance_event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
use crate::{CoinsContext, MmCoin, MmCoinEnum};
use async_trait::async_trait;
use common::{executor::{SpawnFuture, Timer},
log::info};
use mm2_core::mm_ctx::MmArc;
use mm2_event_stream::{behaviour::EventBehaviour, EventStreamConfiguration};
use std::sync::atomic::Ordering;

/// Event tag for broadcasting balance events
pub(crate) const COIN_BALANCE_EVENT_TAG: &str = "COIN_BALANCE";

pub struct CoinBalanceEvent {
ctx: MmArc,
}

impl CoinBalanceEvent {
pub fn new(ctx: MmArc) -> Self { Self { ctx } }
}

#[async_trait]
impl EventBehaviour for CoinBalanceEvent {
const EVENT_NAME: &'static str = COIN_BALANCE_EVENT_TAG;

async fn handle(self, _interval: f64) {
let cctx = CoinsContext::from_ctx(&self.ctx).expect("Unexpected internal panic.");

// Events that are already fired
let mut event_pool: Vec<String> = vec![];
Comment thread
onur-ozkan marked this conversation as resolved.
Outdated

loop {
let coins_mutex = cctx.coins.lock().await;
Comment thread
onur-ozkan marked this conversation as resolved.
Outdated

let coins: Vec<MmCoinEnum> = coins_mutex
.values()
.filter_map(|coin| {
// We loop this over and over, so it's not necessary to sequentially load the atomics all over
// the threads, since the cost of it is way too higher than the `AtomicOrdering::Relaxed`
if coin.is_available.load(Ordering::Relaxed) {
Some(coin.inner.clone())
} else {
None
}
})
.collect();
Comment thread
onur-ozkan marked this conversation as resolved.
Outdated

// Similar to above, we don't need to held the lock(which will block all other processes that depends
// on this lock(like coin activation)) since we loop this over continuously.
drop(coins_mutex);

// Handle balance streaming concurrently for each coin
for coin in coins {
let ticker = coin.ticker().to_owned();

if event_pool.contains(&ticker) {
continue;
}

match coin {
MmCoinEnum::UtxoCoin(inner) => {
self.ctx.spawner().spawn(inner.handle_balance_stream(self.ctx.clone()))
},
MmCoinEnum::QtumCoin(inner) => {
self.ctx.spawner().spawn(inner.handle_balance_stream(self.ctx.clone()))
},
MmCoinEnum::Qrc20Coin(inner) => {
self.ctx.spawner().spawn(inner.handle_balance_stream(self.ctx.clone()))
},
MmCoinEnum::EthCoin(inner) => {
self.ctx.spawner().spawn(inner.handle_balance_stream(self.ctx.clone()))
},
MmCoinEnum::ZCoin(inner) => self.ctx.spawner().spawn(inner.handle_balance_stream(self.ctx.clone())),
MmCoinEnum::Bch(inner) => self.ctx.spawner().spawn(inner.handle_balance_stream(self.ctx.clone())),
MmCoinEnum::SlpToken(inner) => {
self.ctx.spawner().spawn(inner.handle_balance_stream(self.ctx.clone()))
},
MmCoinEnum::Tendermint(inner) => {
self.ctx.spawner().spawn(inner.handle_balance_stream(self.ctx.clone()))
Comment thread
onur-ozkan marked this conversation as resolved.
Outdated
},
MmCoinEnum::TendermintToken(inner) => {
self.ctx.spawner().spawn(inner.handle_balance_stream(self.ctx.clone()))
},
#[cfg(not(target_arch = "wasm32"))]
MmCoinEnum::LightningCoin(inner) => {
self.ctx.spawner().spawn(inner.handle_balance_stream(self.ctx.clone()))
},
MmCoinEnum::Test(inner) => self.ctx.spawner().spawn(inner.handle_balance_stream(self.ctx.clone())),
#[cfg(all(
feature = "enable-solana",
not(target_os = "ios"),
not(target_os = "android"),
not(target_arch = "wasm32")
))]
MmCoinEnum::SolanaCoin(inner) => {
self.ctx.spawner().spawn(inner.handle_balance_stream(self.ctx.clone()))
},
#[cfg(all(
feature = "enable-solana",
not(target_os = "ios"),
not(target_os = "android"),
not(target_arch = "wasm32")
))]
MmCoinEnum::SplToken(inner) => {
self.ctx.spawner().spawn(inner.handle_balance_stream(self.ctx.clone()))
},
}

event_pool.push(ticker);
Comment thread
onur-ozkan marked this conversation as resolved.
Outdated
}

Timer::sleep(5.).await;
Comment thread
onur-ozkan marked this conversation as resolved.
Outdated
}
}

fn spawn_if_active(self, config: &EventStreamConfiguration) {
if let Some(event) = config.get_event(Self::EVENT_NAME) {
info!(
"{} event is activated. `stream_interval_seconds`({}) has no effect for this event.",
Self::EVENT_NAME,
event.stream_interval_seconds
);
self.ctx.spawner().spawn(self.handle(event.stream_interval_seconds));
}
}
}
2 changes: 2 additions & 0 deletions mm2src/coins/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4743,6 +4743,8 @@ impl MmCoin for EthCoin {
tokens.remove(ticker);
};
}

async fn handle_balance_stream(self, _ctx: MmArc) { todo!() }
}

pub trait TryToAddress {
Expand Down
2 changes: 2 additions & 0 deletions mm2src/coins/lightning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1452,4 +1452,6 @@ impl MmCoin for LightningCoin {
fn on_disabled(&self) -> Result<(), AbortedError> { AbortableSystem::abort_all(&self.platform.abortable_system) }

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

async fn handle_balance_stream(self, _ctx: MmArc) { todo!() }
}
5 changes: 5 additions & 0 deletions mm2src/coins/lp_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ pub use test_coin::TestCoin;

pub mod tx_history_storage;

pub mod coin_balance_event;

#[doc(hidden)]
#[allow(unused_variables)]
#[cfg(all(
Expand Down Expand Up @@ -2576,6 +2578,9 @@ pub trait MmCoin:

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

// Handler for coin balance streaming continuously to the thread/stream channels
async fn handle_balance_stream(self, ctx: MmArc);
Comment thread
onur-ozkan marked this conversation as resolved.
Outdated
}

/// The coin futures spawner. It's used to spawn futures that can be aborted immediately or after a timeout
Expand Down
2 changes: 2 additions & 0 deletions mm2src/coins/qrc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,8 @@ impl MmCoin for Qrc20Coin {
fn on_disabled(&self) -> Result<(), AbortedError> { AbortableSystem::abort_all(&self.as_ref().abortable_system) }

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

async fn handle_balance_stream(self, _ctx: MmArc) { todo!() }
}

pub fn qrc20_swap_id(time_lock: u32, secret_hash: &[u8]) -> Vec<u8> {
Expand Down
2 changes: 2 additions & 0 deletions mm2src/coins/solana.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,4 +776,6 @@ impl MmCoin for SolanaCoin {
fn on_disabled(&self) -> Result<(), AbortedError> { AbortableSystem::abort_all(&self.abortable_system) }

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

async fn handle_balance_stream(self, _ctx: MmArc) { todo!() }
}
2 changes: 2 additions & 0 deletions mm2src/coins/solana/spl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,4 +570,6 @@ impl MmCoin for SplToken {
fn on_disabled(&self) -> Result<(), AbortedError> { self.conf.abortable_system.abort_all() }

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

async fn handle_balance_stream(self, _ctx: MmArc) { todo!() }
}
10 changes: 10 additions & 0 deletions mm2src/coins/tendermint/rpc/tendermint_native_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,9 @@ impl HttpClient {
},
})
}

#[inline]
pub fn uri(&self) -> http::Uri { self.inner.uri() }
}

#[async_trait]
Expand Down Expand Up @@ -481,6 +484,13 @@ mod sealed {
HttpClient::Https(c) => c.perform(request).await,
}
}

pub fn uri(&self) -> Uri {
match self {
HttpClient::Http(client) => client.uri.clone(),
HttpClient::Https(client) => client.uri.clone(),
}
}
}

async fn response_to_string(response: hyper::Response<hyper::Body>) -> Result<String, Error> {
Expand Down
3 changes: 3 additions & 0 deletions mm2src/coins/tendermint/rpc/tendermint_wasm_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ impl HttpClient {
Ok(HttpClient { uri: url.to_owned() })
}

#[inline]
pub fn uri(&self) -> http::Uri { Uri::from_str(&self.uri).expect("This should never happen.") }

pub(crate) async fn perform<R>(&self, request: R) -> Result<R::Response, PerformError>
where
R: SimpleRequest,
Expand Down
Loading