10
10
#include " vm/actor/builtin/types/market/policy.hpp"
11
11
#include " vm/runtime/pricelist.hpp"
12
12
13
- #define OUTCOME_LOG (tag, r ) \
13
+ #define OUTCOME_TRY_LOG (tag, r ) \
14
14
{ \
15
15
auto &&_r{r}; \
16
16
if (!_r) { \
17
17
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 ); \
19
28
} \
20
29
}
21
30
@@ -36,6 +45,7 @@ namespace fc::mining {
36
45
mining->prover = std::move (prover);
37
46
mining->miner = miner;
38
47
mining->block_delay = version.block_delay ;
48
+ mining->sleep_time = 5 ;
39
49
mining->propagation =
40
50
std::min<uint64_t >(kPropagationDelaySecs , mining->block_delay * 3 / 10 );
41
51
return mining;
@@ -45,33 +55,53 @@ namespace fc::mining {
45
55
waitParent ();
46
56
}
47
57
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
+
48
66
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
+ }
52
72
});
53
73
}
54
74
55
- outcome::result< void > Mining::waitBeacon () {
75
+ void Mining::waitBeacon () {
56
76
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
+ }
60
82
},
61
83
height ());
62
- return outcome::success ();
63
84
}
64
85
65
86
outcome::result<void > Mining::waitInfo () {
66
87
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
+ });
69
95
} else {
70
96
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
+ }
75
105
},
76
106
miner,
77
107
height (),
@@ -85,12 +115,18 @@ namespace fc::mining {
85
115
auto time {ts->getMinTimestamp () + (skip + 1 ) * block_delay};
86
116
if (block1) {
87
117
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
+ }
90
122
});
91
123
} else {
92
124
++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
+ });
94
130
}
95
131
return outcome::success ();
96
132
}
@@ -128,10 +164,6 @@ namespace fc::mining {
128
164
if (abs ) {
129
165
sec -= clock ->nowUTC ().count ();
130
166
}
131
- cb = [cb{std::move (cb)}, self{shared_from_this ()}] {
132
- // stop condition or weak_ptr
133
- cb ();
134
- };
135
167
scheduler->schedule (std::move (cb),
136
168
std::chrono::seconds{std::max<uint64_t >(0 , sec)});
137
169
}
0 commit comments