Skip to main content

hotmint_types/
context.rs

1use serde::{Deserialize, Serialize};
2
3use crate::block::Height;
4use crate::epoch::EpochNumber;
5use crate::validator::{ValidatorId, ValidatorSet};
6use crate::view::ViewNumber;
7
8/// Context provided to Application trait methods during block processing.
9pub struct BlockContext<'a> {
10    pub height: Height,
11    pub view: ViewNumber,
12    pub proposer: ValidatorId,
13    pub epoch: EpochNumber,
14    pub epoch_start_view: ViewNumber,
15    pub validator_set: &'a ValidatorSet,
16    /// Block timestamp in milliseconds since Unix epoch.
17    pub timestamp: u64,
18    /// Aggregated vote extensions from the previous round's Vote2 messages.
19    /// Only populated for `create_payload` when the previous round committed
20    /// via a DoubleCertificate whose Vote2 round carried extensions.
21    pub vote_extensions: Vec<(ValidatorId, Vec<u8>)>,
22}
23
24/// Lightweight context for transaction validation (mempool admission).
25/// Unlike [`BlockContext`], this does not require a specific block.
26#[derive(Debug, Clone, Serialize, Deserialize)]
27pub struct TxContext {
28    pub height: Height,
29    pub epoch: EpochNumber,
30}
31
32/// Owned version of [`BlockContext`] for cross-process IPC.
33///
34/// `BlockContext<'a>` borrows the `ValidatorSet`, which cannot be sent across
35/// process boundaries. This type owns all its data and is serializable.
36#[derive(Debug, Clone, Serialize, Deserialize)]
37pub struct OwnedBlockContext {
38    pub height: Height,
39    pub view: ViewNumber,
40    pub proposer: ValidatorId,
41    pub epoch: EpochNumber,
42    pub epoch_start_view: ViewNumber,
43    pub validator_set: ValidatorSet,
44    pub timestamp: u64,
45    #[serde(default)]
46    pub vote_extensions: Vec<(ValidatorId, Vec<u8>)>,
47}
48
49impl From<&BlockContext<'_>> for OwnedBlockContext {
50    fn from(ctx: &BlockContext<'_>) -> Self {
51        Self {
52            height: ctx.height,
53            view: ctx.view,
54            proposer: ctx.proposer,
55            epoch: ctx.epoch,
56            epoch_start_view: ctx.epoch_start_view,
57            validator_set: ctx.validator_set.clone(),
58            timestamp: ctx.timestamp,
59            vote_extensions: ctx.vote_extensions.clone(),
60        }
61    }
62}