Skip to content

Commit

Permalink
Merge pull request #642
Browse files Browse the repository at this point in the history
fix: genesis history bounded storage
  • Loading branch information
mrq1911 authored Jul 27, 2023
2 parents f99f258 + 63b0c95 commit 1b95179
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 79 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
- name: Upload to codecov.io
uses: codecov/codecov-action@v1
with:
fail_ci_if_error: true
fail_ci_if_error: false
- name: Build release
run: time cargo build --release --quiet --locked
- name: Version info
Expand Down
6 changes: 3 additions & 3 deletions Cargo.lock

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

6 changes: 2 additions & 4 deletions pallets/genesis-history/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pallet-genesis-history"
version = "2.0.1"
version = "2.1.0"
description = "Keeping track of the past chain generations."
authors = ["GalacticCouncil <[email protected]>"]
edition = "2021"
Expand All @@ -23,9 +23,7 @@ sp-io = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }
sp-core = { workspace = true }

[dev-dependencies]
hex-literal = '0.3.4'
hex-literal = "0.4.1"

[features]
default = ['std']
Expand Down
19 changes: 7 additions & 12 deletions pallets/genesis-history/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,24 @@ use codec::{Decode, Encode};
use frame_support::traits::GenesisBuild;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "std")]
use sp_core::bytes;
use sp_core::RuntimeDebug;
use sp_std::vec::Vec;

use scale_info::TypeInfo;
use sp_core::{MaxEncodedLen, H256};

#[cfg(test)]
mod mock;

#[cfg(test)]
mod tests;

#[derive(PartialEq, Eq, Clone, PartialOrd, Ord, Default, Encode, Decode, RuntimeDebug, derive_more::From, TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Hash))]
pub struct BlockHash(#[cfg_attr(feature = "std", serde(with = "bytes"))] pub Vec<u8>);
pub mod migration;

#[derive(Debug, Encode, Decode, Clone, Default, PartialEq, Eq, TypeInfo)]
#[derive(Encode, Decode, Clone, Default, PartialEq, Eq, RuntimeDebug, MaxEncodedLen, TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct Chain {
pub genesis_hash: BlockHash,
pub last_block_hash: BlockHash,
pub genesis_hash: H256,
pub last_block_hash: H256,
}

