Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port UnfinishedHeaderBlock to Rust #479

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions crates/chia-protocol/src/header_block.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use chia_streamable_macro::streamable;

use crate::unfinished_header_block::UnfinishedHeaderBlock;
use crate::Bytes;
use crate::Bytes32;
use crate::EndOfSubSlotBundle;
Expand Down Expand Up @@ -72,6 +73,18 @@ impl HeaderBlock {
pub fn first_in_sub_slot(&self) -> bool {
!self.finished_sub_slots.is_empty()
}

pub fn into_unfinished_header_block(self) -> UnfinishedHeaderBlock {
arvidn marked this conversation as resolved.
Show resolved Hide resolved
UnfinishedHeaderBlock {
finished_sub_slots: self.finished_sub_slots,
reward_chain_block: self.reward_chain_block.get_unfinished(),
challenge_chain_sp_proof: self.challenge_chain_sp_proof,
reward_chain_sp_proof: self.reward_chain_sp_proof,
foliage: self.foliage,
foliage_transaction_block: self.foliage_transaction_block,
transactions_filter: self.transactions_filter,
}
}
}

#[cfg(feature = "py-bindings")]
Expand Down
1 change: 1 addition & 0 deletions crates/chia-protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod slots;
pub mod spend_bundle;
pub mod sub_epoch_summary;
pub mod unfinished_block;
pub mod unfinished_header_block;
pub mod vdf;
pub mod wallet_protocol;
pub mod weight_proof;
Expand Down
74 changes: 74 additions & 0 deletions crates/chia-protocol/src/unfinished_header_block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use chia_streamable_macro::streamable;
use chia_traits::Streamable;

use crate::{
Bytes, Bytes32, EndOfSubSlotBundle, Foliage, FoliageTransactionBlock,
RewardChainBlockUnfinished, VDFProof,
};

#[streamable]
pub struct UnfinishedHeaderBlock {
/// Same as a FullBlock but without TransactionInfo and Generator, used by light clients.
/// If first sb.
finished_sub_slots: Vec<EndOfSubSlotBundle>,

/// Reward chain trunk data.
reward_chain_block: RewardChainBlockUnfinished,

/// If not first sp in sub-slot.
challenge_chain_sp_proof: Option<VDFProof>,

/// If not first sp in sub-slot.
reward_chain_sp_proof: Option<VDFProof>,

/// Reward chain foliage data.
foliage: Foliage,

/// Reward chain foliage data (tx block).
foliage_transaction_block: Option<FoliageTransactionBlock>,

/// Filter for block transactions.
transactions_filter: Bytes,
}

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

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

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

#[cfg(feature = "py-bindings")]
use chia_traits::ChiaToPython;

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

#[cfg(feature = "py-bindings")]
#[pymethods]
impl UnfinishedHeaderBlock {
#[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()
}

#[getter]
#[pyo3(name = "total_iters")]
fn py_total_iters<'a>(&self, py: Python<'a>) -> PyResult<&'a PyAny> {
ChiaToPython::to_python(&self.total_iters(), py)
}
}
44 changes: 44 additions & 0 deletions wheel/python/chia_rs/chia_rs.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2364,6 +2364,50 @@ class UnfinishedBlock:
transactions_generator: Union[ Optional[Program], _Unspec] = _Unspec(),
transactions_generator_ref_list: Union[ List[uint32], _Unspec] = _Unspec()) -> UnfinishedBlock: ...

class UnfinishedHeaderBlock:
finished_sub_slots: List[EndOfSubSlotBundle]
reward_chain_block: RewardChainBlockUnfinished
challenge_chain_sp_proof: Optional[VDFProof]
reward_chain_sp_proof: Optional[VDFProof]
foliage: Foliage
foliage_transaction_block: Optional[FoliageTransactionBlock]
transactions_filter: bytes
def __init__(
self,
finished_sub_slots: Sequence[EndOfSubSlotBundle],
reward_chain_block: RewardChainBlockUnfinished,
challenge_chain_sp_proof: Optional[VDFProof],
reward_chain_sp_proof: Optional[VDFProof],
foliage: Foliage,
foliage_transaction_block: Optional[FoliageTransactionBlock],
transactions_filter: bytes
) -> None: ...
def __hash__(self) -> int: ...
def __repr__(self) -> str: ...
def __richcmp__(self) -> Any: ...
def __deepcopy__(self) -> UnfinishedHeaderBlock: ...
def __copy__(self) -> UnfinishedHeaderBlock: ...
@staticmethod
def from_bytes(bytes) -> UnfinishedHeaderBlock: ...
@staticmethod
def from_bytes_unchecked(bytes) -> UnfinishedHeaderBlock: ...
@staticmethod
def parse_rust(ReadableBuffer, bool = False) -> Tuple[UnfinishedHeaderBlock, int]: ...
def to_bytes(self) -> bytes: ...
def __bytes__(self) -> bytes: ...
def stream_to_bytes(self) -> bytes: ...
def get_hash(self) -> bytes32: ...
def to_json_dict(self) -> Any: ...
@staticmethod
def from_json_dict(json_dict: Any) -> UnfinishedHeaderBlock: ...
def replace(self, *, finished_sub_slots: Union[ List[EndOfSubSlotBundle], _Unspec] = _Unspec(),
reward_chain_block: Union[ RewardChainBlockUnfinished, _Unspec] = _Unspec(),
challenge_chain_sp_proof: Union[ Optional[VDFProof], _Unspec] = _Unspec(),
reward_chain_sp_proof: Union[ Optional[VDFProof], _Unspec] = _Unspec(),
foliage: Union[ Foliage, _Unspec] = _Unspec(),
foliage_transaction_block: Union[ Optional[FoliageTransactionBlock], _Unspec] = _Unspec(),
transactions_filter: Union[ bytes, _Unspec] = _Unspec()) -> UnfinishedHeaderBlock: ...

class VDFInfo:
challenge: bytes32
number_of_iterations: uint64
Expand Down
Loading