Skip to content

Commit 38aa0db

Browse files
authored
[FIL-97] Power table and part of Storage Power Actor (#73)
Signed-off-by: artyom-yurin <[email protected]>
1 parent 5d78d0c commit 38aa0db

27 files changed

+1126
-6
lines changed

core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ add_subdirectory(crypto)
1010
add_subdirectory(primitives)
1111
add_subdirectory(storage)
1212
add_subdirectory(fslock)
13+
add_subdirectory(power)
1314
add_subdirectory(vm)

core/crypto/randomness/impl/randomness_provider.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,11 @@ namespace fc::crypto::randomness {
3535
return Randomness(hash);
3636
}
3737

38+
int RandomnessProviderImpl::randomInt(const Randomness &randomness,
39+
size_t nonce,
40+
size_t limit) {
41+
// TODO(artyom-yurin): [FIL-141] Implement it
42+
throw std::string("Not implemented");
43+
}
44+
3845
} // namespace fc::crypto::randomness

core/crypto/randomness/impl/randomness_provider.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ namespace fc::crypto::randomness {
2121
Serialization s,
2222
ChainEpoch index) override;
2323

24+
int randomInt(const Randomness &randomness,
25+
size_t nonce,
26+
size_t limit) override;
27+
2428
private:
2529
Randomness deriveRandomnessInternal(DomainSeparationTag tag,
2630
Serialization s,

core/crypto/randomness/randomness_provider.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ namespace fc::crypto::randomness {
3535
virtual Randomness deriveRandomness(DomainSeparationTag tag,
3636
Serialization s,
3737
ChainEpoch index) = 0;
38+
39+
/**
40+
* @brief get random int value
41+
*/
42+
virtual int randomInt(const Randomness &randomness,
43+
size_t nonce,
44+
size_t limit) = 0;
3845
};
3946
} // namespace fc::crypto::randomness
4047

core/power/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright Soramitsu Co., Ltd. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
add_library(power_table
5+
impl/power_table_impl.cpp
6+
impl/power_table_error.cpp
7+
)
8+
target_link_libraries(power_table
9+
outcome
10+
address
11+
)

core/power/impl/power_table_error.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "power/power_table_error.hpp"
7+
8+
OUTCOME_CPP_DEFINE_CATEGORY(fc::power, PowerTableError, e) {
9+
using fc::power::PowerTableError;
10+
11+
switch (e) {
12+
case PowerTableError::NO_SUCH_MINER:
13+
return "PowerTableError: miner not found";
14+
case PowerTableError::NEGATIVE_POWER:
15+
return "PowerTableError: power cannot be negative";
16+
}
17+
18+
return "unknown error";
19+
}

core/power/impl/power_table_impl.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "power/impl/power_table_impl.hpp"
7+
8+
#include "power/power_table_error.hpp"
9+
#include "primitives/address/address_codec.hpp"
10+
11+
namespace fc::power {
12+
13+
outcome::result<Power> PowerTableImpl::getMinerPower(
14+
const primitives::address::Address &address) const {
15+
auto result =
16+
power_table_.find(primitives::address::encodeToString(address));
17+
if (result == power_table_.end()) {
18+
return outcome::failure(PowerTableError::NO_SUCH_MINER);
19+
}
20+
21+
return result->second;
22+
}
23+
24+
outcome::result<void> PowerTableImpl::setMinerPower(
25+
const primitives::address::Address &address, Power power_amount) {
26+
if (power_amount < 0) return PowerTableError::NEGATIVE_POWER;
27+
power_table_[primitives::address::encodeToString(address)] = power_amount;
28+
return outcome::success();
29+
}
30+
31+
outcome::result<void> PowerTableImpl::removeMiner(
32+
const primitives::address::Address &address) {
33+
if (power_table_.erase(primitives::address::encodeToString(address)) == 0)
34+
return PowerTableError::NO_SUCH_MINER;
35+
36+
return outcome::success();
37+
}
38+
39+
size_t PowerTableImpl::getSize() const {
40+
return power_table_.size();
41+
}
42+
43+
Power PowerTableImpl::getMaxPower() const {
44+
if (power_table_.empty()) return 0;
45+
46+
auto res = std::max(power_table_.cbegin(),
47+
power_table_.cend(),
48+
[&](const auto &rhs, const auto &lhs) {
49+
return rhs->second < lhs->second;
50+
});
51+
52+
return res->second;
53+
}
54+
55+
outcome::result<std::vector<primitives::address::Address>>
56+
PowerTableImpl::getMiners() const {
57+
std::vector<primitives::address::Address> result = {};
58+
for (auto &elem : power_table_) {
59+
OUTCOME_TRY(miner_addr,
60+
primitives::address::decodeFromString(elem.first));
61+
result.push_back(miner_addr);
62+
}
63+
return result;
64+
}
65+
} // namespace fc::power

