generated from Warchant/cmake-hunter-seed
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathmining.cpp
230 lines (209 loc) · 7.25 KB
/
mining.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#include "miner/mining.hpp"
#include "common/logger.hpp"
#include "primitives/block/rand.hpp"
#include "vm/actor/builtin/types/market/policy.hpp"
#include "vm/runtime/pricelist.hpp"
#define OUTCOME_TRY_LOG(tag, r) \
{ \
auto &&_r{r}; \
if (!_r) { \
spdlog::error("{}: {}", tag, _r.error().message()); \
return _r.error(); \
} \
}
#define OUTCOME_REBOOT(base, tag, r, time) \
{ \
auto &&_r{r}; \
if (!_r) { \
spdlog::error("{}: {}", tag, _r.error().message()); \
return base->reboot(time); \
} \
}
namespace fc::mining {
using BlsSignature = crypto::bls::Signature;
outcome::result<std::shared_ptr<Mining>> Mining::create(
std::shared_ptr<Scheduler> scheduler,
std::shared_ptr<UTCClock> clock,
std::shared_ptr<FullNodeApi> api,
std::shared_ptr<Prover> prover,
const Address &miner) {
OUTCOME_TRY(version, api->Version());
auto mining{std::make_shared<Mining>()};
mining->scheduler = std::move(scheduler);
mining->clock = std::move(clock);
mining->api = std::move(api);
mining->prover = std::move(prover);
mining->miner = miner;
mining->block_delay = version.block_delay;
mining->sleep_time = 5;
mining->propagation =
std::min<uint64_t>(kPropagationDelaySecs, mining->block_delay * 3 / 10);
return mining;
}
void Mining::start() {
waitParent();
}
void Mining::reboot(uint64_t time) {
wait(time, false, [weak{weak_from_this()}] {
if (auto self{weak.lock()}) {
self->waitParent();
}
});
}
void Mining::waitParent() {
OUTCOME_REBOOT(this, "Mining::waitParent error", bestParent(), 5);
wait(ts->getMinTimestamp() + propagation, true, [weak{weak_from_this()}] {
if (auto self{weak.lock()}) {
self->waitBeacon();
}
});
}
void Mining::waitBeacon() {
api->BeaconGetEntry(
[weak{weak_from_this()}](auto beacon) {
if (auto self{weak.lock()}) {
OUTCOME_REBOOT(self, "Mining::waitBeacon error", beacon, 1);
OUTCOME_REBOOT(self, "Mining::waitInfo error", self->waitInfo(), 1);
}
},
height());
}
outcome::result<void> Mining::waitInfo() {
OUTCOME_TRY(bestParent());
auto maybe_mined = std::make_pair(ts->key, skip);
if (last_mined == maybe_mined) {
wait(block_delay, false, [weak{weak_from_this()}] {
if (auto self{weak.lock()}) {
self->waitParent();
}
});
} else {
api->MinerGetBaseInfo(
[weak{weak_from_this()}, mined{std::move(maybe_mined)}](auto _info) {
if (auto self{weak.lock()}) {
OUTCOME_REBOOT(self, "Mining::waitInfo error", _info, 1);
self->info = std::move(_info.value());
OUTCOME_REBOOT(self, "Mining::prepare error", self->prepare(), 1);
self->last_mined = mined;
}
},
miner,
height(),
ts->key);
}
return outcome::success();
}
outcome::result<void> Mining::prepare() {
OUTCOME_TRY(block1, prepareBlock());
auto time{ts->getMinTimestamp() + (skip + 1) * block_delay};
if (block1) {
block1->timestamp = time;
wait(time, true, [weak{weak_from_this()}, block1{std::move(*block1)}]() {
if (auto self{weak.lock()}) {
OUTCOME_REBOOT(self, "Mining::submit error", self->submit(block1), 1);
}
});
} else {
++skip;
wait(time + propagation, true, [weak{weak_from_this()}] {
if (auto self{weak.lock()}) {
self->waitParent();
}
});
}
return outcome::success();
}
outcome::result<void> Mining::submit(const BlockTemplate &block1) {
// TODO(turuslan): slash filter
OUTCOME_TRY(block2, api->MinerCreateBlock(block1));
auto result{api->SyncSubmitBlock(block2)};
waitParent();
OUTCOME_TRY(result);
return outcome::success();
}
outcome::result<void> Mining::bestParent() {
OUTCOME_TRY(ts2, api->ChainHead());
if (ts) {
if (ts2->key == ts->key) {
return outcome::success();
}
}
OUTCOME_TRY(weight2, api->ChainTipSetWeight(ts2->key));
if (!ts || weight2 > weight) {
ts = *ts2;
weight = weight2;
skip = 0;
}
return outcome::success();
}
ChainEpoch Mining::height() const {
return gsl::narrow<ChainEpoch>(ts->height() + skip + 1);
}
void Mining::wait(uint64_t sec, bool abs, Scheduler::Callback cb) {
if (abs) {
sec -= clock->nowUTC().count();
}
scheduler->schedule(std::move(cb),
std::chrono::seconds{std::max<uint64_t>(0, sec)});
}
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
outcome::result<boost::optional<BlockTemplate>> Mining::prepareBlock() {
if (info && info->has_min_power) {
auto vrf{[&](auto &rand) -> outcome::result<BlsSignature> {
OUTCOME_TRY(sig, api->WalletSign(info->worker, copy(rand)));
return boost::get<BlsSignature>(sig);
}};
const BlockRand rand{
miner,
height(),
info->beacons,
info->prev_beacon,
*ts,
};
OUTCOME_TRY(election_vrf, vrf(rand.election));
auto win_count{computeWinCount(
election_vrf, info->miner_power, info->network_power)};
if (win_count > 0) {
spdlog::info("height={} win={} power={}% ticket={}",
height(),
win_count,
bigdiv(100 * info->miner_power, info->network_power),
common::hex_lower(election_vrf));
OUTCOME_TRY(ticket_vrf, vrf(rand.ticket));
std::vector<primitives::sector::PoStProof> post_proof;
if (kFakeWinningPost) {
copy(post_proof.emplace_back().proof,
common::span::cbytes(kFakeWinningPostStr));
} else {
OUTCOME_TRYA(post_proof,
prover->generateWinningPoSt(
miner.getId(), info->sectors, rand.win));
}
OUTCOME_TRY(messages,
api->MpoolSelect(ts->key, ticketQuality(ticket_vrf)));
return BlockTemplate{
miner,
ts->key.cids(),
primitives::block::Ticket{copy(ticket_vrf)},
primitives::block::ElectionProof{win_count, copy(election_vrf)},
std::move(info->beacons),
messages,
height(),
{},
std::move(post_proof),
};
}
}
return boost::none;
}
double ticketQuality(BytesIn ticket) {
return 1
- boost::multiprecision::cpp_rational{blakeBigInt(ticket),
BigInt{1} << 256}
.convert_to<double>();
}
} // namespace fc::mining