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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use chik_streamable_macro::streamable;

use crate::{Bytes32, ClassgroupElement, Coin, SubEpochSummary};

#[cfg(feature = "py-bindings")]
use pyo3::prelude::*;

// This class is not included or hashed into the blockchain, but it is kept in memory as a more
// efficient way to maintain data about the blockchain. This allows us to validate future blocks,
// difficulty adjustments, etc, without saving the whole header block in memory.
#[streamable]
pub struct BlockRecord {
    header_hash: Bytes32,
    // Header hash of the previous block
    prev_hash: Bytes32,
    height: u32,
    // Total cumulative difficulty of all ancestor blocks since genesis
    weight: u128,
    // Total number of VDF iterations since genesis, including this block
    total_iters: u128,
    signage_point_index: u8,
    // This is the intermediary VDF output at ip_iters in challenge chain
    challenge_vdf_output: ClassgroupElement,
    // This is the intermediary VDF output at ip_iters in infused cc, iff deficit <= 3
    infused_challenge_vdf_output: Option<ClassgroupElement>,
    // The reward chain infusion output, input to next VDF
    reward_infusion_new_challenge: Bytes32,
    // Hash of challenge chain data, used to validate end of slots in the future
    challenge_block_info_hash: Bytes32,
    // Current network sub_slot_iters parameter
    sub_slot_iters: u64,
    // Need to keep track of these because Coins are created in a future block
    pool_puzzle_hash: Bytes32,
    farmer_puzzle_hash: Bytes32,
    // The number of iters required for this proof of space
    required_iters: u64,
    // A deficit of 16 is an overflow block after an infusion. Deficit of 15 is a challenge block
    deficit: u8,
    overflow: bool,
    prev_transaction_block_height: u32,

    // Transaction block (present iff is_transaction_block)
    timestamp: Option<u64>,
    // Header hash of the previous transaction block
    prev_transaction_block_hash: Option<Bytes32>,
    fees: Option<u64>,
    reward_claims_incorporated: Option<Vec<Coin>>,

    // Slot (present iff this is the first SB in sub slot)
    finished_challenge_slot_hashes: Option<Vec<Bytes32>>,
    finished_infused_challenge_slot_hashes: Option<Vec<Bytes32>>,
    finished_reward_slot_hashes: Option<Vec<Bytes32>>,

    // Sub-epoch (present iff this is the first SB after sub-epoch)
    sub_epoch_summary_included: Option<SubEpochSummary>,
}

impl BlockRecord {
    pub fn is_transaction_block(&self) -> bool {
        self.timestamp.is_some()
    }

    pub fn first_in_sub_slot(&self) -> bool {
        self.finished_challenge_slot_hashes.is_some()
    }

    pub fn is_challenge_block(&self, min_blocks_per_challenge_block: u8) -> bool {
        self.deficit == min_blocks_per_challenge_block - 1
    }
}

#[cfg(feature = "py-bindings")]
use pyo3::types::PyDict;

#[cfg(feature = "py-bindings")]
use pyo3::exceptions::PyValueError;

#[cfg(feature = "py-bindings")]
use chik_traits::ChikToPython;

#[cfg(feature = "py-bindings")]
#[pymethods]
impl BlockRecord {
    #[getter]
    #[pyo3(name = "is_transaction_block")]
    fn py_is_transaction_block(&self) -> bool {
        self.is_transaction_block()
    }

    #[getter]
    #[pyo3(name = "first_in_sub_slot")]
    fn py_first_in_sub_slot(&self) -> bool {
        self.first_in_sub_slot()
    }

    #[pyo3(name = "is_challenge_block")]
    fn py_is_challenge_block(&self, constants: &Bound<PyAny>) -> PyResult<bool> {
        Ok(self.is_challenge_block(
            constants
                .getattr("MIN_BLOCKS_PER_CHALLENGE_BLOCK")?
                .extract::<u8>()?,
        ))
    }

