Skip to content

Commit

Permalink
Merge branch 'main' into abdul/config
Browse files Browse the repository at this point in the history
  • Loading branch information
imabdulbasit authored May 17, 2024
2 parents d1e365e + 8518c02 commit 90d3b28
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 38 deletions.
18 changes: 10 additions & 8 deletions hotshot-state-prover/src/mock_ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::one_honest_threshold, Proof, VerifyingKey,
};
use hotshot_types::traits::stake_table::StakeTableScheme;
use hotshot_types::{
light_client::{GenericLightClientState, GenericPublicInput, LightClientState},
Expand Down Expand Up @@ -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() * 2 / 3;
let threshold =
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);
Expand Down Expand Up @@ -120,12 +123,11 @@ impl MockLedger {
{
self.epoch += 1;
self.st.advance();
self.threshold = self
.st
.total_stake(SnapshotVersion::LastEpochStart)
.unwrap()
* 2
/ 3;
self.threshold = one_honest_threshold(
self.st
.total_stake(SnapshotVersion::LastEpochStart)
.unwrap(),
);
}

let new_root = self.new_dummy_comm();
Expand Down
10 changes: 8 additions & 2 deletions hotshot-state-prover/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 one_honest_threshold(total_stake: U256) -> U256 {
total_stake / 3 + 1
}

pub fn init_stake_table(
bls_keys: &[BLSPubKey],
state_keys: &[StateVerKey],
Expand Down Expand Up @@ -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)? * 2 / 3;
let threshold = one_honest_threshold(st.total_stake(SnapshotVersion::LastEpochStart)?);

let pi = vec![
u256_to_field(threshold),
Expand Down Expand Up @@ -297,7 +303,7 @@ pub async fn sync_state<Ver: StaticVersionType>(
tracing::debug!("Old state: {old_state:?}");
tracing::debug!("New state: {:?}", bundle.state);

let threshold = st.total_stake(SnapshotVersion::LastEpochStart)? * 2 / 3;
let threshold = one_honest_threshold(st.total_stake(SnapshotVersion::LastEpochStart)?);
tracing::info!("Threshold before syncing state: {}", threshold);
let entries = st
.try_iter(SnapshotVersion::LastEpochStart)
Expand Down
2 changes: 1 addition & 1 deletion sequencer/src/bin/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,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,

Expand Down
12 changes: 6 additions & 6 deletions sequencer/src/bin/nasty-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ struct ClientConfig {
#[derive(Clone, Debug, Parser)]
struct ActionDistribution {
/// The weight of query actions in the random distribution.
#[clap(long, env = "ESPRESSO_NASTY_CLIENT_WEIGHT_QUERY", default_value = "5")]
#[clap(long, env = "ESPRESSO_NASTY_CLIENT_WEIGHT_QUERY", default_value = "20")]
weight_query: u8,

/// The weight of "open stream" actions in the random distribution.
Expand All @@ -172,39 +172,39 @@ struct ActionDistribution {
#[clap(
long,
env = "ESPRESSO_NASTY_CLIENT_WEIGHT_POLL_STREAM",
default_value = "5"
default_value = "10"
)]
weight_poll_stream: u8,

/// The weight of "query window" actions in the random distribution.
#[clap(
long,
env = "ESPRESSO_NASTY_CLIENT_WEIGHT_QUERY_WINDOW",
default_value = "3"
default_value = "15"
)]
weight_query_window: u8,

/// The weight of "query namespace" actions in the random distribution.
#[clap(
long,
env = "ESPRESSO_NASTY_CLIENT_WEIGHT_QUERY_NAMESPACE",
default_value = "3"
default_value = "15"
)]
weight_query_namespace: u8,

/// The weight of "query block state" actions in the random distribution.
#[clap(
long,
env = "ESPRESSO_NASTY_CLIENT_WEIGHT_QUERY_BLOCK_STATE",
default_value = "3"
default_value = "15"
)]
weight_query_block_state: u8,

