diff --git a/crates/net/rpc/src/beacon/ptc.rs b/crates/net/rpc/src/beacon/ptc.rs index be80bf330..1a00631e4 100644 --- a/crates/net/rpc/src/beacon/ptc.rs +++ b/crates/net/rpc/src/beacon/ptc.rs @@ -533,7 +533,7 @@ fn aggregate_pool( #[cfg(test)] mod tests { use super::*; - use crate::test_utils::{RecordingNetwork, gloas_beacon_block}; + use crate::test_utils::{RecordingNetwork, gloas_beacon_block, slot_root}; use axum::{body::Body, http::Request}; use ethlambda_state_transition::beacon::{ constants::DOMAIN_PTC_ATTESTER, @@ -571,7 +571,7 @@ mod tests { // Distinct roots per slot, so a `dependent_root` read from the wrong // slot shows up. for slot in 0..inner.block_roots.len() { - inner.block_roots[slot] = H256::repeat_byte(slot as u8 + 1); + inner.block_roots[slot] = slot_root(slot as u64); } for index in 0..preset::PTC_WINDOW_LENGTH { inner.ptc_window[index] = vec![index as u64; preset::PTC_SIZE] @@ -739,7 +739,7 @@ mod tests { let expected = if dependent_slot >= head_slot { head_root } else { - H256::repeat_byte(dependent_slot as u8 + 1) + slot_root(dependent_slot) }; assert_eq!(json["dependent_root"], format!("{expected:?}")); } diff --git a/crates/net/rpc/src/beacon/validator.rs b/crates/net/rpc/src/beacon/validator.rs index ee2f0bcde..043f3bd2a 100644 --- a/crates/net/rpc/src/beacon/validator.rs +++ b/crates/net/rpc/src/beacon/validator.rs @@ -866,7 +866,7 @@ fn payload_present_index( #[cfg(test)] mod tests { use super::*; - use crate::test_utils::{beacon_store_at, idle_engine}; + use crate::test_utils::{beacon_store_at, idle_engine, slot_root}; use axum::{ body::Body, http::{Request, StatusCode}, @@ -893,7 +893,7 @@ mod tests { }; fulu.proposer_lookahead = lookahead.try_into().unwrap(); for slot in 0..fulu.block_roots.len() { - fulu.block_roots[slot] = H256::repeat_byte(slot as u8 + 1); + fulu.block_roots[slot] = slot_root(slot as u64); } state } diff --git a/crates/net/rpc/src/lib.rs b/crates/net/rpc/src/lib.rs index 4870c2781..36481df1c 100644 --- a/crates/net/rpc/src/lib.rs +++ b/crates/net/rpc/src/lib.rs @@ -752,6 +752,19 @@ pub(crate) mod test_utils { Some(client.expect("building a client opens no connection")) } + /// A block root of its own for every `slot`, never the zero hash: `slot + 1` + /// in the last eight bytes, big-endian. + /// + /// For filling a state's `block_roots` so a root read from the wrong slot + /// shows up. `H256::repeat_byte(slot as u8 + 1)` cannot do that: the window + /// is `SLOTS_PER_HISTORICAL_ROOT` long, more slots than a byte counts, so it + /// overflows at slot 255 (a panic in debug, repeating roots in release). + pub(crate) fn slot_root(slot: u64) -> H256 { + let mut root = [0; 32]; + root[24..].copy_from_slice(&(slot + 1).to_be_bytes()); + H256(root) + } + /// A beacon store whose anchor, and so head, is `state`, under a block at /// the state's slot. Returns the store and that block's root. ///