Skip to content

Commit 5cb3422

Browse files
committed
Change unionStorage.
Signed-off-by: Markuu-s <[email protected]>
1 parent e7ad727 commit 5cb3422

File tree

3 files changed

+39
-102
lines changed

3 files changed

+39
-102
lines changed

core/miner/storage_fsm/commit_batcher.hpp

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ namespace fc::mining {
2121
RegisteredSealProof spt;
2222
};
2323

24-
2524
class CommitBatcher {
2625
public:
2726
virtual ~CommitBatcher() = default;

core/miner/storage_fsm/impl/commit_batcher_impl.cpp

+28-63
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@
99
#include "vm/actor/builtin/v5/miner/miner_actor.hpp"
1010

1111
namespace fc::mining {
12-
using vm::actor::builtin::types::miner::kChainFinality;
13-
using PairStorage = CommitBatcherImpl::UnionStorage::PairStorage;
1412
using fc::primitives::ActorId;
1513
using primitives::BigInt;
1614
using primitives::sector::AggregateSealVerifyInfo;
15+
using vm::actor::builtin::types::miner::kChainFinality;
1716
using vm::actor::builtin::types::miner::SectorPreCommitOnChainInfo;
1817
using vm::actor::builtin::v5::miner::ProveCommitAggregate;
19-
namespace vm::actor::builtin::types::miner::SectorPreCommitOnChainInfo;
2018

2119
CommitBatcherImpl::CommitBatcherImpl(
2220
const std::chrono::milliseconds &max_time,
@@ -32,10 +30,12 @@ namespace fc::mining {
3230
const SectorInfo &sector_info,
3331
const AggregateInput &aggregate_input,
3432
const CommitCallback &callback) {
33+
std::unique_lock<std::mutex> locker(mutex_storage_);
34+
3535
const SectorNumber &sector_number = sector_info.sector_number;
3636
OUTCOME_TRY(head, api_->ChainHead());
3737

38-
union_storage_.push(sector_number, PairStorage(aggregate_input, callback));
38+
union_storage_[sector_number] = PairStorage{aggregate_input, callback};
3939

4040
if (union_storage_.size() >= max_size_callback_) {
4141
sendCallbacks();
@@ -49,7 +49,12 @@ namespace fc::mining {
4949
void CommitBatcherImpl::forceSend() {}
5050

5151
void CommitBatcherImpl::sendCallbacks() {
52-
UnionStorage union_storage_for_send_(std::move(union_storage_));
52+
std::unique_lock<std::mutex> locker(mutex_storage_);
53+
MapPairStorage union_storage_for_send_(
54+
std::make_move_iterator(union_storage_.begin()),
55+
std::make_move_iterator(union_storage_.end()));
56+
locker.unlock();
57+
5358
const auto maybe_result = sendBatch(union_storage_for_send_);
5459
for (const auto &[key, pair_storage] : union_storage_for_send_) {
5560
pair_storage.commit_callback(maybe_result);
@@ -62,8 +67,8 @@ namespace fc::mining {
6267
}
6368

6469
outcome::result<CID> CommitBatcherImpl::sendBatch(
65-
UnionStorage &union_storage_for_send) {
66-
if (not union_storage_for_send.size()) {
70+
MapPairStorage &union_storage_for_send) {
71+
if (union_storage_for_send.empty()) {
6772
cutoff_start_ = std::chrono::system_clock::now();
6873
return ERROR_TEXT("Empty Batcher");
6974
}
@@ -80,26 +85,24 @@ namespace fc::mining {
8085
BigInt collateral = 0;
8186

8287
for (const auto &[sector_number, pair_storage] : union_storage_for_send) {
83-
84-
85-
TokenAmount sc = getSectorCollateral(head, sector_number, *head.get());
88+
OUTCOME_TRY(sc, getSectorCollateral(sector_number, head->key));
8689
collateral = collateral + sc;
8790

8891
params.sectors.insert(sector_number);
8992
}
9093

91-
92-
9394
for (const auto &[sector_number, pair_storage] : union_storage_for_send) {
94-
proofs.push_back(
95-
pair_storage.aggregate_input.proof);
95+
proofs.push_back(pair_storage.aggregate_input.proof);
9696
}
9797

9898
const ActorId mid = miner_address_.getId();
9999
// TODO maybe long (AggregateSealProofs)
100-
params.proof = proof_->AggregateSealProofs(); // OUTCOME_TRY
101100

102-
// TODO CBOR::ENCODE params
101+
// TODO params.proof = proof_->AggregateSealProofs(); // OUTCOME_TRY
102+
OUTCOME_TRY(a, proof_->AggregateSealProofs());
103+
104+
auto enc = codec::cbor::encode(params);
105+
OUTCOME_TRY(mi, api_->StateMinerInfo(miner_address_, head->key));
103106

104107
// BigDiv usage вместо /(обычное деление)
105108

@@ -115,16 +118,16 @@ namespace fc::mining {
115118
*/
116119
// OTCOME_TRY(mi, api_->StateMinerInfo());
117120

118-
OUTCOME_TRY(bf, api_->ChainBaseFee(head));
119-
OUTCOME_TRY(nv, api_->StateNetworkVersion(/*NetworkVersion*/, head));
121+
// TODO OUTCOME_TRY(bf, api_->ChainBaseFee(head));
122+
OUTCOME_TRY(nv, api_->StateNetworkVersion(head->key));
120123
}
121124

122125
void CommitBatcherImpl::setCommitCutoff(const ChainEpoch &current_epoch,
123126
const SectorInfo &sector_info) {
124127
ChainEpoch cutoff_epoch =
125128
sector_info.ticket_epoch
126129
+ static_cast<int64_t>(kEpochsInDay + kChainFinality);
127-
ChainEpoch start_epoch{};
130+
ChainEpoch start_epoch;
128131
for (const auto &piece : sector_info.pieces) {
129132
if (!piece.deal_info) {
130133
continue;
@@ -150,58 +153,20 @@ namespace fc::mining {
150153
}
151154
}
152155

153-
TokenAmount CommitBatcherImpl::getSectorCollateral(
154-
std::shared_ptr<const Tipset> &head,
155-
const SectorNumber &sector_number,
156-
const TipsetKey &tip_set_key) {
156+
outcome::result<TokenAmount> CommitBatcherImpl::getSectorCollateral(
157+
const SectorNumber &sector_number, const TipsetKey &tip_set_key) {
157158
OUTCOME_TRY(pci,
158159
api_->StateSectorPreCommitInfo(
159160
miner_address_, sector_number, tip_set_key));
161+
160162
OUTCOME_TRY(collateral,
161163
api_->StateMinerInitialPledgeCollateral(
162-
miner_address_, head->key, pci.info, tip_set_key));
164+
miner_address_, pci.info, tip_set_key));
163165

164-
collateral = collateral + pci.PreCommitDeposit;
165-
collateral = max(0, collateral);
166+
collateral = collateral + pci.precommit_deposit;
167+
collateral = std::max(BigInt(0), collateral);
166168

167169
return collateral;
168170
}
169171

170-
void CommitBatcherImpl::UnionStorage::push(const SectorNumber &sector_number,
171-
const PairStorage &pair_storage) {
172-
std::unique_lock<std::mutex> locker(mutex_);
173-
storage_[sector_number] = pair_storage;
174-
}
175-
176-
CommitBatcherImpl::UnionStorage::UnionStorage(
177-
CommitBatcherImpl::UnionStorage &&union_storage1) {
178-
std::unique_lock<std::mutex> locker(union_storage1.mutex_);
179-
storage_.insert(std::make_move_iterator(union_storage1.storage_.begin()),
180-
std::make_move_iterator(union_storage1.storage_.end()));
181-
}
182-
183-
size_t CommitBatcherImpl::UnionStorage::size() {
184-
std::unique_lock<std::mutex> locker(mutex_);
185-
return storage_.size();
186-
}
187-
188-
CommitBatcherImpl::UnionStorage::PairStorage::PairStorage(
189-
const AggregateInput &aggregate_input,
190-
const CommitCallback &commit_callback)
191-
: aggregate_input(aggregate_input), commit_callback(commit_callback) {}
192-
193-
std::map<SectorNumber, CommitBatcherImpl::UnionStorage::PairStorage>::iterator
194-
CommitBatcherImpl::UnionStorage::begin() {
195-
return storage_.begin();
196-
}
197-
198-
std::map<SectorNumber, CommitBatcherImpl::UnionStorage::PairStorage>::iterator
199-
CommitBatcherImpl::UnionStorage::end() {
200-
return storage_.end();
201-
}
202-
203-
PairStorage CommitBatcherImpl::UnionStorage::get(const int index) {
204-
std::unique_lock<std::mutex> locker(mutex_);
205-
return storage_[index];
206-
}
207172
} // namespace fc::mining

core/miner/storage_fsm/impl/commit_batcher_impl.hpp

+11-38
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,16 @@ namespace fc::mining {
1414
using primitives::address::Address;
1515
using primitives::tipset::Tipset;
1616
using primitives::tipset::TipsetKey;
17-
using types::FeeConfig;
1817
using proofs::ProofEngine;
18+
using types::FeeConfig;
1919

2020
class CommitBatcherImpl : public CommitBatcher {
2121
public:
22-
class UnionStorage {
23-
public:
24-
UnionStorage() = default;
25-
UnionStorage(UnionStorage &&);
26-
27-
UnionStorage(const UnionStorage &) = delete;
28-
29-
~UnionStorage() = default;
30-
31-
struct PairStorage {
32-
AggregateInput aggregate_input;
33-
CommitCallback commit_callback;
34-
35-
PairStorage(const AggregateInput &aggregate_input,
36-
const CommitCallback &commit_callback);
37-
};
38-
39-
void push(const SectorNumber &sector_number,
40-
const PairStorage &pair_storage);
41-
42-
size_t size();
43-
44-
PairStorage get(const int index);
45-
46-
std::map<SectorNumber, PairStorage>::iterator begin();
47-
std::map<SectorNumber, PairStorage>::iterator end();
48-
49-
private:
50-
std::mutex mutex_;
51-
52-
std::map<SectorNumber, PairStorage> storage_;
53-
}; // TODO вынести PairStorage и удалить UnionStorage. storage_;
22+
struct PairStorage {
23+
AggregateInput aggregate_input;
24+
CommitCallback commit_callback;
25+
};
26+
typedef std::map<SectorNumber, PairStorage> MapPairStorage;
5427

5528
CommitBatcherImpl(const std::chrono::milliseconds &max_time,
5629
const size_t &max_size_callback,
@@ -72,19 +45,19 @@ namespace fc::mining {
7245
std::chrono::milliseconds closest_cutoff_;
7346
std::chrono::system_clock::time_point cutoff_start_;
7447
size_t max_size_callback_;
75-
UnionStorage union_storage_;
48+
MapPairStorage union_storage_;
7649
std::shared_ptr<FullNodeApi> api_;
7750
Address miner_address_;
7851
std::shared_ptr<FeeConfig> fee_config_;
7952
std::shared_ptr<ProofEngine> proof_;
53+
std::mutex mutex_storage_;
8054

8155
void sendCallbacks();
8256

83-
outcome::result<CID> sendBatch(UnionStorage &union_storage_for_send);
57+
outcome::result<CID> sendBatch(MapPairStorage &union_storage_for_send);
8458

85-
TokenAmount getSectorCollateral(std::shared_ptr<const Tipset> &head,
86-
const SectorNumber &sector_number,
87-
const TipsetKey &tip_set_key);
59+
outcome::result<TokenAmount> getSectorCollateral(
60+
const SectorNumber &sector_number, const TipsetKey &tip_set_key);
8861
};
8962

9063
} // namespace fc::mining

0 commit comments

Comments
 (0)