Skip to content

IPLD refactor #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

add_subdirectory(blake2)
add_subdirectory(bls)
add_subdirectory(hasher)
add_subdirectory(murmur)
add_subdirectory(randomness)
add_subdirectory(vrf)
Expand Down
11 changes: 11 additions & 0 deletions core/crypto/hasher/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#
# Copyright Soramitsu Co., Ltd. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
#

add_library(filecoin_hasher
hasher.cpp
)
target_link_libraries(filecoin_hasher
blake2
)
37 changes: 37 additions & 0 deletions core/crypto/hasher/hasher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#include "hasher.hpp"

#include <libp2p/crypto/sha/sha256.hpp>
#include "crypto/blake2/blake2b160.hpp"

namespace fc::crypto {
std::map<Hasher::HashType, Hasher::HashMethod> Hasher::methods_{
{HashType::sha256, Hasher::sha2_256},
{HashType::blake2b_256, Hasher::blake2b_256}};

Hasher::Multihash Hasher::calculate(HashType hash_type,
gsl::span<const uint8_t> buffer) {
HashMethod hash_method = methods_.at(hash_type);
return std::invoke(hash_method, buffer);
}

Hasher::Multihash Hasher::sha2_256(gsl::span<const uint8_t> buffer) {
auto digest = libp2p::crypto::sha256(buffer);
auto multi_hash = Multihash::create(HashType::sha256, digest);
BOOST_ASSERT_MSG(multi_hash.has_value(),
"fc::crypto::Hasher - failed to create sha2-256 hash");
return multi_hash.value();
}

Hasher::Multihash Hasher::blake2b_256(gsl::span<const uint8_t> buffer) {
auto digest = crypto::blake2b::blake2b_256(buffer);
auto multi_hash = Multihash::create(HashType::blake2b_256, digest);
BOOST_ASSERT_MSG(multi_hash.has_value(),
"fc::crypto::Hasher - failed to create blake2b_256 hash");
return multi_hash.value();
}
} // namespace fc::crypto
48 changes: 48 additions & 0 deletions core/crypto/hasher/hasher.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef FILECOIN_CORE_CRYPTO_HASHER_HPP
#define FILECOIN_CORE_CRYPTO_HASHER_HPP

#include <map>

#include <libp2p/multi/multihash.hpp>

namespace fc::crypto {
/**
* @class Supported methods:
* sha2-256
* blakeb2-256
*/
class Hasher {
protected:
using HashType = libp2p::multi::HashType;
using Multihash = libp2p::multi::Multihash;
using HashMethod = Multihash (*)(gsl::span<const uint8_t>);

private:
static std::map<HashType, HashMethod> methods_;

public:
static Multihash calculate(HashType hash_type,
gsl::span<const uint8_t> buffer);

/**
* @brief Calculate SHA2-256 hash
* @param buffer - source data
* @return SHA2-256 hash
*/
static Multihash sha2_256(gsl::span<const uint8_t> buffer);

/**
* @brief Calculate Blake2b-256 hash
* @param buffer - source data
* @return Blake2b-256 hash
*/
static Multihash blake2b_256(gsl::span<const uint8_t> buffer);
};
} // namespace fc::crypto

#endif
1 change: 0 additions & 1 deletion core/primitives/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ add_subdirectory(block)
add_subdirectory(chain)
add_subdirectory(chain_epoch)
add_subdirectory(cid)
add_subdirectory(persistent_block)
add_subdirectory(rle_bitset)
add_subdirectory(ticket)
add_subdirectory(tipset)
11 changes: 0 additions & 11 deletions core/primitives/persistent_block/CMakeLists.txt

This file was deleted.

22 changes: 0 additions & 22 deletions core/primitives/persistent_block/persistent_block.cpp

This file was deleted.

28 changes: 0 additions & 28 deletions core/primitives/persistent_block/persistent_block.hpp

This file was deleted.

1 change: 1 addition & 0 deletions core/storage/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ add_subdirectory(filestore)
add_subdirectory(hamt)
add_subdirectory(in_memory)
add_subdirectory(ipfs)
add_subdirectory(ipld)
add_subdirectory(keystore)
add_subdirectory(leveldb)
add_subdirectory(repository)
1 change: 0 additions & 1 deletion core/storage/chain/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ target_link_libraries(chain_store
datastore_key
ipfs_blockservice
logger
persistent_block
)

add_library(datastore_key
Expand Down
39 changes: 0 additions & 39 deletions core/storage/ipfs/ipfs_block.hpp

This file was deleted.

29 changes: 5 additions & 24 deletions core/storage/ipfs/merkledag/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,34 +1,15 @@
find_package(Protobuf REQUIRED)
set(PB_SCHEME "merkledag.proto")
set(PB_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR})