// Re-export pallet items so that they can be accessed from the crate namespace.
Expand All @@ -58,7 +54,6 @@ pub mod pallet {
pub trait Config: frame_system::Config {}

#[pallet::pallet]
#[pallet::without_storage_info]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);

Expand Down Expand Up @@ -90,11 +85,11 @@ pub mod pallet {
#[cfg(feature = "std")]
impl GenesisConfig {
pub fn build_storage<T: Config>(&self) -> Result<sp_runtime::Storage, String> {
<Self as frame_support::traits::GenesisBuild<T>>::build_storage(self)
<Self as GenesisBuild<T>>::build_storage(self)
}

pub fn assimilate_storage<T: Config>(&self, storage: &mut sp_runtime::Storage) -> Result<(), String> {
<Self as frame_support::traits::GenesisBuild<T>>::assimilate_storage(self, storage)
<Self as GenesisBuild<T>>::assimilate_storage(self, storage)
}
}

Expand Down
71 changes: 71 additions & 0 deletions pallets/genesis-history/src/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// This file is part of pallet-genesis-history

// Copyright (C) 2020-2023 Intergalactic, Limited (GIB).
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License..

pub use crate::*;
pub use frame_support::{
log,
traits::{Get, StorageVersion},
weights::Weight,
};

// This migration fixes the corrupted value in the storage and fixes it
pub mod v1 {
use super::*;
use hex_literal::hex;

pub fn pre_migrate<T: Config>() {
assert_eq!(StorageVersion::get::<Pallet<T>>(), 0, "Storage version too high.");

log::info!(
target: "runtime::genesis-history",
"Genesis history migration: PRE checks successful!"
);
}

pub fn migrate<T: Config>() -> Weight {
log::info!(
target: "runtime::genesis-history",
"Running migration to v1 for Genesis history"
);

PreviousChain::<T>::put(Chain {
genesis_hash: H256::from(hex!("d2a620c27ec5cbc5621ff9a522689895074f7cca0d08e7134a7804e1a3ba86fc")),
last_block_hash: H256::from(hex!("1c83220b0d0c0c252dddd6a98c9b643e34625419b646c4a6447583c92c01dbcc")),
});

StorageVersion::new(1).put::<Pallet<T>>();

T::DbWeight::get().reads_writes(0, 2)
}

pub fn post_migrate<T: Config>() {
assert_eq!(StorageVersion::get::<Pallet<T>>(), 1, "Unexpected storage version.");
assert_eq!(
PreviousChain::<T>::get(),
Chain {
genesis_hash: H256::from(hex!("d2a620c27ec5cbc5621ff9a522689895074f7cca0d08e7134a7804e1a3ba86fc")),
last_block_hash: H256::from(hex!("1c83220b0d0c0c252dddd6a98c9b643e34625419b646c4a6447583c92c01dbcc")),
},
"Unexpected storage version."
);

log::info!(
target: "runtime::genesis-history",
"Genesis history migration: POST checks successful!"
);
}
}
31 changes: 2 additions & 29 deletions pallets/genesis-history/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

use super::*;
use crate::mock::*;

use hex_literal::hex;

#[test]
Expand All @@ -30,34 +29,8 @@ fn no_previous_chain() {
#[test]
fn some_previous_chain() {
let chain = Chain {
genesis_hash: vec![1, 2, 3].into(),
last_block_hash: vec![6, 6, 6].into(),
};
ExtBuilder { chain: chain.clone() }.build().execute_with(|| {
assert_eq!(GenesisHistory::previous_chain(), chain.clone());
})
}

#[test]
fn construct_using_hex() {
let chain = Chain {
genesis_hash: hex!["aa"].to_vec().into(),
last_block_hash: hex!["bb"].to_vec().into(),
};
ExtBuilder { chain: chain.clone() }.build().execute_with(|| {
assert_eq!(GenesisHistory::previous_chain(), chain.clone());
})
}

#[test]
fn sample_data() {
let chain = Chain {
genesis_hash: hex!["0ed32bfcab4a83517fac88f2aa7cbc2f88d3ab93be9a12b6188a036bf8a943c2"]
.to_vec()
.into(),
last_block_hash: hex!["5800478f2cac4166d40c1ebe80dddbec47275d4b102f228b8a3af54d86d64837"]
.to_vec()
.into(),
genesis_hash: H256::from(hex!("0ed32bfcab4a83517fac88f2aa7cbc2f88d3ab93be9a12b6188a036bf8a943c2")),
last_block_hash: H256::from(hex!("5800478f2cac4166d40c1ebe80dddbec47275d4b102f228b8a3af54d86d64837")),
};
ExtBuilder { chain: chain.clone() }.build().execute_with(|| {
assert_eq!(GenesisHistory::previous_chain(), chain.clone());
Expand Down
2 changes: 1 addition & 1 deletion runtime/hydradx/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hydradx-runtime"
version = "167.0.0"
version = "168.0.0"
authors = ["GalacticCouncil"]
edition = "2021"
license = "Apache 2.0"
Expand Down
14 changes: 2 additions & 12 deletions runtime/hydradx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("hydradx"),
impl_name: create_runtime_str!("hydradx"),
authoring_version: 1,
spec_version: 167,
spec_version: 168,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down Expand Up @@ -232,17 +232,7 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsReversedWithSystemFirst,
(
pallet_preimage::migration::v1::Migration<Runtime>,
pallet_democracy::migrations::v1::Migration<Runtime>,
pallet_multisig::migrations::v1::MigrateToV1<Runtime>,
DmpQueue,
XcmpQueue,
ParachainSystem,
migrations::OnRuntimeUpgradeMigration,
migrations::MigrateRegistryLocationToV3<Runtime>,
migrations::XcmRateLimitMigration,
),
(migrations::OnRuntimeUpgradeMigration,),
>;

impl_runtime_apis! {
Expand Down
27 changes: 10 additions & 17 deletions runtime/hydradx/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,29 @@ pub struct OnRuntimeUpgradeMigration;
impl OnRuntimeUpgrade for OnRuntimeUpgradeMigration {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
frame_support::log::info!("PreMigrate Duster Pallet start");
pallet_duster::migration::v1::pre_migrate::<Runtime, Duster>();
frame_support::log::info!("PreMigrate Duster Pallet end");
log::info!("PreMigrate Genesis History Pallet start");
pallet_genesis_history::migration::v1::pre_migrate::<Runtime>();
log::info!("PreMigrate Genesis History Pallet end");

Ok(vec![])
}

fn on_runtime_upgrade() -> Weight {
let mut weight: Weight = Weight::zero();

frame_support::log::info!("Migrate Scheduler Pallet to v4 start");
weight = weight.saturating_add(Scheduler::migrate_v1_to_v4());
frame_support::log::info!("Migrate Scheduler Pallet to v4 end");

frame_support::log::info!("Migrate Duster Pallet to v1 start");
weight = weight.saturating_add(pallet_duster::migration::v1::migrate::<Runtime, Duster>(
get_all_module_accounts(),
TreasuryAccount::get(),
TreasuryAccount::get(),
));
frame_support::log::info!("Migrate Duster Pallet to v1 end");
log::info!("Migrate Genesis History Pallet to v1 start");
weight = weight.saturating_add(pallet_genesis_history::migration::v1::migrate::<Runtime>());
log::info!("Migrate Genesis History Pallet to v1 end");

weight
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), &'static str> {
frame_support::log::info!("PostMigrate Duster Pallet start");
pallet_duster::migration::v1::post_migrate::<Runtime, Duster>();
frame_support::log::info!("PostMigrate Duster Pallet end");
log::info!("PostMigrate Genesis History Pallet start");
pallet_genesis_history::migration::v1::post_migrate::<Runtime>();
log::info!("PostMigrate Genesis History Pallet end");

Ok(())
}
}
Expand Down

0 comments on commit 1b95179

Please sign in to comment.