Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mining #613

Merged
merged 3 commits into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ set(

include("cmake/Hunter/init.cmake")
HunterGate(
URL "https://github.com/soramitsu/soramitsu-hunter/archive/tags/v0.23.257-soramitsu16.tar.gz"
SHA1 "64a1180e8cbeb98d2cfd786bfb725dcdc81d8fa9"
URL "https://github.com/soramitsu/soramitsu-hunter/archive/v0.23.257-soramitsu23.tar.gz"
SHA1 "f42cec23fced76800b87191525ee1fa6f9c088a5"
LOCAL
)

Expand Down
78 changes: 55 additions & 23 deletions core/miner/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@
#include "vm/actor/builtin/types/market/policy.hpp"
#include "vm/runtime/pricelist.hpp"

#define OUTCOME_LOG(tag, r) \
#define OUTCOME_TRY_LOG(tag, r) \
{ \
auto &&_r{r}; \
if (!_r) { \
spdlog::error("{}: {}", tag, _r.error().message()); \
return; \
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); \
} \
}

Expand All @@ -36,6 +45,7 @@ namespace fc::mining {
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;
Expand All @@ -45,33 +55,53 @@ namespace fc::mining {
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_LOG("Mining::waitParent error", bestParent());
wait(ts->getMinTimestamp() + propagation, true, [this] {
OUTCOME_LOG("Mining::waitBeacon error", waitBeacon());
OUTCOME_REBOOT(this, "Mining::waitParent error", bestParent(), 5);
wait(ts->getMinTimestamp() + propagation, true, [weak{weak_from_this()}] {
if (auto self{weak.lock()}) {
self->waitBeacon();
}
});
}

outcome::result<void> Mining::waitBeacon() {
void Mining::waitBeacon() {
api->BeaconGetEntry(
[self{shared_from_this()}](auto beacon) {
OUTCOME_LOG("Mining::waitBeacon error", beacon);
OUTCOME_LOG("Mining::waitInfo error", self->waitInfo());
[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());
return outcome::success();
}

outcome::result<void> Mining::waitInfo() {
OUTCOME_TRY(bestParent());
if (!mined.emplace(ts->key, skip).second) {
wait(block_delay, false, [this] { waitParent(); });
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(
[self{shared_from_this()}](auto _info) {
OUTCOME_LOG("Mining::waitInfo error", _info);
self->info = std::move(_info.value());
OUTCOME_LOG("Mining::prepare error", self->prepare());
[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(),
Expand All @@ -85,12 +115,18 @@ namespace fc::mining {
auto time{ts->getMinTimestamp() + (skip + 1) * block_delay};
if (block1) {
block1->timestamp = time;
wait(time, true, [this, block1{std::move(*block1)}]() {
OUTCOME_LOG("Mining::submit error", submit(block1));
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, [this] { waitParent(); });
wait(time + propagation, true, [weak{weak_from_this()}] {
if (auto self{weak.lock()}) {
self->waitParent();
}
});
}
return outcome::success();
}
Expand Down Expand Up @@ -128,10 +164,6 @@ namespace fc::mining {
if (abs) {
sec -= clock->nowUTC().count();
}
cb = [cb{std::move(cb)}, self{shared_from_this()}] {
// stop condition or weak_ptr
cb();
};
scheduler->schedule(std::move(cb),
std::chrono::seconds{std::max<uint64_t>(0, sec)});
}
Expand Down
7 changes: 4 additions & 3 deletions core/miner/mining.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ namespace fc::mining {
const Address &miner);
void start();
void waitParent();
outcome::result<void> waitBeacon();
void reboot(uint64_t time);
void waitBeacon();
outcome::result<void> waitInfo();
outcome::result<void> prepare();
outcome::result<void> submit(const BlockTemplate &block1);
Expand All @@ -58,12 +59,12 @@ namespace fc::mining {
std::shared_ptr<Prover> prover;
Address miner;
uint64_t block_delay{0};
uint64_t sleep_time{0};
uint64_t propagation{0};
boost::optional<Tipset> ts;
BigInt weight;
size_t skip{};
// TODO(turuslan): FIL-420 check cache memory usage
std::unordered_set<std::pair<TipsetKey, size_t>, pair_hash> mined;
std::pair<TipsetKey, size_t> last_mined;
boost::optional<MiningBaseInfo> info;
};

Expand Down