Skip to content

Commit

Permalink
Merge pull request #668 from savudani8/feature/vault-release-version
Browse files Browse the repository at this point in the history
feat(vault): client release version
  • Loading branch information
daniel-savu authored Jul 25, 2022
2 parents d80d299 + 7158657 commit 8345465
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 3 deletions.
20 changes: 20 additions & 0 deletions crates/vault-registry/src/default_weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ pub trait WeightInfo {
fn set_premium_redeem_threshold() -> Weight;
fn set_liquidation_collateral_threshold() -> Weight;
fn report_undercollateralized_vault() -> Weight;
fn set_current_client_release() -> Weight;
fn set_pending_client_release() -> Weight;
fn recover_vault_id() -> Weight;
}

Expand Down Expand Up @@ -189,6 +191,15 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
.saturating_add(T::DbWeight::get().writes(16 as Weight))
}

fn set_current_client_release() -> Weight {
(4_130_000 as Weight)
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}

fn set_pending_client_release() -> Weight {
(4_130_000 as Weight)
}

// Storage: VaultRegistry Vaults (r:2 w:1)
fn recover_vault_id() -> Weight {
(14_679_000 as Weight)
Expand Down Expand Up @@ -335,6 +346,15 @@ impl WeightInfo for () {
.saturating_add(RocksDbWeight::get().writes(16 as Weight))
}

fn set_current_client_release() -> Weight {
(4_130_000 as Weight)
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}

fn set_pending_client_release() -> Weight {
(4_130_000 as Weight)
}

// Storage: VaultRegistry Vaults (r:2 w:1)
fn recover_vault_id() -> Weight {
(14_679_000 as Weight)
Expand Down
52 changes: 50 additions & 2 deletions crates/vault-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ use mocktopus::macros::mockable;
use primitives::VaultCurrencyPair;

use crate::types::{
BalanceOf, BtcAddress, CurrencyId, DefaultSystemVault, RichSystemVault, RichVault, SignedInner, UnsignedFixedPoint,
UpdatableVault, Version,
BalanceOf, BtcAddress, ClientRelease, CurrencyId, DefaultSystemVault, RichSystemVault, RichVault, SignedInner,
UnsignedFixedPoint, UpdatableVault, Version,
};

use crate::types::DefaultVaultCurrencyPair;
Expand Down Expand Up @@ -134,6 +134,7 @@ pub mod pallet {

fn on_runtime_upgrade() -> frame_support::weights::Weight {
crate::types::v4::migrate_v4_to_v5::<T>()
+ crate::types::upgrade_vault_release::try_upgrade_current_vault_release::<T>()
}
}

Expand Down Expand Up @@ -443,6 +444,37 @@ pub mod pallet {
Ok(())
}

/// Sets the current client release version, in case of a bug fix or patch.
///
/// # Arguments
/// * `uri` - URI to the client release binary
/// * `code_hash` - The runtime code hash associated with this client release
#[pallet::weight(<T as Config>::WeightInfo::set_current_client_release())]
#[transactional]
pub fn set_current_client_release(origin: OriginFor<T>, uri: Vec<u8>, code_hash: T::Hash) -> DispatchResult {
ensure_root(origin)?;
let release = ClientRelease { uri, code_hash };
CurrentClientRelease::<T>::put(release.clone());
Self::deposit_event(Event::<T>::ApplyClientRelease { release });
Ok(())
}

/// Sets the pending client release version. To be batched alongside the
/// `parachainSystem.enactAuthorizedUpgrade` relay chain xcm call.
///
/// # Arguments
/// * `uri` - URI to the client release binary
/// * `code_hash` - The runtime code hash associated with this client release
#[pallet::weight(<T as Config>::WeightInfo::set_pending_client_release())]
#[transactional]
pub fn set_pending_client_release(origin: OriginFor<T>, uri: Vec<u8>, code_hash: T::Hash) -> DispatchResult {
ensure_root(origin)?;
let release = ClientRelease { uri, code_hash };
PendingClientRelease::<T>::put(Some(release.clone()));
Self::deposit_event(Event::<T>::NotifyClientRelease { release });
Ok(())
}

/// Recover vault ID from a liquidated status.
///
/// # Arguments
Expand Down Expand Up @@ -573,6 +605,12 @@ pub mod pallet {
vault_id: DefaultVaultId<T>,
banned_until: T::BlockNumber,
},
NotifyClientRelease {
release: ClientRelease<T::Hash>,
},
ApplyClientRelease {
release: ClientRelease<T::Hash>,
},
}

#[pallet::error]
Expand Down Expand Up @@ -700,6 +738,16 @@ pub mod pallet {
pub(super) type TotalUserVaultCollateral<T: Config> =
StorageMap<_, Blake2_128Concat, DefaultVaultCurrencyPair<T>, BalanceOf<T>, ValueQuery>;

/// Tuple of (release_uri, code_hash) indicating the current vault client release.
#[pallet::storage]
#[pallet::getter(fn current_client_release)]
pub(super) type CurrentClientRelease<T: Config> = StorageValue<_, ClientRelease<T::Hash>, ValueQuery>;

/// Tuple of (release_uri, code_hash) indicating the pending vault client release.
#[pallet::storage]
#[pallet::getter(fn pending_client_release)]
pub(super) type PendingClientRelease<T: Config> = StorageValue<_, Option<ClientRelease<T::Hash>>, ValueQuery>;

#[pallet::type_value]
pub(super) fn DefaultForStorageVersion() -> Version {
Version::V0
Expand Down
93 changes: 92 additions & 1 deletion crates/vault-registry/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub use primitives::{VaultCurrencyPair, VaultId};
use scale_info::TypeInfo;
use sp_core::H256;
use sp_runtime::traits::{CheckedAdd, CheckedSub, Zero};
use sp_std::collections::btree_set::BTreeSet;
use sp_std::{collections::btree_set::BTreeSet, vec::Vec};

#[cfg(test)]
use mocktopus::macros::mockable;
Expand All @@ -35,6 +35,14 @@ pub enum Version {
V5,
}

#[derive(Encode, Decode, Eq, PartialEq, Clone, Default, TypeInfo, Debug)]
pub struct ClientRelease<Hash> {
/// URI to the client release binary.
pub uri: Vec<u8>,
/// The runtime code hash associated with this client release.
pub code_hash: Hash,
}

#[derive(Debug, PartialEq)]
pub enum CurrencySource<T: frame_system::Config + orml_tokens::Config> {
/// Used by vault to back issued tokens
Expand Down Expand Up @@ -108,6 +116,89 @@ pub type DefaultVaultId<T> = VaultId<<T as frame_system::Config>::AccountId, Cur

pub type DefaultVaultCurrencyPair<T> = VaultCurrencyPair<CurrencyId<T>>;

pub mod upgrade_vault_release {

use super::*;
use frame_support::weights::Weight;

/// If a pending client release exists, set the current release to that.
/// The pending release becomes `None`.
pub fn try_upgrade_current_vault_release<T: Config>() -> Weight {
let writes: Weight = if let Some(pending_release) = crate::PendingClientRelease::<T>::take() {
log::info!("Upgrading current vault release.");
crate::CurrentClientRelease::<T>::put(pending_release.clone());
Pallet::<T>::deposit_event(crate::Event::<T>::ApplyClientRelease {
release: pending_release,
});
2
} else {
log::info!("No pending vault release, skipping migration.");
0
};
T::DbWeight::get().reads_writes(1, writes)
}

#[cfg(test)]
#[test]
fn test_vault_release_migration_is_skipped() {
use crate::mock::Test;

crate::mock::run_test(|| {
let pre_migration_pending_release = None;
crate::PendingClientRelease::<Test>::put(pre_migration_pending_release.clone());

let pre_migration_current_release = ClientRelease {
uri: b"https://github.com/interlay/interbtc-clients/releases/download/1.15.0/vault-standalone-metadata"
.to_vec(),
code_hash: H256::default(),
};
crate::CurrentClientRelease::<Test>::put(pre_migration_current_release.clone());

try_upgrade_current_vault_release::<Test>();

assert_eq!(
crate::PendingClientRelease::<Test>::get(),
pre_migration_pending_release
);
assert_eq!(
crate::CurrentClientRelease::<Test>::get(),
pre_migration_current_release
);
});
}

#[cfg(test)]
#[test]
fn test_vault_release_migration_executes() {
use crate::mock::Test;

crate::mock::run_test(|| {
let pre_migration_pending_release = ClientRelease {
uri: b"https://github.com/interlay/interbtc-clients/releases/download/1.14.0/vault-standalone-metadata"
.to_vec(),
code_hash: H256::default(),
};
crate::PendingClientRelease::<Test>::put(Some(pre_migration_pending_release.clone()));

let pre_migration_current_release = ClientRelease {
uri: b"https://github.com/interlay/interbtc-clients/releases/download/1.15.0/vault-standalone-metadata"
.to_vec(),
code_hash: H256::default(),
};
crate::CurrentClientRelease::<Test>::put(pre_migration_current_release.clone());

try_upgrade_current_vault_release::<Test>();

let no_release: Option<ClientRelease<H256>> = None;
assert_eq!(crate::PendingClientRelease::<Test>::get(), no_release);
assert_eq!(
crate::CurrentClientRelease::<Test>::get(),
pre_migration_pending_release
);
});
}
}

pub mod liquidation_vault_fix {
use super::*;
use primitives::{
Expand Down
39 changes: 39 additions & 0 deletions standalone/runtime/tests/test_vault_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,45 @@ fn integration_test_vault_registry_theft_recovery_works() {
});
}

mod client_release {
use super::{assert_eq, *};
use vault_registry::types::ClientRelease;

#[test]
fn integration_test_vault_registry_set_current_client_release_works() {
test_with(|_vault_id| {
let new_release = ClientRelease {
uri: b"https://github.com/interlay/interbtc-clients/releases/download/1.14.0/vault-standalone-metadata"
.to_vec(),
code_hash: H256::default(),
};
assert_ok!(Call::VaultRegistry(VaultRegistryCall::set_current_client_release {
uri: new_release.uri.clone(),
code_hash: new_release.code_hash.clone()
})
.dispatch(root()));
assert_eq!(VaultRegistryPallet::current_client_release(), new_release);
});
}

#[test]
fn integration_test_vault_registry_set_pending_client_release_works() {
test_with(|_vault_id| {
let new_release = ClientRelease {
uri: b"https://github.com/interlay/interbtc-clients/releases/download/1.15.0/vault-standalone-metadata"
.to_vec(),
code_hash: H256::default(),
};
assert_ok!(Call::VaultRegistry(VaultRegistryCall::set_pending_client_release {
uri: new_release.uri.clone(),
code_hash: new_release.code_hash.clone()
})
.dispatch(root()));
assert_eq!(VaultRegistryPallet::pending_client_release(), Some(new_release));
});
}
}

#[test]
fn integration_test_vault_registry_theft_recovery_with_executed_redeem_works() {
test_with(|vault_id| {
Expand Down

0 comments on commit 8345465

Please sign in to comment.