Skip to content

Commit

Permalink
chore(clippy): make clippy happy
Browse files Browse the repository at this point in the history
  • Loading branch information
drcpu-github committed Nov 3, 2024
1 parent 168e760 commit 7b5bed1
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 64 deletions.
12 changes: 5 additions & 7 deletions data_structures/src/staking/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,11 @@ where
) -> StakesResult<Coins, Address, Coins, Epoch> {
let coins_after = self.coins.sub(coins);

if coins_after > Coins::zero() {
if coins_after < minimum_stakeable {
Err(StakesError::AmountIsBelowMinimum {
amount: coins_after,
minimum: minimum_stakeable,
})?;
}
if coins_after > Coins::zero() && coins_after < minimum_stakeable {
Err(StakesError::AmountIsBelowMinimum {
amount: coins_after,
minimum: minimum_stakeable,
})?;
}

self.coins = coins_after;
Expand Down
1 change: 0 additions & 1 deletion node/src/actors/chain_manager/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ impl Handler<EpochNotification<EveryEpochPayload>> for ChainManager {
let rank_subset: Vec<_> = stakes
.rank(Capability::Mining, previous_epoch)
.take(replication_factor.into())
.map(|sk| sk)
.collect();
for (i, (stake_key, _)) in rank_subset.into_iter().enumerate() {
log::warn!(
Expand Down
45 changes: 22 additions & 23 deletions node/src/actors/chain_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1033,30 +1033,29 @@ impl ChainManager {
let superblock_period = chain_info.consensus_constants.superblock_period;
if get_protocol_version(Some(block_epoch)) == ProtocolVersion::V1_8
&& get_protocol_version_activation_epoch(ProtocolVersion::V2_0) == Epoch::MAX
&& block_epoch % u32::from(superblock_period) == 0
{
if block_epoch % u32::from(superblock_period) == 0 {
let min_total_stake = self
.consensus_constants_wit2
.get_wit2_minimum_total_stake_nanowits();
let activation_delay = self
.consensus_constants_wit2
.get_wit2_activation_delay_epochs();
if stakes.total_staked() >= Wit::from(min_total_stake) {
register_protocol_version(
ProtocolVersion::V2_0,
block_epoch + activation_delay,
20,
);
if let Some(epoch_constants) = &mut self.epoch_constants {
match epoch_constants
.set_values_for_wit2(20, block_epoch + activation_delay)
{
Ok(_) => (),
Err(_) => panic!("Could not set wit/2 checkpoint variables"),
};
} else {
panic!("Could not set wit/2 checkpoint variables");
}
let min_total_stake = self
.consensus_constants_wit2
.get_wit2_minimum_total_stake_nanowits();
let activation_delay = self
.consensus_constants_wit2
.get_wit2_activation_delay_epochs();
if stakes.total_staked() >= Wit::from(min_total_stake) {
register_protocol_version(
ProtocolVersion::V2_0,
block_epoch + activation_delay,
20,
);
if let Some(epoch_constants) = &mut self.epoch_constants {
match epoch_constants
.set_values_for_wit2(20, block_epoch + activation_delay)
{
Ok(_) => (),
Err(_) => panic!("Could not set wit/2 checkpoint variables"),
};
} else {
panic!("Could not set wit/2 checkpoint variables");
}
}
}
Expand Down
38 changes: 20 additions & 18 deletions validations/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1251,9 +1251,10 @@ fn vtt_timelock() {
};

// (epoch, time_lock, should_be_accepted_into_block)
#[allow(clippy::cast_sign_loss)]
let czt = epoch_constants.checkpoint_zero_timestamp as u64;
let tests = vec![
(0, czt + 0, true),
(0, czt, true),
(0, czt + 1, false),
(0, czt + 1_000_000, false),
(999, czt + 1_000_000, false),
Expand Down Expand Up @@ -4134,13 +4135,14 @@ fn commitment_timelock() {
verify_signatures_test(signatures_to_verify)
};

#[allow(clippy::cast_sign_loss)]
let czt = epoch_constants.checkpoint_zero_timestamp as u64;
let first_timestamp =
czt + u64::from(FIRST_HARD_FORK) * u64::from(epoch_constants.checkpoints_period);

// (epoch, time_lock, should_be_accepted_into_block)
let tests = vec![
(0, czt + 0, true),
(0, czt, true),
(0, czt + 1, false),
(0, czt + 1_000_000, false),
(999, czt + 1_000_000, false),
Expand Down Expand Up @@ -8925,7 +8927,7 @@ fn st_no_inputs() {
let x = validate_stake_transaction(
&st_tx,
&utxo_diff,
Epoch::from(10000 as u32),
Epoch::from(10000_u32),
EpochConstants::default(),
&mut vec![],
&stakes,
Expand Down Expand Up @@ -8966,7 +8968,7 @@ fn st_one_input_but_no_signature() {
let x = validate_stake_transaction(
&stake_tx,
&utxo_diff,
Epoch::from(10000 as u32),
Epoch::from(10000_u32),
EpochConstants::default(),
&mut signatures_to_verify,
&stakes,
Expand Down Expand Up @@ -9008,7 +9010,7 @@ fn st_below_min_stake() {
let x = validate_stake_transaction(
&stake_tx,
&utxo_diff,
Epoch::from(10000 as u32),
Epoch::from(10000_u32),
EpochConstants::default(),
&mut signatures_to_verify,
&stakes,
Expand Down Expand Up @@ -9052,7 +9054,7 @@ fn st_above_max_stake() {
let x = validate_stake_transaction(
&stake_tx,
&utxo_diff,
Epoch::from(10000 as u32),
Epoch::from(10000_u32),
EpochConstants::default(),
&mut signatures_to_verify,
&stakes,
Expand All @@ -9079,7 +9081,7 @@ fn st_above_max_stake() {
let x = validate_stake_transaction(
&stake_tx,
&utxo_diff,
Epoch::from(10000 as u32),
Epoch::from(10000_u32),
EpochConstants::default(),
&mut signatures_to_verify,
&stakes,
Expand Down Expand Up @@ -9112,7 +9114,7 @@ fn unstake_success() {
let unstake_tx = UnstakeTransaction::new(unstake_tx_body, signature);
let x = validate_unstake_transaction(
&unstake_tx,
Epoch::from(20001 as u32),
Epoch::from(20001_u32),
&stakes,
MIN_STAKE_NANOWITS,
UNSTAKING_DELAY_SECONDS,
Expand All @@ -9135,7 +9137,7 @@ fn unstake_success() {
let unstake_tx = UnstakeTransaction::new(unstake_tx_body, signature);
let x = validate_unstake_transaction(
&unstake_tx,
Epoch::from(20001 as u32),
Epoch::from(20001_u32),
&stakes,
MIN_STAKE_NANOWITS,
UNSTAKING_DELAY_SECONDS,
Expand All @@ -9155,7 +9157,7 @@ fn unstake_success() {
let unstake_tx = UnstakeTransaction::new(unstake_tx_body, signature);
let x = validate_unstake_transaction(
&unstake_tx,
Epoch::from(20001 as u32),
Epoch::from(20001_u32),
&stakes,
MIN_STAKE_NANOWITS,
UNSTAKING_DELAY_SECONDS,
Expand Down Expand Up @@ -9204,7 +9206,7 @@ fn unstake_more_than_staked() {
let unstake_tx = UnstakeTransaction::new(unstake_tx_body, signature);
let x = validate_unstake_transaction(
&unstake_tx,
Epoch::from(20001 as u32),
Epoch::from(20001_u32),
&stakes,
MIN_STAKE_NANOWITS,
UNSTAKING_DELAY_SECONDS,
Expand All @@ -9228,7 +9230,7 @@ fn unstake_more_than_staked() {
let unstake_tx = UnstakeTransaction::new(unstake_tx_body, signature);
let x = validate_unstake_transaction(
&unstake_tx,
Epoch::from(20001 as u32),
Epoch::from(20001_u32),
&stakes,
MIN_STAKE_NANOWITS,
UNSTAKING_DELAY_SECONDS,
Expand Down Expand Up @@ -9257,7 +9259,7 @@ fn unstake_invalid_nonce() {
let unstake_tx = UnstakeTransaction::new(unstake_tx_body, signature);
let x = validate_unstake_transaction(
&unstake_tx,
Epoch::from(20001 as u32),
Epoch::from(20001_u32),
&stakes,
MIN_STAKE_NANOWITS,
UNSTAKING_DELAY_SECONDS,
Expand Down Expand Up @@ -9287,7 +9289,7 @@ fn unstake_wrong_withdrawer() {
let unstake_tx = UnstakeTransaction::new(unstake_tx_body, signature);
let x = validate_unstake_transaction(
&unstake_tx,
Epoch::from(20001 as u32),
Epoch::from(20001_u32),
&stakes,
MIN_STAKE_NANOWITS,
UNSTAKING_DELAY_SECONDS,
Expand Down Expand Up @@ -9316,7 +9318,7 @@ fn unstake_below_stake_minimum() {
let unstake_tx = UnstakeTransaction::new(unstake_tx_body, signature);
let x = validate_unstake_transaction(
&unstake_tx,
Epoch::from(20001 as u32),
Epoch::from(20001_u32),
&stakes,
MIN_STAKE_NANOWITS,
UNSTAKING_DELAY_SECONDS,
Expand Down Expand Up @@ -9345,7 +9347,7 @@ fn unstake_timelock() {
let unstake_tx = UnstakeTransaction::new(unstake_tx_body, signature);
let x = validate_unstake_transaction(
&unstake_tx,
Epoch::from(20001 as u32),
Epoch::from(20001_u32),
&stakes,
MIN_STAKE_NANOWITS,
UNSTAKING_DELAY_SECONDS,
Expand Down Expand Up @@ -9374,7 +9376,7 @@ fn unstake_signature() {
let mut unstake_tx = UnstakeTransaction::new(unstake_tx_body, signature);
let x = validate_unstake_transaction(
&unstake_tx,
Epoch::from(20001 as u32),
Epoch::from(20001_u32),
&stakes,
MIN_STAKE_NANOWITS,
UNSTAKING_DELAY_SECONDS,
Expand All @@ -9384,7 +9386,7 @@ fn unstake_signature() {
unstake_tx.body.withdrawal.value = 999;
let x = validate_unstake_transaction(
&unstake_tx,
Epoch::from(20001 as u32),
Epoch::from(20001_u32),
&stakes,
MIN_STAKE_NANOWITS,
UNSTAKING_DELAY_SECONDS,
Expand Down
24 changes: 9 additions & 15 deletions validations/src/validations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1317,6 +1317,7 @@ pub type ValidatedStakeTransaction<'a> = (
);

/// Function to validate a stake transaction.
#[allow(clippy::too_many_arguments)]
pub fn validate_stake_transaction<'a>(
st_tx: &'a StakeTransaction,
utxo_diff: &UtxoDiff<'_>,
Expand Down Expand Up @@ -1449,11 +1450,7 @@ pub fn validate_unstake_transaction<'a>(
}

// TODO: modify this to enable delegated staking with multiple withdrawer addresses on a single validator
let nonce = stake_entry
.first()
.map(|stake| stake.value.nonce)
.unwrap()
.into();
let nonce = stake_entry.first().map(|stake| stake.value.nonce).unwrap();
if ut_tx.body.nonce != nonce {
return Err(TransactionError::UnstakeInvalidNonce {
used: ut_tx.body.nonce,
Expand Down Expand Up @@ -1540,11 +1537,11 @@ pub fn validate_unstake_signature(
let signature = ut_tx.signature.signature.clone().try_into().map_err(fte)?;
let public_key = ut_tx.signature.public_key.clone().try_into().map_err(fte)?;

verify(&public_key, &message.to_vec(), &signature).map_err(|e| {
verify(&public_key, message.as_ref(), &signature).map_err(|e| {
TransactionError::VerifyTransactionSignatureFail {
hash: {
let mut sha256 = [0; 32];
sha256.copy_from_slice(&message.to_vec());
sha256.copy_from_slice(message.as_ref());
Hash::SHA256(sha256)
},
msg: e.to_string(),
Expand Down Expand Up @@ -1834,6 +1831,7 @@ pub fn update_utxo_diff<'a, IterInputs, IterOutputs>(
None
};

#[allow(clippy::cast_sign_loss)]
let output_to_insert = if get_protocol_version(Some(epoch)) >= ProtocolVersion::V2_0 {
if output.time_lock < checkpoint_zero_timestamp.try_into().unwrap() {
ValueTransferOutput {
Expand Down Expand Up @@ -1888,16 +1886,12 @@ pub fn validate_block_transactions(
let mut genesis_value_available = max_total_value_genesis;

// Check stake transactions are added in V1_8 at the earliest
if protocol_version == ProtocolVersion::V1_7 {
if block.txns.stake_txns.len() > 0 {
return Err(TransactionError::NoStakeTransactionsAllowed.into());
}
if protocol_version == ProtocolVersion::V1_7 && !block.txns.stake_txns.is_empty() {
return Err(TransactionError::NoStakeTransactionsAllowed.into());
}
// Check stake transactions are added in V2_0 at the earliest
if protocol_version <= ProtocolVersion::V1_8 {
if block.txns.unstake_txns.len() > 0 {
return Err(TransactionError::NoUnstakeTransactionsAllowed.into());
}
if protocol_version <= ProtocolVersion::V1_8 && !block.txns.unstake_txns.is_empty() {
return Err(TransactionError::NoUnstakeTransactionsAllowed.into());
}

// TODO: replace for loop with a try_fold
Expand Down

0 comments on commit 7b5bed1

Please sign in to comment.