Skip to content

Commit 3730b42

Browse files
Feature/refactor chainepoch (#90)
Signed-off-by: Alexey-N-Chernyshov <[email protected]>
1 parent e89c7df commit 3730b42

File tree

19 files changed

+192
-101
lines changed

19 files changed

+192
-101
lines changed

core/clock/impl/chain_epoch_clock_impl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace fc::clock {
1616
outcome::result<ChainEpoch> ChainEpochClockImpl::epochAtTime(
1717
const Time &time) const {
1818
if (time < genesis_time_) {
19-
return EpochAtTimeError::BEFORE_GENESIS;
19+
return outcome::failure(EpochAtTimeError::BEFORE_GENESIS);
2020
}
2121
auto difference = time.unixTimeNano() - genesis_time_.unixTimeNano();
2222
return ChainEpoch(difference / kEpochDuration);

core/common/le_encoder.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@
1111

1212
namespace fc::common {
1313
/**
14-
* @brief little-endian -encodes an integral value
14+
* @brief little-endian-encodes an integral value
1515
* @tparam T integral value type
1616
* @param value integral value to encode
1717
* @param out output buffer
1818
*/
1919
template <class T,
2020
typename I = std::decay_t<T>,
2121
typename = std::enable_if_t<std::is_integral<I>::value>>
22-
void encodeInteger(T value,
23-
common::Buffer &out) { // no need to take integers by &&
22+
void encodeLebInteger(
23+
T value,
24+
common::Buffer &out) { // no need to take integers by &&
2425
constexpr size_t size = sizeof(T);
2526
constexpr size_t bits = size * 8;
2627
boost::endian::endian_buffer<boost::endian::order::little, T, bits> buf{};

core/crypto/randomness/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# SPDX-License-Identifier: Apache-2.0
33

44
add_library(randomness_provider
5-
impl/randomness_provider_imp.cpp
5+
impl/randomness_provider_impl.cpp
66
)
77
target_link_libraries(randomness_provider
88
Boost::boost

core/crypto/randomness/impl/randomness_provider_imp.cpp renamed to core/crypto/randomness/impl/randomness_provider_impl.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,36 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
#include "crypto/randomness/impl/randomness_provider_imp.hpp"
6+
#include "crypto/randomness/impl/randomness_provider_impl.hpp"
77

88
#include <libp2p/crypto/sha/sha256.hpp>
99
#include "common/le_encoder.hpp"
1010

1111
namespace fc::crypto::randomness {
1212

13+
int64_t kNoIndex = -1;
14+
1315
Randomness RandomnessProviderImpl::deriveRandomness(DomainSeparationTag tag,
1416
Serialization s) {
15-
return deriveRandomness(tag, std::move(s), ChainEpoch(-1));
17+
return deriveRandomness(tag, std::move(s), kNoIndex);
1618
}
1719

1820
Randomness RandomnessProviderImpl::deriveRandomness(DomainSeparationTag tag,
1921
Serialization s,
2022
const ChainEpoch &index) {
21-
return deriveRandomnessInternal(tag, std::move(s), index);
23+
return deriveRandomnessInternal(
24+
static_cast<uint64_t>(tag), std::move(s), index.convert_to<int64_t>());
2225
}
2326

24-
Randomness RandomnessProviderImpl::deriveRandomnessInternal(
25-
DomainSeparationTag tag, Serialization s, const ChainEpoch &index) {
27+
Randomness RandomnessProviderImpl::deriveRandomnessInternal(uint64_t tag,
28+
Serialization s,
29+
int64_t index) {
2630
common::Buffer value{};
2731
const size_t bytes_required =
2832
sizeof(DomainSeparationTag) + sizeof(ChainEpoch) + s.size();
2933
value.reserve(bytes_required);
30-
common::encodeInteger(static_cast<size_t>(tag), value);
31-
common::encodeInteger(static_cast<size_t>(index.toUInt64()), value);
34+
common::encodeLebInteger(tag, value);
35+
common::encodeLebInteger(index, value);
3236
value.put(s);
3337
auto hash = libp2p::crypto::sha256(value);
3438

core/crypto/randomness/impl/randomness_provider_imp.hpp renamed to core/crypto/randomness/impl/randomness_provider_impl.hpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,16 @@ namespace fc::crypto::randomness {
2626
size_t limit) override;
2727

2828
private:
29-
Randomness deriveRandomnessInternal(DomainSeparationTag tag,
29+
/**
30+
* Internal implementation of random function
31+
* @param tag - as uint64 - used for seed
32+
* @param s - used for seed
33+
* @param index - chain epoch as int64 - used for seed
34+
* @return Randomness
35+
*/
36+
Randomness deriveRandomnessInternal(uint64_t tag,
3037
Serialization s,
31-
const ChainEpoch &index);
38+
int64_t index);
3239
};
3340

3441
} // namespace fc::crypto::randomness

core/crypto/vrf/vrf_hash_encoder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ namespace fc::crypto::vrf {
2222
auto required_bytes = sizeof(uint64_t) + 2 * sizeof(uint8_t)
2323
+ params.message.size() + miner_bytes.size();
2424
out.reserve(required_bytes);
25-
common::encodeInteger(static_cast<uint64_t>(params.personalization_tag),
26-
out);
25+
common::encodeLebInteger(static_cast<uint64_t>(params.personalization_tag),
26+
out);
2727
out.putUint8(0u);
2828
out.putBuffer(params.message);
2929
out.putUint8(0u);

core/primitives/big_int.hpp

Lines changed: 81 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -13,85 +13,107 @@
1313
namespace fc::primitives {
1414
using boost::multiprecision::cpp_int;
1515

16-
struct BigInt : cpp_int {
16+
struct UBigInt : cpp_int,
17+
boost::totally_ordered<UBigInt, int> {
1718
using cpp_int::cpp_int;
18-
};
1919

20-
struct UBigInt : cpp_int {
21-
using cpp_int::cpp_int;
22-
};
20+
inline bool operator==(int other) const {
21+
return static_cast<cpp_int>(*this) == other;
22+
}
2323

24-
static inline BigInt operator*(const BigInt &lhs, const BigInt &rhs) {
25-
return static_cast<cpp_int>(lhs) * static_cast<cpp_int>(rhs);
26-
}
24+
inline bool operator==(const UBigInt &other) const {
25+
return static_cast<cpp_int>(*this) == static_cast<cpp_int>(other);
26+
}
2727

28-
static inline BigInt operator*(const unsigned long &lhs, const BigInt &rhs) {
29-
return static_cast<cpp_int>(lhs) * static_cast<cpp_int>(rhs);
30-
}
28+
inline bool operator<(int other) const {
29+
return static_cast<cpp_int>(*this) < other;
30+
}
3131

32-
static inline BigInt operator*(const BigInt &rhs, unsigned long long lhs) {
33-
return static_cast<cpp_int>(lhs) * static_cast<cpp_int>(rhs);
34-
}
32+
inline bool operator<(const UBigInt &other) const {
33+
return static_cast<cpp_int>(*this) < static_cast<cpp_int>(other);
34+
}
3535

36-
static inline BigInt operator/(const BigInt &lhs, const BigInt &rhs) {
37-
return static_cast<cpp_int>(lhs) / static_cast<cpp_int>(rhs);
38-
}
36+
inline UBigInt operator*(const UBigInt &other) const {
37+
return static_cast<cpp_int>(*this) * static_cast<cpp_int>(other);
38+
}
3939

40-
static inline BigInt operator/(const BigInt &lhs, unsigned long long rhs) {
41-
return static_cast<cpp_int>(lhs) / static_cast<cpp_int>(rhs);
42-
}
40+
inline UBigInt operator-(const UBigInt &other) const {
41+
return static_cast<cpp_int>(*this) - static_cast<cpp_int>(other);
42+
}
4343

44-
static inline BigInt operator+(const BigInt &lhs, const BigInt &rhs) {
45-
return static_cast<cpp_int>(lhs) + static_cast<cpp_int>(rhs);
46-
}
44+
inline UBigInt operator-(int other) const {
45+
return static_cast<cpp_int>(*this) - other;
46+
}
47+
};
4748

48-
static inline BigInt operator-(const BigInt &lhs, const BigInt &rhs) {
49-
return static_cast<cpp_int>(lhs) - static_cast<cpp_int>(rhs);
50-
}
49+
struct BigInt : cpp_int,
50+
boost::totally_ordered<BigInt>,
51+
boost::totally_ordered<BigInt, int>,
52+
boost::arithmetic<BigInt>,
53+
boost::multiplicative<BigInt, unsigned long>,
54+
boost::less_than_comparable<BigInt, UBigInt>,
55+
boost::multipliable<BigInt, UBigInt> {
56+
using cpp_int::cpp_int;
5157

52-
static inline bool operator>=(const BigInt &lhs, const BigInt &rhs) {
53-
return static_cast<cpp_int>(lhs) >= static_cast<cpp_int>(rhs);
54-
}
58+
inline bool operator==(int other) const {
59+
return static_cast<cpp_int>(*this) == other;
60+
}
5561

56-
static inline bool operator>(const BigInt &lhs, const BigInt &rhs) {
57-
return static_cast<cpp_int>(lhs) > static_cast<cpp_int>(rhs);
58-
}
62+
inline bool operator<(int other) const {
63+
return static_cast<cpp_int>(*this) < other;
64+
}
5965

60-
static inline bool operator<=(const BigInt &lhs, const BigInt &rhs) {
61-
return static_cast<cpp_int>(lhs) <= static_cast<cpp_int>(rhs);
62-
}
66+
inline bool operator==(const BigInt &other) const {
67+
return static_cast<cpp_int>(*this) == static_cast<cpp_int>(other);
68+
}
6369

64-
static inline bool operator<=(const BigInt &lhs, int rhs) {
65-
return static_cast<cpp_int>(lhs) <= rhs;
66-
}
70+
inline bool operator<(const BigInt &other) const {
71+
return static_cast<cpp_int>(*this) < static_cast<cpp_int>(other);
72+
}
6773

68-
static inline bool operator<(const BigInt &lhs, const BigInt &rhs) {
69-
return static_cast<cpp_int>(lhs) < static_cast<cpp_int>(rhs);
70-
}
74+
inline BigInt operator+=(const BigInt &other) {
75+
static_cast<cpp_int *>(this)->operator+=(static_cast<cpp_int>(other));
76+
return *this;
77+
}
7178

72-
static inline bool operator<(const BigInt &lhs, unsigned long long rhs) {
73-
return static_cast<cpp_int>(lhs) < rhs;
74-
}
79+
inline BigInt operator-=(const BigInt &other) {
80+
static_cast<cpp_int *>(this)->operator-=(static_cast<cpp_int>(other));
81+
return *this;
82+
}
7583

76-
static inline bool operator<(const BigInt &lhs, int rhs) {
77-
return static_cast<cpp_int>(lhs) < rhs;
78-
}
84+
inline BigInt operator*=(const BigInt &other) {
85+
static_cast<cpp_int *>(this)->operator*=(static_cast<cpp_int>(other));
86+
return *this;
87+
}
7988

80-
static inline bool operator==(const BigInt &lhs, const BigInt &rhs) {
81-
return static_cast<cpp_int>(lhs) == static_cast<cpp_int>(rhs);
82-
}
89+
inline BigInt operator/=(const BigInt &other) {
90+
static_cast<cpp_int *>(this)->operator/=(static_cast<cpp_int>(other));
91+
return *this;
92+
}
8393

84-
static inline bool operator==(const BigInt &lhs, int rhs) {
85-
return static_cast<cpp_int>(lhs) == rhs;
86-
}
94+
inline BigInt operator*=(const unsigned long &other) {
95+
static_cast<cpp_int *>(this)->operator*=(other);
96+
return *this;
97+
}
8798

88-
static inline bool operator==(const UBigInt &lhs, const UBigInt &rhs) {
89-
return static_cast<cpp_int>(lhs) == static_cast<cpp_int>(rhs);
90-
}
99+
inline BigInt operator/=(const unsigned long &other) {
100+
static_cast<cpp_int *>(this)->operator/=(other);
101+
return *this;
102+
}
91103

92-
static inline bool operator==(const UBigInt &lhs, int rhs) {
93-
return static_cast<cpp_int>(lhs) == static_cast<cpp_int>(rhs);
94-
}
104+
inline bool operator<(const UBigInt &other) const {
105+
return static_cast<cpp_int>(*this) < static_cast<cpp_int>(other);
106+
}
107+
108+
inline bool operator>(const UBigInt &other) const {
109+
return static_cast<cpp_int>(*this) > static_cast<cpp_int>(other);
110+
}
111+
112+
inline BigInt operator*=(const UBigInt &other) {
113+
static_cast<cpp_int *>(this)->operator*=(static_cast<cpp_int>(other));
114+
return *this;
115+
}
116+
};
95117

96118
template <class Stream,
97119
class T,

core/primitives/chain_epoch.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#ifndef CPP_FILECOIN_PRIMITIVES_CHAIN_EPOCH_HPP
77
#define CPP_FILECOIN_PRIMITIVES_CHAIN_EPOCH_HPP
88

9-
#include "libp2p/multi/uvarint.hpp"
109
#include "primitives/big_int.hpp"
1110

1211
namespace fc::primitives {
@@ -15,7 +14,7 @@ namespace fc::primitives {
1514
* @brief epoch index type represents a round of a blockchain protocol, which
1615
* acts as a proxy for time within the VM
1716
*/
18-
using ChainEpoch = libp2p::multi::UVarint;
17+
using ChainEpoch = UBigInt;
1918

2019
using EpochDuration = BigInt;
2120

core/primitives/ticket/epost_ticket_codec.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ namespace fc::primitives::ticket {
6565
typename = std::enable_if_t<
6666
std::remove_reference<Stream>::type::is_cbor_encoder_stream>>
6767
Stream &operator<<(Stream &&s, const EPostProof &epp) {
68-
return s << (s.list() << epp.proof << epp.post_rand
69-
<< epp.candidates);
68+
return s << (s.list() << epp.proof << epp.post_rand << epp.candidates);
7069
}
7170

7271
/**
@@ -80,7 +79,8 @@ namespace fc::primitives::ticket {
8079
typename = std::enable_if_t<
8180
std::remove_reference<Stream>::type::is_cbor_decoder_stream>>
8281
Stream &operator>>(Stream &&s, EPostProof &epp) {
83-
std::vector<uint8_t> proof, rand;
82+
std::vector<uint8_t> proof;
83+
std::vector<uint8_t> rand;
8484
s.list() >> proof >> rand >> epp.candidates;
8585
epp.proof = common::Buffer(std::move(proof));
8686
if (rand.size() != epp.post_rand.size()) {

core/primitives/ticket/ticket.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace fc::primitives::ticket {
1414
int64_t round) {
1515
common::Buffer buffer{};
1616
buffer.put(ticket.bytes);
17-
common::encodeInteger(round, buffer);
17+
common::encodeLebInteger(round, buffer);
1818
auto &&hash = libp2p::crypto::sha256(buffer);
1919

2020
return Randomness::fromSpan(hash);

0 commit comments

Comments
 (0)