Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: restrictive genesis parsing #2605

Merged
merged 3 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading