Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: upgrade to parachain v1.12.1 #280

Merged
merged 6 commits into from
Apr 21, 2022
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
115 changes: 76 additions & 39 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bitcoin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-

[dependencies.interbtc-bitcoin]
git = "https://github.com/interlay/interbtc"
rev = "92278904c8f35b5f3ef3b3393065831df7dd05b7"
rev = "f1ed5205c29934170b0674e6b60a847028b9735e"
package = "bitcoin"
optional = true

Expand Down
2 changes: 2 additions & 0 deletions bitcoin/src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ mod tests {
async fn is_block_known(&self, block_hash: BlockHash) -> Result<bool, Error>;
async fn get_new_address<A: PartialAddress + Send + 'static>(&self) -> Result<A, Error>;
async fn get_new_public_key<P: From<[u8; PUBLIC_KEY_SIZE]> + 'static>(&self) -> Result<P, Error>;
fn dump_derivation_key<P: Into<[u8; PUBLIC_KEY_SIZE]> + Send + Sync + 'static>(&self, public_key: P) -> Result<PrivateKey, Error>;
fn import_derivation_key(&self, private_key: &PrivateKey) -> Result<(), Error>;
async fn add_new_deposit_key<P: Into<[u8; PUBLIC_KEY_SIZE]> + Send + Sync + 'static>(
&self,
public_key: P,
Expand Down
31 changes: 29 additions & 2 deletions bitcoin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ const MULTIPLIER: f64 = 2.0;
// Random value between 25% below and 25% above the ideal delay.
const RANDOMIZATION_FACTOR: f64 = 0.25;

const DERIVATION_KEY_LABEL: &str = "derivation-key";
const DEPOSIT_LABEL: &str = "deposit";

const ELECTRS_TESTNET_URL: &str = "https://btc-testnet.interlay.io";
const ELECTRS_MAINNET_URL: &str = "https://btc-mainnet.interlay.io";
const ELECTRS_LOCALHOST_URL: &str = "http://localhost:3002";
Expand Down Expand Up @@ -118,6 +121,13 @@ pub trait BitcoinCoreApi {

async fn get_new_public_key<P: From<[u8; PUBLIC_KEY_SIZE]> + 'static>(&self) -> Result<P, Error>;

fn dump_derivation_key<P: Into<[u8; PUBLIC_KEY_SIZE]> + Send + Sync + 'static>(
&self,
public_key: P,
) -> Result<PrivateKey, Error>;

fn import_derivation_key(&self, private_key: &PrivateKey) -> Result<(), Error>;

async fn add_new_deposit_key<P: Into<[u8; PUBLIC_KEY_SIZE]> + Send + Sync + 'static>(
&self,
public_key: P,
Expand Down Expand Up @@ -550,12 +560,29 @@ impl BitcoinCoreApi for BitcoinCore {

/// Gets a new public key for an address in the wallet
async fn get_new_public_key<P: From<[u8; PUBLIC_KEY_SIZE]> + 'static>(&self) -> Result<P, Error> {
let address = self.rpc.get_new_address(None, Some(AddressType::Bech32))?;
let address = self
.rpc
.get_new_address(Some(DERIVATION_KEY_LABEL), Some(AddressType::Bech32))?;
let address_info = self.rpc.get_address_info(&address)?;
let public_key = address_info.pubkey.ok_or(Error::MissingPublicKey)?;
Ok(P::from(public_key.key.serialize()))
}

fn dump_derivation_key<P: Into<[u8; PUBLIC_KEY_SIZE]> + Send + Sync + 'static>(
&self,
public_key: P,
) -> Result<PrivateKey, Error> {
let address = Address::p2wpkh(&PublicKey::from_slice(&public_key.into())?, self.network)
.map_err(ConversionError::from)?;
Ok(self.rpc.dump_private_key(&address)?)
}

fn import_derivation_key(&self, private_key: &PrivateKey) -> Result<(), Error> {
Ok(self
.rpc
.import_private_key(&private_key, Some(DERIVATION_KEY_LABEL), Some(false))?)
}

