Skip to content
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

Refactoring database usage #23

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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
902 changes: 902 additions & 0 deletions CMake/conan.cmake

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ set(CMAKE_CXX_STANDARD 20)
# initialize conan libs
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(NO_OUTPUT_DIRS KEEP_RPATHS)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/CMake)
Expand Down Expand Up @@ -67,6 +66,12 @@ endif()
add_subdirectory(Simple-Web-Server)
include_directories(Simple-Web-Server)

add_subdirectory(Simple-WebSocket-Server)
include_directories(Simple-WebSocket-Server)

add_subdirectory(AshDB)
include_directories(AshDB/include)

add_subdirectory(src)

if (BUILD_ASH_TESTS)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ To create a `.sln` file, run the following commands from the root source directo
mkdir build
cd build
conan install .. -s build_type=Debug --build missing
cmake .. -DCMAKE_BUILD_TYPE=Debug -DBUILD_ASH_TESTS=On
cmake .. -DCMAKE_BUILD_TYPE=Debug -DBUILD_ASH_TESTS=On -DASHDB_BUILD_CONAN=Off
```

### macos
Expand All @@ -58,7 +58,7 @@ This project has been compiled on macos 10.15 using Apple clang 11.0. I have bee
mkdir build
cd build
conan install .. -s build_type=Debug --build missing
cmake .. "-GNinja" -DCMAKE_BUILD_TYPE=Debug -DBUILD_ASH_TESTS=On
cmake .. "-GNinja" -DCMAKE_BUILD_TYPE=Debug -DBUILD_ASH_TESTS=On -DASHDB_BUILD_CONAN=Off
```

If there are no errors then the executable can be built with:
Expand Down
17 changes: 16 additions & 1 deletion src/Block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ void from_json(const nl::json& j, Block& b)
BlockTime{std::chrono::milliseconds{j["time"].get<std::uint64_t>()}};
}

void to_json(nl::json& j, const BlockList& b)
{
for (const auto& block : b)
{
j.push_back(block);
}
}

void from_json(const nl::json& j, BlockList& b)
{
for (const auto& jblock : j.items())
{
b.push_back(jblock.value().get<Block>());
}
}

bool ValidHash(const Block& block)
{
const auto computedHash = CalculateBlockHash(block);
Expand Down Expand Up @@ -106,7 +122,6 @@ std::string CalculateBlockHash(const Block& block)
}

Block::Block(std::uint64_t index, std::string_view prevHash, Transactions&& txs)
: _logger(ash::initializeLogger("Block"))
{
_hashed._index = index;
_hashed._nonce = 0;
Expand Down
30 changes: 24 additions & 6 deletions src/Block.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ namespace ash
class Block;
using BlockSharedPtr = std::shared_ptr<Block>;
using BlockUniquePtr = std::unique_ptr<Block>;
using BlockList = std::vector<Block>;

void to_json(nl::json& j, const Block& b);
void from_json(const nl::json& j, Block& b);

void to_json(nl::json& j, const BlockList& b);
void from_json(const nl::json& j, BlockList& b);

bool ValidHash(const Block& block);
bool ValidNewBlock(const Block& block, const Block& prevblock);

Expand All @@ -36,13 +40,18 @@ std::string CalculateBlockHash(

class Block
{
friend void read_block(std::istream& stream, Block& block);
friend void write_block(std::ostream& stream, const Block& block);
friend void ashdb_read(std::istream& stream, Block& block);
friend void from_json(const nl::json& j, Block& b);
friend class Miner;

public:
Block() = default;
Block(std::uint64_t index, std::string_view prevHash)
: Block(index, prevHash, {})
{
// nothing to do
}

Block(std::uint64_t index, std::string_view prevHash, Transactions&& tx);

bool operator==(const Block& other) const;
Expand All @@ -62,10 +71,20 @@ class Block
std::string previousHash() const { return _hashed._prev; }

const Transactions& transactions() const { return _hashed._txs; }
Transactions& transactions()
void add_transaction(Transaction&& tx)
{
return const_cast<Transactions&>(
(static_cast<const Block*>(this))->transactions());
_hashed._txs.push_back(std::move(tx));
}

[[maybe_unused]] bool update_transaction(std::size_t index, const Transaction& tx)
{
if (index >= _hashed._txs.size())
{
return false;
}

_hashed._txs.at(index) = tx;
return true;
}

std::string hash() const { return _hash; }
Expand Down Expand Up @@ -97,7 +116,6 @@ class Block

std::string _hash;
std::string _miner;
SpdLogPtr _logger;
};

} // namespace ash
Expand Down
Loading