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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright 2019-2024 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use crate::beacon::BeaconEntry;
use crate::blocks::{CachingBlockHeader, Ticket, TipsetKey};
use crate::blocks::{ElectionProof, RawBlockHeader};

use crate::chain::{compute_base_fee, ChainStore};

use crate::fil_cns::weight;
use crate::key_management::{Key, KeyStore};
use crate::lotus_json::lotus_json_with_self;

use crate::lotus_json::LotusJson;
use crate::message::SignedMessage;
use crate::networks::Height;

use crate::rpc::reflect::Permission;
use crate::rpc::types::{ApiTipsetKey, MiningBaseInfo};
use crate::rpc::{ApiPaths, Ctx, RpcMethod, ServerError};
use crate::shim::address::Address;
use crate::shim::clock::ChainEpoch;
use crate::shim::crypto::{Signature, SignatureType};

use crate::shim::sector::PoStProof;
use crate::utils::db::CborStoreExt;

use anyhow::{Context as _, Result};
use bls_signatures::Serialize as _;
use cid::Cid;
use fil_actors_shared::fvm_ipld_amt::Amtv0 as Amt;
use fvm_ipld_blockstore::Blockstore;

use fvm_shared2::crypto::signature::BLS_SIG_LEN;
use group::prime::PrimeCurveAffine as _;
use itertools::Itertools;

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_tuple::Serialize_tuple;
use tokio::sync::RwLock;

use std::sync::Arc;

#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, JsonSchema)]
#[serde(rename_all = "PascalCase")]
pub struct BlockTemplate {
    #[schemars(with = "LotusJson<Address>")]
    #[serde(with = "crate::lotus_json")]
    pub miner: Address,
    #[schemars(with = "LotusJson<TipsetKey>")]
    #[serde(with = "crate::lotus_json")]
    pub parents: TipsetKey,
    #[schemars(with = "LotusJson<Ticket>")]
    #[serde(with = "crate::lotus_json")]
    pub ticket: Ticket,
    #[schemars(with = "LotusJson<ElectionProof>")]
    #[serde(with = "crate::lotus_json")]
    pub eproof: ElectionProof,
    #[schemars(with = "LotusJson<Vec<BeaconEntry>>")]
    #[serde(with = "crate::lotus_json")]
    pub beacon_values: Vec<BeaconEntry>,
    #[schemars(with = "LotusJson<Vec<SignedMessage>>")]
    #[serde(with = "crate::lotus_json")]
    pub messages: Vec<SignedMessage>,
    #[schemars(with = "LotusJson<ChainEpoch>")]
    #[serde(with = "crate::lotus_json")]
    pub epoch: ChainEpoch,
    pub timestamp: u64,
    #[schemars(with = "LotusJson<Vec<PoStProof>>")]
    #[serde(rename = "WinningPoStProof", with = "crate::lotus_json")]
    pub winning_post_proof: Vec<PoStProof>,
}

lotus_json_with_self!(BlockTemplate);

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, JsonSchema)]
#[serde(rename_all = "PascalCase")]
pub struct BlockMessage {
    #[schemars(with = "LotusJson<CachingBlockHeader>")]
    #[serde(with = "crate::lotus_json")]
    header: CachingBlockHeader,
    #[schemars(with = "LotusJson<Vec<Cid>>")]
    #[serde(with = "crate::lotus_json")]
    bls_messages: Vec<Cid>,
    #[schemars(with = "LotusJson<Vec<Cid>>")]
    #[serde(with = "crate::lotus_json")]
    secpk_messages: Vec<Cid>,
}

lotus_json_with_self!(BlockMessage);

#[derive(Serialize_tuple)]
struct MessageMeta {
    bls_messages: Cid,
    secpk_messages: Cid,
}

