|
1 | | -use cardano_serialization_lib::{AuxiliaryDataSet, Block, Header, HeaderBody, OperationalCert, ProtocolVersion, Transaction, TransactionBodies, TransactionWitnessSets}; |
| 1 | +use crate::Settings; |
| 2 | +use cardano_serialization_lib::crypto::{ |
| 3 | + BlockHash, Ed25519Signature, KESSignature, KESVKey, PrivateKey, VRFCert, VRFVKey, Vkey, |
| 4 | +}; |
2 | 5 | use cardano_serialization_lib::metadata::AuxiliaryData; |
3 | | -use cardano_serialization_lib::crypto::{BlockHash, Ed25519Signature, KESSignature, KESVKey, PrivateKey, Vkey, VRFCert, VRFVKey}; |
4 | 6 | use cardano_serialization_lib::utils::BigNum; |
5 | | -use crate::Settings; |
6 | | - |
7 | | - |
8 | | -pub struct Block0{ |
| 7 | +use cardano_serialization_lib::{ |
| 8 | + AuxiliaryDataSet, Block, Header, HeaderBody, OperationalCert, ProtocolVersion, Transaction, |
| 9 | + TransactionBodies, TransactionWitnessSets, |
| 10 | +}; |
| 11 | + |
| 12 | +/// Block0 representation |
| 13 | +pub struct Block0 { |
| 14 | + /// Block containing transactions |
9 | 15 | pub block: Block, |
10 | | - pub settings: Settings |
| 16 | + /// Consensus settings |
| 17 | + pub settings: Settings, |
11 | 18 | } |
12 | 19 |
|
13 | | - |
14 | 20 | impl Default for Block0 { |
15 | 21 | fn default() -> Self { |
16 | 22 | Self { |
17 | | - block: BlockBuilder::next_block(None,vec![]), |
18 | | - settings: Default::default() |
| 23 | + block: BlockBuilder::next_block(None, &[]), |
| 24 | + settings: Settings::default(), |
19 | 25 | } |
20 | 26 | } |
21 | 27 | } |
22 | 28 |
|
| 29 | +/// Block builder responsible for building blocks |
23 | 30 | pub struct BlockBuilder; |
24 | 31 |
|
25 | 32 | impl BlockBuilder { |
26 | | - |
27 | | - pub fn next_block(prev: Option<&Block>, transactions: Vec<Transaction>) -> Block { |
| 33 | + /// Mint new block based on previous block and transactions |
| 34 | + /// |
| 35 | + /// # Panics |
| 36 | + /// |
| 37 | + /// On integer conversion |
| 38 | + #[must_use] |
| 39 | + pub fn next_block(prev: Option<&Block>, transactions: &[Transaction]) -> Block { |
28 | 40 | let header_body = Self::block_header( |
29 | | - prev.map(|b| b.header().header_body().block_number()).unwrap_or(1), |
30 | | - prev.map(|b| b.header().header_body().block_body_hash()) |
| 41 | + prev.map_or(1, |b| b.header().header_body().block_number()), |
| 42 | + prev.map(|b| b.header().header_body().block_body_hash()), |
31 | 43 | ); |
32 | 44 |
|
33 | | - let header = Header::new(&header_body,&Self::random_kes_signature()); |
| 45 | + let header = Header::new(&header_body, &Self::random_kes_signature()); |
34 | 46 |
|
35 | | - let bodies = transactions.iter().map(|x| x.body()).fold(TransactionBodies::new(),|mut acc,x| { |
| 47 | + let bodies = transactions.iter().map(Transaction::body).fold( |
| 48 | + TransactionBodies::new(), |
| 49 | + |mut acc, x| { |
36 | 50 | acc.add(&x); |
37 | 51 | acc |
38 | | - }); |
| 52 | + }, |
| 53 | + ); |
39 | 54 |
|
40 | | - let metadata = transactions.iter().filter_map(|x| x.auxiliary_data().map(|x| x.metadata())).enumerate().fold(AuxiliaryDataSet::new(),|mut acc,x| { |
41 | | - let mut auxiliary_data = AuxiliaryData::new(); |
42 | | - if let Some(metadata) = &x.1 { |
43 | | - auxiliary_data.set_metadata(metadata); |
44 | | - } |
45 | | - acc.insert(x.0 as u32, &auxiliary_data); |
46 | | - acc |
47 | | - }); |
| 55 | + let metadata = transactions |
| 56 | + .iter() |
| 57 | + .filter_map(|x| x.auxiliary_data().map(|x| x.metadata())) |
| 58 | + .enumerate() |
| 59 | + .fold(AuxiliaryDataSet::new(), |mut acc, x| { |
| 60 | + let mut auxiliary_data = AuxiliaryData::new(); |
| 61 | + if let Some(metadata) = &x.1 { |
| 62 | + auxiliary_data.set_metadata(metadata); |
| 63 | + } |
| 64 | + acc.insert(u32::try_from(x.0).unwrap(), &auxiliary_data); |
| 65 | + acc |
| 66 | + }); |
48 | 67 |
|
49 | | - Block::new(&header,&bodies,&TransactionWitnessSets::new(), &metadata, vec![]) |
| 68 | + Block::new( |
| 69 | + &header, |
| 70 | + &bodies, |
| 71 | + &TransactionWitnessSets::new(), |
| 72 | + &metadata, |
| 73 | + vec![], |
| 74 | + ) |
50 | 75 | } |
51 | 76 |
|
52 | 77 | fn random_kes_signature() -> KESSignature { |
53 | | - KESSignature::from_bytes(Self::generate_random_bytes_of_len(KESSignature::BYTE_COUNT)).unwrap() |
| 78 | + KESSignature::from_bytes(Self::generate_random_bytes_of_len(KESSignature::BYTE_COUNT)) |
| 79 | + .unwrap() |
54 | 80 | } |
55 | 81 |
|
56 | 82 | fn generate_random_bytes_of_len(len: usize) -> Vec<u8> { |
57 | | - (0..len).map(|_| { rand::random::<u8>() }).collect() |
| 83 | + (0..len).map(|_| rand::random::<u8>()).collect() |
58 | 84 | } |
59 | 85 |
|
| 86 | + /// Builds new block header based on block number and previous block hash |
| 87 | + /// |
| 88 | + /// # Panics |
| 89 | + /// |
| 90 | + /// On random bytes generation issue |
| 91 | + #[must_use] |
60 | 92 | pub fn block_header(block_number: u32, prev_hash: Option<BlockHash>) -> HeaderBody { |
61 | 93 | let issuer_vkey = PrivateKey::generate_ed25519extended().unwrap().to_public(); |
62 | 94 |
|
63 | | - HeaderBody::new_headerbody(block_number, |
64 | | - &BigNum::from(block_number), |
65 | | - prev_hash, |
66 | | - &Vkey::new(&issuer_vkey), |
67 | | - &VRFVKey::from_bytes(Self::generate_random_bytes_of_len(32)).unwrap(), |
68 | | - &VRFCert::new(Self::generate_random_bytes_of_len(32),Self::generate_random_bytes_of_len(VRFCert::PROOF_LEN)).unwrap(), |
69 | | - 0, |
70 | | - &BlockHash::from_bytes(Self::generate_random_bytes_of_len(32)).unwrap(), |
71 | | - &OperationalCert::new(&KESVKey::from_bytes(Self::generate_random_bytes_of_len(32)).unwrap(),0,0,&Ed25519Signature::from_bytes(Self::generate_random_bytes_of_len(64)).unwrap()), &ProtocolVersion::new(0,1)) |
72 | | - |
| 95 | + HeaderBody::new_headerbody( |
| 96 | + block_number, |
| 97 | + &BigNum::from(block_number), |
| 98 | + prev_hash, |
| 99 | + &Vkey::new(&issuer_vkey), |
| 100 | + &VRFVKey::from_bytes(Self::generate_random_bytes_of_len(32)).unwrap(), |
| 101 | + &VRFCert::new( |
| 102 | + Self::generate_random_bytes_of_len(32), |
| 103 | + Self::generate_random_bytes_of_len(VRFCert::PROOF_LEN), |
| 104 | + ) |
| 105 | + .unwrap(), |
| 106 | + 0, |
| 107 | + &BlockHash::from_bytes(Self::generate_random_bytes_of_len(32)).unwrap(), |
| 108 | + &OperationalCert::new( |
| 109 | + &KESVKey::from_bytes(Self::generate_random_bytes_of_len(32)).unwrap(), |
| 110 | + 0, |
| 111 | + 0, |
| 112 | + &Ed25519Signature::from_bytes(Self::generate_random_bytes_of_len(64)).unwrap(), |
| 113 | + ), |
| 114 | + &ProtocolVersion::new(0, 1), |
| 115 | + ) |
73 | 116 | } |
74 | 117 | } |
0 commit comments