Skip to content

Commit

Permalink
feat: support for syncing pre-genesis blocks (#203)
Browse files Browse the repository at this point in the history
We will sync blocks with merkle proofs for the whole batches, which
obsoletes the unused "batch syncing" logic that we had.

I've introduced a concept of PreGenesisBlock which contains a custom
Justification, and a verify_pre_genesis_block() method in
PersistentBlockStore for verifying those.
Some of the RPC requests required tuning to provide backward
compatibility.

I've removed WIRE from protobuf compatibility check, since it is a
subset of WIRE_JSON.
  • Loading branch information
pompon0 authored Oct 9, 2024
1 parent 0694a3e commit 6a4a695
Show file tree
Hide file tree
Showing 61 changed files with 974 additions and 2,021 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/protobuf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ jobs:
with:
github_token: ${{ github.token }}
- name: buf breaking
run: buf breaking './after.binpb' --against './before.binpb' --config '{"version":"v1","breaking":{"use":["WIRE_JSON","WIRE"]}}' --error-format 'github-actions'
run: buf breaking './after.binpb' --against './before.binpb' --config '{"version":"v1","breaking":{"use":["WIRE_JSON"]}}' --error-format 'github-actions'
24 changes: 12 additions & 12 deletions node/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions node/actors/bft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ impl Config {
anyhow::ensure!(genesis.protocol_version == validator::ProtocolVersion::CURRENT);
genesis.verify().context("genesis().verify()")?;

if let Some(prev) = genesis.first_block.prev() {
tracing::info!("Waiting for the pre-genesis blocks to be persisted");
if let Err(ctx::Canceled) = self.block_store.wait_until_persisted(ctx, prev).await {
return Ok(());
}
}

let cfg = Arc::new(self);
let (leader, leader_send) = leader::StateMachine::new(ctx, cfg.clone(), pipe.send.clone());
let (replica, replica_send) =
Expand Down
2 changes: 1 addition & 1 deletion node/actors/bft/src/replica/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl StateMachine {
);
self.config
.block_store
.queue_block(ctx, block.clone())
.queue_block(ctx, block.clone().into())
.await?;
// For availability, replica should not proceed until it stores the block persistently.
self.config
Expand Down
2 changes: 1 addition & 1 deletion node/actors/bft/src/replica/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ async fn leader_prepare_invalid_payload() {
util.replica
.config
.block_store
.queue_block(ctx, block)
.queue_block(ctx, block.into())
.await
.unwrap();

Expand Down
1 change: 0 additions & 1 deletion node/actors/bft/src/testonly/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ pub(super) struct Node {
pub(crate) net: network::Config,
pub(crate) behavior: Behavior,
pub(crate) block_store: Arc<storage::BlockStore>,
pub(crate) batch_store: Arc<storage::BatchStore>,
}

impl Node {
Expand Down
25 changes: 9 additions & 16 deletions node/actors/bft/src/testonly/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use zksync_concurrency::{
oneshot, scope,
};
use zksync_consensus_network::{self as network};
use zksync_consensus_roles::validator;
use zksync_consensus_roles::{validator, validator::testonly::Setup};
use zksync_consensus_storage::{testonly::TestMemoryStorage, BlockStore};
use zksync_consensus_utils::pipe;

Expand Down Expand Up @@ -114,26 +114,23 @@ impl Test {
/// Run a test with the given parameters and a random network setup.
pub(crate) async fn run(&self, ctx: &ctx::Ctx) -> Result<(), TestError> {
let rng = &mut ctx.rng();
let setup = validator::testonly::Setup::new_with_weights(
rng,
self.nodes.iter().map(|(_, w)| *w).collect(),
);
let setup = Setup::new_with_weights(rng, self.nodes.iter().map(|(_, w)| *w).collect());
let nets: Vec<_> = network::testonly::new_configs(rng, &setup, 1);
self.run_with_config(ctx, nets, &setup.genesis).await
self.run_with_config(ctx, nets, &setup).await
}

/// Run a test with the given parameters and network configuration.
pub(crate) async fn run_with_config(
&self,
ctx: &ctx::Ctx,
nets: Vec<Config>,
genesis: &validator::Genesis,
setup: &Setup,
) -> Result<(), TestError> {
let mut nodes = vec![];
let mut honest = vec![];
scope::run!(ctx, |ctx, s| async {
for (i, net) in nets.into_iter().enumerate() {
let store = TestMemoryStorage::new(ctx, genesis).await;
let store = TestMemoryStorage::new(ctx, setup).await;
s.spawn_bg(async { Ok(store.runner.run(ctx).await?) });

if self.nodes[i].0 == Behavior::Honest {
Expand All @@ -144,15 +141,14 @@ impl Test {
net,
behavior: self.nodes[i].0,
block_store: store.blocks,
batch_store: store.batches,
});
}
assert!(!honest.is_empty());
s.spawn_bg(async { Ok(run_nodes(ctx, &self.network, &nodes).await?) });

// Run the nodes until all honest nodes store enough finalized blocks.
assert!(self.blocks_to_finalize > 0);
let first = genesis.first_block;
let first = setup.genesis.first_block;
let last = first + (self.blocks_to_finalize as u64 - 1);
for store in &honest {
store.wait_until_queued(ctx, last).await?;
Expand All @@ -165,7 +161,7 @@ impl Test {
let want = honest[0].block(ctx, i).await?.context("missing block")?;
for store in &honest[1..] {
let got = store.block(ctx, i).await?.context("missing block")?;
if want.payload != got.payload {
if want.payload() != got.payload() {
return Err(TestError::BlockConflict);
}
}
Expand All @@ -189,11 +185,8 @@ async fn run_nodes_real(ctx: &ctx::Ctx, specs: &[Node]) -> anyhow::Result<()> {
scope::run!(ctx, |ctx, s| async {
let mut nodes = vec![];
for (i, spec) in specs.iter().enumerate() {
let (node, runner) = network::testonly::Instance::new(
spec.net.clone(),
spec.block_store.clone(),
spec.batch_store.clone(),
);
let (node, runner) =
network::testonly::Instance::new(spec.net.clone(), spec.block_store.clone());
s.spawn_bg(runner.run(ctx).instrument(tracing::info_span!("node", i)));
nodes.push(node);
}
Expand Down
Loading

0 comments on commit 6a4a695

Please sign in to comment.