Skip to content

Commit

Permalink
Merge branch 'dev' into burn-dex-fee-for-non-kmd
Browse files Browse the repository at this point in the history
* dev:
  feat(tendermint): pubkey-only activation and unsigned tx (KomodoPlatform#2088)
  fix(tests): set txfee for some tbtc tests (KomodoPlatform#2116)
  fix(eth): remove my_address from sign_and_send_transaction_with_keypair (KomodoPlatform#2115)
  fix(utxo-swap): apply events occurred while taker down (KomodoPlatform#2114)
  refactor(memory): memory usage improvements (KomodoPlatform#2098)
  feat(app-dir): implement root application dir `.kdf` (KomodoPlatform#2102)
  fix tendermint fee calculation (KomodoPlatform#2106)
  update dockerfile (KomodoPlatform#2104)
  • Loading branch information
dimxy committed May 14, 2024
2 parents af45b71 + 52326c4 commit 0e2869e
Show file tree
Hide file tree
Showing 83 changed files with 1,500 additions and 922 deletions.
12 changes: 8 additions & 4 deletions .docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM docker.io/debian:buster-slim
FROM docker.io/debian:10

MAINTAINER Onur Özkan <onur@komodoplatform.com>

Expand All @@ -10,6 +10,7 @@ RUN apt-get install -y \
ca-certificates \
curl \
wget \
unzip \
gnupg

RUN ln -s /usr/bin/python3 /bin/python
Expand All @@ -26,11 +27,11 @@ RUN ./llvm.sh 16

RUN rm ./llvm.sh

RUN ln -s /usr/bin/clang-16 /usr/bin/clang

ENV AR=/usr/bin/llvm-ar-16
ENV CC=/usr/bin/clang-16

RUN ln -s /usr/bin/clang-16 /usr/bin/clang

RUN mkdir -m 0755 -p /etc/apt/keyrings

RUN curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
Expand All @@ -47,6 +48,9 @@ RUN apt-get install -y \
containerd.io \
docker-buildx-plugin

RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --profile minimal --default-toolchain nightly-2022-10-29 -y

RUN wget https://github.com/protocolbuffers/protobuf/releases/download/v3.20.1/protoc-3.20.1-linux-x86_64.zip
RUN unzip protoc-3.20.1-linux-x86_64.zip && mv ./include/google /usr/include/google

ENV PATH="/root/.cargo/bin:$PATH"
7 changes: 7 additions & 0 deletions Cargo.lock

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

7 changes: 4 additions & 3 deletions mm2src/adex_cli/src/scenarios/mm2_proc_mng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,11 @@ pub(crate) fn get_status() {
.filter(|line| line.contains("PID"))
.last()
{
let pid = found
.trim()
let chars = found.trim();

let pid = chars
.matches(char::is_numeric)
.fold(String::default(), |mut pid, ch| {
.fold(String::with_capacity(chars.len()), |mut pid, ch| {
pid.push_str(ch);
pid
});
Expand Down
32 changes: 20 additions & 12 deletions mm2src/coins/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,8 +637,10 @@ impl EthCoinImpl {

/// The id used to differentiate payments on Etomic swap smart contract
pub(crate) fn etomic_swap_id(&self, time_lock: u32, secret_hash: &[u8]) -> Vec<u8> {
let mut input = vec![];
input.extend_from_slice(&time_lock.to_le_bytes());
let timelock_bytes = time_lock.to_le_bytes();

let mut input = Vec::with_capacity(timelock_bytes.len() + secret_hash.len());
input.extend_from_slice(&timelock_bytes);
input.extend_from_slice(secret_hash);
sha256(&input).to_vec()
}
Expand Down Expand Up @@ -905,7 +907,7 @@ impl Deref for EthCoin {

#[async_trait]
impl SwapOps for EthCoin {
fn send_taker_fee(&self, dex_fee: DexFee, _uuid: &[u8]) -> TransactionFut {
fn send_taker_fee(&self, dex_fee: DexFee, _uuid: &[u8], _expire_at: u64) -> TransactionFut {
let address = try_tx_fus!(addr_from_raw_pubkey(self.dex_pubkey()));

Box::new(
Expand Down Expand Up @@ -2352,11 +2354,10 @@ async fn sign_and_send_transaction_with_keypair(
data: Vec<u8>,
gas: U256,
) -> Result<SignedEthTx, TransactionErr> {
let my_address = try_tx_s!(coin.derivation_method.single_addr_or_err().await);
let address_lock = coin.get_address_lock(my_address.to_string()).await;
let address_lock = coin.get_address_lock(address.to_string()).await;
let _nonce_lock = address_lock.lock().await;
let (signed, web3_instances_with_latest_nonce) =
sign_transaction_with_keypair(coin, key_pair, value, action, data, gas, my_address).await?;
sign_transaction_with_keypair(coin, key_pair, value, action, data, gas, address).await?;
let bytes = Bytes(rlp::encode(&signed).to_vec());
info!(target: "sign-and-send", "send_raw_transaction…");

Expand Down Expand Up @@ -2927,8 +2928,10 @@ impl EthCoin {
coin: self.ticker.clone(),
fee_details: fee_details.map(|d| d.into()),
block_height: trace.block_number,
tx_hash: format!("{:02x}", BytesJson(raw.hash.as_bytes().to_vec())),
tx_hex: BytesJson(rlp::encode(&raw).to_vec()),
tx: TransactionData::new_signed(
BytesJson(rlp::encode(&raw).to_vec()),
format!("{:02x}", BytesJson(raw.hash.as_bytes().to_vec())),
),
internal_id,
timestamp: block.timestamp.into_or_max(),
kmd_rewards: None,
Expand Down Expand Up @@ -3298,8 +3301,10 @@ impl EthCoin {
coin: self.ticker.clone(),
fee_details: fee_details.map(|d| d.into()),
block_height: block_number.as_u64(),
tx_hash: format!("{:02x}", BytesJson(raw.hash.as_bytes().to_vec())),
tx_hex: BytesJson(rlp::encode(&raw).to_vec()),
tx: TransactionData::new_signed(
BytesJson(rlp::encode(&raw).to_vec()),
format!("{:02x}", BytesJson(raw.hash.as_bytes().to_vec())),
),
internal_id: BytesJson(internal_id.to_vec()),
timestamp: block.timestamp.into_or_max(),
kmd_rewards: None,
Expand Down Expand Up @@ -4081,8 +4086,11 @@ impl EthCoin {
address: Address,
) -> Result<CoinBalanceMap, MmError<BalanceError>> {
let coin = || self;
let mut requests = Vec::new();
for (token_ticker, info) in self.get_erc_tokens_infos() {

let tokens = self.get_erc_tokens_infos();
let mut requests = Vec::with_capacity(tokens.len());

for (token_ticker, info) in tokens {
let fut = async move {
let balance_as_u256 = coin()
.get_token_balance_for_address(address, info.token_address)
Expand Down
2 changes: 2 additions & 0 deletions mm2src/coins/eth/eth_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ fn test_withdraw_impl_manual_fee() {
gas_price: 1.into(),
}),
memo: None,
ibc_source_channel: None,
};
coin.get_balance().wait().unwrap();

Expand Down Expand Up @@ -297,6 +298,7 @@ fn test_withdraw_impl_fee_details() {
gas_price: 1.into(),
}),
memo: None,
ibc_source_channel: None,
};
coin.get_balance().wait().unwrap();

Expand Down
6 changes: 3 additions & 3 deletions mm2src/coins/eth/eth_withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use super::{checksum_address, get_eth_gas_details, u256_to_big_decimal, wei_from
use crate::eth::{Action, Address, EthTxFeeDetails, KeyPair, SignedEthTx, UnSignedEthTx};
use crate::hd_wallet::{HDCoinWithdrawOps, HDWalletOps, WithdrawFrom, WithdrawSenderAddress};
use crate::rpc_command::init_withdraw::{WithdrawInProgressStatus, WithdrawTaskHandleShared};
use crate::{BytesJson, CoinWithDerivationMethod, EthCoin, GetWithdrawSenderAddress, PrivKeyPolicy, TransactionDetails};
use crate::{BytesJson, CoinWithDerivationMethod, EthCoin, GetWithdrawSenderAddress, PrivKeyPolicy, TransactionData,
TransactionDetails};
use async_trait::async_trait;
use bip32::DerivationPath;
use common::custom_futures::timeout::FutureTimerExt;
Expand Down Expand Up @@ -308,8 +309,7 @@ where
my_balance_change: &received_by_me - &spent_by_me,
spent_by_me,
received_by_me,
tx_hex,
tx_hash: tx_hash_str,
tx: TransactionData::new_signed(tx_hex, tx_hash_str),
block_height: 0,
fee_details: Some(fee_details.into()),
coin: coin.ticker.clone(),
Expand Down
1 change: 1 addition & 0 deletions mm2src/coins/hd_wallet/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ impl From<HDWalletStorageError> for AccountUpdatingError {
fn from(e: HDWalletStorageError) -> Self { AccountUpdatingError::WalletStorageError(e) }
}

#[derive(Display)]
pub enum HDWithdrawError {
UnexpectedFromAddress(String),
UnknownAccount { account_id: u32 },
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/lightning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ impl LightningCoin {
#[async_trait]
impl SwapOps for LightningCoin {
// Todo: This uses dummy data for now for the sake of swap P.O.C., this should be implemented probably after agreeing on how fees will work for lightning
fn send_taker_fee(&self, _dex_fee: DexFee, _uuid: &[u8]) -> TransactionFut {
fn send_taker_fee(&self, _dex_fee: DexFee, _uuid: &[u8], _expire_at: u64) -> TransactionFut {
let fut = async move { Ok(TransactionEnum::LightningPayment(PaymentHash([1; 32]))) };
Box::new(fut.boxed().compat())
}
Expand Down
4 changes: 1 addition & 3 deletions mm2src/coins/lightning/ln_p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ fn netaddress_from_ipaddr(addr: IpAddr, port: u16) -> Vec<NetAddress> {
if addr == Ipv4Addr::new(0, 0, 0, 0) || addr == Ipv4Addr::new(127, 0, 0, 1) {
return Vec::new();
}
let mut addresses = Vec::new();
let address = match addr {
IpAddr::V4(addr) => NetAddress::IPv4 {
addr: u32::from(addr).to_be_bytes(),
Expand All @@ -121,8 +120,7 @@ fn netaddress_from_ipaddr(addr: IpAddr, port: u16) -> Vec<NetAddress> {
port,
},
};
addresses.push(address);
addresses
vec![address]
}

pub async fn ln_node_announcement_loop(
Expand Down
4 changes: 2 additions & 2 deletions mm2src/coins/lightning/ln_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,7 @@ mod tests {

fn generate_random_channels(num: u64) -> Vec<DBChannelDetails> {
let mut rng = rand::thread_rng();
let mut channels = vec![];
let mut channels = Vec::with_capacity(num.try_into().expect("Shouldn't overflow."));
let s = Secp256k1::new();
let mut bytes = [0; 32];
for _i in 0..num {
Expand Down Expand Up @@ -1108,7 +1108,7 @@ mod tests {

fn generate_random_payments(num: u64) -> Vec<PaymentInfo> {
let mut rng = rand::thread_rng();
let mut payments = vec![];
let mut payments = Vec::with_capacity(num.try_into().expect("Shouldn't overflow."));
let s = Secp256k1::new();
let mut bytes = [0; 32];
for _ in 0..num {
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/lightning/ln_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pub async fn init_channel_manager(
let (channel_manager_blockhash, channel_manager, channelmonitors) = async_blocking(move || {
let mut manager_file = File::open(persister.manager_path())?;

let mut channel_monitor_mut_references = Vec::new();
let mut channel_monitor_mut_references = Vec::with_capacity(channelmonitors.len());
for (_, channel_monitor) in channelmonitors.iter_mut() {
channel_monitor_mut_references.push(channel_monitor);
}
Expand Down
60 changes: 55 additions & 5 deletions mm2src/coins/lp_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ pub enum WatcherRewardError {
// otherwise mocks called from other crates won't work
#[cfg_attr(any(test, feature = "mocktopus"), mockable)]
pub trait SwapOps {
fn send_taker_fee(&self, dex_fee: DexFee, uuid: &[u8]) -> TransactionFut;
fn send_taker_fee(&self, dex_fee: DexFee, uuid: &[u8], expire_at: u64) -> TransactionFut;

fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs<'_>) -> TransactionFut;

Expand Down Expand Up @@ -1967,6 +1967,10 @@ pub trait GetWithdrawSenderAddress {
) -> MmResult<WithdrawSenderAddress<Self::Address, Self::Pubkey>, WithdrawError>;
}

/// TODO: Avoid using a single request structure on every platform.
/// Instead, accept a generic type from withdraw implementations.
/// This way we won't have to update the payload for every platform when
/// one of them requires specific addition.
#[derive(Clone, Deserialize)]
pub struct WithdrawRequest {
coin: String,
Expand All @@ -1978,6 +1982,8 @@ pub struct WithdrawRequest {
max: bool,
fee: Option<WithdrawFee>,
memo: Option<String>,
/// Tendermint specific field used for manually providing the IBC channel IDs.
ibc_source_channel: Option<String>,
/// Currently, this flag is used by ETH/ERC20 coins activated with MetaMask **only**.
#[cfg(target_arch = "wasm32")]
#[serde(default)]
Expand Down Expand Up @@ -2032,6 +2038,7 @@ impl WithdrawRequest {
max: true,
fee: None,
memo: None,
ibc_source_channel: None,
#[cfg(target_arch = "wasm32")]
broadcast: false,
}
Expand Down Expand Up @@ -2174,15 +2181,14 @@ pub enum TransactionType {
token_id: Option<BytesJson>,
},
NftTransfer,
TendermintIBCTransfer,
}

/// Transaction details
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct TransactionDetails {
/// Raw bytes of signed transaction, this should be sent as is to `send_raw_transaction_bytes` RPC to broadcast the transaction
pub tx_hex: BytesJson,
/// Transaction hash in hexadecimal format
tx_hash: String,
#[serde(flatten)]
pub tx: TransactionData,
/// Coins are sent from these addresses
from: Vec<String>,
/// Coins are sent to these addresses
Expand Down Expand Up @@ -2216,6 +2222,40 @@ pub struct TransactionDetails {
memo: Option<String>,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(untagged)]
pub enum TransactionData {
Signed {
/// Raw bytes of signed transaction, this should be sent as is to `send_raw_transaction_bytes` RPC to broadcast the transaction
tx_hex: BytesJson,
/// Transaction hash in hexadecimal format
tx_hash: String,
},
/// This can contain entirely different data depending on the platform.
/// TODO: Perhaps using generics would be more suitable here?
Unsigned(Json),
}

impl TransactionData {
pub fn new_signed(tx_hex: BytesJson, tx_hash: String) -> Self { Self::Signed { tx_hex, tx_hash } }

pub fn new_unsigned(unsigned_tx_data: Json) -> Self { Self::Unsigned(unsigned_tx_data) }

pub fn tx_hex(&self) -> Option<&BytesJson> {
match self {
TransactionData::Signed { tx_hex, .. } => Some(tx_hex),
TransactionData::Unsigned(_) => None,
}
}

pub fn tx_hash(&self) -> Option<&str> {
match self {
TransactionData::Signed { tx_hash, .. } => Some(tx_hash),
TransactionData::Unsigned(_) => None,
}
}
}

#[derive(Clone, Copy, Debug)]
pub struct BlockHeightAndTime {
height: u64,
Expand Down Expand Up @@ -2824,6 +2864,13 @@ pub enum WithdrawError {
},
#[display(fmt = "Nft Protocol is not supported yet!")]
NftProtocolNotSupported,
#[display(fmt = "'chain_registry_name' was not found in coins configuration for '{}'", _0)]
RegistryNameIsMissing(String),
#[display(
fmt = "IBC channel could not found for '{}' address. Consider providing it manually with 'ibc_source_channel' in the request.",
_0
)]
IBCChannelCouldNotFound(String),
}

impl HttpStatusCode for WithdrawError {
Expand All @@ -2849,6 +2896,8 @@ impl HttpStatusCode for WithdrawError {
| WithdrawError::ContractTypeDoesntSupportNftWithdrawing(_)
| WithdrawError::CoinDoesntSupportNftWithdraw { .. }
| WithdrawError::NotEnoughNftsAmount { .. }
| WithdrawError::RegistryNameIsMissing(_)
| WithdrawError::IBCChannelCouldNotFound(_)
| WithdrawError::MyAddressNotNftOwner { .. } => StatusCode::BAD_REQUEST,
WithdrawError::HwError(_) => StatusCode::GONE,
#[cfg(target_arch = "wasm32")]
Expand Down Expand Up @@ -5521,6 +5570,7 @@ pub mod for_tests {
max: false,
fee,
memo: None,
ibc_source_channel: None,
};
let init = init_withdraw(ctx.clone(), withdraw_req).await.unwrap();
let timeout = wait_until_ms(150000);
Expand Down
Loading

0 comments on commit 0e2869e

Please sign in to comment.