/// The weight of "query fee state" actions in the random distribution.
#[clap(
long,
env = "ESPRESSO_NASTY_CLIENT_WEIGHT_QUERY_FEE_STATE",
default_value = "3"
default_value = "15"
)]
weight_query_fee_state: u8,
}
Expand Down
9 changes: 7 additions & 2 deletions sequencer/src/bin/state-relay-server.rs
Original file line number Diff line number Diff line change
@@ -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::one_honest_threshold;
use sequencer::state_signature::relay_server::run_relay_server;

#[derive(Parser)]
Expand Down Expand Up @@ -31,9 +33,12 @@ async fn main() {
setup_backtrace();

let args = Args::parse();
let threshold = ((2 * args.total_stake) / 3) + 1;
let threshold = one_honest_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,
Expand Down
38 changes: 22 additions & 16 deletions sequencer/src/l1_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,26 +121,32 @@ impl L1Client {

/// Get information about the given block.
///
/// If the desired block number is not available yet, this function will block until it becomes
/// available.
pub async fn wait_for_block(&self, number: u64) -> L1BlockInfo {
/// If the desired block number is not finalized yet, this function will block until it becomes
/// finalized.
pub async fn wait_for_finalized_block(&self, number: u64) -> L1BlockInfo {
let interval = self.provider.get_interval();

// Wait for the block to become available.
loop {
match self.provider.get_block_number().await {
Ok(height) if height > number.into() => break,
Ok(height) => {
tracing::warn!(number, %height, "waiting for L1 block");
}
Err(err) => {
tracing::error!(%err, "failed to get L1 block height");
}
// Wait for the block to finalize.
let finalized = loop {
let Some(block) = self.get_finalized_block().await else {
tracing::info!("waiting for finalized block");
sleep(interval).await;
continue;
};
if block.number >= number {
break block;
}
tracing::info!(current_finalized = %block.number, "waiting for finalized block");
sleep(interval).await;
continue;
};

if finalized.number == number {
return finalized;
}

// Get the block, retrying until we succeed.
// The finalized block may have skipped over the block of interest. In this case, our block
// is still finalized, since it is before the finalized block. We just need to fetch it.
loop {
let block = match self.provider.get_block(number).await {
Ok(Some(block)) => block,
Expand Down Expand Up @@ -477,7 +483,7 @@ mod test {
}

#[async_std::test]
async fn test_wait_for_block() {
async fn test_wait_for_finalized_block() {
setup_logging();
setup_backtrace();

Expand All @@ -487,7 +493,7 @@ mod test {

// Wait for a block 10 blocks in the future.
let block_height = provider.get_block_number().await.unwrap().as_u64();
let block = l1_client.wait_for_block(block_height + 10).await;
let block = l1_client.wait_for_finalized_block(block_height + 10).await;
assert_eq!(block.number, block_height + 10);

// Compare against underlying provider.
Expand Down
2 changes: 1 addition & 1 deletion sequencer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ pub async fn init_node<P: PersistenceOptions, Ver: StaticVersionType + 'static>(

let l1_client = L1Client::new(l1_params.url, l1_params.events_max_block_range);
let l1_genesis = match l1_params.finalized_block {
Some(block) => Some(l1_client.wait_for_block(block).await),
Some(block) => Some(l1_client.wait_for_finalized_block(block).await),
None => None,
};

Expand Down
3 changes: 1 addition & 2 deletions sequencer/src/state_signature/relay_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ where

pub async fn run_relay_server<Ver: StaticVersionType + 'static>(
shutdown_listener: Option<OneShotReceiver<()>>,
threshold: u64,
threshold: U256,
url: Url,
bind_version: Ver,
) -> std::io::Result<()> {
Expand All @@ -234,7 +234,6 @@ pub async fn run_relay_server<Ver: StaticVersionType + 'static>(

// 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::<State, Error>::with_state(state);
Expand Down

0 comments on commit 90d3b28

Please sign in to comment.