Skip to content

Commit 455392e

Browse files
authored
Merge branch 'feat/cardano-blockchain-types' into fix/rust-test-doc
2 parents f86d7d7 + 1ccf814 commit 455392e

File tree

18 files changed

+214
-222
lines changed

18 files changed

+214
-222
lines changed

.github/workflows/semantic_pull_request.yml

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ jobs:
2626
rust/cbork
2727
rust/hermes-ipfs
2828
rust/rbac-registration
29+
rust/cardano-blockchain-types
2930
dart
3031
docs
3132
general

docs/src/architecture/08_concepts/immutable_ledger/cddl/block.cddl

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,19 @@ block = [
88
]
99

1010
block-header = [
11-
chain-id: ULID,
11+
chain-id: UUID, ; UUID v7
1212
height: int,
1313
timestamp: #6.1(uint .ge 1722470400), ; Epoch-based date/time
1414
prev-block-id: hash-bytes, ; hash of the previous block
15-
?ledger-type: UUID,
16-
?purpose-id: ULID / UUID,
15+
?ledger-type: UUID, ; UUID v4
16+
?purpose-id: UUID, ; UUID v7
1717
?validator,
1818
~metadata,
1919
]
2020

2121
block-data = encoded-cbor
2222

2323
UUID = #6.37(bytes) ; UUID type
24-
ULID = #6.32780(bytes) ; ULID type
2524

2625
kid = hash-bytes ; hash of the x509/c509 certificate
2726

docs/src/architecture/08_concepts/immutable_ledger/cddl/genesis_to_prev_hash.cddl

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
; https://datatracker.ietf.org/doc/html/draft-ietf-cbor-cde-06
33

44
genesis-to-prev-hash = [
5-
chain-id: ULID,
5+
chain-id: UUID, ; UUID v7
66
timestamp: #6.1(uint .ge 1722470400), ; Epoch-based date/time
7-
ledger-type: UUID,
8-
purpose-id: ULID / UUID,
7+
ledger-type: UUID, ; UUID v4
8+
purpose-id: UUID, ; UUID v7
99
validator,
1010
]
1111

1212
UUID = #6.37(bytes) ; UUID type
13-
ULID = #6.32780(bytes) ; ULID type
1413

1514
validator = (kid / [2* kid])
1615
kid = hash-bytes ; hash of the x509/c509 certificate

rust/Earthfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ check:
5151
# build : Build crates.
5252
build:
5353
FROM +builder-src
54-
54+
RUN rm -rf ~/build/target/release
5555
DO rust-ci+EXECUTE \
5656
--cmd="/scripts/std_build.py" \
5757
--args1="--libs=c509-certificate --libs=cardano-blockchain-types --libs=cardano-chain-follower --libs=hermes-ipfs" \
5858
--args2="--libs=cbork-cddl-parser --libs=cbork-abnf-parser" \
59-
--args3="--libs=catalyst-voting --libs=vote-tx-v1 --libs=vote-tx-v2" \
59+
--args3="--libs=catalyst-voting --libs=immutable-ledger --libs=vote-tx-v1 --libs=vote-tx-v2" \
6060
--args4="--bins=cbork/cbork --libs=rbac-registration --libs=signed_doc" \
6161
--args5="--cov_report=$HOME/build/coverage-report.info" \
6262
--output="release/[^\./]+" \

rust/cardano-blockchain-types/deps.tmp

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
rbac-registration = { version = "0.0.2", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "v0.0.8" }
32

43
thiserror = "1.0.64"

rust/cardano-blockchain-types/src/auxdata/aux_data.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use super::{
99
scripts::{MutableTransactionScriptsMap, ScriptArray, ScriptType, TransactionScripts},
1010
};
1111

