Skip to content

Commit e3c8196

Browse files
Merge pull request #1243 from mintlayer/fix/checked_ops_uint
Use only checked math ops for Uint* types
2 parents 12e78e4 + 46ed67f commit e3c8196

File tree

12 files changed

+132
-109
lines changed

12 files changed

+132
-109
lines changed

chainstate/src/detail/block_invalidation/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,11 @@ impl<'a, S: BlockchainStorage, V: TransactionVerificationStrategy> BlockInvalida
175175
let cur_best_block_index = get_best_block_index(&chainstate_ref)?;
176176
let cur_best_chain_trust = cur_best_block_index.chain_trust();
177177

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

181184
(cur_best_chain_trust, best_chain_candidates)
182185
};

chainstate/src/detail/chainstateref/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,8 @@ impl<'a, S: BlockchainStorageRead, V: TransactionVerificationStrategy> Chainstat
974974

975975
// Set Chain Trust
976976
let prev_block_chaintrust: Uint256 = prev_block_index.chain_trust();
977-
let chain_trust = prev_block_chaintrust + current_block_proof;
977+
let chain_trust = (prev_block_chaintrust + current_block_proof)
978+
.expect("Chain trust growth is locally controlled. This can't happen.");
978979
let block_index = BlockIndex::new(
979980
block,
980981
chain_trust,

chainstate/tx-verifier/src/transaction_verifier/reward_distribution.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ fn calculate_rewards_per_delegation(
191191
.map(
192192
|(delegation_id, balance_amount)| -> Result<_, ConnectTransactionError> {
193193
let balance = Uint256::from_amount(*balance_amount);
194-
let reward = (total_delegations_reward * balance) / total_delegations_balance;
194+
let numer = (total_delegations_reward * balance).expect("Source types are smaller");
195+
let reward = (numer / total_delegations_balance)
196+
.ok_or(ConnectTransactionError::TotalDelegationBalanceZero(pool_id))?;
195197
let reward: common::primitives::amount::UnsignedIntType =
196198
reward.try_into().map_err(|_| {
197199
ConnectTransactionError::DelegationRewardOverflow(

common/src/chain/block/consensus_data.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ impl PoWData {
145145
let target: Uint256 = self.bits.try_into().ok()?;
146146
let mut ret = !target;
147147
let mut ret1 = target;
148-
ret1.increment();
149-
ret = ret / ret1;
150-
ret.increment();
148+
ret1 = (ret1 + Uint256::ONE)?;
149+
ret = (ret / ret1)?;
150+
ret = (ret + Uint256::ONE).unwrap_or(Uint256::MAX);
151151
Some(ret)
152152
}
153153
}

common/src/chain/chaintrust/asymptote.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ pub fn calculate_block_proof(timestamp_diff: u64) -> Uint256 {
9696
let block_weight = Uint256::from(get_weight_for_block());
9797
let empty_time_slots_weight = Uint256::from(empty_time_slots_weight);
9898

99-
block_weight - empty_time_slots_weight
99+
block_weight
100+
.checked_sub(&empty_time_slots_weight)
101+
.expect("Checked above; cannot fail")
100102
}
101103

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

176178
// There should not be any overflow to ensure that the chain trust is always less than the maximum possible value.
177179
assert!(max_block_height < max_chain_trust);

common/src/chain/pos.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ const DEFAULT_MATURITY_DISTANCE: BlockDistance = BlockDistance::new(2000);
162162

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

167168
PoSChainConfig {
168169
target_limit,
@@ -189,7 +190,8 @@ pub fn create_unittest_pos_config() -> PoSChainConfig {
189190

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

194196
PoSChainConfig {
195197
target_limit,

common/src/chain/pow.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,9 @@ mod tests {
189189
0xFFFFFFFFFFFFFFFF,
190190
]);
191191

192-
let target_max =
193-
target_max / Uint256::from_u64(mainnet_cfg.target_timespan().as_secs() * 4);
192+
let target_max = (target_max
193+
/ Uint256::from_u64(mainnet_cfg.target_timespan().as_secs() * 4))
194+
.unwrap();
194195
assert!(mainnet_cfg.limit() < target_max);
195196
}
196197
}

0 commit comments

Comments
 (0)