    // TODO: at some point it would be nice to port
    // chik.consensus.pot_iterations to rust, and make this less hacky
    fn sp_sub_slot_total_iters_impl(&self, py: Python, constants: &Bound<PyAny>) -> PyResult<u128> {
        let ret = self
            .total_iters
            .checked_sub(self.ip_iters_impl(py, constants)? as u128)
            .ok_or(PyValueError::new_err("uint128 overflow"))?;
        if self.overflow {
            ret.checked_sub(self.sub_slot_iters as u128)
                .ok_or(PyValueError::new_err("uint128 overflow"))
        } else {
            Ok(ret)
        }
    }

    fn ip_sub_slot_total_iters_impl(&self, py: Python, constants: &Bound<PyAny>) -> PyResult<u128> {
        self.total_iters
            .checked_sub(self.ip_iters_impl(py, constants)? as u128)
            .ok_or(PyValueError::new_err("uint128 overflow"))
    }

    fn sp_iters_impl(&self, py: Python, constants: &Bound<PyAny>) -> PyResult<u64> {
        let ctx = PyDict::new_bound(py);
        ctx.set_item("sub_slot_iters", self.sub_slot_iters)?;
        ctx.set_item("signage_point_index", self.signage_point_index)?;
        ctx.set_item("constants", constants)?;
        py.run_bound(
            "from chik.consensus.pot_iterations import calculate_ip_iters, calculate_sp_iters\n\
            ret = calculate_sp_iters(constants, sub_slot_iters, signage_point_index)\n",
            None,
            Some(&ctx),
        )?;
        ctx.get_item("ret").unwrap().unwrap().extract::<u64>()
    }

    fn ip_iters_impl(&self, py: Python, constants: &Bound<PyAny>) -> PyResult<u64> {
        let ctx = PyDict::new_bound(py);
        ctx.set_item("sub_slot_iters", self.sub_slot_iters)?;
        ctx.set_item("signage_point_index", self.signage_point_index)?;
        ctx.set_item("required_iters", self.required_iters)?;
        ctx.set_item("constants", constants)?;
        py.run_bound(
            "from chik.consensus.pot_iterations import calculate_ip_iters, calculate_sp_iters\n\
            ret = calculate_ip_iters(constants, sub_slot_iters, signage_point_index, required_iters)\n",
            None,
            Some(&ctx),
            )?;
        ctx.get_item("ret").unwrap().unwrap().extract::<u64>()
    }

    fn sp_total_iters_impl(&self, py: Python, constants: &Bound<PyAny>) -> PyResult<u128> {
        self.sp_sub_slot_total_iters_impl(py, constants)?
            .checked_add(self.sp_iters_impl(py, constants)? as u128)
            .ok_or(PyValueError::new_err("uint128 overflow"))
    }

    fn sp_sub_slot_total_iters<'a>(
        &self,
        py: Python<'a>,
        constants: &Bound<PyAny>,
    ) -> PyResult<Bound<'a, PyAny>> {
        ChikToPython::to_python(&self.sp_sub_slot_total_iters_impl(py, constants)?, py)
    }

    fn ip_sub_slot_total_iters<'a>(
        &self,
        py: Python<'a>,
        constants: &Bound<PyAny>,
    ) -> PyResult<Bound<'a, PyAny>> {
        ChikToPython::to_python(&self.ip_sub_slot_total_iters_impl(py, constants)?, py)
    }

    fn sp_iters<'a>(&self, py: Python<'a>, constants: &Bound<PyAny>) -> PyResult<Bound<'a, PyAny>> {
        ChikToPython::to_python(&self.sp_iters_impl(py, constants)?, py)
    }

    fn ip_iters<'a>(&self, py: Python<'a>, constants: &Bound<PyAny>) -> PyResult<Bound<'a, PyAny>> {
        ChikToPython::to_python(&self.ip_iters_impl(py, constants)?, py)
    }

    fn sp_total_iters<'a>(
        &self,
        py: Python<'a>,
        constants: &Bound<PyAny>,
    ) -> PyResult<Bound<'a, PyAny>> {
        ChikToPython::to_python(&self.sp_total_iters_impl(py, constants)?, py)
    }
}