Skip to content

Commit

Permalink
Merge pull request #285 from Chia-Network/full-unfinished-block
Browse files Browse the repository at this point in the history
add member functions to FullBlock and UnfinishedBlock
  • Loading branch information
arvidn authored Oct 20, 2023
2 parents 3facc85 + da449e3 commit bf14e37
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 0 deletions.
2 changes: 2 additions & 0 deletions chia-protocol/fuzz/fuzz_targets/streamable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ fuzz_target!(|data: &[u8]| {
test::<FoliageBlockData>(data);
test::<Foliage>(data);
test::<FullBlock>(data);
test::<UnfinishedBlock>(data);
test::<HeaderBlock>(data);
test::<PoolTarget>(data);
test::<ProofOfSpace>(data);
Expand All @@ -70,6 +71,7 @@ fuzz_target!(|data: &[u8]| {
test::<SubSlotData>(data);
test::<SubEpochChallengeSegment>(data);
test::<SubEpochSegments>(data);
test::<SubEpochSummary>(data);

test::<Handshake>(data);

Expand Down
117 changes: 117 additions & 0 deletions chia-protocol/src/fullblock.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
use chia_streamable_macro::Streamable;

use crate::streamable_struct;
use crate::Bytes32;
use crate::Coin;
use crate::EndOfSubSlotBundle;
use crate::Program;
use crate::RewardChainBlock;
use crate::VDFProof;
use crate::{Foliage, FoliageTransactionBlock, TransactionsInfo};
use chia_traits::Streamable;

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

streamable_struct! (FullBlock {
finished_sub_slots: Vec<EndOfSubSlotBundle>,
Expand All @@ -21,3 +29,112 @@ streamable_struct! (FullBlock {
transactions_generator: Option<Program>, // Program that generates transactions
transactions_generator_ref_list: Vec<u32>, // List of block heights of previous generators referenced in this block
});

impl FullBlock {
pub fn prev_header_hash(&self) -> Bytes32 {
self.foliage.prev_block_hash
}

pub fn header_hash(&self) -> Bytes32 {
self.foliage.hash().into()
}

pub fn is_transaction_block(&self) -> bool {
self.foliage.foliage_transaction_block_hash.is_some()
}

pub fn total_iters(&self) -> u128 {
self.reward_chain_block.total_iters
}

pub fn height(&self) -> u32 {
self.reward_chain_block.height
}

pub fn weight(&self) -> u128 {
self.reward_chain_block.weight
}

pub fn get_included_reward_coins(&self) -> Vec<Coin> {
if let Some(ti) = &self.transactions_info {
ti.reward_claims_incorporated.clone()
} else {
vec![]
}
}

pub fn is_fully_compactified(&self) -> bool {
for sub_slot in &self.finished_sub_slots {
if sub_slot.proofs.challenge_chain_slot_proof.witness_type != 0
|| !sub_slot
.proofs
.challenge_chain_slot_proof
.normalized_to_identity
{
return false;
}
if let Some(proof) = &sub_slot.proofs.infused_challenge_chain_slot_proof {
if proof.witness_type != 0 || !proof.normalized_to_identity {
return false;
}
}
}

if let Some(proof) = &self.challenge_chain_sp_proof {
if proof.witness_type != 0 || !proof.normalized_to_identity {
return false;
}
}
self.challenge_chain_ip_proof.witness_type == 0
&& self.challenge_chain_ip_proof.normalized_to_identity
}
}

#[cfg(feature = "py-bindings")]
#[pymethods]
impl FullBlock {
#[getter]
#[pyo3(name = "prev_header_hash")]
fn py_prev_header_hash(&self) -> Bytes32 {
self.prev_header_hash()
}

#[getter]
#[pyo3(name = "header_hash")]
fn py_header_hash(&self) -> Bytes32 {
self.header_hash()
}

#[pyo3(name = "is_transaction_block")]
fn py_is_transaction_block(&self) -> bool {
self.is_transaction_block()
}

#[getter]
#[pyo3(name = "total_iters")]
fn py_total_iters(&self) -> u128 {
self.total_iters()
}

#[getter]
#[pyo3(name = "height")]
fn py_height(&self) -> u32 {
self.height()
}

#[getter]
#[pyo3(name = "weight")]
fn py_weight(&self) -> u128 {
self.weight()
}

#[pyo3(name = "get_included_reward_coins")]
fn py_get_included_reward_coins(&self) -> HashSet<Coin> {
HashSet::from_iter(self.get_included_reward_coins())
}

#[pyo3(name = "is_fully_compactified")]
fn py_is_fully_compactified(&self) -> bool {
self.is_fully_compactified()
}
}
50 changes: 50 additions & 0 deletions chia-protocol/src/unfinished_block.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
use chia_streamable_macro::Streamable;

use crate::streamable_struct;
use crate::Bytes32;
use crate::EndOfSubSlotBundle;
use crate::Program;
use crate::RewardChainBlockUnfinished;
use crate::VDFProof;
use crate::{Foliage, FoliageTransactionBlock, TransactionsInfo};
use chia_traits::Streamable;

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

streamable_struct! (UnfinishedBlock {
// Full block, without the final VDFs
Expand All @@ -19,3 +24,48 @@ streamable_struct! (UnfinishedBlock {
transactions_generator: Option<Program>, // Program that generates transactions
transactions_generator_ref_list: Vec<u32>, // List of block heights of previous generators referenced in this block
});

impl UnfinishedBlock {
pub fn prev_header_hash(&self) -> Bytes32 {
self.foliage.prev_block_hash
}

pub fn partial_hash(&self) -> Bytes32 {
self.reward_chain_block.hash().into()
}

pub fn is_transaction_block(&self) -> bool {
self.foliage.foliage_transaction_block_hash.is_some()
}

pub fn total_iters(&self) -> u128 {
self.reward_chain_block.total_iters
}
}

#[cfg(feature = "py-bindings")]
#[pymethods]
impl UnfinishedBlock {
#[getter]
#[pyo3(name = "prev_header_hash")]
fn py_prev_header_hash(&self) -> Bytes32 {
self.prev_header_hash()
}

#[getter]
#[pyo3(name = "prev_header_hash")]
fn py_partial_hash(&self) -> Bytes32 {
self.partial_hash()
}

#[pyo3(name = "is_transaction_block")]
fn py_is_transaction_block(&self) -> bool {
self.is_transaction_block()
}

#[getter]
#[pyo3(name = "total_iters")]
fn py_total_iters(&self) -> u128 {
self.total_iters()
}
}
14 changes: 14 additions & 0 deletions wheel/chia_rs.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ class Handshake:
class ClassgroupElement:
data: bytes100
@staticmethod
def create(bytes) -> ClassgroupElement: ...
@staticmethod
def get_default_element() -> ClassgroupElement: ...
@staticmethod
def get_size() -> int: ...
Expand Down Expand Up @@ -704,6 +706,14 @@ class FullBlock:
transactions_info: Optional[TransactionsInfo]
transactions_generator: Optional[Program]
transactions_generator_ref_list: List[int]
prev_header_hash: bytes32
header_hash: bytes32
def is_transaction_block(self) -> bool: ...
total_iters: int
height: int
weight: int
def get_included_reward_coins(self) -> List[Coin]: ...
def is_fully_compactified(self) -> bool: ...
def __init__(
self,
finished_sub_slots: Sequence[EndOfSubSlotBundle],
Expand Down Expand Up @@ -1151,6 +1161,10 @@ class UnfinishedBlock:
transactions_info: Optional[TransactionsInfo]
transactions_generator: Optional[Program]
transactions_generator_ref_list: List[int]
prev_header_hash: bytes32
partial_hash: bytes32
def is_transaction_block(self) -> bool: ...
total_iters: int
def __init__(
self,
finished_sub_slots: Sequence[EndOfSubSlotBundle],
Expand Down
17 changes: 17 additions & 0 deletions wheel/generate_type_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,26 @@ def parse_rust_source(filename: str) -> List[Tuple[str, List[str]]]:
"def name(self) -> bytes32: ...",
],
"ClassgroupElement": [
"@staticmethod\n def create(bytes) -> ClassgroupElement: ...",
"@staticmethod\n def get_default_element() -> ClassgroupElement: ...",
"@staticmethod\n def get_size() -> int: ...",
],
"UnfinishedBlock": [
"prev_header_hash: bytes32",
"partial_hash: bytes32",
"def is_transaction_block(self) -> bool: ...",
"total_iters: int",
],
"FullBlock": [
"prev_header_hash: bytes32",
"header_hash: bytes32",
"def is_transaction_block(self) -> bool: ...",
"total_iters: int",
"height: int",
"weight: int",
"def get_included_reward_coins(self) -> List[Coin]: ...",
"def is_fully_compactified(self) -> bool: ...",
],
}

classes = []
Expand Down

0 comments on commit bf14e37

Please sign in to comment.