Skip to content

Commit 6daa523

Browse files
committed
IPLD CR fix
1 parent 507af8b commit 6daa523

File tree

9 files changed

+162
-115
lines changed

9 files changed

+162
-115
lines changed

core/crypto/CMakeLists.txt

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

66
add_subdirectory(blake2)
77
add_subdirectory(bls)
8+
add_subdirectory(hasher)
89
add_subdirectory(murmur)
910
add_subdirectory(randomness)
1011
add_subdirectory(vrf)

core/crypto/hasher/CMakeLists.txt

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

core/crypto/hasher/hasher.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "hasher.hpp"
7+
8+
#include <libp2p/crypto/sha/sha256.hpp>
9+
#include "crypto/blake2/blake2b160.hpp"
10+
11+
namespace fc::crypto {
12+
std::map<Hasher::HashType, Hasher::HashMethod> Hasher::methods_{
13+
{HashType::sha256, Hasher::sha2_256},
14+
{HashType::blake2b_256, Hasher::blake2b_256}};
15+
16+
Hasher::Multihash Hasher::calculate(HashType hash_type,
17+
gsl::span<const uint8_t> buffer) {
18+
HashMethod hash_method = methods_.at(hash_type);
19+
return std::invoke(hash_method, buffer);
20+
}
21+
22+
Hasher::Multihash Hasher::sha2_256(gsl::span<const uint8_t> buffer) {
23+
auto digest = libp2p::crypto::sha256(buffer);
24+
auto multi_hash = Multihash::create(HashType::sha256, digest);
25+
return multi_hash.value();
26+
}
27+
28+
Hasher::Multihash Hasher::blake2b_256(gsl::span<const uint8_t> buffer) {
29+
auto digest = crypto::blake2b::blake2b_256(buffer);
30+
auto multi_hash = Multihash::create(HashType::blake2b_256, digest);
31+
return multi_hash.value();
32+
}
33+
} // namespace fc::crypto

core/crypto/hasher/hasher.hpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef FILECOIN_CORE_CRYPTO_HASHER_HPP
7+
#define FILECOIN_CORE_CRYPTO_HASHER_HPP
8+
9+
#include <map>
10+
11+
#include <libp2p/multi/multihash.hpp>
12+
13+
namespace fc::crypto {
14+
/**
15+
* @class Supported methods:
16+
* sha2-256
17+
* blakeb2-256
18+
*/
19+
class Hasher {
20+
protected:
21+
using HashType = libp2p::multi::HashType;
22+
using Multihash = libp2p::multi::Multihash;
23+
using HashMethod = Multihash (*)(gsl::span<const uint8_t>);
24+
25+
private:
26+
static std::map<HashType, HashMethod> methods_;
27+
28+
public:
29+
static Multihash calculate(HashType hash_type,
30+
gsl::span<const uint8_t> buffer);
31+
32+
/**
33+
* @brief Calculate SHA2-256 hash
34+
* @param buffer - source data
35+
* @return SHA2-256 hash
36+
*/
37+
static Multihash sha2_256(gsl::span<const uint8_t> buffer);
38+
39+
/**
40+
* @brief Calculate Blake2b-256 hash
41+
* @param buffer - source data
42+
* @return Blake2b-256 hash
43+
*/
44+
static Multihash blake2b_256(gsl::span<const uint8_t> buffer);
45+
};
46+
} // namespace fc::crypto
47+
48+
#endif

core/storage/ipld/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ add_library(ipld_block
2727
)
2828
target_link_libraries(ipld_block
2929
cid
30-
blake2
30+
filecoin_hasher
3131
)
3232

