Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Sassafras protocol v0.3.3 #14362

Merged
Prev Previous commit
Next Next commit
Fix (broken) tests build
  • Loading branch information
davxy committed Jun 16, 2023
commit f3691875b766b30175078eb74ebe6f9e0a7ba73d
14 changes: 7 additions & 7 deletions client/consensus/sassafras/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ fn create_test_config() -> SassafrasConfiguration {
slot_duration: SLOT_DURATION,
epoch_duration: EPOCH_DURATION,
authorities: vec![
(Keyring::Alice.public().into(), 1),
(Keyring::Bob.public().into(), 1),
(Keyring::Charlie.public().into(), 1),
Keyring::Alice.public().into(),
Keyring::Bob.public().into(),
Keyring::Charlie.public().into(),
],
randomness: [0; 32],
threshold_params: SassafrasEpochConfiguration { redundancy_factor: 1, attempts_number: 32 },
Expand Down Expand Up @@ -426,7 +426,7 @@ fn claim_primary_slots_works() {
let alice_authority_idx = 0_u32;

let ticket_id = 123;
let ticket_data = TicketData { attempt_idx: 0, erased_public: [0; 32] };
let ticket_body = TicketBody { attempt_idx: 0, erased_public: [0; 32] };
let ticket_secret = TicketSecret { attempt_idx: 0, erased_secret: [0; 32] };

// Fail if we have authority key in our keystore but not ticket aux data
Expand All @@ -435,7 +435,7 @@ fn claim_primary_slots_works() {
let claim = authorship::claim_slot(
0.into(),
&mut epoch,
Some((ticket_id, ticket_data.clone())),
Some((ticket_id, ticket_body.clone())),
&keystore,
);

Expand All @@ -452,7 +452,7 @@ fn claim_primary_slots_works() {
let (pre_digest, auth_id) = authorship::claim_slot(
0.into(),
&mut epoch,
Some((ticket_id, ticket_data.clone())),
Some((ticket_id, ticket_body.clone())),
&keystore,
)
.unwrap();
Expand All @@ -467,7 +467,7 @@ fn claim_primary_slots_works() {
epoch.tickets_aux.insert(ticket_id, (alice_authority_idx + 1, ticket_secret));

let claim =
authorship::claim_slot(0.into(), &mut epoch, Some((ticket_id, ticket_data)), &keystore);
authorship::claim_slot(0.into(), &mut epoch, Some((ticket_id, ticket_body)), &keystore);
assert!(claim.is_none());
assert!(epoch.tickets_aux.is_empty());
}
Expand Down
61 changes: 28 additions & 33 deletions frame/sassafras/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use crate::{self as pallet_sassafras, SameAuthoritiesForever};
use frame_support::traits::{ConstU32, ConstU64, GenesisBuild, OnFinalize, OnInitialize};
use scale_codec::Encode;
use sp_consensus_sassafras::{
digests::PreDigest, AuthorityIndex, AuthorityPair, RingProver, RingVrfContext,
SassafrasEpochConfiguration, Slot, TicketData, TicketEnvelope, VrfSignature,
digests::PreDigest, AuthorityIndex, AuthorityPair, RingProver, SassafrasEpochConfiguration,
Slot, TicketBody, TicketEnvelope, VrfSignature,
};
use sp_core::{
crypto::{Pair, VrfSecret},
Expand Down Expand Up @@ -159,55 +159,50 @@ fn make_ticket_with_prover(
randomness = crate::NextRandomness::<Test>::get();
}

let body = TicketBody { attempt_idx: attempt, erased_public: [0; 32] };

let mut sign_data = sp_consensus_sassafras::ticket_body_sign_data(&body);

let vrf_input = sp_consensus_sassafras::ticket_id_vrf_input(&randomness, attempt, epoch);
let vrf_preout = pair.as_ref().vrf_output(&vrf_input.into());
sign_data.push_vrf_input(vrf_input).unwrap();

let ring_signature = pair.as_ref().ring_vrf_sign(&sign_data, &prover);

// Ticket-id can be generated via vrf-preout.
// We don't care that much about the value here.

let data = TicketData { attempt_idx: attempt, erased_public: [0; 32] };

let sign_data = sp_consensus_sassafras::ticket_body_sign_data(&data);
let ring_signature = pair.as_ref().ring_vrf_sign(&sign_data, prover);

TicketEnvelope { data, vrf_preout, ring_signature }
TicketEnvelope { body, ring_signature }
}

pub fn make_prover(_pair: &AuthorityPair, ring_ctx: &RingVrfContext) -> RingProver {
let authorities = Sassafras::authorities();
let pks: Vec<sp_core::bandersnatch::Public> =
authorities.iter().map(|auth| *auth.as_ref()).collect();
pub fn make_prover(pair: &AuthorityPair) -> RingProver {
let public = pair.public();
let mut prover_idx = None;

// TODO davxy: search into pks for pair.public
let prover_idx = 0;
let ring_ctx = Sassafras::ring_context().unwrap();

let pks: Vec<sp_core::bandersnatch::Public> = Sassafras::authorities()
.iter()
.enumerate()
.map(|(idx, auth)| {
if public == *auth {
prover_idx = Some(idx);
}
*auth.as_ref()
})
.collect();

println!("Make prover");
let prover = ring_ctx.prover(&pks, prover_idx).unwrap();
let prover = ring_ctx.prover(&pks, prover_idx.unwrap()).unwrap();
println!("Done");

prover
}

// pub fn make_ticket(
// slot: Slot,
// attempt: u32,
// pair: &AuthorityPair,
// ring_ctx: &RingVrfContext,
// ) -> TicketEnvelope {
// let prover = make_prover(pair, ring_ctx);
// make_ticket_with_prover(slot, attempt, pair, &prover)
// }

/// Construct at most `attempts` tickets envelopes for the given `slot`.
/// TODO-SASS-P3: filter out invalid tickets according to test threshold.
/// E.g. by passing an optional threshold
pub fn make_tickets(
slot: Slot,
attempts: u32,
pair: &AuthorityPair,
ring_ctx: &RingVrfContext,
) -> Vec<TicketEnvelope> {
let prover = make_prover(pair, ring_ctx);
pub fn make_tickets(slot: Slot, attempts: u32, pair: &AuthorityPair) -> Vec<TicketEnvelope> {
let prover = make_prover(pair);
(0..attempts)
.into_iter()
.map(|attempt| make_ticket_with_prover(slot, attempt, pair, &prover))
Expand Down
Loading