Skip to content

Commit

Permalink
Start with random StreamId and increment from there
Browse files Browse the repository at this point in the history
  • Loading branch information
romac committed Nov 21, 2024
1 parent 7d51a97 commit 98c4a5f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
26 changes: 14 additions & 12 deletions code/crates/starknet/host/src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use eyre::eyre;

use itertools::Itertools;
use ractor::{async_trait, Actor, ActorProcessingErr, SpawnErr};
use rand::rngs::StdRng;
use rand::{RngCore, SeedableRng};
use sha3::Digest;
use tokio::time::Instant;
Expand Down Expand Up @@ -40,29 +41,31 @@ pub struct HostState {
host: MockHost,
block_store: BlockStore,
part_streams_map: PartStreamsMap,
rng: Box<dyn RngCore + Send + Sync>,
next_stream_id: StreamId,
}

impl HostState {
pub fn new(
host: MockHost,
db_path: impl AsRef<Path>,
rng: impl RngCore + Send + Sync + 'static,
) -> Self {
pub fn new<R>(host: MockHost, db_path: impl AsRef<Path>, rng: &mut R) -> Self
where
R: RngCore,
{
Self {
height: Height::new(0, 0),
round: Round::Nil,
proposer: None,
host,
block_store: BlockStore::new(db_path).unwrap(),
part_streams_map: PartStreamsMap::default(),
rng: Box::new(rng),
next_stream_id: rng.next_u64(),
}
}

pub fn next_stream_id(&mut self) -> StreamId {
let id = self.rng.next_u64();
StreamId::from(id)
let stream_id = self.next_stream_id;
// Wrap around if we get to u64::MAX, which may happen if the initial
// stream id was close to it already.
self.next_stream_id = self.next_stream_id.wrapping_add(1);
stream_id
}

#[tracing::instrument(skip_all, fields(%height, %round))]
Expand Down Expand Up @@ -253,12 +256,10 @@ impl StarknetHost {
std::fs::create_dir_all(&db_dir).map_err(|e| SpawnErr::StartupFailed(e.into()))?;
let db_path = db_dir.join("blocks.db");

let rng = rand::rngs::StdRng::from_entropy();

let (actor_ref, _) = Actor::spawn(
None,
Self::new(mempool, gossip_consensus, metrics),
HostState::new(host, db_path, rng),
HostState::new(host, db_path, &mut StdRng::from_entropy()),
)
.await?;

Expand All @@ -276,6 +277,7 @@ impl StarknetHost {
metrics,
}
}

async fn prune_block_store(&self, state: &mut HostState) {
let max_height = state.block_store.last_height().unwrap_or_default();
let max_retain_blocks = state.host.params.max_retain_blocks as u64;
Expand Down
5 changes: 3 additions & 2 deletions code/crates/starknet/host/src/mock/host/build_proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use std::sync::Arc;
use bytes::Bytes;
use bytesize::ByteSize;
use eyre::eyre;
use rand::RngCore;
use rand::rngs::StdRng;
use rand::{RngCore, SeedableRng};
use sha3::Digest;
use tokio::sync::{mpsc, oneshot};
use tokio::time::Instant;
Expand Down Expand Up @@ -151,7 +152,7 @@ async fn run_build_proposal_task(
// BlockProof
{
// TODO: Compute actual "proof"
let mut rng = rand::rngs::OsRng;
let mut rng = StdRng::from_entropy();
let mut proof = vec![0; 32];
rng.fill_bytes(&mut proof);

Expand Down

0 comments on commit 98c4a5f

Please sign in to comment.