66#pragma once
77#include " commit_batcher_impl.hpp"
88#include < iterator>
9+ #include " vm/actor/builtin/v5/miner/miner_actor.hpp"
910
1011namespace 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 §or_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 ¤t_epoch,
60123 const SectorInfo §or_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 §or_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 §or_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 §or_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
0 commit comments