Skip to content

Commit ab18ab9

Browse files
authored
feat(rust/cardano-chain-follower): Make the witness map logic on a MultiEraBlock public. (#18)
* fix(rust/cardano-chain-follower): Expose function to lookup a certs private key in a transaction. Also improves documentation on public functions, ands reduces visibility of functions which should not be public. * fix(rust/cardano-chain-follower): Make txn index a u16. Also removes dead_code markers because this functionality is now publicly exposed. * fix(rust): Sync with cat-ci deny.toml config
1 parent f82ab93 commit ab18ab9

File tree

3 files changed

+64
-15
lines changed

3 files changed

+64
-15
lines changed

rust/cardano-chain-follower/src/multi_era_block_data.rs

+55-4
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ impl MultiEraBlock {
158158
/// # Errors
159159
///
160160
/// If the given bytes cannot be decoded as a multi-era block, an error is returned.
161-
pub fn new(
161+
pub(crate) fn new(
162162
chain: Network, raw_data: Vec<u8>, previous: &Point, fork: u64,
163163
) -> anyhow::Result<Self, Error> {
164164
// This lets us reliably count any bad block arising from deserialization.
@@ -170,55 +170,93 @@ impl MultiEraBlock {
170170
}
171171

172172
/// Remake the block on a new fork.
173-
pub fn set_fork(&mut self, fork: u64) {
173+
pub(crate) fn set_fork(&mut self, fork: u64) {
174174
self.fork = fork;
175175
}
176176

177177
/// Decodes the data into a multi-era block.
178+
///
179+
/// # Returns
180+
/// The decoded block data, which can easily be processed by a consumer.
178181
#[must_use]
179182
#[allow(clippy::missing_panics_doc)]
180183
pub fn decode(&self) -> &pallas::ledger::traverse::MultiEraBlock {
181184
self.inner.data.borrow_block()
182185
}
183186

184187
/// Decodes the data into a multi-era block.
188+
///
189+
/// # Returns
190+
/// The raw byte data of the block.
185191
#[must_use]
186192
#[allow(clippy::missing_panics_doc)]
187193
pub fn raw(&self) -> &Vec<u8> {
188194
self.inner.data.borrow_raw_data()
189195
}
190196

191197
/// Returns the block point of this block.
198+
///
199+
/// # Returns
200+
/// The block point of this block.
192201
#[must_use]
193202
pub fn point(&self) -> Point {
194203
self.inner.point.clone()
195204
}
196205

197206
/// Returns the block point of the previous block.
207+
///
208+
/// # Returns
209+
/// The previous blocks `Point`
198210
#[must_use]
199211
pub fn previous(&self) -> Point {
200212
self.inner.previous.clone()
201213
}
202214

203215
/// Is the block data immutable on-chain.
216+
///
217+
/// Immutable blocks are by-definition those that exist in the Mithril Snapshot
218+
/// (Immutable Database) of the Node.
219+
///
220+
/// # Returns
221+
/// `true` if the block is immutable, `false` otherwise.
204222
#[must_use]
205223
pub fn immutable(&self) -> bool {
206224
self.fork == 0
207225
}
208226

209-
/// Is the block data immutable on-chain.
227+
/// What fork is the block from.
228+
///
229+
/// The fork is a synthetic number that represents how many rollbacks have been
230+
/// detected in the running chain. The fork is:
231+
/// - 0 - for all immutable data;
232+
/// - 1 - for any data read from the blockchain during a *backfill* on initial sync
233+
/// - 2+ - for each subsequent rollback detected while reading live blocks.
234+
///
235+
/// # Returns
236+
/// The fork the block was found on.
210237
#[must_use]
211238
pub fn fork(&self) -> u64 {
212239
self.fork
213240
}
214241

215242
/// What chain was the block from
243+
///
244+
/// # Returns
245+
/// - The chain that this block originated on.
216246
#[must_use]
217247
pub fn chain(&self) -> Network {
218248
self.inner.chain
219249
}
220250

221251
/// Get The Decoded Metadata fora a transaction and known label from the block
252+
///
253+
/// # Parameters
254+
/// - `txn_idx` - Index of the Transaction in the Block
255+
/// - `label` - The label of the transaction
256+
///
257+
/// # Returns
258+
/// - Metadata for the given label in the transaction.
259+
/// - Or None if the label requested isn't present.
222260
#[must_use]
223261
pub fn txn_metadata(
224262
&self, txn_idx: usize, label: u64,
@@ -233,10 +271,23 @@ impl MultiEraBlock {
233271
}
234272

235273
/// Returns the witness map for the block.
236-
#[allow(dead_code)]
237274
pub(crate) fn witness_map(&self) -> Option<&TxWitness> {
238275
self.inner.witness_map.as_ref()
239276
}
277+
278+
/// If the Witness exists for a given transaction then return its public key.
279+
#[must_use]
280+
pub fn witness_for_tx(&self, vkey_hash: &[u8; 28], tx_num: u16) -> Option<Vec<u8>> {
281+
if let Some(witnesses) = self.witness_map() {
282+
if witnesses.check_witness_in_tx(vkey_hash, tx_num) {
283+
if let Some(pub_key) = witnesses.get_witness_pk_addr(vkey_hash) {
284+
return Some(pub_key.into());
285+
}
286+
}
287+
}
288+
289+
None
290+
}
240291
}
241292

242293
impl Display for MultiEraBlock {

rust/cardano-chain-follower/src/witness.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@ use crate::utils::blake2b_244;
99
/// `WitnessMap` type of `DashMap` with
1010
/// key as [u8; 28] = (`blake2b_244` hash of the public key)
1111
/// value as `(Bytes, Vec<u8>) = (public key, tx index within the block)`
12-
#[allow(dead_code)]
13-
pub(crate) type WitnessMap = DashMap<[u8; 28], (Bytes, Vec<u8>)>;
12+
pub(crate) type WitnessMap = DashMap<[u8; 28], (Bytes, Vec<u16>)>;
1413

1514
#[derive(Debug)]
16-
#[allow(dead_code)]
1715
/// `TxWitness` struct to store the witness data.
1816
pub(crate) struct TxWitness(WitnessMap);
1917

20-
#[allow(dead_code)]
2118
impl TxWitness {
2219
/// Create a new `TxWitness` from a list of `MultiEraTx`.
2320
pub(crate) fn new(txs: &[MultiEraTx]) -> anyhow::Result<Self> {
@@ -29,9 +26,9 @@ impl TxWitness {
2926
if let Some(vkey_witness_set) = witness_set.vkeywitness.clone() {
3027
for vkey_witness in vkey_witness_set {
3128
let vkey_hash = blake2b_244(&vkey_witness.vkey)?;
32-
let tx_num = u8::try_from(i)?;
29+
let tx_num = u16::try_from(i)?;
3330
map.entry(vkey_hash)
34-
.and_modify(|entry: &mut (_, Vec<u8>)| entry.1.push(tx_num))
31+
.and_modify(|entry: &mut (_, Vec<u16>)| entry.1.push(tx_num))
3532
.or_insert((vkey_witness.vkey.clone(), vec![tx_num]));
3633
}
3734
};
@@ -41,9 +38,9 @@ impl TxWitness {
4138
if let Some(vkey_witness_set) = witness_set.vkeywitness.clone() {
4239
for vkey_witness in vkey_witness_set {
4340
let vkey_hash = blake2b_244(&vkey_witness.vkey)?;
44-
let tx_num = u8::try_from(i)?;
41+
let tx_num = u16::try_from(i)?;
4542
map.entry(vkey_hash)
46-
.and_modify(|entry: &mut (_, Vec<u8>)| entry.1.push(tx_num))
43+
.and_modify(|entry: &mut (_, Vec<u16>)| entry.1.push(tx_num))
4744
.or_insert((vkey_witness.vkey.clone(), vec![tx_num]));
4845
}
4946
}
@@ -53,9 +50,9 @@ impl TxWitness {
5350
if let Some(vkey_witness_set) = &witness_set.vkeywitness.clone() {
5451
for vkey_witness in vkey_witness_set {
5552
let vkey_hash = blake2b_244(&vkey_witness.vkey)?;
56-
let tx_num = u8::try_from(i)?;
53+
let tx_num = u16::try_from(i)?;
5754
map.entry(vkey_hash)
58-
.and_modify(|entry: &mut (_, Vec<u8>)| entry.1.push(tx_num))
55+
.and_modify(|entry: &mut (_, Vec<u16>)| entry.1.push(tx_num))
5956
.or_insert((vkey_witness.vkey.clone(), vec![tx_num]));
6057
}
6158
}
@@ -67,7 +64,7 @@ impl TxWitness {
6764
}
6865

6966
/// Check whether the public key hash is in the given transaction number.
70-
pub(crate) fn check_witness_in_tx(&self, vkey_hash: &[u8; 28], tx_num: u8) -> bool {
67+
pub(crate) fn check_witness_in_tx(&self, vkey_hash: &[u8; 28], tx_num: u16) -> bool {
7168
self.0
7269
.get(vkey_hash)
7370
.map_or(false, |entry| entry.1.contains(&tx_num))

rust/deny.toml

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ version = 2
1919
ignore = [
2020
{ id = "RUSTSEC-2020-0168", reason = "`mach` is used by wasmtime and we have no control over that." },
2121
{ id = "RUSTSEC-2021-0145", reason = "we don't target windows, and don't use a custom global allocator." },
22+
{ id = "RUSTSEC-2024-0370", reason = "`proc-macro-error` is used by crates we rely on, we can't control what they use."},
2223
]
2324

2425
[bans]

0 commit comments

Comments
 (0)