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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// Copyright (c) 2021-2024 Espresso Systems (espressosys.com)
// This file is part of the HotShot repository.

// You should have received a copy of the MIT License
// along with the HotShot repository. If not, see <https://mit-license.org/>.

#![allow(clippy::panic)]
use std::{fmt::Debug, hash::Hash, marker::PhantomData, sync::Arc};

use async_broadcast::{Receiver, Sender};
use async_lock::RwLock;
use bitvec::bitvec;
use committable::Committable;
use hotshot::{
    traits::{NodeImplementation, TestableNodeImplementation},
    types::{SignatureKey, SystemContextHandle},
    HotShotInitializer, SystemContext,
};
use hotshot_example_types::{
    auction_results_provider_types::TestAuctionResultsProvider,
    block_types::TestTransaction,
    node_types::TestTypes,
    state_types::{TestInstanceState, TestValidatedState},
    storage_types::TestStorage,
};
use hotshot_task_impls::events::HotShotEvent;
use hotshot_types::{
    consensus::ConsensusMetricsValue,
    data::{Leaf2, QuorumProposal2, VidDisperse, VidDisperseShare2},
    message::{GeneralConsensusMessage, Proposal, UpgradeLock},
    simple_certificate::DaCertificate2,
    simple_vote::{DaData2, DaVote2, QuorumData2, QuorumVote2, SimpleVote, VersionedVoteData},
    traits::{
        block_contents::vid_commitment,
        consensus_api::ConsensusApi,
        election::Membership,
        node_implementation::{ConsensusTime, NodeType, Versions},
    },
    utils::{epoch_from_block_number, View, ViewInner},
    vid::{vid_scheme, VidCommitment, VidProposal, VidSchemeType},
    vote::{Certificate, HasViewNumber, Vote},
    ValidatorConfig,
};
use jf_vid::VidScheme;
use primitive_types::U256;
use serde::Serialize;

use crate::{test_builder::TestDescription, test_launcher::TestLauncher};

/// create the [`SystemContextHandle`] from a node id
/// # Panics
/// if cannot create a [`HotShotInitializer`]
pub async fn build_system_handle<
    TYPES: NodeType<InstanceState = TestInstanceState>,
    I: NodeImplementation<
            TYPES,
            Storage = TestStorage<TYPES>,
            AuctionResultsProvider = TestAuctionResultsProvider<TYPES>,
        > + TestableNodeImplementation<TYPES>,
    V: Versions,
>(
    node_id: u64,
) -> (
    SystemContextHandle<TYPES, I, V>,
    Sender<Arc<HotShotEvent<TYPES>>>,
    Receiver<Arc<HotShotEvent<TYPES>>>,
) {
    let builder: TestDescription<TYPES, I, V> = TestDescription::default_multiple_rounds();

    let launcher = builder.gen_launcher(node_id);
    build_system_handle_from_launcher(node_id, &launcher).await
}

/// create the [`SystemContextHandle`] from a node id and `TestLauncher`
/// # Panics
/// if cannot create a [`HotShotInitializer`]
pub async fn build_system_handle_from_launcher<
    TYPES: NodeType<InstanceState = TestInstanceState>,
    I: NodeImplementation<
            TYPES,
            Storage = TestStorage<TYPES>,
            AuctionResultsProvider = TestAuctionResultsProvider<TYPES>,
        > + TestableNodeImplementation<TYPES>,
    V: Versions,
>(
    node_id: u64,
    launcher: &TestLauncher<TYPES, I, V>,
) -> (
    SystemContextHandle<TYPES, I, V>,
    Sender<Arc<HotShotEvent<TYPES>>>,
    Receiver<Arc<HotShotEvent<TYPES>>>,
) {
    let network = (launcher.resource_generator.channel_generator)(node_id).await;
    let storage = (launcher.resource_generator.storage)(node_id);
    let marketplace_config = (launcher.resource_generator.marketplace_config)(node_id);
    let config = launcher.resource_generator.config.clone();

    let initializer = HotShotInitializer::<TYPES>::from_genesis::<V>(TestInstanceState::new(
        launcher.metadata.async_delay_config.clone(),
    ))
    .await
    .unwrap();

    // See whether or not we should be DA
    let is_da = node_id < config.da_staked_committee_size as u64;

    // We assign node's public key and stake value rather than read from config file since it's a test
    let validator_config: ValidatorConfig<TYPES::SignatureKey> =
        ValidatorConfig::generated_from_seed_indexed([0u8; 32], node_id, 1, is_da);
    let private_key = validator_config.private_key.clone();
    let public_key = validator_config.public_key.clone();

    let memberships = Arc::new(RwLock::new(TYPES::Membership::new(
        config.known_nodes_with_stake.clone(),
        config.known_da_nodes.clone(),
    )));

    SystemContext::init(
        public_key,
        private_key,
        node_id,
        config,
        memberships,
        network,
        initializer,
        ConsensusMetricsValue::default(),
        storage,
        marketplace_config,
    )
    .await
    .expect("Could not init hotshot")
}

