diff --git a/CMakeLists.txt b/CMakeLists.txt index d4e295a..8806d1a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.19) project(btcexplorer) set(CMAKE_CXX_STANDARD 20) +#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address") #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") diff --git a/Models/Script.cpp b/Models/Script.cpp index c93a1e9..7ee039b 100644 --- a/Models/Script.cpp +++ b/Models/Script.cpp @@ -4,7 +4,7 @@ #include #include -Script::Script(std::span data, bool coinbase) { +Script::Script(std::span data, bool coinbase): _operations(5) { for(auto iter = data.begin(); iter != data.end();) { ScriptOperation operation; operation.opCode = OpCode(*iter++); @@ -67,8 +67,8 @@ std::string Script::address() { std::span Script::publicKey() { switch (_type) { - case P2PK: return _operations[0].input; - case P2PKH: return _operations[2].input; + case P2PK: return _operations[0].input.value(); + case P2PKH: return _operations[2].input.value(); default: return {}; } } diff --git a/Models/Script.h b/Models/Script.h index e1a110f..df42a9b 100644 --- a/Models/Script.h +++ b/Models/Script.h @@ -154,8 +154,8 @@ enum ScriptType { }; struct ScriptOperation { - OpCode opCode; - std::vector input; + OpCode opCode = OP_INVALIDOPCODE; + std::optional> input = std::nullopt; }; class Script { diff --git a/main.cpp b/main.cpp index b0c9c6a..ab6f9c9 100644 --- a/main.cpp +++ b/main.cpp @@ -3,37 +3,44 @@ #include #include #include +#include +#include #include "Models/Block.h" +namespace fs = std::filesystem; + int main() { - std::string path = "/Users/selim/Documents/blk00000.dat"; //"/home/selim/dl/blk00000.dat"; - std::fstream file(path); + std::string dir = "/home/selim/dl/blocks"; // "/Users/selim/Documents/blk00000.dat"; + std::set paths; + + for (const auto & entry : fs::directory_iterator(dir)) + paths.insert(entry.path().string()); std::vector blocks; - auto start = std::chrono::high_resolution_clock::now(); - PreBlockHeader header{}; - size_t index = 0; - while (true) { - file.read((char*)&header, sizeof(header)); - header.magic = (BlockMagic)__builtin_bswap32(header.magic); + for (const auto & path: paths) { + std::fstream file(path); + std::cout << "Parsing file: " << path << std::endl; - if(file.eof()) { - break; + PreBlockHeader header{}; + size_t index = 0; + while (true) { + file.read((char*)&header, sizeof(header)); + header.magic = (BlockMagic)__builtin_bswap32(header.magic); + + if(file.eof()) { + break; + } + + auto blockData = std::make_unique(header.blockSize); + file.read((char*)blockData.get(), header.blockSize); + + blocks.emplace_back(blockData.get(), header.blockSize); + + //std::cout << "Parsed new block " << index++ << " with size: " << header.blockSize << std::endl; } - - auto blockData = std::make_unique(header.blockSize); - file.read((char*)blockData.get(), header.blockSize); - - blocks.emplace_back(blockData.get(), header.blockSize); - - //std::cout << "Parsed new block " << index++ << " with size: " << header.blockSize << std::endl; - -// if(index == 171) { -// break; -// } } auto stop = std::chrono::high_resolution_clock::now(); @@ -41,9 +48,6 @@ int main() { std::cout << "Blocks found: " << blocks.size() << std::endl; std::cout << "Duration: " << double(duration.count())/1000000 << " seconds" << std::endl; -// for(size_t i = 0; i < 100; ++i) { -// std::cout << "version: " << blocks[i].time() << std::endl; -// } return 0; }