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

CSUB-861, CSUB-883: Example for migrations and their tests #120

Draft
wants to merge 10 commits into
base: dev
Choose a base branch
from
39 changes: 20 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ members = ["node", "runtime", "runtime/generate-bags", "pallets/bridge"]
resolver = "2"

[workspace.package]
version = "3.9.0"
version = "3.9.1"
authors = ["Gluwa Inc.", "Nathan Whitaker <[email protected]>"]
edition = "2021"
license = "Unlicense"
Expand Down Expand Up @@ -172,7 +172,7 @@ precompile-utils = { git = "https://github.com/paritytech/frontier", branch = "p

# Gluwa
creditcoin3-runtime = { path = "runtime", default-features = false }
bridge = { path = "pallets/bridge", default-features = false }
pallet-bridge = { path = "pallets/bridge", default-features = false }

# Arkworks
ark-bls12-377 = { version = "0.4.0", default-features = false, features = [
Expand Down
2 changes: 1 addition & 1 deletion cli/creditcoin.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions cli/src/lib/interfaces/augment-api-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import type { Bytes, Null, Option, U256, U8aFixed, Vec, bool, u128, u32, u64 } f
import type { AnyNumber, ITuple } from '@polkadot/types-codec/types';
import type { AccountId32, H160, H256, Perbill, Percent, Permill } from '@polkadot/types/interfaces/runtime';
import type {
BridgeCollectionInfo,
Creditcoin3RuntimeOpaqueSessionKeys,
EthereumBlock,
EthereumReceiptReceiptV3,
Expand All @@ -28,6 +27,7 @@ import type {
PalletBalancesBalanceLock,
PalletBalancesIdAmount,
PalletBalancesReserveData,
PalletBridgeCollectionInfo,
PalletEvmCodeMetadata,
PalletFastUnstakeUnstakeRequest,
PalletGrandpaStoredPendingChange,
Expand Down Expand Up @@ -324,7 +324,7 @@ declare module '@polkadot/api-base/types/storage' {
QueryableStorageEntry<ApiType, [AccountId32]>;
collections: AugmentedQuery<
ApiType,
(arg: u64 | AnyNumber | Uint8Array) => Observable<Option<BridgeCollectionInfo>>,
(arg: u64 | AnyNumber | Uint8Array) => Observable<Option<PalletBridgeCollectionInfo>>,
[u64]
> &
QueryableStorageEntry<ApiType, [u64]>;
Expand Down
16 changes: 8 additions & 8 deletions cli/src/lib/interfaces/lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -814,9 +814,9 @@ export default {
},
},
/**
* Lookup91: bridge::pallet::Event<T>
* Lookup91: pallet_bridge::pallet::Event<T>
**/
BridgeEvent: {
PalletBridgeEvent: {
_enum: {
FundsCollected: '(u64,AccountId32,u128)',
},
Expand Down Expand Up @@ -2372,9 +2372,9 @@ export default {
},
},
/**
* Lookup306: bridge::pallet::Call<T>
* Lookup306: pallet_bridge::pallet::Call<T>
**/
BridgeCall: {
PalletBridgeCall: {
_enum: {
approve_collection: {
burnId: 'u64',
Expand Down Expand Up @@ -2710,17 +2710,17 @@ export default {
_enum: ['MaxAddressCountExceeded'],
},
/**
* Lookup375: bridge::types::CollectionInfo<sp_core::crypto::AccountId32, Balance, BlockNumber>
* Lookup375: pallet_bridge::types::CollectionInfo<sp_core::crypto::AccountId32, Balance, BlockNumber>
**/
BridgeCollectionInfo: {
PalletBridgeCollectionInfo: {
amount: 'u128',
collector: 'AccountId32',
blockNumber: 'u32',
},
/**
* Lookup376: bridge::pallet::Error<T>
* Lookup376: pallet_bridge::pallet::Error<T>
**/
BridgeError: {
PalletBridgeError: {
_enum: [
'AlreadyCollected',
'InvalidCollectionAmount',
Expand Down
16 changes: 8 additions & 8 deletions cli/src/lib/interfaces/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
import '@polkadot/types/types/registry';

import type {
BridgeCall,
BridgeCollectionInfo,
BridgeError,
BridgeEvent,
Creditcoin3RuntimeOpaqueSessionKeys,
Creditcoin3RuntimeOriginCaller,
Creditcoin3RuntimeProxyFilter,
Expand Down Expand Up @@ -81,6 +77,10 @@ import type {
PalletBalancesReserveData,
PalletBaseFeeCall,
PalletBaseFeeEvent,
PalletBridgeCall,
PalletBridgeCollectionInfo,
PalletBridgeError,
PalletBridgeEvent,
PalletDynamicFeeCall,
PalletEthereumCall,
PalletEthereumError,
Expand Down Expand Up @@ -213,10 +213,6 @@ import type {

declare module '@polkadot/types/types/registry' {
interface InterfaceTypes {
BridgeCall: BridgeCall;
BridgeCollectionInfo: BridgeCollectionInfo;
BridgeError: BridgeError;
BridgeEvent: BridgeEvent;
Creditcoin3RuntimeOpaqueSessionKeys: Creditcoin3RuntimeOpaqueSessionKeys;
Creditcoin3RuntimeOriginCaller: Creditcoin3RuntimeOriginCaller;
Creditcoin3RuntimeProxyFilter: Creditcoin3RuntimeProxyFilter;
Expand Down Expand Up @@ -288,6 +284,10 @@ declare module '@polkadot/types/types/registry' {
PalletBalancesReserveData: PalletBalancesReserveData;
PalletBaseFeeCall: PalletBaseFeeCall;
PalletBaseFeeEvent: PalletBaseFeeEvent;
PalletBridgeCall: PalletBridgeCall;
PalletBridgeCollectionInfo: PalletBridgeCollectionInfo;
PalletBridgeError: PalletBridgeError;
PalletBridgeEvent: PalletBridgeEvent;
PalletDynamicFeeCall: PalletDynamicFeeCall;
PalletEthereumCall: PalletEthereumCall;
PalletEthereumError: PalletEthereumError;
Expand Down
16 changes: 8 additions & 8 deletions cli/src/lib/interfaces/types-lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1013,8 +1013,8 @@ declare module '@polkadot/types/lookup' {
readonly type: 'NewBaseFeePerGas' | 'BaseFeeOverflow' | 'NewElasticity';
}

/** @name BridgeEvent (91) */
interface BridgeEvent extends Enum {
/** @name PalletBridgeEvent (91) */
interface PalletBridgeEvent extends Enum {
readonly isFundsCollected: boolean;
readonly asFundsCollected: ITuple<[u64, AccountId32, u128]>;
readonly type: 'FundsCollected';
Expand Down Expand Up @@ -2561,8 +2561,8 @@ declare module '@polkadot/types/lookup' {
readonly type: 'HotfixIncAccountSufficients';
}

/** @name BridgeCall (306) */
interface BridgeCall extends Enum {
/** @name PalletBridgeCall (306) */
interface PalletBridgeCall extends Enum {
readonly isApproveCollection: boolean;
readonly asApproveCollection: {
readonly burnId: u64;
Expand Down Expand Up @@ -2953,15 +2953,15 @@ declare module '@polkadot/types/lookup' {
readonly type: 'MaxAddressCountExceeded';
}

/** @name BridgeCollectionInfo (375) */
interface BridgeCollectionInfo extends Struct {
/** @name PalletBridgeCollectionInfo (375) */
interface PalletBridgeCollectionInfo extends Struct {
readonly amount: u128;
readonly collector: AccountId32;
readonly blockNumber: u32;
}

/** @name BridgeError (376) */
interface BridgeError extends Enum {
/** @name PalletBridgeError (376) */
interface PalletBridgeError extends Enum {
readonly isAlreadyCollected: boolean;
readonly isInvalidCollectionAmount: boolean;
readonly isNotAnAuthority: boolean;
Expand Down
3 changes: 2 additions & 1 deletion pallets/bridge/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "bridge"
name = "pallet-bridge"
version.workspace = true
authors.workspace = true
edition.workspace = true
Expand All @@ -12,6 +12,7 @@ repository.workspace = true
[dependencies]
frame-support = { workspace = true}
frame-system = { workspace = true }
log = { workspace = true }
parity-scale-codec = { workspace = true }
scale-info = { workspace = true }
sp-runtime = { workspace = true }
Expand Down
16 changes: 16 additions & 0 deletions pallets/bridge/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
#![cfg_attr(not(feature = "std"), no_std)]

mod migrations;
mod types;
use frame_support::traits::StorageVersion;
pub use pallet::*;

#[cfg(test)]
mod mock;

pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);

#[frame_support::pallet]
pub mod pallet {
use super::*;
use crate::types::{BalanceFor, Cc2BurnId, CollectionInfo};
use frame_support::dispatch::PostDispatchInfo;
use frame_support::pallet_prelude::DispatchResult;
Expand All @@ -14,6 +22,7 @@ pub mod pallet {
use sp_runtime::traits::{BlockNumberProvider, Zero};

#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);

#[pallet::config]
Expand Down Expand Up @@ -111,6 +120,13 @@ pub mod pallet {
}
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_runtime_upgrade() -> Weight {
migrations::migrate::<T>()
}
}

impl<T: Config> Pallet<T> {
fn approve_collection_cc2(
origin: OriginFor<T>,
Expand Down
67 changes: 67 additions & 0 deletions pallets/bridge/src/migrations/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use crate::{Config, Pallet};
use frame_support::{traits::StorageVersion, weights::Weight};
use sp_runtime::traits::UniqueSaturatedInto;
use sp_std::vec::Vec;

pub(crate) trait Migrate {
fn pre_upgrade(&self) -> Vec<u8>;
fn migrate(&self) -> Weight;
fn post_upgrade(&self, blob: Vec<u8>);
}

mod v1;

pub(crate) fn migrate<T: Config>() -> Weight {
let version = StorageVersion::get::<Pallet<T>>();
let mut weight: Weight = Weight::zero();

let callbacks: &[&dyn Migrate] = &[&v1::Migration::<T>::new()];

for (idx, &calls) in callbacks.iter().enumerate() {
let migration_idx = (idx + 1).unique_saturated_into();
if version < migration_idx {
#[cfg(feature = "try-runtime")]
let blob = calls.pre_upgrade();
weight.saturating_accrue(calls.migrate());
StorageVersion::new(migration_idx).put::<Pallet<T>>();
#[cfg(feature = "try-runtime")]
calls.post_upgrade(blob);
}
}

weight
}

#[cfg(test)]
pub mod tests {
use super::{migrate, Weight};
use crate::mock::{ExtBuilder, Test};
use frame_support::{pallet_prelude::StorageMap, Blake2_128Concat};

struct OldBridgeAuthoritiesPrefix;
impl frame_support::traits::StorageInstance for OldBridgeAuthoritiesPrefix {
const STORAGE_PREFIX: &'static str = "authorities";
fn pallet_prefix() -> &'static str {
"bridge"
}
}
type OldBridgeAuthorities = StorageMap<
OldBridgeAuthoritiesPrefix,
Blake2_128Concat,
<Test as frame_system::Config>::AccountId,
(),
>;

#[test]
fn migrate_should_not_crash() {
ExtBuilder.build_and_execute(|| {
// simulate an environment where the old "bridge" pallet has some items in storage
// before we apply the migrations
OldBridgeAuthorities::insert(1, ());
OldBridgeAuthorities::insert(3, ());

let weight = migrate::<Test>();
assert_ne!(weight, Weight::zero());
});
}
}
Loading