Skip to content

Commit

Permalink
chore: comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ramizhasan111 committed Nov 14, 2023
1 parent c2a2e20 commit fe789b9
Show file tree
Hide file tree
Showing 16 changed files with 80 additions and 130 deletions.
36 changes: 13 additions & 23 deletions state-chain/pallets/cf-broadcast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,8 +726,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
pub fn threshold_sign_and_broadcast(
api_call: <T as Config<I>>::ApiCall,
maybe_callback: Option<<T as Config<I>>::BroadcastCallable>,
pause_broadcasts: bool,
) -> (BroadcastId, ThresholdSignatureRequestId) {
) -> BroadcastId {
let broadcast_id = BroadcastIdCounter::<T, I>::mutate(|id| {
*id += 1;
*id
Expand All @@ -743,7 +742,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
let initiated_at = T::ChainTracking::get_block_height();

let threshold_signature_payload = api_call.threshold_signature_payload();
let signature_request_id = T::ThresholdSigner::request_signature_with_callback(
T::ThresholdSigner::request_signature_with_callback(
threshold_signature_payload.clone(),
|threshold_request_id| {
Call::on_signature_ready {
Expand All @@ -756,11 +755,8 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
.into()
},
);
if pause_broadcasts {
BroadcastPause::<T, I>::set(Some(broadcast_id));
}

(broadcast_id, signature_request_id)
broadcast_id
}

/// Begin the process of broadcasting a transaction.
Expand Down Expand Up @@ -797,16 +793,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
};

if BroadcastPause::<T, I>::get()
.and_then(
|pause_broadcast_id| {
if broadcast_id > pause_broadcast_id {
Some(())
} else {
None
}
},
)
.is_some()
.is_some_and(|pause_broadcast_id| broadcast_id > pause_broadcast_id)
{
Self::schedule_for_retry(&broadcast_attempt);
} else {
Expand Down Expand Up @@ -851,10 +838,9 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
// to retry from the threshold signing stage.
else {
Self::clean_up_broadcast_storage(broadcast_id);
let (retry_broadcast_id, _) = Self::threshold_sign_and_broadcast(
let retry_broadcast_id = Self::threshold_sign_and_broadcast(
api_call,
RequestCallbacks::<T, I>::get(broadcast_id),
false,
);
log::info!(
"Signature is invalid -> rescheduled threshold signature for broadcast id {}.",
Expand Down Expand Up @@ -935,14 +921,18 @@ impl<T: Config<I>, I: 'static> Broadcaster<T::TargetChain> for Pallet<T, I> {
fn threshold_sign_and_broadcast(
api_call: Self::ApiCall,
pause_broadcasts: bool,
) -> (BroadcastId, ThresholdSignatureRequestId) {
Self::threshold_sign_and_broadcast(api_call, None, pause_broadcasts)
) -> BroadcastId {
let broadcast_id = Self::threshold_sign_and_broadcast(api_call, None);
if pause_broadcasts {
BroadcastPause::<T, I>::set(Some(broadcast_id));
}
broadcast_id
}

fn threshold_sign_and_broadcast_with_callback(
api_call: Self::ApiCall,
callback: Self::Callback,
) -> (BroadcastId, ThresholdSignatureRequestId) {
Self::threshold_sign_and_broadcast(api_call, Some(callback), false)
) -> BroadcastId {
Self::threshold_sign_and_broadcast(api_call, Some(callback))
}
}
24 changes: 14 additions & 10 deletions state-chain/pallets/cf-broadcast/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::{
mock::*, AwaitingBroadcast, BroadcastAttemptCount, BroadcastAttemptId, BroadcastId,
BroadcastRetryQueue, Error, Event as BroadcastEvent, FailedBroadcasters, Instance1,
BroadcastRetryQueue, Config, Error, Event as BroadcastEvent, FailedBroadcasters, Instance1,
PalletOffence, RequestCallbacks, ThresholdSignatureData, Timeouts, TransactionFeeDeficit,
TransactionMetadata, TransactionOutIdToBroadcastId, WeightInfo,
};
Expand All @@ -17,7 +17,8 @@ use cf_chains::{
};
use cf_traits::{
mocks::{signer_nomination::MockNominator, threshold_signer::MockThresholdSigner},
AsyncResult, Chainflip, EpochInfo, SetSafeMode, ThresholdSigner,
AsyncResult, Broadcaster as BroadcasterTrait, Chainflip, EpochInfo, SetSafeMode,
ThresholdSigner,
};
use frame_support::{assert_noop, assert_ok, dispatch::Weight, traits::Hooks};
use frame_system::RawOrigin;
Expand Down Expand Up @@ -496,8 +497,8 @@ fn threshold_sign_and_broadcast_with_callback() {
tx_out_id: MOCK_TRANSACTION_OUT_ID,
};

let (broadcast_id, _threshold_request_id) =
Broadcaster::threshold_sign_and_broadcast(api_call.clone(), Some(MockCallback), false);
let broadcast_id =
Broadcaster::threshold_sign_and_broadcast(api_call.clone(), Some(MockCallback));

EthMockThresholdSigner::execute_signature_result_against_last_request(Ok(ETH_DUMMY_SIG));

Expand Down Expand Up @@ -607,8 +608,9 @@ fn broadcast_pause() {

// create and sign 3 txs that are then ready for broadcast

let (broadcast_id_1, _) =
Broadcaster::threshold_sign_and_broadcast(api_call1.clone(), None, false);
let broadcast_id_1 = <Broadcaster as BroadcasterTrait<
<Test as Config<Instance1>>::TargetChain,
>>::threshold_sign_and_broadcast(api_call1.clone(), false);

EthMockThresholdSigner::execute_signature_result_against_last_request(Ok(ETH_DUMMY_SIG));

Expand All @@ -625,8 +627,9 @@ fn broadcast_pause() {
},
));

let (broadcast_id_2, _) =
Broadcaster::threshold_sign_and_broadcast(api_call2.clone(), None, true);
let broadcast_id_2 = <Broadcaster as BroadcasterTrait<
<Test as Config<Instance1>>::TargetChain,
>>::threshold_sign_and_broadcast(api_call2.clone(), true);

EthMockThresholdSigner::execute_signature_result_against_last_request(Ok(ETH_DUMMY_SIG));

Expand All @@ -643,8 +646,9 @@ fn broadcast_pause() {
},
));

let (broadcast_id_3, _) =
Broadcaster::threshold_sign_and_broadcast(api_call3.clone(), None, false);
let broadcast_id_3 = <Broadcaster as BroadcasterTrait<
<Test as Config<Instance1>>::TargetChain,
>>::threshold_sign_and_broadcast(api_call3.clone(), false);

EthMockThresholdSigner::execute_signature_result_against_last_request(Ok(ETH_DUMMY_SIG));

Expand Down
8 changes: 4 additions & 4 deletions state-chain/pallets/cf-emissions/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use cf_chains::{
mocks::{MockEthereum, MockEthereumChainCrypto},
AnyChain, ApiCall, ChainCrypto, UpdateFlipSupply,
};
use cf_primitives::{BroadcastId, FlipBalance, ThresholdSignatureRequestId};
use cf_primitives::{BroadcastId, FlipBalance};
use cf_traits::{
impl_mock_callback, impl_mock_chainflip, impl_mock_runtime_safe_mode, impl_mock_waived_fees,
mocks::{egress_handler::MockEgressHandler, eth_environment_provider::MockEthEnvironment},
Expand Down Expand Up @@ -181,15 +181,15 @@ impl Broadcaster<MockEthereum> for MockBroadcast {
fn threshold_sign_and_broadcast(
api_call: Self::ApiCall,
_pause_broadcasts: bool,
) -> (BroadcastId, ThresholdSignatureRequestId) {
) -> BroadcastId {
Self::call(api_call);
(1, 2)
1
}

fn threshold_sign_and_broadcast_with_callback(
_api_call: Self::ApiCall,
_callback: Self::Callback,
) -> (BroadcastId, ThresholdSignatureRequestId) {
) -> BroadcastId {
unimplemented!()
}
}
Expand Down
8 changes: 4 additions & 4 deletions state-chain/pallets/cf-environment/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use cf_chains::{
eth, ApiCall, Bitcoin, Chain, ChainCrypto, Polkadot,
};
use cf_primitives::{
BroadcastId, SemVer, ThresholdSignatureRequestId, INPUT_UTXO_SIZE_IN_BYTES,
MINIMUM_BTC_TX_SIZE_IN_BYTES, OUTPUT_UTXO_SIZE_IN_BYTES,
BroadcastId, SemVer, INPUT_UTXO_SIZE_IN_BYTES, MINIMUM_BTC_TX_SIZE_IN_BYTES,
OUTPUT_UTXO_SIZE_IN_BYTES,
};
use cf_traits::{
impl_mock_callback, impl_mock_chainflip, impl_mock_runtime_safe_mode, impl_pallet_safe_mode,
Expand Down Expand Up @@ -106,14 +106,14 @@ impl Broadcaster<Polkadot> for MockPolkadotBroadcaster {
fn threshold_sign_and_broadcast(
_api_call: Self::ApiCall,
_pause_broadcasts: bool,
) -> (BroadcastId, ThresholdSignatureRequestId) {
) -> BroadcastId {
unimplemented!()
}

fn threshold_sign_and_broadcast_with_callback(
_api_call: Self::ApiCall,
_callback: Self::Callback,
) -> (BroadcastId, ThresholdSignatureRequestId) {
) -> BroadcastId {
unimplemented!()
}
}
Expand Down
2 changes: 1 addition & 1 deletion state-chain/pallets/cf-funding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ pub mod pallet {
Self::deposit_event(Event::RedemptionRequested {
account_id,
amount: net_amount,
broadcast_id: T::Broadcaster::threshold_sign_and_broadcast(call, false).0,
broadcast_id: T::Broadcaster::threshold_sign_and_broadcast(call, false),
expiry_time: contract_expiry,
});
} else {
Expand Down
8 changes: 4 additions & 4 deletions state-chain/pallets/cf-funding/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate as pallet_cf_funding;
use crate::PalletSafeMode;
use cf_chains::{evm::EvmCrypto, ApiCall, Chain, ChainCrypto, Ethereum};
use cf_primitives::{AccountRole, BroadcastId, ThresholdSignatureRequestId};
use cf_primitives::{AccountRole, BroadcastId};
use cf_traits::{
impl_mock_callback, impl_mock_chainflip, impl_mock_runtime_safe_mode, impl_mock_waived_fees,
mocks::time_source, AccountRoleRegistry, Broadcaster, WaivedFees,
Expand Down Expand Up @@ -163,17 +163,17 @@ impl Broadcaster<Ethereum> for MockBroadcaster {
fn threshold_sign_and_broadcast(
api_call: Self::ApiCall,
_pause_broadcasts: bool,
) -> (BroadcastId, ThresholdSignatureRequestId) {
) -> BroadcastId {
REDEMPTION_BROADCAST_REQUESTS.with(|cell| {
cell.borrow_mut().push(api_call.amount);
});
(0, 1)
0
}

fn threshold_sign_and_broadcast_with_callback(
_api_call: Self::ApiCall,
_callback: Self::Callback,
) -> (BroadcastId, ThresholdSignatureRequestId) {
) -> BroadcastId {
unimplemented!()
}
}
Expand Down
4 changes: 2 additions & 2 deletions state-chain/pallets/cf-ingress-egress/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
transfer_params,
) {
Ok(egress_transaction) => {
let (broadcast_id, _) = T::Broadcaster::threshold_sign_and_broadcast_with_callback(
let broadcast_id = T::Broadcaster::threshold_sign_and_broadcast_with_callback(
egress_transaction,
Call::finalise_ingress { addresses }.into(),
);
Expand Down Expand Up @@ -769,7 +769,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
ccm.message.to_vec(),
) {
Ok(api_call) => {
let (broadcast_id, _) =
let broadcast_id =
T::Broadcaster::threshold_sign_and_broadcast(api_call, false);
Self::deposit_event(Event::<T, I>::CcmBroadcastRequested {
broadcast_id,
Expand Down
32 changes: 21 additions & 11 deletions state-chain/pallets/cf-vaults/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,6 @@ pub enum PalletOffence {
FailedKeyHandover,
}

#[derive(Encode, Decode, TypeInfo)]
pub struct VaultEpoch {
pub epoch_index: EpochIndex,
}

#[frame_support::pallet]
pub mod pallet {

Expand Down Expand Up @@ -371,7 +366,7 @@ pub mod pallet {
/// The epoch whose authorities control the current vault key.
#[pallet::storage]
#[pallet::getter(fn current_keyholders_epoch)]
pub type CurrentVaultEpoch<T: Config<I>, I: 'static = ()> = StorageValue<_, VaultEpoch>;
pub type CurrentVaultEpoch<T: Config<I>, I: 'static = ()> = StorageValue<_, EpochIndex>;

/// Vault rotation statuses for the current epoch rotation.
#[pallet::storage]
Expand Down Expand Up @@ -678,6 +673,21 @@ pub mod pallet {
)
}

/// Deprecated! This extrinsic does nothing
#[pallet::call_index(3)]
#[pallet::weight(Weight::zero())]
pub fn vault_key_rotated(
origin: OriginFor<T>,
_block_number: ChainBlockNumberFor<T, I>,

// This field is primarily required to ensure the witness calls are unique per
// transaction (on the external chain)
_tx_id: TransactionInIdFor<T, I>,
) -> DispatchResultWithPostInfo {
T::EnsureWitnessedAtCurrentEpoch::ensure_origin(origin)?;
Ok(().into())
}

/// The vault's key has been updated externally, outside of the rotation
/// cycle. This is an unexpected event as far as our chain is concerned, and
/// the only thing we can do is to halt and wait for further governance
Expand Down Expand Up @@ -844,7 +854,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {

fn set_vault_key_for_epoch(epoch_index: EpochIndex, vault: Vault<T::Chain>) {
Vaults::<T, I>::insert(epoch_index, vault);
CurrentVaultEpoch::<T, I>::put(VaultEpoch { epoch_index });
CurrentVaultEpoch::<T, I>::put(epoch_index);
}

// Once we've successfully generated the key, we want to do a signing ceremony to verify that
Expand Down Expand Up @@ -953,11 +963,11 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
impl<T: Config<I>, I: 'static> KeyProvider<<T::Chain as Chain>::ChainCrypto> for Pallet<T, I> {
fn active_epoch_key(
) -> Option<EpochKey<<<T::Chain as Chain>::ChainCrypto as ChainCrypto>::AggKey>> {
CurrentVaultEpoch::<T, I>::get().map(|current_vault_epoch_and_state| {
CurrentVaultEpoch::<T, I>::get().map(|current_vault_epoch| {
EpochKey {
key: Vaults::<T, I>::get(current_vault_epoch_and_state.epoch_index)
.expect("Key must exist if CurrentVaultEpoch exists since they get set at the same place: set_next_vault()").public_key,
epoch_index: current_vault_epoch_and_state.epoch_index,
key: Vaults::<T, I>::get(current_vault_epoch)
.expect("Key must exist if CurrentVaultEpoch exists since they get set at the same place: set_vault_key_for_epoch()").public_key,
epoch_index: current_vault_epoch,
}
})
}
Expand Down
6 changes: 3 additions & 3 deletions state-chain/pallets/cf-vaults/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,15 @@ impl Broadcaster<MockEthereum> for MockBroadcaster {
fn threshold_sign_and_broadcast(
_api_call: Self::ApiCall,
_pause_broadcasts: bool,
) -> (BroadcastId, ThresholdSignatureRequestId) {
) -> BroadcastId {
Self::send_broadcast();
(1, 2)
1
}

fn threshold_sign_and_broadcast_with_callback(
_api_call: Self::ApiCall,
_callback: Self::Callback,
) -> (BroadcastId, ThresholdSignatureRequestId) {
) -> BroadcastId {
unimplemented!()
}
}
Expand Down
6 changes: 0 additions & 6 deletions state-chain/pallets/cf-vaults/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,12 +643,6 @@ fn do_full_key_rotation() {
VaultsPallet::activate();

assert!(!KeygenResolutionPendingSince::<Test, _>::exists());
// assert_eq!(<VaultsPallet as VaultRotator>::status(), AsyncResult::Pending);

// assert!(matches!(
// PendingVaultRotation::<Test, _>::get().unwrap(),
// VaultRotationStatus::<Test, _>::AwaitingActivation { new_public_key: k } if k ==
// NEW_AGG_PUB_KEY_POST_HANDOVER ));

// Voting has been cleared.
assert_eq!(KeygenSuccessVoters::<Test, _>::iter_keys().next(), None);
Expand Down
2 changes: 0 additions & 2 deletions state-chain/pallets/cf-vaults/src/vault_rotator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,6 @@ impl<T: Config<I>, I: 'static> VaultRotator for Pallet<T, I> {
Ok(activation_call) => {
T::Broadcaster::threshold_sign_and_broadcast(activation_call, true);

// Optimistic activation means we don't need to wait for the activation
// transaction to succeed before using the new key.
Self::activate_new_key(
new_public_key,
T::ChainTracking::get_block_height(),
Expand Down
Loading

0 comments on commit fe789b9

Please sign in to comment.