Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into eth-maker-tpu-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
laruh committed Sep 19, 2024
2 parents 5d9d1de + baa72a7 commit d57abdb
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 73 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ opt-level = 3
strip = true
codegen-units = 1
# lto = true
panic = "abort"
panic = 'unwind'

[profile.dev]
opt-level = 0
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

## What is the Komodo DeFi Framework?

The Komodo DeFi Framework is open-source [atomic-swap](https://komodoplatform.com/en/academy/atomic-swaps/) software for seamless, decentralised, peer to peer trading between almost every blockchain asset in existence. This software works with propagation of orderbooks and swap states through the [libp2p](https://libp2p.io/) protocol and uses [Hash Time Lock Contracts (HTLCs)](https://en.bitcoinwiki.org/wiki/Hashed_Timelock_Contracts) for ensuring that the two parties in a swap either mutually complete a trade, or funds return to thier original owner.
The Komodo DeFi Framework is open-source [atomic-swap](https://komodoplatform.com/en/academy/atomic-swaps/) software for seamless, decentralized, peer to peer trading between almost every blockchain asset in existence. This software works with propagation of orderbooks and swap states through the [libp2p](https://libp2p.io/) protocol and uses [Hash Time Lock Contracts (HTLCs)](https://en.bitcoinwiki.org/wiki/Hashed_Timelock_Contracts) for ensuring that the two parties in a swap either mutually complete a trade, or funds return to thier original owner.

There is no 3rd party intermediary, no proxy tokens, and at all times users remain in sole possession of their private keys.

Expand Down Expand Up @@ -172,7 +172,7 @@ Refer to the [Komodo Developer Docs](https://developers.komodoplatform.com/basic

## Project structure

[mm2src](mm2src) - Rust code, contains some parts ported from C `as is` (e.g. `lp_ordermatch`) to reach the most essential/error prone code. Some other modules/crates are reimplemented from scratch.
[mm2src](mm2src) - Rust code, contains some parts ported from C `as is` (e.g. `lp_ordermatch`) to reach the most essential/error-prone code. Some other modules/crates are reimplemented from scratch.


## Additional docs for developers
Expand All @@ -185,8 +185,8 @@ Refer to the [Komodo Developer Docs](https://developers.komodoplatform.com/basic

## Disclaimer

This repository contains the `work in progress` code of the brand new Komodo DeFi Framework (kdf) built mainly on Rust.
The current state can be considered as a alpha version.
This repository contains the `work in progress` code of the brand-new Komodo DeFi Framework (kdf) built mainly on Rust.
The current state can be considered as an alpha version.

**<b>WARNING: Use with test coins only or with assets which value does not exceed an amount you are willing to lose. This is alpha stage software! </b>**

Expand Down
13 changes: 0 additions & 13 deletions mm2src/common/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,19 +518,6 @@ pub fn set_panic_hook() {
}))
}

/// Simulates the panic-in-panic crash.
pub fn double_panic_crash() {
struct Panicker;
impl Drop for Panicker {
fn drop(&mut self) { panic!("panic in drop") }
}
let panicker = Panicker;
if 1 < 2 {
panic!("first panic")
}
drop(panicker) // Delays the drop.
}

/// RPC response, returned by the RPC handlers.
/// NB: By default the future is executed on the shared asynchronous reactor (`CORE`),
/// the handler is responsible for spawning the future on another reactor if it doesn't fit the `CORE` well.
Expand Down
5 changes: 0 additions & 5 deletions mm2src/mm2_bin_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ authors = ["James Lee", "Artem Pikulin", "Artem Grinblat", "Omar S.", "Onur Ozka
edition = "2018"
default-run = "kdf"

# wasm-opt reduces the size from 17 Mb to 14. But it runs for few minutes, which is not good for CI.
# For production builds, it's recommended to run wasm-opt separately.
[package.metadata.wasm-pack.profile.release]
wasm-opt = false

[features]
custom-swap-locktime = ["mm2_main/custom-swap-locktime"] # only for testing purposes, should never be activated on release builds.
native = ["mm2_main/native"] # Deprecated
Expand Down
2 changes: 1 addition & 1 deletion mm2src/mm2_bin_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,5 @@ fn prepare_for_mm2_stop() -> PrepareForStopResult {

async fn finalize_mm2_stop(ctx: MmArc) {
dispatch_lp_event(ctx.clone(), StopCtxEvent.into()).await;
let _ = ctx.stop();
let _ = ctx.stop().await;
}
22 changes: 19 additions & 3 deletions mm2src/mm2_core/src/mm_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,13 @@ impl MmCtx {
})
}

/// Returns the path to the MM databases root.
#[cfg(not(target_arch = "wasm32"))]
pub fn db_root(&self) -> PathBuf { path_to_db_root(self.conf["dbdir"].as_str()) }

#[cfg(not(target_arch = "wasm32"))]
pub fn wallet_file_path(&self, wallet_name: &str) -> PathBuf {
let db_root = path_to_db_root(self.conf["dbdir"].as_str());
db_root.join(wallet_name.to_string() + ".dat")
self.db_root().join(wallet_name.to_string() + ".dat")
}

/// MM database path.
Expand Down Expand Up @@ -500,7 +503,10 @@ lazy_static! {
impl MmArc {
pub fn new(ctx: MmCtx) -> MmArc { MmArc(SharedRc::new(ctx)) }

pub fn stop(&self) -> Result<(), String> {
pub async fn stop(&self) -> Result<(), String> {
#[cfg(not(target_arch = "wasm32"))]
try_s!(self.close_async_connection().await);

try_s!(self.stop.pin(true));

// Notify shutdown listeners.
Expand All @@ -514,6 +520,16 @@ impl MmArc {
Ok(())
}

#[cfg(not(target_arch = "wasm32"))]
async fn close_async_connection(&self) -> Result<(), db_common::async_sql_conn::AsyncConnError> {
if let Some(async_conn) = self.async_sqlite_connection.as_option() {
let mut conn = async_conn.lock().await;
conn.close().await?;
}

Ok(())
}

#[cfg(feature = "track-ctx-pointer")]
fn track_ctx_pointer(&self) {
let ctx_weak = self.weak();
Expand Down
4 changes: 2 additions & 2 deletions mm2src/mm2_libp2p/src/atomicdex_behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ const ANNOUNCE_INTERVAL: Duration = Duration::from_secs(600);
const ANNOUNCE_INITIAL_DELAY: Duration = Duration::from_secs(60);
const CHANNEL_BUF_SIZE: usize = 1024 * 8;

/// Returns info about connected peers
pub async fn get_peers_info(mut cmd_tx: AdexCmdTx) -> BTreeMap<String, Vec<String>> {
/// Returns info about directly connected peers.
pub async fn get_directly_connected_peers(mut cmd_tx: AdexCmdTx) -> BTreeMap<String, Vec<String>> {
let (result_tx, rx) = oneshot::channel();
let cmd = AdexBehaviourCmd::GetPeersInfo { result_tx };
cmd_tx.send(cmd).await.expect("Rx should be present");
Expand Down
2 changes: 1 addition & 1 deletion mm2src/mm2_main/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ hyper = { version = "0.14.26", features = ["client", "http2", "server", "tcp"] }
rcgen = "0.10"
rustls = { version = "0.21", default-features = false }
rustls-pemfile = "1.0.2"
tokio = { version = "1.20", features = ["io-util", "rt-multi-thread", "net"] }
tokio = { version = "1.20", features = ["io-util", "rt-multi-thread", "net", "signal"] }

[target.'cfg(windows)'.dependencies]
winapi = "0.3"
Expand Down
4 changes: 3 additions & 1 deletion mm2src/mm2_main/src/lp_native_dex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,9 +498,11 @@ pub async fn lp_init_continue(ctx: MmArc) -> MmInitResult<()> {
pub async fn lp_init(ctx: MmArc, version: String, datetime: String) -> MmInitResult<()> {
info!("Version: {} DT {}", version, datetime);

// Ensure the database root directory exists before initializing the wallet passphrase.
// This is necessary to store the encrypted wallet passphrase if needed.
#[cfg(not(target_arch = "wasm32"))]
{
let dbdir = ctx.dbdir();
let dbdir = ctx.db_root();
fs::create_dir_all(&dbdir).map_to_mm(|e| MmInitError::ErrorCreatingDbDir {
path: dbdir.clone(),
error: e.to_string(),
Expand Down
38 changes: 25 additions & 13 deletions mm2src/mm2_main/src/mm2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

#[cfg(not(target_arch = "wasm32"))] use common::block_on;
use common::crash_reports::init_crash_reports;
use common::double_panic_crash;
use common::log;
use common::log::LogLevel;
use common::password_policy::password_policy;
use mm2_core::mm_ctx::MmCtxBuilder;
Expand All @@ -54,7 +54,6 @@ use lp_swap::PAYMENT_LOCKTIME;
use std::sync::atomic::Ordering;

use gstuff::slurp;

use serde::ser::Serialize;
use serde_json::{self as json, Value as Json};

Expand All @@ -64,7 +63,6 @@ use std::process::exit;
use std::ptr::null;
use std::str;

mod lp_native_dex;
pub use self::lp_native_dex::init_hw;
pub use self::lp_native_dex::lp_init;
use coins::update_coins_config;
Expand All @@ -75,6 +73,7 @@ use mm2_err_handle::prelude::*;
pub mod heartbeat_event;
pub mod lp_dispatcher;
pub mod lp_message_service;
mod lp_native_dex;
pub mod lp_network;
pub mod lp_ordermatch;
pub mod lp_stats;
Expand Down Expand Up @@ -160,10 +159,33 @@ pub async fn lp_main(
.with_datetime(datetime.clone())
.into_mm_arc();
ctx_cb(try_s!(ctx.ffi_handle()));

#[cfg(not(target_arch = "wasm32"))]
spawn_ctrl_c_handler(ctx.clone());

try_s!(lp_init(ctx, version, datetime).await);
Ok(())
}

/// Handles CTRL-C signals and shutdowns the KDF runtime gracefully.
///
/// It's important to spawn this task as soon as `Ctx` is in the correct state.
#[cfg(not(target_arch = "wasm32"))]
fn spawn_ctrl_c_handler(ctx: mm2_core::mm_ctx::MmArc) {
use crate::lp_dispatcher::{dispatch_lp_event, StopCtxEvent};

common::executor::spawn(async move {
tokio::signal::ctrl_c()
.await
.expect("Couldn't listen for the CTRL-C signal.");

log::info!("Wrapping things up and shutting down...");

dispatch_lp_event(ctx.clone(), StopCtxEvent.into()).await;
ctx.stop().await.expect("Couldn't stop the KDF runtime.");
});
}

fn help() {
const HELP_MSG: &str = r#"Command-line options.
The first command-line argument is special and designates the mode.
Expand Down Expand Up @@ -248,16 +270,6 @@ pub fn mm2_main(version: String, datetime: String) {
// we're not checking them for the mode switches in order not to risk [untrusted] data being mistaken for a mode switch.
let first_arg = args_os.get(1).and_then(|arg| arg.to_str());

if first_arg == Some("panic") {
panic!("panic message")
}
if first_arg == Some("crash") {
double_panic_crash()
}
if first_arg == Some("stderr") {
eprintln!("This goes to stderr");
return;
}
if first_arg == Some("update_config") {
match on_update_config(&args_os) {
Ok(_) => println!("Success"),
Expand Down
2 changes: 1 addition & 1 deletion mm2src/mm2_main/src/rpc/dispatcher/dispatcher_legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ pub fn dispatcher(req: Json, ctx: MmArc) -> DispatcherRes {
"electrum" => hyres(electrum(ctx, req)),
"enable" => hyres(enable(ctx, req)),
"get_enabled_coins" => hyres(get_enabled_coins(ctx)),
"get_directly_connected_peers" => hyres(get_directly_connected_peers(ctx)),
"get_gossip_mesh" => hyres(get_gossip_mesh(ctx)),
"get_gossip_peer_topics" => hyres(get_gossip_peer_topics(ctx)),
"get_gossip_topic_peers" => hyres(get_gossip_topic_peers(ctx)),
"get_my_peer_id" => hyres(get_my_peer_id(ctx)),
"get_peers_info" => hyres(get_peers_info(ctx)),
"get_relay_mesh" => hyres(get_relay_mesh(ctx)),
"get_trade_fee" => hyres(get_trade_fee(ctx, req)),
// "fundvalue" => lp_fundvalue (ctx, req, false),
Expand Down
23 changes: 3 additions & 20 deletions mm2src/mm2_main/src/rpc/lp_commands/lp_commands_legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

use coins::{lp_coinfind, lp_coinfind_any, lp_coininit, CoinsContext, MmCoinEnum};
use common::executor::Timer;
use common::log::error;
use common::{rpc_err_response, rpc_response, HyRes};
use futures::compat::Future01CompatExt;
use http::Response;
Expand Down Expand Up @@ -242,29 +241,13 @@ pub async fn my_balance(ctx: MmArc, req: Json) -> Result<Response<Vec<u8>>, Stri
Ok(try_s!(Response::builder().body(res)))
}

#[cfg(not(target_arch = "wasm32"))]
async fn close_async_connection(ctx: &MmArc) {
if let Some(async_conn) = ctx.async_sqlite_connection.as_option() {
let mut conn = async_conn.lock().await;
if let Err(e) = conn.close().await {
error!("Error stopping AsyncConnection: {}", e);
}
}
}

pub async fn stop(ctx: MmArc) -> Result<Response<Vec<u8>>, String> {
dispatch_lp_event(ctx.clone(), StopCtxEvent.into()).await;
// Should delay the shutdown a bit in order not to trip the "stop" RPC call in unit tests.
// Stopping immediately leads to the "stop" RPC call failing with the "errno 10054" sometimes.
let fut = async move {
Timer::sleep(0.05).await;

#[cfg(not(target_arch = "wasm32"))]
close_async_connection(&ctx).await;

if let Err(e) = ctx.stop() {
error!("Error stopping MmCtx: {}", e);
}
ctx.stop().await.expect("Couldn't stop the KDF runtime.");
};

// Please note we shouldn't use `MmCtx::spawner` to spawn this future,
Expand Down Expand Up @@ -316,10 +299,10 @@ pub fn version(ctx: MmArc) -> HyRes {
}
}

pub async fn get_peers_info(ctx: MmArc) -> Result<Response<Vec<u8>>, String> {
pub async fn get_directly_connected_peers(ctx: MmArc) -> Result<Response<Vec<u8>>, String> {
let ctx = P2PContext::fetch_from_mm_arc(&ctx);
let cmd_tx = ctx.cmd_tx.lock().clone();
let result = mm2_libp2p::get_peers_info(cmd_tx).await;
let result = mm2_libp2p::get_directly_connected_peers(cmd_tx).await;
let result = json!({
"result": result,
});
Expand Down
4 changes: 2 additions & 2 deletions mm2src/mm2_net/src/network_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ impl EventBehaviour for NetworkEvent {
loop {
let p2p_cmd_tx = p2p_ctx.cmd_tx.lock().clone();

let peers_info = atomicdex::get_peers_info(p2p_cmd_tx.clone()).await;
let directly_connected_peers = atomicdex::get_directly_connected_peers(p2p_cmd_tx.clone()).await;
let gossip_mesh = atomicdex::get_gossip_mesh(p2p_cmd_tx.clone()).await;
let gossip_peer_topics = atomicdex::get_gossip_peer_topics(p2p_cmd_tx.clone()).await;
let gossip_topic_peers = atomicdex::get_gossip_topic_peers(p2p_cmd_tx.clone()).await;
let relay_mesh = atomicdex::get_relay_mesh(p2p_cmd_tx).await;

let event_data = json!({
"peers_info": peers_info,
"directly_connected_peers": directly_connected_peers,
"gossip_mesh": gossip_mesh,
"gossip_peer_topics": gossip_peer_topics,
"gossip_topic_peers": gossip_topic_peers,
Expand Down
4 changes: 2 additions & 2 deletions mm2src/mm2_p2p/src/behaviours/atomicdex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ pub enum AdexBehaviourCmd {
},
}

/// Returns info about connected peers
pub async fn get_peers_info(mut cmd_tx: AdexCmdTx) -> HashMap<String, Vec<String>> {
/// Returns info about directly connected peers.
pub async fn get_directly_connected_peers(mut cmd_tx: AdexCmdTx) -> HashMap<String, Vec<String>> {
let (result_tx, rx) = oneshot::channel();
let cmd = AdexBehaviourCmd::GetPeersInfo { result_tx };
cmd_tx.send(cmd).await.expect("Rx should be present");
Expand Down
9 changes: 5 additions & 4 deletions mm2src/mm2_p2p/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ use sha2::{Digest, Sha256};
pub use crate::swarm_runtime::SwarmRuntime;

// atomicdex related re-exports
pub use behaviours::atomicdex::{get_gossip_mesh, get_gossip_peer_topics, get_gossip_topic_peers, get_peers_info,
get_relay_mesh, spawn_gossipsub, AdexBehaviourCmd, AdexBehaviourError,
AdexBehaviourEvent, AdexCmdTx, AdexEventRx, AdexResponse, AdexResponseChannel,
GossipsubEvent, GossipsubMessage, MessageId, NodeType, TopicHash, WssCerts};
pub use behaviours::atomicdex::{get_directly_connected_peers, get_gossip_mesh, get_gossip_peer_topics,
get_gossip_topic_peers, get_relay_mesh, spawn_gossipsub, AdexBehaviourCmd,
AdexBehaviourError, AdexBehaviourEvent, AdexCmdTx, AdexEventRx, AdexResponse,
AdexResponseChannel, GossipsubEvent, GossipsubMessage, MessageId, NodeType, TopicHash,
WssCerts};

// peers-exchange re-exports
pub use behaviours::peers_exchange::PeerAddresses;
Expand Down

0 comments on commit d57abdb

Please sign in to comment.