Skip to content

Commit 3f5ace8

Browse files
committed
feat(block validation): ledger
1 parent 672e2d7 commit 3f5ace8

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

rust/immutable-ledger/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//!
33
//! Facilitates block serialization and validation for immutable ledger
44
//!
5-
//! Spec: https://input-output-hk.github.io/catalyst-voices/architecture/08_concepts/immutable_ledger/ledger/
5+
//! Spec: <https://input-output-hk.github.io/catalyst-voices/architecture/08_concepts/immutable_ledger/ledger>/
66
//!
77
88
/// Block validation logic

rust/immutable-ledger/src/serialize.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub struct BlockTimeStamp(pub i64);
3535
pub struct PreviousBlockHash(pub Vec<u8>);
3636

3737
/// unique identifier of the ledger type.
38-
/// In general, this is the way to strictly bound and specify block_data of the ledger for the specific ledger_type.
38+
/// In general, this is the way to strictly bound and specify `block_data` of the ledger for the specific `ledger_type`.
3939
#[derive(Debug, Clone, PartialEq)]
4040
pub struct LedgerType(pub Uuid);
4141

@@ -106,7 +106,7 @@ pub type DecodedBlockGenesis = (
106106
/// Encoded whole block including block header, cbor encoded block data and signatures.
107107
pub type EncodedBlock = Vec<u8>;
108108

109-
/// Encoded genesis block, see genesis_to_prev_hash
109+
/// Encoded genesis block, see `genesis_to_prev_hash`
110110
pub type EncodedGenesisBlock = Vec<u8>;
111111

112112
/// Choice of hash function:
@@ -140,7 +140,7 @@ pub fn encode_block(
140140
.0
141141
.iter()
142142
.map(|sk| {
143-
let mut sk: SigningKey = SigningKey::from_bytes(&sk);
143+
let mut sk: SigningKey = SigningKey::from_bytes(sk);
144144
sk.sign(&data_to_sign).to_bytes()
145145
})
146146
.collect();
@@ -150,11 +150,11 @@ pub fn encode_block(
150150

151151
encoder.bytes(&block_data.0)?;
152152

153-
for sig in signatures.iter() {
153+
for sig in &signatures {
154154
encoder.bytes(sig)?;
155155
}
156156

157-
let block_data_with_sigs = encoder.writer().to_vec();
157+
let block_data_with_sigs = encoder.writer().clone();
158158
// block hdr + block data + sigs
159159
let encoded_block = [block_hdr_cbor, block_data_with_sigs].concat();
160160

@@ -217,21 +217,21 @@ pub fn encode_block_header(
217217
encoder.bytes(&chain_id.0.to_bytes())?;
218218
encoder.bytes(&height.0.to_be_bytes())?;
219219
encoder.bytes(&ts.0.to_be_bytes())?;
220-
encoder.bytes(&prev_block_hash.0.as_slice())?;
220+
encoder.bytes(prev_block_hash.0.as_slice())?;
221221
encoder.bytes(ledger_type.0.as_bytes())?;
222222
encoder.bytes(&pid.0.to_bytes())?;
223223

224224
// marks how many validators for decoding side.
225225
encoder.bytes(&validator.0.len().to_be_bytes())?;
226-
for validator in validator.0.iter() {
226+
for validator in &validator.0 {
227227
encoder.bytes(&validator.0)?;
228228
}
229229

230230
if let Some(meta) = metadata {
231231
encoder.bytes(&meta.0)?;
232232
}
233233

234-
Ok(encoder.writer().to_vec())
234+
Ok(encoder.writer().clone())
235235
}
236236

237237
/// Decode block header
@@ -343,12 +343,12 @@ pub fn encode_genesis(
343343

344344
// marks how many validators for decoding side.
345345
encoder.bytes(&validator.0.len().to_be_bytes())?;
346-
for validator in validator.0.iter() {
346+
for validator in &validator.0 {
347347
encoder.bytes(&validator.0)?;
348348
}
349349

350350
// Get hash of the genesis_to_prev_hash bytes i.e hash of itself
351-
let genesis_prev_bytes = encoder.writer().to_vec();
351+
let genesis_prev_bytes = encoder.writer().clone();
352352

353353
// Size of encoded contents which is hashed
354354
encoder.bytes(&genesis_prev_bytes.len().to_be_bytes())?;
@@ -360,9 +360,9 @@ pub fn encode_genesis(
360360

361361
// prev_block_id for the Genesis block MUST be a hash of the genesis_to_prev_hash bytes
362362
// last 64 bytes (depending on given hash function) of encoding are the hash of the genesis contents
363-
encoder.bytes(&genesis_prev_hash.as_slice())?;
363+
encoder.bytes(genesis_prev_hash.as_slice())?;
364364

365-
Ok(encoder.writer().to_vec())
365+
Ok(encoder.writer().clone())
366366
}
367367

368368
/// Decode genesis
@@ -494,7 +494,7 @@ mod tests {
494494

495495
let chain_id = ChainId(Ulid::new());
496496
let block_height = Height(5);
497-
let block_ts = BlockTimeStamp(1728474515);
497+
let block_ts = BlockTimeStamp(1_728_474_515);
498498
let prev_block_height = PreviousBlockHash(vec![0; 64]);
499499
let ledger_type = LedgerType(Uuid::new_v4());
500500
let purpose_id = PurposeId(Ulid::new());
@@ -538,7 +538,7 @@ mod tests {
538538

539539
let chain_id = ChainId(Ulid::new());
540540
let block_height = Height(5);
541-
let block_ts = BlockTimeStamp(1728474515);
541+
let block_ts = BlockTimeStamp(1_728_474_515);
542542
let prev_block_height = PreviousBlockHash(vec![0; 64]);
543543
let ledger_type = LedgerType(Uuid::new_v4());
544544
let purpose_id = PurposeId(Ulid::new());

rust/immutable-ledger/src/validate.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ mod tests {
162162

163163
let chain_id = ChainId(Ulid::new());
164164
let block_height = Height(5);
165-
let block_ts = BlockTimeStamp(1728474515);
165+
let block_ts = BlockTimeStamp(1_728_474_515);
166166
let prev_block_height = PreviousBlockHash(vec![0; 64]);
167167
let ledger_type = LedgerType(Uuid::new_v4());
168168
let purpose_id = PurposeId(Ulid::new());
@@ -213,7 +213,7 @@ mod tests {
213213
//
214214

215215
let block_height = Height(6);
216-
let block_ts = BlockTimeStamp(1728474518);
216+
let block_ts = BlockTimeStamp(1_728_474_518);
217217
let prev_block_hash = PreviousBlockHash(blake2b_512(&previous_block).unwrap().to_vec());
218218
let validators = Validator(vec![Kid(kid_a), Kid(kid_b)]);
219219
let metadata = Some(Metadata(vec![1; 128]));
@@ -245,8 +245,8 @@ mod tests {
245245
//
246246

247247
match validate_block(current_block, previous_block, Blake2b) {
248-
Ok(_) => (),
249-
Err(err) => panic!("Block validation failed: {:?}", err),
248+
Ok(()) => (),
249+
Err(err) => panic!("Block validation failed: {err:?}"),
250250
};
251251
}
252252

@@ -279,8 +279,8 @@ mod tests {
279279
.unwrap();
280280

281281
match validate_genesis(encoded_block_genesis, Blake2b) {
282-
Ok(_) => (),
283-
Err(err) => panic!("Genesis Block validation failed: {:?}", err),
282+
Ok(()) => (),
283+
Err(err) => panic!("Genesis Block validation failed: {err:?}"),
284284
};
285285
}
286286
}

0 commit comments

Comments
 (0)