Skip to content

Commit

Permalink
Moved the coin parameters to a separate settings file so we can index…
Browse files Browse the repository at this point in the history
… other chains too
  • Loading branch information
gertjaap committed Nov 11, 2017
1 parent 70f1d1d commit f1e0444
Show file tree
Hide file tree
Showing 23 changed files with 1,926 additions and 73 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Expand Up @@ -6,6 +6,6 @@ ADD Makefile /root/sources/vtc-wallet-middleware-cpp/Makefile
RUN make clean -C /root/sources/vtc-wallet-middleware-cpp
RUN make -C /root/sources/vtc-wallet-middleware-cpp

CMD ["/root/sources/vtc-wallet-middleware-cpp/vtc_indexer", "/blocks"]
ENTRYPOINT ["/root/sources/vtc-wallet-middleware-cpp/vtc_indexer"]


2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -29,7 +29,7 @@ endif

PLATFORMCXXFLAGS += -g -Wall -std=c++14 -O3 -Wl,-E

INDEXERSRC = src/main.cpp src/blockfilewatcher.cpp src/byte_array_buffer.cpp src/blockscanner.cpp src/scriptsolver.cpp src/httpserver.cpp src/utility.cpp src/blockreader.cpp src/filereader.cpp src/mempoolmonitor.cpp src/blockindexer.cpp src/crypto/ripemd160.cpp src/crypto/base58.cpp src/crypto/bech32.cpp
INDEXERSRC = src/main.cpp src/blockfilewatcher.cpp src/coinparams.cpp src/byte_array_buffer.cpp src/blockscanner.cpp src/scriptsolver.cpp src/httpserver.cpp src/utility.cpp src/blockreader.cpp src/filereader.cpp src/mempoolmonitor.cpp src/blockindexer.cpp src/crypto/ripemd160.cpp src/crypto/base58.cpp src/crypto/bech32.cpp
INDEXEROBJS = $(INDEXERSRC:.cpp=.cpp.o)

INDEXERLDFLAGS = $(BINFLAGS) -lrestbed -lcrypto -ldl -pthread -lleveldb -lssl -lsecp256k1 -ljsonrpccpp-client -ljsonrpccpp-common -ljsoncpp
Expand Down
7 changes: 7 additions & 0 deletions coins/vertcoin-mainnet.json
@@ -0,0 +1,7 @@
{
"magic" : "fabfb5da",
"version_p2pkh" : "47",
"version_p2sh" : "05",
"prefix_bech32": "vtc"

}
6 changes: 6 additions & 0 deletions coins/vertcoin-testnet.json
@@ -0,0 +1,6 @@
{
"magic" : "76657274",
"version_p2pkh" : "4a",
"version_p2sh" : "c4",
"prefix_bech32": "tvtc"
}
4 changes: 3 additions & 1 deletion mainnet.yml
Expand Up @@ -23,8 +23,10 @@ services:
volumes:
- ./data/main/vertcoind/blocks:/blocks
- ./data/main/index:/index
- ./coins:/coins
command: --coinParams=/coins/vertcoin-mainnet.json

