Skip to content

Commit

Permalink
fix: restrictive genesis parsing (#2605)
Browse files Browse the repository at this point in the history
The error about receiving an unsupported consensus genesis was
unnecessarily delayed. I've disallowed unknown fields in genesis to fix
that.
  • Loading branch information
pompon0 authored Aug 7, 2024
1 parent 2cb7918 commit d5f8f38
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
18 changes: 15 additions & 3 deletions core/lib/dal/src/consensus_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use zksync_db_connection::{
error::{DalError, DalResult, SqlxContext},
instrument::{InstrumentExt, Instrumented},
};
use zksync_protobuf::ProtoFmt as _;
use zksync_types::L2BlockNumber;

pub use crate::consensus::{AttestationStatus, Payload};
Expand Down Expand Up @@ -48,9 +49,20 @@ impl ConsensusDal<'_, '_> {
let Some(genesis) = row.genesis else {
return Ok(None);
};
let genesis: validator::GenesisRaw =
zksync_protobuf::serde::deserialize(genesis).decode_column("genesis")?;
Ok(Some(genesis.with_hash()))
// Deserialize the json, but don't allow for unknown fields.
// We might encounter an unknown fields here in case if support for the previous
// consensus protocol version is removed before the migration to a new version
// is performed. The node should NOT operate in such a state.
Ok(Some(
validator::GenesisRaw::read(
&zksync_protobuf::serde::deserialize_proto_with_options(
&genesis, /*deny_unknown_fields=*/ true,
)
.decode_column("genesis")?,
)
.decode_column("genesis")?
.with_hash(),
))
})
.instrument("genesis")
.fetch_optional(self.storage)
Expand Down
12 changes: 11 additions & 1 deletion core/node/consensus/src/en.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use zksync_dal::consensus_dal;
use zksync_node_sync::{
fetcher::FetchedBlock, sync_action::ActionQueueSender, MainNodeClient, SyncState,
};
use zksync_protobuf::ProtoFmt as _;
use zksync_types::L2BlockNumber;
use zksync_web3_decl::client::{DynClient, L2};

Expand Down Expand Up @@ -200,7 +201,16 @@ impl EN {
.await?
.context("fetch_consensus_genesis()")?
.context("main node is not running consensus component")?;
Ok(zksync_protobuf::serde::deserialize(&genesis.0).context("deserialize(genesis)")?)
// Deserialize the json, but don't allow for unknown fields.
// We need to compute the hash of the Genesis, so simply ignoring the unknown fields won't
// do.
Ok(validator::GenesisRaw::read(
&zksync_protobuf::serde::deserialize_proto_with_options(
&genesis.0, /*deny_unknown_fields=*/ true,
)
.context("deserialize")?,
)?
.with_hash())
}

/// Fetches (with retries) the given block from the main node.
Expand Down

0 comments on commit d5f8f38

Please sign in to comment.