Skip to content

Commit 416cbc0

Browse files
authored
Small refactoring (#156)
Signed-off-by: turuslan <[email protected]>
1 parent 220c3c4 commit 416cbc0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+433
-969
lines changed

core/adt/array.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ namespace fc::adt {
5353
return amt.remove(key);
5454
}
5555

56+
outcome::result<void> append(const Value &value) {
57+
OUTCOME_TRY(count, amt.count());
58+
return set(count, value);
59+
}
60+
5661
outcome::result<void> flush() {
5762
OUTCOME_TRY(amt.flush());
5863
return outcome::success();

core/adt/empty_value.hpp

-24
This file was deleted.

core/adt/multimap.hpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ namespace fc::adt {
2121
array = Array<Value>{};
2222
}
2323
array->load(map.hamt.getIpld());
24-
OUTCOME_TRY(count, array->amt.count());
25-
OUTCOME_TRY(array->set(count, value));
24+
OUTCOME_TRY(array->append(value));
2625
OUTCOME_TRY(array->flush());
2726
return map.set(key, *array);
2827
}

core/adt/set.hpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef CPP_FILECOIN_ADT_SET_HPP
7+
#define CPP_FILECOIN_ADT_SET_HPP
8+
9+
#include "adt/map.hpp"
10+
11+
namespace fc::adt {
12+
struct SetValue {};
13+
CBOR_ENCODE(SetValue, v) {
14+
return s << nullptr;
15+
}
16+
CBOR_DECODE(SetValue, v) {
17+
s.next();
18+
return s;
19+
}
20+
21+
template <typename Keyer>
22+
struct Set : Map<SetValue, Keyer> {};
23+
} // namespace fc::adt
24+
25+
#endif // CPP_FILECOIN_ADT_SET_HPP

core/api/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ add_library(api
88
)
99
target_link_libraries(api
1010
address
11+
block_producer
1112
chain_store
1213
cid
1314
interpreter

core/api/make.cpp

+53-57
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "api/make.hpp"
77

8+
#include "blockchain/production/impl/block_producer_impl.hpp"
89
#include "vm/actor/builtin/market/actor.hpp"
910
#include "vm/actor/builtin/miner/types.hpp"
1011
#include "vm/actor/builtin/storage_power/storage_power_actor_state.hpp"
@@ -19,21 +20,24 @@ namespace fc::api {
1920
using vm::actor::builtin::miner::MinerActorState;
2021
using vm::actor::builtin::storage_power::StoragePowerActorState;
2122
using vm::interpreter::InterpreterImpl;
23+
using InterpreterResult = vm::interpreter::Result;
2224
using vm::state::StateTreeImpl;
2325
using MarketActorState = vm::actor::builtin::market::State;
26+
using blockchain::production::BlockProducerImpl;
2427
using crypto::randomness::RandomnessProvider;
2528
using crypto::signature::BlsSignature;
2629
using primitives::block::MsgMeta;
2730
using storage::amt::Amt;
31+
using vm::isVMExitCode;
32+
using vm::normalizeVMExitCode;
2833
using vm::VMExitCode;
2934
using vm::actor::InvokerImpl;
30-
using vm::indices::Indices;
3135
using vm::runtime::Env;
3236

3337
struct TipsetContext {
3438
Tipset tipset;
3539
StateTreeImpl state_tree;
36-
boost::optional<CID> receipts;
40+
boost::optional<InterpreterResult> interpreted;
3741

3842
template <typename T, bool load>
3943
outcome::result<T> actorState(const Address &address) {
@@ -70,10 +74,9 @@ namespace fc::api {
7074
OUTCOME_TRY(tipset, chain_store->loadTipset(tipset_key));
7175
TipsetContext context{tipset, {ipld, tipset.getParentStateRoot()}, {}};
7276
if (interpret) {
73-
// TODO(turuslan): our Indices are not used anywhere
74-
OUTCOME_TRY(result, InterpreterImpl{}.interpret(ipld, tipset, nullptr));
77+
OUTCOME_TRY(result, InterpreterImpl{}.interpret(ipld, tipset));
7578
context.state_tree = {ipld, result.state_root};
76-
context.receipts = result.message_receipts;
79+
context.interpreted = result;
7780
}
7881
return context;
7982
};
@@ -100,59 +103,43 @@ namespace fc::api {
100103
auto height,
101104
auto timestamp) -> outcome::result<BlockMsg> {
102105
OUTCOME_TRY(context, tipsetContext(parent, true));
106+
BlockProducerImpl producer{
107+
ipld,
108+
nullptr,
109+
nullptr,
110+
nullptr,
111+
weight_calculator,
112+
bls_provider,
113+
std::make_shared<InterpreterImpl>(),
114+
};
115+
OUTCOME_TRY(block,
116+
producer.generate(miner,
117+
context.tipset,
118+
*context.interpreted,
119+
proof,
120+
ticket,
121+
messages,
122+
height,
123+
timestamp));
103124

104125
OUTCOME_TRY(miner_state, context.minerState(miner));
105126
OUTCOME_TRY(worker_id,
106127
context.state_tree.lookupId(miner_state.info.worker));
107-
OUTCOME_TRY(state_root, context.state_tree.flush());
108-
109-
BlockMsg block;
110-
block.header.miner = miner;
111-
block.header.parents = parent.cids;
112-
block.header.ticket = ticket;
113-
block.header.height = height;
114-
block.header.timestamp = timestamp;
115-
block.header.epost_proof = proof;
116-
block.header.parent_state_root = state_root;
117-
block.header.parent_message_receipts = *context.receipts;
118-
block.header.fork_signaling = 0;
119-
120-
std::vector<BlsSignature> bls_sigs;
121-
Amt amt_bls{ipld}, amt_secp{ipld};
122-
uint64_t i_bls{0}, i_secp{0};
123-
for (auto &message : messages) {
124-
if (message.signature.isBls()) {
125-
OUTCOME_TRY(message_cid, ipld->setCbor(message.message));
126-
bls_sigs.emplace_back(
127-
boost::get<BlsSignature>(message.signature));
128-
block.bls_messages.emplace_back(std::move(message_cid));
129-
OUTCOME_TRY(amt_bls.setCbor(++i_bls, message_cid));
130-
} else {
131-
OUTCOME_TRY(message_cid, ipld->setCbor(message));
132-
block.secp_messages.emplace_back(std::move(message_cid));
133-
OUTCOME_TRY(amt_secp.setCbor(++i_secp, message_cid));
134-
}
135-
}
136-
137-
OUTCOME_TRY(amt_bls.flush());
138-
OUTCOME_TRY(amt_secp.flush());
139-
OUTCOME_TRY(msg_meta,
140-
ipld->setCbor(MsgMeta{amt_bls.cid(), amt_secp.cid()}));
141-
block.header.messages = msg_meta;
142-
143-
OUTCOME_TRY(bls_aggregate,
144-
bls_provider->aggregateSignatures(bls_sigs));
145-
block.header.bls_aggregate = bls_aggregate;
146-
147-
OUTCOME_TRY(parent_weight,
148-
weight_calculator->calculateWeight(context.tipset));
149-
block.header.parent_weight = parent_weight;
150-
151128
OUTCOME_TRY(block_signable, codec::cbor::encode(block.header));
152129
OUTCOME_TRY(block_sig, key_store->sign(worker_id, block_signable));
153130
block.header.block_sig = block_sig;
154131

155-
return block;
132+
BlockMsg block2;
133+
block2.header = block.header;
134+
for (auto &msg : block.bls_messages) {
135+
OUTCOME_TRY(cid, ipld->setCbor(msg));
136+
block2.bls_messages.emplace_back(std::move(cid));
137+
}
138+
for (auto &msg : block.secp_messages) {
139+
OUTCOME_TRY(cid, ipld->setCbor(msg));
140+
block2.secp_messages.emplace_back(std::move(cid));
141+
}
142+
return block2;
156143
}},
157144
// TODO(turuslan): FIL-165 implement method
158145
.MpoolPending = {},
@@ -163,20 +150,29 @@ namespace fc::api {
163150
.StateCall = {[&](auto &message,
164151
auto &tipset_key) -> outcome::result<InvocResult> {
165152
OUTCOME_TRY(context, tipsetContext(tipset_key));
166-
// TODO(turuslan): our Indices are not used anywhere
167-
std::shared_ptr<Indices> indices;
168153
// TODO(turuslan): FIL-146 randomness from tipset
169154
std::shared_ptr<RandomnessProvider> randomness;
170155
Env env{randomness,
171156
std::make_shared<StateTreeImpl>(context.state_tree),
172-
indices,
173157
std::make_shared<InvokerImpl>(),
174-
static_cast<ChainEpoch>(context.tipset.height),
175-
Address{}};
176-
OUTCOME_TRY(receipt, env.applyMessage(message));
158+
static_cast<ChainEpoch>(context.tipset.height)};
177159
InvocResult result;
178160
result.message = message;
179-
result.receipt = receipt;
161+
auto maybe_result = env.applyImplicitMessage(message);
162+
if (maybe_result) {
163+
result.receipt = {
164+
VMExitCode::Ok, maybe_result.value().return_value, 0};
165+
} else {
166+
if (isVMExitCode(maybe_result.error())) {
167+
auto ret_code =
168+
normalizeVMExitCode(VMExitCode{maybe_result.error().value()});
169+
BOOST_ASSERT_MSG(ret_code,
170+
"c++ actor code returned unknown error");
171+
result.receipt = {*ret_code, {}, 0};
172+
} else {
173+
return maybe_result.error();
174+
}
175+
}
180176
return result;
181177
}},
182178
.StateGetActor = {[&](auto &address,

core/api/rpc/json.hpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -373,16 +373,14 @@ namespace fc::api {
373373

374374
ENCODE(MessageReceipt) {
375375
Value j{rapidjson::kObjectType};
376-
Set(j, "ExitCode", static_cast<uint64_t>(v.exit_code));
376+
Set(j, "ExitCode", common::to_int(v.exit_code));
377377
Set(j, "Return", gsl::make_span(v.return_value));
378378
Set(j, "GasUsed", v.gas_used);
379379
return j;
380380
}
381381

382382
DECODE(MessageReceipt) {
383-
uint64_t exit_code;
384-
decode(exit_code, Get(j, "ExitCode"));
385-
v.exit_code = exit_code;
383+
decodeEnum(v.exit_code, Get(j, "ExitCode"));
386384
decode(v.return_value, Get(j, "Return"));
387385
decode(v.gas_used, Get(j, "GasUsed"));
388386
}

core/blockchain/block_validator/impl/block_validator_impl.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace fc::blockchain::block_validator {
1515
using primitives::address::Protocol;
1616
using storage::amt::Amt;
17-
using MsgMeta = primitives::block::MsgMeta;
1817
using SignedMessage = vm::message::SignedMessage;
1918
using UnsignedMessage = vm::message::UnsignedMessage;
2019
using BlsCryptoSignature = crypto::bls::Signature;
@@ -141,9 +140,7 @@ namespace fc::blockchain::block_validator {
141140
outcome::result<void> BlockValidatorImpl::stateTree(
142141
const BlockHeader &block) const {
143142
OUTCOME_TRY(parent_tipset, getParentTipset(block));
144-
OUTCOME_TRY(
145-
result,
146-
vm_interpreter_->interpret(datastore_, parent_tipset, vm_indices_));
143+
OUTCOME_TRY(result, vm_interpreter_->interpret(datastore_, parent_tipset));
147144
if (result.state_root == block.parent_state_root
148145
&& result.message_receipts == block.parent_message_receipts) {
149146
return outcome::success();
@@ -174,7 +171,8 @@ namespace fc::blockchain::block_validator {
174171
}
175172
}
176173
OUTCOME_TRY(tipset, Tipset::create(parent_blocks));
177-
parent_tipset_cache_ = std::make_pair(std::move(ipld_block.cid), std::move(tipset));
174+
parent_tipset_cache_ =
175+
std::make_pair(std::move(ipld_block.cid), std::move(tipset));
178176
return parent_tipset_cache_.value().second;
179177
}
180178

core/blockchain/block_validator/impl/block_validator_impl.hpp

+2-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "crypto/bls/bls_provider.hpp"
2020
#include "power/power_table.hpp"
2121
#include "storage/ipfs/datastore.hpp"
22-
#include "vm/indices/indices.hpp"
2322
#include "vm/interpreter/interpreter.hpp"
2423

2524
namespace fc::blockchain::block_validator {
@@ -36,7 +35,6 @@ namespace fc::blockchain::block_validator {
3635
using SecpProvider = libp2p::crypto::secp256k1::Secp256k1Provider;
3736
using Interpreter = vm::interpreter::Interpreter;
3837
using Tipset = primitives::tipset::Tipset;
39-
using Indices = vm::indices::Indices;
4038

4139
public:
4240
using StageExecutor = outcome::result<void> (BlockValidatorImpl::*)(
@@ -49,17 +47,15 @@ namespace fc::blockchain::block_validator {
4947
std::shared_ptr<PowerTable> power_table,
5048
std::shared_ptr<BlsProvider> bls_crypto_provider,
5149
std::shared_ptr<SecpProvider> secp_crypto_provider,
52-
std::shared_ptr<Interpreter> vm_interpreter,
53-
std::shared_ptr<Indices> indices)
50+
std::shared_ptr<Interpreter> vm_interpreter)
5451
: datastore_{std::move(ipfs_store)},
5552
clock_{std::move(utc_clock)},
5653
epoch_clock_{std::move(epoch_clock)},
5754
weight_calculator_{std::move(weight_calculator)},
5855
power_table_{std::move(power_table)},
5956
bls_provider_{std::move(bls_crypto_provider)},
6057
secp_provider_{std::move(secp_crypto_provider)},
61-
vm_interpreter_{std::move(vm_interpreter)},
62-
vm_indices_{std::move(indices)} {}
58+
vm_interpreter_{std::move(vm_interpreter)} {}
6359

6460
outcome::result<void> validateBlock(
6561
const BlockHeader &header, scenarios::Scenario scenario) const override;
@@ -75,7 +71,6 @@ namespace fc::blockchain::block_validator {
7571
std::shared_ptr<BlsProvider> bls_provider_;
7672
std::shared_ptr<SecpProvider> secp_provider_;
7773
std::shared_ptr<Interpreter> vm_interpreter_;
78-
std::shared_ptr<Indices> vm_indices_;
7974

8075
/**
8176
* BlockHeader CID -> Parent tipset

core/blockchain/production/block_producer.hpp

+4-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include "primitives/block/block.hpp"
99
#include "primitives/ticket/epost_ticket.hpp"
1010
#include "primitives/ticket/ticket.hpp"
11-
#include "vm/indices/indices.hpp"
1211

1312
namespace fc::blockchain::production {
1413
/**
@@ -20,7 +19,6 @@ namespace fc::blockchain::production {
2019
using Address = primitives::address::Address;
2120
using EPostProof = primitives::ticket::EPostProof;
2221
using Ticket = primitives::ticket::Ticket;
23-
using Indices = vm::indices::Indices;
2422

2523
public:
2624
virtual ~BlockProducer() = default;
@@ -33,12 +31,10 @@ namespace fc::blockchain::production {
3331
* @param ticket - used ticket for election round
3432
* @return Generated full block
3533
*/
36-
virtual outcome::result<Block> generate(
37-
Address miner_address,
38-
const CID &parent_tipset_id,
39-
EPostProof proof,
40-
Ticket ticket,
41-
std::shared_ptr<Indices> indices) = 0;
34+
virtual outcome::result<Block> generate(Address miner_address,
35+
const CID &parent_tipset_id,
36+
EPostProof proof,
37+
Ticket ticket) = 0;
4238
};
4339

4440
/**

0 commit comments

Comments
 (0)