essential_hash/
address_impl.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use crate::Address;
use essential_types::{
    contract::Contract,
    predicate::Predicate,
    solution::{Solution, SolutionData},
    Block, ContentAddress,
};
use sha2::Digest;

impl Address for Block {
    fn content_address(&self) -> ContentAddress {
        crate::block_addr::from_block(self)
    }
}

impl Address for Predicate {
    fn content_address(&self) -> ContentAddress {
        let Ok(header) = self.encoded_header() else {
            // Invalid predicates can't be hashed.
            return ContentAddress([0; 32]);
        };
        let mut hasher = <sha2::Sha256 as Digest>::new();
        hasher.update(header.fixed_size_header.0);
        hasher.update(header.lens);
        for item in self.programs() {
            hasher.update(item);
        }
        ContentAddress(hasher.finalize().into())
    }
}

impl Address for Contract {
    fn content_address(&self) -> ContentAddress {
        crate::contract_addr::from_contract(self)
    }
}

/// Hash the [`SolutionData`] in a manner that treats `state_mutations` like a set.
///
/// Hashing occurs as follows:
///
/// - predicate_to_solve.contract.0
impl Address for SolutionData {
    fn content_address(&self) -> ContentAddress {
        ContentAddress(crate::hash(self))
    }
}

impl Address for Solution {
    fn content_address(&self) -> ContentAddress {
        crate::solution_addr::from_solution(self)
    }
}