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

Light client contract related changes #3655

Merged
merged 18 commits into from
Sep 6, 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: 9 additions & 9 deletions crates/hotshot/src/tasks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions crates/task-impls/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
33 changes: 20 additions & 13 deletions crates/testing/src/byzantine/byzantine_behaviour.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 1 addition & 2 deletions crates/testing/src/spinning_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 11 additions & 10 deletions crates/testing/src/test_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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 {
Expand Down
93 changes: 60 additions & 33 deletions crates/types/src/light_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CircuitField>;
/// Concrete type for stake table state
pub type StakeTableState = GenericStakeTableState<CircuitField>;
/// Signature scheme
pub type StateSignatureScheme =
jf_signature::schnorr::SchnorrSignatureScheme<ark_ed_on_bn254::EdwardsConfig>;
Expand Down Expand Up @@ -80,39 +82,54 @@ pub struct GenericLightClientState<F: PrimeField> {
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<F: PrimeField> From<GenericLightClientState<F>> for [F; 7] {
impl<F: PrimeField> From<GenericLightClientState<F>> for [F; 3] {
fn from(state: GenericLightClientState<F>) -> 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<F: PrimeField> From<&GenericLightClientState<F>> for [F; 7] {

impl<F: PrimeField> From<&GenericLightClientState<F>> for [F; 3] {
fn from(state: &GenericLightClientState<F>) -> 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<F: PrimeField> {
/// 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<Config>;

Expand Down Expand Up @@ -161,6 +178,22 @@ impl From<schnorr::KeyPair<Config>> for StateKeyPair {
#[derive(Clone, Debug)]
pub struct GenericPublicInput<F: PrimeField>(Vec<F>);

impl<F: PrimeField> GenericPublicInput<F> {
/// Construct a public input from light client state and static stake table state
pub fn new(lc_state: GenericLightClientState<F>, st_state: GenericStakeTableState<F>) -> 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<F: PrimeField> AsRef<[F]> for GenericPublicInput<F> {
fn as_ref(&self) -> &[F] {
&self.0
Expand All @@ -174,57 +207,51 @@ impl<F: PrimeField> From<Vec<F>> for GenericPublicInput<F> {
}

impl<F: PrimeField> GenericPublicInput<F> {
/// 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]
Comment on lines +249 to +255
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of these magic numbers are kind of tricky to reason about, can we just use a named struct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alxiong @alysiahuggins do you remember if there is any particular reason why we don't use a named struct?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the decision made by @mrain and i recall the public input right now is simple enough to keep track of?
i also personally prefer a named struct, less error prone.

but i tried to make minimal changes for this task, so kept the legacy way. Can update in the future as tech debt.

}
}
Loading