execute_process(COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} --cpp_out=${PB_BUILD_DIR} ${PB_SCHEME}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/impl/protobuf
RESULT_VARIABLE CMD_OUTPUT
)

add_library(ipfs_merkledag_service_protobuf
${PB_BUILD_DIR}/merkledag.pb.h
${PB_BUILD_DIR}/merkledag.pb.cc
)
target_include_directories(ipfs_merkledag_service_protobuf PUBLIC ${PB_BUILD_DIR})
target_link_libraries(ipfs_merkledag_service_protobuf
protobuf::libprotobuf
)
disable_clang_tidy(ipfs_merkledag_service_protobuf)
#
# Copyright Soramitsu Co., Ltd. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
#

add_library(ipfs_merkledag_service
impl/link_impl.cpp
impl/node_impl.cpp
impl/merkledag_service_impl.cpp
impl/pb_node_encoder.cpp
impl/pb_node_decoder.cpp
impl/leaf_impl.cpp
)
target_link_libraries(ipfs_merkledag_service
cid
Boost::boost
ipld_node
ipfs_blockservice
ipfs_datastore_in_memory
ipfs_merkledag_service_protobuf
)
27 changes: 0 additions & 27 deletions core/storage/ipfs/merkledag/impl/link_impl.cpp

This file was deleted.

20 changes: 11 additions & 9 deletions core/storage/ipfs/merkledag/impl/merkledag_service_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@

#include <boost/assert.hpp>
#include <libp2p/multi/content_identifier_codec.hpp>
#include "storage/ipfs/merkledag/impl/node_impl.hpp"
#include "storage/ipld/impl/ipld_node_impl.hpp"

using libp2p::multi::ContentIdentifierCodec;

namespace fc::storage::ipfs::merkledag {
using ipld::IPLDNodeImpl;

MerkleDagServiceImpl::MerkleDagServiceImpl(
std::shared_ptr<IpfsDatastore> service)
: block_service_{std::move(service)} {
Expand All @@ -20,14 +22,14 @@ namespace fc::storage::ipfs::merkledag {
}

outcome::result<void> MerkleDagServiceImpl::addNode(
std::shared_ptr<const Node> node) {
std::shared_ptr<const IPLDNode> node) {
return block_service_->set(node->getCID(), node->getRawBytes());
}

outcome::result<std::shared_ptr<Node>> MerkleDagServiceImpl::getNode(
outcome::result<std::shared_ptr<IPLDNode>> MerkleDagServiceImpl::getNode(
const CID &cid) const {
OUTCOME_TRY(content, block_service_->get(cid));
return NodeImpl::createFromRawBytes(content);
return IPLDNodeImpl::createFromRawBytes(content);
}

outcome::result<void> MerkleDagServiceImpl::removeNode(const CID &cid) {
Expand All @@ -37,15 +39,15 @@ namespace fc::storage::ipfs::merkledag {
outcome::result<size_t> MerkleDagServiceImpl::select(
gsl::span<const uint8_t> root_cid,
gsl::span<const uint8_t> selector,
std::function<bool(std::shared_ptr<const Node>)> handler) const {
std::function<bool(std::shared_ptr<const IPLDNode>)> handler) const {
std::ignore = selector;
OUTCOME_TRY(content_id, ContentIdentifierCodec::decode(root_cid));
CID cid{std::move(content_id)};
OUTCOME_TRY(root_node, getNode(cid));
std::vector<std::shared_ptr<const Node>> node_set{};
std::vector<std::shared_ptr<const IPLDNode>> node_set{};
node_set.emplace_back(std::move(root_node));
const auto &links = node_set.front()->getLinks();
for (const auto& link : links) {
for (const auto &link : links) {
auto request = getNode(link.get().getCID());
if (request.has_error()) return ServiceError::UNRESOLVED_LINK;
node_set.emplace_back(std::move(request.value()));
Expand Down Expand Up @@ -79,7 +81,7 @@ namespace fc::storage::ipfs::merkledag {

outcome::result<void> MerkleDagServiceImpl::buildGraph(
const std::shared_ptr<LeafImpl> &root,
const std::vector<std::reference_wrapper<const Link>> &links,
const std::vector<std::reference_wrapper<const IPLDLink>> &links,
bool depth_limit,
const size_t max_depth,
size_t current_depth) const {
Expand All @@ -89,7 +91,7 @@ namespace fc::storage::ipfs::merkledag {
for (const auto &link : links) {
auto request = getNode(link.get().getCID());
if (request.has_error()) return ServiceError::UNRESOLVED_LINK;
std::shared_ptr<Node> node = request.value();
std::shared_ptr<IPLDNode> node = request.value();
auto child_leaf = std::make_shared<LeafImpl>(node->content());
auto build_result = buildGraph(child_leaf,
node->getLinks(),
Expand Down
Loading