pub enum MinerCreateBlock {}
impl RpcMethod<1> for MinerCreateBlock {
    const NAME: &'static str = "Filecoin.MinerCreateBlock";
    const PARAM_NAMES: [&'static str; 1] = ["block_template"];
    const API_PATHS: ApiPaths = ApiPaths::V1;
    const PERMISSION: Permission = Permission::Write;

    type Params = (BlockTemplate,);
    type Ok = BlockMessage;

    async fn handle(
        ctx: Ctx<impl Blockstore + Send + Sync + 'static>,
        (block_template,): Self::Params,
    ) -> Result<Self::Ok, ServerError> {
        let store = ctx.store();
        let parent_tipset = ctx
            .chain_index()
            .load_required_tipset(&block_template.parents)?;

        let lookback_state = ChainStore::get_lookback_tipset_for_round(
            ctx.chain_index().clone(),
            ctx.chain_config().clone(),
            parent_tipset.clone(),
            block_template.epoch,
        )
        .map(|(_, s)| Arc::new(s))?;

        let worker = ctx
            .state_manager
            .get_miner_work_addr(*lookback_state, &block_template.miner)?;

        let parent_weight = weight(store, &parent_tipset)?;
        //let parent_weight = parent_tipset.weight().to_owned();
        let parent_base_fee = compute_base_fee(
            store,
            &parent_tipset,
            ctx.chain_config()
                .height_infos
                .get(&Height::Smoke)
                .context("Missing Smoke height")?
                .epoch,
        )?;
        let (state, receipts) = ctx.state_manager.tipset_state(&parent_tipset).await?;

        let network_version = ctx.state_manager.get_network_version(block_template.epoch);

        let mut bls_messages = Vec::new();
        let mut secpk_messages = Vec::new();
        let mut bls_msg_cids = Vec::new();
        let mut secpk_msg_cids = Vec::new();
        let mut bls_sigs = Vec::new();

        for msg in block_template.messages {
            match msg.signature().signature_type() {
                SignatureType::Bls => {
                    let cid = ctx.store().put_cbor_default(&msg.message)?;
                    bls_msg_cids.push(cid);
                    bls_sigs.push(msg.signature);
                    bls_messages.push(msg.message);
                }
                SignatureType::Secp256k1 | SignatureType::Delegated => {
                    if msg.signature.is_valid_secpk_sig_type(network_version) {
                        let cid = ctx.store().put_cbor_default(&msg)?;
                        secpk_msg_cids.push(cid);
                        secpk_messages.push(msg);
                    } else {
                        Err(anyhow::anyhow!(
                            "unknown sig type: {}",
                            msg.signature.signature_type()
                        ))?;
                    }
                }
            }
        }

        let store = ctx.store();
        let mut message_array = Amt::<Cid, _>::new(store);
        for (i, cid) in bls_msg_cids.iter().enumerate() {
            message_array.set(i as u64, *cid)?;
        }
        let bls_msgs_root = message_array.flush()?;
        let mut message_array = Amt::<Cid, _>::new(store);
        for (i, cid) in secpk_msg_cids.iter().enumerate() {
            message_array.set(i as u64, *cid)?;
        }
        let secpk_msgs_root = message_array.flush()?;

        let message_meta_cid = store.put_cbor_default(&MessageMeta {
            bls_messages: bls_msgs_root,
            secpk_messages: secpk_msgs_root,
        })?;

        let bls_aggregate = aggregate_from_bls_signatures(bls_sigs)?;

        let mut block_header = RawBlockHeader {
            miner_address: block_template.miner,
            ticket: block_template.ticket.into(),
            election_proof: block_template.eproof.into(),
            beacon_entries: block_template.beacon_values,
            winning_post_proof: block_template.winning_post_proof,
            parents: block_template.parents,
            weight: parent_weight,
            epoch: block_template.epoch,
            state_root: state,
            message_receipts: receipts,
            messages: message_meta_cid,
            bls_aggregate: bls_aggregate.into(),
            timestamp: block_template.timestamp,
            signature: None,
            fork_signal: Default::default(),
            parent_base_fee,
        };

        block_header.signature = sign_block_header(&block_header, &worker, ctx.keystore.clone())
            .await?
            .into();

        Ok(BlockMessage {
            header: CachingBlockHeader::from(block_header),
            bls_messages: bls_msg_cids,
            secpk_messages: secpk_msg_cids,
        })
    }
}

async fn sign_block_header(
    block_header: &RawBlockHeader,
    worker: &Address,
    keystore: Arc<RwLock<KeyStore>>,
) -> Result<Signature> {
    let signing_bytes = block_header.signing_bytes();

    let mut keystore = keystore.write().await;
    let key = match crate::key_management::find_key(worker, &keystore) {
        Ok(key) => key,
        Err(_) => {
            let key_info = crate::key_management::try_find(worker, &mut keystore)?;
            Key::try_from(key_info)?
        }
    };
    drop(keystore);

    let sig = crate::key_management::sign(
        *key.key_info.key_type(),
        key.key_info.private_key(),
        &signing_bytes,
    )?;
    Ok(sig)
}

fn aggregate_from_bls_signatures(bls_sigs: Vec<Signature>) -> anyhow::Result<Signature> {
    let signatures: Vec<_> = bls_sigs
        .iter()
        .map(|sig| anyhow::Ok(bls_signatures::Signature::from_bytes(sig.bytes())?))
        .try_collect()?;

    if signatures.is_empty() {
        let sig: bls_signatures::Signature = blstrs::G2Affine::identity().into();
        let mut raw_signature: [u8; BLS_SIG_LEN] = [0; BLS_SIG_LEN];
        sig.write_bytes(&mut raw_signature.as_mut())?;
        Ok(Signature::new_bls(raw_signature.to_vec()))
    } else {
        let bls_aggregate =
            bls_signatures::aggregate(&signatures).context("failed to aggregate signatures")?;
        Ok(Signature {
            sig_type: SignatureType::Bls,
            bytes: bls_aggregate.as_bytes().to_vec(),
        })
    }
}

pub enum MinerGetBaseInfo {}
impl RpcMethod<3> for MinerGetBaseInfo {
    const NAME: &'static str = "Filecoin.MinerGetBaseInfo";
    const PARAM_NAMES: [&'static str; 3] = ["address", "epoch", "tsk"];
    const API_PATHS: ApiPaths = ApiPaths::V1;
    const PERMISSION: Permission = Permission::Read;

    type Params = (Address, i64, ApiTipsetKey);
    type Ok = Option<MiningBaseInfo>;

    async fn handle(
        ctx: Ctx<impl Blockstore + Send + Sync + 'static>,
        (address, epoch, ApiTipsetKey(tsk)): Self::Params,
    ) -> Result<Self::Ok, ServerError> {
        let ts = ctx.chain_store().load_required_tipset_or_heaviest(&tsk)?;

        Ok(ctx
            .state_manager
            .miner_get_base_info(ctx.beacon(), ts, address, epoch)
            .await?)
    }
}