-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implemented 1 validator consensus for the main node (#554)
## What ❔ Implemented 1 validator consensus for the main node. It is NOT enabled yet, corresponding configuration still needs to be provided. It is responsible for populating consensus column in the Miniblocks table. Consensus will be running asynchronously for now, not affecting the miniblock production at all. Fixes BFT-388. ## Why ❔ Reaching a consensus with just 1 validator doesn't provide much value, however we want to start running the consensus code in production (in a "shadow" mode) to make sure it works as expected before we start relying on it.
- Loading branch information
Showing
27 changed files
with
1,440 additions
and
367 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
core/lib/dal/migrations/20231128123456_create_consensus_replica_state_table.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE consensus_replica_state; |
6 changes: 6 additions & 0 deletions
6
core/lib/dal/migrations/20231128123456_create_consensus_replica_state_table.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
CREATE TABLE IF NOT EXISTS consensus_replica_state ( | ||
state JSONB NOT NULL, | ||
-- artificial primary key ensuring that the table contains at most 1 row. | ||
fake_key BOOLEAN PRIMARY KEY, | ||
CHECK (fake_key) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use zksync_consensus_storage::ReplicaState; | ||
|
||
use crate::StorageProcessor; | ||
|
||
#[derive(Debug)] | ||
pub struct ConsensusDal<'a, 'c> { | ||
pub storage: &'a mut StorageProcessor<'c>, | ||
} | ||
|
||
impl ConsensusDal<'_, '_> { | ||
pub async fn replica_state(&mut self) -> anyhow::Result<Option<ReplicaState>> { | ||
let Some(row) = | ||
sqlx::query!("SELECT state as \"state!\" FROM consensus_replica_state WHERE fake_key") | ||
.fetch_optional(self.storage.conn()) | ||
.await? | ||
else { | ||
return Ok(None); | ||
}; | ||
Ok(Some(zksync_protobuf::serde::deserialize(row.state)?)) | ||
} | ||
|
||
pub async fn put_replica_state(&mut self, state: &ReplicaState) -> sqlx::Result<()> { | ||
let state = | ||
zksync_protobuf::serde::serialize(state, serde_json::value::Serializer).unwrap(); | ||
sqlx::query!("INSERT INTO consensus_replica_state(fake_key,state) VALUES(true,$1) ON CONFLICT (fake_key) DO UPDATE SET state = excluded.state", state) | ||
.execute(self.storage.conn()) | ||
.await?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use rand::Rng as _; | ||
use zksync_consensus_storage::ReplicaState; | ||
|
||
use crate::ConnectionPool; | ||
|
||
#[tokio::test] | ||
async fn replica_state_read_write() { | ||
let pool = ConnectionPool::test_pool().await; | ||
let mut conn = pool.access_storage().await.unwrap(); | ||
assert!(conn | ||
.consensus_dal() | ||
.replica_state() | ||
.await | ||
.unwrap() | ||
.is_none()); | ||
let rng = &mut rand::thread_rng(); | ||
for _ in 0..10 { | ||
let want: ReplicaState = rng.gen(); | ||
conn.consensus_dal().put_replica_state(&want).await.unwrap(); | ||
assert_eq!( | ||
Some(want), | ||
conn.consensus_dal().replica_state().await.unwrap() | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.