diff --git a/Cargo.lock b/Cargo.lock index 7fe03e5ab..34baeeaea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -765,7 +765,7 @@ dependencies = [ "btc-parachain-runtime", "frame-benchmarking", "frame-benchmarking-cli", - "jsonrpc-core", + "jsonrpc-core 15.1.0", "module-exchange-rate-oracle-rpc", "module-issue-rpc", "module-redeem-rpc", @@ -2972,7 +2972,7 @@ checksum = "489b9c612e60c766f751ab40fcb43cbb55a1e10bb44a9b4307ed510ca598cbd7" dependencies = [ "failure", "futures 0.1.30", - "jsonrpc-core", + "jsonrpc-core 15.1.0", "jsonrpc-pubsub", "log 0.4.11", "serde", @@ -2993,6 +2993,19 @@ dependencies = [ "serde_json", ] +[[package]] +name = "jsonrpc-core" +version = "16.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a47c4c3ac843f9a4238943f97620619033dadef4b378cd1e8addd170de396b3" +dependencies = [ + "futures 0.3.7", + "log 0.4.11", + "serde", + "serde_derive", + "serde_json", +] + [[package]] name = "jsonrpc-core-client" version = "15.1.0" @@ -3021,22 +3034,39 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fb5c4513b7b542f42da107942b7b759f27120b5cc894729f88254b28dff44b7" dependencies = [ "hyper 0.12.35", - "jsonrpc-core", - "jsonrpc-server-utils", + "jsonrpc-core 15.1.0", + "jsonrpc-server-utils 15.1.0", "log 0.4.11", "net2", "parking_lot 0.10.2", "unicase 2.6.0", ] +[[package]] +name = "jsonrpc-http-server" +version = "16.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf0d9a2b5a7396ea4847b39c2f91504013eaeac8b49681efb5b4bcb0ab27651" +dependencies = [ + "futures 0.1.30", + "futures 0.3.7", + "hyper 0.12.35", + "jsonrpc-core 16.0.0", + "jsonrpc-server-utils 16.0.0", + "log 0.4.11", + "net2", + "parking_lot 0.11.0", + "unicase 2.6.0", +] + [[package]] name = "jsonrpc-ipc-server" version = "15.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf50e53e4eea8f421a7316c5f63e395f7bc7c4e786a6dc54d76fab6ff7aa7ce7" dependencies = [ - "jsonrpc-core", - "jsonrpc-server-utils", + "jsonrpc-core 15.1.0", + "jsonrpc-server-utils 15.1.0", "log 0.4.11", "parity-tokio-ipc", "parking_lot 0.10.2", @@ -3049,7 +3079,7 @@ version = "15.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "639558e0604013be9787ae52f798506ae42bf4220fe587bdc5625871cc8b9c77" dependencies = [ - "jsonrpc-core", + "jsonrpc-core 15.1.0", "log 0.4.11", "parking_lot 0.10.2", "rand 0.7.3", @@ -3064,7 +3094,24 @@ checksum = "72f1f3990650c033bd8f6bd46deac76d990f9bbfb5f8dc8c4767bf0a00392176" dependencies = [ "bytes 0.4.12", "globset", - "jsonrpc-core", + "jsonrpc-core 15.1.0", + "lazy_static", + "log 0.4.11", + "tokio 0.1.22", + "tokio-codec", + "unicase 2.6.0", +] + +[[package]] +name = "jsonrpc-server-utils" +version = "16.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d199e3780083bd0798911be3cc0543515a57a98e97f6051957af36fb11852939" +dependencies = [ + "bytes 0.4.12", + "futures 0.1.30", + "globset", + "jsonrpc-core 16.0.0", "lazy_static", "log 0.4.11", "tokio 0.1.22", @@ -3078,8 +3125,8 @@ version = "15.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6596fe75209b73a2a75ebe1dce4e60e03b88a2b25e8807b667597f6315150d22" dependencies = [ - "jsonrpc-core", - "jsonrpc-server-utils", + "jsonrpc-core 15.1.0", + "jsonrpc-server-utils 15.1.0", "log 0.4.11", "parity-ws", "parking_lot 0.10.2", @@ -4051,7 +4098,7 @@ name = "module-exchange-rate-oracle-rpc" version = "0.3.0" source = "git+https://gitlab.com/interlay/btc-parachain?branch=dev#48de7ac0d491f5ac43253e6f68dd51cf5948f414" dependencies = [ - "jsonrpc-core", + "jsonrpc-core 15.1.0", "jsonrpc-core-client", "jsonrpc-derive", "module-exchange-rate-oracle-rpc-runtime-api", @@ -4079,7 +4126,7 @@ name = "module-issue-rpc" version = "0.3.0" source = "git+https://gitlab.com/interlay/btc-parachain?branch=dev#48de7ac0d491f5ac43253e6f68dd51cf5948f414" dependencies = [ - "jsonrpc-core", + "jsonrpc-core 15.1.0", "jsonrpc-core-client", "jsonrpc-derive", "module-issue-rpc-runtime-api", @@ -4106,7 +4153,7 @@ name = "module-redeem-rpc" version = "0.3.0" source = "git+https://gitlab.com/interlay/btc-parachain?branch=dev#48de7ac0d491f5ac43253e6f68dd51cf5948f414" dependencies = [ - "jsonrpc-core", + "jsonrpc-core 15.1.0", "jsonrpc-core-client", "jsonrpc-derive", "module-redeem-rpc-runtime-api", @@ -4133,7 +4180,7 @@ name = "module-replace-rpc" version = "0.3.0" source = "git+https://gitlab.com/interlay/btc-parachain?branch=dev#48de7ac0d491f5ac43253e6f68dd51cf5948f414" dependencies = [ - "jsonrpc-core", + "jsonrpc-core 15.1.0", "jsonrpc-core-client", "jsonrpc-derive", "module-replace-rpc-runtime-api", @@ -4160,7 +4207,7 @@ name = "module-sla-rpc" version = "0.3.0" source = "git+https://gitlab.com/interlay/btc-parachain?branch=dev#48de7ac0d491f5ac43253e6f68dd51cf5948f414" dependencies = [ - "jsonrpc-core", + "jsonrpc-core 15.1.0", "jsonrpc-core-client", "jsonrpc-derive", "module-exchange-rate-oracle-rpc-runtime-api", @@ -4189,7 +4236,7 @@ name = "module-staked-relayers-rpc" version = "0.3.0" source = "git+https://gitlab.com/interlay/btc-parachain?branch=dev#48de7ac0d491f5ac43253e6f68dd51cf5948f414" dependencies = [ - "jsonrpc-core", + "jsonrpc-core 15.1.0", "jsonrpc-core-client", "jsonrpc-derive", "module-staked-relayers-rpc-runtime-api", @@ -4216,7 +4263,7 @@ name = "module-vault-registry-rpc" version = "0.3.0" source = "git+https://gitlab.com/interlay/btc-parachain?branch=dev#48de7ac0d491f5ac43253e6f68dd51cf5948f414" dependencies = [ - "jsonrpc-core", + "jsonrpc-core 15.1.0", "jsonrpc-core-client", "jsonrpc-derive", "module-exchange-rate-oracle-rpc-runtime-api", @@ -4861,7 +4908,7 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90fb0463589eeb1be8a3237e7260d139240e7d113950904f5a3cae5502576078" dependencies = [ - "jsonrpc-core", + "jsonrpc-core 15.1.0", "jsonrpc-core-client", "jsonrpc-derive", "pallet-transaction-payment-rpc-runtime-api", @@ -6717,7 +6764,7 @@ checksum = "fbc3793d8ff10dbeb0b683151a1ea33570dc994195cc29451e0b72ce35179adc" dependencies = [ "futures 0.3.7", "hash-db", - "jsonrpc-core", + "jsonrpc-core 15.1.0", "jsonrpc-pubsub", "log 0.4.11", "parity-scale-codec", @@ -6750,7 +6797,7 @@ checksum = "cfb4b79b9b6b410c745a00eb4ead11b2ef0819e6eac970a5ec6415abf82777be" dependencies = [ "derive_more", "futures 0.3.7", - "jsonrpc-core", + "jsonrpc-core 15.1.0", "jsonrpc-core-client", "jsonrpc-derive", "jsonrpc-pubsub", @@ -6774,8 +6821,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2f9118867e60870b99cc1877edb4c35878babe6696335841e5b636dcba2fdb3d" dependencies = [ "futures 0.1.30", - "jsonrpc-core", - "jsonrpc-http-server", + "jsonrpc-core 15.1.0", + "jsonrpc-http-server 15.1.0", "jsonrpc-ipc-server", "jsonrpc-pubsub", "jsonrpc-ws-server", @@ -6799,7 +6846,7 @@ dependencies = [ "futures 0.3.7", "futures-timer 3.0.2", "hash-db", - "jsonrpc-core", + "jsonrpc-core 15.1.0", "jsonrpc-pubsub", "lazy_static", "log 0.4.11", @@ -8126,7 +8173,7 @@ dependencies = [ "frame-system", "futures 0.3.7", "hex 0.4.2", - "jsonrpc-http-server", + "jsonrpc-http-server 15.1.0", "log 0.4.11", "mockall", "parity-scale-codec", @@ -8292,7 +8339,7 @@ checksum = "44e6202803178f25f71a3218a69341289d38c1369cc63e78dfe51577599163f7" dependencies = [ "frame-system-rpc-runtime-api", "futures 0.3.7", - "jsonrpc-core", + "jsonrpc-core 15.1.0", "jsonrpc-core-client", "jsonrpc-derive", "log 0.4.11", @@ -8484,7 +8531,7 @@ dependencies = [ "env_logger", "frame-system", "hex 0.4.2", - "jsonrpc-core", + "jsonrpc-core 15.1.0", "log 0.4.11", "parity-scale-codec", "primitive-types", @@ -9276,13 +9323,14 @@ dependencies = [ "env_logger", "futures 0.3.7", "hex 0.4.2", - "jsonrpc-http-server", + "jsonrpc-http-server 16.0.0", "log 0.4.11", "mockall", "parity-scale-codec", "runtime", "serde", "serde_json", + "sp-arithmetic", "sp-core", "sp-keyring", "thiserror", diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 9a15b25d0..813b977b9 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -15,9 +15,10 @@ use pallets::*; pub use rpc::{ historic_event_types, AccountId, BtcRelayPallet, BtcTxFeesPerByte, DotBalancesPallet, ExchangeRateOraclePallet, FeePallet, IssuePallet, PolkaBtcHeader, PolkaBtcIssueRequest, - PolkaBtcProvider, PolkaBtcRedeemRequest, PolkaBtcReplaceRequest, PolkaBtcRequestIssueEvent, - PolkaBtcStatusUpdate, PolkaBtcVault, RedeemPallet, ReplacePallet, SecurityPallet, - StakedRelayerPallet, TimestampPallet, UtilFuncs, VaultRegistryPallet, + PolkaBtcProvider, PolkaBtcRedeemRequest, PolkaBtcReplaceRequest, + PolkaBtcRequestIssueEvent, PolkaBtcStatusUpdate, PolkaBtcVault, RedeemPallet, + ReplacePallet, SecurityPallet, StakedRelayerPallet, TimestampPallet, UtilFuncs, + VaultRegistryPallet, }; pub use security::{ErrorCode, StatusCode}; pub use sp_arithmetic::{traits as FixedPointTraits, FixedI128, FixedPointNumber, FixedU128}; diff --git a/runtime/src/pallets/fee.rs b/runtime/src/pallets/fee.rs index 832d5f9b9..a87fb1bbf 100644 --- a/runtime/src/pallets/fee.rs +++ b/runtime/src/pallets/fee.rs @@ -7,9 +7,14 @@ use substrate_subxt_proc_macro::{module, Store}; #[module] pub trait Fee: Core {} -/// Current BTC/DOT exchange rate #[derive(Clone, Debug, Eq, PartialEq, Store, Encode)] pub struct IssueGriefingCollateralStore { #[store(returns = T::UnsignedFixedPoint)] pub _runtime: PhantomData, } + +#[derive(Clone, Debug, Eq, PartialEq, Store, Encode)] +pub struct ReplaceGriefingCollateralStore { + #[store(returns = T::UnsignedFixedPoint)] + pub _runtime: PhantomData, +} diff --git a/runtime/src/rpc.rs b/runtime/src/rpc.rs index 79db727fc..8396ec615 100644 --- a/runtime/src/rpc.rs +++ b/runtime/src/rpc.rs @@ -1406,6 +1406,8 @@ impl VaultRegistryPallet for PolkaBtcProvider { #[async_trait] pub trait FeePallet { async fn get_issue_griefing_collateral(&self) -> Result; + + async fn get_replace_griefing_collateral(&self) -> Result; } #[async_trait] @@ -1413,4 +1415,8 @@ impl FeePallet for PolkaBtcProvider { async fn get_issue_griefing_collateral(&self) -> Result { Ok(self.ext_client.issue_griefing_collateral(None).await?) } + + async fn get_replace_griefing_collateral(&self) -> Result { + Ok(self.ext_client.replace_griefing_collateral(None).await?) + } } diff --git a/staked-relayer/src/oracle.rs b/staked-relayer/src/oracle.rs index e73dd430c..3dee66690 100644 --- a/staked-relayer/src/oracle.rs +++ b/staked-relayer/src/oracle.rs @@ -55,8 +55,10 @@ mod tests { use super::*; use async_trait::async_trait; use runtime::PolkaBtcStatusUpdate; - use runtime::{AccountId, BtcTxFeesPerByte, Error, ErrorCode, H256Le, StatusCode}; - + use runtime::{ + AccountId, BtcTxFeesPerByte, Error, ErrorCode, FixedPointNumber, FixedU128, H256Le, + StatusCode, + }; use std::collections::BTreeSet; use std::iter::FromIterator; diff --git a/vault/Cargo.toml b/vault/Cargo.toml index 30bf716e7..cc8a8edc2 100644 --- a/vault/Cargo.toml +++ b/vault/Cargo.toml @@ -12,6 +12,7 @@ thiserror = "1.0" clap = "3.0.0-beta.2" sp-keyring = "2.0.0" sp-core = "2.0.0" +sp-arithmetic = "2.0.0" runtime = { path = "../runtime" } bitcoin = { path = "../bitcoin", features = ["cli"] } backoff = { version = "0.2.1", features = ["tokio"] } @@ -19,7 +20,7 @@ serde = "1.0.116" serde_json = { version = "1.0.57", features = ["raw_value"] } hex = "0.4.2" parity-scale-codec = "1.3.1" -jsonrpc-http-server = "15.1.0" +jsonrpc-http-server = "16.0.0" futures = "0.3.5" async-trait = "0.1.40" diff --git a/vault/src/api.rs b/vault/src/api.rs index 9dc7b3303..80e989396 100644 --- a/vault/src/api.rs +++ b/vault/src/api.rs @@ -8,8 +8,13 @@ use jsonrpc_http_server::{ }; use log::info; use parity_scale_codec::{Decode, Encode}; -use runtime::{BtcAddress, PolkaBtcProvider, ReplacePallet, UtilFuncs, VaultRegistryPallet}; +use runtime::{ + BtcAddress, ExchangeRateOraclePallet, FeePallet, FixedPointNumber, + FixedPointTraits::{CheckedAdd, CheckedMul}, + PolkaBtcProvider, ReplacePallet, UtilFuncs, VaultRegistryPallet, +}; use serde::{Deserialize, Deserializer}; +use sp_arithmetic::FixedU128; use sp_core::crypto::Ss58Codec; use sp_core::H256; use std::{net::SocketAddr, sync::Arc}; @@ -54,19 +59,33 @@ fn _account_id(api: &Arc) -> Result, params: Params) -> Result<(), Error> { +async fn _request_replace(api: &Arc, params: Params) -> Result<(), Error> { let req = parse_params::(params)?; - let result = block_on(api.request_replace(req.amount, req.griefing_collateral)); + + let amount_in_dot = api.btc_to_dots(req.amount).await?; + let griefing_collateral_percentage = api.get_replace_griefing_collateral().await?; + let griefing_collateral = calculate_for(amount_in_dot, griefing_collateral_percentage)?; + let result = block_on(api.request_replace(req.amount, griefing_collateral)); info!( "Requesting replace for amount = {} with griefing_collateral = {}: {:?}", - req.amount, req.griefing_collateral, result + req.amount, griefing_collateral, result ); Ok(result.map(|_| ())?) } +/// Take the `percentage` of an `amount` +fn calculate_for(amount: u128, percentage: FixedU128) -> Result { + FixedU128::checked_from_integer(amount) + .ok_or(Error::ArithmeticOverflow)? + .checked_mul(&percentage) + .ok_or(Error::ArithmeticOverflow)? + .into_inner() + .checked_div(FixedU128::accuracy()) + .ok_or(Error::ArithmeticUnderflow) +} + #[derive(Encode, Decode, Debug)] struct RegisterVaultJsonRpcRequest { collateral: u128, @@ -156,43 +175,44 @@ pub async fn start( let mut io = IoHandler::default(); { let api = api.clone(); - io.add_method("account_id", move |_| handle_resp(_account_id(&api))); + io.add_sync_method("account_id", move |_| handle_resp(_account_id(&api))); } { let api = api.clone(); io.add_method("request_replace", move |params| { - handle_resp(_request_replace(&api, params)) + let api = api.clone(); + async move { handle_resp(_request_replace(&api, params).await) } }); } { let api = api.clone(); let btc = btc.clone(); - io.add_method("register_vault", move |params| { + io.add_sync_method("register_vault", move |params| { handle_resp(_register_vault(&api, &btc, params)) }); } { let api = api.clone(); - io.add_method("lock_additional_collateral", move |params| { + io.add_sync_method("lock_additional_collateral", move |params| { handle_resp(_lock_additional_collateral(&api, params)) }); } { let api = api.clone(); - io.add_method("withdraw_collateral", move |params| { + io.add_sync_method("withdraw_collateral", move |params| { handle_resp(_withdraw_collateral(&api, params)) }); } { let api = api.clone(); let btc = btc.clone(); - io.add_method("update_btc_address", move |_| { + io.add_sync_method("update_btc_address", move |_| { handle_resp(_update_btc_address(&api, &btc)) }); } { let api = api.clone(); - io.add_method("withdraw_replace", move |params| { + io.add_sync_method("withdraw_replace", move |params| { handle_resp(_withdraw_replace(&api, params)) }); } diff --git a/vault/src/error.rs b/vault/src/error.rs index c62437010..bf7d42f5f 100644 --- a/vault/src/error.rs +++ b/vault/src/error.rs @@ -21,6 +21,10 @@ pub enum Error { WalletInitializationFailure(BitcoinError), #[error("Transaction contains more than one return-to-self uxto")] TooManyReturnToSelfAddresses, + #[error("Mathematical operation caused an overflow")] + ArithmeticOverflow, + #[error("Mathematical operation caused an underflow")] + ArithmeticUnderflow, #[error("BitcoinError: {0}")] BitcoinError(#[from] BitcoinError), diff --git a/vault/src/execution.rs b/vault/src/execution.rs index 6f88de38b..25a69ca6a 100644 --- a/vault/src/execution.rs +++ b/vault/src/execution.rs @@ -5,9 +5,12 @@ use backoff::{future::FutureOperation as _, ExponentialBackoff}; use bitcoin::{BitcoinCoreApi, Transaction, TransactionExt, TransactionMetadata}; use log::*; use runtime::{ - pallets::{redeem::RequestRedeemEvent, replace::AcceptReplaceEvent}, - BtcAddress, H256Le, PolkaBtcProvider, PolkaBtcRedeemRequest, PolkaBtcReplaceRequest, - PolkaBtcRuntime, RedeemPallet, ReplacePallet, UtilFuncs, VaultRegistryPallet, + pallets::{ + redeem::RequestRedeemEvent, replace::AcceptReplaceEvent, + }, + BtcAddress, H256Le, PolkaBtcProvider, PolkaBtcRedeemRequest, + PolkaBtcReplaceRequest, PolkaBtcRuntime, RedeemPallet, ReplacePallet, UtilFuncs, + VaultRegistryPallet, }; use sp_core::H256; use std::{collections::HashMap, sync::Arc, time::Duration}; @@ -328,181 +331,181 @@ fn get_request_for_btc_tx(tx: &Transaction, hash_map: &HashMap) - #[cfg(test)] mod tests { - use super::*; - use async_trait::async_trait; - use bitcoin::{ - Block, BlockHash, Error as BitcoinError, GetBlockResult, GetRawTransactionResult, - LockedTransaction, Network, PartialAddress, Transaction, TransactionMetadata, Txid, - }; - use runtime::{AccountId, Error as RuntimeError, PolkaBtcVault}; - use sp_core::H160; - use std::future::Future; - - macro_rules! assert_ok { - ( $x:expr $(,)? ) => { - let is = $x; - match is { - Ok(_) => (), - _ => assert!(false, "Expected Ok(_). Got {:#?}", is), - } - }; - ( $x:expr, $y:expr $(,)? ) => { - assert_eq!($x, Ok($y)); - }; - } - - macro_rules! assert_err { - ($result:expr, $err:pat) => {{ - match $result { - Err($err) => (), - Ok(v) => panic!("assertion failed: Ok({:?})", v), - _ => panic!("expected: Err($err)"), - } - }}; - } - - mockall::mock! { - Provider {} - - #[async_trait] - pub trait UtilFuncs { - async fn get_current_chain_height(&self) -> Result; - async fn get_blockchain_height_at(&self, parachain_height: u32) -> Result; - fn get_account_id(&self) -> &AccountId; - } - - #[async_trait] - pub trait VaultRegistryPallet { - async fn get_vault(&self, vault_id: AccountId) -> Result; - async fn get_all_vaults(&self) -> Result, RuntimeError>; - async fn register_vault(&self, collateral: u128, btc_address: BtcAddress) -> Result<(), RuntimeError>; - async fn lock_additional_collateral(&self, amount: u128) -> Result<(), RuntimeError>; - async fn withdraw_collateral(&self, amount: u128) -> Result<(), RuntimeError>; - async fn update_btc_address(&self, address: BtcAddress) -> Result<(), RuntimeError>; - async fn get_required_collateral_for_polkabtc(&self, amount_btc: u128) -> Result; - async fn get_required_collateral_for_vault(&self, vault_id: AccountId) -> Result; - async fn is_vault_below_auction_threshold(&self, vault_id: AccountId) -> Result; - } - - #[async_trait] - pub trait RedeemPallet { - async fn get_redeem_request(&self, redeem_id: H256) -> Result; - async fn request_redeem( - &self, - amount_polka_btc: u128, - btc_address: BtcAddress, - vault_id: AccountId, - ) -> Result; - async fn execute_redeem( - &self, - redeem_id: H256, - tx_id: H256Le, - merkle_proof: Vec, - raw_tx: Vec, - ) -> Result<(), RuntimeError>; - async fn cancel_redeem(&self, redeem_id: H256, reimburse: bool) -> Result<(), RuntimeError>; - async fn get_vault_redeem_requests( - &self, - account_id: AccountId, - ) -> Result, RuntimeError>; - async fn set_redeem_period(&self, period: u32) -> Result<(), RuntimeError>; - } - #[async_trait] - pub trait ReplacePallet { - async fn request_replace(&self, amount: u128, griefing_collateral: u128) - -> Result; - async fn withdraw_replace(&self, replace_id: H256) -> Result<(), RuntimeError>; - async fn accept_replace(&self, replace_id: H256, collateral: u128) -> Result<(), RuntimeError>; - async fn auction_replace( - &self, - old_vault: AccountId, - btc_amount: u128, - collateral: u128, - ) -> Result<(), RuntimeError>; - async fn execute_replace( - &self, - replace_id: H256, - tx_id: H256Le, - merkle_proof: Vec, - raw_tx: Vec, - ) -> Result<(), RuntimeError>; - async fn cancel_replace(&self, replace_id: H256) -> Result<(), RuntimeError>; - async fn get_new_vault_replace_requests( - &self, - account_id: AccountId, - ) -> Result, RuntimeError>; - async fn get_old_vault_replace_requests( - &self, - account_id: AccountId, - ) -> Result, RuntimeError>; - async fn get_replace_period(&self) -> Result; - async fn set_replace_period(&self, period: u32) -> Result<(), RuntimeError>; - async fn get_replace_request(&self, replace_id: H256) -> Result; - } - } - - mockall::mock! { - Bitcoin {} - - #[async_trait] - trait BitcoinCoreApi { - async fn wait_for_block(&self, height: u32, delay: Duration) -> Result; - fn get_block_count(&self) -> Result; - fn get_block_transactions( - &self, - hash: &BlockHash, - ) -> Result>, BitcoinError>; - fn get_raw_tx_for(&self, txid: &Txid, block_hash: &BlockHash) -> Result, BitcoinError>; - fn get_proof_for(&self, txid: Txid, block_hash: &BlockHash) -> Result, BitcoinError>; - fn get_block_hash_for(&self, height: u32) -> Result; - fn is_block_known(&self, block_hash: BlockHash) -> Result; - fn get_new_address(&self) -> Result; - fn get_best_block_hash(&self) -> Result; - fn get_block(&self, hash: &BlockHash) -> Result; - fn get_block_info(&self, hash: &BlockHash) -> Result; - fn get_mempool_transactions<'a>( - self: Arc, - ) -> Result> + 'a>, BitcoinError>; - async fn wait_for_transaction_metadata( - &self, - txid: Txid, - op_timeout: Duration, - num_confirmations: u32, - ) -> Result; - async fn create_transaction( - &self, - address: A, - sat: u64, - request_id: &[u8; 32], - ) -> Result; - fn send_transaction(&self, transaction: LockedTransaction) -> Result; - async fn create_and_send_transaction( - &self, - address: A, - sat: u64, - request_id: &[u8; 32], - ) -> Result; - async fn send_to_address( - &self, - address: A, - sat: u64, - request_id: &[u8; 32], - op_timeout: Duration, - num_confirmations: u32, - ) -> Result; - fn create_wallet(&self, wallet: &str) -> Result<(), BitcoinError>; - } - } - - fn dummy_transaction_metadata() -> TransactionMetadata { - TransactionMetadata { - block_hash: Default::default(), - block_height: Default::default(), - proof: Default::default(), - raw_tx: Default::default(), - txid: Default::default(), - } - } + // use super::*; + // use async_trait::async_trait; + // use bitcoin::{ + // Block, BlockHash, Error as BitcoinError, GetBlockResult, GetRawTransactionResult, + // LockedTransaction, Network, PartialAddress, Transaction, TransactionMetadata, Txid, + // }; + // use runtime::{AccountId, Error as RuntimeError, PolkaBtcVault}; + // use sp_core::H160; + // use std::future::Future; + // + // macro_rules! assert_ok { + // ( $x:expr $(,)? ) => { + // let is = $x; + // match is { + // Ok(_) => (), + // _ => assert!(false, "Expected Ok(_). Got {:#?}", is), + // } + // }; + // ( $x:expr, $y:expr $(,)? ) => { + // assert_eq!($x, Ok($y)); + // }; + // } + // + // macro_rules! assert_err { + // ($result:expr, $err:pat) => {{ + // match $result { + // Err($err) => (), + // Ok(v) => panic!("assertion failed: Ok({:?})", v), + // _ => panic!("expected: Err($err)"), + // } + // }}; + // } + // + // mockall::mock! { + // Provider {} + // + // #[async_trait] + // pub trait UtilFuncs { + // async fn get_current_chain_height(&self) -> Result; + // async fn get_blockchain_height_at(&self, parachain_height: u32) -> Result; + // fn get_account_id(&self) -> &AccountId; + // } + // + // #[async_trait] + // pub trait VaultRegistryPallet { + // async fn get_vault(&self, vault_id: AccountId) -> Result; + // async fn get_all_vaults(&self) -> Result, RuntimeError>; + // async fn register_vault(&self, collateral: u128, btc_address: BtcAddress) -> Result<(), RuntimeError>; + // async fn lock_additional_collateral(&self, amount: u128) -> Result<(), RuntimeError>; + // async fn withdraw_collateral(&self, amount: u128) -> Result<(), RuntimeError>; + // async fn update_btc_address(&self, address: BtcAddress) -> Result<(), RuntimeError>; + // async fn get_required_collateral_for_polkabtc(&self, amount_btc: u128) -> Result; + // async fn get_required_collateral_for_vault(&self, vault_id: AccountId) -> Result; + // async fn is_vault_below_auction_threshold(&self, vault_id: AccountId) -> Result; + // } + // + // #[async_trait] + // pub trait RedeemPallet { + // async fn get_redeem_request(&self, redeem_id: H256) -> Result; + // async fn request_redeem( + // &self, + // amount_polka_btc: u128, + // btc_address: BtcAddress, + // vault_id: AccountId, + // ) -> Result; + // async fn execute_redeem( + // &self, + // redeem_id: H256, + // tx_id: H256Le, + // merkle_proof: Vec, + // raw_tx: Vec, + // ) -> Result<(), RuntimeError>; + // async fn cancel_redeem(&self, redeem_id: H256, reimburse: bool) -> Result<(), RuntimeError>; + // async fn get_vault_redeem_requests( + // &self, + // account_id: AccountId, + // ) -> Result, RuntimeError>; + // async fn set_redeem_period(&self, period: u32) -> Result<(), RuntimeError>; + // } + // #[async_trait] + // pub trait ReplacePallet { + // async fn request_replace(&self, amount: u128, griefing_collateral: u128) + // -> Result; + // async fn withdraw_replace(&self, replace_id: H256) -> Result<(), RuntimeError>; + // async fn accept_replace(&self, replace_id: H256, collateral: u128) -> Result<(), RuntimeError>; + // async fn auction_replace( + // &self, + // old_vault: AccountId, + // btc_amount: u128, + // collateral: u128, + // ) -> Result<(), RuntimeError>; + // async fn execute_replace( + // &self, + // replace_id: H256, + // tx_id: H256Le, + // merkle_proof: Vec, + // raw_tx: Vec, + // ) -> Result<(), RuntimeError>; + // async fn cancel_replace(&self, replace_id: H256) -> Result<(), RuntimeError>; + // async fn get_new_vault_replace_requests( + // &self, + // account_id: AccountId, + // ) -> Result, RuntimeError>; + // async fn get_old_vault_replace_requests( + // &self, + // account_id: AccountId, + // ) -> Result, RuntimeError>; + // async fn get_replace_period(&self) -> Result; + // async fn set_replace_period(&self, period: u32) -> Result<(), RuntimeError>; + // async fn get_replace_request(&self, replace_id: H256) -> Result; + // } + // } + // + // mockall::mock! { + // Bitcoin {} + // + // #[async_trait] + // trait BitcoinCoreApi { + // async fn wait_for_block(&self, height: u32, delay: Duration) -> Result; + // fn get_block_count(&self) -> Result; + // fn get_block_transactions( + // &self, + // hash: &BlockHash, + // ) -> Result>, BitcoinError>; + // fn get_raw_tx_for(&self, txid: &Txid, block_hash: &BlockHash) -> Result, BitcoinError>; + // fn get_proof_for(&self, txid: Txid, block_hash: &BlockHash) -> Result, BitcoinError>; + // fn get_block_hash_for(&self, height: u32) -> Result; + // fn is_block_known(&self, block_hash: BlockHash) -> Result; + // fn get_new_address(&self) -> Result; + // fn get_best_block_hash(&self) -> Result; + // fn get_block(&self, hash: &BlockHash) -> Result; + // fn get_block_info(&self, hash: &BlockHash) -> Result; + // fn get_mempool_transactions<'a>( + // self: Arc, + // ) -> Result> + 'a>, BitcoinError>; + // async fn wait_for_transaction_metadata( + // &self, + // txid: Txid, + // op_timeout: Duration, + // num_confirmations: u32, + // ) -> Result; + // async fn create_transaction( + // &self, + // address: A, + // sat: u64, + // request_id: &[u8; 32], + // ) -> Result; + // fn send_transaction(&self, transaction: LockedTransaction) -> Result; + // async fn create_and_send_transaction( + // &self, + // address: A, + // sat: u64, + // request_id: &[u8; 32], + // ) -> Result; + // async fn send_to_address( + // &self, + // address: A, + // sat: u64, + // request_id: &[u8; 32], + // op_timeout: Duration, + // num_confirmations: u32, + // ) -> Result; + // fn create_wallet(&self, wallet: &str) -> Result<(), BitcoinError>; + // } + // } + // + // fn dummy_transaction_metadata() -> TransactionMetadata { + // TransactionMetadata { + // block_hash: Default::default(), + // block_height: Default::default(), + // proof: Default::default(), + // raw_tx: Default::default(), + // txid: Default::default(), + // } + // } // TODO: re-enable these tests