#![cfg(feature = "trust")]
use nwep::trust::{self, bls::BlsKeypair, AnchorNode, CheckpointStatus, TrustStore};
use nwep::Identity;
use std::time::Duration;
const EPOCH: u64 = 1;
const EPOCH_SECS: i64 = 3600;
const LOG_SIZE: u64 = 128;
#[test]
fn anchors_produce_aggregate_and_verify_a_checkpoint() {
let root = [0xABu8; 32];
let bls_keys: Vec<BlsKeypair> = (0..3).map(|_| BlsKeypair::generate().unwrap()).collect();
let mut anchors: Vec<AnchorNode> = bls_keys
.iter()
.enumerate()
.map(|(i, bls)| {
let id = Identity::generate().unwrap();
AnchorNode::new(&id, bls, (i + 1) as u8, Duration::from_secs(3300)).unwrap()
})
.collect();
let partials: Vec<_> = anchors
.iter_mut()
.map(|a| {
a.collect_log_root(EPOCH, &root, LOG_SIZE, &root).unwrap();
a.produce_partial_sig(EPOCH, &root, LOG_SIZE).unwrap()
})
.collect();
assert_eq!(
partials.iter().map(|p| p.index()).collect::<Vec<_>>(),
vec![1, 2, 3]
);
let anchor_pubkeys: Vec<[u8; 48]> = bls_keys.iter().map(|k| *k.public_key()).collect();
let checkpoint =
trust::finish_checkpoint(EPOCH, &root, LOG_SIZE, &partials, &anchor_pubkeys).unwrap();
assert!(!checkpoint.is_empty());
let mut store = TrustStore::new().unwrap();
store.load_genesis_anchors(&anchor_pubkeys).unwrap();
let now = EPOCH as i64 * EPOCH_SECS; assert!(store.verify_checkpoint(&checkpoint, now).is_ok());
assert_eq!(
store.update_checkpoint(&checkpoint, now).unwrap(),
CheckpointStatus::Fresh
);
let forged =
trust::finish_checkpoint(EPOCH, &[0x11u8; 32], LOG_SIZE, &partials, &anchor_pubkeys);
let rejected = match forged {
Err(_) => true,
Ok(bytes) => store.verify_checkpoint(&bytes, now).is_err(),
};
assert!(rejected, "a checkpoint over a forged root must not verify");
}