Skip to content

Commit 7284eae

Browse files
IPLD refactor (#108)
1 parent 3a5deca commit 7284eae

40 files changed

+681
-571
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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
BOOST_ASSERT_MSG(multi_hash.has_value(),
26+
"fc::crypto::Hasher - failed to create sha2-256 hash");
27+
return multi_hash.value();
28+
}
29+
30+
Hasher::Multihash Hasher::blake2b_256(gsl::span<const uint8_t> buffer) {
31+
auto digest = crypto::blake2b::blake2b_256(buffer);
32+
auto multi_hash = Multihash::create(HashType::blake2b_256, digest);
33+
BOOST_ASSERT_MSG(multi_hash.has_value(),
34+
"fc::crypto::Hasher - failed to create blake2b_256 hash");
35+
return multi_hash.value();
36+
}
37+
} // 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/primitives/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ add_subdirectory(block)
88
add_subdirectory(chain)
99
add_subdirectory(chain_epoch)
1010
add_subdirectory(cid)
11-
add_subdirectory(persistent_block)
1211
add_subdirectory(rle_bitset)
1312
add_subdirectory(ticket)
1413
add_subdirectory(tipset)

core/primitives/persistent_block/CMakeLists.txt

Lines changed: 0 additions & 11 deletions
This file was deleted.

core/primitives/persistent_block/persistent_block.cpp

Lines changed: 0 additions & 22 deletions
This file was deleted.

core/primitives/persistent_block/persistent_block.hpp

Lines changed: 0 additions & 28 deletions
This file was deleted.

core/storage/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_subdirectory(filestore)
1010
add_subdirectory(hamt)
1111
add_subdirectory(in_memory)
1212
add_subdirectory(ipfs)
13+
add_subdirectory(ipld)
1314
add_subdirectory(keystore)
1415
add_subdirectory(leveldb)
1516
add_subdirectory(repository)

core/storage/chain/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ target_link_libraries(chain_store
99
datastore_key
1010
ipfs_blockservice
1111
logger
12-
persistent_block
1312
)
1413

1514
add_library(datastore_key

0 commit comments

Comments
 (0)