networks:
networks:
default:
external:
name: vertcoin-middleware
5 changes: 1 addition & 4 deletions src/blockfilewatcher.cpp
Expand Up @@ -92,9 +92,6 @@ void VtcBlockIndexer::BlockFileWatcher::scanBlocks(string fileName) {
while(blockScanner->moveNext()) {
this->totalBlocks++;
VtcBlockIndexer::ScannedBlock block = blockScanner->scanNextBlock();
if(block.testnet != mempoolMonitor->testnet) {
mempoolMonitor->testnet = block.testnet;
}
// Create an empty vector inside the unordered map if this previousBlockHash
// was not found before.
if(this->blocks.find(block.previousBlockHash) == this->blocks.end()) {
Expand Down Expand Up @@ -199,7 +196,7 @@ string VtcBlockIndexer::BlockFileWatcher::processNextBlock(string prevBlockHash)
}

if(!blockIndexer->hasIndexedBlock(bestBlock.blockHash, this->blockHeight)) {
VtcBlockIndexer::Block fullBlock = blockReader->readBlock(bestBlock.fileName, bestBlock.filePosition, this->blockHeight, bestBlock.testnet, false);
VtcBlockIndexer::Block fullBlock = blockReader->readBlock(bestBlock.fileName, bestBlock.filePosition, this->blockHeight, false);

blockIndexer->indexBlock(fullBlock);
}
Expand Down
4 changes: 1 addition & 3 deletions src/blockindexer.cpp
Expand Up @@ -108,8 +108,6 @@ bool VtcBlockIndexer::BlockIndexer::hasIndexedBlock(string blockHash, int blockH
}

bool VtcBlockIndexer::BlockIndexer::indexBlock(Block block) {
this->scriptSolver->testnet = block.testnet;

stringstream ss;
ss << "block-" << setw(8) << setfill('0') << block.height;

Expand Down Expand Up @@ -142,7 +140,7 @@ bool VtcBlockIndexer::BlockIndexer::indexBlock(Block block) {
stringstream ssBlockFilePositionKey;
ssBlockFilePositionKey << "block-filePosition-" << setw(8) << setfill('0') << block.height;
stringstream ssBlockFilePositionValue;
ssBlockFilePositionValue << block.fileName << setw(12) << setfill('0') << block.filePosition << (block.testnet ? 1 : 0);
ssBlockFilePositionValue << block.fileName << setw(12) << setfill('0') << block.filePosition;

this->db->Put(leveldb::WriteOptions(), ssBlockFilePositionKey.str(), ssBlockFilePositionValue.str());

Expand Down
3 changes: 1 addition & 2 deletions src/blockreader.cpp
Expand Up @@ -48,13 +48,12 @@ std::vector<unsigned char> VtcBlockIndexer::BlockReader::readRawBlockHeader(stri
}


VtcBlockIndexer::Block VtcBlockIndexer::BlockReader::readBlock(string fileName, uint64_t filePosition, uint64_t blockHeight, bool testnet, bool headerOnly) {
VtcBlockIndexer::Block VtcBlockIndexer::BlockReader::readBlock(string fileName, uint64_t filePosition, uint64_t blockHeight, bool headerOnly) {
VtcBlockIndexer::Block fullBlock;

fullBlock.fileName = fileName;
fullBlock.filePosition = filePosition;
fullBlock.height = blockHeight;
fullBlock.testnet = testnet;

stringstream ss;
ss << blocksDir << "/" << fileName;
Expand Down
2 changes: 1 addition & 1 deletion src/blockreader.h
Expand Up @@ -44,7 +44,7 @@ class BlockReader {

/** Reads the contents of the block that was scanned
*/
Block readBlock(std::string fileName, uint64_t filePosition, uint64_t blockHeight, bool testnet, bool headerOnly);
Block readBlock(std::string fileName, uint64_t filePosition, uint64_t blockHeight, bool headerOnly);

/** Reads a transaction from an open stream
*/
Expand Down
21 changes: 4 additions & 17 deletions src/blockscanner.cpp
Expand Up @@ -25,8 +25,6 @@
#include <string>
#include <iomanip>

const char magic[] = "\xfa\xbf\xb5\xda";
const char magicTestnet[] = "\x76\x65\x72\x74";

VtcBlockIndexer::BlockScanner::BlockScanner(const std::string blocksDir, const std::string blockFileName) {
std::stringstream ss;
Expand All @@ -47,8 +45,8 @@ bool VtcBlockIndexer::BlockScanner::close() {
}

bool VtcBlockIndexer::BlockScanner::moveNext() {
std::unique_ptr<char> buffer(new char[4]);
this->blockFileStream.read(buffer.get(), 4);
std::vector<unsigned char> buffer(4);
this->blockFileStream.read(reinterpret_cast<char *>(&buffer[0]), 4);

if(this->blockFileStream.eof()) {
return false;
Expand All @@ -58,17 +56,7 @@ bool VtcBlockIndexer::BlockScanner::moveNext() {
return false;
}

bool magicMatch = (memcmp(buffer.get(), magic, 4) == 0);
if(!magicMatch) {
magicMatch = (memcmp(buffer.get(), magicTestnet, 4) == 0);
if(magicMatch) {
this->testnet = true;
}
} else {
this->testnet = false;
}

return (magicMatch);
return std::equal(buffer.begin(), buffer.end(), VtcBlockIndexer::CoinParams::magic.begin());
}

VtcBlockIndexer::ScannedBlock VtcBlockIndexer::BlockScanner::scanNextBlock() {
Expand All @@ -91,7 +79,6 @@ VtcBlockIndexer::ScannedBlock VtcBlockIndexer::BlockScanner::scanNextBlock() {
block.previousBlockHash = VtcBlockIndexer::Utility::hashToReverseHex(previousBlockHash);

this->blockFileStream.seekg(blockSize - 80, std::ios_base::cur);

block.testnet = this->testnet;

return block;
}
5 changes: 1 addition & 4 deletions src/blockscanner.h
Expand Up @@ -26,7 +26,7 @@
#include <fstream>

#include "blockchaintypes.h"

#include "coinparams.h"
namespace VtcBlockIndexer {

/**
Expand Down Expand Up @@ -82,9 +82,6 @@ class BlockScanner {
*/
std::string blockFileName;

/** When the scanner finds magic bytes from testnet it will toggle this to true */
bool testnet;

};

}
Expand Down
51 changes: 51 additions & 0 deletions src/coinparams.cpp
@@ -0,0 +1,51 @@
#include "coinparams.h"
/* VTC Blockindexer - A utility to build additional indexes to the
Vertcoin blockchain by scanning and indexing the blockfiles
downloaded by Vertcoin Core.
Copyright (C) 2017 Gert-Jaap Glasbergen
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


#include <iostream>
#include <fstream>
#include "json.hpp"
#include "utility.h"
using json = nlohmann::json;
using namespace std;

vector<unsigned char> VtcBlockIndexer::CoinParams::magic;
string VtcBlockIndexer::CoinParams::bech32Prefix;
unsigned char VtcBlockIndexer::CoinParams::p2pkhVersion;
unsigned char VtcBlockIndexer::CoinParams::p2shVersion;

void VtcBlockIndexer::CoinParams::readFromFile(string fileName)
{
ifstream i(fileName);
json j;
i >> j;

assert(j["magic"].is_string());
assert(j["prefix_bech32"].is_string());
assert(j["version_p2sh"].is_string());
assert(j["version_p2pkh"].is_string());

magic = VtcBlockIndexer::Utility::hexToBytes(j["magic"].get<string>());
bech32Prefix = j["prefix_bech32"].get<string>();
p2shVersion = (unsigned char) strtol(j["version_p2sh"].get<string>().c_str(), NULL, 16);
p2pkhVersion = (unsigned char) strtol(j["version_p2pkh"].get<string>().c_str(), NULL, 16);

}
45 changes: 45 additions & 0 deletions src/coinparams.h
@@ -0,0 +1,45 @@
/* VTC Blockindexer - A utility to build additional indexes to the
Vertcoin blockchain by scanning and indexing the blockfiles
downloaded by Vertcoin Core.
Copyright (C) 2017 Gert-Jaap Glasbergen
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <vector>
#include <string>

using namespace std;

namespace VtcBlockIndexer {

/**
* The CoinParms class provides static access to the coin parameters and a method
* to read them from a JSON file
*/

class CoinParams {
public:
static void readFromFile(string fileName);
static vector<unsigned char> magic;
static string bech32Prefix;
static unsigned char p2pkhVersion;
static unsigned char p2shVersion;
~CoinParams();

private:
CoinParams() {}
};
}

0 comments on commit f1e0444

Please sign in to comment.