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

chore: expose more block members #243

Merged
merged 9 commits into from
Oct 4, 2023
181 changes: 174 additions & 7 deletions workspaces/src/types/block.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
use near_account_id::AccountId;
use near_primitives::views::{BlockHeaderView, BlockView};

use crate::types::{Balance, ChunkHeader};
use crate::{BlockHeight, CryptoHash};

/// Struct containing information on block coming from the network
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Block {
author: AccountId,
header: BlockHeader,
chunks: Vec<ChunkHeader>,
}

impl Block {
/// The account id of the block author.
pub fn author(&self) -> &AccountId {
&self.author
}

/// The block header info.
pub fn header(&self) -> &BlockHeader {
&self.header
}

/// The list of chunks in this block.
pub fn chunks(&self) -> &[ChunkHeader] {
&self.chunks
}

/// The block timestamp in nanoseconds.
pub fn timestamp(&self) -> u64 {
self.header.timestamp_nanosec
Expand All @@ -32,18 +51,148 @@ impl Block {

/// The block header info. This is a non-exhaustive list of items that
/// could be present in a block header. More can be added in the future.
#[derive(Clone, Debug, PartialEq)]
struct BlockHeader {
///
/// NOTE: For maintainability purposes, some items have been excluded. If required,
/// please submit an issue to [workspaces](https://github.com/near/workspaces-rs/issues).
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct BlockHeader {
height: BlockHeight,
epoch_id: CryptoHash,
next_epoch_id: CryptoHash,
hash: CryptoHash,
prev_hash: CryptoHash,
timestamp_nanosec: u64,
random_value: CryptoHash,
gas_price: Balance,
block_ordinal: Option<u64>,
total_supply: Balance,
last_final_block: CryptoHash,
last_ds_final_block: CryptoHash,
next_bp_hash: CryptoHash,
latest_protocol_version: u32,

prev_state_root: CryptoHash,
chunk_receipts_root: CryptoHash,
chunk_headers_root: CryptoHash,
chunk_tx_root: CryptoHash,
outcome_root: CryptoHash,
challenges_root: CryptoHash,
block_merkle_root: CryptoHash,
}

impl BlockHeader {
/// Current height of this block.
pub fn height(&self) -> BlockHeight {
self.height
}

/// The id of an epoch this block belongs to.
pub fn epoch_id(&self) -> &CryptoHash {
&self.epoch_id
}

/// The next epoch id.
pub fn next_epoch_id(&self) -> &CryptoHash {
&self.next_epoch_id
}

/// The hash of the block itself.
pub fn hash(&self) -> &CryptoHash {
&self.hash
}

/// The hash of the previous block.
pub fn prev_hash(&self) -> &CryptoHash {
&self.prev_hash
}

/// The block timestamp in nanoseconds.
pub fn timestamp_nanosec(&self) -> u64 {
self.timestamp_nanosec
}

/// The random value of the block.
pub fn random_value(&self) -> &CryptoHash {
&self.random_value
}

/// The gas price of the block.
pub fn gas_price(&self) -> Balance {
self.gas_price
}

/// The block ordinal.
pub fn block_ordinal(&self) -> Option<u64> {
self.block_ordinal
}

/// The total supply balance of the block.
pub fn total_supply(&self) -> Balance {
self.total_supply
}

/// The last final block hash.
pub fn last_final_block(&self) -> &CryptoHash {
&self.last_final_block
}

/// The last ds final block hash.
pub fn last_ds_final_block(&self) -> &CryptoHash {
&self.last_ds_final_block
}

/// The next bp hash.
pub fn next_bp_hash(&self) -> &CryptoHash {
&self.next_bp_hash
}

/// The latest protocol version.
pub fn latest_protocol_version(&self) -> u32 {
self.latest_protocol_version
}

/// The previous state root.
pub fn prev_state_root(&self) -> &CryptoHash {
&self.prev_state_root
}

/// The chunk receipts root.
pub fn chunk_receipts_root(&self) -> &CryptoHash {
&self.chunk_receipts_root
}

/// The chunk headers root.
pub fn chunk_headers_root(&self) -> &CryptoHash {
&self.chunk_headers_root
}

/// The chunk tx root.
pub fn chunk_tx_root(&self) -> &CryptoHash {
&self.chunk_tx_root
}

/// The outcome root.
pub fn outcome_root(&self) -> &CryptoHash {
&self.outcome_root
}

/// The challenges root.
pub fn challenges_root(&self) -> &CryptoHash {
&self.challenges_root
}

/// The block merkle root.
pub fn block_merkle_root(&self) -> &CryptoHash {
&self.block_merkle_root
}
}

impl From<BlockView> for Block {
fn from(block_view: BlockView) -> Self {
fn from(view: BlockView) -> Self {
Self {
header: block_view.header.into(),
author: view.author,
header: view.header.into(),
chunks: view.chunks.into_iter().map(Into::into).collect(),
}
}
}
Expand All @@ -52,9 +201,27 @@ impl From<BlockHeaderView> for BlockHeader {
fn from(header_view: BlockHeaderView) -> Self {
Self {
height: header_view.height,
epoch_id: CryptoHash(header_view.epoch_id.0),
hash: CryptoHash(header_view.hash.0),
epoch_id: header_view.epoch_id.into(),
next_epoch_id: header_view.next_epoch_id.into(),
hash: header_view.hash.into(),
prev_hash: header_view.prev_hash.into(),
timestamp_nanosec: header_view.timestamp_nanosec,
random_value: header_view.random_value.into(),
gas_price: header_view.gas_price,
block_ordinal: header_view.block_ordinal,
total_supply: header_view.total_supply,
last_final_block: header_view.last_final_block.into(),
last_ds_final_block: header_view.last_ds_final_block.into(),
next_bp_hash: header_view.next_bp_hash.into(),
latest_protocol_version: header_view.latest_protocol_version,

prev_state_root: header_view.prev_state_root.into(),
chunk_receipts_root: header_view.chunk_receipts_root.into(),
chunk_headers_root: header_view.chunk_headers_root.into(),
chunk_tx_root: header_view.chunk_tx_root.into(),
outcome_root: header_view.outcome_root.into(),
challenges_root: header_view.challenges_root.into(),
block_merkle_root: header_view.block_merkle_root.into(),
}
}
}
43 changes: 24 additions & 19 deletions workspaces/src/types/chunk.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use near_account_id::AccountId;
use near_gas::NearGas;
use near_primitives::views::ChunkView;
use near_primitives::views::{ChunkHeaderView, ChunkView};

use crate::types::{Balance, Gas, ShardId};
use crate::{BlockHeight, CryptoHash};
Expand All @@ -17,8 +17,8 @@ pub struct Chunk {
/// The header belonging to a [`Chunk`]. This is a non-exhaustive list of
/// members belonging to a Chunk, where newer fields can be added in the future.
///
/// NOTE: validator_proposals have been omitted for now. If you need it, submit a ticket to:
/// https://github.com/near/workspaces-rs/issues
/// NOTE: For maintainability purposes, some items have been excluded. If required,
/// please submit an issue to [workspaces](https://github.com/near/workspaces-rs/issues).
#[derive(Debug, Clone, Eq, PartialEq)]
#[non_exhaustive]
pub struct ChunkHeader {
Expand All @@ -43,23 +43,28 @@ impl From<ChunkView> for Chunk {
fn from(view: ChunkView) -> Self {
Self {
author: view.author,
header: ChunkHeader {
chunk_hash: view.header.chunk_hash.into(),
prev_block_hash: view.header.prev_block_hash.into(),
height_created: view.header.height_created,
height_included: view.header.height_included,
shard_id: view.header.shard_id,
gas_used: NearGas::from_gas(view.header.gas_used),
gas_limit: NearGas::from_gas(view.header.gas_limit),
balance_burnt: view.header.balance_burnt,
header: view.header.into(),
}
}
}

tx_root: view.header.tx_root.into(),
outcome_root: view.header.outcome_root.into(),
prev_state_root: view.header.prev_state_root.into(),
outgoing_receipts_root: view.header.outgoing_receipts_root.into(),
encoded_merkle_root: view.header.encoded_merkle_root.into(),
encoded_length: view.header.encoded_length,
},
impl From<ChunkHeaderView> for ChunkHeader {
fn from(view: ChunkHeaderView) -> Self {
ChunkHeader {
chunk_hash: view.chunk_hash.into(),
prev_block_hash: view.prev_block_hash.into(),
height_created: view.height_created,
height_included: view.height_included,
shard_id: view.shard_id,
gas_used: NearGas::from_gas(view.gas_used),
gas_limit: NearGas::from_gas(view.gas_limit),
balance_burnt: view.balance_burnt,
tx_root: view.tx_root.into(),
outcome_root: view.outcome_root.into(),
prev_state_root: view.prev_state_root.into(),
outgoing_receipts_root: view.outgoing_receipts_root.into(),
encoded_merkle_root: view.encoded_merkle_root.into(),
encoded_length: view.encoded_length,
}
}
}
Expand Down
Loading