-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Adding source to burnTransaction type (#888)
- Loading branch information
1 parent
faaf0c9
commit ab138c8
Showing
15 changed files
with
290 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
docs/architecture/0016-BurnTransaction-BurnTransactionExpired-changed.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# 16. Add Source account ID to BurnTransaction storage type and BurnTransactionExpired event | ||
|
||
Date: 2023-11-14 | ||
|
||
## Status | ||
|
||
Accepted | ||
|
||
## Context | ||
|
||
See [here](https://github.com/threefoldtech/tfchain/issues/883) for more details. | ||
|
||
## Decision | ||
|
||
Add `Source` account ID to `BurnTransaction` storage type and `BurnTransactionExpired` event and migrate previous `BurnTransactions` and `ExecutedBurnTransactions` storages |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
substrate-node/pallets/pallet-tft-bridge/src/migrations/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod types; | ||
pub mod v2; |
70 changes: 70 additions & 0 deletions
70
substrate-node/pallets/pallet-tft-bridge/src/migrations/types.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
pub mod v1 { | ||
use frame_support::{pallet_prelude::ValueQuery, storage_alias, Blake2_128Concat}; | ||
use parity_scale_codec::{Decode, Encode}; | ||
use scale_info::{prelude::vec::Vec, TypeInfo}; | ||
|
||
use crate::{types::StellarSignature, Config, Pallet}; | ||
|
||
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Decode, Default, Debug, TypeInfo)] | ||
pub struct BurnTransaction<BlockNumber> { | ||
pub block: BlockNumber, | ||
pub amount: u64, | ||
pub target: Vec<u8>, | ||
pub signatures: Vec<StellarSignature>, | ||
pub sequence_number: u64, | ||
} | ||
|
||
#[storage_alias] | ||
pub type BurnTransactions<T: Config> = StorageMap< | ||
Pallet<T>, | ||
Blake2_128Concat, | ||
u64, | ||
BurnTransaction<<T as frame_system::Config>::BlockNumber>, | ||
ValueQuery, | ||
>; | ||
|
||
#[storage_alias] | ||
pub type ExecutedBurnTransactions<T: Config> = StorageMap< | ||
Pallet<T>, | ||
Blake2_128Concat, | ||
u64, | ||
BurnTransaction<<T as frame_system::Config>::BlockNumber>, | ||
ValueQuery, | ||
>; | ||
} | ||
|
||
pub mod v2 { | ||
use frame_support::{pallet_prelude::OptionQuery, storage_alias, Blake2_128Concat}; | ||
use parity_scale_codec::{Decode, Encode}; | ||
use scale_info::{prelude::vec::Vec, TypeInfo}; | ||
|
||
use crate::{types::StellarSignature, Config, Pallet}; | ||
|
||
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Decode, Default, Debug, TypeInfo)] | ||
pub struct BurnTransaction<AccountId, BlockNumber> { | ||
pub block: BlockNumber, | ||
pub amount: u64, | ||
pub source: Option<AccountId>, | ||
pub target: Vec<u8>, | ||
pub signatures: Vec<StellarSignature>, | ||
pub sequence_number: u64, | ||
} | ||
|
||
#[storage_alias] | ||
pub type BurnTransactions<T: Config> = StorageMap< | ||
Pallet<T>, | ||
Blake2_128Concat, | ||
u64, | ||
BurnTransaction<<T as frame_system::Config>::AccountId, <T as frame_system::Config>::BlockNumber>, | ||
OptionQuery, | ||
>; | ||
|
||
#[storage_alias] | ||
pub type ExecutedBurnTransactions<T: Config> = StorageMap< | ||
Pallet<T>, | ||
Blake2_128Concat, | ||
u64, | ||
BurnTransaction<<T as frame_system::Config>::AccountId, <T as frame_system::Config>::BlockNumber>, | ||
OptionQuery, | ||
>; | ||
} |
117 changes: 117 additions & 0 deletions
117
substrate-node/pallets/pallet-tft-bridge/src/migrations/v2.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
use crate::*; | ||
use frame_support::log::{debug, info}; | ||
use frame_support::{traits::Get, traits::OnRuntimeUpgrade, weights::Weight}; | ||
use sp_std::marker::PhantomData; | ||
|
||
#[cfg(feature = "try-runtime")] | ||
use sp_std::vec::Vec; | ||
|
||
pub struct MigrateBurnTransactionsV2<T: Config>(PhantomData<T>); | ||
|
||
impl<T: Config> OnRuntimeUpgrade for MigrateBurnTransactionsV2<T> { | ||
#[cfg(feature = "try-runtime")] | ||
fn pre_upgrade() -> Result<Vec<u8>, &'static str> { | ||
info!("current pallet version: {:?}", PalletVersion::<T>::get()); | ||
if PalletVersion::<T>::get() != types::StorageVersion::V1 { | ||
return Ok(Vec::<u8>::new()); | ||
}; | ||
|
||
let burn_transactions_count: u64 = | ||
migrations::types::v1::BurnTransactions::<T>::iter().count() as u64; | ||
info!( | ||
"🔎 MigrateBurnTransactionsV2 pre migration: Number of existing burn transactions {:?}", | ||
burn_transactions_count | ||
); | ||
|
||
let executed_burn_transactions_count: u64 = | ||
migrations::types::v1::ExecutedBurnTransactions::<T>::iter().count() as u64; | ||
info!( | ||
"🔎 MigrateBurnTransactionsV2 pre migration: Number of existing executed burn transactions {:?}", | ||
executed_burn_transactions_count | ||
); | ||
|
||
info!("👥 TFT-BRIDGE pallet to V1 passes PRE migrate checks ✅",); | ||
return Ok(Vec::<u8>::new()); | ||
} | ||
|
||
fn on_runtime_upgrade() -> Weight { | ||
if PalletVersion::<T>::get() == types::StorageVersion::V1 { | ||
migrate_burn_transactions::<T>() | ||
} else { | ||
info!(" >>> Unused TFT-BRIDGE pallet V2 migration"); | ||
Weight::zero() | ||
} | ||
} | ||
|
||
#[cfg(feature = "try-runtime")] | ||
fn post_upgrade(_pre_burn_transactions_count: Vec<u8>) -> Result<(), &'static str> { | ||
info!("current pallet version: {:?}", PalletVersion::<T>::get()); | ||
if PalletVersion::<T>::get() != types::StorageVersion::V2 { | ||
return Ok(()); | ||
} | ||
let burn_transactions_count: u64 = migrations::types::v2::BurnTransactions::<T>::iter().count() as u64; | ||
info!( | ||
"🔎 MigrateBurnTransactionsV2 post migration: Number of existing burn transactions {:?}", | ||
burn_transactions_count | ||
); | ||
|
||
let executed_burn_transactions_count: u64 = | ||
migrations::types::v2::ExecutedBurnTransactions::<T>::iter().count() as u64; | ||
info!( | ||
"🔎 MigrateBurnTransactionsV2 post migration: Number of existing executed burn transactions {:?}", | ||
executed_burn_transactions_count | ||
); | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
pub fn migrate_burn_transactions<T: Config>() -> frame_support::weights::Weight { | ||
info!(" >>> Migrating burn transactions storage..."); | ||
|
||
let mut read_writes = 0; | ||
|
||
migrations::types::v2::BurnTransactions::<T>::translate::<super::types::v1::BurnTransaction<T::BlockNumber>, _>( | ||
|k, burn_transaction| { | ||
debug!("migrated burn transaction: {:?}", k); | ||
|
||
let new_burn_transaction = migrations::types::v2::BurnTransaction::<T::AccountId, T::BlockNumber> { | ||
block: burn_transaction.block, | ||
amount: burn_transaction.amount, | ||
source: None, | ||
target: burn_transaction.target, | ||
signatures: burn_transaction.signatures, | ||
sequence_number: burn_transaction.sequence_number, | ||
}; | ||
|
||
read_writes += 1; | ||
Some(new_burn_transaction) | ||
}, | ||
); | ||
|
||
migrations::types::v2::ExecutedBurnTransactions::<T>::translate::<super::types::v1::BurnTransaction<T::BlockNumber>, _>( | ||
|k, executed_burn_transaction| { | ||
debug!("migrated executed burn transaction: {:?}", k); | ||
|
||
let new_executed_burn_transaction = | ||
migrations::types::v2::BurnTransaction::<T::AccountId, T::BlockNumber> { | ||
block: executed_burn_transaction.block, | ||
amount: executed_burn_transaction.amount, | ||
source: None, | ||
target: executed_burn_transaction.target, | ||
signatures: executed_burn_transaction.signatures, | ||
sequence_number: executed_burn_transaction.sequence_number, | ||
}; | ||
|
||
read_writes += 1; | ||
Some(new_executed_burn_transaction) | ||
}, | ||
); | ||
|
||
// Update pallet storage version | ||
PalletVersion::<T>::set(types::StorageVersion::V2); | ||
info!(" <<< burnTransactions migration success, storage version upgraded"); | ||
|
||
// Return the weight consumed by the migration. | ||
T::DbWeight::get().reads_writes(read_writes, read_writes + 1) | ||
} |
Oops, something went wrong.