Skip to content
This repository has been archived by the owner on Mar 13, 2023. It is now read-only.

Commit

Permalink
add migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavier Lau committed Jul 23, 2021
1 parent 828be1e commit c5f30a3
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 39 deletions.
103 changes: 67 additions & 36 deletions bin/node/runtime/pangolin/src/lib.rs
Expand Up @@ -898,6 +898,71 @@ impl dvm_rpc_runtime_api::ConvertTransaction<OpaqueExtrinsic> for TransactionCon
}
}

const PARCEL: &[u8] = &[
15, 21, 36, 189, 59, 182, 232, 78, 195, 151, 250, 101, 204, 126, 219, 30, 255, 183, 239, 102,
148, 85, 35, 246, 249, 242, 46, 158, 197, 134, 117, 134, 55, 22, 249, 96, 0, 0, 0, 0, 51, 5,
163, 0, 0, 0, 0, 0, 159, 254, 210, 41, 124, 123, 129, 41, 52, 19, 85, 13, 182, 117, 7, 58, 180,
105, 128, 178, 86, 232, 31, 23, 27, 204, 85, 166, 255, 131, 69, 230, 146, 192, 248, 110, 91,
72, 224, 27, 153, 108, 173, 192, 1, 98, 47, 181, 227, 99, 180, 33, 29, 204, 77, 232, 222, 199,
93, 122, 171, 133, 181, 103, 182, 204, 212, 26, 211, 18, 69, 27, 148, 138, 116, 19, 240, 161,
66, 253, 64, 212, 147, 71, 100, 216, 131, 1, 10, 5, 132, 103, 101, 116, 104, 136, 103, 111, 49,
46, 49, 54, 46, 53, 133, 108, 105, 110, 117, 120, 18, 103, 62, 63, 109, 213, 138, 26, 223, 109,
111, 250, 183, 3, 116, 181, 56, 98, 6, 75, 162, 61, 231, 54, 226, 240, 202, 143, 164, 170, 53,
213, 86, 232, 31, 23, 27, 204, 85, 166, 255, 131, 69, 230, 146, 192, 248, 110, 91, 72, 224, 27,
153, 108, 173, 192, 1, 98, 47, 181, 227, 99, 180, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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27, 85, 149, 236, 161, 98, 226, 233, 139, 47,
138, 226, 138, 150, 200, 189, 225, 177, 36, 136, 33, 215, 113, 72, 135, 88, 41, 60, 1, 107, 16,
54, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
50, 108, 10, 48, 215, 123, 120, 217, 21, 149, 164, 182, 138, 206, 15, 28, 13, 8, 217, 207, 128,
249, 138, 185, 171, 236, 94, 177, 42, 219, 211, 114, 50, 108, 10, 48, 215, 123, 120, 217, 21,
149, 164, 182, 138, 206, 15, 28, 13, 8, 217, 207, 128, 249, 138, 185, 171, 236, 94, 177, 42,
219, 211, 114,
];

#[test]
fn assert_encoded_eq() {
darwinia_ethereum_relay::migration::assert_encoded_eq(
r#"{
"header": {
"baseFeePerGas": "0x536106b",
"difficulty": "0x1e074a90",
"extraData": "0xd883010a05846765746888676f312e31362e35856c696e7578",
"gasLimit": "0x7a1200",
"gasUsed": "0x0",
"hash": "0x326c0a30d77b78d91595a4b68ace0f1c0d08d9cf80f98ab9abec5eb12adbd372",
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"miner": "0x9ffed2297c7b81293413550db675073ab46980b2",
"mixHash": "0x6fef2c5eded7b7d98dbf81bc40be1b5595eca162e2e98b2f8ae28a96c8bde1b1",
"nonce": "0x21d771488758293c",
"number": "0xa30533",
"parentHash": "0x0f1524bd3bb6e84ec397fa65cc7edb1effb7ef66945523f6f9f22e9ec5867586",
"receiptsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"size": "0x221",
"stateRoot": "0x12673e3f6dd58a1adf6d6ffab70374b53862064ba23de736e2f0ca8fa4aa35d5",
"timestamp": "0x60f91637",
"totalDifficulty": "0x799b46acb904b7",
"transactions": [],
"transactionsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"uncles": []
},
"mmr_root": "0x326c0a30d77b78d91595a4b68ace0f1c0d08d9cf80f98ab9abec5eb12adbd372"
}"#,
PARCEL,
)
}

pub struct CustomOnRuntimeUpgrade;
impl OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
#[cfg(feature = "try-runtime")]
Expand All @@ -907,51 +972,17 @@ impl OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
let _ = Ring::deposit_creating(&BridgeMillauMessages::relayer_fund_account_id(), 1 << 50);
// --->

darwinia_header_mmr::migration::initialize_new_mmr_state::<Runtime>(b"HeaderMMR", 2);
darwinia_relay_authorities::migration::migrate::<Runtime, EthereumRelayAuthoritiesInstance>(
b"Instance1DarwiniaRelayAuthorities",
);

Ok(())
}

