Skip to content

Commit 9799341

Browse files
authored
fvm (#651)
Signed-off-by: turuslan <[email protected]>
1 parent 65a4b1b commit 9799341

File tree

27 files changed

+754
-196
lines changed

27 files changed

+754
-196
lines changed

core/api/full_node/make.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
#include "vm/actor/builtin/v5/miner/monies.hpp"
3838
#include "vm/message/impl/message_signer_impl.hpp"
3939
#include "vm/message/message.hpp"
40-
#include "vm/runtime/env.hpp"
4140
#include "vm/runtime/impl/tipset_randomness.hpp"
41+
#include "vm/runtime/make_vm.hpp"
4242
#include "vm/state/impl/state_tree_impl.hpp"
4343
#include "vm/toolchain/toolchain.hpp"
4444

@@ -71,7 +71,6 @@ namespace fc::api {
7171
using vm::actor::builtin::types::miner::kChainFinality;
7272
using vm::actor::builtin::types::storage_power::kConsensusMinerMinPower;
7373
using vm::interpreter::InterpreterCache;
74-
using vm::runtime::Env;
7574
using vm::state::StateTreeImpl;
7675
using vm::toolchain::Toolchain;
7776
using vm::version::getNetworkVersion;
@@ -588,7 +587,14 @@ namespace fc::api {
588587
if (!message.gas_limit) {
589588
message.gas_limit = kBlockGasLimit;
590589
}
591-
OUTCOME_TRY(env, Env::make(env_context, ts_branch, context.tipset));
590+
const auto buf_ipld{std::make_shared<vm::IpldBuffered>(ipld)};
591+
OUTCOME_TRY(env,
592+
vm::makeVm(buf_ipld,
593+
env_context,
594+
ts_branch,
595+
context.tipset->getParentBaseFee(),
596+
context.tipset->getParentStateRoot(),
597+
context.tipset->epoch()));
592598
InvocResult result;
593599
result.message = message;
594600
OUTCOME_TRYA(result.receipt, env->applyImplicitMessage(message));

core/api/full_node/node_api.hpp

+1-7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ namespace fc::api {
3434
using crypto::randomness::DomainSeparationTag;
3535
using crypto::randomness::Randomness;
3636
using crypto::signature::Signature;
37+
using data_transfer::ChannelId;
3738
using data_transfer::TransferId;
3839
using drand::BeaconEntry;
3940
using libp2p::multi::Multiaddress;
@@ -95,13 +96,6 @@ namespace fc::api {
9596
bool is_car;
9697
};
9798

98-
/** Unique identifier for a channel */
99-
struct ChannelId {
100-
PeerId initiator{codec::cbor::kDefaultT<PeerId>()};
101-
PeerId responder{codec::cbor::kDefaultT<PeerId>()};
102-
TransferId id;
103-
};
104-
10599
struct DatatransferChannel {
106100
TransferId transfer_id;
107101
uint64_t status;

core/api/network/network_api.hpp

+9
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
#pragma once
77

88
#include "api/utils.hpp"
9+
#include "fwd.hpp"
910

1011
namespace fc::api {
1112
namespace jwt = primitives::jwt;
13+
using libp2p::peer::PeerId;
14+
using libp2p::peer::PeerInfo;
1215

1316
class NetworkApi {
1417
public:
@@ -31,6 +34,11 @@ namespace fc::api {
3134
* Removes provided peer from list of connected.
3235
*/
3336
API_METHOD(NetDisconnect, jwt::kWritePermission, void, const PeerInfo &)
37+
38+
API_METHOD(NetProtectAdd,
39+
jwt::kAdminPermission,
40+
void,
41+
const std::vector<PeerId> &);
3442
};
3543

3644
template <typename A, typename F>
@@ -39,6 +47,7 @@ namespace fc::api {
3947
f(a.NetConnect);
4048
f(a.NetPeers);
4149
f(a.NetDisconnect);
50+
f(a.NetProtectAdd);
4251
}
4352

4453
}; // namespace fc::api

core/api/network/setup_net.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ namespace fc::api {
4444
host->disconnect(peer.id);
4545
return outcome::success();
4646
};
47+
48+
api->NetProtectAdd = [](const std::vector<PeerId> &) {
49+
// note: not supported by cpp-libp2p
50+
return outcome::success();
51+
};
4752
}
4853

4954
} // namespace fc::api

core/codec/cbor/cbor_types.cpp

+100
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
#include "codec/cbor/cbor_codec.hpp"
7+
#include "data_transfer/message.hpp"
78
#include "primitives/address/address_codec.hpp"
89
#include "storage/ipfs/graphsync/extension.hpp"
910

@@ -24,3 +25,102 @@ namespace fc::storage::ipfs::graphsync {
2425
return s << m;
2526
}
2627
} // namespace fc::storage::ipfs::graphsync
28+
29+
namespace fc::data_transfer {
30+
using codec::cbor::CborDecodeStream;
31+
using codec::cbor::CborEncodeStream;
32+
33+
CBOR2_ENCODE(DataTransferRequest) {
34+
auto m{CborEncodeStream::map()};
35+
m["BCid"] << v.base_cid;
36+
m["Type"] << v.type;
37+
m["Paus"] << v.is_pause;
38+
m["Part"] << v.is_part;
39+
m["Pull"] << v.is_pull;
40+
m["Stor"] << v.selector;
41+
m["Vouch"] << v.voucher;
42+
m["VTyp"] << v.voucher_type;
43+
m["XferID"] << v.transfer_id;
44+
45+
// note: https://github.com/filecoin-project/go-data-transfer/pull/315
46+
if (v.restart) {
47+
auto l{CborEncodeStream::list()};
48+
s << common::span::bytestr(v.restart->initiator.toVector());
49+
s << common::span::bytestr(v.restart->responder.toVector());
50+
l << v.restart->id;
51+
m["RestartChannel"] << l;
52+
} else {
53+
m["RestartChannel"] << (CborEncodeStream::list() << ""
54+
<< "" << 0);
55+
}
56+
57+
return s << m;
58+
}
59+
CBOR2_DECODE(DataTransferRequest) {
60+
auto m{s.map()};
61+
CborDecodeStream::named(m, "BCid") >> v.base_cid;
62+
CborDecodeStream::named(m, "Type") >> v.type;
63+
CborDecodeStream::named(m, "Paus") >> v.is_pause;
64+
CborDecodeStream::named(m, "Part") >> v.is_part;
65+
CborDecodeStream::named(m, "Pull") >> v.is_pull;
66+
CborDecodeStream::named(m, "Stor") >> v.selector;
67+
CborDecodeStream::named(m, "Vouch") >> v.voucher;
68+
CborDecodeStream::named(m, "VTyp") >> v.voucher_type;
69+
CborDecodeStream::named(m, "XferID") >> v.transfer_id;
70+
71+
// note: https://github.com/filecoin-project/go-data-transfer/pull/315
72+
{
73+
v.restart.reset();
74+
auto l{CborDecodeStream::named(m, "RestartChannel").list()};
75+
std::string initiator;
76+
std::string responder;
77+
TransferId id;
78+
l >> initiator >> responder >> id;
79+
if (!initiator.empty() && !responder.empty()) {
80+
v.restart = ChannelId{
81+
PeerId::fromBytes(common::span::cbytes(initiator)).value(),
82+
PeerId::fromBytes(common::span::cbytes(responder)).value(),
83+
id,
84+
};
85+
}
86+
}
87+
88+
return s;
89+
}
90+
91+
CBOR2_ENCODE(DataTransferResponse) {
92+
auto m{CborEncodeStream::map()};
93+
m["Type"] << v.type;
94+
m["Acpt"] << v.is_accepted;
95+
m["Paus"] << v.is_pause;
96+
m["XferID"] << v.transfer_id;
97+
m["VRes"] << v.voucher;
98+
m["VTyp"] << v.voucher_type;
99+
return s << m;
100+
}
101+
CBOR2_DECODE(DataTransferResponse) {
102+
auto m{s.map()};
103+
CborDecodeStream::named(m, "Type") >> v.type;
104+
CborDecodeStream::named(m, "Acpt") >> v.is_accepted;
105+
CborDecodeStream::named(m, "Paus") >> v.is_pause;
106+
CborDecodeStream::named(m, "XferID") >> v.transfer_id;
107+
CborDecodeStream::named(m, "VRes") >> v.voucher;
108+
CborDecodeStream::named(m, "VTyp") >> v.voucher_type;
109+
return s;
110+
}
111+
112+
CBOR2_ENCODE(DataTransferMessage) {
113+
auto m{CborEncodeStream::map()};
114+
m["IsRq"] << v.is_request;
115+
m["Request"] << v.request;
116+
m["Response"] << v.response;
117+
return s << m;
118+
}
119+
CBOR2_DECODE(DataTransferMessage) {
120+
auto m{s.map()};
121+
CborDecodeStream::named(m, "IsRq") >> v.is_request;
122+
CborDecodeStream::named(m, "Request") >> v.request;
123+
CborDecodeStream::named(m, "Response") >> v.response;
124+
return s;
125+
}
126+
} // namespace fc::data_transfer

core/data_transfer/dt.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ namespace fc::data_transfer {
4242
}
4343

4444
struct DataTransfer {
45-
static inline const std::string kProtocol{"/fil/datatransfer/1.0.0"};
46-
static inline const std::string kExtension{"fil/data-transfer"};
45+
static inline const std::string kProtocol{"/fil/datatransfer/1.2.0"};
46+
static inline const std::string kExtension{"fil/data-transfer/1.1"};
4747

4848
using OkCb = std::function<void(bool)>;
4949
using OnCid = std::function<void(const CID &)>;

core/data_transfer/message.hpp

+12-21
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "data_transfer/types.hpp"
1010

1111
namespace fc::data_transfer {
12+
using libp2p::peer::PeerId;
13+
1214
enum class MessageType {
1315
kNewMessage,
1416
kUpdateMessage,
@@ -18,6 +20,12 @@ namespace fc::data_transfer {
1820
kVoucherResultMessage,
1921
};
2022

23+
struct ChannelId {
24+
PeerId initiator{codec::cbor::kDefaultT<PeerId>()};
25+
PeerId responder{codec::cbor::kDefaultT<PeerId>()};
26+
TransferId id;
27+
};
28+
2129
/**
2230
* DataTransferRequest is a request message for the data transfer protocol
2331
*/
@@ -31,6 +39,7 @@ namespace fc::data_transfer {
3139
boost::optional<CborRaw> voucher;
3240
std::string voucher_type;
3341
TransferId transfer_id{};
42+
boost::optional<ChannelId> restart{};
3443

3544
inline bool operator==(const DataTransferRequest &other) const {
3645
return base_cid == other.base_cid && type == other.type
@@ -80,25 +89,7 @@ namespace fc::data_transfer {
8089
}
8190
};
8291

83-
CBOR_TUPLE(DataTransferRequest,
84-
base_cid,
85-
type,
86-
is_pause,
87-
is_part,
88-
is_pull,
89-
selector,
90-
voucher,
91-
voucher_type,
92-
transfer_id)
93-
94-
CBOR_TUPLE(DataTransferResponse,
95-
type,
96-
is_accepted,
97-
is_pause,
98-
transfer_id,
99-
voucher,
100-
voucher_type)
101-
102-
CBOR_TUPLE(DataTransferMessage, is_request, request, response)
103-
92+
CBOR2_DECODE_ENCODE(DataTransferRequest)
93+
CBOR2_DECODE_ENCODE(DataTransferResponse)
94+
CBOR2_DECODE_ENCODE(DataTransferMessage)
10495
} // namespace fc::data_transfer

core/node/main/main.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ namespace fc {
140140
return result;
141141
};
142142

143-
144143
node_objects.api->ClientGetDealInfo =
145144
[&node_objects](auto &cid) -> outcome::result<StorageMarketDealInfo> {
146145
OUTCOME_TRY(deal, node_objects.storage_market_client->getLocalDeal(cid));
@@ -160,8 +159,8 @@ namespace fc {
160159
}; // TODO(@Elestrias): [FIL-614] Creation time
161160
};
162161

163-
node_objects.api->ClientListRetrievals = [&node_objects]()
164-
-> outcome::result<std::vector<api::RetrievalDeal>> {
162+
node_objects.api->ClientListRetrievals =
163+
[&node_objects]() -> outcome::result<std::vector<api::RetrievalDeal>> {
165164
return node_objects.retrieval_market_client->getRetrievals();
166165
};
167166

core/storage/car/cids_index/cids_index.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,12 @@ namespace fc::storage::cids_index {
205205
row.max_size64 = maxSize64(size);
206206
++total;
207207
} else {
208-
OUTCOME_TRY(cid, CID::read(input));
209-
if (ipld) {
210-
if (!asIdentity(cid)) {
211-
OUTCOME_TRY(ipld->set(cid, input));
208+
if (!startsWith(input, kMainnetGenesisBlockParent)) {
209+
OUTCOME_TRY(cid, CID::read(input));
210+
if (ipld) {
211+
if (!asIdentity(cid)) {
212+
OUTCOME_TRY(ipld->set(cid, input));
213+
}
212214
}
213215
}
214216
}

core/storage/mpool/mpool.cpp

+22-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "node/pubsub_gate.hpp"
2020
#include "vm/actor/builtin/v0/payment_channel/payment_channel_actor.hpp"
2121
#include "vm/interpreter/interpreter.hpp"
22-
#include "vm/runtime/env.hpp"
22+
#include "vm/runtime/make_vm.hpp"
2323
#include "vm/state/impl/state_tree_impl.hpp"
2424
#include "vm/state/resolve_key.hpp"
2525
#include "vm/toolchain/toolchain.hpp"
@@ -31,6 +31,7 @@ namespace fc::storage::mpool {
3131
using vm::actor::builtin::types::miner::kChainFinality;
3232
using vm::interpreter::InterpreterCache;
3333
using vm::message::UnsignedMessage;
34+
using vm::state::StateTreeImpl;
3435

3536
constexpr GasAmount kMinGas{1298450};
3637
constexpr size_t kMaxBlocks{15};
@@ -628,11 +629,15 @@ namespace fc::storage::mpool {
628629
std::shared_lock head_lock(head_mutex_);
629630
const auto height = head_->height();
630631
OUTCOME_TRY(interpeted, env_context.interpreter_cache->get(head_->key));
631-
OUTCOME_TRY(env, vm::runtime::Env::make(env_context, ts_main, head_));
632+
const auto buf_ipld{std::make_shared<vm::IpldBuffered>(ipld)};
633+
OUTCOME_TRY(env,
634+
vm::makeVm(buf_ipld,
635+
env_context,
636+
ts_main,
637+
head_->getParentBaseFee(),
638+
interpeted.state_root,
639+
head_->epoch() + 1));
632640
head_lock.unlock();
633-
env->state_tree = std::make_shared<vm::state::StateTreeImpl>(
634-
env->ipld, interpeted.state_root);
635-
++env->epoch;
636641
std::shared_lock pending_lock{pending_mutex_};
637642
auto pending_it{pending_.find(msg.from)};
638643
if (pending_it != pending_.end()) {
@@ -641,7 +646,18 @@ namespace fc::storage::mpool {
641646
}
642647
}
643648
pending_lock.unlock();
644-
OUTCOME_TRY(actor, env->state_tree->get(msg.from));
649+
OUTCOME_TRY(state, env->flush());
650+
OUTCOME_TRY(actor,
651+
vm::state::StateTreeImpl{
652+
withVersion(buf_ipld, head_->height()), state}
653+
.get(msg.from));
654+
OUTCOME_TRYA(env,
655+
vm::makeVm(buf_ipld,
656+
env_context,
657+
ts_main,
658+
head_->getParentBaseFee(),
659+
state,
660+
head_->epoch() + 1));
645661
msg.nonce = actor.nonce;
646662
OUTCOME_TRY(
647663
apply,

core/vm/actor/cgo/actors.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,8 @@ namespace fc::vm::actor::cgo {
290290
auto _fault{rt->verifyConsensusFault(block1, block2, extra)};
291291
// TODO(turuslan): correct error handling
292292
if (!charge(ret, _fault)) {
293-
auto &fault{_fault.value()};
294-
if (fault) {
293+
if (_fault && _fault.value()) {
294+
const auto &fault{_fault.value()};
295295
ret << kOk << true << fault->target << fault->epoch << fault->type;
296296
} else {
297297
ret << kOk << false;

core/vm/actor/cgo/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ require (
1313
github.com/filecoin-project/specs-actors/v6 v6.0.1
1414
github.com/filecoin-project/specs-actors/v7 v7.0.0
1515
github.com/ipfs/go-cid v0.1.0
16-
github.com/whyrusleeping/cbor-gen v0.0.0-20210713220151-be142a5ae1a8
16+
github.com/whyrusleeping/cbor-gen v0.0.0-20220302191723-37c43cae8e14
1717
)

0 commit comments

Comments
 (0)