Skip to content

Commit

Permalink
Implement storage failure rollback (#621)
Browse files Browse the repository at this point in the history
* Fix remove proposal

* Add transactional for gateway

* Delete unnecessary transactional

* Fix cancel_trustee_election

* Fix set_withdrawal_state

* Fix remove_proposal

 * Regenerate pallet weight

* Bump version into 17

Co-authored-by: hacpy <hacpy.lbx@gmail.com>
  • Loading branch information
AAweidai and hacpy committed Mar 16, 2022
1 parent cfd879f commit fa33c8e
Show file tree
Hide file tree
Showing 15 changed files with 176 additions and 162 deletions.
2 changes: 1 addition & 1 deletion runtime/chainx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("chainx"),
impl_name: create_runtime_str!("chainx-net"),
authoring_version: 1,
spec_version: 16,
spec_version: 17,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 4,
Expand Down
2 changes: 1 addition & 1 deletion runtime/dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("chainx"),
impl_name: create_runtime_str!("chainx-dev"),
authoring_version: 1,
spec_version: 16,
spec_version: 17,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 4,
Expand Down
2 changes: 1 addition & 1 deletion runtime/malan/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("chainx"),
impl_name: create_runtime_str!("chainx-malan"),
authoring_version: 1,
spec_version: 16,
spec_version: 17,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 4,
Expand Down
18 changes: 9 additions & 9 deletions xpallets/assets-registrar/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

//! Weights for xpallet_assets_registrar
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2022-03-14, STEPS: 50, REPEAT: 20, LOW RANGE: [], HIGH RANGE: []
//! DATE: 2022-03-16, STEPS: 50, REPEAT: 20, LOW RANGE: [], HIGH RANGE: []
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("benchmarks"), DB CACHE: 128

// Executed Command:
Expand Down Expand Up @@ -41,22 +41,22 @@ pub trait WeightInfo {
pub struct SubstrateWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
fn register() -> Weight {
(56_934_000 as Weight)
(65_046_000 as Weight)
.saturating_add(T::DbWeight::get().reads(3 as Weight))
.saturating_add(T::DbWeight::get().writes(6 as Weight))
}
fn deregister() -> Weight {
(28_004_000 as Weight)
(33_203_000 as Weight)
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
}
fn recover() -> Weight {
(38_430_000 as Weight)
(43_419_000 as Weight)
.saturating_add(T::DbWeight::get().reads(3 as Weight))
.saturating_add(T::DbWeight::get().writes(3 as Weight))
}
fn update_asset_info() -> Weight {
(12_783_000 as Weight)
(14_237_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
Expand All @@ -65,22 +65,22 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// For backwards compatibility and tests
impl WeightInfo for () {
fn register() -> Weight {
(56_934_000 as Weight)
(65_046_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(3 as Weight))
.saturating_add(RocksDbWeight::get().writes(6 as Weight))
}
fn deregister() -> Weight {
(28_004_000 as Weight)
(33_203_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(2 as Weight))
.saturating_add(RocksDbWeight::get().writes(2 as Weight))
}
fn recover() -> Weight {
(38_430_000 as Weight)
(43_419_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(3 as Weight))
.saturating_add(RocksDbWeight::get().writes(3 as Weight))
}
fn update_asset_info() -> Weight {
(12_783_000 as Weight)
(14_237_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
Expand Down
18 changes: 9 additions & 9 deletions xpallets/assets/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

//! Weights for xpallet_assets
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2022-03-14, STEPS: 50, REPEAT: 20, LOW RANGE: [], HIGH RANGE: []
//! DATE: 2022-03-16, STEPS: 50, REPEAT: 20, LOW RANGE: [], HIGH RANGE: []
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("benchmarks"), DB CACHE: 128

// Executed Command:
Expand Down Expand Up @@ -41,22 +41,22 @@ pub trait WeightInfo {
pub struct SubstrateWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
fn transfer() -> Weight {
(155_430_000 as Weight)
(153_678_000 as Weight)
.saturating_add(T::DbWeight::get().reads(8 as Weight))
.saturating_add(T::DbWeight::get().writes(6 as Weight))
}
fn force_transfer() -> Weight {
(266_433_000 as Weight)
(153_437_000 as Weight)
.saturating_add(T::DbWeight::get().reads(8 as Weight))
.saturating_add(T::DbWeight::get().writes(6 as Weight))
}
fn set_balance(_n: u32) -> Weight {
(206_078_000 as Weight)
(141_113_000 as Weight)
.saturating_add(T::DbWeight::get().reads(3 as Weight))
.saturating_add(T::DbWeight::get().writes(3 as Weight))
}
fn set_asset_limit() -> Weight {
(9_785_000 as Weight)
(9_327_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
Expand All @@ -65,22 +65,22 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// For backwards compatibility and tests
impl WeightInfo for () {
fn transfer() -> Weight {
(155_430_000 as Weight)
(153_678_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(8 as Weight))
.saturating_add(RocksDbWeight::get().writes(6 as Weight))
}
fn force_transfer() -> Weight {
(266_433_000 as Weight)
(153_437_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(8 as Weight))
.saturating_add(RocksDbWeight::get().writes(6 as Weight))
}
fn set_balance(_n: u32) -> Weight {
(206_078_000 as Weight)
(141_113_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(3 as Weight))
.saturating_add(RocksDbWeight::get().writes(3 as Weight))
}
fn set_asset_limit() -> Weight {
(9_785_000 as Weight)
(9_327_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
Expand Down
30 changes: 15 additions & 15 deletions xpallets/dex/spot/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

//! Weights for xpallet_dex_spot
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2022-03-14, STEPS: 50, REPEAT: 20, LOW RANGE: [], HIGH RANGE: []
//! DATE: 2022-03-16, STEPS: 50, REPEAT: 20, LOW RANGE: [], HIGH RANGE: []
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("benchmarks"), DB CACHE: 128

// Executed Command:
Expand Down Expand Up @@ -44,33 +44,33 @@ pub trait WeightInfo {
pub struct SubstrateWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
fn put_order() -> Weight {
(144_806_000 as Weight)
(150_111_000 as Weight)
.saturating_add(T::DbWeight::get().reads(11 as Weight))
.saturating_add(T::DbWeight::get().writes(6 as Weight))
}
fn cancel_order() -> Weight {
(131_865_000 as Weight)
(141_411_000 as Weight)
.saturating_add(T::DbWeight::get().reads(8 as Weight))
.saturating_add(T::DbWeight::get().writes(5 as Weight))
}
fn force_cancel_order() -> Weight {
(130_662_000 as Weight)
(137_268_000 as Weight)
.saturating_add(T::DbWeight::get().reads(8 as Weight))
.saturating_add(T::DbWeight::get().writes(5 as Weight))
}
fn set_handicap() -> Weight {
(13_681_000 as Weight).saturating_add(T::DbWeight::get().writes(1 as Weight))
(17_195_000 as Weight).saturating_add(T::DbWeight::get().writes(1 as Weight))
}
fn set_price_fluctuation() -> Weight {
(16_741_000 as Weight).saturating_add(T::DbWeight::get().writes(1 as Weight))
(17_500_000 as Weight).saturating_add(T::DbWeight::get().writes(1 as Weight))
}
fn add_trading_pair() -> Weight {
(42_442_000 as Weight)
(45_422_000 as Weight)
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(3 as Weight))
}
fn update_trading_pair() -> Weight {
(35_317_000 as Weight)
(38_741_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
Expand All @@ -79,33 +79,33 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// For backwards compatibility and tests
impl WeightInfo for () {
fn put_order() -> Weight {
(144_806_000 as Weight)
(150_111_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(11 as Weight))
.saturating_add(RocksDbWeight::get().writes(6 as Weight))
}
fn cancel_order() -> Weight {
(131_865_000 as Weight)
(141_411_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(8 as Weight))
.saturating_add(RocksDbWeight::get().writes(5 as Weight))
}
fn force_cancel_order() -> Weight {
(130_662_000 as Weight)
(137_268_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(8 as Weight))
.saturating_add(RocksDbWeight::get().writes(5 as Weight))
}
fn set_handicap() -> Weight {
(13_681_000 as Weight).saturating_add(RocksDbWeight::get().writes(1 as Weight))
(17_195_000 as Weight).saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
fn set_price_fluctuation() -> Weight {
(16_741_000 as Weight).saturating_add(RocksDbWeight::get().writes(1 as Weight))
(17_500_000 as Weight).saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
fn add_trading_pair() -> Weight {
(42_442_000 as Weight)
(45_422_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(2 as Weight))
.saturating_add(RocksDbWeight::get().writes(3 as Weight))
}
fn update_trading_pair() -> Weight {
(35_317_000 as Weight)
(38_741_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
Expand Down
12 changes: 12 additions & 0 deletions xpallets/gateway/bitcoin/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,25 @@ benchmarks! {
}

remove_proposal {
let caller = alice::<T>();
let amount: BalanceOf<T> = 1_000_000_000u32.into();
let withdrawal: BalanceOf<T> = 10000u32.into();

XGatewayRecords::<T>::deposit(&caller, X_BTC, amount).unwrap();
XGatewayRecords::<T>::withdraw(&caller, X_BTC, withdrawal, b"tb1pexff2s7l58sthpyfrtx500ax234stcnt0gz2lr4kwe0ue95a2e0srxsc68".to_vec(), b"".to_vec().into()).unwrap();
XGatewayRecords::<T>::withdraw(&caller, X_BTC, withdrawal, b"tb1pexff2s7l58sthpyfrtx500ax234stcnt0gz2lr4kwe0ue95a2e0srxsc68".to_vec(), b"".to_vec().into()).unwrap();

XGatewayRecords::<T>::withdrawal_state_insert(0, WithdrawalState::Processing);
XGatewayRecords::<T>::withdrawal_state_insert(0, WithdrawalState::Processing);

let (tx, _, _) = withdraw_tx();
let proposal = BtcWithdrawalProposal::<T::AccountId> {
sig_state: VoteResult::Unfinish,
withdrawal_id_list: vec![0, 1],
tx,
trustee_list: vec![],
};

WithdrawalProposal::<T>::put(proposal);
}: _(RawOrigin::Root)
verify {
Expand Down
39 changes: 17 additions & 22 deletions xpallets/gateway/bitcoin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ macro_rules! log {
pub mod pallet {
use sp_std::marker::PhantomData;

use frame_support::{dispatch::DispatchResult, pallet_prelude::*, traits::UnixTime};
use frame_support::{
dispatch::DispatchResult, pallet_prelude::*, traits::UnixTime, transactional,
};
use frame_system::pallet_prelude::*;
use sp_runtime::traits::Saturating;

Expand Down Expand Up @@ -226,31 +228,12 @@ pub mod pallet {
/// Dangerous! remove current withdrawal proposal directly. Please check business logic before
/// do this operation.
#[pallet::weight(<T as Config>::WeightInfo::remove_proposal())]
#[transactional]
pub fn remove_proposal(origin: OriginFor<T>) -> DispatchResult {
T::CouncilOrigin::try_origin(origin)
.map(|_| ())
.or_else(ensure_root)?;

if let Some(proposal) = WithdrawalProposal::<T>::take() {
proposal.withdrawal_id_list.iter().for_each(|id| {
match xpallet_gateway_records::Pallet::<T>::set_withdrawal_state_by_trustees(
*id,
Chain::Bitcoin,
xpallet_gateway_records::WithdrawalState::Applying,
) {
Ok(_) => {}
Err(e) => {
log!(
error,
"[remove_proposal] set withdrawal state error, id:{:?}, error:{:?}",
id,
e
);
}
}
});
}
Ok(())
Self::apply_remove_proposal()
}

/// Dangerous! Be careful to set BestIndex
Expand Down Expand Up @@ -776,6 +759,18 @@ pub mod pallet {
BtcTxResult::Failure => Err(Error::<T>::ProcessTxFailed.into()),
}
}

pub(crate) fn apply_remove_proposal() -> DispatchResult {
if let Some(proposal) = WithdrawalProposal::<T>::take() {
for id in proposal.withdrawal_id_list.iter() {
xpallet_gateway_records::Pallet::<T>::set_withdrawal_state_by_root(
*id,
xpallet_gateway_records::WithdrawalState::Applying,
)?;
}
}
Ok(())
}
}

impl<T: Config> Pallet<T> {
Expand Down
Loading

0 comments on commit fa33c8e

Please sign in to comment.