forest/lotus_json/
block_header.rs

1// Copyright 2019-2025 ChainSafe Systems
2// SPDX-License-Identifier: Apache-2.0, MIT
3
4use crate::beacon::BeaconEntry;
5use crate::lotus_json::*;
6use crate::shim::sector::PoStProof;
7use crate::{
8    blocks::{ElectionProof, Ticket, TipsetKey},
9    shim::address::Address,
10    shim::{crypto::Signature, econ::TokenAmount},
11};
12use ::cid::Cid;
13use num::BigInt;
14use schemars::JsonSchema;
15use serde::{Deserialize, Serialize};
16
17use crate::blocks::{CachingBlockHeader, RawBlockHeader};
18
19#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, JsonSchema)]
20#[serde(rename_all = "PascalCase")]
21#[schemars(rename = "BlockHeader")]
22pub struct BlockHeaderLotusJson {
23    #[schemars(with = "LotusJson<Address>")]
24    #[serde(with = "crate::lotus_json")]
25    miner: Address,
26    #[schemars(with = "LotusJson<Option<Ticket>>")]
27    #[serde(
28        with = "crate::lotus_json",
29        skip_serializing_if = "Option::is_none",
30        default
31    )]
32    ticket: Option<Ticket>,
33    #[schemars(with = "LotusJson<Option<ElectionProof>>")]
34    #[serde(
35        with = "crate::lotus_json",
36        skip_serializing_if = "Option::is_none",
37        default
38    )]
39    election_proof: Option<ElectionProof>,
40    #[schemars(with = "LotusJson<Vec<BeaconEntry>>")]
41    #[serde(with = "crate::lotus_json")]
42    beacon_entries: Vec<BeaconEntry>,
43    #[schemars(with = "LotusJson<Vec<PoStProof>>")]
44    #[serde(with = "crate::lotus_json")]
45    win_po_st_proof: Vec<PoStProof>,
46    #[schemars(with = "LotusJson<TipsetKey>")]
47    #[serde(with = "crate::lotus_json")]
48    parents: TipsetKey,
49    #[schemars(with = "LotusJson<BigInt>")]
50    #[serde(with = "crate::lotus_json")]
51    parent_weight: BigInt,
52    height: i64,
53    #[schemars(with = "LotusJson<Cid>")]
54    #[serde(with = "crate::lotus_json")]
55    parent_state_root: Cid,
56    #[schemars(with = "LotusJson<Cid>")]
57    #[serde(with = "crate::lotus_json")]
58    parent_message_receipts: Cid,
59    #[schemars(with = "LotusJson<Cid>")]
60    #[serde(with = "crate::lotus_json")]
61    messages: Cid,
62    #[schemars(with = "LotusJson<Option<Signature>>")]
63    #[serde(
64        with = "crate::lotus_json",
65        skip_serializing_if = "Option::is_none",
66        default
67    )]
68    b_l_s_aggregate: Option<Signature>,
69    timestamp: u64,
70    #[schemars(with = "LotusJson<Option<Signature>>")]
71    #[serde(
72        with = "crate::lotus_json",
73        skip_serializing_if = "Option::is_none",
74        default
75    )]
76    block_sig: Option<Signature>,
77    fork_signaling: u64,
78    #[schemars(with = "LotusJson<TokenAmount>")]
79    #[serde(with = "crate::lotus_json")]
80    parent_base_fee: TokenAmount,
81}
82
83impl HasLotusJson for CachingBlockHeader {
84    type LotusJson = BlockHeaderLotusJson;
85
86    #[cfg(test)]
87    fn snapshots() -> Vec<(serde_json::Value, Self)> {
88        use serde_json::json;
89
90        vec![(
91            json!({
92                "BeaconEntries": null,
93                "Miner": "f00",
94                "Parents": [{"/":"bafyreiaqpwbbyjo4a42saasj36kkrpv4tsherf2e7bvezkert2a7dhonoi"}],
95                "ParentWeight": "0",
96                "Height": 0,
97                "ParentStateRoot": {
98                    "/": "baeaaaaa"
99                },
100                "ParentMessageReceipts": {
101                    "/": "baeaaaaa"
102                },
103                "Messages": {
104                    "/": "baeaaaaa"
105                },
106                "WinPoStProof": null,
107                "Timestamp": 0,
108                "ForkSignaling": 0,
109                "ParentBaseFee": "0",
110            }),
111            CachingBlockHeader::default(),
112        )]
113    }
114
115    fn into_lotus_json(self) -> Self::LotusJson {
116        let RawBlockHeader {
117            miner_address,
118            ticket,
119            election_proof,
120            beacon_entries,
121            winning_post_proof,
122            parents,
123            weight,
124            epoch,
125            state_root,
126            message_receipts,
127            messages,
128            bls_aggregate,
129            timestamp,
130            signature,
131            fork_signal,
132            parent_base_fee,
133        } = self.into_raw();
134        Self::LotusJson {
135            miner: miner_address,
136            ticket,
137            election_proof,
138            beacon_entries,
139            win_po_st_proof: winning_post_proof,
140            parents,
141            parent_weight: weight,
142            height: epoch,
143            parent_state_root: state_root,
144            parent_message_receipts: message_receipts,
145            messages,
146            b_l_s_aggregate: bls_aggregate,
147            timestamp,
148            block_sig: signature,
149            fork_signaling: fork_signal,
150            parent_base_fee,
151        }
152    }
153
154    fn from_lotus_json(lotus_json: Self::LotusJson) -> Self {
155        let Self::LotusJson {
156            miner,
157            ticket,
158            election_proof,
159            beacon_entries,
160            win_po_st_proof,
161            parents,
162            parent_weight,
163            height,
164            parent_state_root,
165            parent_message_receipts,
166            messages,
167            b_l_s_aggregate,
168            timestamp,
169            block_sig,
170            fork_signaling,
171            parent_base_fee,
172        } = lotus_json;
173        Self::new(RawBlockHeader {
174            parents,
175            weight: parent_weight,
176            epoch: height,
177            beacon_entries,
178            winning_post_proof: win_po_st_proof,
179            miner_address: miner,
180            messages,
181            message_receipts: parent_message_receipts,
182            state_root: parent_state_root,
183            fork_signal: fork_signaling,
184            signature: block_sig,
185            election_proof,
186            timestamp,
187            ticket,
188            bls_aggregate: b_l_s_aggregate,
189            parent_base_fee,
190        })
191    }
192}