Skip to content

Commit a1ff337

Browse files
authored
Remove UBigInt (#101)
Signed-off-by: turuslan <[email protected]>
1 parent a844b45 commit a1ff337

File tree

9 files changed

+45
-171
lines changed

9 files changed

+45
-171
lines changed

core/power/power_table.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
namespace fc::power {
1414

1515
using primitives::address::Address;
16-
using Power = primitives::UBigInt;
16+
using Power = primitives::BigInt;
1717

1818
/**
1919
* @interface Provides an interface to the power table

core/primitives/big_int.hpp

Lines changed: 10 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -8,168 +8,35 @@
88

99
#include <boost/multiprecision/cpp_int.hpp>
1010

11-
#include "codec/cbor/cbor.hpp"
11+
#include "codec/cbor/streams_annotation.hpp"
1212

1313
namespace fc::primitives {
14-
using boost::multiprecision::cpp_int;
15-
16-
struct UBigInt : cpp_int,
17-
boost::totally_ordered<UBigInt>,
18-
boost::arithmetic<UBigInt>,
19-
boost::totally_ordered<UBigInt, int> {
20-
using cpp_int::cpp_int;
21-
22-
inline bool operator==(int other) const {
23-
return static_cast<cpp_int>(*this) == other;
24-
}
25-
26-
inline bool operator==(const UBigInt &other) const {
27-
return static_cast<cpp_int>(*this) == static_cast<cpp_int>(other);
28-
}
29-
30-
inline bool operator<(int other) const {
31-
return static_cast<cpp_int>(*this) < other;
32-
}
33-
34-
inline bool operator<(const UBigInt &other) const {
35-
return static_cast<cpp_int>(*this) < static_cast<cpp_int>(other);
36-
}
37-
38-
inline UBigInt operator+=(const UBigInt &other) {
39-
*static_cast<cpp_int *>(this) += static_cast<cpp_int>(other);
40-
return *this;
41-
}
42-
43-
inline UBigInt operator-=(const UBigInt &other) {
44-
*static_cast<cpp_int *>(this) -= static_cast<cpp_int>(other);
45-
return *this;
46-
}
47-
48-
inline UBigInt operator*=(const UBigInt &other) {
49-
*static_cast<cpp_int *>(this) *= static_cast<cpp_int>(other);
50-
return *this;
51-
}
52-
53-
inline UBigInt operator/=(const UBigInt &other) {
54-
*static_cast<cpp_int *>(this) /= static_cast<cpp_int>(other);
55-
return *this;
56-
}
57-
58-
template <typename Integral>
59-
inline UBigInt operator+(Integral other) const {
60-
return static_cast<cpp_int>(*this) + static_cast<cpp_int>(other);
61-
}
62-
63-
inline UBigInt operator-(int other) const {
64-
return static_cast<cpp_int>(*this) - other;
65-
}
66-
};
67-
68-
struct BigInt : cpp_int,
69-
boost::totally_ordered<BigInt>,
70-
boost::totally_ordered<BigInt, int>,
71-
boost::arithmetic<BigInt>,
72-
boost::multiplicative<BigInt, unsigned long>,
73-
boost::less_than_comparable<BigInt, UBigInt>,
74-
boost::multipliable<BigInt, UBigInt> {
75-
using cpp_int::cpp_int;
76-
77-
inline bool operator==(int other) const {
78-
return static_cast<cpp_int>(*this) == other;
79-
}
80-
81-
inline bool operator<(int other) const {
82-
return static_cast<cpp_int>(*this) < other;
83-
}
84-
85-
inline bool operator==(const BigInt &other) const {
86-
return static_cast<cpp_int>(*this) == static_cast<cpp_int>(other);
87-
}
88-
89-
inline bool operator<(const BigInt &other) const {
90-
return static_cast<cpp_int>(*this) < static_cast<cpp_int>(other);
91-
}
92-
93-
inline BigInt operator+=(const BigInt &other) {
94-
*static_cast<cpp_int *>(this) += static_cast<cpp_int>(other);
95-
return *this;
96-
}
97-
98-
inline BigInt operator-=(const BigInt &other) {
99-
*static_cast<cpp_int *>(this) -= static_cast<cpp_int>(other);
100-
return *this;
101-
}
102-
103-
inline BigInt operator*=(const BigInt &other) {
104-
*static_cast<cpp_int *>(this) *= static_cast<cpp_int>(other);
105-
return *this;
106-
}
107-
108-
inline BigInt operator/=(const BigInt &other) {
109-
*static_cast<cpp_int *>(this) /= static_cast<cpp_int>(other);
110-
return *this;
111-
}
112-
113-
inline BigInt operator*=(const unsigned long &other) {
114-
*static_cast<cpp_int *>(this) *= other;
115-
return *this;
116-
}
117-
118-
inline BigInt operator/=(const unsigned long &other) {
119-
*static_cast<cpp_int *>(this) /= other;
120-
return *this;
121-
}
122-
123-
inline bool operator<(const UBigInt &other) const {
124-
return static_cast<cpp_int>(*this) < static_cast<cpp_int>(other);
125-
}
126-
127-
inline bool operator>(const UBigInt &other) const {
128-
return static_cast<cpp_int>(*this) > static_cast<cpp_int>(other);
129-
}
130-
131-
inline BigInt operator*=(const UBigInt &other) {
132-
*static_cast<cpp_int *>(this) *= static_cast<cpp_int>(other);
133-
return *this;
134-
}
135-
};
14+
using BigInt = boost::multiprecision::cpp_int;
15+
} // namespace fc::primitives
13616

137-
template <class Stream,
138-
class T,
139-
typename = std::enable_if_t<
140-
(std::is_same_v<T, BigInt> || std::is_same_v<T, UBigInt>)&&std::
141-
remove_reference<Stream>::type::is_cbor_encoder_stream>>
142-
Stream &operator<<(Stream &&s, const T &big_int) {
17+
namespace boost::multiprecision {
18+
CBOR_ENCODE(cpp_int, big_int) {
14319
std::vector<uint8_t> bytes;
14420
if (big_int != 0) {
145-
if (std::is_same_v<T, BigInt>) {
146-
bytes.push_back(big_int < 0 ? 1 : 0);
147-
}
21+
bytes.push_back(big_int < 0 ? 1 : 0);
14822
export_bits(big_int, std::back_inserter(bytes), 8);
14923
}
15024
return s << bytes;
15125
}
15226

153-
template <class Stream,
154-
class T,
155-
typename = std::enable_if_t<
156-
(std::is_same_v<T, BigInt> || std::is_same_v<T, UBigInt>)&&std::
157-
remove_reference_t<Stream>::is_cbor_decoder_stream>>
158-
Stream &operator>>(Stream &&s, T &big_int) {
27+
CBOR_DECODE(cpp_int, big_int) {
15928
std::vector<uint8_t> bytes;
16029
s >> bytes;
16130
if (bytes.empty()) {
16231
big_int = 0;
16332
} else {
164-
import_bits(big_int,
165-
bytes.begin() + (std::is_same_v<T, BigInt> ? 1 : 0),
166-
bytes.end());
167-
if (std::is_same_v<T, BigInt> && bytes[0] == 1) {
33+
import_bits(big_int, bytes.begin() + 1, bytes.end());
34+
if (bytes[0] == 1) {
16835
big_int = -big_int;
16936
}
17037
}
17138
return s;
17239
}
173-
} // namespace fc::primitives
40+
} // namespace boost::multiprecision
17441

17542
#endif // CPP_FILECOIN_CORE_PRIMITIVES_BIG_INT_HPP

core/primitives/chain_epoch.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace fc::primitives {
1414
* @brief epoch index type represents a round of a blockchain protocol, which
1515
* acts as a proxy for time within the VM
1616
*/
17-
using ChainEpoch = UBigInt;
17+
using ChainEpoch = BigInt;
1818

1919
using EpochDuration = BigInt;
2020

core/storage/hamt/hamt.hpp

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,46 @@
99
#include <string>
1010
#include <vector>
1111

12+
#include <boost/multiprecision/cpp_int.hpp>
1213
#include <boost/variant.hpp>
1314

1415
#include "codec/cbor/cbor.hpp"
16+
#include "codec/cbor/streams_annotation.hpp"
1517
#include "common/outcome_throw.hpp"
1618
#include "common/visitor.hpp"
17-
#include "primitives/big_int.hpp"
1819
#include "primitives/cid/cid.hpp"
1920
#include "storage/ipfs/datastore.hpp"
2021

2122
namespace fc::storage::hamt {
2223
enum class HamtError { EXPECTED_CID = 1, NOT_FOUND, MAX_DEPTH };
2324

24-
using fc::primitives::UBigInt;
25+
using boost::multiprecision::cpp_int;
2526
using Value = ipfs::IpfsDatastore::Value;
2627

2728
constexpr size_t kLeafMax = 3;
2829
constexpr size_t kDefaultBitWidth = 8;
2930

31+
struct Bits : cpp_int {};
32+
33+
CBOR_ENCODE(Bits, bits) {
34+
std::vector<uint8_t> bytes;
35+
if (bits != 0) {
36+
export_bits(bits, std::back_inserter(bytes), 8);
37+
}
38+
return s << bytes;
39+
}
40+
41+
CBOR_DECODE(Bits, bits) {
42+
std::vector<uint8_t> bytes;
43+
s >> bytes;
44+
if (bytes.empty()) {
45+
bits = {0};
46+
} else {
47+
import_bits(bits, bytes.begin(), bytes.end());
48+
}
49+
return s;
50+
}
51+
3052
/** Hamt node representation */
3153
struct Node {
3254
using Ptr = std::shared_ptr<Node>;
@@ -40,7 +62,7 @@ namespace fc::storage::hamt {
4062
typename = std::enable_if_t<Stream::is_cbor_encoder_stream>>
4163
Stream &operator<<(Stream &s, const Node &node) {
4264
auto l_items = s.list();
43-
UBigInt bits;
65+
Bits bits;
4466
for (auto &item : node.items) {
4567
bit_set(bits, item.first);
4668
auto m_item = s.map();
@@ -67,7 +89,7 @@ namespace fc::storage::hamt {
6789
Stream &operator>>(Stream &s, Node &node) {
6890
node.items.clear();
6991
auto l_node = s.list();
70-
UBigInt bits;
92+
Bits bits;
7193
l_node >> bits;
7294
auto n_items = l_node.listLength();
7395
auto l_items = l_node.list();

core/vm/actor/builtin/payment_channel/payment_channel_actor_state.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ namespace fc::vm::actor::builtin::payment_channel {
2020
using fc::vm::actor::MethodNumber;
2121
using primitives::BigInt;
2222
using primitives::ChainEpoch;
23-
using primitives::UBigInt;
2423
using primitives::address::Address;
2524

2625
struct LaneState {
@@ -37,7 +36,7 @@ namespace fc::vm::actor::builtin::payment_channel {
3736
struct PaymentChannelActorState {
3837
Address from;
3938
Address to;
40-
UBigInt to_send{};
39+
BigInt to_send{};
4140
ChainEpoch settling_at;
4241
ChainEpoch min_settling_height;
4342
std::vector<LaneState> lanes{};
@@ -63,7 +62,7 @@ namespace fc::vm::actor::builtin::payment_channel {
6362
ModularVerificationParameter extra;
6463
uint64_t lane{};
6564
uint64_t nonce{};
66-
UBigInt amount;
65+
BigInt amount;
6766
uint64_t min_close_height{};
6867
std::vector<Merge> merges{};
6968
Signature signature;

core/vm/actor/builtin/reward/reward_actor.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ namespace fc::vm::actor::builtin::reward {
1818
ActorMethod(RewardActor::awardBlockReward)},
1919
{kWithdrawRewardMethodNumber, ActorMethod(RewardActor::withdrawReward)}};
2020

21-
auto asBig(const primitives::UBigInt &u) {
22-
return u.convert_to<primitives::BigInt>();
23-
}
24-
2521
primitives::BigInt Reward::amountVested(
2622
const primitives::ChainEpoch &current_epoch) {
2723
auto elapsed = current_epoch - start_epoch;
@@ -30,10 +26,10 @@ namespace fc::vm::actor::builtin::reward {
3026
return value;
3127
case VestingFunction::LINEAR: {
3228
auto vest_duration{end_epoch - start_epoch};
33-
if (asBig(elapsed) >= asBig(vest_duration)) {
29+
if (elapsed >= vest_duration) {
3430
return value;
3531
}
36-
return (value * elapsed) / asBig(vest_duration);
32+
return (value * elapsed) / vest_duration;
3733
}
3834
default:
3935
return 0;
@@ -131,7 +127,7 @@ namespace fc::vm::actor::builtin::reward {
131127
OUTCOME_TRY(state, runtime.getIpfsDatastore()->getCbor<State>(state_cid));
132128

133129
auto block_reward = computeBlockReward(state, reward_params.gas_reward);
134-
auto total_reward = block_reward + reward_params.gas_reward;
130+
TokenAmount total_reward = block_reward + reward_params.gas_reward;
135131

136132
penalty = std::min(reward_params.penalty, total_reward);
137133
auto reward_payable = total_reward - penalty;
@@ -177,7 +173,7 @@ namespace fc::vm::actor::builtin::reward {
177173

178174
TokenAmount RewardActor::computeBlockReward(const State &state,
179175
const TokenAmount &balance) {
180-
auto treasury = balance - state.reward_total;
176+
TokenAmount treasury = balance - state.reward_total;
181177
auto target_reward = kBlockRewardTarget;
182178
return std::min(target_reward, treasury);
183179
}

test/core/codec/cbor/cbor_test.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,6 @@ TEST(Cbor, BigInt) {
5757
EXPECT_OUTCOME_EQ(decode<BigInt>("40"_unhex), 0);
5858
}
5959

60-
/** UBigInt CBOR encoding and decoding */
61-
TEST(Cbor, UBigInt) {
62-
using fc::primitives::UBigInt;
63-
EXPECT_OUTCOME_EQ(encode(UBigInt(0xCAFE)), "42CAFE"_unhex);
64-
EXPECT_OUTCOME_EQ(decode<UBigInt>("42CAFE"_unhex), 0xCAFE);
65-
EXPECT_OUTCOME_EQ(encode(UBigInt(0)), "40"_unhex);
66-
EXPECT_OUTCOME_EQ(decode<UBigInt>("40"_unhex), 0);
67-
}
68-
6960
/** Null CBOR encoding and decoding */
7061
TEST(Cbor, Null) {
7162
EXPECT_OUTCOME_EQ(encode(nullptr), "F6"_unhex);

test/core/primitives/big_int_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,5 @@ TEST(BigInt, Divide) {
4747
TEST(BigInt, DivideByZero) {
4848
BigInt a{8};
4949
BigInt b{0};
50-
ASSERT_THROW(a / b, std::exception);
50+
ASSERT_THROW(BigInt{a / b}, std::exception);
5151
}

test/core/primitives/tipset/tipset_key_test.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ TEST_F(TipsetKeyTest, DISABLED_ToPrettyStringSuccess) {
5050
* @then the representation meets lotus-calculated value
5151
*/
5252
TEST_F(TipsetKeyTest, ToBytesSuccess) {
53-
EXPECT_OUTCOME_TRUE(vec1, key1.toBytes());
5453
EXPECT_OUTCOME_TRUE(vec2, key2.toBytes());
5554
EXPECT_OUTCOME_TRUE(vec3, key3.toBytes());
5655
EXPECT_OUTCOME_TRUE(vec4, key4.toBytes());

0 commit comments

Comments
 (0)