Skip to content

Commit

Permalink
Add test for jf / contract types conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
sveitser committed Dec 9, 2024
1 parent 94a07a4 commit a55cfd6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions contracts/rust/adapter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ num-bigint = { version = "0.4", default-features = false }
num-traits = { version = "0.2", default-features = false }
serde = { workspace = true }

[dev-dependencies]
rand = { workspace = true }

[[bin]]
name = "eval-domain"
path = "src/bin/eval_domain.rs"
36 changes: 35 additions & 1 deletion contracts/rust/adapter/src/stake_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ where
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct NodeInfoJf {
pub stake_table_key: BLSPubKey,
pub state_ver_key: StateVerKey,
Expand Down Expand Up @@ -189,3 +189,37 @@ impl From<PeerConfigKeys<BLSPubKey>> for NodeInfoJf {
}
}
}

#[cfg(test)]
mod test {
use hotshot_types::{light_client::StateKeyPair, traits::signature_key::BuilderSignatureKey};
use rand::{Rng, RngCore};

use super::*;

impl NodeInfoJf {
fn random() -> Self {
let mut seed = [0u8; 32];
let mut rng = rand::thread_rng();
rng.fill_bytes(&mut seed);

let (stake_table_key, _) = BLSPubKey::generated_from_seed_indexed(seed, 0);
let state_key_pair = StateKeyPair::generate_from_seed_indexed(seed, 0);
Self {
stake_table_key,
state_ver_key: state_key_pair.ver_key(),
da: rng.gen(),
}
}
}

#[test]
fn test_node_info_round_trip() {
for _ in 0..20 {
let jf = NodeInfoJf::random();
let sol: NodeInfo = jf.clone().into();
let jf2: NodeInfoJf = sol.into();
assert_eq!(jf2, jf);
}
}
}

0 comments on commit a55cfd6

Please sign in to comment.