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

load stake table from toml file #2377

Merged
merged 6 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 2 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ ESPRESSO_BUILDER_ETH_ACCOUNT_INDEX=8
ESPRESSO_DEPLOYER_ACCOUNT_INDEX=9

# Contracts
ESPRESSO_SEQUENCER_LIGHT_CLIENT_PROXY_ADDRESS=0x0c8e79f3534b00d9a3d4a856b665bf4ebc22f2ba
ESPRESSO_SEQUENCER_LIGHT_CLIENT_PROXY_ADDRESS=0xf7cd8fa9b94db2aa972023b379c7f72c65e4de9d
ESPRESSO_SEQUENCER_LIGHTCLIENT_ADDRESS=$ESPRESSO_SEQUENCER_LIGHT_CLIENT_PROXY_ADDRESS
ESPRESSO_SEQUENCER_PERMISSIONED_PROVER=0x14dc79964da2c08b23698b3d3cc7ca32193d9955
SPRESSO_SEQUENCER_PERMISSIONED_STAKE_TABLE_ADDRESS=0x8ce361602b935680e8dec218b820ff5056beb7af

# Example sequencer demo private keys
ESPRESSO_DEMO_SEQUENCER_STAKING_PRIVATE_KEY_0=BLS_SIGNING_KEY~lNDh4Pn-pTAyzyprOAFdXHwhrKhEwqwtMtkD3CZF4x3o
Expand Down
1 change: 1 addition & 0 deletions .typos.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[files]
extend-exclude = [
"data/initial_stake_table.toml",
".env",
"*.json",
"**/*.pdf",
Expand Down
25 changes: 25 additions & 0 deletions data/initial_stake_table.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[[public_keys]]
stake_table_key = "BLS_VER_KEY~bQszS-QKYvUij2g20VqS8asttGSb95NrTu2PUj0uMh1CBUxNy1FqyPDjZqB29M7ZbjWqj79QkEOWkpga84AmDYUeTuWmy-0P1AdKHD3ehc-dKvei78BDj5USwXPJiDUlCxvYs_9rWYhagaq-5_LXENr78xel17spftNd5MA1Mw5U"
state_ver_key = "SCHNORR_VER_KEY~ibJCbfPOhDoURqiGLe683TDJ_KOLQCx8_Hdq43dOviSuL6WJJ_2mARKO3xA2k5zpXE3iiq4_z7mzvA-V1VXvIWw"
da = true

[[public_keys]]
stake_table_key = "BLS_VER_KEY~4zQnaCOFJ7m95OjxeNls0QOOwWbz4rfxaL3NwmN2zSdnf8t5Nw_dfmMHq05ee8jCegw6Bn5T8inmrnGGAsQJMMWLv77nd7FJziz2ViAbXg-XGGF7o4HyzELCmypDOIYF3X2UWferFE_n72ZX0iQkUhOvYZZ7cfXToXxRTtb_mwRR"
state_ver_key = "SCHNORR_VER_KEY~lNCMqH5qLthH5OXxW_Z25tLXJUqmzzhsuQ6oVuaPWhtRPmgIKSqcBoJTaEbmGZL2VfTyQNguaoQL4U_4tCA_HmI"
da = true

[[public_keys]]
stake_table_key = "BLS_VER_KEY~IBRoz_Q1EXvcm1pNZcmVlyYZU8hZ7qmy337ePAjEMhz8Hl2q8vWPFOd3BaLwgRS1UzAPW3z4E-XIgRDGcRBTAMZX9b_0lKYjlyTlNF2EZfNnKmvv-xJ0yurkfjiveeYEsD2l5d8q_rJJbH1iZdXy-yPEbwI0SIvQfwdlcaKw9po4"
state_ver_key = "SCHNORR_VER_KEY~nkFKzpLhJAafJ3LBkY_0h9OzxSyTu95Z029EUFPO4QNkeUo6DHQGTTVjxmprTA5H8jRSn73i0slJvig6dZ5kLX4"
da = true

[[public_keys]]
stake_table_key = "BLS_VER_KEY~rO2PIjyY30HGfapFcloFe3mNDKMIFi6JlOLkH5ZWBSYoRm5fE2-Rm6Lp3EvmAcB5r7KFJ0c1Uor308x78r04EY_sfjcsDCWt7RSJdL4cJoD_4fSTCv_bisO8k98hs_8BtqQt8BHlPeJohpUXvcfnK8suXJETiJ6Er97pfxRbzgAL"
state_ver_key = "SCHNORR_VER_KEY~NwYhzlWarlZHxTNvChWuf74O3fP7zIt5NdC7V8gV6w2W92JOBDkrNmKQeMGxMUke-G5HHxUjHlZEWr1m1xLjEaI"
da = false


[[public_keys]]
stake_table_key = "BLS_VER_KEY~r6b-Cwzp-b3czlt0MHmYPJIow5kMsXbrNmZsLSYg9RV49oCCO4WEeCRFR02x9bqLCa_sgNFMrIeNdEa11qNiBAohApYFIvrSa-zP5QGj3xbZaMOCrshxYit6E2TR-XsWvv6gjOrypmugjyTAth-iqQzTboSfmO9DD1-gjJIdCaD7"
state_ver_key = "SCHNORR_VER_KEY~qMfMj1c1hRVTnugvz3MKNnVC5JA9jvZcV3ZCLL_J4Ap-u0i6ulGWveTk3OOelZj2-kd_WD5ojtYGWV1jHx9wCaA"
da = true
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ services:

deploy-sequencer-contracts:
image: ghcr.io/espressosystems/espresso-sequencer/deploy:main
command: deploy --only sequencer,permissioned-stake-table
command: deploy --only fee-contract,permissioned-stake-table
environment:
- ESPRESSO_SEQUENCER_ETH_MULTISIG_ADDRESS
- ESPRESSO_SEQUENCER_L1_PROVIDER
Expand Down
1 change: 1 addition & 0 deletions process-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ environment:
- ESPRESSO_SEQUENCER_L1_PROVIDER=http://localhost:$ESPRESSO_SEQUENCER_L1_PORT
- ESPRESSO_SEQUENCER_GENESIS_FILE=data/genesis/demo.toml
- ESPRESSO_BUILDER_GENESIS_FILE=data/genesis/demo.toml
- ESPRESSO_SEQUENCER_INITIAL_PERMISSIONED_STAKE_TABLE_PATH=data/initial_stake_table.toml
- ESPRESSO_STATE_RELAY_SERVER_URL=http://localhost:$ESPRESSO_STATE_RELAY_SERVER_PORT
- QUERY_SERVICE_URI=http://localhost:$ESPRESSO_SEQUENCER1_API_PORT/v0/
- NODE_VALIDATOR_URI=ws://localhost:$ESPRESSO_NODE_VALIDATOR_PORT/v0/
Expand Down
13 changes: 11 additions & 2 deletions sequencer-sqlite/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

149 changes: 148 additions & 1 deletion utils/src/stake_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
use contract_bindings::permissioned_stake_table::NodeInfo;
use hotshot::types::BLSPubKey;
use hotshot_contract_adapter::stake_table::NodeInfoJf;
use hotshot_types::network::PeerConfigKeys;
use hotshot_types::{light_client::StateVerKey, network::PeerConfigKeys};
use serde::{
de::{SeqAccess, Visitor},
Deserializer,
};
use std::{fs, path::Path};

/// A stake table config stored in a file
Expand All @@ -15,9 +19,64 @@ pub struct PermissionedStakeTableConfig {
/// The list of public keys that are initially inserted into the
/// permissioned stake table contract.
#[serde(default)]
// Custom deserialization to handle toml file where the "stake" field is not provided.
// Defaults the "stake" field to 1 if not specified.
#[serde(deserialize_with = "deserialize_peer_config_keys")]
pub public_keys: Vec<PeerConfigKeys<BLSPubKey>>,
}

fn deserialize_peer_config_keys<'de, D>(
Copy link
Collaborator

@sveitser sveitser Dec 10, 2024

Choose a reason for hiding this comment

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

@imabdulbasit I think I prefer to add the dummy stake amount to the toml file (which the current orchestrator config also requires anyway) instead of adding the custom deserialization code just for this. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

SGTM! I'll remove it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

deserializer: D,
) -> Result<Vec<PeerConfigKeys<BLSPubKey>>, D::Error>
where
D: Deserializer<'de>,
{
struct PeerConfigKeysVisitor;

// We deserialize the toml file entries into this struct which contains optional stake field.
// which is then converted into `PeerConfigKeys`
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct PeerConfigKeysOptionalStakeField {
pub stake_table_key: BLSPubKey,
pub state_ver_key: StateVerKey,
pub stake: Option<u64>,
pub da: bool,
}

impl<'de> Visitor<'de> for PeerConfigKeysVisitor {
type Value = Vec<PeerConfigKeys<BLSPubKey>>;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("peer config keys")
}

fn visit_seq<A>(self, mut seq: A) -> Result<Vec<PeerConfigKeys<BLSPubKey>>, A::Error>
where
A: SeqAccess<'de>,
{
let mut keys = vec![];
while let Some(PeerConfigKeysOptionalStakeField {
stake_table_key,
state_ver_key,
stake,
da,
}) = seq.next_element::<PeerConfigKeysOptionalStakeField>()?
{
keys.push(PeerConfigKeys {
stake_table_key,
state_ver_key,
stake: stake.unwrap_or(1),
da,
});
}

Ok(keys)
}
}

