diff --git a/CMakeLists.txt b/CMakeLists.txt index bd97cb8ba9..6d4b64a176 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 ) diff --git a/core/miner/mining.cpp b/core/miner/mining.cpp index 5fd0ac7a3d..66ca4ad42d 100644 --- a/core/miner/mining.cpp +++ b/core/miner/mining.cpp @@ -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); \ } \ } @@ -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(kPropagationDelaySecs, mining->block_delay * 3 / 10); return mining; @@ -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 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 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(), @@ -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(); } @@ -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(0, sec)}); } diff --git a/core/miner/mining.hpp b/core/miner/mining.hpp index f8bf0f1ce2..51e77f1dd9 100644 --- a/core/miner/mining.hpp +++ b/core/miner/mining.hpp @@ -43,7 +43,8 @@ namespace fc::mining { const Address &miner); void start(); void waitParent(); - outcome::result waitBeacon(); + void reboot(uint64_t time); + void waitBeacon(); outcome::result waitInfo(); outcome::result prepare(); outcome::result submit(const BlockTemplate &block1); @@ -58,12 +59,12 @@ namespace fc::mining { std::shared_ptr prover; Address miner; uint64_t block_delay{0}; + uint64_t sleep_time{0}; uint64_t propagation{0}; boost::optional ts; BigInt weight; size_t skip{}; - // TODO(turuslan): FIL-420 check cache memory usage - std::unordered_set, pair_hash> mined; + std::pair last_mined; boost::optional info; };