Skip to content

Commit

Permalink
fix(protocol): Channel ID Randomization (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell authored and emhane committed Jan 9, 2025
1 parent 7583924 commit ad9f927
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ tracing = { version = "0.1.41", default-features = false }
## misc-testing
arbitrary = { version = "1.4.1", features = ["derive"] }
arbtest = "0.3.2"
rand = "0.8.5"
rand = { version = "0.8.5", default-features = false }
proptest = "1.6.0"
proptest-derive = "0.5.1"
tokio = "1.43.0"
Expand Down
1 change: 1 addition & 0 deletions crates/protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ alloy-eips.workspace = true
alloy-consensus.workspace = true

# Misc
rand = { workspace = true, features = ["small_rng"] }
derive_more = { workspace = true, default-features = false, features = ["from", "as_ref"] }
tracing.workspace = true
thiserror.workspace = true
Expand Down
28 changes: 27 additions & 1 deletion crates/protocol/src/channel_out.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::{Batch, ChannelCompressor, ChannelId, CompressorError, Frame};
use alloc::{vec, vec::Vec};
use op_alloy_genesis::RollupConfig;
use rand::{rngs::SmallRng, RngCore, SeedableRng};

/// The frame overhead.
const FRAME_V0_OVERHEAD: usize = 23;
Expand Down Expand Up @@ -67,7 +68,11 @@ where
self.frame_number = 0;
self.closed = false;
self.compressor.reset();
// TODO: read random bytes into the channel id.
// `getrandom` isn't available for wasm and risc targets
// Thread-based RNGs are not available for no_std
// So we must use a seeded RNG.
let mut small_rng = SmallRng::seed_from_u64(43);
SmallRng::fill_bytes(&mut small_rng, &mut self.id);
}

/// Accepts the given [crate::Batch] data into the [ChannelOut], compressing it
Expand Down Expand Up @@ -187,6 +192,27 @@ mod tests {
}
}

#[test]
fn test_channel_out_reset() {
let config = RollupConfig::default();
let mut channel = ChannelOut {
id: ChannelId::default(),
config: &config,
rlp_length: 10,
closed: true,
frame_number: 11,
compressor: MockCompressor::default(),
};
channel.reset();
assert_eq!(channel.rlp_length, 0);
assert_eq!(channel.frame_number, 0);
// The odds of a randomized channel id being equal to the
// default are so astronomically low, this test will always pass.
// The randomized [u8; 16] is about 1/255^16.
assert!(channel.id != ChannelId::default());
assert!(!channel.closed);
}

#[test]
fn test_channel_out_ready_bytes_empty() {
let config = RollupConfig::default();
Expand Down

0 comments on commit ad9f927

Please sign in to comment.