/// create certificate
/// # Panics
/// if we fail to sign the data
pub async fn build_cert<
    TYPES: NodeType,
    V: Versions,
    DATAType: Committable + Clone + Eq + Hash + Serialize + Debug + 'static,
    VOTE: Vote<TYPES, Commitment = DATAType>,
    CERT: Certificate<TYPES, VOTE::Commitment, Voteable = VOTE::Commitment>,
>(
    data: DATAType,
    membership: &Arc<RwLock<TYPES::Membership>>,
    view: TYPES::View,
    epoch: TYPES::Epoch,
    public_key: &TYPES::SignatureKey,
    private_key: &<TYPES::SignatureKey as SignatureKey>::PrivateKey,
    upgrade_lock: &UpgradeLock<TYPES, V>,
) -> CERT {
    let real_qc_sig = build_assembled_sig::<TYPES, V, VOTE, CERT, DATAType>(
        &data,
        membership,
        view,
        epoch,
        upgrade_lock,
    )
    .await;

    let vote = SimpleVote::<TYPES, DATAType>::create_signed_vote(
        data,
        view,
        public_key,
        private_key,
        upgrade_lock,
    )
    .await
    .expect("Failed to sign data!");

    let vote_commitment =
        VersionedVoteData::new(vote.date().clone(), vote.view_number(), upgrade_lock)
            .await
            .expect("Failed to create VersionedVoteData!")
            .commit();

    let cert = CERT::create_signed_certificate(
        vote_commitment,
        vote.date().clone(),
        real_qc_sig,
        vote.view_number(),
    );
    cert
}

pub fn vid_share<TYPES: NodeType>(
    shares: &[Proposal<TYPES, VidDisperseShare2<TYPES>>],
    pub_key: TYPES::SignatureKey,
) -> Proposal<TYPES, VidDisperseShare2<TYPES>> {
    shares
        .iter()
        .filter(|s| s.data.recipient_key == pub_key)
        .cloned()
        .collect::<Vec<_>>()
        .first()
        .expect("No VID for key")
        .clone()
}

/// create signature
/// # Panics
/// if fails to convert node id into keypair
pub async fn build_assembled_sig<
    TYPES: NodeType,
    V: Versions,
    VOTE: Vote<TYPES>,
    CERT: Certificate<TYPES, VOTE::Commitment, Voteable = VOTE::Commitment>,
    DATAType: Committable + Clone + Eq + Hash + Serialize + Debug + 'static,
>(
    data: &DATAType,
    membership: &Arc<RwLock<TYPES::Membership>>,
    view: TYPES::View,
    epoch: TYPES::Epoch,
    upgrade_lock: &UpgradeLock<TYPES, V>,
) -> <TYPES::SignatureKey as SignatureKey>::QcType {
    let membership_reader = membership.read().await;
    let stake_table = CERT::stake_table(&*membership_reader, epoch);
    let real_qc_pp: <TYPES::SignatureKey as SignatureKey>::QcParams =
        <TYPES::SignatureKey as SignatureKey>::public_parameter(
            stake_table.clone(),
            U256::from(CERT::threshold(&*membership_reader, epoch)),
        );
    drop(membership_reader);

    let total_nodes = stake_table.len();
    let signers = bitvec![1; total_nodes];
    let mut sig_lists = Vec::new();

    // assemble the vote
    for node_id in 0..total_nodes {
        let (private_key_i, public_key_i) = key_pair_for_id::<TYPES>(node_id.try_into().unwrap());
        let vote: SimpleVote<TYPES, DATAType> = SimpleVote::<TYPES, DATAType>::create_signed_vote(
            data.clone(),
            view,
            &public_key_i,
            &private_key_i,
            upgrade_lock,
        )
        .await
        .expect("Failed to sign data!");
        let original_signature: <TYPES::SignatureKey as SignatureKey>::PureAssembledSignatureType =
            vote.signature();
        sig_lists.push(original_signature);
    }

    let real_qc_sig = <TYPES::SignatureKey as SignatureKey>::assemble(
        &real_qc_pp,
        signers.as_bitslice(),
        &sig_lists[..],
    );

    real_qc_sig
}

