Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use only checked math ops for Uint* types #1243

Merged
merged 6 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions chainstate/src/detail/block_invalidation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,11 @@ impl<'a, S: BlockchainStorage, V: TransactionVerificationStrategy> BlockInvalida
let cur_best_block_index = get_best_block_index(&chainstate_ref)?;
let cur_best_chain_trust = cur_best_block_index.chain_trust();

let best_chain_candidates =
BestChainCandidates::new(&chainstate_ref, cur_best_chain_trust + Uint256::ONE)?;
let best_chain_candidates = BestChainCandidates::new(
&chainstate_ref,
(cur_best_chain_trust + Uint256::ONE)
.expect("Chain trust won't be saturated in a very long time"),
)?;

(cur_best_chain_trust, best_chain_candidates)
};
Expand Down
3 changes: 2 additions & 1 deletion chainstate/src/detail/chainstateref/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,8 @@ impl<'a, S: BlockchainStorageRead, V: TransactionVerificationStrategy> Chainstat

// Set Chain Trust
let prev_block_chaintrust: Uint256 = prev_block_index.chain_trust();
let chain_trust = prev_block_chaintrust + current_block_proof;
let chain_trust = (prev_block_chaintrust + current_block_proof)
.expect("Chain trust growth is locally controlled. This can't happen.");
let block_index = BlockIndex::new(
block,
chain_trust,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ fn calculate_rewards_per_delegation(
.map(
|(delegation_id, balance_amount)| -> Result<_, ConnectTransactionError> {
let balance = Uint256::from_amount(*balance_amount);
let reward = (total_delegations_reward * balance) / total_delegations_balance;
let numer = (total_delegations_reward * balance).expect("Source types are smaller");
let reward = (numer / total_delegations_balance)
.ok_or(ConnectTransactionError::TotalDelegationBalanceZero(pool_id))?;
let reward: common::primitives::amount::UnsignedIntType =
reward.try_into().map_err(|_| {
ConnectTransactionError::DelegationRewardOverflow(
Expand Down
6 changes: 3 additions & 3 deletions common/src/chain/block/consensus_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ impl PoWData {
let target: Uint256 = self.bits.try_into().ok()?;
let mut ret = !target;
let mut ret1 = target;
ret1.increment();
ret = ret / ret1;
ret.increment();
ret1 = (ret1 + Uint256::ONE)?;
ret = (ret / ret1)?;
ret = (ret + Uint256::ONE).unwrap_or(Uint256::MAX);
Some(ret)
}
}
6 changes: 4 additions & 2 deletions common/src/chain/chaintrust/asymptote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ pub fn calculate_block_proof(timestamp_diff: u64) -> Uint256 {
let block_weight = Uint256::from(get_weight_for_block());
let empty_time_slots_weight = Uint256::from(empty_time_slots_weight);

block_weight - empty_time_slots_weight
block_weight
.checked_sub(&empty_time_slots_weight)
.expect("Checked above; cannot fail")
}

#[cfg(test)]
Expand Down Expand Up @@ -171,7 +173,7 @@ mod tests {
// Given that the maximum block weight is 1*SCALING_FACTOR,
// and it only goes down when there are empty time-slots in between,
// the maximum chain trust is the following:
let max_chain_trust = max_block_height * single_block_weight;
let max_chain_trust = (max_block_height * single_block_weight).unwrap();

// There should not be any overflow to ensure that the chain trust is always less than the maximum possible value.
assert!(max_block_height < max_chain_trust);
Expand Down
6 changes: 4 additions & 2 deletions common/src/chain/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ const DEFAULT_MATURITY_DISTANCE: BlockDistance = BlockDistance::new(2000);

pub fn create_testnet_pos_config(consensus_version: PoSConsensusVersion) -> PoSChainConfig {
let target_block_time = NonZeroU64::new(2 * 60).expect("cannot be 0");
let target_limit = Uint256::MAX / Uint256::from_u64(target_block_time.get());
let target_limit = (Uint256::MAX / Uint256::from_u64(target_block_time.get()))
.expect("Target block time cannot be zero as per NonZeroU64");

PoSChainConfig {
target_limit,
Expand All @@ -189,7 +190,8 @@ pub fn create_unittest_pos_config() -> PoSChainConfig {

pub fn create_regtest_pos_config(consensus_version: PoSConsensusVersion) -> PoSChainConfig {
let target_block_time = NonZeroU64::new(2 * 60).expect("cannot be 0");
let target_limit = Uint256::MAX / Uint256::from_u64(target_block_time.get());
let target_limit = (Uint256::MAX / Uint256::from_u64(target_block_time.get()))
.expect("Target block time cannot be zero as per NonZeroU64");

PoSChainConfig {
target_limit,
Expand Down
5 changes: 3 additions & 2 deletions common/src/chain/pow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,9 @@ mod tests {
0xFFFFFFFFFFFFFFFF,
]);

let target_max =
target_max / Uint256::from_u64(mainnet_cfg.target_timespan().as_secs() * 4);
let target_max = (target_max
/ Uint256::from_u64(mainnet_cfg.target_timespan().as_secs() * 4))
.unwrap();
assert!(mainnet_cfg.limit() < target_max);
}
}
Expand Down
Loading
Loading