/// Derive and import the private key for the master public key and public secret
async fn add_new_deposit_key<P: Into<[u8; PUBLIC_KEY_SIZE]> + Send + Sync + 'static>(
&self,
Expand All @@ -573,7 +600,7 @@ impl BitcoinCoreApi for BitcoinCore {
network: self.network,
key: deposit_secret_key,
},
None,
Some(DEPOSIT_LABEL),
// rescan true by default
Some(false),
)?;
Expand Down
20 changes: 8 additions & 12 deletions faucet/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,10 +486,8 @@ mod tests {
.await
.expect("Funding the account failed");

bob_provider
.register_vault(&bob_vault_id, 100, dummy_public_key())
.await
.unwrap();
bob_provider.register_public_key(dummy_public_key()).await.unwrap();
bob_provider.register_vault(&bob_vault_id, 100).await.unwrap();

let bob_funds_before = alice_provider
.get_free_balance_for_id(bob_account_id.clone(), DEFAULT_TESTING_CURRENCY)
Expand Down Expand Up @@ -581,10 +579,10 @@ mod tests {
let expected_amount_planck: u128 = vault_allowance_dot * rich_currency_id.inner().one();

let bob_provider = setup_provider(client.clone(), AccountKeyring::Bob).await;
bob_provider
.register_vault(&bob_vault_id, 100, dummy_public_key())
.await
.unwrap();
if bob_provider.get_public_key().await.unwrap().is_none() {
bob_provider.register_public_key(dummy_public_key()).await.unwrap();
}
bob_provider.register_vault(&bob_vault_id, 100).await.unwrap();

let alice_provider = setup_provider(client.clone(), AccountKeyring::Alice).await;

Expand Down Expand Up @@ -643,10 +641,8 @@ mod tests {
let expected_amount_planck: u128 = vault_allowance_dot * testing_currency.inner().one();

let bob_provider = setup_provider(client.clone(), AccountKeyring::Bob).await;
bob_provider
.register_vault(&bob_vault_id, 100, dummy_public_key())
.await
.unwrap();
bob_provider.register_public_key(dummy_public_key()).await.unwrap();
bob_provider.register_vault(&bob_vault_id, 100).await.unwrap();

let alice_provider = setup_provider(client.clone(), AccountKeyring::Alice).await;
// Drain the amount Bob was prefunded by, so he is eligible to receive Faucet funding
Expand Down
10 changes: 5 additions & 5 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,27 @@ bitcoin = { path = "../bitcoin"}

# Dependencies for the testing utils for integration tests
tempdir = { version = "0.3.7", optional = true }
interbtc = { package = "interbtc-standalone", git = "https://github.com/interlay/interbtc", rev = "92278904c8f35b5f3ef3b3393065831df7dd05b7", optional = true }
interbtc = { package = "interbtc-standalone", git = "https://github.com/interlay/interbtc", rev = "f1ed5205c29934170b0674e6b60a847028b9735e", optional = true }
rand = { version = "0.7", optional = true }

[dependencies.primitives]
git = "https://github.com/interlay/interbtc"
rev = "92278904c8f35b5f3ef3b3393065831df7dd05b7"
rev = "f1ed5205c29934170b0674e6b60a847028b9735e"
package = "interbtc-primitives"

[dependencies.module-bitcoin]
git = "https://github.com/interlay/interbtc"
rev = "92278904c8f35b5f3ef3b3393065831df7dd05b7"
rev = "f1ed5205c29934170b0674e6b60a847028b9735e"
package = "bitcoin"

[dependencies.module-btc-relay]
git = "https://github.com/interlay/interbtc"
rev = "92278904c8f35b5f3ef3b3393065831df7dd05b7"
rev = "f1ed5205c29934170b0674e6b60a847028b9735e"
package = "btc-relay"

[dependencies.module-oracle-rpc-runtime-api]
git = "https://github.com/interlay/interbtc"
rev = "92278904c8f35b5f3ef3b3393065831df7dd05b7"
rev = "f1ed5205c29934170b0674e6b60a847028b9735e"
package = "module-oracle-rpc-runtime-api"

[dev-dependencies]
Expand Down
Binary file modified runtime/metadata-parachain-interlay.scale
Binary file not shown.
Binary file modified runtime/metadata-parachain-kintsugi.scale
Binary file not shown.
Binary file modified runtime/metadata-parachain-testnet.scale
Binary file not shown.
Binary file modified runtime/metadata-standalone.scale
Binary file not shown.
9 changes: 9 additions & 0 deletions runtime/src/integration/bitcoin_simulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,15 @@ impl BitcoinCoreApi for MockBitcoinCore {
let public_key = PublicKey::from_secret_key(&secp, &secret_key);
Ok(P::from(public_key.serialize()))
}
fn dump_derivation_key<P: Into<[u8; PUBLIC_KEY_SIZE]> + Send + Sync + 'static>(
&self,
public_key: P,
) -> Result<PrivateKey, BitcoinError> {
todo!()
}
fn import_derivation_key(&self, private_key: &PrivateKey) -> Result<(), BitcoinError> {
todo!()
}
async fn add_new_deposit_key<P: Into<[u8; PUBLIC_KEY_SIZE]> + Send + Sync + 'static>(
&self,
_public_key: P,
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/integration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub async fn assert_issue(
vault_id: &VaultId,
amount: u128,
) {
let issue = parachain_rpc.request_issue(amount, vault_id, 10000).await.unwrap();
let issue = parachain_rpc.request_issue(amount, vault_id).await.unwrap();

let metadata = btc_rpc
.send_to_address(issue.vault_address, (issue.amount + issue.fee) as u64, None, 0)
Expand Down
60 changes: 27 additions & 33 deletions runtime/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ cfg_if::cfg_if! {
const DEFAULT_SPEC_VERSION: u32 = 1;
pub const SS58_PREFIX: u16 = 42;
} else if #[cfg(feature = "parachain-metadata-interlay")] {
const DEFAULT_SPEC_VERSION: u32 = 2;
const DEFAULT_SPEC_VERSION: u32 = 3;
pub const SS58_PREFIX: u16 = 2032;
} else if #[cfg(feature = "parachain-metadata-kintsugi")] {
const DEFAULT_SPEC_VERSION: u32 = 14;
const DEFAULT_SPEC_VERSION: u32 = 15;
pub const SS58_PREFIX: u16 = 2092;
} else if #[cfg(feature = "parachain-metadata-testnet")] {
const DEFAULT_SPEC_VERSION: u32 = 5;
const DEFAULT_SPEC_VERSION: u32 = 6;
pub const SS58_PREFIX: u16 = 42;
}
}
Expand Down Expand Up @@ -468,8 +468,7 @@ pub trait ReplacePallet {
///
/// * `&self` - sender of the transaction
/// * `amount` - amount of [Wrapped]
/// * `griefing_collateral` - amount of griefing collateral
async fn request_replace(&self, vault_id: &VaultId, amount: u128, griefing_collateral: u128) -> Result<(), Error>;
async fn request_replace(&self, vault_id: &VaultId, amount: u128) -> Result<(), Error>;

/// Withdraw a request of vault replacement
///
Expand Down Expand Up @@ -540,12 +539,12 @@ pub trait ReplacePallet {

#[async_trait]
impl ReplacePallet for InterBtcParachain {
async fn request_replace(&self, vault_id: &VaultId, amount: u128, griefing_collateral: u128) -> Result<(), Error> {
async fn request_replace(&self, vault_id: &VaultId, amount: u128) -> Result<(), Error> {
self.with_unique_signer(|signer| async move {
self.api
.tx()
.replace()
.request_replace(vault_id.currencies.clone(), amount, griefing_collateral)
.request_replace(vault_id.currencies.clone(), amount)
.sign_and_submit_then_watch_default(&signer)
.await
})
Expand Down Expand Up @@ -998,12 +997,7 @@ impl SecurityPallet for InterBtcParachain {
#[async_trait]
pub trait IssuePallet {
/// Request a new issue
async fn request_issue(
&self,
amount: u128,
vault_id: &VaultId,
griefing_collateral: u128,
) -> Result<RequestIssueEvent, Error>;
async fn request_issue(&self, amount: u128, vault_id: &VaultId) -> Result<RequestIssueEvent, Error>;

/// Execute a issue request by providing a Bitcoin transaction inclusion proof
async fn execute_issue(&self, issue_id: H256, merkle_proof: &[u8], raw_tx: &[u8]) -> Result<(), Error>;
Expand All @@ -1023,17 +1017,12 @@ pub trait IssuePallet {

#[async_trait]
impl IssuePallet for InterBtcParachain {
async fn request_issue(
&self,
amount: u128,
vault_id: &VaultId,
griefing_collateral: u128,
) -> Result<RequestIssueEvent, Error> {
async fn request_issue(&self, amount: u128, vault_id: &VaultId) -> Result<RequestIssueEvent, Error> {
self.with_unique_signer(|signer| async move {
self.api
.tx()
.issue()
.request_issue(amount, vault_id.clone(), griefing_collateral)
.request_issue(amount, vault_id.clone())
.sign_and_submit_then_watch_default(&signer)
.await
})
Expand Down Expand Up @@ -1412,14 +1401,15 @@ pub trait VaultRegistryPallet {

async fn get_all_vaults(&self) -> Result<Vec<InterBtcVault>, Error>;

async fn register_vault(&self, vault_id: &VaultId, collateral: u128, public_key: BtcPublicKey)
-> Result<(), Error>;
async fn register_vault(&self, vault_id: &VaultId, collateral: u128) -> Result<(), Error>;

async fn deposit_collateral(&self, vault_id: &VaultId, amount: u128) -> Result<(), Error>;

async fn withdraw_collateral(&self, vault_id: &VaultId, amount: u128) -> Result<(), Error>;

async fn update_public_key(&self, vault_id: &VaultId, public_key: BtcPublicKey) -> Result<(), Error>;
async fn get_public_key(&self) -> Result<Option<BtcPublicKey>, Error>;

async fn register_public_key(&self, public_key: BtcPublicKey) -> Result<(), Error>;

async fn register_address(&self, vault_id: &VaultId, btc_address: BtcAddress) -> Result<(), Error>;

Expand Down Expand Up @@ -1491,23 +1481,17 @@ impl VaultRegistryPallet for InterBtcParachain {
/// # Arguments
/// * `collateral` - deposit
/// * `public_key` - Bitcoin public key
async fn register_vault(
&self,
vault_id: &VaultId,
collateral: u128,
public_key: BtcPublicKey,
) -> Result<(), Error> {
async fn register_vault(&self, vault_id: &VaultId, collateral: u128) -> Result<(), Error> {
// TODO: check MinimumDeposit
if collateral == 0 {
return Err(Error::InsufficientFunds);
}

let public_key = &public_key.clone();
self.with_unique_signer(|signer| async move {
self.api
.tx()
.vault_registry()
.register_vault(vault_id.currencies.clone(), collateral, public_key.clone())
.register_vault(vault_id.currencies.clone(), collateral)
.sign_and_submit_then_watch_default(&signer)
.await
})
Expand Down Expand Up @@ -1555,17 +1539,27 @@ impl VaultRegistryPallet for InterBtcParachain {
Ok(())
}

async fn get_public_key(&self) -> Result<Option<BtcPublicKey>, Error> {
let head = self.get_latest_block_hash().await?;
Ok(self
.api
.storage()
.vault_registry()
.vault_bitcoin_public_key(self.get_account_id(), head)
.await?)
}

/// Update the default BTC public key for the vault corresponding to the signer.
///
/// # Arguments
/// * `public_key` - the new public key of the vault
async fn update_public_key(&self, vault_id: &VaultId, public_key: BtcPublicKey) -> Result<(), Error> {
async fn register_public_key(&self, public_key: BtcPublicKey) -> Result<(), Error> {
let public_key = &public_key.clone();
self.with_unique_signer(|signer| async move {
self.api
.tx()
.vault_registry()
.update_public_key(vault_id.currencies.clone(), public_key.clone())
.register_public_key(public_key.clone())
.sign_and_submit_then_watch_default(&signer)
.await
})
Expand Down
10 changes: 4 additions & 6 deletions runtime/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,10 @@ async fn test_register_vault() {

let vault_id = VaultId::new(AccountKeyring::Alice.into(), Token(DOT), Token(IBTC));

parachain_rpc
.register_vault(&vault_id, 100, dummy_public_key())
.await
.unwrap();
let vault = parachain_rpc.get_vault(&vault_id).await.unwrap();
assert_eq!(vault.wallet.public_key, dummy_public_key());
parachain_rpc.register_public_key(dummy_public_key()).await.unwrap();
parachain_rpc.register_vault(&vault_id, 100).await.unwrap();
parachain_rpc.get_vault(&vault_id).await.unwrap();
assert_eq!(parachain_rpc.get_public_key().await.unwrap(), Some(dummy_public_key()));
}

#[tokio::test]
Expand Down
9 changes: 7 additions & 2 deletions service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ impl<Config: Clone + Send + 'static, S: Service<Config>> ConnectionManager<Confi
let config = self.config.clone();
let (shutdown_tx, _) = tokio::sync::broadcast::channel(16);

let bitcoin_core = self.bitcoin_config.new_client(None).await?;
let prefix = self.wallet_name.clone().unwrap_or_else(|| "vault".to_string());

let bitcoin_core = self
.bitcoin_config
.new_client(Some(format!("{prefix}-master",)))
.await?;

bitcoin_core.sync().await?;

// only open connection to parachain after bitcoind sync to prevent timeout
Expand All @@ -95,7 +101,6 @@ impl<Config: Clone + Send + 'static, S: Service<Config>> ConnectionManager<Confi

let config_copy = self.bitcoin_config.clone();
let network_copy = bitcoin_core.network();
let prefix = self.wallet_name.clone().unwrap_or_else(|| "vault".to_string());
let constructor = move |vault_id: VaultId| {
let collateral_currency: CurrencyId = vault_id.collateral_currency();
let wrapped_currency: CurrencyId = vault_id.wrapped_currency();
Expand Down
4 changes: 2 additions & 2 deletions vault/src/cancellation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ mod tests {

#[async_trait]
pub trait IssuePallet {
async fn request_issue(&self, amount: u128, vault_id: &VaultId, griefing_collateral: u128) -> Result<RequestIssueEvent, RuntimeError>;
async fn request_issue(&self, amount: u128, vault_id: &VaultId) -> Result<RequestIssueEvent, RuntimeError>;
async fn execute_issue(&self, issue_id: H256, merkle_proof: &[u8], raw_tx: &[u8]) -> Result<(), RuntimeError>;
async fn cancel_issue(&self, issue_id: H256) -> Result<(), RuntimeError>;
async fn get_issue_request(&self, issue_id: H256) -> Result<InterBtcIssueRequest, RuntimeError>;
Expand All @@ -351,7 +351,7 @@ mod tests {

#[async_trait]
pub trait ReplacePallet {
async fn request_replace(&self, vault_id: &VaultId, amount: u128, griefing_collateral: u128) -> Result<(), RuntimeError>;
async fn request_replace(&self, vault_id: &VaultId, amount: u128) -> Result<(), RuntimeError>;
async fn withdraw_replace(&self, vault_id: &VaultId, amount: u128) -> Result<(), RuntimeError>;
async fn accept_replace(&self, new_vault: &VaultId, old_vault: &VaultId, amount_btc: u128, collateral: u128, btc_address: BtcAddress) -> Result<(), RuntimeError>;
async fn execute_replace(&self, replace_id: H256, merkle_proof: &[u8], raw_tx: &[u8]) -> Result<(), RuntimeError>;
Expand Down
Loading