/// get the keypair for a node id
#[must_use]
pub fn key_pair_for_id<TYPES: NodeType>(
    node_id: u64,
) -> (
    <TYPES::SignatureKey as SignatureKey>::PrivateKey,
    TYPES::SignatureKey,
) {
    let private_key = TYPES::SignatureKey::generated_from_seed_indexed([0u8; 32], node_id).1;
    let public_key = <TYPES as NodeType>::SignatureKey::from_private(&private_key);
    (private_key, public_key)
}

/// initialize VID
/// # Panics
/// if unable to create a [`VidSchemeType`]
#[must_use]
pub async fn vid_scheme_from_view_number<TYPES: NodeType>(
    membership: &Arc<RwLock<TYPES::Membership>>,
    view_number: TYPES::View,
    epoch_number: TYPES::Epoch,
) -> VidSchemeType {
    let num_storage_nodes = membership
        .read()
        .await
        .committee_members(view_number, epoch_number)
        .len();
    vid_scheme(num_storage_nodes)
}

pub async fn vid_payload_commitment<TYPES: NodeType>(
    membership: &Arc<RwLock<<TYPES as NodeType>::Membership>>,
    view_number: TYPES::View,
    epoch_number: TYPES::Epoch,
    transactions: Vec<TestTransaction>,
) -> VidCommitment {
    let mut vid = vid_scheme_from_view_number::<TYPES>(membership, view_number, epoch_number).await;
    let encoded_transactions = TestTransaction::encode(&transactions);
    let vid_disperse = vid.disperse(&encoded_transactions).unwrap();

    vid_disperse.commit
}

pub async fn da_payload_commitment<TYPES: NodeType>(
    membership: &Arc<RwLock<<TYPES as NodeType>::Membership>>,
    transactions: Vec<TestTransaction>,
    epoch_number: TYPES::Epoch,
) -> VidCommitment {
    let encoded_transactions = TestTransaction::encode(&transactions);

    vid_commitment(
        &encoded_transactions,
        membership.read().await.total_nodes(epoch_number),
    )
}

pub async fn build_payload_commitment<TYPES: NodeType>(
    membership: &Arc<RwLock<<TYPES as NodeType>::Membership>>,
    view: TYPES::View,
    epoch: TYPES::Epoch,
) -> <VidSchemeType as VidScheme>::Commit {
    // Make some empty encoded transactions, we just care about having a commitment handy for the
    // later calls. We need the VID commitment to be able to propose later.
    let mut vid = vid_scheme_from_view_number::<TYPES>(membership, view, epoch).await;
    let encoded_transactions = Vec::new();
    vid.commit_only(&encoded_transactions).unwrap()
}

/// TODO: <https://github.com/EspressoSystems/HotShot/issues/2821>
pub async fn build_vid_proposal<TYPES: NodeType>(
    membership: &Arc<RwLock<<TYPES as NodeType>::Membership>>,
    view_number: TYPES::View,
    epoch_number: TYPES::Epoch,
    transactions: Vec<TestTransaction>,
    private_key: &<TYPES::SignatureKey as SignatureKey>::PrivateKey,
) -> VidProposal<TYPES> {
    let mut vid = vid_scheme_from_view_number::<TYPES>(membership, view_number, epoch_number).await;
    let encoded_transactions = TestTransaction::encode(&transactions);

    let vid_disperse = VidDisperse::from_membership(
        view_number,
        vid.disperse(&encoded_transactions).unwrap(),
        membership,
        epoch_number,
        epoch_number,
        None,
    )
    .await;

    let signature =
        TYPES::SignatureKey::sign(private_key, vid_disperse.payload_commitment.as_ref())
            .expect("Failed to sign VID commitment");
    let vid_disperse_proposal = Proposal {
        data: vid_disperse.clone(),
        signature,
        _pd: PhantomData,
    };

    (
        vid_disperse_proposal,
        VidDisperseShare2::from_vid_disperse(vid_disperse)
            .into_iter()
            .map(|vid_disperse| {
                vid_disperse
                    .to_proposal(private_key)
                    .expect("Failed to sign payload commitment")
            })
            .collect(),
    )
}

