Skip to content

Commit

Permalink
fix: polkadot nonce issue (#4054)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramizhasan111 authored and dandanlen committed Oct 4, 2023
1 parent 97873de commit e7723cd
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 49 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 @@ -387,15 +387,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 {
*nonce = 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 @@ -961,7 +959,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 @@ -132,11 +132,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 @@ -223,7 +218,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
20 changes: 4 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,7 @@ use cf_traits::{
impl_runtime_safe_mode, AccountRoleRegistry, BlockEmissions, BroadcastAnyChainGovKey,
Broadcaster, Chainflip, CommKeyBroadcaster, DepositApi, DepositHandler, EgressApi, EpochInfo,
Heartbeat, Issuance, KeyProvider, OnBroadcastReady, QualifyNode, RewardsDistribution,
RuntimeUpgrade, VaultTransitionHandler,
RuntimeUpgrade,
};
use codec::{Decode, Encode};
use frame_support::{
Expand Down Expand Up @@ -323,10 +323,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 @@ -369,18 +369,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
13 changes: 4 additions & 9 deletions state-chain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,13 @@ use sp_version::RuntimeVersion;

pub use cf_primitives::{Asset, AssetAmount, BlockNumber, FlipBalance, SemVer, SwapOutput};
pub use cf_traits::{EpochInfo, QualifyNode, SessionKeysRegistered, SwappingApi};

pub use chainflip::chain_instances::*;

use chainflip::{
epoch_transition::ChainflipEpochTransitions, BroadcastReadyProvider, BtcEnvironment,
BtcVaultTransitionHandler, ChainAddressConverter, ChainflipHeartbeat, EthEnvironment,
EthVaultTransitionHandler, TokenholderGovernanceBroadcaster,
all_vaults_rotator::AllVaultRotator, epoch_transition::ChainflipEpochTransitions,
BroadcastReadyProvider, BtcEnvironment, ChainAddressConverter, ChainflipHeartbeat,
DotEnvironment, EthEnvironment, TokenholderGovernanceBroadcaster,
};

use chainflip::{all_vaults_rotator::AllVaultRotator, DotEnvironment, DotVaultTransitionHandler};
use constants::common::*;
use pallet_cf_flip::{Bonder, FlipSlasher};
use pallet_cf_vaults::Vault;
Expand Down Expand Up @@ -230,7 +228,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 @@ -248,7 +245,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 @@ -266,7 +262,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 @@ -738,9 +738,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 e7723cd

Please sign in to comment.