Skip to content

Commit 4584e80

Browse files
authored
Update block (#206)
Signed-off-by: turuslan <[email protected]>
1 parent 047c95c commit 4584e80

39 files changed

+419
-749
lines changed

core/api/api.hpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ namespace fc::api {
5959
using primitives::address::Address;
6060
using primitives::block::BlockHeader;
6161
using primitives::block::BlockMsg;
62+
using primitives::block::BlockTemplate;
6263
using primitives::ticket::EPostProof;
6364
using primitives::ticket::Ticket;
6465
using primitives::tipset::HeadChange;
@@ -299,15 +300,7 @@ namespace fc::api {
299300
const TokenAmount &,
300301
const TipsetKey &)
301302

302-
API_METHOD(MinerCreateBlock,
303-
BlockMsg,
304-
const Address &,
305-
const TipsetKey &,
306-
const Ticket &,
307-
const EPostProof &,
308-
const std::vector<SignedMessage> &,
309-
ChainEpoch,
310-
uint64_t)
303+
API_METHOD(MinerCreateBlock, BlockMsg, const BlockTemplate &)
311304
API_METHOD(MinerGetBaseInfo,
312305
MiningBaseInfo,
313306
const Address &,

core/api/make.cpp

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <boost/algorithm/string.hpp>
99
#include <libp2p/peer/peer_id.hpp>
1010

11-
#include "blockchain/production/impl/block_producer_impl.hpp"
11+
#include "blockchain/production/block_producer.hpp"
1212
#include "storage/hamt/hamt.hpp"
1313
#include "vm/actor/builtin/account/account_actor.hpp"
1414
#include "vm/actor/builtin/init/init_actor.hpp"
@@ -33,7 +33,6 @@ namespace fc::api {
3333
using InterpreterResult = vm::interpreter::Result;
3434
using vm::state::StateTreeImpl;
3535
using MarketActorState = vm::actor::builtin::market::State;
36-
using blockchain::production::BlockProducerImpl;
3736
using crypto::randomness::RandomnessProvider;
3837
using crypto::signature::BlsSignature;
3938
using libp2p::peer::PeerId;
@@ -248,34 +247,12 @@ namespace fc::api {
248247
.ClientStartDeal = {},
249248
// TODO(turuslan): FIL-165 implement method
250249
.MarketEnsureAvailable = {},
251-
.MinerCreateBlock = {[=](auto &miner,
252-
auto &parent,
253-
auto &ticket,
254-
auto &proof,
255-
auto &messages,
256-
auto height,
257-
auto timestamp) -> outcome::result<BlockMsg> {
258-
OUTCOME_TRY(context, tipsetContext(parent, true));
259-
BlockProducerImpl producer{
260-
ipld,
261-
nullptr,
262-
nullptr,
263-
nullptr,
264-
weight_calculator,
265-
bls_provider,
266-
std::make_shared<InterpreterImpl>(),
267-
};
250+
.MinerCreateBlock = {[=](auto &t) -> outcome::result<BlockMsg> {
251+
OUTCOME_TRY(context, tipsetContext(t.parents, true));
252+
OUTCOME_TRY(miner_state, context.minerState(t.miner));
268253
OUTCOME_TRY(block,
269-
producer.generate(miner,
270-
context.tipset,
271-
*context.interpreted,
272-
proof,
273-
ticket,
274-
messages,
275-
height,
276-
timestamp));
277-
278-
OUTCOME_TRY(miner_state, context.minerState(miner));
254+
blockchain::production::generate(ipld, std::move(t)));
255+
279256
OUTCOME_TRY(block_signable, codec::cbor::encode(block.header));
280257
OUTCOME_TRY(worker_key, context.accountKey(miner_state.info.worker));
281258
OUTCOME_TRY(block_sig, key_store->sign(worker_key, block_signable));

core/api/rpc/json.hpp

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ namespace fc::api {
3030
using crypto::signature::Signature;
3131
using markets::storage::StorageAsk;
3232
using primitives::BigInt;
33+
using primitives::block::BeaconEntry;
3334
using primitives::block::BlockHeader;
35+
using primitives::block::ElectionProof;
3436
using primitives::cid::getCidOfCbor;
3537
using primitives::sector::PoStProof;
3638
using primitives::sector::RegisteredProof;
@@ -350,7 +352,9 @@ namespace fc::api {
350352
Value j{rapidjson::kObjectType};
351353
Set(j, "Miner", v.miner);
352354
Set(j, "Ticket", v.ticket);
353-
Set(j, "EPostProof", v.epost_proof);
355+
Set(j, "ElectionProof", v.election_proof);
356+
Set(j, "BeaconEntries", v.beacon_entries);
357+
Set(j, "WinPoStProof", v.win_post_proof);
354358
Set(j, "Parents", v.parents);
355359
Set(j, "ParentWeight", v.parent_weight);
356360
Set(j, "Height", v.height);
@@ -367,7 +371,9 @@ namespace fc::api {
367371
DECODE(BlockHeader) {
368372
decode(v.miner, Get(j, "Miner"));
369373
decode(v.ticket, Get(j, "Ticket"));
370-
decode(v.epost_proof, Get(j, "EPostProof"));
374+
decode(v.election_proof, Get(j, "ElectionProof"));
375+
decode(v.beacon_entries, Get(j, "BeaconEntries"));
376+
decode(v.win_post_proof, Get(j, "WinPoStProof"));
371377
decode(v.parents, Get(j, "Parents"));
372378
decode(v.parent_weight, Get(j, "ParentWeight"));
373379
decode(v.height, Get(j, "Height"));
@@ -380,6 +386,40 @@ namespace fc::api {
380386
decode(v.fork_signaling, Get(j, "ForkSignaling"));
381387
}
382388

389+
DECODE(BlockTemplate) {
390+
decode(v.miner, Get(j, "Miner"));
391+
decode(v.parents, Get(j, "Parents"));
392+
decode(v.ticket, Get(j, "Ticket"));
393+
decode(v.election_proof, Get(j, "Eproof"));
394+
decode(v.beacon_entries, Get(j, "BeaconValues"));
395+
decode(v.messages, Get(j, "Messages"));
396+
decode(v.height, Get(j, "Epoch"));
397+
decode(v.timestamp, Get(j, "Timestamp"));
398+
decode(v.win_post_proof, Get(j, "WinningPoStProof"));
399+
}
400+
401+
ENCODE(ElectionProof) {
402+
Value j{rapidjson::kObjectType};
403+
Set(j, "VRFProof", v.vrf_proof);
404+
return j;
405+
}
406+
407+
DECODE(ElectionProof) {
408+
decode(v.vrf_proof, Get(j, "VRFProof"));
409+
}
410+
411+
ENCODE(BeaconEntry) {
412+
Value j{rapidjson::kObjectType};
413+
Set(j, "Round", v.round);
414+
Set(j, "Data", v.data);
415+
return j;
416+
}
417+
418+
DECODE(BeaconEntry) {
419+
decode(v.round, Get(j, "Round"));
420+
decode(v.data, Get(j, "Data"));
421+
}
422+
383423
ENCODE(Tipset) {
384424
Value j{rapidjson::kObjectType};
385425
Set(j, "Cids", v.cids);

core/blockchain/production/CMakeLists.txt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,11 @@
44
#
55

66
add_library(block_producer
7-
impl/block_producer_impl.cpp
7+
block_producer.cpp
88
)
99
target_link_libraries(block_producer
1010
bls_provider
11-
outcome
12-
buffer
13-
address
14-
clock
1511
interpreter
1612
tipset
17-
ipfs_datastore_in_memory
13+
weight_calculator
1814
)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "blockchain/production/block_producer.hpp"
7+
8+
#include "blockchain/impl/weight_calculator_impl.hpp"
9+
#include "crypto/bls/impl/bls_provider_impl.hpp"
10+
#include "vm/interpreter/impl/interpreter_impl.hpp"
11+
12+
namespace fc::blockchain::production {
13+
using crypto::signature::BlsSignature;
14+
using crypto::signature::Secp256k1Signature;
15+
using primitives::block::MsgMeta;
16+
using vm::message::SignedMessage;
17+
using vm::message::UnsignedMessage;
18+
19+
outcome::result<Block> generate(std::shared_ptr<Ipld> ipld, BlockTemplate t) {
20+
OUTCOME_TRY(parent_tipset,
21+
primitives::tipset::Tipset::load(*ipld, t.parents));
22+
OUTCOME_TRY(
23+
vm_result,
24+
vm::interpreter::InterpreterImpl{}.interpret(ipld, parent_tipset));
25+
Block b;
26+
MsgMeta msg_meta;
27+
msg_meta.load(ipld);
28+
std::vector<crypto::bls::Signature> bls_signatures;
29+
for (auto &message : t.messages) {
30+
OUTCOME_TRY(visit_in_place(
31+
message.signature,
32+
[&](const BlsSignature &signature) -> outcome::result<void> {
33+
b.bls_messages.emplace_back(message.message);
34+
bls_signatures.push_back(signature);
35+
OUTCOME_TRY(message_cid, ipld->setCbor(message.message));
36+
OUTCOME_TRY(msg_meta.bls_messages.append(message_cid));
37+
return outcome::success();
38+
},
39+
[&](const Secp256k1Signature &signature) -> outcome::result<void> {
40+
b.secp_messages.emplace_back(message);
41+
OUTCOME_TRY(message_cid, ipld->setCbor(message));
42+
OUTCOME_TRY(msg_meta.secp_messages.append(message_cid));
43+
return outcome::success();
44+
}));
45+
}
46+
b.header.miner = std::move(t.miner);
47+
b.header.ticket = std::move(t.ticket);
48+
b.header.election_proof = std::move(t.election_proof);
49+
b.header.beacon_entries = std::move(t.beacon_entries);
50+
b.header.win_post_proof = std::move(t.win_post_proof);
51+
b.header.parents = std::move(t.parents);
52+
OUTCOME_TRYA(
53+
b.header.parent_weight,
54+
weight::WeightCalculatorImpl{ipld}.calculateWeight(parent_tipset));
55+
b.header.height = t.height;
56+
b.header.parent_state_root = std::move(vm_result.state_root);
57+
b.header.parent_message_receipts = std::move(vm_result.message_receipts);
58+
OUTCOME_TRY(msg_meta.flush());
59+
OUTCOME_TRYA(b.header.messages, ipld->setCbor(msg_meta));
60+
OUTCOME_TRYA(
61+
b.header.bls_aggregate,
62+
crypto::bls::BlsProviderImpl{}.aggregateSignatures(bls_signatures));
63+
b.header.timestamp = t.timestamp;
64+
// TODO: the only caller of "generate" is MinerCreateBlock, it signs block
65+
b.header.block_sig = {};
66+
b.header.fork_signaling = 0;
67+
return std::move(b);
68+
}
69+
} // namespace fc::blockchain::production

core/blockchain/production/block_producer.hpp

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,14 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
#include "common/outcome.hpp"
7-
#include "primitives/address/address.hpp"
86
#include "primitives/block/block.hpp"
9-
#include "primitives/ticket/epost_ticket.hpp"
10-
#include "primitives/ticket/ticket.hpp"
117

128
namespace fc::blockchain::production {
13-
/**
14-
* @class Generating new blocks for chain's tipset
15-
*/
16-
class BlockProducer {
17-
protected:
18-
using Block = primitives::block::Block;
19-
using Address = primitives::address::Address;
20-
using EPostProof = primitives::ticket::EPostProof;
21-
using Ticket = primitives::ticket::Ticket;
9+
using primitives::block::Block;
10+
using primitives::block::BlockTemplate;
11+
using Ipld = storage::ipfs::IpfsDatastore;
2212

23-
public:
24-
virtual ~BlockProducer() = default;
13+
constexpr size_t kBlockMaxMessagesCount = 1000;
2514

26-
/**
27-
* @brief Generate new block
28-
* @param miner_address - source address
29-
* @param parent_tipset_id - id of the parent tipset
30-
* @param proof - evidence of mining permission
31-
* @param ticket - used ticket for election round
32-
* @return Generated full block
33-
*/
34-
virtual outcome::result<Block> generate(Address miner_address,
35-
const CID &parent_tipset_id,
36-
EPostProof proof,
37-
Ticket ticket) = 0;
38-
};
39-
40-
/**
41-
* @enum Block production errors
42-
*/
43-
enum class BlockProducerError {
44-
PARENT_TIPSET_NOT_FOUND = 1,
45-
PARENT_TIPSET_INVALID_CONTENT
46-
};
15+
outcome::result<Block> generate(std::shared_ptr<Ipld> ipld, BlockTemplate t);
4716
} // namespace fc::blockchain::production
48-
49-
OUTCOME_HPP_DECLARE_ERROR(fc::blockchain::production, BlockProducerError);

0 commit comments

Comments
 (0)