Skip to main content

chia_sdk_driver/clear_signing/
facts.rs

1use std::{cmp::min, collections::HashSet};
2
3use chia_protocol::Bytes32;
4
5#[derive(Debug, Default, Clone)]
6pub struct Facts {
7    actual_expiration_time: Option<u64>,
8    required_expiration_time: Option<u64>,
9    reserved_fees: u128,
10    asserted_puzzle_announcements: HashSet<Bytes32>,
11    asserted_spends: HashSet<Bytes32>,
12}
13
14impl Facts {
15    /// Updates the transaction's expiration time to the minimum of the current expiration time and
16    /// the given time. This is used to ensure that the transaction will not be valid after the given
17    /// time (i.e., after a clawback expires).
18    pub fn update_actual_expiration_time(&mut self, expiration_time: u64) {
19        if let Some(old_time) = self.actual_expiration_time {
20            self.actual_expiration_time = Some(min(old_time, expiration_time));
21        } else {
22            self.actual_expiration_time = Some(expiration_time);
23        }
24    }
25
26    /// Updates the required expiration time to the minimum of the current required expiration time and
27    /// the given time. This is used to ensure that the transaction will not be valid after the given
28    /// time (i.e., after a clawback expires).
29    pub fn update_required_expiration_time(&mut self, required_expiration_time: u64) {
30        if let Some(old_time) = self.required_expiration_time {
31            self.required_expiration_time = Some(min(old_time, required_expiration_time));
32        } else {
33            self.required_expiration_time = Some(required_expiration_time);
34        }
35    }
36
37    /// Adds to the total reserved fees, from coins that have been validated to be linked.
38    pub fn add_reserved_fees(&mut self, amount: u64) {
39        self.reserved_fees += u128::from(amount);
40    }
41
42    /// Adds an announcement id to the set of asserted puzzle announcements.
43    pub fn assert_puzzle_announcement(&mut self, announcement_id: Bytes32) {
44        self.asserted_puzzle_announcements.insert(announcement_id);
45    }
46
47    /// Adds a coin id to the set of asserted spends.
48    pub fn assert_spend(&mut self, coin_id: Bytes32) {
49        self.asserted_spends.insert(coin_id);
50    }
51
52    pub fn actual_expiration_time(&self) -> Option<u64> {
53        self.actual_expiration_time
54    }
55
56    pub fn required_expiration_time(&self) -> Option<u64> {
57        self.required_expiration_time
58    }
59
60    pub fn reserved_fees(&self) -> u128 {
61        self.reserved_fees
62    }
63
64    pub fn is_puzzle_announcement_asserted(&self, announcement_id: Bytes32) -> bool {
65        self.asserted_puzzle_announcements
66            .contains(&announcement_id)
67    }
68
69    pub fn is_spend_asserted(&self, coin_id: Bytes32) -> bool {
70        self.asserted_spends.contains(&coin_id)
71    }
72}