/
test.rs
118 lines (109 loc) · 3.81 KB
/
test.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use super::*;
use crate::chaintypes::ChainLength;
use crate::header::{BftProof, BftSignature, Common, GenesisPraosProof, KesSignature};
use crate::key::BftLeaderId;
#[cfg(test)]
use crate::testing::serialization::serialization_bijection_r;
use chain_crypto::{
self, AsymmetricKey, Ed25519, RistrettoGroup2HashDh, SecretKey, SumEd25519_12,
VerifiableRandomFunction,
};
use lazy_static::lazy_static;
#[cfg(test)]
use quickcheck::TestResult;
use quickcheck::{Arbitrary, Gen};
quickcheck! {
fn header_serialization_bijection(b: Header) -> TestResult {
serialization_bijection_r(b)
}
}
impl Arbitrary for BlockVersion {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
BlockVersion::from_u16(u16::arbitrary(g) % 3).unwrap()
}
}
impl Arbitrary for AnyBlockVersion {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
u16::arbitrary(g).into()
}
}
impl Arbitrary for Common {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
Common {
block_version: Arbitrary::arbitrary(g),
block_date: Arbitrary::arbitrary(g),
block_content_size: Arbitrary::arbitrary(g),
block_content_hash: Arbitrary::arbitrary(g),
block_parent_hash: Arbitrary::arbitrary(g),
chain_length: ChainLength(Arbitrary::arbitrary(g)),
}
}
}
impl Arbitrary for BftProof {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
let sk: chain_crypto::SecretKey<Ed25519> = Arbitrary::arbitrary(g);
let pk = sk.to_public();
let signature = sk.sign(&[0u8, 1, 2, 3]);
BftProof {
leader_id: BftLeaderId(pk),
signature: BftSignature(signature.coerce()),
}
}
}
impl Arbitrary for GenesisPraosProof {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
use chain_crypto::testing;
let tcg = testing::TestCryptoGen::arbitrary(g);
let node_id = Arbitrary::arbitrary(g);
let vrf_proof = {
let sk = RistrettoGroup2HashDh::generate(&mut tcg.get_rng(0));
RistrettoGroup2HashDh::evaluate_and_prove(&sk, &[0, 1, 2, 3], &mut tcg.get_rng(1))
};
let kes_proof = {
lazy_static! {
static ref SK_FIRST: SecretKey<SumEd25519_12> = testing::static_secret_key();
}
let sk = SK_FIRST.clone();
let signature = sk.sign(&[0u8, 1, 2, 3]);
KesSignature(signature.coerce())
};
GenesisPraosProof {
node_id,
vrf_proof: vrf_proof.into(),
kes_proof,
}
}
}
impl Arbitrary for Header {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
let common = Common::arbitrary(g);
let hdrbuilder = HeaderBuilderNew::new_raw(
common.block_version,
&common.block_content_hash,
common.block_content_size,
)
.set_parent(&common.block_parent_hash, common.chain_length)
.set_date(common.block_date);
match common.block_version {
BlockVersion::Genesis => hdrbuilder.into_unsigned_header().unwrap().generalize(),
BlockVersion::Ed25519Signed => {
let bft_proof: BftProof = Arbitrary::arbitrary(g);
hdrbuilder
.into_bft_builder()
.unwrap()
.set_consensus_data(&bft_proof.leader_id)
.set_signature(bft_proof.signature)
.generalize()
}
BlockVersion::KesVrfproof => {
let gp_proof: GenesisPraosProof = Arbitrary::arbitrary(g);
hdrbuilder
.into_genesis_praos_builder()
.unwrap()
.set_consensus_data(&gp_proof.node_id, &gp_proof.vrf_proof)
.set_signature(gp_proof.kes_proof)
.generalize()
}
}
}
}