12-
/// Auxiliary Data (Metadata) for a single Transaction in a block
12+
/// Auxiliary Data (Metadata) for a single transaction in a block
1313
#[derive(Clone, Debug)]
1414
#[allow(clippy::module_name_repetitions)]
1515
pub struct TransactionAuxData {
@@ -58,7 +58,7 @@ impl TransactionAuxData {
5858
self.metadata.get(label)
5959
}
6060

61-
/// Decode a Shelley-MA Auxiliary Data Array
61+
/// Decode a Shelley-MA auxiliary data array
6262
fn decode_shelley_ma_array(d: &mut minicbor::Decoder) -> Result<Self, minicbor::decode::Error> {
6363
match d.array() {
6464
Ok(Some(entries)) => {
@@ -158,9 +158,9 @@ impl TransactionAuxData {
158158

159159
let script_array = ScriptArray::decode(d, &mut ctx)?;
160160
if scripts.insert(script_type, script_array).is_some() {
161-
return Err(minicbor::decode::Error::message(
161+
return Err(minicbor::decode::Error::message(format!(
162162
"Multiple Alonzo+ Script entries of type {script_type} found. Invalid.",
163-
));
163+
)));
164164
}
165165
}
166166

rust/cardano-blockchain-types/src/auxdata/block.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use pallas::ledger::traverse::MultiEraBlock;
99
use super::aux_data::TransactionAuxData;
1010
use crate::txn_index::TxnIndex;
1111

12-
/// Auxiliary Data for every transaction within a block.
12+
/// Auxiliary data for every transaction within a block.
1313
#[derive(Debug)]
1414
#[allow(clippy::module_name_repetitions)]
1515
pub struct BlockAuxData(Arc<dashmap::ReadOnlyView<TxnIndex, TransactionAuxData>>);

rust/cardano-blockchain-types/src/auxdata/metadatum.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::conversion::from_saturating;
1414
pub struct Metadata(Arc<MetadataInner>);
1515

1616
#[derive(Clone, Debug)]
17-
/// Transaction Metadata - Inner
17+
/// Transaction Metadata - inner
1818
/// See: <https://github.com/IntersectMBO/cardano-ledger/blob/78b32d585fd4a0340fb2b184959fb0d46f32c8d2/eras/conway/impl/cddl-files/conway.cddl#L519>
1919
pub struct MetadataInner {
2020
/// Sequence the metadatum labels appear in the metadata.
@@ -88,8 +88,8 @@ impl Decode<'_, ()> for Metadata {
8888
sequence.push(label);
8989
let _unused = metadata.insert(label, value);
9090

91-
// Look for End Sentinel IF its an indefinite MAP
92-
// (which we know because entries is u64::MAX).
91+
// Look for end sentinel IF its an indefinite MAP
92+
// (which we know because entries is u64::MAX).
9393
if entries == u64::MAX {
9494
match d.datatype() {
9595
Ok(minicbor::data::Type::Break) => {

rust/cardano-blockchain-types/src/conversion.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use anyhow::bail;
44

5-
/// Convert an `<T>` to `<R>`. (saturate if out of range.)
5+
/// Convert an `<T>` to `<R>` (saturate if out of range).
66
/// Note can convert any int to float, or f32 to f64 as well.
77
/// can not convert from float to int, or f64 to f32.
88
pub(crate) fn from_saturating<

rust/cardano-blockchain-types/src/fork.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
use crate::conversion::from_saturating;
1010

1111
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd)]
12-
/// Counter that is incremented every time there is a roll-back in live-chain
12+
/// Counter that is incremented every time there is a roll-back in live-chain.
1313
pub struct Fork(u64);
1414

1515
impl Fork {
16-
/// Convert an `<T>` to Fork. (saturate if out of range.)
16+
/// Convert an `<T>` to `Fork` (saturate if out of range).
1717
pub fn from_saturating<
1818
T: Copy
1919
+ TryInto<u64>
@@ -29,12 +29,12 @@ impl Fork {
2929

3030
/// Increment the fork count.
3131
pub fn incr(&mut self) {
32-
self.0 += 1;
32+
self.0 = self.0.saturating_add(1);
3333
}
3434

3535
/// Decrement the fork count.
3636
pub fn decr(&mut self) {
37-
self.0 -= 1;
37+
self.0 = self.0.saturating_sub(1);
3838
}
3939
}
4040

rust/cardano-blockchain-types/src/hashes.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@ use pallas_crypto::hash::Hash;
88

99
/// Number of bytes in a blake2b 224 hash.
1010
pub const BLAKE_2B224_SIZE: usize = 224 / 8;
11-
/// `Blake2B` 224bit Hash
11+
12+
/// `Blake2B` 224bit Hash.
1213
pub type Blake2b224Hash = Blake2bHash<BLAKE_2B224_SIZE>;
1314

1415
/// Number of bytes in a blake2b 256 hash.
1516
pub const BLAKE_2B256_SIZE: usize = 256 / 8;
17+
1618
/// `Blake2B` 256bit Hash
1719
pub type Blake2b256Hash = Blake2bHash<BLAKE_2B256_SIZE>;
1820

1921
/// Number of bytes in a blake2b 128 hash.
2022
pub const BLAKE_2B128_SIZE: usize = 128 / 8;
23+
2124
/// `Blake2B` 128bit Hash
2225
pub type Blake2b128Hash = Blake2bHash<BLAKE_2B128_SIZE>;
2326

@@ -68,6 +71,7 @@ impl<const BYTES: usize> From<Blake2bHash<BYTES>> for Vec<u8> {
6871
}
6972
}
7073

74+
/// Convert hash in a form of byte array into the `Blake2bHash` type.
7175
impl<const BYTES: usize> TryFrom<&[u8]> for Blake2bHash<BYTES> {
7276
type Error = anyhow::Error;
7377

@@ -133,12 +137,8 @@ impl<'a, C, const BYTES: usize> minicbor::Decode<'a, C> for Blake2bHash<BYTES> {
133137
d: &mut minicbor::Decoder<'a>, _ctx: &mut C,
134138
) -> Result<Self, minicbor::decode::Error> {
135139
let bytes = d.bytes()?;
136-
if bytes.len() == BYTES {
137-
let mut hash = [0; BYTES];
138-
hash.copy_from_slice(bytes);
139-
Ok(hash.into())
140-
} else {
141-
Err(minicbor::decode::Error::message("Invalid hash size"))
142-
}
140+
bytes.try_into().map_err(|_| {
141+
minicbor::decode::Error::message("Invalid hash size for Blake2bHash cbor decode")
142+
})
143143
}
144144
}

rust/cardano-blockchain-types/src/multi_era_block_data.rs

+27-15
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ struct SelfReferencedMultiEraBlock {
4545
/// Multi-era block - inner.
4646
#[derive(Debug)]
4747
struct MultiEraBlockInner {
48-
/// What blockchain was the block produced on.
49-
//#[allow(dead_code)]
50-
pub chain: Network,
48+
/// What blockchain network was the block produced on.
49+
network: Network,
5150
/// The Point on the blockchain this block can be found.
5251
point: Point,
5352
/// The previous point on the blockchain before this block.
@@ -93,7 +92,7 @@ impl MultiEraBlock {
9392
///
9493
/// If the given bytes cannot be decoded as a multi-era block, an error is returned.
9594
fn new_block(
96-
chain: Network, raw_data: Vec<u8>, previous: &Point, fork: Fork,
95+
network: Network, raw_data: Vec<u8>, previous: &Point, fork: Fork,
9796
) -> anyhow::Result<Self> {
9897
let builder = SelfReferencedMultiEraBlockTryBuilder {
9998
raw_data,
@@ -127,7 +126,7 @@ impl MultiEraBlock {
127126
// Special case, when the previous block is actually UNKNOWN, we can't check it.
128127
if !previous.is_unknown()
129128
// Otherwise, we make sure the hash chain is intact
130-
&& !previous.cmp_hash(&decoded_block.header().previous_hash())
129+
&& previous != &decoded_block.header().previous_hash()
131130
{
132131
debug!("{}, {:?}", previous, decoded_block.header().previous_hash());
133132

@@ -140,7 +139,7 @@ impl MultiEraBlock {
140139
Ok(Self {
141140
fork,
142141
inner: Arc::new(MultiEraBlockInner {
143-
chain,
142+
network,
144143
point,
145144
previous: previous.clone(),
146145
data: self_ref_block,
@@ -156,9 +155,9 @@ impl MultiEraBlock {
156155
///
157156
/// If the given bytes cannot be decoded as a multi-era block, an error is returned.
158157
pub fn new(
159-
chain: Network, raw_data: Vec<u8>, previous: &Point, fork: Fork,
158+
network: Network, raw_data: Vec<u8>, previous: &Point, fork: Fork,
160159
) -> anyhow::Result<Self> {
161-
MultiEraBlock::new_block(chain, raw_data, previous, fork)
160+
MultiEraBlock::new_block(network, raw_data, previous, fork)
162161
}
163162

164163
/// Remake the block on a new fork.
@@ -169,6 +168,7 @@ impl MultiEraBlock {
169168
/// Decodes the data into a multi-era block.
170169
///
171170
/// # Returns
171+
///
172172
/// The decoded block data, which can easily be processed by a consumer.
173173
#[must_use]
174174
#[allow(clippy::missing_panics_doc)]
@@ -179,6 +179,7 @@ impl MultiEraBlock {
179179
/// Decodes the data into a multi-era block.
180180
///
181181
/// # Returns
182+
///
182183
/// The raw byte data of the block.
183184
#[must_use]
184185
#[allow(clippy::missing_panics_doc)]
@@ -189,6 +190,7 @@ impl MultiEraBlock {
189190
/// Returns the block point of this block.
190191
///
191192
/// # Returns
193+
///
192194
/// The block point of this block.
193195
#[must_use]
194196
pub fn point(&self) -> Point {
@@ -198,6 +200,7 @@ impl MultiEraBlock {
198200
/// Returns the block point of the previous block.
199201
///
200202
/// # Returns
203+
///
201204
/// The previous blocks `Point`
202205
#[must_use]
203206
pub fn previous(&self) -> Point {
@@ -210,9 +213,10 @@ impl MultiEraBlock {
210213
/// (Immutable Database) of the Node.
211214
///
212215
/// # Returns
216+
///
213217
/// `true` if the block is immutable, `false` otherwise.
214218
#[must_use]
215-
pub fn immutable(&self) -> bool {
219+
pub fn is_immutable(&self) -> bool {
216220
self.fork == 0.into()
217221
}
218222

@@ -225,28 +229,32 @@ impl MultiEraBlock {
225229
/// - 2+ - for each subsequent rollback detected while reading live blocks.
226230
///
227231
/// # Returns
232+
///
228233
/// The fork the block was found on.
229234
#[must_use]
230235
pub fn fork(&self) -> Fork {
231236
self.fork
232237
}
233238

234-
/// What chain was the block from
239+
/// What blockchain network was the block from
235240
///
236241
/// # Returns
237-
/// - The chain that this block originated on.
242+
///
243+
/// - The network that this block originated on.
238244
#[must_use]
239-
pub fn chain(&self) -> Network {
240-
self.inner.chain
245+
pub fn network(&self) -> Network {
246+
self.inner.network
241247
}
242248

243249
/// Get The Metadata fora a transaction and known label from the block
244250
///
245251
/// # Parameters
252+
///
246253
/// - `txn_idx` - Index of the Transaction in the Block
247254
/// - `label` - The label of the transaction
248255
///
249256
/// # Returns
257+
///
250258
/// - Metadata for the given label in the transaction.
251259
/// - Or None if the label requested isn't present.
252260
#[must_use]
@@ -262,7 +270,7 @@ impl MultiEraBlock {
262270
self.inner.witness_map.as_ref()
263271
}
264272

265-
/// If the Witness exists for a given transaction then return its public key.
273+
/// If the witness exists for a given transaction then return its public key.
266274
#[must_use]
267275
pub fn witness_for_tx(&self, vkey_hash: &VKeyHash, tx_num: TxnIndex) -> Option<VerifyingKey> {
268276
if let Some(witnesses) = self.witness_map() {
@@ -286,7 +294,7 @@ impl Display for MultiEraBlock {
286294
let txns = block.tx_count();
287295
let aux_data = block.has_aux_data();
288296

289-
let fork = if self.immutable() {
297+
let fork = if self.is_immutable() {
290298
"Immutable".to_string()
291299
} else {
292300
format!("Fork: {fork:?}")
@@ -461,6 +469,10 @@ pub(crate) mod tests {
461469
);
462470

463471
assert!(block.is_err());
472+
assert!(block
473+
.unwrap_err()
474+
.to_string()
475+
.contains("Previous slot is not less than current slot"));
464476
}
465477

466478
Ok(())

0 commit comments

Comments
 (0)