essential_node_types/block/
addr.rs

1//! The `Address` implementation for `Block` along with related fns for producing the block addr.
2
3use super::{Block, Header};
4use essential_hash::Address;
5use essential_types::ContentAddress;
6
7impl Address for Block {
8    fn content_address(&self) -> ContentAddress {
9        from_block(self)
10    }
11}
12
13/// Shorthand for the common case of producing a block's content address
14/// from a [`Block`].
15///
16/// *Note:* this also hashes each solution set.
17/// If you already have the content address for each solution set, consider
18/// using [`from_header_and_solution_set_addrs`] or [`from_header_and_solution_set_addrs_slice`].
19pub fn from_block(block: &Block) -> ContentAddress {
20    let solution_addrs = block.solution_sets.iter().map(essential_hash::content_addr);
21    from_header_and_solution_set_addrs(&block.header, solution_addrs)
22}
23
24/// Given the content address for each solution set in the block, produce the
25/// block's content address.
26///
27/// *Warning:* the caller **must** ensure that the order of the solution sets
28/// matches the order of the solution sets in the block.
29/// Otherwise the content address will be different then the one calculated
30/// for the [`Block`].
31pub fn from_header_and_solution_set_addrs(
32    header: &Header,
33    solution_set_addrs: impl IntoIterator<Item = ContentAddress>,
34) -> ContentAddress {
35    let solution_set_addrs: Vec<ContentAddress> = solution_set_addrs.into_iter().collect();
36    from_header_and_solution_set_addrs_slice(header, &solution_set_addrs)
37}
38
39/// Given the content address for each solution set in the block, produce the
40/// block's content address.
41///
42/// *Warning:* the caller **must** ensure that the order of the solution sets
43/// matches the order of the solution sets in the block.
44/// Otherwise the content address will be different then the one calculated
45/// for the [`Block`].
46pub fn from_header_and_solution_set_addrs_slice(
47    header: &Header,
48    solution_set_addrs: &[ContentAddress],
49) -> ContentAddress {
50    ContentAddress(essential_hash::hash(&(
51        header.number,
52        header.timestamp,
53        solution_set_addrs,
54    )))
55}