Skip to content

Commit

Permalink
[r2r] refactor p2p message processing flow (#1436)
Browse files Browse the repository at this point in the history
* refactor p2p message processing flow

Signed-off-by: ozkanonur <work@onurozkan.dev>

* drop `message` mutability in `process_p2p_message`

Signed-off-by: ozkanonur <work@onurozkan.dev>

* refactor `fn tx_enum_from_bytes` error type

Signed-off-by: Onur Özkan <work@onurozkan.dev>

* check topics in `lp_topic_list_validation`

Signed-off-by: Onur Özkan <work@onurozkan.dev>

* optimize

Signed-off-by: Onur Özkan <work@onurozkan.dev>

* check if there is any byte left in tx deserialization

Signed-off-by: Onur Özkan <work@onurozkan.dev>

* add backwards comparison in `fn deserialization`

Signed-off-by: Onur Özkan <work@onurozkan.dev>

* rollback 78c6810

Signed-off-by: Onur Özkan <work@onurozkan.dev>

* set JEMALLOC_SYS_WITH_MALLOC_CONF on build pipelines

Signed-off-by: ozkanonur <work@onurozkan.dev>

* Update README.md

* add `_` to unused variables

Signed-off-by: ozkanonur <work@onurozkan.dev>

* refactor `lp_topic_list_validation` as `topic_prefix_and_coin_validation`

Signed-off-by: ozkanonur <work@onurozkan.dev>

* fix reviews

Signed-off-by: Onur Özkan <work@onurozkan.dev>

* update `tx_enum_from_bytes` and write unit test

Signed-off-by: ozkanonur <work@onurozkan.dev>

* update `tx_enum_from_bytes` in tendermint

Signed-off-by: ozkanonur <work@onurozkan.dev>

* update error log of `process_p2p_message`

Signed-off-by: Onur Özkan <work@onurozkan.dev>

* create `TxMarshalingErr`

Signed-off-by: ozkanonur <work@onurozkan.dev>

* fix `test_tx_enum_from_bytes`

Signed-off-by: ozkanonur <work@onurozkan.dev>

* fix test compilation

Signed-off-by: ozkanonur <work@onurozkan.dev>

Signed-off-by: ozkanonur <work@onurozkan.dev>
Signed-off-by: Onur Özkan <work@onurozkan.dev>
  • Loading branch information
onur-ozkan authored and borngraced committed Aug 17, 2022
1 parent 4c4583c commit 2864134
Show file tree
Hide file tree
Showing 26 changed files with 181 additions and 58 deletions.
22 changes: 22 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 Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ RUN cd /mm2 && cargo fetch
# Only needed when we're developing or changing something locally.
#COPY . /mm2

# Important for x86_64 builds
ENV JEMALLOC_SYS_WITH_MALLOC_CONF="background_thread:true,narenas:1,tcache:false,dirty_decay_ms:0,muzzy_decay_ms:0,metadata_thp:auto"

# Build MM1 and MM2.
# Increased verbosity here allows us to see the MM1 CMake logs.
RUN cd /mm2 &&\
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,13 @@ If you want to build from source, the following prerequisites are required:
rustup default nightly-2022-02-01
rustup component add rustfmt-preview
```

**Note for x86_64 UNIX systems**:
To have more efficient memory consumption please execute the following command before building mm2. (It's also good to have before launching mm2.)
```sh
export JEMALLOC_SYS_WITH_MALLOC_CONF="background_thread:true,narenas:1,tcache:false,dirty_decay_ms:0,muzzy_decay_ms:0,metadata_thp:auto"
```

To build, run `cargo build` (or `cargo build -vv` to get verbose build output).

For more detailed instructions, please refer to the [Installation Guide](https://developers.komodoplatform.com/basic-docs/atomicdex/atomicdex-setup/get-started-atomicdex.html).
Expand Down
1 change: 1 addition & 0 deletions azure-pipelines-build-stage-job.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ jobs:
condition: ne ( variables['Build.Reason'], 'PullRequest' )
env:
MANUAL_MM_VERSION: true
JEMALLOC_SYS_WITH_MALLOC_CONF: "background_thread:true,narenas:1,tcache:false,dirty_decay_ms:0,muzzy_decay_ms:0,metadata_thp:auto"
- task: Docker@2
displayName: Build & Push container of dev branch
condition: and( eq( variables['Agent.OS'], 'Linux' ), eq( variables['Build.SourceBranchName'], 'dev' ) )
Expand Down
1 change: 1 addition & 0 deletions azure-pipelines-release-stage-job.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ jobs:
condition: eq( variables['DEBUG_UPLOADED'], '' )
env:
MANUAL_MM_VERSION: true
JEMALLOC_SYS_WITH_MALLOC_CONF: "background_thread:true,narenas:1,tcache:false,dirty_decay_ms:0,muzzy_decay_ms:0,metadata_thp:auto"
- bash: |
zip upload/mm2-$(COMMIT_HASH)-$(Agent.OS)-Debug target-xenial/debug/mm2 target-xenial/debug/libmm2.a -j
displayName: 'Prepare debug build upload Linux'
Expand Down
12 changes: 7 additions & 5 deletions mm2src/coins/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ use super::{coin_conf, AsyncMutex, BalanceError, BalanceFut, CoinBalance, CoinPr
RawTransactionRequest, RawTransactionRes, RawTransactionResult, RpcClientType, RpcTransportEventHandler,
RpcTransportEventHandlerShared, SearchForSwapTxSpendInput, SignatureError, SignatureResult, SwapOps,
TradeFee, TradePreimageError, TradePreimageFut, TradePreimageResult, TradePreimageValue, Transaction,
TransactionDetails, TransactionEnum, TransactionErr, TransactionFut, UnexpectedDerivationMethod,
ValidateAddressResult, ValidatePaymentInput, VerificationError, VerificationResult, WithdrawError,
WithdrawFee, WithdrawFut, WithdrawRequest, WithdrawResult};
TransactionDetails, TransactionEnum, TransactionErr, TransactionFut, TxMarshalingErr,
UnexpectedDerivationMethod, ValidateAddressResult, ValidatePaymentInput, VerificationError,
VerificationResult, WithdrawError, WithdrawFee, WithdrawFut, WithdrawRequest, WithdrawResult};

pub use rlp;

Expand Down Expand Up @@ -1376,8 +1376,10 @@ impl MarketCoinOps for EthCoin {
Box::new(fut.boxed().compat())
}

fn tx_enum_from_bytes(&self, bytes: &[u8]) -> Result<TransactionEnum, String> {
Ok(try_s!(signed_eth_tx_from_bytes(bytes)).into())
fn tx_enum_from_bytes(&self, bytes: &[u8]) -> Result<TransactionEnum, MmError<TxMarshalingErr>> {
signed_eth_tx_from_bytes(bytes)
.map(TransactionEnum::from)
.map_to_mm(TxMarshalingErr::InvalidInput)
}

fn current_block(&self) -> Box<dyn Future<Item = u64, Error = String> + Send> {
Expand Down
12 changes: 8 additions & 4 deletions mm2src/coins/lightning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ use crate::utxo::{sat_from_big_decimal, BlockchainNetwork, FeePolicy, GetUtxoLis
use crate::{BalanceFut, CoinBalance, FeeApproxStage, FoundSwapTxSpend, HistorySyncState, MarketCoinOps, MmCoin,
NegotiateSwapContractAddrErr, RawTransactionFut, RawTransactionRequest, SearchForSwapTxSpendInput,
SignatureError, SignatureResult, SwapOps, TradeFee, TradePreimageFut, TradePreimageResult,
TradePreimageValue, TransactionEnum, TransactionFut, UnexpectedDerivationMethod, UtxoStandardCoin,
ValidateAddressResult, ValidatePaymentInput, VerificationError, VerificationResult, WithdrawError,
WithdrawFut, WithdrawRequest};
TradePreimageValue, TransactionEnum, TransactionFut, TxMarshalingErr, UnexpectedDerivationMethod,
UtxoStandardCoin, ValidateAddressResult, ValidatePaymentInput, VerificationError, VerificationResult,
WithdrawError, WithdrawFut, WithdrawRequest};
use async_trait::async_trait;
use bitcoin::hashes::Hash;
use bitcoin_hashes::sha256::Hash as Sha256;
Expand Down Expand Up @@ -482,7 +482,11 @@ impl MarketCoinOps for LightningCoin {
}

// Todo: Implement this when implementing swaps for lightning as it's is used mainly for swaps
fn tx_enum_from_bytes(&self, _bytes: &[u8]) -> Result<TransactionEnum, String> { unimplemented!() }
fn tx_enum_from_bytes(&self, _bytes: &[u8]) -> Result<TransactionEnum, MmError<TxMarshalingErr>> {
MmError::err(TxMarshalingErr::NotSupported(
"tx_enum_from_bytes is not supported for Lightning yet.".to_string(),
))
}

fn current_block(&self) -> Box<dyn Future<Item = u64, Error = String> + Send> { Box::new(futures01::future::ok(0)) }

Expand Down
11 changes: 10 additions & 1 deletion mm2src/coins/lp_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,15 @@ impl Deref for TransactionEnum {
}
}

/// Error type for handling tx serialization/deserialization operations.
#[derive(Debug, Clone)]
pub enum TxMarshalingErr {
InvalidInput(String),
/// For cases where serialized and deserialized values doesn't verify each other.
CrossCheckFailed(String),
NotSupported(String),
}

#[derive(Debug, Clone)]
#[allow(clippy::large_enum_variant)]
pub enum TransactionErr {
Expand Down Expand Up @@ -634,7 +643,7 @@ pub trait MarketCoinOps {
swap_contract_address: &Option<BytesJson>,
) -> TransactionFut;

fn tx_enum_from_bytes(&self, bytes: &[u8]) -> Result<TransactionEnum, String>;
fn tx_enum_from_bytes(&self, bytes: &[u8]) -> Result<TransactionEnum, MmError<TxMarshalingErr>>;

fn current_block(&self) -> Box<dyn Future<Item = u64, Error = String> + Send>;

Expand Down
7 changes: 4 additions & 3 deletions mm2src/coins/qrc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ use crate::{BalanceError, BalanceFut, CoinBalance, FeeApproxStage, FoundSwapTxSp
MmCoin, NegotiateSwapContractAddrErr, PrivKeyNotAllowed, RawTransactionFut, RawTransactionRequest,
SearchForSwapTxSpendInput, SignatureResult, SwapOps, TradeFee, TradePreimageError, TradePreimageFut,
TradePreimageResult, TradePreimageValue, TransactionDetails, TransactionEnum, TransactionErr,
TransactionFut, TransactionType, UnexpectedDerivationMethod, ValidateAddressResult, ValidatePaymentInput,
VerificationResult, WithdrawError, WithdrawFee, WithdrawFut, WithdrawRequest, WithdrawResult};
TransactionFut, TransactionType, TxMarshalingErr, UnexpectedDerivationMethod, ValidateAddressResult,
ValidatePaymentInput, VerificationResult, WithdrawError, WithdrawFee, WithdrawFut, WithdrawRequest,
WithdrawResult};
use async_trait::async_trait;
use bitcrypto::{dhash160, sha256};
use chain::TransactionOutput;
Expand Down Expand Up @@ -1110,7 +1111,7 @@ impl MarketCoinOps for Qrc20Coin {
Box::new(fut.boxed().compat())
}

fn tx_enum_from_bytes(&self, bytes: &[u8]) -> Result<TransactionEnum, String> {
fn tx_enum_from_bytes(&self, bytes: &[u8]) -> Result<TransactionEnum, MmError<TxMarshalingErr>> {
utxo_common::tx_enum_from_bytes(self.as_ref(), bytes)
}

Expand Down
10 changes: 7 additions & 3 deletions mm2src/coins/solana.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::solana::spl::SplTokenInfo;
use crate::{BalanceError, BalanceFut, FeeApproxStage, FoundSwapTxSpend, NegotiateSwapContractAddrErr,
RawTransactionFut, RawTransactionRequest, SearchForSwapTxSpendInput, SignatureResult, TradePreimageFut,
TradePreimageResult, TradePreimageValue, TransactionDetails, TransactionFut, TransactionType,
UnexpectedDerivationMethod, ValidateAddressResult, ValidatePaymentInput, VerificationResult,
WithdrawError, WithdrawFut, WithdrawRequest, WithdrawResult};
TxMarshalingErr, UnexpectedDerivationMethod, ValidateAddressResult, ValidatePaymentInput,
VerificationResult, WithdrawError, WithdrawFut, WithdrawRequest, WithdrawResult};
use async_trait::async_trait;
use base58::ToBase58;
use bincode::{deserialize, serialize};
Expand Down Expand Up @@ -429,7 +429,11 @@ impl MarketCoinOps for SolanaCoin {
unimplemented!()
}

fn tx_enum_from_bytes(&self, _bytes: &[u8]) -> Result<TransactionEnum, String> { unimplemented!() }
fn tx_enum_from_bytes(&self, bytes: &[u8]) -> Result<TransactionEnum, MmError<TxMarshalingErr>> {
MmError::err(TxMarshalingErr::NotSupported(
"tx_enum_from_bytes is not supported for Solana yet.".to_string(),
))
}

fn current_block(&self) -> Box<dyn Future<Item = u64, Error = String> + Send> {
let coin = self.clone();
Expand Down
10 changes: 7 additions & 3 deletions mm2src/coins/solana/spl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::solana::{solana_common, AccountError, SolanaCommonOps, SolanaFeeDetai
use crate::{BalanceFut, FeeApproxStage, FoundSwapTxSpend, NegotiateSwapContractAddrErr, RawTransactionFut,
RawTransactionRequest, SearchForSwapTxSpendInput, SignatureResult, SolanaCoin, TradePreimageFut,
TradePreimageResult, TradePreimageValue, TransactionDetails, TransactionFut, TransactionType,
UnexpectedDerivationMethod, ValidateAddressResult, ValidatePaymentInput, VerificationResult,
WithdrawError, WithdrawFut, WithdrawRequest, WithdrawResult};
TxMarshalingErr, UnexpectedDerivationMethod, ValidateAddressResult, ValidatePaymentInput,
VerificationResult, WithdrawError, WithdrawFut, WithdrawRequest, WithdrawResult};
use async_trait::async_trait;
use bincode::serialize;
use common::{async_blocking, now_ms};
Expand Down Expand Up @@ -267,7 +267,11 @@ impl MarketCoinOps for SplToken {
unimplemented!()
}

fn tx_enum_from_bytes(&self, _bytes: &[u8]) -> Result<TransactionEnum, String> { unimplemented!() }
fn tx_enum_from_bytes(&self, _bytes: &[u8]) -> Result<TransactionEnum, MmError<TxMarshalingErr>> {
MmError::err(TxMarshalingErr::NotSupported(
"tx_enum_from_bytes is not supported for Spl yet.".to_string(),
))
}

fn current_block(&self) -> Box<dyn Future<Item = u64, Error = String> + Send> { self.platform_coin.current_block() }

Expand Down
11 changes: 8 additions & 3 deletions mm2src/coins/tendermint/tendermint_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use crate::{big_decimal_from_sat_unsigned, BalanceError, BalanceFut, BigDecimal,
FoundSwapTxSpend, HistorySyncState, MarketCoinOps, MmCoin, NegotiateSwapContractAddrErr,
RawTransactionFut, RawTransactionRequest, SearchForSwapTxSpendInput, SignatureResult, SwapOps, TradeFee,
TradePreimageFut, TradePreimageResult, TradePreimageValue, TransactionDetails, TransactionEnum,
TransactionFut, TransactionType, TxFeeDetails, UnexpectedDerivationMethod, ValidateAddressResult,
ValidatePaymentInput, VerificationResult, WithdrawError, WithdrawFut, WithdrawRequest};
TransactionFut, TransactionType, TxFeeDetails, TxMarshalingErr, UnexpectedDerivationMethod,
ValidateAddressResult, ValidatePaymentInput, VerificationResult, WithdrawError, WithdrawFut,
WithdrawRequest};
use async_trait::async_trait;
use bitcrypto::sha256;
use common::Future01CompatExt;
Expand Down Expand Up @@ -481,7 +482,11 @@ impl MarketCoinOps for TendermintCoin {
todo!()
}

fn tx_enum_from_bytes(&self, bytes: &[u8]) -> Result<TransactionEnum, String> { todo!() }
fn tx_enum_from_bytes(&self, bytes: &[u8]) -> Result<TransactionEnum, MmError<TxMarshalingErr>> {
MmError::err(TxMarshalingErr::NotSupported(
"tx_enum_from_bytes is not supported for Tendermint yet.".to_string(),
))
}

fn current_block(&self) -> Box<dyn Future<Item = u64, Error = String> + Send> {
let coin = self.clone();
Expand Down
10 changes: 7 additions & 3 deletions mm2src/coins/test_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use super::{CoinBalance, HistorySyncState, MarketCoinOps, MmCoin, RawTransaction
TradeFee, TransactionEnum, TransactionFut};
use crate::{BalanceFut, CanRefundHtlc, FeeApproxStage, FoundSwapTxSpend, NegotiateSwapContractAddrErr,
SearchForSwapTxSpendInput, SignatureResult, TradePreimageFut, TradePreimageResult, TradePreimageValue,
UnexpectedDerivationMethod, ValidateAddressResult, ValidatePaymentInput, VerificationResult, WithdrawFut,
WithdrawRequest};
TxMarshalingErr, UnexpectedDerivationMethod, ValidateAddressResult, ValidatePaymentInput,
VerificationResult, WithdrawFut, WithdrawRequest};
use async_trait::async_trait;
use futures01::Future;
use keys::KeyPair;
Expand Down Expand Up @@ -78,7 +78,11 @@ impl MarketCoinOps for TestCoin {
unimplemented!()
}

fn tx_enum_from_bytes(&self, bytes: &[u8]) -> Result<TransactionEnum, String> { unimplemented!() }
fn tx_enum_from_bytes(&self, _bytes: &[u8]) -> Result<TransactionEnum, MmError<TxMarshalingErr>> {
MmError::err(TxMarshalingErr::NotSupported(
"tx_enum_from_bytes is not supported for Test coin yet.".to_string(),
))
}

fn current_block(&self) -> Box<dyn Future<Item = u64, Error = String> + Send> { unimplemented!() }

Expand Down
6 changes: 3 additions & 3 deletions mm2src/coins/utxo/bch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::utxo::utxo_builder::{UtxoArcBuilder, UtxoCoinBuilder};
use crate::utxo::utxo_common::big_decimal_from_sat_unsigned;
use crate::{BlockHeightAndTime, CanRefundHtlc, CoinBalance, CoinProtocol, NegotiateSwapContractAddrErr,
PrivKeyBuildPolicy, RawTransactionFut, RawTransactionRequest, SearchForSwapTxSpendInput, SignatureResult,
SwapOps, TradePreimageValue, TransactionFut, TransactionType, TxFeeDetails, UnexpectedDerivationMethod,
ValidateAddressResult, ValidatePaymentInput, VerificationResult, WithdrawFut};
SwapOps, TradePreimageValue, TransactionFut, TransactionType, TxFeeDetails, TxMarshalingErr,
UnexpectedDerivationMethod, ValidateAddressResult, ValidatePaymentInput, VerificationResult, WithdrawFut};
use common::log::warn;
use derive_more::Display;
use futures::{FutureExt, TryFutureExt};
Expand Down Expand Up @@ -1130,7 +1130,7 @@ impl MarketCoinOps for BchCoin {
)
}

fn tx_enum_from_bytes(&self, bytes: &[u8]) -> Result<TransactionEnum, String> {
fn tx_enum_from_bytes(&self, bytes: &[u8]) -> Result<TransactionEnum, MmError<TxMarshalingErr>> {
utxo_common::tx_enum_from_bytes(self.as_ref(), bytes)
}

Expand Down
7 changes: 4 additions & 3 deletions mm2src/coins/utxo/qtum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ use crate::utxo::utxo_builder::{BlockHeaderUtxoArcOps, MergeUtxoArcOps, UtxoCoin
UtxoFieldsWithIguanaPrivKeyBuilder};
use crate::{eth, CanRefundHtlc, CoinBalance, CoinWithDerivationMethod, DelegationError, DelegationFut,
GetWithdrawSenderAddress, NegotiateSwapContractAddrErr, PrivKeyBuildPolicy, SearchForSwapTxSpendInput,
SignatureResult, StakingInfosFut, SwapOps, TradePreimageValue, TransactionFut, UnexpectedDerivationMethod,
ValidateAddressResult, ValidatePaymentInput, VerificationResult, WithdrawFut, WithdrawSenderAddress};
SignatureResult, StakingInfosFut, SwapOps, TradePreimageValue, TransactionFut, TxMarshalingErr,
UnexpectedDerivationMethod, ValidateAddressResult, ValidatePaymentInput, VerificationResult, WithdrawFut,
WithdrawSenderAddress};
use crypto::trezor::utxo::TrezorUtxoCoin;
use crypto::Bip44Chain;
use ethereum_types::H160;
Expand Down Expand Up @@ -782,7 +783,7 @@ impl MarketCoinOps for QtumCoin {
)
}

fn tx_enum_from_bytes(&self, bytes: &[u8]) -> Result<TransactionEnum, String> {
fn tx_enum_from_bytes(&self, bytes: &[u8]) -> Result<TransactionEnum, MmError<TxMarshalingErr>> {
utxo_common::tx_enum_from_bytes(self.as_ref(), bytes)
}

Expand Down
8 changes: 4 additions & 4 deletions mm2src/coins/utxo/slp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ use crate::{BalanceFut, CoinBalance, FeeApproxStage, FoundSwapTxSpend, HistorySy
NegotiateSwapContractAddrErr, NumConversError, PrivKeyNotAllowed, RawTransactionFut,
RawTransactionRequest, SearchForSwapTxSpendInput, SignatureResult, SwapOps, TradeFee, TradePreimageError,
TradePreimageFut, TradePreimageResult, TradePreimageValue, TransactionDetails, TransactionEnum,
TransactionErr, TransactionFut, TxFeeDetails, UnexpectedDerivationMethod, ValidateAddressResult,
ValidatePaymentInput, VerificationError, VerificationResult, WithdrawError, WithdrawFee, WithdrawFut,
WithdrawRequest};
TransactionErr, TransactionFut, TxFeeDetails, TxMarshalingErr, UnexpectedDerivationMethod,
ValidateAddressResult, ValidatePaymentInput, VerificationError, VerificationResult, WithdrawError,
WithdrawFee, WithdrawFut, WithdrawRequest};
use async_trait::async_trait;
use bitcrypto::dhash160;
use chain::constants::SEQUENCE_FINAL;
Expand Down Expand Up @@ -1161,7 +1161,7 @@ impl MarketCoinOps for SlpToken {
)
}

fn tx_enum_from_bytes(&self, bytes: &[u8]) -> Result<TransactionEnum, String> {
fn tx_enum_from_bytes(&self, bytes: &[u8]) -> Result<TransactionEnum, MmError<TxMarshalingErr>> {
self.platform_coin.tx_enum_from_bytes(bytes)
}

Expand Down
20 changes: 15 additions & 5 deletions mm2src/coins/utxo/utxo_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use crate::utxo::tx_cache::TxCacheResult;
use crate::utxo::utxo_withdraw::{InitUtxoWithdraw, StandardUtxoWithdraw, UtxoWithdraw};
use crate::{CanRefundHtlc, CoinBalance, CoinWithDerivationMethod, GetWithdrawSenderAddress, HDAddressId,
RawTransactionError, RawTransactionRequest, RawTransactionRes, SearchForSwapTxSpendInput, SignatureError,
SignatureResult, SwapOps, TradePreimageValue, TransactionFut, TxFeeDetails, ValidateAddressResult,
ValidatePaymentInput, VerificationError, VerificationResult, WithdrawFrom, WithdrawResult,
WithdrawSenderAddress};
SignatureResult, SwapOps, TradePreimageValue, TransactionFut, TxFeeDetails, TxMarshalingErr,
ValidateAddressResult, ValidatePaymentInput, VerificationError, VerificationResult, WithdrawFrom,
WithdrawResult, WithdrawSenderAddress};
use bitcrypto::dhash256;
pub use bitcrypto::{dhash160, sha256, ChecksumType};
use chain::constants::SEQUENCE_FINAL;
Expand Down Expand Up @@ -1822,8 +1822,18 @@ pub fn wait_for_output_spend(
Box::new(fut.boxed().compat())
}

pub fn tx_enum_from_bytes(coin: &UtxoCoinFields, bytes: &[u8]) -> Result<TransactionEnum, String> {
let mut transaction: UtxoTx = try_s!(deserialize(bytes).map_err(|err| format!("{:?}", err)));
pub fn tx_enum_from_bytes(coin: &UtxoCoinFields, bytes: &[u8]) -> Result<TransactionEnum, MmError<TxMarshalingErr>> {
let mut transaction: UtxoTx = deserialize(bytes).map_to_mm(|e| TxMarshalingErr::InvalidInput(e.to_string()))?;

let serialized_length = transaction.tx_hex().len();
if bytes.len() != serialized_length {
return MmError::err(TxMarshalingErr::CrossCheckFailed(format!(
"Expected '{}' lenght of the serialized transaction, found '{}'",
bytes.len(),
transaction.tx_hex().len()
)));
}

transaction.tx_hash_algo = coin.tx_hash_algo;
Ok(transaction.into())
}
Expand Down
Loading

0 comments on commit 2864134

Please sign in to comment.