Skip to content

Commit 9b9542a

Browse files
Feature/storage power actor cboring (#98)
Signed-off-by: Alexey-N-Chernyshov <[email protected]>
1 parent a1ff337 commit 9b9542a

30 files changed

+966
-415
lines changed

core/adt/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ target_link_libraries(array
1616
amt
1717
)
1818

19+
add_library(balance_table_hamt
20+
impl/balance_table_hamt.cpp
21+
)
22+
target_link_libraries(balance_table_hamt
23+
address
24+
hamt
25+
)
26+
1927
add_library(multimap
2028
impl/multimap.cpp
2129
)

core/adt/balance_table_hamt.hpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef CPP_FILECOIN_ADT_BALANCE_TABLE_HAMT_HPP
7+
#define CPP_FILECOIN_ADT_BALANCE_TABLE_HAMT_HPP
8+
9+
#include "common/outcome.hpp"
10+
#include "primitives/address/address.hpp"
11+
#include "primitives/big_int.hpp"
12+
#include "primitives/cid/cid.hpp"
13+
#include "storage/hamt/hamt.hpp"
14+
#include "storage/ipfs/datastore.hpp"
15+
16+
namespace fc::adt {
17+
18+
using primitives::BigInt;
19+
using primitives::address::Address;
20+
using storage::hamt::Hamt;
21+
using storage::ipfs::IpfsDatastore;
22+
using TokenAmount = BigInt;
23+
24+
/**
25+
* Stores miner balances
26+
*/
27+
class BalanceTableHamt {
28+
public:
29+
/**
30+
* Constructor
31+
* @param datastore - internal store
32+
* @param new_root - HAMT root CID
33+
*/
34+
explicit BalanceTableHamt(std::shared_ptr<IpfsDatastore> datastore,
35+
const CID &new_root);
36+
37+
/**
38+
* Get balance
39+
* @param key - address
40+
* @return balance or error
41+
*/
42+
outcome::result<TokenAmount> get(const Address &key) const;
43+
44+
/**
45+
* Checks if address is present in table
46+
* @param key address
47+
* @return true or false
48+
*/
49+
outcome::result<bool> has(const Address &key) const;
50+
51+
/**
52+
* Store balance
53+
* @param key address
54+
* @param balance token amount
55+
* @return error in case of failure
56+
*/
57+
outcome::result<void> set(const Address &key, const TokenAmount &balance);
58+
59+
/**
60+
* Add amount to balance
61+
* @param key address
62+
* @param amount to add
63+
* @return error in case of failure
64+
*/
65+
outcome::result<void> add(const Address &key, const TokenAmount &amount);
66+
67+
/**
68+
* Subtracts up to the specified amount from a balance, without reducing the
69+
* balance below some minimum
70+
* @param key address
71+
* @param amount to subtract
72+
* @param floor - minimum
73+
* @return the amount subtracted
74+
*/
75+
outcome::result<TokenAmount> subtractWithMinimum(const Address &key,
76+
const TokenAmount &amount,
77+
const TokenAmount &floor);
78+
79+
/**
80+
* Removes from table
81+
* @param key addrss
82+
* @return error in case of failure
83+
*/
84+
outcome::result<void> remove(const Address &key);
85+
86+
/**
87+
* HAMT CID of root
88+
*/
89+
CID root;
90+
91+
private:
92+
mutable Hamt hamt_;
93+
};
94+
95+
} // namespace fc::adt
96+
97+
#endif // CPP_FILECOIN_ADT_BALANCE_TABLE_HAMT_HPP

core/adt/impl/balance_table_hamt.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "adt/balance_table_hamt.hpp"
7+
#include "primitives/address/address_codec.hpp"
8+
9+
using fc::adt::BalanceTableHamt;
10+
using fc::adt::TokenAmount;
11+
using fc::primitives::address::encodeToByteString;
12+
13+
BalanceTableHamt::BalanceTableHamt(std::shared_ptr<IpfsDatastore> datastore,
14+
const CID &new_root)
15+
: root{new_root}, hamt_{datastore} {}
16+
17+
fc::outcome::result<TokenAmount> BalanceTableHamt::get(
18+
const Address &key) const {
19+
return hamt_.getCbor<TokenAmount>(encodeToByteString(key));
20+
}
21+
22+
fc::outcome::result<bool> BalanceTableHamt::has(const Address &key) const {
23+
return hamt_.contains(encodeToByteString(key));
24+
}
25+
26+
fc::outcome::result<void> BalanceTableHamt::set(const Address &key,
27+
const TokenAmount &balance) {
28+
OUTCOME_TRY(hamt_.setCbor(encodeToByteString(key), balance));
29+
OUTCOME_TRY(new_root, hamt_.flush());
30+
root = new_root;
31+
return fc::outcome::success();
32+
}
33+
34+
fc::outcome::result<void> BalanceTableHamt::add(const Address &key,
35+
const TokenAmount &amount) {
36+
OUTCOME_TRY(balance, hamt_.getCbor<TokenAmount>(encodeToByteString(key)));
37+
OUTCOME_TRY(hamt_.setCbor(encodeToByteString(key), balance + amount));
38+
OUTCOME_TRY(new_root, hamt_.flush());
39+
root = new_root;
40+
return fc::outcome::success();
41+
}
42+
43+
fc::outcome::result<TokenAmount> BalanceTableHamt::subtractWithMinimum(
44+
const Address &key, const TokenAmount &amount, const TokenAmount &floor) {
45+
OUTCOME_TRY(balance, hamt_.getCbor<TokenAmount>(encodeToByteString(key)));
46+
TokenAmount available = balance < floor ? TokenAmount{0} : balance - floor;
47+
TokenAmount sub = std::min(available, amount);
48+
OUTCOME_TRY(hamt_.setCbor(encodeToByteString(key), balance - sub));
49+
OUTCOME_TRY(new_root, hamt_.flush());
50+
root = new_root;
51+
return sub;
52+
}
53+
54+
fc::outcome::result<void> BalanceTableHamt::remove(const Address &key) {
55+
OUTCOME_TRY(hamt_.remove(encodeToByteString(key)));
56+
OUTCOME_TRY(new_root, hamt_.flush());
57+
root = new_root;
58+
return fc::outcome::success();
59+
}

core/clock/chain_epoch_clock.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#define CPP_FILECOIN_CORE_CLOCK_CHAIN_EPOCH_CLOCK_HPP
88

99
#include "clock/time.hpp"
10-
#include "primitives/chain_epoch.hpp"
10+
#include "primitives/chain_epoch/chain_epoch.hpp"
1111

1212
namespace fc::clock {
1313
enum class EpochAtTimeError { BEFORE_GENESIS = 1 };

core/crypto/randomness/randomness_provider.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#define CPP_FILECOIN_CORE_CRYPTO_RANDOMNESS_RANDOMNESS_PROVIDER_HPP
88

99
#include "crypto/randomness/randomness_types.hpp"
10-
#include "primitives/chain_epoch.hpp"
10+
#include "primitives/chain_epoch/chain_epoch.hpp"
1111

1212
namespace fc::crypto::randomness {
1313

core/power/impl/power_table_impl.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "power/power_table.hpp"
1111

1212
namespace fc::power {
13+
1314
class PowerTableImpl : public PowerTable {
1415
public:
1516
outcome::result<Power> getMinerPower(
@@ -32,5 +33,6 @@ namespace fc::power {
3233
private:
3334
std::unordered_map<std::string, Power> power_table_;
3435
};
36+
3537
} // namespace fc::power
3638
#endif // FILECOIN_CORE_STORAGE_POWER_TABLE_IMPL_HPP

core/primitives/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
add_subdirectory(address)
77
add_subdirectory(block)
8+
add_subdirectory(chain_epoch)
89
add_subdirectory(cid)
910
add_subdirectory(ticket)
1011
add_subdirectory(tipset)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright Soramitsu Co., Ltd. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
add_library(chain_epoch_codec
5+
chain_epoch_codec.cpp
6+
)
7+
target_link_libraries(chain_epoch_codec
8+
p2p::p2p_uvarint
9+
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "primitives/chain_epoch/chain_epoch_codec.hpp"
7+
8+
#include <libp2p/multi/uvarint.hpp>
9+
10+
namespace fc::primitives::chain_epoch {
11+
12+
using libp2p::multi::UVarint;
13+
14+
std::string encodeToByteString(const ChainEpoch &epoch) {
15+
// TODO (a.chernyshov) actor-specs uses Protobuf Varint encoding
16+
UVarint number(epoch.convert_to<int64_t>());
17+
auto encoded = number.toBytes();
18+
return std::string(encoded.begin(), encoded.end());
19+
}
20+
21+
} // namespace fc::primitives::chain_epoch

0 commit comments

Comments
 (0)