Permalink
Browse files

Moved the coin parameters to a separate settings file so we can index…

… other chains too
  • Loading branch information...
gertjaap committed Nov 11, 2017
1 parent 70f1d1d commit f1e04443e6a6e84a3d203c7c3419bc0fb9b44871
@@ -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"]


@@ -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
@@ -0,0 +1,7 @@
{
"magic" : "fabfb5da",
"version_p2pkh" : "47",
"version_p2sh" : "05",
"prefix_bech32": "vtc"

}
@@ -0,0 +1,6 @@
{
"magic" : "76657274",
"version_p2pkh" : "4a",
"version_p2sh" : "c4",
"prefix_bech32": "tvtc"
}
@@ -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
@@ -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()) {
@@ -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);
}
@@ -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;

@@ -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());

@@ -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;
@@ -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
*/
@@ -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;
@@ -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;
@@ -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() {
@@ -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;
}
@@ -26,7 +26,7 @@
#include <fstream>

#include "blockchaintypes.h"

#include "coinparams.h"
namespace VtcBlockIndexer {

/**
@@ -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;

};

}
@@ -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);

}
@@ -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() {}
};
}
Oops, something went wrong.

0 comments on commit f1e0444

Please sign in to comment.