#[cfg(feature = "std")]
fn on_runtime_upgrade() -> Weight {
// <--- Hack for local test
// use frame_support::traits::Currency;
// let _ = Ring::deposit_creating(&BridgeMillauMessages::relayer_fund_account_id(), 1 << 50);
// --->

// if let Ok(parcel) = serde_json::from_str(
// r#"{
// "header": {
// "baseFeePerGas": "0x536106b",
// "difficulty": "0x1e074a90",
// "extraData": "0xd883010a05846765746888676f312e31362e35856c696e7578",
// "gasLimit": "0x7a1200",
// "gasUsed": "0x0",
// "hash": "0x326c0a30d77b78d91595a4b68ace0f1c0d08d9cf80f98ab9abec5eb12adbd372",
// "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
// "miner": "0x9ffed2297c7b81293413550db675073ab46980b2",
// "mixHash": "0x6fef2c5eded7b7d98dbf81bc40be1b5595eca162e2e98b2f8ae28a96c8bde1b1",
// "nonce": "0x21d771488758293c",
// "number": "0xa30533",
// "parentHash": "0x0f1524bd3bb6e84ec397fa65cc7edb1effb7ef66945523f6f9f22e9ec5867586",
// "receiptsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
// "sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
// "size": "0x221",
// "stateRoot": "0x12673e3f6dd58a1adf6d6ffab70374b53862064ba23de736e2f0ca8fa4aa35d5",
// "timestamp": "0x60f91637",
// "totalDifficulty": "0x799b46acb904b7",
// "transactions": [],
// "transactionsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
// "uncles": []
// },
// "mmr_root": "TODO"
// }"#,
// ) {
// darwinia_ethereum_relay::migration::migrate(parcel);
// }
darwinia_ethereum_relay::migration::migrate(PARCEL);
darwinia_relayer_game::migration::migrate::<Runtime, EthereumRelayerGameInstance>();

RuntimeBlockWeights::get().max_block
}
Expand Down
47 changes: 46 additions & 1 deletion frame/bridge/ethereum/relay/src/lib.rs
Expand Up @@ -24,7 +24,52 @@ pub mod migration {
// --- darwinia-network ---
use crate::*;

pub fn migrate(parcel: EthereumRelayHeaderParcel) {
#[test]
fn encode() {
let parcel = serde_json::from_str::<EthereumRelayHeaderParcel>(r#"{
"header": {
"baseFeePerGas": "0x536106b",
"difficulty": "0x1e074a90",
"extraData": "0xd883010a05846765746888676f312e31362e35856c696e7578",
"gasLimit": "0x7a1200",
"gasUsed": "0x0",
"hash": "0x326c0a30d77b78d91595a4b68ace0f1c0d08d9cf80f98ab9abec5eb12adbd372",
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"miner": "0x9ffed2297c7b81293413550db675073ab46980b2",
"mixHash": "0x6fef2c5eded7b7d98dbf81bc40be1b5595eca162e2e98b2f8ae28a96c8bde1b1",
"nonce": "0x21d771488758293c",
"number": "0xa30533",
"parentHash": "0x0f1524bd3bb6e84ec397fa65cc7edb1effb7ef66945523f6f9f22e9ec5867586",
"receiptsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"size": "0x221",
"stateRoot": "0x12673e3f6dd58a1adf6d6ffab70374b53862064ba23de736e2f0ca8fa4aa35d5",
"timestamp": "0x60f91637",
"totalDifficulty": "0x799b46acb904b7",
"transactions": [],
"transactionsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"uncles": []
},
"mmr_root": "0x326c0a30d77b78d91595a4b68ace0f1c0d08d9cf80f98ab9abec5eb12adbd372"
}"#).unwrap();

dbg!(parcel.encode());
}

#[cfg(feature = "full-serde")]
pub fn assert_encoded_eq(raw: impl AsRef<str>, encoded: impl AsRef<[u8]>) {
let from_json = serde_json::from_str::<EthereumRelayHeaderParcel>(raw.as_ref()).unwrap();
let from_codec = decode(encoded);

assert_eq!(from_json, from_codec);
}

fn decode(encoded: impl AsRef<[u8]>) -> EthereumRelayHeaderParcel {
Decode::decode(&mut &*encoded.as_ref()).unwrap()
}

pub fn migrate(encoded: impl AsRef<[u8]>) {
let parcel = decode(encoded);
let number = parcel.header.number;

BestConfirmedBlockNumber::put(number);
Expand Down
28 changes: 26 additions & 2 deletions frame/bridge/relayer-game/src/lib.rs
Expand Up @@ -20,6 +20,30 @@

#![cfg_attr(not(feature = "std"), no_std)]

pub mod migration {
// --- darwinia-network ---
use crate::*;

pub fn migrate<T, I>()
where
T: Config<I>,
I: Instance,
{
<RelayHeaderParcelToResolve<T, I>>::put(<Vec<RelayHeaderId<T, I>>>::new());
<Affirmations<T, I>>::remove_all();
<BestConfirmedHeaderId<T, I>>::remove_all();
<RoundCounts<T, I>>::remove_all();
<AffirmTime<T, I>>::remove_all();
<GamesToUpdate<T, I>>::remove_all();

for (staker, _) in <Stakes<T, I>>::drain() {
T::RingCurrency::remove_lock(T::LockId::get(), &staker);
}

<GameSamplePoints<T, I>>::remove_all();
}
}

pub mod weights;
// --- darwinia ---
pub use weights::WeightInfo;
Expand Down Expand Up @@ -176,7 +200,7 @@ decl_storage! {

/// All the stakes here
pub Stakes
get(fn stakes_of)
get(fn stake_of)
: map hasher(blake2_128_concat) AccountId<T>
=> RingBalance<T, I>;

Expand Down Expand Up @@ -290,7 +314,7 @@ impl<T: Config<I>, I: Instance> Module<T, I> {
where
F: FnOnce(RingBalance<T, I>) -> RingBalance<T, I>,
{
let stakes = calc_stakes(Self::stakes_of(relayer));
let stakes = calc_stakes(Self::stake_of(relayer));

if stakes.is_zero() {
T::RingCurrency::remove_lock(T::LockId::get(), relayer);
Expand Down

0 comments on commit c5f30a3

Please sign in to comment.