1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2019-2024 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use crate::message::SignedMessage;
use crate::shim::message::Message;
use cid::Cid;
use fvm_ipld_blockstore::Blockstore;
use serde_tuple::{self, Deserialize_tuple, Serialize_tuple};

use super::CachingBlockHeader;

/// Limit of BLS and SECP messages combined in a block.
pub const BLOCK_MESSAGE_LIMIT: usize = 10000;

/// A complete Filecoin block. This contains the block header as well as all BLS
/// and SECP messages.
#[derive(Clone, Debug, PartialEq)]
pub struct Block {
    pub header: CachingBlockHeader,
    pub bls_messages: Vec<Message>,
    pub secp_messages: Vec<SignedMessage>,
}

impl Block {
    pub fn header(&self) -> &CachingBlockHeader {
        &self.header
    }
    pub fn bls_msgs(&self) -> &[Message] {
        &self.bls_messages
    }
    pub fn secp_msgs(&self) -> &[SignedMessage] {
        &self.secp_messages
    }
    /// Returns block header's CID.
    pub fn cid(&self) -> &Cid {
        self.header.cid()
    }

    /// Persists the block in the given block store
    pub fn persist(&self, db: &impl Blockstore) -> Result<(), crate::chain::store::Error> {
        crate::chain::persist_objects(&db, std::iter::once(self.header()))?;
        crate::chain::persist_objects(&db, self.bls_msgs().iter())?;
        crate::chain::persist_objects(&db, self.secp_msgs().iter())
    }
}

/// Tracks the Merkle roots of both SECP and BLS messages separately.
#[derive(Serialize_tuple, Deserialize_tuple)]
pub struct TxMeta {
    pub bls_message_root: Cid,
    pub secp_message_root: Cid,
}