Skip to content

Commit 186399a

Browse files
authored
Refactor CID (#67)
Signed-off-by: turuslan <[email protected]>
1 parent c4e4ed7 commit 186399a

25 files changed

+104
-100
lines changed

core/codec/cbor/cbor_common.hpp

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

1313
#include "codec/cbor/cbor_errors.hpp"
1414
#include "common/outcome_throw.hpp"
15+
#include "primitives/cid/cid.hpp"
1516

1617
namespace fc::codec::cbor {
1718
constexpr uint64_t kCidTag = 42;

core/codec/cbor/cbor_decode_stream.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ namespace fc::codec::cbor {
5656
}
5757

5858
CborDecodeStream &CborDecodeStream::operator>>(
59-
libp2p::multi::ContentIdentifier &cid) {
59+
CID &cid) {
6060
if (!cbor_value_is_tag(&value_)) {
6161
outcome::raise(CborDecodeError::INVALID_CBOR_CID);
6262
}

core/codec/cbor/cbor_decode_stream.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ namespace fc::codec::cbor {
6464
/** Decodes string */
6565
CborDecodeStream &operator>>(std::string &str);
6666
/** Decodes CID */
67-
CborDecodeStream &operator>>(libp2p::multi::ContentIdentifier &cid);
67+
CborDecodeStream &operator>>(CID &cid);
6868
/** Creates list container decode substream */
6969
CborDecodeStream list();
7070
/** Skips current element */

core/codec/cbor/cbor_encode_stream.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ namespace fc::codec::cbor {
3535
return *this;
3636
}
3737

38-
CborEncodeStream &CborEncodeStream::operator<<(
39-
const libp2p::multi::ContentIdentifier &cid) {
38+
CborEncodeStream &CborEncodeStream::operator<<(const CID &cid) {
4039
auto maybe_cid_bytes = libp2p::multi::ContentIdentifierCodec::encode(cid);
4140
if (maybe_cid_bytes.has_error()) {
4241
outcome::raise(CborEncodeError::INVALID_CID);

core/codec/cbor/cbor_encode_stream.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace fc::codec::cbor {
4545
/** Encodes string */
4646
CborEncodeStream &operator<<(const std::string &str);
4747
/** Encodes CID */
48-
CborEncodeStream &operator<<(const libp2p::multi::ContentIdentifier &cid);
48+
CborEncodeStream &operator<<(const CID &cid);
4949
/** Encodes list container encode substream */
5050
CborEncodeStream &operator<<(const CborEncodeStream &other);
5151
/** Encodes map container encode substream map */

core/common/CMakeLists.txt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,3 @@ add_library(logger
4141
target_link_libraries(logger
4242
spdlog::spdlog
4343
)
44-
45-
add_library(cid
46-
cid.cpp
47-
)
48-
target_link_libraries(cid
49-
blake2
50-
p2p::p2p_cid
51-
)

core/primitives/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
#
55

66
add_subdirectory(address)
7+
add_subdirectory(cid)
78
add_subdirectory(ticket)

core/primitives/cid/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#
2+
# Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
6+
add_library(cid
7+
cid.cpp
8+
)
9+
target_link_libraries(cid
10+
blake2
11+
p2p::p2p_cid
12+
)

core/common/cid.cpp renamed to core/primitives/cid/cid.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
#include "common/cid.hpp"
6+
#include "primitives/cid/cid.hpp"
77

88
#include "crypto/blake2/blake2b160.hpp"
99

10+
namespace fc {
11+
CID::CID()
12+
: ContentIdentifier(
13+
{}, {}, libp2p::multi::Multihash::create({}, {}).value()) {}
14+
15+
CID::CID(const ContentIdentifier &cid) : ContentIdentifier(cid) {}
16+
} // namespace fc
17+
1018
namespace fc::common {
1119
outcome::result<CID> getCidOf(gsl::span<const uint8_t> bytes) {
1220
OUTCOME_TRY(hash_raw, crypto::blake2b::blake2b_256(bytes));

core/common/cid.hpp renamed to core/primitives/cid/cid.hpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,22 @@
1111
#include "common/outcome.hpp"
1212

1313
namespace fc {
14-
using CID = libp2p::multi::ContentIdentifier;
14+
class CID : public libp2p::multi::ContentIdentifier {
15+
public:
16+
using ContentIdentifier::ContentIdentifier;
17+
18+
/**
19+
* ContentIdentifier is not default-constructible, but in some cases we need
20+
* default value. This value can be used to initialize class member or local
21+
* variable. Trying to CBOR encode this value will yield error, to ensure
22+
* proper initialization.
23+
*/
24+
CID();
25+
CID(const ContentIdentifier &cid);
26+
};
1527
} // namespace fc
1628

1729
namespace fc::common {
18-
/**
19-
* CID is not default-constructible, but in some cases we need default value.
20-
* This value can be used to initialize class member or local variable. Trying
21-
* to CBOR encode this value will yield error, to ensure proper
22-
* initialization.
23-
*/
24-
const static inline CID kEmptyCid(
25-
{}, {}, libp2p::multi::Multihash::create({}, {}).value());
26-
2730
/// Compute CID from bytes
2831
outcome::result<CID> getCidOf(gsl::span<const uint8_t> bytes);
2932
} // namespace fc::common

0 commit comments

Comments
 (0)