Skip to content

Commit

Permalink
Merge branch '1.0' into 'main'
Browse files Browse the repository at this point in the history
  • Loading branch information
allenvox committed Nov 3, 2024
2 parents e148c70 + 372a60b commit 7da32c6
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
Language: Cpp
BasedOnStyle: LLVM
...

19 changes: 19 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: CMake build & run workflow
on: push
jobs:
check_codestyle:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Check codestyle
run: git ls-files '*.c' '*.h' '*.cpp' '*.hpp' | xargs clang-format -i --verbose && git diff --exit-code
preset_release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Configure CMake
run: cmake --preset release
- name: Build
run: cmake --build --preset release
- name: Run
run: build/bin/blkchn
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.vscode
build
bin
obj
22 changes: 22 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
cmake_minimum_required(VERSION 3.22)

project(blkchn)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake)
include(CompileOptions)

find_package(OpenSSL REQUIRED)

find_program(CLANG_TIDY_EXE NAMES clang-tidy-14 clang-tidy)
if(NOT CLANG_TIDY_EXE)
message(WARNING "clang-tidy not found")
else()
execute_process(
COMMAND ${CLANG_TIDY_EXE} --version
OUTPUT_VARIABLE CLANG_TIDY_VERSION
)
message("clang-tidy found:\n" ${CLANG_TIDY_VERSION})
endif()

add_subdirectory(src)
27 changes: 27 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"version": 3,
"cmakeMinimumRequired": {
"major": 3,
"minor": 22,
"patch": 0
},
"configurePresets": [
{
"name": "base",
"generator": "Unix Makefiles",
"hidden": true
},
{
"name": "release",
"inherits": "base",
"binaryDir": "${sourceDir}/build"
}
],
"buildPresets": [
{
"name": "release",
"configurePreset": "release",
"jobs": 4
}
]
}
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# blkchn
![GitHub last commit](https://img.shields.io/github/last-commit/allenvox/blkchn)<br>
C++ blockchain examples

**Requirements:** OpenSSL<br>
**Build:** `cmake --preset release && cmake --build --preset release`<br>
**Run:** `build/bin/blkchn`<br>
17 changes: 17 additions & 0 deletions cmake/CompileOptions.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function(set_compile_options target_name)
target_compile_options(${target_name} PRIVATE -Wall -Wextra -Werror -pedantic)
set_target_properties(
${target_name}
PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
)
if(CLANG_TIDY_EXE)
set_target_properties(
${target_name}
PROPERTIES
CXX_CLANG_TIDY ${CLANG_TIDY_EXE}
)
endif()
endfunction()
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(blkchn)
16 changes: 16 additions & 0 deletions src/blkchn/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
set(target_name blkchn)

add_executable(${target_name})
set_compile_options(${target_name})

target_sources(
${target_name}
PRIVATE
main.cpp
)

target_link_libraries(
${target_name}
OpenSSL::SSL
OpenSSL::Crypto
)
89 changes: 89 additions & 0 deletions src/blkchn/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <openssl/sha.h>

#include <ctime>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

std::string calculateHash(const std::string &data) {
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256(reinterpret_cast<const unsigned char *>(data.c_str()), data.size(),
hash);

std::stringstream ss;
for (size_t i = 0; i < SHA256_DIGEST_LENGTH; ++i) {
ss << std::hex << (int)hash[i];
}

return ss.str();
}

struct Block {
int index;
std::string data;
std::string prevHash;
std::string hash;
time_t timestamp;

std::string getSignature() {
return std::to_string(index) + std::to_string(timestamp) + data + prevHash;
}

Block(int idx, const std::string &info, const std::string &previousHash)
: index(idx), data(info), prevHash(previousHash),
timestamp(time(nullptr)) {
hash = calculateHash(getSignature());
}
};

class Blockchain {
public:
Blockchain() { chain.push_back(createGenesisBlock()); }

void addBlock(const std::string &data) {
Block newBlock(chain.size(), data, chain.back().hash);
chain.push_back(newBlock);
}

bool isChainValid() {
for (size_t i = 1; i < chain.size(); ++i) {
auto result = calculateHash(chain[i].getSignature());
auto expected = chain[i].hash;
if (expected != result) {
return false;
}
}
return true;
}

void printChain() const {
for (const auto &block : chain) {
std::cout << "Index: " << block.index
<< ", Timestamp: " << block.timestamp
<< "\nData: " << block.data << "\nHash: " << block.hash
<< "\nPrevious Hash: " << block.prevHash
<< "\n----------------\n";
}
}

private:
std::vector<Block> chain;

Block createGenesisBlock() { return Block(0, "Genesis Block", "0"); }
};

int main() {
Blockchain myBlockchain;

myBlockchain.addBlock("Alexei paid Yuriy $50");
myBlockchain.addBlock("Yuriy paid Mikhail $30");

std::cout << "Blockchain is " << (myBlockchain.isChainValid() ? "" : " not ")
<< "valid\n";

std::cout << "\nThe blockchain is:\n----------------\n";
myBlockchain.printChain();

return 0;
}

0 comments on commit 7da32c6

Please sign in to comment.