3333
add_library(ipld_link

core/storage/ipld/impl/ipld_block_impl.cpp

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,11 @@
55

66
#include "storage/ipld/impl/ipld_block_impl.hpp"
77

8-
#include <libp2p/crypto/sha/sha256.hpp>
9-
#include <libp2p/multi/multihash.hpp>
10-
#include "crypto/blake2/blake2b160.hpp"
8+
#include "crypto/hasher/hasher.hpp"
119

1210
namespace fc::storage::ipld {
13-
using crypto::blake2b::Blake2b256Hash;
14-
using libp2p::common::Hash256;
15-
using libp2p::multi::Multihash;
1611

17-
std::map<HashType, IPLDBlockImpl::HashMethod> IPLDBlockImpl::hash_methods_{
18-
{HashType::sha256, IPLDBlockImpl::hash_sha2_256},
19-
{HashType::blake2b_256, IPLDBlockImpl::hash_blake2b_256}};
20-
21-
IPLDBlockImpl::IPLDBlockImpl(CID::Version version,
12+
IPLDBlockImpl::IPLDBlockImpl(CID::Version version,
2213
HashType hash_type,
2314
ContentType content_type)
2415
: cid_version_{version},
@@ -27,39 +18,23 @@ namespace fc::storage::ipld {
2718

2819
const CID &IPLDBlockImpl::getCID() const {
2920
if (!cid_) {
30-
HashMethod hash_method = hash_methods_.at(cid_hash_type_);
31-
const common::Buffer &raw_data = getRawBytes();
32-
std::vector<uint8_t> digest = std::invoke(hash_method, raw_data);
33-
auto multi_hash = Multihash::create(cid_hash_type_, digest);
34-
CID id{cid_version_, content_type_, std::move(multi_hash.value())};
21+
auto multi_hash =
22+
crypto::Hasher::calculate(cid_hash_type_, getRawBytes());
23+
CID id{cid_version_, content_type_, std::move(multi_hash)};
3524
cid_ = std::move(id);
3625
}
3726
return cid_.value();
3827
}
3928

40-
const common::Buffer &IPLDBlockImpl::getRawBytes() const {
41-
if (!raw_bytes_) {
42-
raw_bytes_ = getBlockContent();
43-
}
44-
return raw_bytes_.value();
45-
}
46-
47-
void IPLDBlockImpl::clearCache() const {
48-
cid_ = boost::none;
49-
raw_bytes_ = boost::none;
29+
const common::Buffer &IPLDBlockImpl::getRawBytes() const {
30+
if (!raw_bytes_) {
31+
raw_bytes_ = getBlockContent();
5032
}
51-
52-
std::vector<uint8_t> IPLDBlockImpl::hash_sha2_256(
53-
const common::Buffer &data) {
54-
Hash256 digest = libp2p::crypto::sha256(data.toVector());
55-
return {std::make_move_iterator(digest.begin()),
56-
std::make_move_iterator((digest.end()))};
33+
return raw_bytes_.value();
5734
}
5835

59-
std::vector<uint8_t> IPLDBlockImpl::hash_blake2b_256(
60-
const common::Buffer &data) {
61-
Blake2b256Hash digest = crypto::blake2b::blake2b_256(data.toVector());
62-
return {std::make_move_iterator(digest.begin()),
63-
std::make_move_iterator((digest.end()))};
36+
void IPLDBlockImpl::clearCache() const {
37+
cid_ = boost::none;
38+
raw_bytes_ = boost::none;
6439
}
6540
} // namespace fc::storage::ipld

core/storage/ipld/impl/ipld_block_impl.hpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010
#include <vector>
1111

1212
#include <boost/optional.hpp>
13-
1413
#include <libp2p/multi/hash_type.hpp>
1514
#include <libp2p/multi/multicodec_type.hpp>
16-
1715
#include "common/buffer.hpp"
1816
#include "primitives/cid/cid.hpp"
1917
#include "storage/ipld/ipld_block.hpp"
@@ -48,30 +46,12 @@ namespace fc::storage::ipld {
4846
void clearCache() const;
4947

5048
private:
51-
using HashMethod = std::vector<uint8_t> (*)(const common::Buffer &);
52-
5349
CID::Version cid_version_;
5450
HashType cid_hash_type_;
5551
ContentType content_type_;
5652

5753
mutable boost::optional<CID> cid_;
5854
mutable boost::optional<common::Buffer> raw_bytes_;
59-
60-
static std::map<HashType, HashMethod> hash_methods_;
61-
62-
/**
63-
* @brief Calculate SHA-256 hash
64-
* @param data - bytes to hash
65-
* @return SHA-256 digest
66-
*/
67-
static std::vector<uint8_t> hash_sha2_256(const common::Buffer &data);
68-
69-
/**
70-
* @brief Calculate Blake2b-256 hash
71-
* @param data - bytes to hash
72-
* @return Blake2b-256 digest
73-
*/
74-
static std::vector<uint8_t> hash_blake2b_256(const common::Buffer &data);
7555
};
7656
} // namespace fc::storage::ipld
7757

core/storage/ipld/impl/ipld_node_decoder_pb.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <gsl/span>
1212
#include "common/buffer.hpp"
1313
#include "common/outcome.hpp"
14-
#include "ipld_node.pb.h"
14+
#include "ipld_node.pb.h" //TODO: Sergey Kaprovich FIL-150
1515

1616
namespace fc::storage::ipld {
1717
/**

core/storage/ipld/ipld_node.hpp

Lines changed: 55 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,80 +8,79 @@
88

99
#include <functional>
1010
#include <memory>
11-
#include <vector>
1211
#include <string>
12+
#include <vector>
1313

1414
#include "common/buffer.hpp"
1515
#include "common/outcome.hpp"
16-
#include "storage/ipld/ipld_link.hpp"
1716
#include "storage/ipld/ipld_block.hpp"
17+
#include "storage/ipld/ipld_link.hpp"
1818

1919
namespace fc::storage::ipld {
20+
/**
21+
* @interface MerkleDAG service node
22+
*/
23+
class IPLDNode : public virtual IPLDBlock {
24+
public:
25+
virtual ~IPLDNode() = default;
2026
/**
21-
* @interface MerkleDAG service node
27+
* @brief Total size of the data including the total sizes of references
28+
* @return Cumulative size in bytes
2229
*/
23-
class IPLDNode : public virtual IPLDBlock {
24-
public:
25-
26-
virtual ~IPLDNode() = default;
27-
/**
28-
* @brief Total size of the data including the total sizes of references
29-
* @return Cumulative size in bytes
30-
*/
31-
virtual size_t size() const = 0;
30+
virtual size_t size() const = 0;
3231

33-
/**
34-
* @brief Assign Node's content
35-
* @param input - data bytes
36-
* @return operation result
37-
*/
38-
virtual void assign(common::Buffer input) = 0;
32+
/**
33+
* @brief Assign Node's content
34+
* @param input - data bytes
35+
* @return operation result
36+
*/
37+
virtual void assign(common::Buffer input) = 0;
3938

40-
/**
41-
* @brief Get Node data
42-
* @return content bytes
43-
*/
44-
virtual const common::Buffer &content() const = 0;
39+
/**
40+
* @brief Get Node data
41+
* @return content bytes
42+
*/
43+
virtual const common::Buffer &content() const = 0;
4544

46-
/**
47-
* @brief Add link to the child node
48-
* @param name - name of the child node
49-
* @param node - child object to link
50-
* @return operation result
51-
*/
52-
virtual outcome::result<void> addChild(const std::string &name,
53-
std::shared_ptr<const IPLDNode> node) = 0;
45+
/**
46+
* @brief Add link to the child node
47+
* @param name - name of the child node
48+
* @param node - child object to link
49+
* @return operation result
50+
*/
51+
virtual outcome::result<void> addChild(
52+
const std::string &name, std::shared_ptr<const IPLDNode> node) = 0;
5453

55-
/**
56-
* @brief Get particular link to the child node
57-
* @param name - id of the link
58-
* @return Requested link of error, if link not found
59-
*/
60-
virtual outcome::result<std::reference_wrapper<const IPLDLink>> getLink(
61-
const std::string &name) const = 0;
54+
/**
55+
* @brief Get particular link to the child node
56+
* @param name - id of the link
57+
* @return Requested link of error, if link not found
58+
*/
59+
virtual outcome::result<std::reference_wrapper<const IPLDLink>> getLink(
60+
const std::string &name) const = 0;
6261

63-
/**
64-
* @brief Remove link to the child node
65-
* @param name - name of the child node
66-
* @return operation result
67-
*/
68-
virtual void removeLink(const std::string &name) = 0;
62+
/**
63+
* @brief Remove link to the child node
64+
* @param name - name of the child node
65+
* @return operation result
66+
*/
67+
virtual void removeLink(const std::string &name) = 0;
6968

70-
/**
71-
* @brief Insert link to the child node
72-
* @param link - object to add
73-
*/
74-
virtual void addLink(const IPLDLink &link) = 0;
69+
/**
70+
* @brief Insert link to the child node
71+
* @param link - object to add
72+
*/
73+
virtual void addLink(const IPLDLink &link) = 0;
7574

76-
virtual std::vector<std::reference_wrapper<const IPLDLink>> getLinks()
75+
virtual std::vector<std::reference_wrapper<const IPLDLink>> getLinks()
7776
const = 0;
78-
};
77+
};
7978

80-
/**
81-
* @class Possible Node errors
82-
*/
83-
enum class IPLDNodeError : int { LINK_NOT_FOUND, INVALID_RAW_DATA };
84-
} // namespace fc::storage::ipfs::merkledag
79+
/**
80+
* @class Possible Node errors
81+
*/
82+
enum class IPLDNodeError { LINK_NOT_FOUND = 1, INVALID_RAW_DATA };
83+
} // namespace fc::storage::ipld
8584

8685
OUTCOME_HPP_DECLARE_ERROR(fc::storage::ipld, IPLDNodeError)
8786

0 commit comments

Comments
 (0)