Skip to content

Commit

Permalink
Update contract bindings, non-test code
Browse files Browse the repository at this point in the history
  • Loading branch information
sveitser committed Dec 20, 2024
1 parent a88ce93 commit 01e77fe
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 124 deletions.
86 changes: 19 additions & 67 deletions contract-bindings/src/permissioned_stake_table.rs

Large diffs are not rendered by default.

50 changes: 31 additions & 19 deletions contracts/rust/adapter/src/stake_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,25 +173,7 @@ impl From<NodeInfo> for NodeInfoJf {
schnorr_vk,
is_da,
} = value;
let stake_table_key = {
let g2 = diff_test_bn254::ParsedG2Point {
x0: bls_vk.x_0,
x1: bls_vk.x_1,
y0: bls_vk.y_0,
y1: bls_vk.y_1,
};
let g2_affine = short_weierstrass::Affine::<ark_bn254::g2::Config>::from(g2);
let mut bytes = vec![];
// TODO: remove serde round-trip once jellyfin provides a way to
// convert from Affine representation to VerKey.
//
// Serialization and de-serialization shouldn't fail.
g2_affine
.into_group()
.serialize_compressed(&mut bytes)
.unwrap();
BLSPubKey::deserialize_compressed(&bytes[..]).unwrap()
};
let stake_table_key = bls_sol_to_jf(bls_vk);
let state_ver_key = {
let g1_point: ParsedEdOnBN254Point = schnorr_vk.into();
let state_sk_affine = twisted_edwards::Affine::<EdwardsConfig>::from(g1_point);
Expand Down Expand Up @@ -221,6 +203,36 @@ impl From<PeerConfigKeys<BLSPubKey>> for NodeInfoJf {
}
}

pub fn bls_jf_to_sol(bls_vk: BLSPubKey) -> permissioned_stake_table::G2Point {
let ParsedG2Point { x0, x1, y0, y1 } = bls_vk.to_affine().into();
permissioned_stake_table::G2Point {
x_0: x0,
x_1: x1,
y_0: y0,
y_1: y1,
}
}