deserializer.deserialize_seq(PeerConfigKeysVisitor)
}

impl PermissionedStakeTableConfig {
pub fn from_toml_file(path: &Path) -> anyhow::Result<Self> {
let config_file_as_string: String = fs::read_to_string(path)
Expand Down Expand Up @@ -46,3 +105,91 @@ impl From<PermissionedStakeTableConfig> for Vec<NodeInfo> {
.collect()
}
}

#[cfg(test)]
mod test {
use crate::stake_table::PermissionedStakeTableConfig;
use crate::test_utils::setup_test;
use hotshot::types::{BLSPubKey, SignatureKey};
use hotshot_types::{light_client::StateKeyPair, network::PeerConfigKeys};
use toml::toml;
#[test]
fn test_permissioned_stake_table_from_toml() {
setup_test();

let mut keys = Vec::new();
for i in 0..3 {
let (pubkey, _) = BLSPubKey::generated_from_seed_indexed([0; 32], i);
let state_kp = StateKeyPair::generate_from_seed_indexed([0; 32], i).0;
let ver_key = state_kp.ver_key();
keys.push(PeerConfigKeys {
stake_table_key: pubkey,
state_ver_key: ver_key,
stake: 1,
da: i == 0,
});
}

let st_key_1 = keys[0].stake_table_key.to_string();
let verkey_1 = keys[0].state_ver_key.to_string();
let da_1 = keys[0].da;

let st_key_2 = keys[1].stake_table_key.to_string();
let verkey_2 = keys[1].state_ver_key.to_string();
let da_2 = keys[1].da;

let st_key_3 = keys[2].stake_table_key.to_string();
let verkey_3 = keys[2].state_ver_key.to_string();
let da_3 = keys[2].da;

let toml = toml! {
[[public_keys]]
stake_table_key = st_key_1
state_ver_key = verkey_1
stake = 1
da = da_1

[[public_keys]]
stake_table_key = st_key_2
state_ver_key = verkey_2
stake = 1
da = da_2

[[public_keys]]
stake_table_key = st_key_3
state_ver_key = verkey_3
da = da_3

}
.to_string();

let toml_st: PermissionedStakeTableConfig = toml::from_str(&toml).unwrap();

assert_eq!(toml_st.public_keys.len(), 3);

// TODO: add `PartialEq` to PeerConfigKeys
assert_eq!(toml_st.public_keys[0].state_ver_key, keys[0].state_ver_key);
assert_eq!(
toml_st.public_keys[0].stake_table_key,
keys[0].stake_table_key
);
assert_eq!(toml_st.public_keys[0].da, da_1);
assert_eq!(toml_st.public_keys[0].stake, 1);

assert_eq!(toml_st.public_keys[1].state_ver_key, keys[1].state_ver_key);
assert_eq!(
toml_st.public_keys[1].stake_table_key,
keys[1].stake_table_key
);
assert_eq!(toml_st.public_keys[1].da, da_2);
assert_eq!(toml_st.public_keys[1].stake, 1);

assert_eq!(toml_st.public_keys[2].state_ver_key, keys[2].state_ver_key);
assert_eq!(
toml_st.public_keys[2].stake_table_key,
keys[2].stake_table_key
);
assert_eq!(toml_st.public_keys[2].da, da_3);
assert_eq!(toml_st.public_keys[2].stake, 1);
}
}
Loading