|
| 1 | +/** |
| 2 | + * Copyright Soramitsu Co., Ltd. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +#include "blockchain/production/impl/block_producer_impl.hpp" |
| 7 | + |
| 8 | +#include <vector> |
| 9 | + |
| 10 | +#include <gsl/span> |
| 11 | +#include "clock/chain_epoch_clock.hpp" |
| 12 | +#include "codec/cbor/cbor.hpp" |
| 13 | +#include "common/visitor.hpp" |
| 14 | +#include "primitives/cid/cid_of_cbor.hpp" |
| 15 | +#include "storage/amt/amt.hpp" |
| 16 | +#include "storage/ipfs/impl/in_memory_datastore.hpp" |
| 17 | + |
| 18 | +namespace fc::blockchain::production { |
| 19 | + using clock::Time; |
| 20 | + using codec::cbor::decode; |
| 21 | + using crypto::signature::BlsSignature; |
| 22 | + using crypto::signature::Secp256k1Signature; |
| 23 | + using primitives::block::BlockHeader; |
| 24 | + using storage::amt::Amt; |
| 25 | + using storage::amt::Root; |
| 26 | + using storage::ipfs::InMemoryDatastore; |
| 27 | + using vm::message::SignedMessage; |
| 28 | + using vm::message::UnsignedMessage; |
| 29 | + |
| 30 | + BlockProducerImpl::BlockProducerImpl( |
| 31 | + std::shared_ptr<IpfsDatastore> data_store, |
| 32 | + std::shared_ptr<MessageStorage> message_store, |
| 33 | + std::shared_ptr<UTCClock> utc_clock, |
| 34 | + std::shared_ptr<ChainEpochClock> epoch_clock, |
| 35 | + std::shared_ptr<WeightCalculator> weight_calculator, |
| 36 | + std::shared_ptr<BlsProvider> crypto_provider, |
| 37 | + std::shared_ptr<Interpreter> interpreter) |
| 38 | + : data_storage_{std::move(data_store)}, |
| 39 | + message_storage_{std::move(message_store)}, |
| 40 | + clock_{std::move(utc_clock)}, |
| 41 | + epoch_{std::move(epoch_clock)}, |
| 42 | + chain_weight_calculator_{std::move(weight_calculator)}, |
| 43 | + bls_provider_{std::move(crypto_provider)}, |
| 44 | + vm_interpreter_{std::move(interpreter)} {} |
| 45 | + |
| 46 | + outcome::result<BlockProducer::Block> BlockProducerImpl::generate( |
| 47 | + primitives::address::Address miner_address, |
| 48 | + const CID &parent_tipset_id, |
| 49 | + EPostProof proof, |
| 50 | + Ticket ticket, |
| 51 | + std::shared_ptr<Indices> indices) { |
| 52 | + OUTCOME_TRY(parent_tipset, getTipset(parent_tipset_id)); |
| 53 | + OUTCOME_TRY( |
| 54 | + vm_result, |
| 55 | + vm_interpreter_->interpret(data_storage_, parent_tipset, indices)); |
| 56 | + OUTCOME_TRY(parent_weight, |
| 57 | + chain_weight_calculator_->calculateWeight(parent_tipset)); |
| 58 | + std::vector<SignedMessage> messages = |
| 59 | + message_storage_->getTopScored(config::kBlockMaxMessagesCount); |
| 60 | + OUTCOME_TRY(msg_meta, getMessagesMeta(messages)); |
| 61 | + std::vector<UnsignedMessage> bls_messages; |
| 62 | + std::vector<SignedMessage> secp_messages; |
| 63 | + std::vector<crypto::bls::Signature> bls_signatures; |
| 64 | + for (auto &&message : messages) { |
| 65 | + visit_in_place( |
| 66 | + message.signature, |
| 67 | + [&message, &bls_messages, &bls_signatures]( |
| 68 | + const BlsSignature &signature) { |
| 69 | + bls_messages.emplace_back(std::move(message.message)); |
| 70 | + bls_signatures.push_back(signature); |
| 71 | + }, |
| 72 | + [&message, &secp_messages](const Secp256k1Signature &signature) { |
| 73 | + secp_messages.emplace_back(std::move(message)); |
| 74 | + }); |
| 75 | + } |
| 76 | + OUTCOME_TRY(bls_aggregate_sign, |
| 77 | + bls_provider_->aggregateSignatures(bls_signatures)); |
| 78 | + Time now = clock_->nowUTC(); |
| 79 | + OUTCOME_TRY(current_epoch, epoch_->epochAtTime(now)); |
| 80 | + BlockHeader header{ |
| 81 | + .miner = std::move(miner_address), |
| 82 | + .ticket = ticket, |
| 83 | + .epost_proof = std::move(proof), |
| 84 | + .parents = std::move(parent_tipset.cids), |
| 85 | + .parent_weight = parent_weight, |
| 86 | + .height = static_cast<uint64_t>(current_epoch), |
| 87 | + .parent_state_root = std::move(vm_result.state_root), |
| 88 | + .parent_message_receipts = std::move(vm_result.message_receipts), |
| 89 | + .messages = msg_meta.getCID(), |
| 90 | + .bls_aggregate = {std::move_iterator(bls_aggregate_sign.begin()), |
| 91 | + std::move_iterator(bls_aggregate_sign.end())}, |
| 92 | + .timestamp = static_cast<uint64_t>(now.unixTime().count()), |
| 93 | + .block_sig = {}, // Block must be signed be Actor Miner |
| 94 | + .fork_signaling = 0}; |
| 95 | + return Block{.header = std::move(header), |
| 96 | + .bls_messages = std::move(bls_messages), |
| 97 | + .secp_messages = std::move(secp_messages)}; |
| 98 | + } |
| 99 | + |
| 100 | + outcome::result<BlockProducerImpl::Tipset> BlockProducerImpl::getTipset( |
| 101 | + const CID &tipset_id) const { |
| 102 | + auto raw_data = data_storage_->get(tipset_id); |
| 103 | + if (raw_data.has_error()) { |
| 104 | + return BlockProducerError::PARENT_TIPSET_NOT_FOUND; |
| 105 | + } |
| 106 | + auto tipset = codec::cbor::decode<Tipset>(raw_data.value()); |
| 107 | + if (tipset.has_error()) { |
| 108 | + return BlockProducerError::PARENT_TIPSET_INVALID_CONTENT; |
| 109 | + } |
| 110 | + return tipset.value(); |
| 111 | + } |
| 112 | + |
| 113 | + outcome::result<BlockProducerImpl::MsgMeta> |
| 114 | + BlockProducerImpl::getMessagesMeta( |
| 115 | + const std::vector<SignedMessage> &messages) { |
| 116 | + auto bls_backend = std::make_shared<InMemoryDatastore>(); |
| 117 | + auto secp_backend = std::make_shared<InMemoryDatastore>(); |
| 118 | + Amt bls_messages_amt{bls_backend}; |
| 119 | + Amt secp_messages_amt{secp_backend}; |
| 120 | + std::vector<SignedMessage> bls_messages; |
| 121 | + std::vector<SignedMessage> secp_messages; |
| 122 | + for (const auto &msg : messages) { |
| 123 | + visit_in_place( |
| 124 | + msg.signature, |
| 125 | + [&msg, &bls_messages](const BlsSignature &signature) { |
| 126 | + std::ignore = signature; |
| 127 | + bls_messages.push_back(msg); |
| 128 | + }, |
| 129 | + [&msg, &secp_messages](const Secp256k1Signature &signature) { |
| 130 | + std::ignore = signature; |
| 131 | + secp_messages.push_back(msg); |
| 132 | + }); |
| 133 | + } |
| 134 | + for (size_t index = 0; index < bls_messages.size(); ++index) { |
| 135 | + OUTCOME_TRY(message_cid, |
| 136 | + primitives::cid::getCidOfCbor(bls_messages.at(index))); |
| 137 | + OUTCOME_TRY(bls_messages_amt.setCbor(index, message_cid)); |
| 138 | + } |
| 139 | + for (size_t index = 0; index < secp_messages.size(); ++index) { |
| 140 | + OUTCOME_TRY(message_cid, |
| 141 | + primitives::cid::getCidOfCbor(secp_messages.at(index))); |
| 142 | + OUTCOME_TRY(secp_messages_amt.setCbor(index, message_cid)); |
| 143 | + } |
| 144 | + OUTCOME_TRY(bls_root_cid, bls_messages_amt.flush()); |
| 145 | + OUTCOME_TRY(secp_root_cid, secp_messages_amt.flush()); |
| 146 | + MsgMeta msg_meta{}; |
| 147 | + msg_meta.bls_messages = bls_root_cid; |
| 148 | + msg_meta.secpk_messages = secp_root_cid; |
| 149 | + return msg_meta; |
| 150 | + } |
| 151 | +} // namespace fc::blockchain::production |
| 152 | + |
| 153 | +OUTCOME_CPP_DEFINE_CATEGORY(fc::blockchain::production, BlockProducerError, e) { |
| 154 | + using fc::blockchain::production::BlockProducerError; |
| 155 | + switch (e) { |
| 156 | + case (BlockProducerError::PARENT_TIPSET_NOT_FOUND): |
| 157 | + return "Block Generator: failed to load parent tipset"; |
| 158 | + case (BlockProducerError::PARENT_TIPSET_INVALID_CONTENT): |
| 159 | + return "Block Generator: failed to decode parent tipset content"; |
| 160 | + } |
| 161 | +} |
0 commit comments