-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
Language: Cpp | ||
BasedOnStyle: LLVM | ||
... | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.vscode | ||
build | ||
bin | ||
obj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_subdirectory(blkchn) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |