2023-04-11 19:04:53 -04:00
|
|
|
use core::fmt::Debug;
|
|
|
|
|
|
|
|
|
|
use rand_core::{RngCore, OsRng};
|
|
|
|
|
|
|
|
|
|
use tributary::{ReadWrite, tests::random_signed};
|
|
|
|
|
|
2023-04-17 02:09:29 -04:00
|
|
|
use crate::tributary::{SignData, Transaction};
|
2023-04-11 19:04:53 -04:00
|
|
|
|
2023-04-22 10:49:52 -04:00
|
|
|
mod chain;
|
2023-04-22 22:27:12 -04:00
|
|
|
pub use chain::*;
|
|
|
|
|
|
|
|
|
|
mod tx;
|
2023-04-22 10:49:52 -04:00
|
|
|
|
2023-04-23 01:00:46 -04:00
|
|
|
mod dkg;
|
2023-04-23 03:48:50 -04:00
|
|
|
// TODO: Test the other transactions
|
2023-04-23 01:00:46 -04:00
|
|
|
|
2023-04-24 02:50:03 -04:00
|
|
|
mod handle_p2p;
|
|
|
|
|
mod sync;
|
|
|
|
|
|
2023-04-11 19:04:53 -04:00
|
|
|
fn random_u32<R: RngCore>(rng: &mut R) -> u32 {
|
|
|
|
|
u32::try_from(rng.next_u64() >> 32).unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn random_vec<R: RngCore>(rng: &mut R, limit: usize) -> Vec<u8> {
|
|
|
|
|
let len = usize::try_from(rng.next_u64() % u64::try_from(limit).unwrap()).unwrap();
|
|
|
|
|
let mut res = vec![0; len];
|
|
|
|
|
rng.fill_bytes(&mut res);
|
|
|
|
|
res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn random_sign_data<R: RngCore>(rng: &mut R) -> SignData {
|
|
|
|
|
let mut plan = [0; 32];
|
|
|
|
|
rng.fill_bytes(&mut plan);
|
|
|
|
|
|
|
|
|
|
SignData {
|
|
|
|
|
plan,
|
|
|
|
|
attempt: random_u32(&mut OsRng),
|
|
|
|
|
|
|
|
|
|
data: random_vec(&mut OsRng, 512),
|
|
|
|
|
|
|
|
|
|
signed: random_signed(&mut OsRng),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn test_read_write<RW: Eq + Debug + ReadWrite>(value: RW) {
|
|
|
|
|
assert_eq!(value, RW::read::<&[u8]>(&mut value.serialize().as_ref()).unwrap());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn serialize_sign_data() {
|
|
|
|
|
test_read_write(random_sign_data(&mut OsRng));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn serialize_transaction() {
|
|
|
|
|
test_read_write(Transaction::DkgCommitments(
|
|
|
|
|
random_u32(&mut OsRng),
|
|
|
|
|
random_vec(&mut OsRng, 512),
|
|
|
|
|
random_signed(&mut OsRng),
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
// This supports a variable share length, yet share length is expected to be constant among
|
|
|
|
|
// shares
|
2023-04-11 20:24:27 -04:00
|
|
|
let share_len = usize::try_from(OsRng.next_u64() % 512).unwrap();
|
2023-09-01 00:03:53 -04:00
|
|
|
// Create a valid vec of shares
|
|
|
|
|
let mut shares = vec![];
|
2023-04-11 20:24:27 -04:00
|
|
|
// Create up to 512 participants
|
|
|
|
|
for i in 0 .. (OsRng.next_u64() % 512) {
|
2023-04-11 19:04:53 -04:00
|
|
|
let mut share = vec![0; share_len];
|
|
|
|
|
OsRng.fill_bytes(&mut share);
|
2023-09-01 00:03:53 -04:00
|
|
|
shares.push(share);
|
2023-04-11 19:04:53 -04:00
|
|
|
}
|
|
|
|
|
|
2023-08-14 06:08:55 -04:00
|
|
|
test_read_write(Transaction::DkgShares {
|
|
|
|
|
attempt: random_u32(&mut OsRng),
|
2023-04-11 19:04:53 -04:00
|
|
|
shares,
|
2023-08-14 06:08:55 -04:00
|
|
|
confirmation_nonces: {
|
|
|
|
|
let mut nonces = [0; 64];
|
|
|
|
|
OsRng.fill_bytes(&mut nonces);
|
|
|
|
|
nonces
|
|
|
|
|
},
|
|
|
|
|
signed: random_signed(&mut OsRng),
|
|
|
|
|
});
|
2023-04-11 19:04:53 -04:00
|
|
|
}
|
|
|
|
|
|
2023-08-14 06:08:55 -04:00
|
|
|
test_read_write(Transaction::DkgConfirmed(
|
|
|
|
|
random_u32(&mut OsRng),
|
|
|
|
|
{
|
|
|
|
|
let mut share = [0; 32];
|
|
|
|
|
OsRng.fill_bytes(&mut share);
|
|
|
|
|
share
|
|
|
|
|
},
|
|
|
|
|
random_signed(&mut OsRng),
|
|
|
|
|
));
|
|
|
|
|
|
2023-04-20 14:24:49 -04:00
|
|
|
{
|
2023-08-31 23:04:37 -04:00
|
|
|
let mut block = [0; 32];
|
|
|
|
|
OsRng.fill_bytes(&mut block);
|
Replace ExternalBlock with Batch
The initial TODO was simply to use one ExternalBlock per all batches in the
block. This would require publishing ExternalBlock after the last batch,
requiring knowing the last batch. While we could add such a pipeline, it'd
require:
1) Initial preprocesses using a distinct message from BatchPreprocess
2) An additional message sent after all BatchPreprocess are sent
Unfortunately, both would require tweaks to the SubstrateSigner which aren't
worth the complexity compared to the solution here, at least, not at this time.
While this will cause, if a Tributary is signing a block whose total batch data
exceeds 25 kB, to use multiple transactions which could be optimized out by
'better' local data pipelining, that's an extreme edge case. Given the temporal
nature of each Tributary, it's also an acceptable edge.
This does no longer achieve synchrony over external blocks accordingly. While
signed batches have synchrony, as they embed their block hash, batches being
signed don't have cryptographic synchrony on their contents. This means
validators who are eclipsed may produce invalid shares, as they sign a
different batch. This will be introduced in a follow-up commit.
2023-08-31 22:48:02 -04:00
|
|
|
let mut batch = [0; 32];
|
|
|
|
|
OsRng.fill_bytes(&mut batch);
|
2023-08-31 23:04:37 -04:00
|
|
|
test_read_write(Transaction::Batch(block, batch));
|
2023-04-20 14:24:49 -04:00
|
|
|
}
|
2023-04-20 15:37:22 -04:00
|
|
|
test_read_write(Transaction::SubstrateBlock(OsRng.next_u64()));
|
2023-04-11 19:04:53 -04:00
|
|
|
|
|
|
|
|
test_read_write(Transaction::BatchPreprocess(random_sign_data(&mut OsRng)));
|
|
|
|
|
test_read_write(Transaction::BatchShare(random_sign_data(&mut OsRng)));
|
2023-04-20 06:59:42 -04:00
|
|
|
|
|
|
|
|
test_read_write(Transaction::SignPreprocess(random_sign_data(&mut OsRng)));
|
|
|
|
|
test_read_write(Transaction::SignShare(random_sign_data(&mut OsRng)));
|
2023-08-31 23:39:36 -04:00
|
|
|
|
|
|
|
|
{
|
|
|
|
|
let mut plan = [0; 32];
|
|
|
|
|
OsRng.fill_bytes(&mut plan);
|
|
|
|
|
let mut tx_hash = vec![0; (OsRng.next_u64() % 64).try_into().unwrap()];
|
|
|
|
|
OsRng.fill_bytes(&mut tx_hash);
|
|
|
|
|
test_read_write(Transaction::SignCompleted {
|
|
|
|
|
plan,
|
|
|
|
|
tx_hash,
|
|
|
|
|
first_signer: random_signed(&mut OsRng).signer,
|
|
|
|
|
signature: random_signed(&mut OsRng).signature,
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-04-11 19:04:53 -04:00
|
|
|
}
|