Skip to content

Commit 220c3c4

Browse files
authored
Refactor power actor (#154)
Signed-off-by: turuslan <[email protected]>
1 parent 25c5e3e commit 220c3c4

21 files changed

+302
-698
lines changed

core/adt/CMakeLists.txt

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,10 @@ target_link_libraries(map INTERFACE
2323
hamt
2424
)
2525

26-
add_library(balance_table_hamt
27-
impl/balance_table_hamt.cpp
28-
)
29-
target_link_libraries(balance_table_hamt
30-
address_key
31-
hamt
32-
)
33-
34-
add_library(multimap
35-
impl/multimap.cpp
36-
)
37-
target_link_libraries(multimap
38-
amt
39-
hamt
26+
add_library(multimap INTERFACE)
27+
target_link_libraries(multimap INTERFACE
28+
array
29+
map
4030
)
4131

4232
add_library(uvarint_key

core/adt/balance_table_hamt.hpp

Lines changed: 0 additions & 96 deletions
This file was deleted.

core/adt/impl/balance_table_hamt.cpp

Lines changed: 0 additions & 60 deletions
This file was deleted.

core/adt/impl/multimap.cpp

Lines changed: 0 additions & 49 deletions
This file was deleted.

core/adt/multimap.hpp

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,56 +6,40 @@
66
#ifndef CPP_FILECOIN_MULTIMAP_HPP
77
#define CPP_FILECOIN_MULTIMAP_HPP
88

9-
#include "storage/hamt/hamt.hpp"
9+
#include "adt/array.hpp"
10+
#include "adt/map.hpp"
1011

1112
namespace fc::adt {
12-
13-
/**
14-
* Container for storing multiple values per key.
15-
* Implemented over a HAMT of AMTs.
16-
* The order of values by the same key is preserved.
17-
*/
1813
struct Multimap {
19-
using Value = storage::ipfs::IpfsDatastore::Value;
20-
using Visitor = std::function<outcome::result<void>(const Value &)>;
21-
22-
explicit Multimap(
23-
const std::shared_ptr<storage::ipfs::IpfsDatastore> &store);
24-
Multimap(const std::shared_ptr<storage::ipfs::IpfsDatastore> &store,
25-
const CID &root);
26-
27-
/**
28-
* Apply changes to storage
29-
* @return root CID
30-
*/
31-
outcome::result<CID> flush();
32-
33-
/// Adds a value by key to the end of array. Does not change the store
34-
outcome::result<void> add(const std::string &key,
35-
gsl::span<const uint8_t> value);
36-
37-
/**
38-
* Adds a value which can be CBOR-encoded.
39-
* Does not change the store immediately
40-
* @tparam T value type that supports CBOR encoding
41-
* @param value to be marshalled
42-
* @return operation result
43-
*/
44-
template <typename T>
45-
outcome::result<void> addCbor(const std::string &key, const T &value) {
46-
OUTCOME_TRY(bytes, codec::cbor::encode(value));
47-
return add(key, bytes);
14+
template <typename Value, typename Keyer, size_t bit_width>
15+
static outcome::result<void> append(
16+
Map<Array<Value>, Keyer, bit_width> &map,
17+
const typename Keyer::Key &key,
18+
const Value &value) {
19+
OUTCOME_TRY(array, map.tryGet(key));
20+
if (!array) {
21+
array = Array<Value>{};
22+
}
23+
array->load(map.hamt.getIpld());
24+
OUTCOME_TRY(count, array->amt.count());
25+
OUTCOME_TRY(array->set(count, value));
26+
OUTCOME_TRY(array->flush());
27+
return map.set(key, *array);
4828
}
4929

50-
/// Removes array under the key
51-
outcome::result<void> removeAll(const std::string &key);
52-
53-
/// Iterates over stored values by key
54-
outcome::result<void> visit(const std::string &key, const Visitor &visitor);
55-
56-
private:
57-
std::shared_ptr<storage::ipfs::IpfsDatastore> store_;
58-
storage::hamt::Hamt hamt_;
30+
template <typename Value, typename Keyer, size_t bit_width>
31+
static outcome::result<void> visit(
32+
Map<Array<Value>, Keyer, bit_width> &map,
33+
const typename Keyer::Key &key,
34+
const std::function<outcome::result<void>(const Value &)> &visitor) {
35+
OUTCOME_TRY(array, map.tryGet(key));
36+
if (array) {
37+
array->load(map.hamt.getIpld());
38+
OUTCOME_TRY(
39+
array->visit([&](auto, auto &value) { return visitor(value); }));
40+
}
41+
return outcome::success();
42+
}
5943
};
6044
} // namespace fc::adt
6145

core/api/make.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ namespace fc::api {
1717
using vm::actor::kStorageMarketAddress;
1818
using vm::actor::kStoragePowerAddress;
1919
using vm::actor::builtin::miner::MinerActorState;
20-
using vm::actor::builtin::storage_power::StoragePowerActor;
2120
using vm::actor::builtin::storage_power::StoragePowerActorState;
2221
using vm::interpreter::InterpreterImpl;
2322
using vm::state::StateTreeImpl;
@@ -55,7 +54,7 @@ namespace fc::api {
5554
}
5655

5756
auto powerState() {
58-
return actorState<StoragePowerActorState, false>(kStoragePowerAddress);
57+
return actorState<StoragePowerActorState, true>(kStoragePowerAddress);
5958
}
6059
};
6160

@@ -240,13 +239,9 @@ namespace fc::api {
240239

241240
OUTCOME_TRY(power_state, context.powerState());
242241
StoragePower miner_power = 0;
243-
auto maybe_claim =
244-
StoragePowerActor{ipld, power_state}.getClaim(address);
245-
if (maybe_claim) {
246-
miner_power = maybe_claim.value().power;
247-
} else if (maybe_claim.error()
248-
!= VMExitCode::STORAGE_POWER_ILLEGAL_ARGUMENT) {
249-
return maybe_claim.error();
242+
OUTCOME_TRY(claim, power_state.claims.tryGet(address));
243+
if (claim) {
244+
miner_power = claim->power;
250245
}
251246

252247
OUTCOME_TRY(miner_state, context.minerState(address));

core/vm/actor/builtin/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@ add_subdirectory(miner)
1111
add_subdirectory(multisig)
1212
add_subdirectory(payment_channel)
1313
add_subdirectory(reward)
14-
add_subdirectory(shared)
1514
add_subdirectory(storage_power)

0 commit comments

Comments
 (0)