#[allow(clippy::too_many_arguments)]
pub async fn build_da_certificate<TYPES: NodeType, V: Versions>(
    membership: &Arc<RwLock<<TYPES as NodeType>::Membership>>,
    view_number: TYPES::View,
    epoch_number: TYPES::Epoch,
    transactions: Vec<TestTransaction>,
    public_key: &TYPES::SignatureKey,
    private_key: &<TYPES::SignatureKey as SignatureKey>::PrivateKey,
    upgrade_lock: &UpgradeLock<TYPES, V>,
) -> DaCertificate2<TYPES> {
    let encoded_transactions = TestTransaction::encode(&transactions);

    let da_payload_commitment = vid_commitment(
        &encoded_transactions,
        membership.read().await.total_nodes(epoch_number),
    );

    let da_data = DaData2 {
        payload_commit: da_payload_commitment,
        epoch: epoch_number,
    };

    build_cert::<TYPES, V, DaData2<TYPES>, DaVote2<TYPES>, DaCertificate2<TYPES>>(
        da_data,
        membership,
        view_number,
        epoch_number,
        public_key,
        private_key,
        upgrade_lock,
    )
    .await
}

pub async fn build_vote<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions>(
    handle: &SystemContextHandle<TYPES, I, V>,
    proposal: QuorumProposal2<TYPES>,
) -> GeneralConsensusMessage<TYPES> {
    let view = proposal.view_number;

    let leaf: Leaf2<_> = Leaf2::from_quorum_proposal(&proposal);
    let vote = QuorumVote2::<TYPES>::create_signed_vote(
        QuorumData2 {
            leaf_commit: leaf.commit(),
            epoch: leaf.epoch(),
        },
        view,
        &handle.public_key(),
        handle.private_key(),
        &handle.hotshot.upgrade_lock,
    )
    .await
    .expect("Failed to create quorum vote");
    GeneralConsensusMessage::<TYPES>::Vote(vote.to_vote())
}

/// This function permutes the provided input vector `inputs`, given some order provided within the
/// `order` vector.
///
/// # Examples
/// let output = permute_input_with_index_order(vec![1, 2, 3], vec![2, 1, 0]);
/// // Output is [3, 2, 1] now
pub fn permute_input_with_index_order<T>(inputs: Vec<T>, order: Vec<usize>) -> Vec<T>
where
    T: Clone,
{
    let mut ordered_inputs = Vec::with_capacity(inputs.len());
    for &index in &order {
        ordered_inputs.push(inputs[index].clone());
    }
    ordered_inputs
}

/// This function will create a fake [`View`] from a provided [`Leaf`].
pub async fn build_fake_view_with_leaf<V: Versions>(
    leaf: Leaf2<TestTypes>,
    upgrade_lock: &UpgradeLock<TestTypes, V>,
    epoch_height: u64,
) -> View<TestTypes> {
    build_fake_view_with_leaf_and_state(
        leaf,
        TestValidatedState::default(),
        upgrade_lock,
        epoch_height,
    )
    .await
}

/// This function will create a fake [`View`] from a provided [`Leaf`] and `state`.
pub async fn build_fake_view_with_leaf_and_state<V: Versions>(
    leaf: Leaf2<TestTypes>,
    state: TestValidatedState,
    _upgrade_lock: &UpgradeLock<TestTypes, V>,
    epoch_height: u64,
) -> View<TestTypes> {
    let epoch =
        <TestTypes as NodeType>::Epoch::new(epoch_from_block_number(leaf.height(), epoch_height));
    View {
        view_inner: ViewInner::Leaf {
            leaf: leaf.commit(),
            state: state.into(),
            delta: None,
            epoch,
        },
    }
}