Skip to content

Commit 3e4dc7a

Browse files
committed
Use checked arithmetic in types and state proc
1 parent 065ea15 commit 3e4dc7a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+525
-169
lines changed

.github/workflows/test-suite.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,10 @@ jobs:
6565
- uses: actions/checkout@v1
6666
- name: Typecheck benchmark code without running it
6767
run: make check-benches
68+
clippy:
69+
runs-on: ubuntu-latest
70+
needs: cargo-fmt
71+
steps:
72+
- uses: actions/checkout@v1
73+
- name: Lint code for quality and style with Clippy
74+
run: make lint

Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ members = [
1717
"eth2/utils/lighthouse_bootstrap",
1818
"eth2/utils/merkle_proof",
1919
"eth2/utils/int_to_bytes",
20+
"eth2/utils/safe_arith",
2021
"eth2/utils/serde_hex",
2122
"eth2/utils/slot_clock",
2223
"eth2/utils/ssz",

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ test: test-release
4141
# Runs the entire test suite, downloading test vectors if required.
4242
test-full: cargo-fmt test-release test-debug test-ef
4343

44+
# Lints the code for bad style and potentially unsafe arithmetic using Clippy.
45+
# Clippy lints are opt-in per-crate for now, which is why we allow all by default.
46+
lint:
47+
cargo clippy --all -- -A clippy::all
48+
4449
# Runs the makefile in the `ef_tests` repo.
4550
#
4651
# May download and extract an archive of test vectors from the ethereum

beacon_node/beacon_chain/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ rand = "0.7.2"
4343
proto_array_fork_choice = { path = "../../eth2/proto_array_fork_choice" }
4444
lru = "0.4.3"
4545
tempfile = "3.1.0"
46+
safe_arith = { path = "../../eth2/utils/safe_arith" }
4647

4748
[dev-dependencies]
4849
lazy_static = "1.4.0"

beacon_node/beacon_chain/src/eth1_chain.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ pub enum Error {
4545
///
4646
/// The eth1 caches are stale, or a junk value was voted into the chain.
4747
UnknownPreviousEth1BlockHash,
48+
/// An arithmetic error occurred.
49+
ArithError(safe_arith::ArithError),
50+
}
51+
52+
impl From<safe_arith::ArithError> for Error {
53+
fn from(e: safe_arith::ArithError) -> Self {
54+
Self::ArithError(e)
55+
}
4856
}
4957

5058
#[derive(Encode, Decode, Clone)]
@@ -369,7 +377,7 @@ impl<T: EthSpec, S: Store<T>> Eth1ChainBackend<T, S> for CachingEth1Backend<T, S
369377
_spec: &ChainSpec,
370378
) -> Result<Vec<Deposit>, Error> {
371379
let deposit_index = state.eth1_deposit_index;
372-
let deposit_count = if let Some(new_eth1_data) = get_new_eth1_data(state, eth1_data_vote) {
380+
let deposit_count = if let Some(new_eth1_data) = get_new_eth1_data(state, eth1_data_vote)? {
373381
new_eth1_data.deposit_count
374382
} else {
375383
state.eth1_data.deposit_count

beacon_node/genesis/src/eth1_genesis_service.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,8 @@ impl Eth1GenesisService {
383383
.map_err(|e| format!("Error whilst processing deposit: {:?}", e))
384384
})?;
385385

386-
process_activations(&mut local_state, spec);
386+
process_activations(&mut local_state, spec)
387+
.map_err(|e| format!("Error whilst processing activations: {:?}", e))?;
387388
let is_valid = is_valid_genesis_state(&local_state, spec);
388389

389390
trace!(

eth2/state_processing/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ eth2_ssz = "0.1.2"
2727
eth2_ssz_types = { path = "../utils/ssz_types" }
2828
merkle_proof = { path = "../utils/merkle_proof" }
2929
log = "0.4.8"
30+
safe_arith = { path = "../utils/safe_arith" }
3031
tree_hash = "0.1.0"
3132
tree_hash_derive = "0.2"
3233
types = { path = "../types" }

eth2/state_processing/src/common/deposit_data_tree.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use eth2_hashing::hash;
22
use int_to_bytes::int_to_bytes32;
33
use merkle_proof::{MerkleTree, MerkleTreeError};
4+
use safe_arith::SafeArith;
45
use types::Hash256;
56

67
/// Emulates the eth1 deposit contract merkle tree.
@@ -46,7 +47,7 @@ impl DepositDataTree {
4647
/// Add a deposit to the merkle tree.
4748
pub fn push_leaf(&mut self, leaf: Hash256) -> Result<(), MerkleTreeError> {
4849
self.tree.push_leaf(leaf, self.depth)?;
49-
self.mix_in_length += 1;
50+
self.mix_in_length.increment()?;
5051
Ok(())
5152
}
5253
}

eth2/state_processing/src/common/get_base_reward.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use integer_sqrt::IntegerSquareRoot;
2+
use safe_arith::SafeArith;
23
use types::*;
34

45
/// Returns the base reward for some validator.
@@ -14,10 +15,10 @@ pub fn get_base_reward<T: EthSpec>(
1415
if total_active_balance == 0 {
1516
Ok(0)
1617
} else {
17-
Ok(
18-
state.get_effective_balance(index, spec)? * spec.base_reward_factor
19-
/ total_active_balance.integer_sqrt()
20-
/ spec.base_rewards_per_epoch,
21-
)
18+
Ok(state
19+
.get_effective_balance(index, spec)?
20+
.safe_mul(spec.base_reward_factor)?
21+
.safe_div(total_active_balance.integer_sqrt())?
22+
.safe_div(spec.base_rewards_per_epoch)?)
2223
}
2324
}

0 commit comments

Comments
 (0)