Skip to content

Commit e7ad727

Browse files
committed
Added sendBatch.
Signed-off-by: Markuu-s <[email protected]>
1 parent cf975e7 commit e7ad727

File tree

5 files changed

+125
-52
lines changed

5 files changed

+125
-52
lines changed

core/api/full_node/node_api.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,13 @@ namespace fc::api {
463463
const Address &,
464464
const TokenAmount &)
465465

466+
API_METHOD(ChainBaseFee,
467+
jwt::kWritePermission,
468+
TokenAmount,
469+
Address,
470+
SectorNumber,
471+
TipsetKey)
472+
466473
API_METHOD(MinerCreateBlock,
467474
jwt::kWritePermission,
468475
BlockWithCids,
@@ -787,6 +794,7 @@ namespace fc::api {
787794
visitNet(a, f);
788795
visitWallet(a, f);
789796
f(a.BeaconGetEntry);
797+
f(a.ChainBaseFee);
790798
f(a.ChainGetBlock);
791799
f(a.ChainGetBlockMessages);
792800
f(a.ChainGetGenesis);

core/miner/storage_fsm/commit_batcher.hpp

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

24+
2425
class CommitBatcher {
2526
public:
2627
virtual ~CommitBatcher() = default;

core/miner/storage_fsm/impl/commit_batcher_impl.cpp

Lines changed: 97 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,17 @@
66
#pragma once
77
#include "commit_batcher_impl.hpp"
88
#include <iterator>
9+
#include "vm/actor/builtin/v5/miner/miner_actor.hpp"
910

1011
namespace fc::mining {
1112
using vm::actor::builtin::types::miner::kChainFinality;
13+
using PairStorage = CommitBatcherImpl::UnionStorage::PairStorage;
14+
using fc::primitives::ActorId;
15+
using primitives::BigInt;
16+
using primitives::sector::AggregateSealVerifyInfo;
17+
using vm::actor::builtin::types::miner::SectorPreCommitOnChainInfo;
18+
using vm::actor::builtin::v5::miner::ProveCommitAggregate;
19+
namespace vm::actor::builtin::types::miner::SectorPreCommitOnChainInfo;
1220

1321
CommitBatcherImpl::CommitBatcherImpl(
1422
const std::chrono::milliseconds &max_time,
@@ -25,36 +33,91 @@ namespace fc::mining {
2533
const AggregateInput &aggregate_input,
2634
const CommitCallback &callback) {
2735
const SectorNumber &sector_number = sector_info.sector_number;
36+
OUTCOME_TRY(head, api_->ChainHead());
2837

29-
/*
30-
* TODO batch_storage_ and callbacks_ may union DONE
31-
*/
32-
union_storage_.push(sector_number, aggregate_input, callback);
33-
34-
// setPreCommitCutoff(head->epoch(), sector_info); TODO same precommit DONE
38+
union_storage_.push(sector_number, PairStorage(aggregate_input, callback));
3539

3640
if (union_storage_.size() >= max_size_callback_) {
3741
sendCallbacks();
3842
}
43+
// Вынести мьютексы
44+
setCommitCutoff(head->epoch(), sector_info);
45+
3946
return outcome::success();
4047
}
4148

4249
void CommitBatcherImpl::forceSend() {}
4350

4451
void CommitBatcherImpl::sendCallbacks() {
45-
UnionStorage temp_union_storage(std::move(union_storage_));
46-
const auto maybe_result = sendBatch();
47-
for (const auto &[key, pair_storage] : temp_union_storage) {
52+
UnionStorage union_storage_for_send_(std::move(union_storage_));
53+
const auto maybe_result = sendBatch(union_storage_for_send_);
54+
for (const auto &[key, pair_storage] : union_storage_for_send_) {
4855
pair_storage.commit_callback(maybe_result);
49-
} // TODO does it work? because I didn`t iterator++
56+
}
5057

5158
cutoff_start_ = std::chrono::system_clock::now();
5259
closest_cutoff_ = max_delay_;
5360

5461
handle_.reschedule(max_delay_).value();
5562
}
5663

57-
outcome::result<CID> CommitBatcherImpl::sendBatch() {}
64+
outcome::result<CID> CommitBatcherImpl::sendBatch(
65+
UnionStorage &union_storage_for_send) {
66+
if (not union_storage_for_send.size()) {
67+
cutoff_start_ = std::chrono::system_clock::now();
68+
return ERROR_TEXT("Empty Batcher");
69+
}
70+
OUTCOME_TRY(head, api_->ChainHead());
71+
72+
// TODO ?
73+
const size_t total = union_storage_for_send.size();
74+
75+
ProveCommitAggregate::Params params;
76+
77+
std::vector<std::vector<uint8_t>> proofs;
78+
proofs.reserve(total);
79+
80+
BigInt collateral = 0;
81+
82+
for (const auto &[sector_number, pair_storage] : union_storage_for_send) {
83+
84+
85+
TokenAmount sc = getSectorCollateral(head, sector_number, *head.get());
86+
collateral = collateral + sc;
87+
88+
params.sectors.insert(sector_number);
89+
}
90+
91+
92+
93+
for (const auto &[sector_number, pair_storage] : union_storage_for_send) {
94+
proofs.push_back(
95+
pair_storage.aggregate_input.proof);
96+
}
97+
98+
const ActorId mid = miner_address_.getId();
99+
// TODO maybe long (AggregateSealProofs)
100+
params.proof = proof_->AggregateSealProofs(); // OUTCOME_TRY
101+
102+
// TODO CBOR::ENCODE params
103+
104+
// BigDiv usage вместо /(обычное деление)
105+
106+
const TokenAmount max_fee =
107+
fee_config_->max_commit_batch_gas_fee.FeeForSector(proofs.size());
108+
109+
/*
110+
* API_METHOD(StateMinerInfo,
111+
* jwt::kReadPermission,
112+
* MinerInfo,
113+
* const Address &,
114+
* const TipsetKey &)
115+
*/
116+
// OTCOME_TRY(mi, api_->StateMinerInfo());
117+
118+
OUTCOME_TRY(bf, api_->ChainBaseFee(head));
119+
OUTCOME_TRY(nv, api_->StateNetworkVersion(/*NetworkVersion*/, head));
120+
}
58121

59122
void CommitBatcherImpl::setCommitCutoff(const ChainEpoch &current_epoch,
60123
const SectorInfo &sector_info) {
@@ -87,34 +150,38 @@ namespace fc::mining {
87150
}
88151
}
89152

90-
void CommitBatcherImpl::UnionStorage::push(
153+
TokenAmount CommitBatcherImpl::getSectorCollateral(
154+
std::shared_ptr<const Tipset> &head,
91155
const SectorNumber &sector_number,
92-
const AggregateInput &aggregate_input,
93-
const CommitCallback &commit_callback) {
94-
push(sector_number, PairStorage(aggregate_input, commit_callback));
156+
const TipsetKey &tip_set_key) {
157+
OUTCOME_TRY(pci,
158+
api_->StateSectorPreCommitInfo(
159+
miner_address_, sector_number, tip_set_key));
160+
OUTCOME_TRY(collateral,
161+
api_->StateMinerInitialPledgeCollateral(
162+
miner_address_, head->key, pci.info, tip_set_key));
163+
164+
collateral = collateral + pci.PreCommitDeposit;
165+
collateral = max(0, collateral);
166+
167+
return collateral;
95168
}
96169

97-
void CommitBatcherImpl::UnionStorage::push(
98-
const SectorNumber &sector_number,
99-
const CommitCallback &commit_callback,
100-
const AggregateInput &aggregate_input) {
101-
push(sector_number, PairStorage(aggregate_input, commit_callback));
102-
}
103170
void CommitBatcherImpl::UnionStorage::push(const SectorNumber &sector_number,
104171
const PairStorage &pair_storage) {
105172
std::unique_lock<std::mutex> locker(mutex_);
106173
storage_[sector_number] = pair_storage;
107174
}
108175

109-
110-
111176
CommitBatcherImpl::UnionStorage::UnionStorage(
112177
CommitBatcherImpl::UnionStorage &&union_storage1) {
113-
std::unique_lock<std::mutex> locker(mutex_);
178+
std::unique_lock<std::mutex> locker(union_storage1.mutex_);
114179
storage_.insert(std::make_move_iterator(union_storage1.storage_.begin()),
115180
std::make_move_iterator(union_storage1.storage_.end()));
116181
}
117-
size_t CommitBatcherImpl::UnionStorage::size() const {
182+
183+
size_t CommitBatcherImpl::UnionStorage::size() {
184+
std::unique_lock<std::mutex> locker(mutex_);
118185
return storage_.size();
119186
}
120187

@@ -123,11 +190,6 @@ namespace fc::mining {
123190
const CommitCallback &commit_callback)
124191
: aggregate_input(aggregate_input), commit_callback(commit_callback) {}
125192

126-
CommitBatcherImpl::UnionStorage::PairStorage::PairStorage(
127-
const CommitCallback &commit_callback,
128-
const AggregateInput &aggregate_input)
129-
: aggregate_input(aggregate_input), commit_callback(commit_callback) {}
130-
131193
std::map<SectorNumber, CommitBatcherImpl::UnionStorage::PairStorage>::iterator
132194
CommitBatcherImpl::UnionStorage::begin() {
133195
return storage_.begin();
@@ -137,4 +199,9 @@ namespace fc::mining {
137199
CommitBatcherImpl::UnionStorage::end() {
138200
return storage_.end();
139201
}
202+
203+
PairStorage CommitBatcherImpl::UnionStorage::get(const int index) {
204+
std::unique_lock<std::mutex> locker(mutex_);
205+
return storage_[index];
206+
}
140207
} // namespace fc::mining

core/miner/storage_fsm/impl/commit_batcher_impl.hpp

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ namespace fc::mining {
1111

1212
using libp2p::basic::Scheduler;
1313
using primitives::SectorNumber;
14+
using primitives::address::Address;
15+
using primitives::tipset::Tipset;
16+
using primitives::tipset::TipsetKey;
17+
using types::FeeConfig;
18+
using proofs::ProofEngine;
1419

1520
class CommitBatcherImpl : public CommitBatcher {
1621
public:
17-
1822
class UnionStorage {
1923
public:
2024
UnionStorage() = default;
@@ -30,21 +34,14 @@ namespace fc::mining {
3034

3135
PairStorage(const AggregateInput &aggregate_input,
3236
const CommitCallback &commit_callback);
33-
34-
PairStorage(const CommitCallback &commit_callback,
35-
const AggregateInput &aggregate_input);
3637
};
3738

3839
void push(const SectorNumber &sector_number,
39-
const AggregateInput &aggregate_input,
40-
const CommitCallback &commit_callback);
41-
void push(const SectorNumber &sector_number,
42-
const CommitCallback &commit_callback,
43-
const AggregateInput &aggregate_input);
44-
void push(const SectorNumber &sector_number,
45-
const PairStorage &pair_storage); // TODO make one push
40+
const PairStorage &pair_storage);
41+
42+
size_t size();
4643

47-
size_t size() const;
44+
PairStorage get(const int index);
4845

4946
std::map<SectorNumber, PairStorage>::iterator begin();
5047
std::map<SectorNumber, PairStorage>::iterator end();
@@ -53,22 +50,15 @@ namespace fc::mining {
5350
std::mutex mutex_;
5451

5552
std::map<SectorNumber, PairStorage> storage_;
56-
};
53+
}; // TODO вынести PairStorage и удалить UnionStorage. storage_;
5754

5855
CommitBatcherImpl(const std::chrono::milliseconds &max_time,
5956
const size_t &max_size_callback,
6057
const std::shared_ptr<Scheduler> &scheduler);
6158

62-
/*
63-
* 1) Должен уметь отправлять информацию за время, которую ему задали
64-
* NEW 2) Должен уметь отправлять информацию за размер, которую ему задали
65-
* (например 10 коммитов макс вместимость мы должны его отправлять)
66-
* 3) После любой отправки время отправки батчера заводится снова (
67-
* скедлинг, хэндлер)
68-
*/
6959
outcome::result<void> addCommit(const SectorInfo &sector_info,
7060
const AggregateInput &aggregate_input,
71-
const CommitCallback &callBack);
61+
const CommitCallback &callBack) override;
7262

7363
void forceSend() override;
7464

@@ -84,11 +74,17 @@ namespace fc::mining {
8474
size_t max_size_callback_;
8575
UnionStorage union_storage_;
8676
std::shared_ptr<FullNodeApi> api_;
77+
Address miner_address_;
78+
std::shared_ptr<FeeConfig> fee_config_;
79+
std::shared_ptr<ProofEngine> proof_;
8780

8881
void sendCallbacks();
8982

90-
outcome::result<CID> sendBatch();
83+
outcome::result<CID> sendBatch(UnionStorage &union_storage_for_send);
9184

85+
TokenAmount getSectorCollateral(std::shared_ptr<const Tipset> &head,
86+
const SectorNumber &sector_number,
87+
const TipsetKey &tip_set_key);
9288
};
9389

9490
} // namespace fc::mining

core/miner/storage_fsm/types.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ namespace fc::mining::types {
222222

223223
// maxBatchFee = maxBase + maxPerSector * nSectors
224224
BatchConfing max_precommit_batch_gas_fee;
225+
BatchConfing max_commit_batch_gas_fee;
225226

226227
// TODO (Ruslan Gilvanov): TokenAmount max_terminate_gas_fee,
227228
// max_window_poSt_gas_fee, max_publish_deals_fee,

0 commit comments

Comments
 (0)