Skip to content

Commit 63e5c81

Browse files
IPFS MerkleDAG (#77)
Signed-off-by: Sergey Kaprovich <[email protected]>
1 parent 80b50ed commit 63e5c81

29 files changed

+1547
-16
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ jobs:
4646
brew install ninja
4747
else
4848
sudo apt-get update || true
49-
sudo apt-get install -y ninja-build
49+
sudo apt-get install -y ninja-build python-setuptools
5050
fi
5151
5252
pip3 -V || sudo python3 -m pip install --upgrade pip
53-
sudo pip3 install scikit-build cmake requests gitpython gcovr pyyaml
53+
sudo pip3 install scikit-build
54+
sudo pip3 install cmake requests gitpython gcovr pyyaml
5455
sudo curl https://sh.rustup.rs -sSf | sh -s -- -y
5556
- name: cmake
5657
env:

cmake/Hunter/config.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ hunter_config(
3030
)
3131

3232
hunter_config(libp2p
33-
URL https://github.com/soramitsu/libp2p/archive/cdc092fe74ef5d8eeedf42e3e949488ff5052cb4.zip
34-
SHA1 16605084e4f018ebdb09018e906a54aa1909c8e1
33+
URL https://github.com/soramitsu/libp2p/archive/06a70dfcdc2649264f44c460a542e0c774db2290.zip
34+
SHA1 c6bbd0e55659fb0a7a84091a97d219f59ae37d14
3535
CMAKE_ARGS TESTING=OFF
3636
)

cmake/dependencies.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,7 @@ find_package(libp2p CONFIG REQUIRED)
4545
# https://docs.hunter.sh/en/latest/packages/pkg/cppcodec.html
4646
hunter_add_package(cppcodec)
4747
find_package(cppcodec CONFIG REQUIRED)
48+
49+
# https://hunter.readthedocs.io/en/stable/packages/pkg/Protobuf.html
50+
hunter_add_package(Protobuf)
51+
find_package(Protobuf CONFIG REQUIRED)

core/storage/ipfs/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ add_library(ipfs_blockservice
2929
target_link_libraries(ipfs_blockservice
3030
buffer
3131
)
32+
33+
add_subdirectory(merkledag)

core/storage/ipfs/block.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
#ifndef FILECOIN_STORAGE_IPFS_BLOCK_HPP
77
#define FILECOIN_STORAGE_IPFS_BLOCK_HPP
88

9+
#include <functional>
10+
911
#include "common/buffer.hpp"
12+
#include "common/outcome.hpp"
1013
#include "primitives/cid/cid.hpp"
1114

1215
namespace fc::storage::ipfs {
@@ -31,7 +34,7 @@ namespace fc::storage::ipfs {
3134
* @brief Get block content
3235
* @return Block's raw data for store in the BlockService
3336
*/
34-
virtual const Content &getContent() const = 0;
37+
virtual const Content &getRawBytes() const = 0;
3538
};
3639
} // namespace fc::storage::ipfs
3740

core/storage/ipfs/impl/blockservice_impl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace fc::storage::ipfs {
1515
}
1616

1717
outcome::result<void> BlockServiceImpl::addBlock(const Block &block) {
18-
auto result = local_storage_->set(block.getCID(), block.getContent());
18+
auto result = local_storage_->set(block.getCID(), block.getRawBytes());
1919
if (result.has_error()) {
2020
return BlockServiceError::ADD_BLOCK_FAILED;
2121
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
find_package(Protobuf REQUIRED)
2+
set(PB_SCHEME "merkledag.proto")
3+
set(PB_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR})
4+
5+
execute_process(COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} --cpp_out=${PB_BUILD_DIR} ${PB_SCHEME}
6+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/impl/protobuf
7+
RESULT_VARIABLE CMD_OUTPUT
8+
)
9+
10+
add_library(ipfs_merkledag_service_protobuf
11+
${PB_BUILD_DIR}/merkledag.pb.h
12+
${PB_BUILD_DIR}/merkledag.pb.cc
13+
)
14+
target_include_directories(ipfs_merkledag_service_protobuf PUBLIC ${PB_BUILD_DIR})
15+
target_link_libraries(ipfs_merkledag_service_protobuf
16+
protobuf::libprotobuf
17+
)
18+
disable_clang_tidy(ipfs_merkledag_service_protobuf)
19+
20+
add_library(ipfs_merkledag_service
21+
impl/link_impl.cpp
22+
impl/node_impl.cpp
23+
impl/merkledag_service_impl.cpp
24+
impl/pb_node_encoder.cpp
25+
impl/pb_node_decoder.cpp
26+
impl/leaf_impl.cpp
27+
)
28+
target_link_libraries(ipfs_merkledag_service
29+
cid
30+
Boost::boost
31+
ipfs_blockservice
32+
ipfs_datastore_in_memory
33+
ipfs_merkledag_service_protobuf
34+
)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "storage/ipfs/merkledag/impl/leaf_impl.hpp"
7+
8+
namespace fc::storage::ipfs::merkledag {
9+
LeafImpl::LeafImpl(common::Buffer data)
10+
: content_{std::move(data)} {}
11+
12+
const common::Buffer &LeafImpl::content() const {
13+
return content_;
14+
}
15+
16+
size_t LeafImpl::count() const {
17+
return children_.size();
18+
}
19+
20+
outcome::result<std::reference_wrapper<const Leaf>> LeafImpl::subLeaf(
21+
std::string_view name) const {
22+
if (auto iter = children_.find(name); iter != children_.end()) {
23+
return iter->second;
24+
}
25+
return LeafError::LEAF_NOT_FOUND;
26+
}
27+
28+
std::vector<std::string_view> LeafImpl::getSubLeafNames() const {
29+
std::vector<std::string_view> names;
30+
for (const auto &child : children_) {
31+
names.push_back(child.first);
32+
}
33+
return names;
34+
}
35+
36+
outcome::result<void> LeafImpl::insertSubLeaf(std::string name,
37+
LeafImpl children) {
38+
auto result = children_.emplace(std::move(name), std::move(children));
39+
if (result.second) {
40+
return outcome::success();
41+
}
42+
return LeafError::DUPLICATE_LEAF;
43+
}
44+
} // namespace fc::storage::ipfs::merkledag
45+
46+
OUTCOME_CPP_DEFINE_CATEGORY(fc::storage::ipfs::merkledag, LeafError, e) {
47+
using fc::storage::ipfs::merkledag::LeafError;
48+
switch (e) {
49+
case (LeafError::LEAF_NOT_FOUND):
50+
return "MerkleDAG leaf: children leaf not found";
51+
case (LeafError::DUPLICATE_LEAF):
52+
return "MerkleDAG leaf: duplicate leaf name";
53+
}
54+
return "MerkleDAG leaf: unknown error";
55+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef FILECOIN_STORAGE_IPFS_MERKLEDAG_LEAF_IMPL_HPP
7+
#define FILECOIN_STORAGE_IPFS_MERKLEDAG_LEAF_IMPL_HPP
8+
9+
#include "storage/ipfs/merkledag/leaf.hpp"
10+
11+
#include <map>
12+
#include <string>
13+
14+
namespace fc::storage::ipfs::merkledag {
15+
class LeafImpl : public Leaf {
16+
public:
17+
/**
18+
* @brief Construct leaf
19+
* @param data - leaf content
20+
*/
21+
explicit LeafImpl(common::Buffer data);
22+
23+
const common::Buffer &content() const override;
24+
25+
size_t count() const override;
26+
27+
outcome::result<std::reference_wrapper<const Leaf>> subLeaf(
28+
std::string_view name) const override;
29+
30+
std::vector<std::string_view> getSubLeafNames() const override;
31+
32+
/**
33+
* @brief Insert children leaf
34+
* @param name - id of the leaf
35+
* @param children - leaf to insert
36+
* @return operation result
37+
*/
38+
outcome::result<void> insertSubLeaf(std::string name, LeafImpl children);
39+
40+
private:
41+
common::Buffer content_;
42+
std::map<std::string, LeafImpl, std::less<>> children_;
43+
};
44+
} // namespace fc::storage::ipfs::merkledag
45+
46+
#endif
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "storage/ipfs/merkledag/impl/link_impl.hpp"
7+
8+
namespace fc::storage::ipfs::merkledag {
9+
LinkImpl::LinkImpl(libp2p::multi::ContentIdentifier id,
10+
std::string name,
11+
size_t size)
12+
: cid_{std::move(id)},
13+
name_{std::move(name)},
14+
size_{size} {}
15+
16+
const std::string &LinkImpl::getName() const {
17+
return name_;
18+
}
19+
20+
const CID &LinkImpl::getCID() const {
21+
return cid_;
22+
}
23+
24+
size_t LinkImpl::getSize() const {
25+
return size_;
26+
}
27+
} // namespace fc::storage::ipfs::merkledag

0 commit comments

Comments
 (0)