Skip to content

Commit

Permalink
Support Segwit addresses in orderbooks #826 (#1064)
Browse files Browse the repository at this point in the history
* WIP.

* WIP.

* WIP.

* WIP. Some tests fail.

* Fix tests and add more.

* Fix WASM tests and clippy.

* Review fixes. Return Option<_> from order_getter to avoid unwrap/panic.
  • Loading branch information
artemii235 authored Sep 14, 2021
1 parent fa01207 commit 7173ae2
Show file tree
Hide file tree
Showing 15 changed files with 1,080 additions and 181 deletions.
2 changes: 1 addition & 1 deletion mm2src/coins/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2963,7 +2963,7 @@ impl MmCoin for EthCoin {

fn mature_confirmations(&self) -> Option<u32> { None }

fn coin_protocol_info(&self) -> Option<Vec<u8>> { None }
fn coin_protocol_info(&self) -> Vec<u8> { Vec::new() }

fn is_coin_protocol_supported(&self, _info: &Option<Vec<u8>>) -> bool { true }
}
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/lp_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ pub trait MmCoin: SwapOps + MarketCoinOps + fmt::Debug + Send + Sync + 'static {
fn mature_confirmations(&self) -> Option<u32>;

/// Get some of the coin config info in serialized format for p2p messaging.
fn coin_protocol_info(&self) -> Option<Vec<u8>>;
fn coin_protocol_info(&self) -> Vec<u8>;

