Skip to content

Commit

Permalink
Merge branch 'main' into fork_configs
Browse files Browse the repository at this point in the history
  • Loading branch information
ralexstokes authored Mar 26, 2024
2 parents 0dcf076 + d8e3395 commit f99d1eb
Show file tree
Hide file tree
Showing 1,568 changed files with 3,187 additions and 217,220 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[workspace]
resolver = "2"

members = ["beacon-api-client", "ethereum-consensus", "test-gen", "spec-gen"]
default-members = ["ethereum-consensus"]
members = ["beacon-api-client", "ethereum-consensus", "spec-gen", "spec-tests"]
default-members = ["ethereum-consensus", "beacon-api-client"]

[workspace.dependencies]
tokio = { version = "1.18.2", features = ["full"] }
Expand Down
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ethereum-consensus

A library for interacting with ethereum consensus objects.
A library for interacting with ethereum consensus data.

# 🚧 WARNING 🚧

Expand All @@ -27,7 +27,7 @@ The generic types are exposed but most users will want to access each fork's log
[Examples](#examples) for further details.

An important thing to note is that the `state_transition` module of each fork (after the `phase0` fork) is generated
by a code-generation utility in this crate called `gen-spec`. This utility specializes each fork based on the prior
by a code-generation utility in the `spec-gen` crate. This utility specializes each fork based on the prior
Rust module as an input. See the README for that binary to learn further details about operating this utility. The
generated files are checked in so you should not need to use this binary under
most circumstances.
Expand Down Expand Up @@ -57,14 +57,10 @@ A client for the Ethereum beacon node APIs:

https://ethereum.github.io/beacon-APIs

### Example
### Examples

Refer to the code in `examples` for a demonstration on how to use the API client.

## `test-gen`

This crate generates spec test following https://github.com/ethereum/consensus-spec-tests. It is not user-facing.

## `spec-gen`

This crate generates spec code following the "fork diff" defined in each fork of http://github.com/ethereum/consensus-specs. It is not user-facing.
9 changes: 3 additions & 6 deletions ethereum-consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ license = "MIT OR Apache-2.0"
default = ["serde", "async"]
serde = ["hex", "serde_json", "serde_yaml"]
async = ["tokio", "tokio-stream", "async-stream"]
spec-tests = ["serde", "serde_yaml", "secret-key-debug"]
# enable if you want to be able to print `crypto::SecretKey`
secret-key-debug = []
secret-key-debug = [
] # enable if you want to be able to print `crypto::SecretKey`
spec-tests = [] # enable extra features for testing
ec = [
"secret-key-debug",
"clap",
Expand Down Expand Up @@ -66,11 +66,8 @@ unicode-normalization = { workspace = true, optional = true }
bitvec = { workspace = true, optional = true }

[dev-dependencies]
serde_with = "3.7.0"
snap = "1"
toml = "0.8.2"
reth-primitives = { git = "https://github.com/paradigmxyz/reth", rev = "28f3a2e2d9525bf2f6373e755e2d6dc0c2f97821" }
alloy-rlp = "0.3.4"

[[bin]]
name = "ec"
Expand Down
10 changes: 2 additions & 8 deletions ethereum-consensus/examples/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,7 @@ fn main() {
let current_epoch = bellatrix::get_current_epoch(&state, &context);
dbg!(current_epoch);

let execution_engine = bellatrix::DefaultExecutionEngine::default();
let _ = bellatrix::state_transition(
&mut state,
&mut signed_block,
&execution_engine,
Validation::Enabled,
&context,
);
let _ =
bellatrix::state_transition(&mut state, &mut signed_block, Validation::Enabled, &context);
dbg!(state.fork);
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
use ethereum_consensus::{
altair::mainnet as altair,
bellatrix::mainnet as bellatrix,
phase0::mainnet as phase0,
ssz::prelude::*,
state_transition::mainnet::{Context, ExecutionEngine, Executor},
state_transition::mainnet::{Context, Executor},
types::mainnet::{BeaconState, SignedBeaconBlock},
};
use std::error::Error;

fn main() -> std::result::Result<(), Box<dyn Error>> {
println!("this example is for illustration purposes...");
println!("to get to the end, we need utilities to make correct blocks with respect to the state transition");
println!("this example illustrates how to use the codebase when applying state transitions");
println!("note: the code currently does not run to the end as the input data is not correct");

let genesis_state = phase0::BeaconState::default();
let genesis_state = BeaconState::Phase0(Default::default());
let context = Context::for_mainnet();
let execution_engine = ExecutionEngine::Bellatrix(bellatrix::DefaultExecutionEngine::default());
let mut executor = Executor::new(genesis_state.into(), execution_engine, context);
let mut executor = Executor::new(genesis_state, context);

let mut block = phase0::SignedBeaconBlock::default();
block.message.slot = 1;
executor.apply_block(&mut block.into())?;
let mut block = SignedBeaconBlock::Phase0(Default::default());
*block.message_mut().slot_mut() = 1;
executor.apply_block(&mut block)?;

let mut block = altair::SignedBeaconBlock::default();
block.message.slot = executor.context.altair_fork_epoch * executor.context.slots_per_epoch;
executor.apply_block(&mut block.into())?;
let mut block = SignedBeaconBlock::Altair(Default::default());
*block.message_mut().slot_mut() =
executor.context.altair_fork_epoch * executor.context.slots_per_epoch;
executor.apply_block(&mut block)?;

let mut block = bellatrix::SignedBeaconBlock::default();
block.message.slot = executor.context.bellatrix_fork_epoch * executor.context.slots_per_epoch;
executor.apply_block(&mut block.into())?;
let mut block = SignedBeaconBlock::Bellatrix(Default::default());
*block.message_mut().slot_mut() =
executor.context.bellatrix_fork_epoch * executor.context.slots_per_epoch;
executor.apply_block(&mut block)?;

let mut state = executor.state.bellatrix().unwrap();
let state = executor.state.bellatrix_mut().unwrap();
let state_root = state.hash_tree_root()?;
dbg!(state_root);
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion ethereum-consensus/src/altair/epoch_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ pub fn process_epoch<
process_justification_and_finalization(state, context)?;
process_inactivity_updates(state, context)?;
process_rewards_and_penalties(state, context)?;
process_registry_updates(state, context);
process_registry_updates(state, context)?;
process_slashings(state, context)?;
process_eth1_data_reset(state, context);
process_effective_balance_updates(state, context);
Expand Down
2 changes: 1 addition & 1 deletion ethereum-consensus/src/altair/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ pub fn slash_validator<
context: &Context,
) -> Result<()> {
let epoch = get_current_epoch(state, context);
initiate_validator_exit(state, slashed_index, context);
initiate_validator_exit(state, slashed_index, context)?;
state.validators[slashed_index].slashed = true;
state.validators[slashed_index].withdrawable_epoch = u64::max(
state.validators[slashed_index].withdrawable_epoch,
Expand Down
Loading

0 comments on commit f99d1eb

Please sign in to comment.