Skip to content

Commit df0aba8

Browse files
ElestriasAlexey-N-Chernyshovortyomka
authored
Feature/tipsetcash (#468)
* tipsetcash update Signed-off-by: elestrias <[email protected]> * Tipsetcache_tests Signed-off-by: elestrias <[email protected]> * build fix * include changes * Remove Events error Signed-off-by: ortyomka <[email protected]> Co-authored-by: Alexey <[email protected]> Co-authored-by: ortyomka <[email protected]>
1 parent 2e8c1dc commit df0aba8

File tree

10 files changed

+59
-83
lines changed

10 files changed

+59
-83
lines changed

core/miner/impl/miner_impl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ namespace fc::miner {
8181
std::shared_ptr<TipsetCache> tipset_cache =
8282
std::make_shared<TipsetCacheImpl>(
8383
2 * kGlobalChainConfidence,
84-
[=](auto h) { return api->ChainGetTipSetByHeight(h, {}); });
84+
api);
8585
OUTCOME_TRY(events, EventsImpl::createEvents(api, tipset_cache));
8686
std::shared_ptr<PreCommitPolicy> precommit_policy =
8787
std::make_shared<BasicPreCommitPolicy>(

core/miner/storage_fsm/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ add_library(tipset_cache
2424
target_link_libraries(tipset_cache
2525
tipset
2626
Boost::boost
27+
api
2728
)
2829

2930
add_library(events

core/miner/storage_fsm/events.hpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,4 @@ namespace fc::mining {
3333
ChainEpoch height) = 0;
3434
};
3535

36-
enum class EventsError {
37-
kNotFoundTipset = 1,
38-
};
39-
4036
} // namespace fc::mining
41-
42-
OUTCOME_HPP_DECLARE_ERROR(fc::mining, EventsError);

core/miner/storage_fsm/impl/events_impl.cpp

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,9 @@ namespace fc::mining {
4747
EpochDuration confidence,
4848
ChainEpoch height) {
4949
std::unique_lock<std::mutex> lock(mutex_);
50-
auto best_tipset = tipset_cache_->best();
50+
OUTCOME_TRY(best_tipset, tipset_cache_->best());
5151

52-
// TODO(ortyomka): [FIL-370] should be updated after update TipSetCache
53-
if (!best_tipset) {
54-
return EventsError::kNotFoundTipset;
55-
}
56-
57-
ChainEpoch best_height = best_tipset->height();
52+
ChainEpoch best_height = best_tipset.height();
5853

5954
if (best_height >= height + confidence) {
6055
OUTCOME_TRY(tipset, tipset_cache_->getNonNull(height));
@@ -64,13 +59,9 @@ namespace fc::mining {
6459
OUTCOME_TRY(handler(tipset, best_height));
6560

6661
lock.lock();
67-
best_tipset = tipset_cache_->best();
62+
OUTCOME_TRYA(best_tipset, tipset_cache_->best());
6863

69-
// TODO(ortyomka): [FIL-370] should be updated after update TipSetCache
70-
if (!best_tipset) {
71-
return EventsError::kNotFoundTipset;
72-
}
73-
best_height = best_tipset->height();
64+
best_height = best_tipset.height();
7465

7566
if (best_height >= height + confidence + kGlobalChainConfidence) {
7667
return outcome::success();
@@ -226,13 +217,3 @@ namespace fc::mining {
226217
}
227218

228219
} // namespace fc::mining
229-
230-
OUTCOME_CPP_DEFINE_CATEGORY(fc::mining, EventsError, e) {
231-
using fc::mining::EventsError;
232-
switch (e) {
233-
case (EventsError::kNotFoundTipset):
234-
return "Events: not found tipset";
235-
default:
236-
return "Events: unknown error";
237-
}
238-
}

core/miner/storage_fsm/impl/tipset_cache_impl.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
#include "primitives/tipset/tipset.hpp"
99

1010
namespace fc::mining {
11-
1211
TipsetCacheImpl::TipsetCacheImpl(uint64_t capability,
13-
GetTipsetFunction get_function)
14-
: get_function_(std::move(get_function)) {
12+
std::shared_ptr<FullNodeApi> api)
13+
: api_(std::move(api)) {
1514
cache_.resize(capability);
1615
start_ = 0;
1716
len_ = 0;
@@ -85,7 +84,7 @@ namespace fc::mining {
8584
std::shared_lock lock(mutex_);
8685

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

@@ -105,16 +104,20 @@ namespace fc::mining {
105104
}
106105

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

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

115-
boost::optional<Tipset> TipsetCacheImpl::best() const {
114+
outcome::result<Tipset> TipsetCacheImpl::best() const {
116115
std::shared_lock lock(mutex_);
117-
return cache_[start_];
116+
if (len_ == 0) {
117+
OUTCOME_TRY(tipset, api_->ChainHead());
118+
return *tipset;
119+
}
120+
return cache_[start_].value();
118121
}
119122

120123
int64_t TipsetCacheImpl::mod(int64_t x) {

core/miner/storage_fsm/impl/tipset_cache_impl.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,20 @@
55

66
#pragma once
77

8-
#include "miner/storage_fsm/tipset_cache.hpp"
9-
10-
#include "primitives/tipset/tipset_key.hpp"
11-
128
#include <shared_mutex>
9+
#include "api/full_node/node_api.hpp"
10+
#include "miner/storage_fsm/tipset_cache.hpp"
1311

1412
namespace fc::mining {
13+
using api::FullNodeApi;
1514

16-
// TODO(ortyomka): [FIL-370] update it
1715
class TipsetCacheImpl : public TipsetCache {
1816
public:
1917
using GetTipsetFunction =
2018
std::function<outcome::result<primitives::tipset::TipsetCPtr>(
2119
ChainEpoch)>;
2220

23-
TipsetCacheImpl(uint64_t capability, GetTipsetFunction get_function);
21+
TipsetCacheImpl(uint64_t capability, std::shared_ptr<FullNodeApi> api);
2422

2523
outcome::result<void> add(const Tipset &tipset) override;
2624

@@ -30,7 +28,7 @@ namespace fc::mining {
3028

3129
outcome::result<boost::optional<Tipset>> get(ChainEpoch height) override;
3230

33-
boost::optional<Tipset> best() const override;
31+
outcome::result<Tipset> best() const override;
3432

3533
private:
3634
int64_t mod(int64_t x);
@@ -44,5 +42,7 @@ namespace fc::mining {
4442
uint64_t len_;
4543

4644
GetTipsetFunction get_function_;
45+
46+
std::shared_ptr<FullNodeApi> api_;
4747
};
4848
} // namespace fc::mining

core/miner/storage_fsm/tipset_cache.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ namespace fc::mining {
5050
* Get Head tipset
5151
* @return head tipset
5252
*/
53-
virtual boost::optional<Tipset> best() const = 0;
53+
virtual outcome::result<Tipset> best() const = 0;
5454
};
5555

5656
enum class TipsetCacheError {

test/core/miner/events_test.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,26 @@ namespace fc::mining {
3737
/**
3838
* @given events
3939
* @when try to add Handler, but tipsetcache cannot get tipset
40-
* @then error EventsError::kNotFoundTipset occurs
40+
* @then error occurs
4141
*/
4242
TEST_F(EventsTest, ChainAtNotFoundTipset) {
4343
Events::HeightHandler height_handler;
4444
Events::RevertHandler revert_handler;
4545
EpochDuration confidence = 4;
4646
ChainEpoch height = 4;
4747

48-
EXPECT_CALL(*tipset_cache_, best()).WillOnce(testing::Return(boost::none));
48+
const auto error = ERROR_TEXT("API_ERROR");
49+
EXPECT_CALL(*tipset_cache_, best()).WillOnce(testing::Return(error));
4950

5051
EXPECT_OUTCOME_ERROR(
51-
EventsError::kNotFoundTipset,
52+
error,
5253
events_->chainAt(height_handler, revert_handler, confidence, height));
5354
}
5455

5556
/**
5657
* @given events, tipset that should be executed immediately
5758
* @when try to add Handler, but tipsetcache cannot get tipset in second time
58-
* @then error EventsError::kNotFoundTipset occurs
59+
* @then error occurs
5960
*/
6061
TEST_F(EventsTest, ChainAtNotFoundTipsetAfterExecution) {
6162
Events::HeightHandler height_handler =
@@ -66,18 +67,19 @@ namespace fc::mining {
6667
EpochDuration confidence = 4;
6768
ChainEpoch height = 4;
6869

70+
const auto error = ERROR_TEXT("API_ERROR");
6971
BlockHeader block;
7072
block.height = 9;
7173
Tipset tipset(TipsetKey(), {block});
7274
EXPECT_CALL(*tipset_cache_, best())
7375
.WillOnce(testing::Return(tipset))
74-
.WillOnce(testing::Return(boost::none));
76+
.WillOnce(testing::Return(error));
7577

7678
EXPECT_CALL(*tipset_cache_, getNonNull(height))
7779
.WillOnce(testing::Return(outcome::success(tipset)));
7880

7981
EXPECT_OUTCOME_ERROR(
80-
EventsError::kNotFoundTipset,
82+
error,
8183
events_->chainAt(height_handler, revert_handler, confidence, height));
8284
}
8385

test/core/miner/tipset_cache_test.cpp

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,23 @@
99

1010
#include "testutil/outcome.hpp"
1111

12-
namespace fc::mining {
12+
#include "testutil/mocks/api.hpp"
1313

14+
namespace fc::mining {
1415
using primitives::block::BlockHeader;
1516

1617
class TipsetCacheTest : public testing::Test {
1718
protected:
1819
virtual void SetUp() {
1920
capability_ = 4;
20-
21-
auto getFunc = [&](ChainEpoch epoch)
22-
-> outcome::result<primitives::tipset::TipsetCPtr> {
23-
return mock_function_(epoch);
24-
};
25-
26-
tipset_cache_ = std::make_shared<TipsetCacheImpl>(capability_, getFunc);
21+
tipset_cache_ = std::make_shared<TipsetCacheImpl>(capability_, api_);
2722
}
2823

2924
uint64_t capability_;
30-
TipsetCacheImpl::GetTipsetFunction mock_function_;
3125
std::shared_ptr<TipsetCache> tipset_cache_;
26+
std::shared_ptr<FullNodeApi> api_ = std::make_shared<FullNodeApi>();
27+
MOCK_API(api_, ChainHead);
28+
MOCK_API(api_, ChainGetTipSetByHeight);
3229
};
3330

3431
/**
@@ -43,7 +40,7 @@ namespace fc::mining {
4340
Tipset tipset2(TipsetKey(), {block});
4441
EXPECT_OUTCOME_TRUE_1(tipset_cache_->add(tipset1));
4542
EXPECT_OUTCOME_TRUE_1(tipset_cache_->add(tipset2));
46-
EXPECT_EQ(tipset_cache_->best(), tipset2);
43+
EXPECT_OUTCOME_EQ(tipset_cache_->best(), tipset2);
4744
}
4845

4946
/**
@@ -84,7 +81,7 @@ namespace fc::mining {
8481
EXPECT_OUTCOME_TRUE_1(tipset_cache_->add(tipset1));
8582
EXPECT_OUTCOME_TRUE_1(tipset_cache_->add(tipset2));
8683
EXPECT_OUTCOME_TRUE_1(tipset_cache_->revert(tipset2));
87-
EXPECT_EQ(tipset_cache_->best(), tipset1);
84+
EXPECT_OUTCOME_EQ(tipset_cache_->best(), tipset1);
8885
}
8986

9087
/**
@@ -147,17 +144,9 @@ namespace fc::mining {
147144
auto tipset2 = std::make_shared<Tipset>(TipsetKey(),
148145
std::vector<BlockHeader>({block2}));
149146
EXPECT_OUTCOME_TRUE_1(tipset_cache_->add(tipset1));
150-
auto is_called = false;
151-
mock_function_ = [&](ChainEpoch height)
152-
-> outcome::result<primitives::tipset::TipsetCPtr> {
153-
if (height != 1) {
154-
return TipsetCacheError::kNotInCache;
155-
}
156-
is_called = true;
157-
return tipset2;
158-
};
147+
EXPECT_CALL(mock_ChainGetTipSetByHeight, Call(1, TipsetKey{}))
148+
.WillRepeatedly(testing::Return(tipset2));
159149
EXPECT_OUTCOME_EQ(tipset_cache_->get(1), *tipset2);
160-
ASSERT_TRUE(is_called);
161150
}
162151

163152
/**
@@ -170,17 +159,9 @@ namespace fc::mining {
170159
block1.height = 1;
171160
auto tipset1 = std::make_shared<Tipset>(TipsetKey(),
172161
std::vector<BlockHeader>({block1}));
173-
auto is_called = false;
174-
mock_function_ = [&](ChainEpoch height)
175-
-> outcome::result<primitives::tipset::TipsetCPtr> {
176-
if (height != 1) {
177-
return TipsetCacheError::kNotInCache;
178-
}
179-
is_called = true;
180-
return tipset1;
181-
};
162+
EXPECT_CALL(mock_ChainGetTipSetByHeight, Call(1, TipsetKey{}))
163+
.WillOnce(testing::Return(tipset1));
182164
EXPECT_OUTCOME_EQ(tipset_cache_->get(1), *tipset1);
183-
ASSERT_TRUE(is_called);
184165
}
185166

186167
/**
@@ -200,4 +181,18 @@ namespace fc::mining {
200181
EXPECT_OUTCOME_EQ(tipset_cache_->getNonNull(2), tipset2);
201182
}
202183

184+
/**
185+
* @given empty tipset_chache
186+
* @when try to get tipset from empty tipset_chace
187+
* @then success and best have returned Chain Head
188+
*/
189+
TEST_F(TipsetCacheTest, EmptyCache) {
190+
BlockHeader block1;
191+
block1.height = 1;
192+
auto tipset1 = std::make_shared<Tipset>(TipsetKey(),
193+
std::vector<BlockHeader>({block1}));
194+
EXPECT_CALL(mock_ChainHead, Call()).WillOnce(testing::Return(tipset1));
195+
EXPECT_OUTCOME_EQ(tipset_cache_->best(), *tipset1);
196+
}
197+
203198
} // namespace fc::mining

test/testutil/mocks/miner/tipset_cache_mock.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ namespace fc::mining {
2020

2121
MOCK_METHOD1(get, outcome::result<boost::optional<Tipset>>(ChainEpoch));
2222

23-
MOCK_CONST_METHOD0(best, boost::optional<Tipset>());
23+
MOCK_CONST_METHOD0(best, outcome::result<Tipset>());
2424
};
2525
} // namespace fc::mining

0 commit comments

Comments
 (0)