Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 3 additions & 2 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions mm2src/coins/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ mm2_db = { path = "../mm2_db" }
mm2_metamask = { path = "../mm2_metamask" }
mm2_test_helpers = { path = "../mm2_test_helpers" }
time = { version = "0.3.20", features = ["wasm-bindgen"] }
timed-map = { version = "1.3", features = ["rustc-hash", "wasm"] }
timed-map = { version = "1.4", features = ["rustc-hash", "wasm"] }
tonic = { version = "0.10", default-features = false, features = ["prost", "codegen", "gzip"] }
tower-service = "0.3"
wasm-bindgen = "0.2.86"
Expand All @@ -148,7 +148,7 @@ lightning-net-tokio = "0.0.113"
rust-ini = { version = "0.13" }
rustls = { version = "0.21", features = ["dangerous_configuration"] }
secp256k1v24 = { version = "0.24", package = "secp256k1" }
timed-map = { version = "1.3", features = ["rustc-hash"] }
timed-map = { version = "1.4", features = ["rustc-hash"] }
tokio = { version = "1.20" }
tokio-rustls = { version = "0.24" }
tonic = { version = "0.10", features = ["tls", "tls-webpki-roots", "gzip"] }
Expand Down
4 changes: 2 additions & 2 deletions mm2src/mm2_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ uuid = { version = "1.2.2", features = ["fast-rng", "serde", "v4"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
mm2_rpc = { path = "../mm2_rpc", features = [ "rpc_facilities" ] }
timed-map = { version = "1.3", features = ["rustc-hash", "wasm"] }
timed-map = { version = "1.4", features = ["rustc-hash", "wasm"] }
wasm-bindgen-test = { version = "0.3.2" }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
rustls = { version = "0.21", default-features = false }
tokio = { version = "1.20", features = ["io-util", "rt-multi-thread", "net"] }
timed-map = { version = "1.3", features = ["rustc-hash"] }
timed-map = { version = "1.4", features = ["rustc-hash"] }
4 changes: 2 additions & 2 deletions mm2src/mm2_main/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ instant = { version = "0.1.12", features = ["wasm-bindgen"] }
js-sys = { version = "0.3.27" }
mm2_db = { path = "../mm2_db" }
mm2_test_helpers = { path = "../mm2_test_helpers" }
timed-map = { version = "1.3", features = ["rustc-hash", "wasm"] }
timed-map = { version = "1.4", features = ["rustc-hash", "wasm"] }
wasm-bindgen = "0.2.86"
wasm-bindgen-futures = { version = "0.4.1" }
wasm-bindgen-test = { version = "0.3.1" }
Expand All @@ -121,7 +121,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"
timed-map = { version = "1.3", features = ["rustc-hash"] }
timed-map = { version = "1.4", features = ["rustc-hash"] }
tokio = { version = "1.20", features = ["io-util", "rt-multi-thread", "net", "signal"] }

[target.'cfg(windows)'.dependencies]
Expand Down
4 changes: 2 additions & 2 deletions mm2src/mm2_main/src/lp_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ struct LockedAmountInfo {
struct SwapsContext {
running_swaps: Mutex<HashMap<Uuid, Arc<dyn AtomicSwap>>>,
active_swaps_v2_infos: Mutex<HashMap<Uuid, ActiveSwapV2Info>>,
banned_pubkeys: Mutex<HashMap<H256Json, BanReason>>,
banned_pubkeys: Mutex<TimedMap<H256Json, BanReason>>,
swap_msgs: Mutex<HashMap<Uuid, SwapMsgStore>>,
swap_v2_msgs: Mutex<HashMap<Uuid, SwapV2MsgStore>>,
taker_swap_watchers: PaMutex<TimedMap<Vec<u8>, ()>>,
Expand All @@ -548,7 +548,7 @@ impl SwapsContext {
Ok(SwapsContext {
running_swaps: Mutex::new(HashMap::new()),
active_swaps_v2_infos: Mutex::new(HashMap::new()),
banned_pubkeys: Mutex::new(HashMap::new()),
banned_pubkeys: Mutex::new(TimedMap::new_with_map_kind(MapKind::FxHashMap)),
swap_msgs: Mutex::new(HashMap::new()),
swap_v2_msgs: Mutex::new(HashMap::new()),
taker_swap_watchers: PaMutex::new(TimedMap::new_with_map_kind(MapKind::FxHashMap)),
Expand Down
61 changes: 43 additions & 18 deletions mm2src/mm2_main/src/lp_swap/pubkey_banning.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::collections::HashMap;

use super::{SwapEvent, SwapsContext};
use chain::hash::H256;
use compatible_time::Duration;
use http::Response;
use mm2_core::mm_ctx::MmArc;
use rpc::v1::types::H256 as H256Json;
use serde_json::{self as json, Value as Json};
use std::collections::hash_map::{Entry, HashMap};
use uuid::Uuid;

#[derive(Serialize)]
Expand All @@ -21,12 +23,19 @@ pub enum BanReason {
}

pub fn ban_pubkey_on_failed_swap(ctx: &MmArc, pubkey: H256, swap_uuid: &Uuid, event: SwapEvent) {
// Ban them for an hour.
const PENALTY: Duration = Duration::from_secs(60 * 60);

let ctx = SwapsContext::from_ctx(ctx).unwrap();
let mut banned = ctx.banned_pubkeys.lock().unwrap();
banned.insert(pubkey.into(), BanReason::FailedSwap {
caused_by_swap: *swap_uuid,
caused_by_event: event,
});
banned.insert_expirable(
pubkey.into(),
BanReason::FailedSwap {
caused_by_swap: *swap_uuid,
caused_by_event: event,
},
PENALTY,
);
}

pub fn is_pubkey_banned(ctx: &MmArc, pubkey: &H256Json) -> bool {
Expand All @@ -47,23 +56,33 @@ pub async fn list_banned_pubkeys_rpc(ctx: MmArc) -> Result<Response<Vec<u8>>, St
struct BanPubkeysReq {
pubkey: H256Json,
reason: String,
duration_min: Option<u32>,
}

pub async fn ban_pubkey_rpc(ctx: MmArc, req: Json) -> Result<Response<Vec<u8>>, String> {
let req: BanPubkeysReq = try_s!(json::from_value(req));
let ctx = try_s!(SwapsContext::from_ctx(&ctx));
let mut banned_pubs = try_s!(ctx.banned_pubkeys.lock());

match banned_pubs.entry(req.pubkey) {
Entry::Occupied(_) => ERR!("Pubkey is banned already"),
Entry::Vacant(entry) => {
entry.insert(BanReason::Manual { reason: req.reason });
let res = try_s!(json::to_vec(&json!({
"result": "success",
})));
Ok(try_s!(Response::builder().body(res)))
},
if banned_pubs.contains_key(&req.pubkey) {
return ERR!("Pubkey is banned already");
}

if let Some(duration_min) = req.duration_min {
banned_pubs.insert_expirable(
req.pubkey,
BanReason::Manual { reason: req.reason },
Duration::from_secs(duration_min as u64 * 60),
);
} else {
banned_pubs.insert_constant(req.pubkey, BanReason::Manual { reason: req.reason });
}

let res = try_s!(json::to_vec(&json!({
"result": "success",
})));

Response::builder().body(res).map_err(|e| e.to_string())
}

#[derive(Deserialize)]
Expand All @@ -77,13 +96,16 @@ pub async fn unban_pubkeys_rpc(ctx: MmArc, req: Json) -> Result<Response<Vec<u8>
let req: UnbanPubkeysReq = try_s!(json::from_value(req["unban_by"].clone()));
let ctx = try_s!(SwapsContext::from_ctx(&ctx));
let mut banned_pubs = try_s!(ctx.banned_pubkeys.lock());
let mut unbanned = HashMap::new();
let mut were_not_banned = vec![];
match req {

let unbanned = match req {
UnbanPubkeysReq::All => {
unbanned = banned_pubs.drain().collect();
let unbanned = json!(*banned_pubs);
banned_pubs.clear();
unbanned
},
UnbanPubkeysReq::Few(pubkeys) => {
let mut unbanned = HashMap::new();
for pubkey in pubkeys {
match banned_pubs.remove(&pubkey) {
Some(removed) => {
Expand All @@ -92,8 +114,11 @@ pub async fn unban_pubkeys_rpc(ctx: MmArc, req: Json) -> Result<Response<Vec<u8>
None => were_not_banned.push(pubkey),
}
}

json!(unbanned)
},
}
};

let res = try_s!(json::to_vec(&json!({
"result": {
"still_banned": *banned_pubs,
Expand Down
4 changes: 2 additions & 2 deletions mm2src/mm2_p2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ void = "1.0"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
futures-rustls = "0.24"
libp2p = { git = "https://github.com/KomodoPlatform/rust-libp2p.git", tag = "k-0.52.12", default-features = false, features = ["dns", "identify", "floodsub", "gossipsub", "noise", "ping", "request-response", "secp256k1", "tcp", "tokio", "websocket", "macros", "yamux"] }
timed-map = { version = "1.3", features = ["rustc-hash"] }
timed-map = { version = "1.4", features = ["rustc-hash"] }
tokio = { version = "1.20", default-features = false }

[target.'cfg(target_arch = "wasm32")'.dependencies]
futures-rustls = "0.22"
libp2p = { git = "https://github.com/KomodoPlatform/rust-libp2p.git", tag = "k-0.52.12", default-features = false, features = ["identify", "floodsub", "noise", "gossipsub", "ping", "request-response", "secp256k1", "wasm-ext", "wasm-ext-websocket", "macros", "yamux"] }
timed-map = { version = "1.3", features = ["rustc-hash", "wasm"] }
timed-map = { version = "1.4", features = ["rustc-hash", "wasm"] }

[dev-dependencies]
async-std = "1.6.2"
Expand Down