Skip to content

Commit

Permalink
remove custom deser
Browse files Browse the repository at this point in the history
  • Loading branch information
imabdulbasit committed Dec 10, 2024
1 parent 6ab9a81 commit 09ea796
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 62 deletions.
7 changes: 6 additions & 1 deletion data/initial_stake_table.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,29 @@
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
stake = 1

[[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
stake = 1

[[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
stake = 1

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


[[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
da = true
stake = 1
65 changes: 4 additions & 61 deletions utils/src/stake_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
use contract_bindings::permissioned_stake_table::NodeInfo;
use hotshot::types::BLSPubKey;
use hotshot_contract_adapter::stake_table::NodeInfoJf;
use hotshot_types::{light_client::StateVerKey, network::PeerConfigKeys};
use serde::{
de::{SeqAccess, Visitor},
Deserializer,
};
use hotshot_types::network::PeerConfigKeys;

use std::{fs, path::Path};

/// A stake table config stored in a file
Expand All @@ -19,64 +16,9 @@ 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>(
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 @@ -158,6 +100,7 @@ mod test {
[[public_keys]]
stake_table_key = st_key_3
state_ver_key = verkey_3
stake = 2
da = da_3

}
Expand Down Expand Up @@ -190,6 +133,6 @@ mod test {
keys[2].stake_table_key
);
assert_eq!(toml_st.public_keys[2].da, da_3);
assert_eq!(toml_st.public_keys[2].stake, 1);
assert_eq!(toml_st.public_keys[2].stake, 2);
}
}

0 comments on commit 09ea796

Please sign in to comment.