core/power/impl/power_table_impl.hpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef FILECOIN_CORE_STORAGE_POWER_TABLE_IMPL_HPP
7+
#define FILECOIN_CORE_STORAGE_POWER_TABLE_IMPL_HPP
8+
9+
#include <unordered_map>
10+
#include "power/power_table.hpp"
11+
12+
namespace fc::power {
13+
class PowerTableImpl : public PowerTable {
14+
public:
15+
outcome::result<Power> getMinerPower(
16+
const primitives::address::Address &address) const override;
17+
18+
outcome::result<void> setMinerPower(
19+
const primitives::address::Address &address,
20+
Power power_amount) override;
21+
22+
outcome::result<void> removeMiner(
23+
const primitives::address::Address &address) override;
24+
25+
size_t getSize() const override;
26+
27+
Power getMaxPower() const override;
28+
29+
outcome::result<std::vector<primitives::address::Address>> getMiners()
30+
const override;
31+
32+
private:
33+
std::unordered_map<std::string, Power> power_table_;
34+
};
35+
} // namespace fc::power
36+
#endif // FILECOIN_CORE_STORAGE_POWER_TABLE_IMPL_HPP

core/power/power_table.hpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef FILECOIN_CORE_POWER_TABLE_HPP
7+
#define FILECOIN_CORE_POWER_TABLE_HPP
8+
9+
#include "common/outcome.hpp"
10+
#include "primitives/address/address.hpp"
11+
#include "primitives/big_int.hpp"
12+
13+
namespace fc::power {
14+
15+
using Power = primitives::BigInt;
16+
17+
/**
18+
* @interface Provides an interface to the power table
19+
*/
20+
class PowerTable {
21+
public:
22+
virtual ~PowerTable() = default;
23+
24+
/**
25+
* @brief Get the power of a particular miner
26+
* @param address of the miner
27+
* @return power of the miner or No such miner error
28+
*/
29+
virtual outcome::result<Power> getMinerPower(
30+
const primitives::address::Address &address) const = 0;
31+
32+
/**
33+
* @brief Set the power of a particular miner
34+
* @param address of the miner
35+
* @param power_amount - amount of power
36+
* @return success or Negative power error if amount less than 0
37+
*/
38+
virtual outcome::result<void> setMinerPower(
39+
const primitives::address::Address &address, Power power_amount) = 0;
40+
41+
/**
42+
* @brief Remove a miner from power table
43+
* @param address of the miner
44+
* @return success or No such miner error
45+
*/
46+
virtual outcome::result<void> removeMiner(
47+
const primitives::address::Address &address) = 0;
48+
49+
/**
50+
* @brief Get size of table
51+
* @return number of miners
52+
*/
53+
virtual size_t getSize() const = 0;
54+
55+
/**
56+
* @brief Get Max power from table
57+
* @return max power from all miners
58+
*/
59+
virtual Power getMaxPower() const = 0;
60+
61+
/**
62+
* @brief Get list of all miners from table
63+
* @return list of miners
64+
*/
65+
virtual outcome::result<std::vector<primitives::address::Address>>
66+
getMiners() const = 0;
67+
};
68+
} // namespace fc::power
69+
#endif // FILECOIN_CORE_POWER_TABLE_HPP

core/power/power_table_error.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef FILECOIN_CORE_POWER_POWER_TABLE_ERROR_HPP
7+
#define FILECOIN_CORE_POWER_POWER_TABLE_ERROR_HPP
8+
9+
#include "common/outcome.hpp"
10+
11+
namespace fc::power {
12+
13+
/**
14+
* @brief Type of errors returned by Power Table
15+
*/
16+
enum class PowerTableError { NO_SUCH_MINER = 1, NEGATIVE_POWER };
17+
18+
} // namespace fc::power
19+
20+
OUTCOME_HPP_DECLARE_ERROR(fc::power, PowerTableError);
21+
22+
#endif // FILECOIN_CORE_POWER_POWER_TABLE_ERROR_HPP

0 commit comments

Comments
 (0)