Skip to content
Draft
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
87 changes: 87 additions & 0 deletions core/profiler/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Copyright Soramitsu Co., Ltd. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

add_library(common INTERFACE
# bind.hpp
# blob.hpp
# byteutils.hpp
# cloneable.hpp
# default_constructible_unary_fn.hpp
# instanceof.hpp
# is_any.hpp
# obj_utils.hpp
# result.hpp
# set.hpp
# visitor.hpp
# memory_utils.hpp
# murmur2.hpp
# spin_lock.hpp
)
target_link_libraries(common INTERFACE
Boost::boost
)

add_library(libs_files
files.cpp
)
target_link_libraries(libs_files
logger
common
Boost::filesystem
)

add_library(libs_timeout INTERFACE
# timeout.hpp
)
target_link_libraries(libs_timeout INTERFACE
common
rxcpp
)

add_library(permutation_generator permutation_generator.cpp)

add_library(irohad_version irohad_version.cpp)

add_library(libs_to_string INTERFACE
# to_string.hpp
)
target_link_libraries(libs_to_string INTERFACE
Boost::boost
)

# Get the git repo data
set(GIT_REPO_PRETTY_VER "version info unavailable")
if (EXISTS "${PROJECT_SOURCE_DIR}/.git")
if(NOT GIT_FOUND)
find_package(Git QUIET)
endif()

