Skip to content

Commit 679e44c

Browse files
authoredMar 4, 2022
Fix mining (#613)
* Fix mining Signed-off-by: ortyomka <iurin.art@gmail.com>
1 parent afa22ed commit 679e44c

File tree

3 files changed

+61
-28
lines changed

3 files changed

+61
-28
lines changed
 

‎CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ set(
1717

1818
include("cmake/Hunter/init.cmake")
1919
HunterGate(
20-
URL "https://github.com/soramitsu/soramitsu-hunter/archive/tags/v0.23.257-soramitsu16.tar.gz"
21-
SHA1 "64a1180e8cbeb98d2cfd786bfb725dcdc81d8fa9"
20+
URL "https://github.com/soramitsu/soramitsu-hunter/archive/v0.23.257-soramitsu23.tar.gz"
21+
SHA1 "f42cec23fced76800b87191525ee1fa6f9c088a5"
2222
LOCAL
2323
)
2424

‎core/miner/mining.cpp

+55-23
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,21 @@
1010
#include "vm/actor/builtin/types/market/policy.hpp"
1111
#include "vm/runtime/pricelist.hpp"
1212

13-
#define OUTCOME_LOG(tag, r) \
13+
#define OUTCOME_TRY_LOG(tag, r) \
1414
{ \
1515
auto &&_r{r}; \
1616
if (!_r) { \
1717
spdlog::error("{}: {}", tag, _r.error().message()); \
18-
return; \
18+
return _r.error(); \
19+
} \
20+
}
21+
22+
#define OUTCOME_REBOOT(base, tag, r, time) \
23+
{ \
24+
auto &&_r{r}; \
25+
if (!_r) { \
26+
spdlog::error("{}: {}", tag, _r.error().message()); \
27+
return base->reboot(time); \
1928
} \
2029
}
2130

@@ -36,6 +45,7 @@ namespace fc::mining {
3645
mining->prover = std::move(prover);
3746
mining->miner = miner;
3847
mining->block_delay = version.block_delay;
48+
mining->sleep_time = 5;
3949
mining->propagation =
4050
std::min<uint64_t>(kPropagationDelaySecs, mining->block_delay * 3 / 10);
4151
return mining;
@@ -45,33 +55,53 @@ namespace fc::mining {
4555
waitParent();
4656
}
4757

58+
void Mining::reboot(uint64_t time) {
59+
wait(time, false, [weak{weak_from_this()}] {
60+
if (auto self{weak.lock()}) {
61+
self->waitParent();
62+
}
63+
});
64+
}
65+
4866
void Mining::waitParent() {
49-
OUTCOME_LOG("Mining::waitParent error", bestParent());
50-
wait(ts->getMinTimestamp() + propagation, true, [this] {
51-
OUTCOME_LOG("Mining::waitBeacon error", waitBeacon());
67+
OUTCOME_REBOOT(this, "Mining::waitParent error", bestParent(), 5);
68+
wait(ts->getMinTimestamp() + propagation, true, [weak{weak_from_this()}] {
69+
if (auto self{weak.lock()}) {
70+
self->waitBeacon();
71+
}
5272
});
5373
}
5474

55-
outcome::result<void> Mining::waitBeacon() {
75+
void Mining::waitBeacon() {
5676
api->BeaconGetEntry(
57-
[self{shared_from_this()}](auto beacon) {
58-
OUTCOME_LOG("Mining::waitBeacon error", beacon);
59-
OUTCOME_LOG("Mining::waitInfo error", self->waitInfo());
77+
[weak{weak_from_this()}](auto beacon) {
78+
if (auto self{weak.lock()}) {
79+
OUTCOME_REBOOT(self, "Mining::waitBeacon error", beacon, 1);
80+
OUTCOME_REBOOT(self, "Mining::waitInfo error", self->waitInfo(), 1);
81+
}
6082
},
6183
height());
62-
return outcome::success();
6384
}
6485

6586
outcome::result<void> Mining::waitInfo() {
6687
OUTCOME_TRY(bestParent());
67-
if (!mined.emplace(ts->key, skip).second) {
68-
wait(block_delay, false, [this] { waitParent(); });
88+
auto maybe_mined = std::make_pair(ts->key, skip);
89+
if (last_mined == maybe_mined) {
90+
wait(block_delay, false, [weak{weak_from_this()}] {
91+
if (auto self{weak.lock()}) {
92+
self->waitParent();
93+
}
94+
});
6995
} else {
7096
api->MinerGetBaseInfo(
71-
[self{shared_from_this()}](auto _info) {
72-
OUTCOME_LOG("Mining::waitInfo error", _info);
73-
self->info = std::move(_info.value());
74-
OUTCOME_LOG("Mining::prepare error", self->prepare());
97+
[weak{weak_from_this()}, mined{std::move(maybe_mined)}](auto _info) {
98+
if (auto self{weak.lock()}) {
99+
OUTCOME_REBOOT(self, "Mining::waitInfo error", _info, 1);
100+
self->info = std::move(_info.value());
101+
OUTCOME_REBOOT(self, "Mining::prepare error", self->prepare(), 1);
102+
103+
self->last_mined = mined;
104+
}
75105
},
76106
miner,
77107
height(),
@@ -85,12 +115,18 @@ namespace fc::mining {
85115
auto time{ts->getMinTimestamp() + (skip + 1) * block_delay};
86116
if (block1) {
87117
block1->timestamp = time;
88-
wait(time, true, [this, block1{std::move(*block1)}]() {
89-
OUTCOME_LOG("Mining::submit error", submit(block1));
118+
wait(time, true, [weak{weak_from_this()}, block1{std::move(*block1)}]() {
119+
if (auto self{weak.lock()}) {
120+
OUTCOME_REBOOT(self, "Mining::submit error", self->submit(block1), 1);
121+
}
90122
});
91123
} else {
92124
++skip;
93-
wait(time + propagation, true, [this] { waitParent(); });
125+
wait(time + propagation, true, [weak{weak_from_this()}] {
126+
if (auto self{weak.lock()}) {
127+
self->waitParent();
128+
}
129+
});
94130
}
95131
return outcome::success();
96132
}
@@ -128,10 +164,6 @@ namespace fc::mining {
128164
if (abs) {
129165
sec -= clock->nowUTC().count();
130166
}
131-
cb = [cb{std::move(cb)}, self{shared_from_this()}] {
132-
// stop condition or weak_ptr
133-
cb();
134-
};
135167
scheduler->schedule(std::move(cb),
136168
std::chrono::seconds{std::max<uint64_t>(0, sec)});
137169
}

‎core/miner/mining.hpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ namespace fc::mining {
4343
const Address &miner);
4444
void start();
4545
void waitParent();
46-
outcome::result<void> waitBeacon();
46+
void reboot(uint64_t time);
47+
void waitBeacon();
4748
outcome::result<void> waitInfo();
4849
outcome::result<void> prepare();
4950
outcome::result<void> submit(const BlockTemplate &block1);
@@ -58,12 +59,12 @@ namespace fc::mining {
5859
std::shared_ptr<Prover> prover;
5960
Address miner;
6061
uint64_t block_delay{0};
62+
uint64_t sleep_time{0};
6163
uint64_t propagation{0};
6264
boost::optional<Tipset> ts;
6365
BigInt weight;
6466
size_t skip{};
65-
// TODO(turuslan): FIL-420 check cache memory usage
66-
std::unordered_set<std::pair<TipsetKey, size_t>, pair_hash> mined;
67+
std::pair<TipsetKey, size_t> last_mined;
6768
boost::optional<MiningBaseInfo> info;
6869
};
6970

0 commit comments

Comments
 (0)
Please sign in to comment.