Skip to content

Commit 6bb7b1b

Browse files
authored
update checks (#573)
Signed-off-by: turuslan <[email protected]>
1 parent f3bbd70 commit 6bb7b1b

File tree

13 files changed

+307
-89
lines changed

13 files changed

+307
-89
lines changed

core/markets/storage/chain_events/impl/chain_events_impl.cpp

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "common/outcome_fmt.hpp"
88
#include "storage/ipfs/api_ipfs_datastore/api_ipfs_datastore.hpp"
99
#include "vm/actor/builtin/states/miner/miner_actor_state.hpp"
10-
#include "vm/actor/builtin/v5/miner/miner_actor.hpp"
10+
#include "vm/actor/builtin/v7/miner/miner_actor.hpp"
1111

1212
namespace fc::markets::storage::chain_events {
1313
using primitives::RleBitset;
@@ -18,6 +18,8 @@ namespace fc::markets::storage::chain_events {
1818
using vm::actor::builtin::v5::miner::PreCommitSector;
1919
using vm::actor::builtin::v5::miner::ProveCommitAggregate;
2020
using vm::actor::builtin::v5::miner::ProveCommitSector;
21+
using vm::actor::builtin::v7::miner::ProveReplicaUpdates;
22+
using vm::actor::builtin::v7::miner::ReplicaUpdate;
2123
using vm::message::SignedMessage;
2224

2325
ChainEventsImpl::ChainEventsImpl(std::shared_ptr<FullNodeApi> api,
@@ -83,12 +85,7 @@ namespace fc::markets::storage::chain_events {
8385
if (sector) {
8486
watched_events_[provider].commits.emplace(*sector, std::move(cb));
8587
} else {
86-
watched_events_[provider].precommits.emplace(
87-
deal_id, [this, provider, cb{std::move(cb)}](auto _sector) mutable {
88-
OUTCOME_CB(auto sector, _sector);
89-
std::unique_lock lock{watched_events_mutex_};
90-
watched_events_[provider].commits.emplace(sector, std::move(cb));
91-
});
88+
watched_events_[provider].precommits.emplace(deal_id, std::move(cb));
9289
}
9390
return outcome::success();
9491
}()};
@@ -151,18 +148,31 @@ namespace fc::markets::storage::chain_events {
151148
return outcome::success();
152149
}
153150
auto &watch{_watch->second};
154-
const auto on_precommit{[&](const SectorPreCommitInfo &precommit) {
155-
for (const auto &deal_id : precommit.deal_ids) {
151+
const auto on_deals{[&](const std::vector<DealId> &deals,
152+
SectorNumber sector,
153+
bool update) {
154+
for (const auto &deal_id : deals) {
156155
const auto [begin, end]{watch.precommits.equal_range(deal_id)};
157156
auto it{begin};
158157
while (it != end) {
159158
api_->StateWaitMsg(
160-
[cb{std::move(it->second)}, sector{precommit.sector}](auto _r) {
159+
[=, cb{std::move(it->second)}, provider{_watch->first}](auto _r) {
161160
OUTCOME_CB(auto r, _r);
162161
if (r.receipt.exit_code != VMExitCode::kOk) {
163162
return cb(r.receipt.exit_code);
164163
}
165-
return cb(sector);
164+
if (update) {
165+
OUTCOME_CB(auto result,
166+
codec::cbor::decode<ProveReplicaUpdates::Result>(
167+
r.receipt.return_value));
168+
if (!result.has(sector)) {
169+
return cb(ERROR_TEXT("ProveReplicaUpdates failed"));
170+
}
171+
return cb(outcome::success());
172+
}
173+
std::unique_lock lock{watched_events_mutex_};
174+
watched_events_[provider].commits.emplace(sector,
175+
std::move(cb));
166176
},
167177
cid,
168178
kMessageConfidence,
@@ -172,6 +182,9 @@ namespace fc::markets::storage::chain_events {
172182
}
173183
}
174184
}};
185+
const auto on_precommit{[&](const SectorPreCommitInfo &precommit) {
186+
on_deals(precommit.deal_ids, precommit.sector, false);
187+
}};
175188
const auto on_commit{[&](SectorNumber sector) {
176189
const auto [begin, end]{watch.commits.equal_range(sector)};
177190
auto it{begin};
@@ -191,6 +204,9 @@ namespace fc::markets::storage::chain_events {
191204
it = watch.commits.erase(it);
192205
}
193206
}};
207+
const auto on_update{[&](const ReplicaUpdate &update) {
208+
on_deals(update.deals, update.sector, true);
209+
}};
194210
if (message.method == PreCommitSector::Number) {
195211
OUTCOME_TRY(param,
196212
codec::cbor::decode<PreCommitSector::Params>(message.params));
@@ -213,6 +229,13 @@ namespace fc::markets::storage::chain_events {
213229
for (const auto &sector : param.sectors) {
214230
on_commit(sector);
215231
}
232+
} else if (message.method == ProveReplicaUpdates::Number) {
233+
OUTCOME_TRY(
234+
param,
235+
codec::cbor::decode<ProveReplicaUpdates::Params>(message.params));
236+
for (const auto &update : param.updates) {
237+
on_update(update);
238+
}
216239
}
217240
return outcome::success();
218241
}

core/markets/storage/chain_events/impl/chain_events_impl.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@ namespace fc::markets::storage::chain_events {
2424
class ChainEventsImpl : public ChainEvents,
2525
public std::enable_shared_from_this<ChainEventsImpl> {
2626
public:
27-
using PrecommitCb = std::function<void(outcome::result<SectorNumber>)>;
28-
2927
struct Watch {
3028
// TODO(turuslan): FIL-420 check cache memory usage
31-
std::multimap<DealId, PrecommitCb> precommits;
29+
std::multimap<DealId, CommitCb> precommits;
3230
// TODO(turuslan): FIL-420 check cache memory usage
3331
std::multimap<SectorNumber, CommitCb> commits;
3432
};

core/miner/storage_fsm/impl/checks.cpp

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ namespace fc::mining::checks {
6161
}
6262
}
6363

64-
outcome::result<void> checkPieces(
64+
outcome::result<size_t> checkPieces(
6565
const Address &miner_address,
6666
const std::shared_ptr<SectorInfo> &sector_info,
6767
const std::shared_ptr<FullNodeApi> &api) {
68+
size_t deal_count{0};
6869
OUTCOME_TRY(chain_head, api->ChainHead());
6970

7071
for (const auto &piece : sector_info->pieces) {
@@ -96,9 +97,10 @@ namespace fc::mining::checks {
9697
if (chain_head->epoch() >= proposal.proposal.start_epoch) {
9798
return ChecksError::kExpiredDeal;
9899
}
100+
++deal_count;
99101
}
100102

101-
return outcome::success();
103+
return deal_count;
102104
}
103105

104106
outcome::result<CID> getDataCommitment(
@@ -276,6 +278,48 @@ namespace fc::mining::checks {
276278
return outcome::success();
277279
}
278280

281+
outcome::result<void> checkUpdate(
282+
const Address &miner_address,
283+
const std::shared_ptr<SectorInfo> &sector_info,
284+
const TipsetKey &tipset_key,
285+
const std::shared_ptr<FullNodeApi> &api,
286+
const std::shared_ptr<proofs::ProofEngine> &proofs) {
287+
if (!sector_info->comm_r) {
288+
return ERROR_TEXT("checkUpdate: no comm_r");
289+
}
290+
if (!sector_info->update) {
291+
return ERROR_TEXT("checkUpdate: not marked for update");
292+
}
293+
OUTCOME_TRY(deal_count, checkPieces(miner_address, sector_info, api));
294+
if (deal_count == 0) {
295+
return ERROR_TEXT("checkUpdate: no deals");
296+
}
297+
OUTCOME_TRY(comm_d,
298+
getDataCommitment(miner_address, sector_info, tipset_key, api));
299+
if (sector_info->update_comm_d != comm_d) {
300+
return ERROR_TEXT("checkUpdate: wrong update_comm_d");
301+
}
302+
if (!sector_info->update_comm_r) {
303+
return ERROR_TEXT("checkUpdate: no update_comm_r");
304+
}
305+
if (!sector_info->update_proof) {
306+
return ERROR_TEXT("checkUpdate: no update_proof");
307+
}
308+
OUTCOME_TRY(update_type,
309+
getRegisteredUpdateProof(sector_info->sector_type));
310+
OUTCOME_TRY(verified,
311+
proofs->verifyUpdateProof({
312+
update_type,
313+
*sector_info->comm_r,
314+
*sector_info->update_comm_r,
315+
*sector_info->update_comm_d,
316+
*sector_info->update_proof,
317+
}));
318+
if (!verified) {
319+
return ERROR_TEXT("checkUpdate: wrong proof");
320+
}
321+
return outcome::success();
322+
}
279323
} // namespace fc::mining::checks
280324

281325
OUTCOME_CPP_DEFINE_CATEGORY(fc::mining::checks, ChecksError, e) {

core/miner/storage_fsm/impl/checks.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace fc::mining::checks {
2222
outcome::result<EpochDuration> getMaxProveCommitDuration(
2323
NetworkVersion network, const std::shared_ptr<SectorInfo> &sector_info);
2424

25-
outcome::result<void> checkPieces(
25+
outcome::result<size_t> checkPieces(
2626
const Address &miner_address,
2727
const std::shared_ptr<SectorInfo> &sector_info,
2828
const std::shared_ptr<FullNodeApi> &api);
@@ -52,6 +52,13 @@ namespace fc::mining::checks {
5252
const std::shared_ptr<FullNodeApi> &api,
5353
const std::shared_ptr<proofs::ProofEngine> &proofs);
5454

55+
outcome::result<void> checkUpdate(
56+
const Address &miner_address,
57+
const std::shared_ptr<SectorInfo> &sector_info,
58+
const TipsetKey &tipset_key,
59+
const std::shared_ptr<FullNodeApi> &api,
60+
const std::shared_ptr<proofs::ProofEngine> &proofs);
61+
5562
enum class ChecksError {
5663
kInvalidDeal = 1,
5764
kExpiredDeal,

core/miner/storage_fsm/types.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ namespace fc::mining::types {
105105

106106
boost::optional<CID> fault_report_message;
107107

108+
bool update{false};
109+
std::vector<Piece> update_pieces;
110+
boost::optional<CID> update_comm_d;
111+
boost::optional<CID> update_comm_r;
112+
boost::optional<Bytes> update_proof;
113+
boost::optional<CID> update_message;
114+
108115
SealingState return_state{};
109116

110117
inline std::vector<UnpaddedPieceSize> getExistingPieceSizes() const {

core/primitives/sector/sector.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,31 @@ namespace fc::primitives::sector {
8383
}
8484
}
8585

86+
outcome::result<RegisteredUpdateProof> getRegisteredUpdateProof(
87+
RegisteredSealProof proof) {
88+
switch (proof) {
89+
case RegisteredSealProof::kStackedDrg64GiBV1:
90+
case RegisteredSealProof::kStackedDrg64GiBV1_1:
91+
return RegisteredUpdateProof::kStackedDrg64GiBV1;
92+
case RegisteredSealProof::kStackedDrg32GiBV1:
93+
case RegisteredSealProof::kStackedDrg32GiBV1_1:
94+
return RegisteredUpdateProof::kStackedDrg32GiBV1;
95+
case RegisteredSealProof::kStackedDrg512MiBV1:
96+
case RegisteredSealProof::kStackedDrg512MiBV1_1:
97+
return RegisteredUpdateProof::kStackedDrg512MiBV1;
98+
case RegisteredSealProof::kStackedDrg8MiBV1:
99+
case RegisteredSealProof::kStackedDrg8MiBV1_1:
100+
return RegisteredUpdateProof::kStackedDrg8MiBV1;
101+
case RegisteredSealProof::kStackedDrg2KiBV1:
102+
case RegisteredSealProof::kStackedDrg2KiBV1_1:
103+
return RegisteredUpdateProof::kStackedDrg2KiBV1;
104+
case RegisteredSealProof::kUndefined:
105+
return RegisteredUpdateProof::kUndefined;
106+
default:
107+
return Errors::kInvalidSealProof;
108+
}
109+
}
110+
86111
outcome::result<SectorSize> getSectorSize(RegisteredSealProof proof) {
87112
switch (proof) {
88113
case RegisteredSealProof::kStackedDrg64GiBV1:

core/primitives/sector/sector.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ namespace fc::primitives::sector {
9090
RegisteredSealProof proof);
9191
outcome::result<RegisteredPoStProof> getRegisteredWinningPoStProof(
9292
RegisteredPoStProof proof);
93+
outcome::result<RegisteredUpdateProof> getRegisteredUpdateProof(
94+
RegisteredSealProof proof);
9395

9496
outcome::result<SectorSize> getSectorSize(RegisteredSealProof proof);
9597
outcome::result<SectorSize> getSectorSize(RegisteredPoStProof proof);

core/sector_storage/impl/manager_impl.cpp

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "api/storage_miner/return_api.hpp"
1818
#include "codec/json/json.hpp"
19+
#include "common/outcome_fmt.hpp"
1920
#include "sector_storage/impl/allocate_selector.hpp"
2021
#include "sector_storage/impl/existing_selector.hpp"
2122
#include "sector_storage/impl/local_worker.hpp"
@@ -222,36 +223,22 @@ namespace fc::sector_storage {
222223
OUTCOME_TRY(index_->storageLock(
223224
sector.id,
224225
SectorFileType::FTNone,
225-
static_cast<SectorFileType>(SectorFileType::FTSealed
226-
| SectorFileType::FTUnsealed
227-
| SectorFileType::FTCache)));
226+
SectorFileType::FTCache | SectorFileType::FTSealed
227+
| SectorFileType::FTUnsealed | SectorFileType::FTUpdate
228+
| SectorFileType::FTUpdateCache));
228229

229230
bool isError = false;
230231

231-
auto cache_err = remote_store_->remove(sector.id, SectorFileType::FTCache);
232-
if (cache_err.has_error()) {
233-
isError = true;
234-
logger_->error("removing cached sector {} : {}",
235-
primitives::sector_file::sectorName(sector.id),
236-
cache_err.error().message());
237-
}
238-
239-
auto sealed_err =
240-
remote_store_->remove(sector.id, SectorFileType::FTSealed);
241-
if (sealed_err.has_error()) {
242-
isError = true;
243-
logger_->error("removing sealed sector {} : {}",
244-
primitives::sector_file::sectorName(sector.id),
245-
sealed_err.error().message());
246-
}
247-
248-
auto unsealed_err =
249-
remote_store_->remove(sector.id, SectorFileType::FTUnsealed);
250-
if (unsealed_err.has_error()) {
232+
for (const auto &type : primitives::sector_file::kSectorFileTypes) {
233+
const auto r{remote_store_->remove(sector.id, type)};
234+
if (r) {
235+
continue;
236+
}
251237
isError = true;
252-
logger_->error("removing unsealed sector {} : {}",
238+
logger_->error("removing sector {}/{}: {:#}",
239+
toString(type),
253240
primitives::sector_file::sectorName(sector.id),
254-
unsealed_err.error().message());
241+
r.error());
255242
}
256243

257244
if (isError) {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#pragma once
7+
8+
#include "vm/actor/builtin/v5/miner/miner_actor.hpp"
9+
10+
namespace fc::vm::actor::builtin::v7::miner {
11+
using primitives::DealId;
12+
using primitives::RleBitset;
13+
using primitives::SectorNumber;
14+
using primitives::sector::RegisteredUpdateProof;
15+
16+
// TODO(m.tagirov): implement
17+
using Construct = v5::miner::Construct;
18+
using ControlAddresses = v5::miner::ControlAddresses;
19+
using ChangeWorkerAddress = v5::miner::ChangeWorkerAddress;
20+
using ChangePeerId = v5::miner::ChangePeerId;
21+
using SubmitWindowedPoSt = v5::miner::SubmitWindowedPoSt;
22+
using PreCommitSector = v5::miner::PreCommitSector;
23+
using ProveCommitSector = v5::miner::ProveCommitSector;
24+
using ExtendSectorExpiration = v5::miner::ExtendSectorExpiration;
25+
using TerminateSectors = v5::miner::TerminateSectors;
26+
using DeclareFaults = v5::miner::DeclareFaults;
27+
using DeclareFaultsRecovered = v5::miner::DeclareFaultsRecovered;
28+
using OnDeferredCronEvent = v5::miner::OnDeferredCronEvent;
29+
using CheckSectorProven = v5::miner::CheckSectorProven;
30+
using ApplyRewards = v5::miner::ApplyRewards;
31+
using ReportConsensusFault = v5::miner::ReportConsensusFault;
32+
using WithdrawBalance = v5::miner::WithdrawBalance;
33+
using ConfirmSectorProofsValid = v5::miner::ConfirmSectorProofsValid;
34+
using ChangeMultiaddresses = v5::miner::ChangeMultiaddresses;
35+
using CompactPartitions = v5::miner::CompactPartitions;
36+
using CompactSectorNumbers = v5::miner::CompactSectorNumbers;
37+
using ConfirmUpdateWorkerKey = v5::miner::ConfirmUpdateWorkerKey;
38+
using RepayDebt = v5::miner::RepayDebt;
39+
using ChangeOwnerAddress = v5::miner::ChangeOwnerAddress;
40+
using DisputeWindowedPoSt = v5::miner::DisputeWindowedPoSt;
41+
42+
struct ReplicaUpdate {
43+
SectorNumber sector{};
44+
uint64_t deadline{};
45+
uint64_t partition{};
46+
CID comm_r;
47+
std::vector<DealId> deals;
48+
RegisteredUpdateProof update_type{};
49+
Bytes proof;
50+
};
51+
CBOR_TUPLE(ReplicaUpdate,
52+
sector,
53+
deadline,
54+
partition,
55+
comm_r,
56+
deals,
57+
update_type,
58+
proof)
59+
60+
struct ProveReplicaUpdates : ActorMethodBase<27> {
61+
struct Params {
62+
std::vector<ReplicaUpdate> updates;
63+
};
64+
using Result = RleBitset;
65+
ACTOR_METHOD_DECL();
66+
};
67+
CBOR_TUPLE(ProveReplicaUpdates::Params, updates)
68+
} // namespace fc::vm::actor::builtin::v7::miner

0 commit comments

Comments
 (0)