/// Check if serialized coin protocol info is supported by current version.
fn is_coin_protocol_supported(&self, info: &Option<Vec<u8>>) -> bool;
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/qrc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1163,7 +1163,7 @@ impl MmCoin for Qrc20Coin {

fn mature_confirmations(&self) -> Option<u32> { Some(self.utxo.conf.mature_confirmations) }

fn coin_protocol_info(&self) -> Option<Vec<u8>> { utxo_common::coin_protocol_info(&self.utxo) }
fn coin_protocol_info(&self) -> Vec<u8> { utxo_common::coin_protocol_info(&self.utxo) }

fn is_coin_protocol_supported(&self, info: &Option<Vec<u8>>) -> bool {
utxo_common::is_coin_protocol_supported(&self.utxo, info)
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/test_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ impl MmCoin for TestCoin {

fn mature_confirmations(&self) -> Option<u32> { unimplemented!() }

fn coin_protocol_info(&self) -> Option<Vec<u8>> { None }
fn coin_protocol_info(&self) -> Vec<u8> { Vec::new() }

fn is_coin_protocol_supported(&self, _info: &Option<Vec<u8>>) -> bool { true }
}
2 changes: 1 addition & 1 deletion mm2src/coins/utxo/qtum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ impl MmCoin for QtumCoin {

fn mature_confirmations(&self) -> Option<u32> { Some(self.utxo_arc.conf.mature_confirmations) }

fn coin_protocol_info(&self) -> Option<Vec<u8>> { utxo_common::coin_protocol_info(&self.utxo_arc) }
fn coin_protocol_info(&self) -> Vec<u8> { utxo_common::coin_protocol_info(&self.utxo_arc) }

fn is_coin_protocol_supported(&self, info: &Option<Vec<u8>>) -> bool {
utxo_common::is_coin_protocol_supported(&self.utxo_arc, info)
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/utxo/slp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,7 @@ impl MmCoin for SlpToken {

fn mature_confirmations(&self) -> Option<u32> { self.platform_utxo.mature_confirmations() }

fn coin_protocol_info(&self) -> Option<Vec<u8>> { unimplemented!() }
fn coin_protocol_info(&self) -> Vec<u8> { unimplemented!() }

fn is_coin_protocol_supported(&self, _info: &Option<Vec<u8>>) -> bool { unimplemented!() }
}
Expand Down
4 changes: 2 additions & 2 deletions mm2src/coins/utxo/utxo_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2314,8 +2314,8 @@ pub fn set_requires_notarization(coin: &UtxoCoinFields, requires_nota: bool) {
.store(requires_nota, AtomicOrderding::Relaxed);
}

pub fn coin_protocol_info(coin: &UtxoCoinFields) -> Option<Vec<u8>> {
Some(rmp_serde::to_vec(&coin.my_address.addr_format).unwrap())
pub fn coin_protocol_info(coin: &UtxoCoinFields) -> Vec<u8> {
rmp_serde::to_vec(&coin.my_address.addr_format).expect("Serialization should not fail")
}

pub fn is_coin_protocol_supported(coin: &UtxoCoinFields, info: &Option<Vec<u8>>) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/utxo/utxo_standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ impl MmCoin for UtxoStandardCoin {

fn mature_confirmations(&self) -> Option<u32> { Some(self.utxo_arc.conf.mature_confirmations) }

fn coin_protocol_info(&self) -> Option<Vec<u8>> { utxo_common::coin_protocol_info(&self.utxo_arc) }
fn coin_protocol_info(&self) -> Vec<u8> { utxo_common::coin_protocol_info(&self.utxo_arc) }

fn is_coin_protocol_supported(&self, info: &Option<Vec<u8>>) -> bool {
utxo_common::is_coin_protocol_supported(&self.utxo_arc, info)
Expand Down
21 changes: 13 additions & 8 deletions mm2src/coins/utxo/utxo_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ use serialization::{deserialize, CoinVariant};
const TEST_COIN_NAME: &'static str = "RICK";
// Made-up hrp for rick to test p2wpkh script
const TEST_COIN_HRP: &'static str = "rck";
const RICK_ELECTRUM_ADDRS: &[&'static str] = &[
"electrum1.cipig.net:10017",
"electrum2.cipig.net:10017",
"electrum3.cipig.net:10017",
];

pub fn electrum_client_for_test(servers: &[&str]) -> ElectrumClient {
let ctx = MmCtxBuilder::default().into_mm_arc();
Expand Down Expand Up @@ -135,7 +140,7 @@ fn utxo_coin_for_test(

#[test]
fn test_extract_secret() {
let client = electrum_client_for_test(&["electrum1.cipig.net:10017"]);
let client = electrum_client_for_test(RICK_ELECTRUM_ADDRS);
let coin = utxo_coin_for_test(client.into(), None, false);

let tx_hex = hex::decode("0100000001de7aa8d29524906b2b54ee2e0281f3607f75662cbc9080df81d1047b78e21dbc00000000d7473044022079b6c50820040b1fbbe9251ced32ab334d33830f6f8d0bf0a40c7f1336b67d5b0220142ccf723ddabb34e542ed65c395abc1fbf5b6c3e730396f15d25c49b668a1a401209da937e5609680cb30bff4a7661364ca1d1851c2506fa80c443f00a3d3bf7365004c6b6304f62b0e5cb175210270e75970bb20029b3879ec76c4acd320a8d0589e003636264d01a7d566504bfbac6782012088a9142fb610d856c19fd57f2d0cffe8dff689074b3d8a882103f368228456c940ac113e53dad5c104cf209f2f102a409207269383b6ab9b03deac68ffffffff01d0dc9800000000001976a9146d9d2b554d768232320587df75c4338ecc8bf37d88ac40280e5c").unwrap();
Expand All @@ -147,7 +152,7 @@ fn test_extract_secret() {

#[test]
fn test_generate_transaction() {
let client = electrum_client_for_test(&["electrum1.cipig.net:10017"]);
let client = electrum_client_for_test(RICK_ELECTRUM_ADDRS);
let coin = utxo_coin_for_test(client.into(), None, false);
let unspents = vec![UnspentInfo {
value: 10000000000,
Expand Down Expand Up @@ -226,7 +231,7 @@ fn test_generate_transaction() {

#[test]
fn test_addresses_from_script() {
let client = electrum_client_for_test(&["electrum1.cipig.net:10017", "electrum2.cipig.net:10017"]);
let client = electrum_client_for_test(RICK_ELECTRUM_ADDRS);
let coin = utxo_coin_for_test(client.into(), None, false);
// P2PKH
let script: Script = "76a91405aab5342166f8594baf17a7d9bef5d56744332788ac".into();
Expand Down Expand Up @@ -395,7 +400,7 @@ fn test_wait_for_payment_spend_timeout_electrum() {
#[test]
fn test_search_for_swap_tx_spend_electrum_was_spent() {
let secret = [0; 32];
let client = electrum_client_for_test(&["electrum1.cipig.net:10017", "electrum2.cipig.net:10017"]);
let client = electrum_client_for_test(RICK_ELECTRUM_ADDRS);
let coin = utxo_coin_for_test(
client.into(),
Some("spice describe gravity federal blast come thank unfair canal monkey style afraid"),
Expand Down Expand Up @@ -428,7 +433,7 @@ fn test_search_for_swap_tx_spend_electrum_was_spent() {
#[test]
fn test_search_for_swap_tx_spend_electrum_was_refunded() {
let secret = [0; 20];
let client = electrum_client_for_test(&["electrum1.cipig.net:10017", "electrum2.cipig.net:10017"]);
let client = electrum_client_for_test(RICK_ELECTRUM_ADDRS);
let coin = utxo_coin_for_test(
client.into(),
Some("spice describe gravity federal blast come thank unfair canal monkey style afraid"),
Expand Down Expand Up @@ -827,7 +832,7 @@ fn test_withdraw_rick_rewards_none() {

#[test]
fn test_ordered_mature_unspents_without_tx_cache() {
let client = electrum_client_for_test(&["electrum1.cipig.net:10017", "electrum2.cipig.net:10017"]);
let client = electrum_client_for_test(RICK_ELECTRUM_ADDRS);
let coin = utxo_coin_for_test(
client.into(),
Some("spice describe gravity federal blast come thank unfair canal monkey style afraid"),
Expand All @@ -847,7 +852,7 @@ fn test_ordered_mature_unspents_without_tx_cache() {
#[test]
fn test_utxo_lock() {
// send several transactions concurrently to check that they are not using same inputs
let client = electrum_client_for_test(&["electrum1.cipig.net:10017", "electrum2.cipig.net:10017"]);
let client = electrum_client_for_test(RICK_ELECTRUM_ADDRS);
let coin = utxo_coin_for_test(client.into(), None, false);
let output = TransactionOutput {
value: 1000000,
Expand Down Expand Up @@ -1694,7 +1699,7 @@ fn test_ordered_mature_unspents_from_cache_impl(
) {
const TX_HASH: &str = "0a0fda88364b960000f445351fe7678317a1e0c80584de0413377ede00ba696f";
let tx_hash: H256Json = hex::decode(TX_HASH).unwrap().as_slice().into();
let client = electrum_client_for_test(&["electrum1.cipig.net:10017"]);
let client = electrum_client_for_test(RICK_ELECTRUM_ADDRS);
let mut verbose = client.get_verbose_transaction(tx_hash.clone()).wait().unwrap();
verbose.confirmations = cached_confs;
verbose.height = cached_height;
Expand Down
Loading

0 comments on commit 7173ae2

Please sign in to comment.