Skip to content

Commit

Permalink
fix: move resetting of the proxy account nonce to when constructed tx
Browse files Browse the repository at this point in the history
  • Loading branch information
ramizhasan111 committed Sep 27, 2023
1 parent 3c2afa8 commit 5f55d5b
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 46 deletions.
4 changes: 3 additions & 1 deletion state-chain/chains/src/dot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,12 @@ impl Chain for Polkadot {
type DepositChannelState = PolkadotChannelState;
type DepositDetails = ();
type Transaction = PolkadotTransactionData;
type ReplayProtectionParams = ();
type ReplayProtectionParams = ResetProxyAccountNonce;
type ReplayProtection = PolkadotReplayProtection;
}

pub type ResetProxyAccountNonce = bool;

#[derive(Clone, Encode, Decode, MaxEncodedLen, TypeInfo, Debug, PartialEq, Eq, Default)]
pub struct PolkadotChannelState;

Expand Down
7 changes: 4 additions & 3 deletions state-chain/chains/src/dot/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ where
transfer_params: Vec<TransferAssetParams<Polkadot>>,
) -> Result<Self, AllBatchError> {
Ok(Self::BatchFetchAndTransfer(batch_fetch_and_transfer::extrinsic_builder(
E::replay_protection(()),
E::replay_protection(false),
fetch_params,
transfer_params,
E::try_vault_account().ok_or(AllBatchError::Other)?,
Expand All @@ -85,7 +85,7 @@ where
let vault = E::try_vault_account().ok_or(())?;

Ok(Self::ChangeGovKey(rotate_vault_proxy::extrinsic_builder(
E::replay_protection(()),
E::replay_protection(false),
maybe_old_key,
new_key,
vault,
Expand All @@ -104,7 +104,8 @@ where
let vault = E::try_vault_account().ok_or(SetAggKeyWithAggKeyError::Failed)?;

Ok(Self::RotateVaultProxy(rotate_vault_proxy::extrinsic_builder(
E::replay_protection(()),
// we reset the proxy account nonce on a rotation tx
E::replay_protection(true),
maybe_old_key,
new_key,
vault,
Expand Down
16 changes: 9 additions & 7 deletions state-chain/pallets/cf-environment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,15 +386,17 @@ impl<T: Config> Pallet<T> {
})
}

pub fn next_polkadot_proxy_account_nonce() -> PolkadotIndex {
pub fn next_polkadot_proxy_account_nonce(reset_nonce: bool) -> PolkadotIndex {
PolkadotProxyAccountNonce::<T>::mutate(|nonce| {
*nonce += 1;
*nonce - 1
})
}
let current_nonce = *nonce;

pub fn reset_polkadot_proxy_account_nonce() {
PolkadotProxyAccountNonce::<T>::set(0);
if reset_nonce {
PolkadotProxyAccountNonce::<T>::set(0);
} else {
*nonce += 1;
}
current_nonce
})
}

pub fn add_bitcoin_utxo_to_list(
Expand Down
5 changes: 1 addition & 4 deletions state-chain/pallets/cf-vaults/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use cf_traits::{
impl_pallet_safe_mode, offence_reporting::OffenceReporter, AccountRoleRegistry, AsyncResult,
Broadcaster, Chainflip, CurrentEpochIndex, EpochKey, GetBlockHeight, KeyProvider, KeyState,
SafeMode, SetSafeMode, Slashing, ThresholdSigner, VaultKeyWitnessedHandler, VaultRotator,
VaultStatus, VaultTransitionHandler,
VaultStatus,
};
use frame_support::{
pallet_prelude::*,
Expand Down Expand Up @@ -174,8 +174,6 @@ pub mod pallet {
<<Self as pallet::Config<I>>::Chain as Chain>::ChainCrypto,
>;

type VaultTransitionHandler: VaultTransitionHandler<Self::Chain>;

/// The pallet dispatches calls, so it depends on the runtime's aggregated Call type.
type RuntimeCall: From<Call<Self, I>> + IsType<<Self as frame_system::Config>::RuntimeCall>;

Expand Down Expand Up @@ -954,7 +952,6 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
active_from_block: block_number.saturating_add(One::one()),
},
);
T::VaultTransitionHandler::on_new_vault();
Self::deposit_event(Event::VaultRotationCompleted);
}
}
Expand Down
6 changes: 0 additions & 6 deletions state-chain/pallets/cf-vaults/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,6 @@ impl ApiCall<MockEthereumChainCrypto> for MockSetAggKeyWithAggKey {
}
}

pub struct MockVaultTransitionHandler;
impl VaultTransitionHandler<MockEthereum> for MockVaultTransitionHandler {
fn on_new_vault() {}
}

pub struct MockBroadcaster;

impl MockBroadcaster {
Expand Down Expand Up @@ -214,7 +209,6 @@ impl pallet_cf_vaults::Config for Test {
type ThresholdSigner = MockThresholdSigner<MockEthereumChainCrypto, RuntimeCall>;
type OffenceReporter = MockOffenceReporter;
type SetAggKeyWithAggKey = MockSetAggKeyWithAggKey;
type VaultTransitionHandler = MockVaultTransitionHandler;
type WeightInfo = ();
type Broadcaster = MockBroadcaster;
type SafeMode = MockRuntimeSafeMode;
Expand Down
19 changes: 3 additions & 16 deletions state-chain/runtime/src/chainflip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use cf_chains::{
},
dot::{
api::PolkadotApi, Polkadot, PolkadotAccountId, PolkadotCrypto, PolkadotReplayProtection,
PolkadotTransactionData, RuntimeVersion,
PolkadotTransactionData, ResetProxyAccountNonce, RuntimeVersion,
},
eth::{
self,
Expand All @@ -47,7 +47,6 @@ use cf_traits::{
AccountRoleRegistry, BlockEmissions, BroadcastAnyChainGovKey, Broadcaster, Chainflip,
CommKeyBroadcaster, DepositApi, DepositHandler, EgressApi, EpochInfo, Heartbeat, Issuance,
KeyProvider, OnBroadcastReady, QualifyNode, RewardsDistribution, RuntimeUpgrade,
VaultTransitionHandler,
};
use codec::{Decode, Encode};
use frame_support::{
Expand Down Expand Up @@ -330,10 +329,10 @@ pub struct DotEnvironment;
impl ReplayProtectionProvider<Polkadot> for DotEnvironment {
// Get the Environment values for vault_account, NetworkChoice and the next nonce for the
// proxy_account
fn replay_protection(_params: ()) -> PolkadotReplayProtection {
fn replay_protection(reset_nonce: ResetProxyAccountNonce) -> PolkadotReplayProtection {
PolkadotReplayProtection {
genesis_hash: Environment::polkadot_genesis_hash(),
nonce: Environment::next_polkadot_proxy_account_nonce(),
nonce: Environment::next_polkadot_proxy_account_nonce(reset_nonce),
}
}
}
Expand Down Expand Up @@ -376,18 +375,6 @@ impl ChainEnvironment<(), cf_chains::btc::AggKey> for BtcEnvironment {
}
}

pub struct EthVaultTransitionHandler;
impl VaultTransitionHandler<Ethereum> for EthVaultTransitionHandler {}

pub struct DotVaultTransitionHandler;
impl VaultTransitionHandler<Polkadot> for DotVaultTransitionHandler {
fn on_new_vault() {
Environment::reset_polkadot_proxy_account_nonce();
}
}
pub struct BtcVaultTransitionHandler;
impl VaultTransitionHandler<Bitcoin> for BtcVaultTransitionHandler {}

pub struct TokenholderGovernanceBroadcaster;

impl TokenholderGovernanceBroadcaster {
Expand Down
8 changes: 2 additions & 6 deletions state-chain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,8 @@ pub use cf_traits::{EpochInfo, QualifyNode, SessionKeysRegistered, SwappingApi};
pub use chainflip::chain_instances::*;
use chainflip::{
all_vaults_rotator::AllVaultRotator, epoch_transition::ChainflipEpochTransitions,
BroadcastReadyProvider, BtcEnvironment, BtcVaultTransitionHandler, ChainAddressConverter,
ChainflipHeartbeat, DotEnvironment, DotVaultTransitionHandler, EthEnvironment,
EthVaultTransitionHandler, TokenholderGovernanceBroadcaster,
BroadcastReadyProvider, BtcEnvironment, ChainAddressConverter, ChainflipHeartbeat,
DotEnvironment, EthEnvironment, TokenholderGovernanceBroadcaster,
};
use safe_mode::{RuntimeSafeMode, WitnesserCallPermission};

Expand Down Expand Up @@ -241,7 +240,6 @@ impl pallet_cf_vaults::Config<EthereumInstance> for Runtime {
type Offence = chainflip::Offence;
type Chain = Ethereum;
type SetAggKeyWithAggKey = eth::api::EthereumApi<EthEnvironment>;
type VaultTransitionHandler = EthVaultTransitionHandler;
type Broadcaster = EthereumBroadcaster;
type OffenceReporter = Reputation;
type WeightInfo = pallet_cf_vaults::weights::PalletWeight<Runtime>;
Expand All @@ -259,7 +257,6 @@ impl pallet_cf_vaults::Config<PolkadotInstance> for Runtime {
type Offence = chainflip::Offence;
type Chain = Polkadot;
type SetAggKeyWithAggKey = dot::api::PolkadotApi<DotEnvironment>;
type VaultTransitionHandler = DotVaultTransitionHandler;
type Broadcaster = PolkadotBroadcaster;
type OffenceReporter = Reputation;
type WeightInfo = pallet_cf_vaults::weights::PalletWeight<Runtime>;
Expand All @@ -277,7 +274,6 @@ impl pallet_cf_vaults::Config<BitcoinInstance> for Runtime {
type Offence = chainflip::Offence;
type Chain = Bitcoin;
type SetAggKeyWithAggKey = cf_chains::btc::api::BitcoinApi<BtcEnvironment>;
type VaultTransitionHandler = BtcVaultTransitionHandler;
type Broadcaster = BitcoinBroadcaster;
type OffenceReporter = Reputation;
type WeightInfo = pallet_cf_vaults::weights::PalletWeight<Runtime>;
Expand Down
3 changes: 0 additions & 3 deletions state-chain/traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,9 +730,6 @@ impl<T: frame_system::Config> EgressApi<Polkadot> for T {
}
}

pub trait VaultTransitionHandler<C: Chain> {
fn on_new_vault() {}
}
pub trait VaultKeyWitnessedHandler<C: Chain> {
fn on_new_key_activated(block_number: C::ChainBlockNumber) -> DispatchResultWithPostInfo;
}
Expand Down

0 comments on commit 5f55d5b

Please sign in to comment.