pub fn bls_sol_to_jf(bls_vk: permissioned_stake_table::G2Point) -> BLSPubKey {
let g2 = diff_test_bn254::ParsedG2Point {
x0: bls_vk.x_0,
x1: bls_vk.x_1,
y0: bls_vk.y_0,
y1: bls_vk.y_1,
};
let g2_affine = short_weierstrass::Affine::<ark_bn254::g2::Config>::from(g2);
let mut bytes = vec![];
// TODO: remove serde round-trip once jellyfin provides a way to
// convert from Affine representation to VerKey.
//
// Serialization and de-serialization shouldn't fail.
g2_affine
.into_group()
.serialize_compressed(&mut bytes)
.unwrap();
BLSPubKey::deserialize_compressed(&bytes[..]).unwrap()
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
6 changes: 4 additions & 2 deletions sequencer/src/bin/update-permissioned-stake-table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use anyhow::Result;
use clap::Parser;
use espresso_types::parse_duration;
use ethers::types::Address;
use sequencer_utils::{logging, stake_table::{update_stake_table, PermissionedStakeTableUpdate}};
use sequencer_utils::{
logging,
stake_table::{update_stake_table, PermissionedStakeTableUpdate},
};
use std::{path::PathBuf, time::Duration};
use url::Url;

Expand Down Expand Up @@ -89,7 +92,6 @@ async fn main() -> Result<()> {
opts.logging.init();
let update = PermissionedStakeTableUpdate::from_toml_file(&opts.update_toml_path)?;


update_stake_table(
opts.rpc_url,
opts.l1_polling_interval,
Expand Down
2 changes: 1 addition & 1 deletion sequencer/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub struct Genesis {
pub header: GenesisHeader,
#[serde(rename = "upgrade", with = "upgrade_ser")]
#[serde(default)]
pub upgrades: BTreeMap<Version, Upgrade>,
pub upgrades: BTreeMap<Version, Upgrade>,
}

impl Genesis {
Expand Down
43 changes: 16 additions & 27 deletions types/src/v0/impls/stake_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use super::{

use contract_bindings::permissioned_stake_table::StakersUpdatedFilter;
use ethers::types::U256;
use hotshot::types::SignatureKey as _;
use hotshot_contract_adapter::stake_table::NodeInfoJf;
use hotshot::types::{BLSPubKey, SignatureKey as _};
use hotshot_contract_adapter::stake_table::{bls_sol_to_jf, NodeInfoJf};
use hotshot_types::{
data::EpochNumber,
stake_table::StakeTableEntry,
Expand Down Expand Up @@ -55,25 +55,25 @@ impl StakeTables {
event
.removed
.into_iter()
.map(|node_info| StakeTableDelta::remove(node_info.into()))
.map(|key| StakeTableChange::Remove(bls_sol_to_jf(key)))
.chain(
event
.added
.into_iter()
.map(|node_info| StakeTableDelta::add(node_info.into())),
.map(|node_info| StakeTableChange::Add(node_info.into())),
)
})
.group_by(|delta| delta.node_info.stake_table_key);
.group_by(|change| change.key());

// If the last event for a stakers is `Added` the staker is currently
// staking, if the last event is removed or (or the staker is not present)
// they are not staking.
let currently_staking = changes_per_node
.into_iter()
.map(|(_pub_key, deltas)| deltas.last().expect("deltas non-empty").clone())
.filter_map(|delta| match delta.change {
StakeTableChange::Add => Some(delta.node_info),
StakeTableChange::Remove => None,
.filter_map(|change| match change {
StakeTableChange::Add(node_info) => Some(node_info),
StakeTableChange::Remove(_) => None,
});

let mut consensus_stake_table: Vec<StakeTableEntry<PubKey>> = vec![];
Expand Down Expand Up @@ -103,30 +103,19 @@ pub struct EpochCommittees {

#[derive(Debug, Clone, PartialEq)]
enum StakeTableChange {
Add,
Remove,
Add(NodeInfoJf),
Remove(BLSPubKey),
}

#[derive(Debug, Clone)]
struct StakeTableDelta {
change: StakeTableChange,
node_info: NodeInfoJf,
}

impl StakeTableDelta {
fn add(node_info: NodeInfoJf) -> Self {
Self {
change: StakeTableChange::Add,
node_info,
}
}
fn remove(node_info: NodeInfoJf) -> Self {
Self {
change: StakeTableChange::Remove,
node_info,
impl StakeTableChange {
pub(crate) fn key(&self) -> BLSPubKey {
match self {
StakeTableChange::Add(node_info) => node_info.stake_table_key,
StakeTableChange::Remove(key) => *key,
}
}
}

/// Holds Stake table and da stake
#[derive(Clone, Debug)]
struct Committee {
Expand Down
13 changes: 5 additions & 8 deletions utils/src/stake_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
///
/// The initial stake table is passed to the permissioned stake table contract
/// on deployment.
use contract_bindings::permissioned_stake_table::{NodeInfo, PermissionedStakeTable};
use contract_bindings::permissioned_stake_table::{G2Point, NodeInfo, PermissionedStakeTable};
use ethers::{
middleware::SignerMiddleware,
providers::{Http, Middleware as _, Provider},
signers::{coins_bip39::English, MnemonicBuilder, Signer as _},
types::Address,
};
use hotshot::types::BLSPubKey;
use hotshot_contract_adapter::stake_table::NodeInfoJf;
use hotshot_contract_adapter::stake_table::{bls_jf_to_sol, NodeInfoJf};
use hotshot_types::network::PeerConfigKeys;
use url::Url;

Expand Down Expand Up @@ -60,7 +60,7 @@ impl From<PermissionedStakeTableConfig> for Vec<NodeInfo> {
#[serde(bound(deserialize = ""))]
pub struct PermissionedStakeTableUpdate {
#[serde(default)]
stakers_to_remove: Vec<PeerConfigKeys<BLSPubKey>>,
stakers_to_remove: Vec<BLSPubKey>,
#[serde(default)]
new_stakers: Vec<PeerConfigKeys<BLSPubKey>>,
}
Expand All @@ -80,13 +80,10 @@ impl PermissionedStakeTableUpdate {
)
}

fn stakers_to_remove(&self) -> Vec<NodeInfo> {
fn stakers_to_remove(&self) -> Vec<G2Point> {
self.stakers_to_remove
.iter()
.map(|peer_config| {
let node_info: NodeInfoJf = peer_config.clone().into();
node_info.into()
})
.map(|v| bls_jf_to_sol(*v))
.collect()
}

Expand Down

0 comments on commit 01e77fe

Please sign in to comment.