From f532baa295b2230969cbd5fb0b78e8930bf0a03b Mon Sep 17 00:00:00 2001 From: Philippe Camacho Date: Fri, 6 Sep 2024 12:41:37 -0400 Subject: [PATCH] Light client contract related changes (#3655) * Add struct StakeTableState. * Remove fields from the GenericLightClientState * Remove no longer used fields from the generalPublicInput that have been removed from the light client state --------- Co-authored-by: Alysia Huggins Co-authored-by: Alex Xiong --- crates/hotshot/src/tasks/mod.rs | 18 ++-- crates/task-impls/src/network.rs | 6 +- .../src/byzantine/byzantine_behaviour.rs | 33 ++++--- crates/testing/src/spinning_task.rs | 3 +- crates/testing/src/test_builder.rs | 21 +++-- crates/types/src/light_client.rs | 93 ++++++++++++------- 6 files changed, 105 insertions(+), 69 deletions(-) diff --git a/crates/hotshot/src/tasks/mod.rs b/crates/hotshot/src/tasks/mod.rs index bf28daba3a..5f7964c24b 100644 --- a/crates/hotshot/src/tasks/mod.rs +++ b/crates/hotshot/src/tasks/mod.rs @@ -8,11 +8,7 @@ /// Provides trait to create task states from a `SystemContextHandle` pub mod task_state; -use crate::{ - tasks::task_state::CreateTaskState, types::SystemContextHandle, ConsensusApi, - ConsensusMetricsValue, ConsensusTaskRegistry, HotShotConfig, HotShotInitializer, - MarketplaceConfig, Memberships, NetworkTaskRegistry, SignatureKey, SystemContext, Versions, -}; +use std::{fmt::Debug, sync::Arc, time::Duration}; use async_broadcast::broadcast; use async_compatibility_layer::art::{async_sleep, async_spawn}; @@ -36,20 +32,24 @@ use hotshot_task_impls::{ vid::VidTaskState, view_sync::ViewSyncTaskState, }; -use hotshot_types::{consensus::Consensus, message::UpgradeLock}; use hotshot_types::{ + consensus::Consensus, constants::EVENT_CHANNEL_SIZE, - message::Messages, + message::{Messages, UpgradeLock}, request_response::RequestReceiver, traits::{ network::ConnectedNetwork, node_implementation::{ConsensusTime, NodeImplementation, NodeType}, }, }; -use std::fmt::Debug; -use std::{sync::Arc, time::Duration}; use vbs::version::StaticVersionType; +use crate::{ + tasks::task_state::CreateTaskState, types::SystemContextHandle, ConsensusApi, + ConsensusMetricsValue, ConsensusTaskRegistry, HotShotConfig, HotShotInitializer, + MarketplaceConfig, Memberships, NetworkTaskRegistry, SignatureKey, SystemContext, Versions, +}; + /// event for global event stream #[derive(Clone, Debug)] pub enum GlobalEvent { diff --git a/crates/task-impls/src/network.rs b/crates/task-impls/src/network.rs index de6f3ba534..1ea7a3cc1d 100644 --- a/crates/task-impls/src/network.rs +++ b/crates/task-impls/src/network.rs @@ -622,12 +622,14 @@ impl< /// A module with test helpers pub mod test { + use std::ops::{Deref, DerefMut}; + + use async_trait::async_trait; + use super::{ Arc, ConnectedNetwork, HotShotEvent, MessageKind, NetworkEventTaskState, NodeType, Receiver, Result, Sender, Storage, TaskState, TransmitType, Versions, }; - use async_trait::async_trait; - use std::ops::{Deref, DerefMut}; /// A dynamic type alias for a function that takes the result of `NetworkEventTaskState::parse_event` /// and changes it before transmitting on the network. diff --git a/crates/testing/src/byzantine/byzantine_behaviour.rs b/crates/testing/src/byzantine/byzantine_behaviour.rs index 6bca9a2fc0..27ac3a0161 100644 --- a/crates/testing/src/byzantine/byzantine_behaviour.rs +++ b/crates/testing/src/byzantine/byzantine_behaviour.rs @@ -1,22 +1,29 @@ +use std::{ + collections::{HashMap, HashSet}, + sync::Arc, +}; + use anyhow::Context; use async_lock::RwLock; use async_trait::async_trait; -use hotshot::tasks::EventTransformerState; -use hotshot::types::{SignatureKey, SystemContextHandle}; -use hotshot_task_impls::events::HotShotEvent; -use hotshot_task_impls::network::test::{ModifierClosure, NetworkEventTaskStateModifier}; -use hotshot_task_impls::network::NetworkEventTaskState; -use hotshot_types::consensus::Consensus; -use hotshot_types::message::UpgradeLock; -use hotshot_types::simple_vote::QuorumVote; -use hotshot_types::traits::node_implementation::ConsensusTime; +use hotshot::{ + tasks::EventTransformerState, + types::{SignatureKey, SystemContextHandle}, +}; +use hotshot_task_impls::{ + events::HotShotEvent, + network::{ + test::{ModifierClosure, NetworkEventTaskStateModifier}, + NetworkEventTaskState, + }, +}; use hotshot_types::{ + consensus::Consensus, data::QuorumProposal, - message::Proposal, - traits::node_implementation::{NodeImplementation, NodeType, Versions}, + message::{Proposal, UpgradeLock}, + simple_vote::QuorumVote, + traits::node_implementation::{ConsensusTime, NodeImplementation, NodeType, Versions}, }; -use std::collections::{HashMap, HashSet}; -use std::sync::Arc; #[derive(Debug)] /// An `EventTransformerState` that multiplies `QuorumProposalSend` events, incrementing the view number of the proposal diff --git a/crates/testing/src/spinning_task.rs b/crates/testing/src/spinning_task.rs index 4f6743fee7..8d5d8420ae 100644 --- a/crates/testing/src/spinning_task.rs +++ b/crates/testing/src/spinning_task.rs @@ -20,14 +20,13 @@ use hotshot_example_types::{ storage_types::TestStorage, testable_delay::DelayConfig, }; -use hotshot_types::traits::node_implementation::ConsensusTime; use hotshot_types::{ data::Leaf, event::Event, simple_certificate::QuorumCertificate, traits::{ network::ConnectedNetwork, - node_implementation::{NodeImplementation, NodeType, Versions}, + node_implementation::{ConsensusTime, NodeImplementation, NodeType, Versions}, }, vote::HasViewNumber, ValidatorConfig, diff --git a/crates/testing/src/test_builder.rs b/crates/testing/src/test_builder.rs index a11ceb94b9..2cb521b08b 100644 --- a/crates/testing/src/test_builder.rs +++ b/crates/testing/src/test_builder.rs @@ -6,16 +6,6 @@ use std::{collections::HashMap, num::NonZeroUsize, rc::Rc, sync::Arc, time::Duration}; -use super::{ - completion_task::{CompletionTaskDescription, TimeBasedCompletionTaskDescription}, - overall_safety_task::OverallSafetyPropertiesDescription, - txn_task::TxnTaskDescription, -}; -use crate::{ - spinning_task::SpinningTaskDescription, - test_launcher::{Network, ResourceGenerators, TestLauncher}, - view_sync_task::ViewSyncTaskDescription, -}; use hotshot::{ tasks::EventTransformerState, traits::{NetworkReliability, NodeImplementation, TestableNodeImplementation}, @@ -33,6 +23,17 @@ use hotshot_types::{ }; use tide_disco::Url; use vec1::Vec1; + +use super::{ + completion_task::{CompletionTaskDescription, TimeBasedCompletionTaskDescription}, + overall_safety_task::OverallSafetyPropertiesDescription, + txn_task::TxnTaskDescription, +}; +use crate::{ + spinning_task::SpinningTaskDescription, + test_launcher::{Network, ResourceGenerators, TestLauncher}, + view_sync_task::ViewSyncTaskDescription, +}; /// data describing how a round should be timed. #[derive(Clone, Debug, Copy)] pub struct TimingData { diff --git a/crates/types/src/light_client.rs b/crates/types/src/light_client.rs index debb265495..1fac6614e4 100644 --- a/crates/types/src/light_client.rs +++ b/crates/types/src/light_client.rs @@ -22,6 +22,8 @@ use tagged_base64::tagged; pub type CircuitField = ark_ed_on_bn254::Fq; /// Concrete type for light client state pub type LightClientState = GenericLightClientState; +/// Concrete type for stake table state +pub type StakeTableState = GenericStakeTableState; /// Signature scheme pub type StateSignatureScheme = jf_signature::schnorr::SchnorrSignatureScheme; @@ -80,39 +82,54 @@ pub struct GenericLightClientState { pub block_height: usize, /// Root of the block commitment tree pub block_comm_root: F, - /// Commitment for fee ledger - pub fee_ledger_comm: F, - /// Commitment for the stake table - pub stake_table_comm: (F, F, F), } -impl From> for [F; 7] { +impl From> for [F; 3] { fn from(state: GenericLightClientState) -> Self { [ F::from(state.view_number as u64), F::from(state.block_height as u64), state.block_comm_root, - state.fee_ledger_comm, - state.stake_table_comm.0, - state.stake_table_comm.1, - state.stake_table_comm.2, ] } } -impl From<&GenericLightClientState> for [F; 7] { + +impl From<&GenericLightClientState> for [F; 3] { fn from(state: &GenericLightClientState) -> Self { [ F::from(state.view_number as u64), F::from(state.block_height as u64), state.block_comm_root, - state.fee_ledger_comm, - state.stake_table_comm.0, - state.stake_table_comm.1, - state.stake_table_comm.2, ] } } +/// Stake table state +#[tagged("STAKE_TABLE_STATE")] +#[derive( + Clone, + Debug, + CanonicalSerialize, + CanonicalDeserialize, + Default, + Eq, + PartialEq, + PartialOrd, + Ord, + Hash, + Copy, +)] +pub struct GenericStakeTableState { + /// Commitments to the table column for BLS public keys + pub bls_key_comm: F, + /// Commitments to the table column for Schnorr public keys + pub schnorr_key_comm: F, + /// Commitments to the table column for Stake amounts + pub amount_comm: F, + /// threshold + pub threshold: F, +} + impl std::ops::Deref for StateKeyPair { type Target = schnorr::KeyPair; @@ -161,6 +178,22 @@ impl From> for StateKeyPair { #[derive(Clone, Debug)] pub struct GenericPublicInput(Vec); +impl GenericPublicInput { + /// Construct a public input from light client state and static stake table state + pub fn new(lc_state: GenericLightClientState, st_state: GenericStakeTableState) -> Self { + let lc_state_f: [F; 3] = lc_state.into(); + Self(vec![ + lc_state_f[0], + lc_state_f[1], + lc_state_f[2], + st_state.bls_key_comm, + st_state.schnorr_key_comm, + st_state.amount_comm, + st_state.threshold, + ]) + } +} + impl AsRef<[F]> for GenericPublicInput { fn as_ref(&self) -> &[F] { &self.0 @@ -174,57 +207,51 @@ impl From> for GenericPublicInput { } impl GenericPublicInput { - /// Return the threshold - #[must_use] - pub fn threshold(&self) -> F { - self.0[0] - } - /// Return the view number of the light client state #[must_use] pub fn view_number(&self) -> F { - self.0[1] + self.0[0] } /// Return the block height of the light client state #[must_use] pub fn block_height(&self) -> F { - self.0[2] + self.0[1] } /// Return the block commitment root of the light client state #[must_use] pub fn block_comm_root(&self) -> F { - self.0[3] - } - - /// Return the fee ledger commitment of the light client state - #[must_use] - pub fn fee_ledger_comm(&self) -> F { - self.0[4] + self.0[2] } /// Return the stake table commitment of the light client state #[must_use] pub fn stake_table_comm(&self) -> (F, F, F) { - (self.0[5], self.0[6], self.0[7]) + (self.0[3], self.0[4], self.0[5]) } /// Return the qc key commitment of the light client state #[must_use] pub fn qc_key_comm(&self) -> F { - self.0[5] + self.0[3] } /// Return the state key commitment of the light client state #[must_use] pub fn state_key_comm(&self) -> F { - self.0[6] + self.0[4] } /// Return the stake amount commitment of the light client state #[must_use] pub fn stake_amount_comm(&self) -> F { - self.0[7] + self.0[5] + } + + /// Return the threshold + #[must_use] + pub fn threshold(&self) -> F { + self.0[6] } }