|
| 1 | +use crate::{ |
| 2 | + is_equivocation_proof_valid, CompatibleDigestItem, EquivocationProof, FarmerPublicKey, |
| 3 | + FarmerSignature, |
| 4 | +}; |
| 5 | +use schnorrkel::Keypair; |
| 6 | +use sp_consensus_slots::Slot; |
| 7 | +use sp_core::crypto::UncheckedFrom; |
| 8 | +use sp_runtime::traits::BlakeTwo256; |
| 9 | +use sp_runtime::{Digest, DigestItem}; |
| 10 | +use subspace_core_primitives::{LocalChallenge, Solution, TagSignature}; |
| 11 | +use subspace_solving::REWARD_SIGNING_CONTEXT; |
| 12 | + |
| 13 | +type Header = sp_runtime::generic::Header<u32, BlakeTwo256>; |
| 14 | +type PreDigest = crate::PreDigest<FarmerPublicKey, ()>; |
| 15 | + |
| 16 | +#[test] |
| 17 | +fn test_is_equivocation_proof_valid() { |
| 18 | + let keypair = Keypair::generate(); |
| 19 | + let offender = FarmerPublicKey::unchecked_from(keypair.public.to_bytes()); |
| 20 | + let slot = Slot::from(1); |
| 21 | + let solution = Solution { |
| 22 | + public_key: offender.clone(), |
| 23 | + reward_address: (), |
| 24 | + piece_index: 0, |
| 25 | + encoding: Default::default(), |
| 26 | + tag_signature: TagSignature { |
| 27 | + output: Default::default(), |
| 28 | + proof: [0u8; 64], |
| 29 | + }, |
| 30 | + local_challenge: LocalChallenge { |
| 31 | + output: Default::default(), |
| 32 | + proof: [0u8; 64], |
| 33 | + }, |
| 34 | + tag: [0u8; 8], |
| 35 | + }; |
| 36 | + |
| 37 | + let mut first_header = Header { |
| 38 | + parent_hash: [0u8; 32].into(), |
| 39 | + number: 1, |
| 40 | + state_root: Default::default(), |
| 41 | + extrinsics_root: Default::default(), |
| 42 | + digest: Digest { |
| 43 | + logs: vec![DigestItem::subspace_pre_digest(&PreDigest { |
| 44 | + slot, |
| 45 | + solution: solution.clone(), |
| 46 | + })], |
| 47 | + }, |
| 48 | + }; |
| 49 | + first_header |
| 50 | + .digest |
| 51 | + .logs |
| 52 | + .push(DigestItem::subspace_seal(FarmerSignature::unchecked_from( |
| 53 | + keypair |
| 54 | + .sign( |
| 55 | + schnorrkel::context::signing_context(REWARD_SIGNING_CONTEXT) |
| 56 | + .bytes(first_header.hash().as_bytes()), |
| 57 | + ) |
| 58 | + .to_bytes(), |
| 59 | + ))); |
| 60 | + |
| 61 | + let mut second_header = Header { |
| 62 | + parent_hash: [1u8; 32].into(), |
| 63 | + number: 1, |
| 64 | + state_root: Default::default(), |
| 65 | + extrinsics_root: Default::default(), |
| 66 | + digest: Digest { |
| 67 | + logs: vec![DigestItem::subspace_pre_digest(&PreDigest { |
| 68 | + slot, |
| 69 | + solution, |
| 70 | + })], |
| 71 | + }, |
| 72 | + }; |
| 73 | + second_header |
| 74 | + .digest |
| 75 | + .logs |
| 76 | + .push(DigestItem::subspace_seal(FarmerSignature::unchecked_from( |
| 77 | + keypair |
| 78 | + .sign( |
| 79 | + schnorrkel::context::signing_context(REWARD_SIGNING_CONTEXT) |
| 80 | + .bytes(second_header.hash().as_bytes()), |
| 81 | + ) |
| 82 | + .to_bytes(), |
| 83 | + ))); |
| 84 | + |
| 85 | + let equivocation_proof = EquivocationProof { |
| 86 | + offender, |
| 87 | + slot, |
| 88 | + first_header, |
| 89 | + second_header, |
| 90 | + }; |
| 91 | + |
| 92 | + assert!(is_equivocation_proof_valid::<_, ()>(equivocation_proof)); |
| 93 | +} |
0 commit comments