Skip to content
Permalink
Browse files

tools/sharelog_to_parquet: support BCH/UBTC/BSV/LTC/ZEC/ETC

  • Loading branch information
SwimmingTiger committed Nov 5, 2019
1 parent 7c2b4b5 commit e037e20af87a92cea98625f78045ce5d3f69453b
@@ -33,19 +33,23 @@
#include <string>

void BitsToTarget(uint32_t bits, uint256 &target) {
// The previous sharelog stored arith_uint256::bits() in the bits_reached field.
// The previous sharelog stored arith_uint256::bits() in the bits_reached
// field.
if (bits <= 0xffL) {
target = ArithToUint256((arith_uint256("1") << bits) - 1);
return;
}

// The new sharelog will store arith_uint256::GetCompact() to improve precision.
// The new sharelog will store arith_uint256::GetCompact() to improve
// precision.
target = ArithToUint256(arith_uint256{}.SetCompact(bits));
}

template <uint32_t DiffOneBits, size_t TableSize = 64>
struct Difficulty {
static const uint64_t GetDiffOneBits() { return DiffOneBits; }
struct BitcoinDifficulty {
static uint32_t DiffOneBits;
static constexpr size_t TableSize = 64;

static const uint32_t GetDiffOneBits() { return DiffOneBits; }

static const arith_uint256 &GetDiffOneTarget() {
static const auto DiffOneTarget = arith_uint256{}.SetCompact(DiffOneBits);
@@ -79,7 +83,7 @@ struct Difficulty {
static void
DiffToTarget(uint64_t diff, uint256 &target, bool useTable = true) {
static const auto MaxTarget = uint256S(
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
if (diff == 0) {
target = MaxTarget;
return;
@@ -100,43 +104,51 @@ struct Difficulty {
}

static void BitsToDifficulty(uint32_t bits, double *difficulty) {
// The previous sharelog stored arith_uint256::bits() in the bits_reached field.
// The previous sharelog stored arith_uint256::bits() in the bits_reached
// field.
if (bits <= 0xffL) {
arith_uint256 target = (arith_uint256("1") << bits) - 1;
difficulty = GetDiffOneTarget().getdouble() / target.getdouble();
*difficulty = GetDiffOneTarget().getdouble() / target.getdouble();
return;
}

// The new sharelog will store arith_uint256::GetCompact() to improve precision.
// The new sharelog will store arith_uint256::GetCompact() to improve
// precision.
arith_uint256 target;
target.SetCompact(bits);
*difficulty = GetDiffOneTarget().getdouble() / target.getdouble();
}

static void BitsToDifficulty(uint32_t bits, uint64_t *difficulty) {
// The previous sharelog stored arith_uint256::bits() in the bits_reached field.
// The previous sharelog stored arith_uint256::bits() in the bits_reached
// field.
if (bits <= 0xffL) {
arith_uint256 target = (arith_uint256("1") << bits) - 1;
difficulty = (GetDiffOneTarget() / target).GetLow64();
*difficulty = (GetDiffOneTarget() / target).GetLow64();
return;
}

// The new sharelog will store arith_uint256::GetCompact() to improve precision.
// The new sharelog will store arith_uint256::GetCompact() to improve
// precision.
arith_uint256 target;
target.SetCompact(bits);
*difficulty = (GetDiffOneTarget() / target).GetLow64();
}

static double BitsToDifficulty(uint32_t bits) {
// The previous sharelog stored arith_uint256::bits() in the bits_reached field.
// The previous sharelog stored arith_uint256::bits() in the bits_reached
// field.
if (bits <= 0xffL) {
arith_uint256 target = (arith_uint256("1") << bits) - 1;
return GetDiffOneTarget().getdouble() / target.getdouble();
}

// The new sharelog will store arith_uint256::GetCompact() to improve precision.
// The new sharelog will store arith_uint256::GetCompact() to improve
// precision.
arith_uint256 target;
target.SetCompact(bits);
return GetDiffOneTarget().getdouble() / target.getdouble();
}
};

uint32_t BitcoinDifficulty::DiffOneBits = 0;
@@ -81,7 +81,8 @@ class ShareLogParserT : public ShareLogParser {
bool openParquet();

public:
ShareLogParserT(const libconfig::Config &cfg, time_t timestamp);
ShareLogParserT(
const libconfig::Config &cfg, time_t timestamp, const string &chainType);
virtual ~ShareLogParserT();

bool init();
@@ -131,7 +132,7 @@ class ShareLogParserServerT : public ShareLogParserServer {
void trySwitchBinFile(shared_ptr<ShareLogParserT<SHARE>> shareLogParser);

public:
ShareLogParserServerT(const libconfig::Config &cfg);
ShareLogParserServerT(const libconfig::Config &cfg, const string &chainType);
virtual ~ShareLogParserServerT();

void stop();
@@ -29,10 +29,10 @@
/////////////////////////////// ShareLogParserT ///////////////////////////////
template <class SHARE>
ShareLogParserT<SHARE>::ShareLogParserT(
const libconfig::Config &cfg, time_t timestamp)
const libconfig::Config &cfg, time_t timestamp, const string &chainType)
: date_(timestamp)
, outputDir_(cfg.lookup("parquet.data_dir").operator string())
, chainType_(cfg.lookup("sharelog.chain_type").operator string())
, chainType_(chainType)
, f_(nullptr)
, buf_(nullptr)
, incompleteShareSize_(0) {
@@ -291,10 +291,10 @@ bool ShareLogParserT<SHARE>::isReachEOF() {
///////////////////////////////
template <class SHARE>
ShareLogParserServerT<SHARE>::ShareLogParserServerT(
const libconfig::Config &cfg)
const libconfig::Config &cfg, const string &chainType)
: cfg_(cfg)
, running_(true)
, chainType_(cfg.lookup("sharelog.chain_type").operator string())
, chainType_(chainType)
, dataDir_(cfg.lookup("sharelog.data_dir").operator string()) {
const time_t now = time(nullptr);
date_ = now - (now % 86400);
@@ -336,7 +336,7 @@ bool ShareLogParserServerT<SHARE>::initShareLogParser(time_t datets) {
template <class SHARE>
shared_ptr<ShareLogParserT<SHARE>>
ShareLogParserServerT<SHARE>::createShareLogParser(time_t datets) {
return std::make_shared<ShareLogParserT<SHARE>>(cfg_, datets);
return std::make_shared<ShareLogParserT<SHARE>>(cfg_, datets, chainType_);
}

template <class SHARE>
@@ -294,7 +294,7 @@ class ParquetWriterT<ShareBeam> : public ParquetWriter {
GroupNode::Make("share_beam", Repetition::REQUIRED, fields));
}

void flushShares() {
void flushShares() override {
DLOG(INFO) << "flush " << shareNum_ << " shares";

// Create a RowGroupWriter instance
@@ -31,7 +31,6 @@
#include "StratumStatus.h"
#include "Difficulty.hpp"

using BitcoinDifficulty = Difficulty<0x1d00ffff>;
using namespace std;

struct ShareBitcoinBytesV1 {
@@ -363,7 +362,7 @@ class ParquetWriterT<ShareBitcoin> : public ParquetWriter {
GroupNode::Make("share_bitcoin", Repetition::REQUIRED, fields));
}

void flushShares() {
void flushShares() override {
DLOG(INFO) << "flush " << shareNum_ << " shares";

// Create a RowGroupWriter instance
@@ -346,7 +346,7 @@ class ParquetWriterT<ShareEth> : public ParquetWriter {
GroupNode::Make("share_eth", Repetition::REQUIRED, fields));
}

void flushShares() {
void flushShares() override {
DLOG(INFO) << "flush " << shareNum_ << " shares";

// Create a RowGroupWriter instance
@@ -37,8 +37,6 @@
#include "utils.hpp"
#include "shares.hpp"
#include "ShareLogParser.h"
#include "StratumBitcoin.hpp"
#include "StratumBeam.hpp"

using namespace std;
using namespace libconfig;
@@ -66,12 +64,20 @@ void usage() {

std::shared_ptr<ShareLogParser> newShareLogParser(
const string &chainType, time_t timestamp, const libconfig::Config &cfg) {
if (chainType == "BTC") {
return std::make_shared<ShareLogParserBitcoin>(cfg, timestamp);
if (chainType == "BTC" || chainType == "BCH" || chainType == "BSV" ||
chainType == "UBTC") {
BitcoinDifficulty::DiffOneBits = 0x1d00ffff;
return std::make_shared<ShareLogParserBitcoin>(cfg, timestamp, chainType);
} else if (chainType == "LTC") {
BitcoinDifficulty::DiffOneBits = 0x1f00ffff;
return std::make_shared<ShareLogParserBitcoin>(cfg, timestamp, chainType);
} else if (chainType == "ZEC") {
BitcoinDifficulty::DiffOneBits = 0x1f07ffff;
return std::make_shared<ShareLogParserBitcoin>(cfg, timestamp, chainType);
} else if (chainType == "BEAM") {
return std::make_shared<ShareLogParserBeam>(cfg, timestamp);
} else if (chainType == "ETH") {
return std::make_shared<ShareLogParserEth>(cfg, timestamp);
return std::make_shared<ShareLogParserBeam>(cfg, timestamp, chainType);
} else if (chainType == "ETH" || chainType == "ETC") {
return std::make_shared<ShareLogParserEth>(cfg, timestamp, "ETH");
} else {
LOG(FATAL) << "newShareLogParser: unknown chain type " << chainType;
return nullptr;
@@ -80,12 +86,20 @@ std::shared_ptr<ShareLogParser> newShareLogParser(

std::shared_ptr<ShareLogParserServer>
newShareLogParserServer(const string &chainType, const libconfig::Config &cfg) {
if (chainType == "BTC") {
return std::make_shared<ShareLogParserServerBitcoin>(cfg);
if (chainType == "BTC" || chainType == "BCH" || chainType == "BSV" ||
chainType == "UBTC") {
BitcoinDifficulty::DiffOneBits = 0x1d00ffff;
return std::make_shared<ShareLogParserServerBitcoin>(cfg, chainType);
} else if (chainType == "LTC") {
BitcoinDifficulty::DiffOneBits = 0x1f00ffff;
return std::make_shared<ShareLogParserServerBitcoin>(cfg, chainType);
} else if (chainType == "ZEC") {
BitcoinDifficulty::DiffOneBits = 0x1f07ffff;
return std::make_shared<ShareLogParserServerBitcoin>(cfg, chainType);
} else if (chainType == "BEAM") {
return std::make_shared<ShareLogParserServerBeam>(cfg);
} else if (chainType == "ETH") {
return std::make_shared<ShareLogParserServerEth>(cfg);
return std::make_shared<ShareLogParserServerBeam>(cfg, chainType);
} else if (chainType == "ETH" || chainType == "ETC") {
return std::make_shared<ShareLogParserServerEth>(cfg, "ETH");
} else {
LOG(FATAL) << "newShareLogParserServer: unknown chain type " << chainType;
return nullptr;
No changes.

0 comments on commit e037e20

Please sign in to comment.
You can’t perform that action at this time.