From 936763f0243880d5795a3b62a816dd9e7686207f Mon Sep 17 00:00:00 2001 From: MRain Date: Wed, 15 May 2024 14:12:57 +0800 Subject: [PATCH 1/5] correct threshold --- hotshot-state-prover/src/mock_ledger.rs | 6 +++--- hotshot-state-prover/src/service.rs | 4 ++-- sequencer/src/bin/state-relay-server.rs | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/hotshot-state-prover/src/mock_ledger.rs b/hotshot-state-prover/src/mock_ledger.rs index 0b59ceeef..01fe60b4a 100644 --- a/hotshot-state-prover/src/mock_ledger.rs +++ b/hotshot-state-prover/src/mock_ledger.rs @@ -86,7 +86,7 @@ impl MockLedger { key_archive.insert(qc_keys[i], state_keys[i].0.clone()); } let st = stake_table_for_testing(&qc_keys, &state_keys); - let threshold = st.total_stake(SnapshotVersion::LastEpochStart).unwrap() * 2 / 3; + let threshold = st.total_stake(SnapshotVersion::LastEpochStart).unwrap() / 3 + 1; // arbitrary commitment values as they don't affect logic being tested let block_comm_root = F::from(1234); @@ -124,8 +124,8 @@ impl MockLedger { .st .total_stake(SnapshotVersion::LastEpochStart) .unwrap() - * 2 - / 3; + / 3 + + 1; } let new_root = self.new_dummy_comm(); diff --git a/hotshot-state-prover/src/service.rs b/hotshot-state-prover/src/service.rs index afaff42f8..c9a055707 100644 --- a/hotshot-state-prover/src/service.rs +++ b/hotshot-state-prover/src/service.rs @@ -156,7 +156,7 @@ pub async fn light_client_genesis( let (bls_comm, schnorr_comm, stake_comm) = st .commitment(SnapshotVersion::LastEpochStart) .expect("Commitment computation shouldn't fail."); - let threshold = st.total_stake(SnapshotVersion::LastEpochStart)? * 2 / 3; + let threshold = st.total_stake(SnapshotVersion::LastEpochStart)? / 3 + 1; let pi = vec![ u256_to_field(threshold), @@ -297,7 +297,7 @@ pub async fn sync_state( tracing::debug!("Old state: {old_state:?}"); tracing::debug!("New state: {:?}", bundle.state); - let threshold = st.total_stake(SnapshotVersion::LastEpochStart)? * 2 / 3; + let threshold = st.total_stake(SnapshotVersion::LastEpochStart)? / 3 + 1; tracing::info!("Threshold before syncing state: {}", threshold); let entries = st .try_iter(SnapshotVersion::LastEpochStart) diff --git a/sequencer/src/bin/state-relay-server.rs b/sequencer/src/bin/state-relay-server.rs index 2aef99c31..0df3277fd 100644 --- a/sequencer/src/bin/state-relay-server.rs +++ b/sequencer/src/bin/state-relay-server.rs @@ -31,7 +31,7 @@ async fn main() { setup_backtrace(); let args = Args::parse(); - let threshold = ((2 * args.total_stake) / 3) + 1; + let threshold = (args.total_stake / 3) + 1; tracing::info!(port = args.port, threshold, "starting state relay server"); run_relay_server( From d9054252ebdc71e7ac17d24cd8ba34877dfda647 Mon Sep 17 00:00:00 2001 From: MRain Date: Wed, 15 May 2024 14:18:47 +0800 Subject: [PATCH 2/5] correct comment --- sequencer/src/bin/deploy.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sequencer/src/bin/deploy.rs b/sequencer/src/bin/deploy.rs index 022265034..2bf82d5bd 100644 --- a/sequencer/src/bin/deploy.rs +++ b/sequencer/src/bin/deploy.rs @@ -82,7 +82,7 @@ struct Options { #[clap(flatten)] contracts: DeployedContracts, - /// If toggled, launch a mock prover contract that does not do any proof verification. + /// If toggled, launch a mock prover contract with a smaller verification key. #[clap(short, long)] pub use_mock_contract: bool, From d36c2d09442c7b3dbd79e4a4205c553b4c9f2dc6 Mon Sep 17 00:00:00 2001 From: MRain Date: Thu, 16 May 2024 20:33:03 +0800 Subject: [PATCH 3/5] use a helper fn --- hotshot-state-prover/src/mock_ledger.rs | 18 ++++++++++-------- hotshot-state-prover/src/service.rs | 10 ++++++++-- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/hotshot-state-prover/src/mock_ledger.rs b/hotshot-state-prover/src/mock_ledger.rs index 01fe60b4a..5bcb29e49 100644 --- a/hotshot-state-prover/src/mock_ledger.rs +++ b/hotshot-state-prover/src/mock_ledger.rs @@ -16,7 +16,9 @@ use hotshot_contract_adapter::jellyfish::{field_to_u256, open_key, u256_to_field use hotshot_contract_adapter::light_client::ParsedLightClientState; use hotshot_stake_table::vec_based::StakeTable; -use crate::{generate_state_update_proof, preprocess, Proof, VerifyingKey}; +use crate::{ + generate_state_update_proof, preprocess, service::compute_quorum_threshold, Proof, VerifyingKey, +}; use hotshot_types::traits::stake_table::StakeTableScheme; use hotshot_types::{ light_client::{GenericLightClientState, GenericPublicInput, LightClientState}, @@ -86,7 +88,8 @@ impl MockLedger { key_archive.insert(qc_keys[i], state_keys[i].0.clone()); } let st = stake_table_for_testing(&qc_keys, &state_keys); - let threshold = st.total_stake(SnapshotVersion::LastEpochStart).unwrap() / 3 + 1; + let threshold = + compute_quorum_threshold(st.total_stake(SnapshotVersion::LastEpochStart).unwrap()); // arbitrary commitment values as they don't affect logic being tested let block_comm_root = F::from(1234); @@ -120,12 +123,11 @@ impl MockLedger { { self.epoch += 1; self.st.advance(); - self.threshold = self - .st - .total_stake(SnapshotVersion::LastEpochStart) - .unwrap() - / 3 - + 1; + self.threshold = compute_quorum_threshold( + self.st + .total_stake(SnapshotVersion::LastEpochStart) + .unwrap(), + ); } let new_root = self.new_dummy_comm(); diff --git a/hotshot-state-prover/src/service.rs b/hotshot-state-prover/src/service.rs index c9a055707..08e27fe66 100644 --- a/hotshot-state-prover/src/service.rs +++ b/hotshot-state-prover/src/service.rs @@ -79,6 +79,12 @@ pub struct StateProverConfig { pub stake_table_capacity: usize, } +#[inline] +/// A helper function to compute the quorum threshold given a total amount of stake. +pub fn compute_quorum_threshold(total_stake: U256) -> U256 { + total_stake / 3 + 1 +} + pub fn init_stake_table( bls_keys: &[BLSPubKey], state_keys: &[StateVerKey], @@ -156,7 +162,7 @@ pub async fn light_client_genesis( let (bls_comm, schnorr_comm, stake_comm) = st .commitment(SnapshotVersion::LastEpochStart) .expect("Commitment computation shouldn't fail."); - let threshold = st.total_stake(SnapshotVersion::LastEpochStart)? / 3 + 1; + let threshold = compute_quorum_threshold(st.total_stake(SnapshotVersion::LastEpochStart)?); let pi = vec![ u256_to_field(threshold), @@ -297,7 +303,7 @@ pub async fn sync_state( tracing::debug!("Old state: {old_state:?}"); tracing::debug!("New state: {:?}", bundle.state); - let threshold = st.total_stake(SnapshotVersion::LastEpochStart)? / 3 + 1; + let threshold = compute_quorum_threshold(st.total_stake(SnapshotVersion::LastEpochStart)?); tracing::info!("Threshold before syncing state: {}", threshold); let entries = st .try_iter(SnapshotVersion::LastEpochStart) From ead3d4ea71e668beffd5256374ba4b803876605d Mon Sep 17 00:00:00 2001 From: MRain Date: Thu, 16 May 2024 20:42:30 +0800 Subject: [PATCH 4/5] fix relay server --- sequencer/src/bin/state-relay-server.rs | 9 +++++++-- sequencer/src/state_signature/relay_server.rs | 3 +-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/sequencer/src/bin/state-relay-server.rs b/sequencer/src/bin/state-relay-server.rs index 0df3277fd..15bf5b202 100644 --- a/sequencer/src/bin/state-relay-server.rs +++ b/sequencer/src/bin/state-relay-server.rs @@ -1,6 +1,8 @@ use async_compatibility_layer::logging::{setup_backtrace, setup_logging}; use clap::Parser; use es_version::SEQUENCER_VERSION; +use ethers::types::U256; +use hotshot_state_prover::service::compute_quorum_threshold; use sequencer::state_signature::relay_server::run_relay_server; #[derive(Parser)] @@ -31,9 +33,12 @@ async fn main() { setup_backtrace(); let args = Args::parse(); - let threshold = (args.total_stake / 3) + 1; + let threshold = compute_quorum_threshold(U256::from(args.total_stake)); - tracing::info!(port = args.port, threshold, "starting state relay server"); + tracing::info!( + port = args.port, + "starting state relay server, quorum threshold: {threshold}" + ); run_relay_server( None, threshold, diff --git a/sequencer/src/state_signature/relay_server.rs b/sequencer/src/state_signature/relay_server.rs index d61d3c417..4dd0263f8 100644 --- a/sequencer/src/state_signature/relay_server.rs +++ b/sequencer/src/state_signature/relay_server.rs @@ -224,7 +224,7 @@ where pub async fn run_relay_server( shutdown_listener: Option>, - threshold: u64, + threshold: U256, url: Url, bind_version: Ver, ) -> std::io::Result<()> { @@ -234,7 +234,6 @@ pub async fn run_relay_server( // We don't have a stake table yet, putting some temporary value here. // Related issue: [https://github.com/EspressoSystems/espresso-sequencer/issues/1022] - let threshold = U256::from(threshold); let state = State::new(StateRelayServerState::new(threshold).with_shutdown_signal(shutdown_listener)); let mut app = App::::with_state(state); From 0a6b8b1d97294c7b3625836a6c33bec03003b261 Mon Sep 17 00:00:00 2001 From: MRain Date: Thu, 16 May 2024 21:06:22 +0800 Subject: [PATCH 5/5] rename --- hotshot-state-prover/src/mock_ledger.rs | 6 +++--- hotshot-state-prover/src/service.rs | 6 +++--- sequencer/src/bin/state-relay-server.rs | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/hotshot-state-prover/src/mock_ledger.rs b/hotshot-state-prover/src/mock_ledger.rs index 5bcb29e49..0e9ec1190 100644 --- a/hotshot-state-prover/src/mock_ledger.rs +++ b/hotshot-state-prover/src/mock_ledger.rs @@ -17,7 +17,7 @@ use hotshot_contract_adapter::light_client::ParsedLightClientState; use hotshot_stake_table::vec_based::StakeTable; use crate::{ - generate_state_update_proof, preprocess, service::compute_quorum_threshold, Proof, VerifyingKey, + generate_state_update_proof, preprocess, service::one_honest_threshold, Proof, VerifyingKey, }; use hotshot_types::traits::stake_table::StakeTableScheme; use hotshot_types::{ @@ -89,7 +89,7 @@ impl MockLedger { } let st = stake_table_for_testing(&qc_keys, &state_keys); let threshold = - compute_quorum_threshold(st.total_stake(SnapshotVersion::LastEpochStart).unwrap()); + one_honest_threshold(st.total_stake(SnapshotVersion::LastEpochStart).unwrap()); // arbitrary commitment values as they don't affect logic being tested let block_comm_root = F::from(1234); @@ -123,7 +123,7 @@ impl MockLedger { { self.epoch += 1; self.st.advance(); - self.threshold = compute_quorum_threshold( + self.threshold = one_honest_threshold( self.st .total_stake(SnapshotVersion::LastEpochStart) .unwrap(), diff --git a/hotshot-state-prover/src/service.rs b/hotshot-state-prover/src/service.rs index 08e27fe66..5b609b17a 100644 --- a/hotshot-state-prover/src/service.rs +++ b/hotshot-state-prover/src/service.rs @@ -81,7 +81,7 @@ pub struct StateProverConfig { #[inline] /// A helper function to compute the quorum threshold given a total amount of stake. -pub fn compute_quorum_threshold(total_stake: U256) -> U256 { +pub fn one_honest_threshold(total_stake: U256) -> U256 { total_stake / 3 + 1 } @@ -162,7 +162,7 @@ pub async fn light_client_genesis( let (bls_comm, schnorr_comm, stake_comm) = st .commitment(SnapshotVersion::LastEpochStart) .expect("Commitment computation shouldn't fail."); - let threshold = compute_quorum_threshold(st.total_stake(SnapshotVersion::LastEpochStart)?); + let threshold = one_honest_threshold(st.total_stake(SnapshotVersion::LastEpochStart)?); let pi = vec![ u256_to_field(threshold), @@ -303,7 +303,7 @@ pub async fn sync_state( tracing::debug!("Old state: {old_state:?}"); tracing::debug!("New state: {:?}", bundle.state); - let threshold = compute_quorum_threshold(st.total_stake(SnapshotVersion::LastEpochStart)?); + let threshold = one_honest_threshold(st.total_stake(SnapshotVersion::LastEpochStart)?); tracing::info!("Threshold before syncing state: {}", threshold); let entries = st .try_iter(SnapshotVersion::LastEpochStart) diff --git a/sequencer/src/bin/state-relay-server.rs b/sequencer/src/bin/state-relay-server.rs index 15bf5b202..5b77ec490 100644 --- a/sequencer/src/bin/state-relay-server.rs +++ b/sequencer/src/bin/state-relay-server.rs @@ -2,7 +2,7 @@ use async_compatibility_layer::logging::{setup_backtrace, setup_logging}; use clap::Parser; use es_version::SEQUENCER_VERSION; use ethers::types::U256; -use hotshot_state_prover::service::compute_quorum_threshold; +use hotshot_state_prover::service::one_honest_threshold; use sequencer::state_signature::relay_server::run_relay_server; #[derive(Parser)] @@ -33,7 +33,7 @@ async fn main() { setup_backtrace(); let args = Args::parse(); - let threshold = compute_quorum_threshold(U256::from(args.total_stake)); + let threshold = one_honest_threshold(U256::from(args.total_stake)); tracing::info!( port = args.port,