Skip to content

Commit c0e2ead

Browse files
authored
Fixed reverting in height events (#596)
Signed-off-by: ortyomka <[email protected]>
1 parent e0b4ca0 commit c0e2ead

File tree

11 files changed

+147
-141
lines changed

11 files changed

+147
-141
lines changed

core/miner/storage_fsm/events.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
namespace fc::mining {
1212
using primitives::ChainEpoch;
1313
using primitives::EpochDuration;
14-
using primitives::tipset::Tipset;
14+
using primitives::tipset::TipsetCPtr;
1515

1616
constexpr ChainEpoch kGlobalChainConfidence =
1717
2 * vm::actor::builtin::types::miner::kChainFinality;
@@ -22,8 +22,8 @@ namespace fc::mining {
2222
* @param confidence = current_height - tipset.height
2323
*/
2424
using HeightHandler =
25-
std::function<outcome::result<void>(const Tipset &, ChainEpoch)>;
26-
using RevertHandler = std::function<outcome::result<void>(const Tipset &)>;
25+
std::function<outcome::result<void>(TipsetCPtr, ChainEpoch)>;
26+
using RevertHandler = std::function<outcome::result<void>(TipsetCPtr)>;
2727

2828
virtual ~Events() = default;
2929

core/miner/storage_fsm/impl/events_impl.cpp

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace fc::mining {
1111

1212
EventsImpl::EventsImpl(std::shared_ptr<TipsetCache> tipset_cache)
13-
: global_id_(0), tipset_cache_(std::move(tipset_cache)) {}
13+
: tipset_cache_(std::move(tipset_cache)) {}
1414

1515
outcome::result<std::shared_ptr<EventsImpl>> EventsImpl::createEvents(
1616
const std::shared_ptr<FullNodeApi> &api,
@@ -46,10 +46,19 @@ namespace fc::mining {
4646
RevertHandler revert_handler,
4747
EpochDuration confidence,
4848
ChainEpoch height) {
49+
std::shared_ptr<HeightHandle> height_handler =
50+
std::make_shared<HeightHandle>();
51+
height_handler->confidence = confidence;
52+
height_handler->called = false;
53+
height_handler->handler = handler;
54+
height_handler->revert = revert_handler;
55+
56+
ChainEpoch trigger_at = height + confidence;
57+
4958
std::unique_lock<std::mutex> lock(mutex_);
5059
OUTCOME_TRY(best_tipset, tipset_cache_->best());
5160

52-
ChainEpoch best_height = best_tipset.height();
61+
ChainEpoch best_height = best_tipset->height();
5362

5463
if (best_height >= height + confidence) {
5564
OUTCOME_TRY(tipset, tipset_cache_->getNonNull(height));
@@ -61,27 +70,15 @@ namespace fc::mining {
6170
lock.lock();
6271
OUTCOME_TRYA(best_tipset, tipset_cache_->best());
6372

64-
best_height = best_tipset.height();
73+
best_height = best_tipset->height();
6574

66-
if (best_height >= height + confidence + kGlobalChainConfidence) {
75+
if (best_height >= trigger_at + kGlobalChainConfidence) {
6776
return outcome::success();
6877
}
6978
}
7079

71-
ChainEpoch trigger_at = height + confidence;
72-
73-
// TODO: maybe overflow
74-
uint64_t id = global_id_++;
75-
76-
height_triggers_[id] = HeightHandle{
77-
.confidence = confidence,
78-
.called = false,
79-
.handler = handler,
80-
.revert = revert_handler,
81-
};
82-
83-
message_height_to_trigger_[height].insert(id);
84-
height_to_trigger_[trigger_at].insert(id);
80+
tipsets_heights_[height].insert(height_handler);
81+
triggers_heights_[trigger_at].insert(height_handler);
8582

8683
return outcome::success();
8784
}
@@ -90,29 +87,28 @@ namespace fc::mining {
9087
if (change.type == HeadChangeType::APPLY) {
9188
std::unique_lock<std::mutex> lock(mutex_);
9289

93-
auto maybe_error = tipset_cache_->add(*change.value);
90+
auto maybe_error = tipset_cache_->add(change.value);
9491
if (maybe_error.has_error()) {
9592
logger_->error("Adding tipset into cache failed: {}",
9693
maybe_error.error().message());
9794
return false;
9895
}
9996

10097
auto apply = [&](ChainEpoch height) -> outcome::result<void> {
101-
for (const auto tid : height_to_trigger_[height]) {
102-
auto &handler{height_triggers_.at(tid)};
103-
if (handler.called) {
98+
for (auto &handler : triggers_heights_[height]) {
99+
if (handler->called) {
104100
return outcome::success();
105101
}
106102

107-
auto trigger_height = height - handler.confidence;
103+
auto trigger_height = height - handler->confidence;
108104

109105
OUTCOME_TRY(income_tipset, tipset_cache_->getNonNull(trigger_height));
110106

111-
auto &handle{handler.handler};
107+
auto &handle{handler->handler};
112108
lock.unlock();
113109
auto maybe_error = handle(income_tipset, height);
114110
lock.lock();
115-
height_triggers_[tid].called = true;
111+
handler->called = true;
116112
if (maybe_error.has_error()) {
117113
logger_->error("Height handler is failed: {}",
118114
maybe_error.error().message());
@@ -163,24 +159,28 @@ namespace fc::mining {
163159
// TODO (ortyomka):[FIL-371] log error if h below gcconfidence
164160
// revert height-based triggers
165161

166-
auto revert = [&](ChainEpoch height, const Tipset &tipset) {
167-
for (const auto tid : message_height_to_trigger_[height]) {
168-
auto &revert_handle{height_triggers_[tid].revert};
169-
lock.unlock();
170-
auto maybe_error = revert_handle(tipset);
171-
lock.lock();
172-
height_triggers_[tid].called = false;
162+
auto revert = [&](ChainEpoch height, const TipsetCPtr &tipset) {
163+
for (auto &handler : tipsets_heights_[height]) {
164+
if (not handler->called) {
165+
continue;
166+
}
173167

174-
if (maybe_error.has_error()) {
175-
logger_->error("Revert handler is failed: {}",
176-
maybe_error.error().message());
168+
auto &revert_handle{handler->revert};
169+
lock.unlock();
170+
auto maybe_error = revert_handle(tipset);
171+
lock.lock();
172+
handler->called = false;
173+
174+
if (maybe_error.has_error()) {
175+
logger_->error("Revert handler is failed: {}",
176+
maybe_error.error().message());
177+
}
177178
}
178-
}
179179
};
180180

181181
auto tipset = change.value;
182182

183-
revert(tipset->height(), *tipset);
183+
revert(tipset->height(), tipset);
184184

185185
ChainEpoch sub_height = tipset->height() - 1;
186186
while (true) {
@@ -196,11 +196,11 @@ namespace fc::mining {
196196
break;
197197
}
198198

199-
revert(sub_height, *tipset);
199+
revert(sub_height, tipset);
200200
sub_height--;
201201
}
202202

203-
auto maybe_error = tipset_cache_->revert(*tipset);
203+
auto maybe_error = tipset_cache_->revert(tipset);
204204
if (maybe_error.has_error()) {
205205
logger_->error("Reverting tipset failed: {}",
206206
maybe_error.error().message());

core/miner/storage_fsm/impl/events_impl.hpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ namespace fc::mining {
4040
explicit EventsImpl(std::shared_ptr<TipsetCache> tipset_cache);
4141

4242
struct HeightHandle {
43-
EpochDuration confidence;
44-
bool called;
43+
EpochDuration confidence{};
44+
bool called{};
4545

4646
HeightHandler handler;
4747
RevertHandler revert;
@@ -57,16 +57,12 @@ namespace fc::mining {
5757
*/
5858
std::shared_ptr<Channel<std::vector<HeadChange>>> channel_;
5959

60-
uint64_t global_id_;
61-
62-
// TODO(turuslan): FIL-420 check cache memory usage
63-
std::unordered_map<uint64_t, HeightHandle> height_triggers_;
64-
6560
// TODO(turuslan): FIL-420 check cache memory usage
66-
std::map<ChainEpoch, std::set<uint64_t>> height_to_trigger_; // for apply
61+
std::map<ChainEpoch, std::set<std::shared_ptr<HeightHandle>>>
62+
triggers_heights_; // for apply
6763
// TODO(turuslan): FIL-420 check cache memory usage
68-
std::map<ChainEpoch, std::set<uint64_t>>
69-
message_height_to_trigger_; // for revert
64+
std::map<ChainEpoch, std::set<std::shared_ptr<HeightHandle>>>
65+
tipsets_heights_; // for revert
7066

7167
std::mutex mutex_;
7268

core/miner/storage_fsm/impl/sealing_impl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,7 +1375,7 @@ namespace fc::mining {
13751375
+ vm::actor::builtin::types::miner::kPreCommitChallengeDelay;
13761376

13771377
const auto maybe_error = events_->chainAt(
1378-
[=](const Tipset &,
1378+
[=](const TipsetCPtr &,
13791379
ChainEpoch current_height) -> outcome::result<void> {
13801380
OUTCOME_TRY(head, api_->ChainHead());
13811381

@@ -1402,7 +1402,7 @@ namespace fc::mining {
14021402
FSM_SEND_CONTEXT(info, SealingEvent::kSectorSeedReady, context);
14031403
return outcome::success();
14041404
},
1405-
[=](const Tipset &token) -> outcome::result<void> {
1405+
[=](const TipsetCPtr &) -> outcome::result<void> {
14061406
logger_->warn("revert in interactive commit sector step");
14071407
// TODO(ortyomka): cancel running and restart
14081408
return outcome::success();

core/miner/storage_fsm/impl/tipset_cache_impl.cpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@ namespace fc::mining {
1616
len_ = 0;
1717
}
1818

19-
outcome::result<void> TipsetCacheImpl::add(const Tipset &tipset) {
19+
outcome::result<void> TipsetCacheImpl::add(TipsetCPtr tipset) {
2020
std::unique_lock lock(mutex_);
2121

2222
if (len_ > 0) {
23-
if (cache_[start_]->height() >= tipset.height()) {
23+
if (cache_[start_]->height() >= tipset->height()) {
2424
return TipsetCacheError::kSmallerHeight;
2525
}
2626
}
2727

28-
auto current_height = tipset.height();
28+
auto current_height = tipset->height();
2929
if (len_ > 0) {
3030
current_height = cache_[start_]->height();
3131
}
3232

33-
while (++current_height < tipset.height()) {
33+
while (++current_height < tipset->height()) {
3434
start_ = mod(start_ + 1);
35-
cache_[start_] = boost::none;
35+
cache_[start_] = nullptr;
3636
if (len_ < cache_.size()) {
3737
len_++;
3838
}
@@ -47,45 +47,44 @@ namespace fc::mining {
4747
return outcome::success();
4848
}
4949

50-
outcome::result<void> TipsetCacheImpl::revert(const Tipset &tipset) {
50+
outcome::result<void> TipsetCacheImpl::revert(TipsetCPtr tipset) {
5151
std::unique_lock lock(mutex_);
5252

5353
if (len_ == 0) {
5454
return outcome::success();
5555
}
5656

57-
if (cache_[start_] != tipset) {
57+
if (*cache_[start_] != *tipset) {
5858
return TipsetCacheError::kNotMatchHead;
5959
}
6060

61-
cache_[start_] = boost::none;
61+
cache_[start_] = nullptr;
6262
start_ = mod(start_ - 1);
6363
len_--;
6464

65-
while (len_ && cache_[start_] == boost::none) {
65+
while (len_ && cache_[start_] == nullptr) {
6666
start_ = mod(start_ - 1);
6767
len_--;
6868
}
6969
return outcome::success();
7070
}
7171

72-
outcome::result<Tipset> TipsetCacheImpl::getNonNull(ChainEpoch height) {
72+
outcome::result<TipsetCPtr> TipsetCacheImpl::getNonNull(ChainEpoch height) {
7373
while (true) {
7474
OUTCOME_TRY(tipset, get(height++));
7575

7676
if (tipset) {
77-
return tipset.get();
77+
return std::move(tipset);
7878
}
7979
}
8080
}
8181

82-
outcome::result<boost::optional<Tipset>> TipsetCacheImpl::get(
83-
ChainEpoch height) {
82+
outcome::result<TipsetCPtr> TipsetCacheImpl::get(ChainEpoch height) {
8483
std::shared_lock lock(mutex_);
8584

8685
if (len_ == 0) {
8786
OUTCOME_TRY(tipset, api_->ChainGetTipSetByHeight(height, {}));
88-
return *tipset;
87+
return std::move(tipset);
8988
}
9089

9190
auto head_height = cache_[start_]->height();
@@ -94,7 +93,7 @@ namespace fc::mining {
9493
return TipsetCacheError::kNotInCache;
9594
}
9695

97-
boost::optional<Tipset> tail = boost::none;
96+
TipsetCPtr tail = nullptr;
9897
uint64_t i;
9998
for (i = 1; i <= len_; i++) {
10099
tail = cache_[mod(start_ - len_ + i)];
@@ -105,19 +104,19 @@ namespace fc::mining {
105104

106105
if (height < tail->height()) {
107106
OUTCOME_TRY(tipset, api_->ChainGetTipSetByHeight(height, {}));
108-
return *tipset;
107+
return std::move(tipset);
109108
}
110109

111110
return cache_[mod(start_ - (head_height - height))];
112111
}
113112

114-
outcome::result<Tipset> TipsetCacheImpl::best() const {
113+
outcome::result<TipsetCPtr> TipsetCacheImpl::best() const {
115114
std::shared_lock lock(mutex_);
116115
if (len_ == 0) {
117116
OUTCOME_TRY(tipset, api_->ChainHead());
118-
return *tipset;
117+
return std::move(tipset);
119118
}
120-
return cache_[start_].value();
119+
return cache_[start_];
121120
}
122121

123122
int64_t TipsetCacheImpl::mod(int64_t x) {

core/miner/storage_fsm/impl/tipset_cache_impl.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,22 @@ namespace fc::mining {
2020

2121
TipsetCacheImpl(uint64_t capability, std::shared_ptr<FullNodeApi> api);
2222

23-
outcome::result<void> add(const Tipset &tipset) override;
23+
outcome::result<void> add(TipsetCPtr tipset) override;
2424

25-
outcome::result<void> revert(const Tipset &tipset) override;
25+
outcome::result<void> revert(TipsetCPtr tipset) override;
2626

27-
outcome::result<Tipset> getNonNull(ChainEpoch height) override;
27+
outcome::result<TipsetCPtr> getNonNull(ChainEpoch height) override;
2828

29-
outcome::result<boost::optional<Tipset>> get(ChainEpoch height) override;
29+
outcome::result<TipsetCPtr> get(ChainEpoch height) override;
3030

31-
outcome::result<Tipset> best() const override;
31+
outcome::result<TipsetCPtr> best() const override;
3232

3333
private:
3434
int64_t mod(int64_t x);
3535

3636
mutable std::shared_mutex mutex_;
3737

38-
std::vector<boost::optional<Tipset>> cache_;
38+
std::vector<TipsetCPtr> cache_;
3939

4040
int64_t start_;
4141

0 commit comments

Comments
 (0)