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

fix: genesis history bounded storage #642

Merged
merged 5 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ env:

jobs:
build:
runs-on: lark
runs-on: medium
steps:
- uses: actions/checkout@v2
with:
Expand All @@ -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 All @@ -53,7 +53,7 @@ jobs:

docker:
needs: [build]
runs-on: lark
runs-on: medium
env:
DOCKER_CLI_EXPERIMENTAL: enabled
steps:
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 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 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 @@
#[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)

Check warning on line 88 in pallets/genesis-history/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

pallets/genesis-history/src/lib.rs#L88

Added line #L88 was not covered by tests
}

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so the current values are not correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. We manually set the storage value to something, but the storage has different data type.

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.");

Check warning on line 31 in pallets/genesis-history/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/genesis-history/src/migration.rs#L30-L31

Added lines #L30 - L31 were not covered by tests

log::info!(
target: "runtime::genesis-history",

Check warning on line 34 in pallets/genesis-history/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/genesis-history/src/migration.rs#L33-L34

Added lines #L33 - L34 were not covered by tests
"Genesis history migration: PRE checks successful!"
);
}

pub fn migrate<T: Config>() -> Weight {
log::info!(
target: "runtime::genesis-history",

Check warning on line 41 in pallets/genesis-history/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/genesis-history/src/migration.rs#L39-L41

Added lines #L39 - L41 were not covered by tests
"Running migration to v1 for Genesis history"
);

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

Check warning on line 47 in pallets/genesis-history/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/genesis-history/src/migration.rs#L45-L47

Added lines #L45 - L47 were not covered by tests
});

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

Check warning on line 50 in pallets/genesis-history/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/genesis-history/src/migration.rs#L50

Added line #L50 was not covered by tests

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

Check warning on line 52 in pallets/genesis-history/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/genesis-history/src/migration.rs#L52

Added line #L52 was not covered by tests
}

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")),

Check warning on line 61 in pallets/genesis-history/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/genesis-history/src/migration.rs#L55-L61

Added lines #L55 - L61 were not covered by tests
},
"Unexpected storage version."
);

log::info!(
target: "runtime::genesis-history",

Check warning on line 67 in pallets/genesis-history/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/genesis-history/src/migration.rs#L66-L67

Added lines #L66 - L67 were not covered by tests
"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 @@
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");

Check warning on line 16 in runtime/hydradx/src/migrations.rs

View check run for this annotation

Codecov / codecov/patch

runtime/hydradx/src/migrations.rs#L14-L16

Added lines #L14 - L16 were not covered by tests

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");

Check warning on line 26 in runtime/hydradx/src/migrations.rs

View check run for this annotation

Codecov / codecov/patch

runtime/hydradx/src/migrations.rs#L24-L26

Added lines #L24 - L26 were not covered by tests

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");

Check warning on line 35 in runtime/hydradx/src/migrations.rs

View check run for this annotation

Codecov / codecov/patch

runtime/hydradx/src/migrations.rs#L33-L35

Added lines #L33 - L35 were not covered by tests

Ok(())
}
}
Expand Down
Loading