if(GIT_EXECUTABLE)
# Get pretty version
execute_process(COMMAND
"${GIT_EXECUTABLE}"
describe --tags --always
WORKING_DIRECTORY
"${PROJECT_SOURCE_DIR}"
RESULT_VARIABLE
res
OUTPUT_VARIABLE
GIT_REPO_PRETTY_VER
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(NOT res EQUAL 0)
message(STATUS "Error running `git describe': ${res}")
endif()
message(STATUS "Git repo pretty version: ${GIT_REPO_PRETTY_VER}")
else()
message(STATUS "Git executable not found!")
endif()
endif()

target_compile_definitions(irohad_version
PRIVATE
-DGIT_REPO_PRETTY_VER="${GIT_REPO_PRETTY_VER}"
-DIROHA_MAJOR_VERSION=${PROJECT_VERSION_MAJOR}
-DIROHA_MINOR_VERSION=${PROJECT_VERSION_MINOR}
-DIROHA_PATCH_VERSION=${PROJECT_VERSION_PATCH}
)
19 changes: 19 additions & 0 deletions core/profiler/common/array_size.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef IROHA_COMMON_ARRAY_SIZE_HPP
#define IROHA_COMMON_ARRAY_SIZE_HPP

#ifdef IROHA_ARRAY_SIZE
#error IROHA_ARRAY_SIZE already defined.
#endif // IROHA_ARRAY_SIZE

#ifndef IROHA_ARRAY_SIZE
template <typename T, size_t N>
char (&IrohaArraySizeHelper(T (&array)[N]))[N];
#define IROHA_ARRAY_SIZE(array) (sizeof(IrohaArraySizeHelper(array)))
#endif // IROHA_ARRAY_SIZE

#endif // IROHA_COMMON_ARRAY_SIZE_HPP
87 changes: 87 additions & 0 deletions core/profiler/common/bind.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef IROHA_COMMON_BIND_HPP
#define IROHA_COMMON_BIND_HPP

#include <ciso646>
#include <type_traits>
#include <utility>

namespace iroha {

/**
* Bind operator. If argument has value, dereferences argument and calls
* given function, which should return wrapped value
* operator| is used since it has to be binary and left-associative
* Non-void returning specialization
*
* boost::optional<int> f();
* boost::optional<double> g(int);
*
* boost::optional<double> d = f()
* | g;
*
* std::forward should be used in any reference of arguments because
* operator bool, operator*, and operator() of arguments can have
* different implementation with ref-qualifiers
*
* Trailing return type checks that result of applying function to
* unwrapped value results in non-void type
*
* @tparam T - monadic type
* @tparam Transform - transform function type
* @param t - monadic value
* @param f - function, which takes dereferenced value, and returns
* wrapped value
* @return monadic value, which can be of another type
*/
template <typename T, typename Transform>
auto operator|(T &&t, Transform &&f) -> std::enable_if_t<
not std::is_same<
decltype(std::forward<Transform>(f)(*std::forward<T>(t))),
void>::value,
decltype(std::forward<Transform>(f)(*std::forward<T>(t)))> {
if (std::forward<T>(t)) {
return std::forward<Transform>(f)(*std::forward<T>(t));
}
return {};
}

/**
* Bind operator. If argument has value, dereferences argument and calls
* given function, which should return wrapped value
* operator| is used since it has to be binary and left-associative
* Void specialization
*
* boost::optional<int> f();
* void g(int);
*
* f() | g;
*
* std::forward should be used in any reference of arguments because
* operator bool, operator*, and operator() of arguments can have
* different implementation with ref-qualifiers
*
* Trailing return type checks that result of applying function to
* unwrapped value results in void type
*
* @tparam T - monadic type
* @tparam Transform - transform function type
* @param t - monadic value
* @param f - function, which takes dereferenced value, and returns
* wrapped value
*/
template <typename T, typename Transform>
auto operator|(T &&t, Transform &&f) -> std::enable_if_t<
std::is_same<decltype(std::forward<Transform>(f)(*std::forward<T>(t))),
void>::value> {
if (std::forward<T>(t)) {
std::forward<Transform>(f)(*std::forward<T>(t));
}
}
} // namespace iroha

#endif // IROHA_COMMON_BIND_HPP
87 changes: 87 additions & 0 deletions core/profiler/common/blob.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef IROHA_COMMON_BLOB_HPP
#define IROHA_COMMON_BLOB_HPP

#include <algorithm>
#include <array>
#include <cstdint>
#include <stdexcept>
#include <string>

#include "common/hexutils.hpp"
#include "common/result.hpp"

namespace iroha {
using BadFormatException = std::invalid_argument;
using byte_t = uint8_t;

/**
* Base type which represents blob of fixed size.
*
* std::string is convenient to use but it is not safe.
* We can not specify the fixed length for string.
*
* For std::array it is possible, so we prefer it over std::string.
*/
template <size_t size_>
class blob_t : public std::array<byte_t, size_> {
public:
/**
* Initialize blob value
*/
blob_t() {
this->fill(0);
}

/**
* In compile-time returns size of current blob.
*/
constexpr static size_t size() {
return size_;
}

/**
* Converts current blob to std::string
*/
std::string to_string() const noexcept {
return std::string{this->begin(), this->end()};
}

/**
* Converts current blob to hex string.
*/
std::string to_hexstring() const noexcept {
return bytestringToHexstring(std::string_view{
reinterpret_cast<const char *>(this->data()), this->size()});
}

static blob_t<size_> from_raw(const byte_t data[size_]) {
blob_t<size_> b;
std::copy(data, data + size_, b.begin());
return b;
}

static expected::Result<blob_t<size_>, std::string> from_string(
std::string_view data) {
if (data.size() != size_) {
return expected::makeError(
std::string{"blob_t: input string has incorrect length. Found: "}
+ std::to_string(data.size())
+ +", required: " + std::to_string(size_));
}
return from_raw(reinterpret_cast<const byte_t *>(data.data()));
}

static expected::Result<blob_t<size_>, std::string> from_hexstring(
std::string_view hex) {
return iroha::hexstringToBytestringResult(hex) |
[](auto &&bytes) { return from_string(bytes); };
}
};
} // namespace iroha

#endif // IROHA_COMMON_BLOB_HPP
67 changes: 67 additions & 0 deletions core/profiler/common/byteutils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef IROHA_BYTEUTILS_H
#define IROHA_BYTEUTILS_H

#include <algorithm>
#include <string>
#include <vector>

#include <boost/optional.hpp>

#include "common/bind.hpp"
#include "common/blob.hpp"
#include "common/hexutils.hpp"

namespace iroha {
/**
* Convert string to blob vector
* @param source - string for conversion
* @return vector<blob>
*/
inline std::vector<uint8_t> stringToBytes(const std::string &source) {
return std::vector<uint8_t>(source.begin(), source.end());
}

/**
* blob vector to string
* @param source - vector for conversion
* @return result string
*/
inline std::string bytesToString(const std::vector<uint8_t> &source) {
return std::string(source.begin(), source.end());
}

/**
* Create blob_t from string of specified size
* @tparam size - size of blob_t, expected size of string
* @param s - string to convert
* @return blob, if conversion was successful, otherwise nullopt
*/
template <size_t size>
boost::optional<blob_t<size>> stringToBlob(const std::string &string) {
if (size != string.size()) {
return boost::none;
}
blob_t<size> array;
std::copy(string.begin(), string.end(), array.begin());
return array;
}

/**
* Convert hexstring to array of given size
* @tparam size - output array size
* @param string - input string for transform
* @return array of given size if size matches, nullopt otherwise
*/
template <size_t size>
boost::optional<blob_t<size>> hexstringToArray(const std::string &string) {
return hexstringToBytestring(string) | stringToBlob<size>;
}

} // namespace iroha

#endif // IROHA_BYTEUTILS_H
Loading