Skip to content
Permalink
Browse files

support ckb-0.22.rc2

  • Loading branch information...
BTC.com
BTC.com committed Oct 4, 2019
1 parent 86effa9 commit bb62a3b11f77c02d22ca758b3f79c3460c550b40
@@ -0,0 +1,150 @@
#include "hextodec.h"
#include <stdexcept>
#include <algorithm>


const char* BaseConverter::binarySet_ = "01";
const char* BaseConverter::decimalSet_ = "0123456789";
const char* BaseConverter::hexSet_ = "0123456789ABCDEF";

BaseConverter::BaseConverter(const std::string& sourceBaseSet, const std::string& targetBaseSet)
: sourceBaseSet_(sourceBaseSet)
, targetBaseSet_(targetBaseSet)
{
if (sourceBaseSet.empty() || targetBaseSet.empty())
throw std::invalid_argument("Invalid base character set");
}

const BaseConverter& BaseConverter::DecimalToBinaryConverter()
{
static const BaseConverter dec2bin(decimalSet_, binarySet_);
return dec2bin;
}

const BaseConverter& BaseConverter::BinaryToDecimalConverter()
{
static const BaseConverter bin2dec(binarySet_, decimalSet_);
return bin2dec;
}

const BaseConverter& BaseConverter::DecimalToHexConverter()
{
static const BaseConverter dec2hex(decimalSet_, hexSet_);
return dec2hex;
}

const BaseConverter& BaseConverter::HexToDecimalConverter()
{
static const BaseConverter hex2dec(hexSet_, decimalSet_);
return hex2dec;
}

std::string BaseConverter::Convert(std::string value) const
{
unsigned int numberBase = GetTargetBase();
std::string result;

do
{
unsigned int remainder = divide(sourceBaseSet_, value, numberBase);
result.push_back(targetBaseSet_[remainder]);
}
while (!value.empty() && !(value.length() == 1 && value[0] == sourceBaseSet_[0]));

std::reverse(result.begin(), result.end());
return result;
}

std::string BaseConverter::Convert(const std::string& value, size_t minDigits) const
{
std::string result = Convert(value);
if (result.length() < minDigits)
return std::string(minDigits - result.length(), targetBaseSet_[0]) + result;
else
return result;
}

std::string BaseConverter::FromDecimal(unsigned int value) const
{
return dec2base(targetBaseSet_, value);
}

std::string BaseConverter::FromDecimal(unsigned int value, size_t minDigits) const
{
std::string result = FromDecimal(value);
if (result.length() < minDigits)
return std::string(minDigits - result.length(), targetBaseSet_[0]) + result;
else
return result;
}

unsigned int BaseConverter::ToDecimal(std::string value) const
{
return base2dec(sourceBaseSet_, value);
}

unsigned int BaseConverter::divide(const std::string& baseDigits, std::string& x, unsigned int y)
{
std::string quotient;

size_t lenght = x.length();
for (size_t i = 0; i < lenght; ++i)
{
size_t j = i + 1 + x.length() - lenght;
if (x.length() < j)
break;

unsigned int value = base2dec(baseDigits, x.substr(0, j));

quotient.push_back(baseDigits[value / y]);
x = dec2base(baseDigits, value % y) + x.substr(j);
}

// calculate remainder
unsigned int remainder = base2dec(baseDigits, x);

// remove leading "zeros" from quotient and store in 'x'
size_t n = quotient.find_first_not_of(baseDigits[0]);
if (n != std::string::npos)
{
x = quotient.substr(n);
}
else
{
x.clear();
}

return remainder;
}

std::string BaseConverter::dec2base(const std::string& baseDigits, unsigned int value)
{
unsigned int numberBase = (unsigned int)baseDigits.length();
std::string result;
do
{
result.push_back(baseDigits[value % numberBase]);
value /= numberBase;
}
while (value > 0);

std::reverse(result.begin(), result.end());
return result;
}

unsigned int BaseConverter::base2dec(const std::string& baseDigits, const std::string& value)
{
unsigned int numberBase = (unsigned int)baseDigits.length();
unsigned int result = 0;
for (size_t i = 0; i < value.length(); ++i)
{
result *= numberBase;
int c = baseDigits.find(value[i]);
if (c == std::string::npos)
throw std::runtime_error("Invalid character");

result += (unsigned int)c;
}

return result;
}
@@ -0,0 +1,46 @@
#include <string>

class BaseConverter
{
public:
std::string GetSourceBaseSet() const { return sourceBaseSet_; }
std::string GetTargetBaseSet() const { return targetBaseSet_; }
unsigned int GetSourceBase() const { return (unsigned int)sourceBaseSet_.length(); }
unsigned int GetTargetBase() const { return (unsigned int)targetBaseSet_.length(); }

BaseConverter(const std::string& sourceBaseSet, const std::string& targetBaseSet);

static const BaseConverter& DecimalToBinaryConverter();

static const BaseConverter& BinaryToDecimalConverter();
static const BaseConverter& DecimalToHexConverter();
static const BaseConverter& HexToDecimalConverter();

std::string Convert(std::string value) const;


std::string Convert(const std::string& value, size_t minDigits) const;

std::string FromDecimal(unsigned int value) const;

std::string FromDecimal(unsigned int value, size_t minDigits) const;

unsigned int ToDecimal(std::string value) const;

private:
static unsigned int divide(const std::string& baseDigits,
std::string& x,
unsigned int y);

static unsigned int base2dec(const std::string& baseDigits,
const std::string& value);

static std::string dec2base(const std::string& baseDigits, unsigned int value);

private:
static const char* binarySet_;
static const char* decimalSet_;
static const char* hexSet_;
std::string sourceBaseSet_;
std::string targetBaseSet_;
};
@@ -453,6 +453,7 @@ include_directories(
3rdparty
3rdparty/equihash
3rdparty/eaglesong
3rdparty/hextodec
src
test
${CHAIN_SRC_ROOT}/src
@@ -513,6 +514,9 @@ file(GLOB LIB_SOURCES
add_library(eaglesong
3rdparty/eaglesong/eaglesong.cc)

add_library(hextodec
3rdparty/hextodec/hextodec.cc)

file(GLOB LIB_SOURCES_CHAINS
src/eth/*.cc
src/bytom/*.cc
@@ -546,7 +550,7 @@ add_library(
${LIB_SOURCES_PROMETHEUS}
${LIB_SOURCES_SHARES}
${LIB_SOURCES_CHAINS})
target_link_libraries(btcpool PUBLIC beam cuckoo eaglesong)
target_link_libraries(btcpool PUBLIC beam cuckoo eaglesong hextodec)
target_link_libraries(btcpool PUBLIC rlpvalue)

if(BUILD_TESTING)
@@ -8,7 +8,7 @@

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
# Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk
@@ -7,8 +7,8 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ckb-jsonrpc-types = { git = "https://github.com/nervosnetwork/ckb", tag = "v0.22.0-rc1" }
ckb-types = { git = "https://github.com/nervosnetwork/ckb", tag = "v0.22.0-rc1" }
ckb-jsonrpc-types = { git = "https://github.com/nervosnetwork/ckb", tag = "v0.22.0-rc2" }
ckb-types = { git = "https://github.com/nervosnetwork/ckb", tag = "v0.22.0-rc2" }
chrono = "0.4"
env_logger = "0.6"
failure = "0.1"
@@ -30,7 +30,7 @@ impl From<BlockTemplate> for MiningJob {
let work_id = block_template.work_id.into();
let height = block_template.number.into();
let timestamp = block_template.current_time.into();
let compact_target = block_template.compact_target.clone();
let compact_target = block_template.compact_target.into();
let parent_hash = block_template.parent_hash.clone();
let block: Block = block_template.into();
let (target, _,) =
@@ -59,7 +59,7 @@ pub struct SolvedShare {
pub timestamp: u64,
pub pow_hash: H256,
pub target: U256,
pub nonce: u64,
pub nonce: u128,
pub job_id: u64,
#[serde(rename = "userId")]
pub user_id: i32,
@@ -4,7 +4,7 @@ use std::{str, thread};
use chrono::prelude::*;
use ckb_jsonrpc_types::BlockTemplate;
use ckb_types::prelude::{Builder, Entity, Pack};
use ckb_types::H256;
use ckb_types::{H256};
use failure::Error;
use futures::future::{loop_fn, Loop};
use futures::sync::mpsc::{channel, Sender};
@@ -156,9 +156,9 @@ fn create_block_record(
height: u64,
block_hash: &H256,
pow_hash: &H256,
nonce: u64,
nonce: u128,
parent_hash: &H256,
difficulty: u32,
compact_target: &u32,
) {
let stmt = format!(
"INSERT INTO `found_blocks` (`puid`,`worker_id`,`worker_full_name`,`height`,`hash`,`hash_no_nonce`,`nonce`,`prev_hash`,`network_diff`,`created_at`) VALUES({},{},'0x{}',{},'0x{}','0x{}','0x{:016x}','0x{}',{},'{}')",
@@ -170,7 +170,7 @@ fn create_block_record(
pow_hash,
nonce,
parent_hash,
difficulty,
compact_target,
Utc::now().format("%F %T"),
);
execute_query(url, stmt);
@@ -214,7 +214,7 @@ fn submit_block(rpc_client: Client, job: &MiningJob, solved_share: &SolvedShare,
&solved_share.pow_hash,
solved_share.nonce,
&job.parent_hash,
job.compact_target,
&job.compact_target,
);

info!("Submitting block {}", &hash);
@@ -26,18 +26,6 @@
#include "eaglesong/eaglesong.h"
#include "utilstrencodings.h"
#include <iostream>
arith_uint256 CKB::GetEaglesongHash(uint256 pow_hash, uint64_t nonce) {
std::reverse(pow_hash.begin(), pow_hash.end());
boost::endian::little_uint64_buf_t nonce_t(nonce);
uint8_t input[40] = {0};
uint8_t output[32] = {0};
std::string hash_s;
memcpy(input, (uint8_t *)(&nonce_t), sizeof(nonce_t));
memcpy(input + 8, pow_hash.begin(), 32);
EaglesongHash(output, input, 40);
Bin2Hex(output, 32, hash_s);
return UintToArith256(uint256S(hash_s.c_str()));
}

arith_uint256
CKB::GetEaglesongHash2(uint256 pow_hash, uint64_t nonce) { // 3e29d5eaf71970c0
@@ -47,7 +35,18 @@ CKB::GetEaglesongHash2(uint256 pow_hash, uint64_t nonce) { // 3e29d5eaf71970c0
std::string hash_s = nonce_s + pow_hash.GetHex();
std::vector<char> hashvec;
Hex2Bin(hash_s.c_str(), hashvec);

EaglesongHash(output, (uint8_t *)(hashvec.data()), 40);
Bin2Hex(output, 32, hash_s);
return UintToArith256(uint256S(hash_s.c_str()));
}

arith_uint256 CKB::GetEaglesongHash128(uint256 pow_hash, string nonce) {
uint8_t output[32] = {0};
std::string hash_s = pow_hash.GetHex() + nonce; // 32 + 16
std::vector<char> hashvec;
Hex2Bin(hash_s.c_str(), hashvec);
EaglesongHash(output, (uint8_t *)(hashvec.data()), 48);
Bin2Hex(output, 32, hash_s);
return UintToArith256(uint256S(hash_s.c_str()));
}
@@ -31,6 +31,6 @@
using CkbDifficulty = Difficulty<0x2100ffff>;

namespace CKB {
arith_uint256 GetEaglesongHash(uint256 pow_hash, uint64_t nonce);
arith_uint256 GetEaglesongHash2(uint256 pow_hash, uint64_t nonce);
arith_uint256 GetEaglesongHash128(uint256 pow_hash, std::string nonce);
} // namespace CKB
@@ -42,15 +42,14 @@ bool JobMakerHandlerCkb::processMsg(const string &msg) {
DLOG(INFO) << "key already exist in workMap: " << key;
return false;
}
jobid2work_.insert({job->jobId_, job});

std::pair<std::map<uint64_t, shared_ptr<StratumJobCkb>>::iterator, bool> ret;
ret = workMap_.insert(std::make_pair(key, job));
if (!ret.second) {
DLOG(INFO) << "insert key into workMap failed: " << key;
return false;
}

jobid2work_.insert(std::make_pair(job->jobId_, job));
clearTimeoutMsg();

if (job->height_ < lastReceivedHeight_) {
@@ -90,9 +89,9 @@ void JobMakerHandlerCkb::clearTimeoutMsg() {
<< ", height:" << height
<< ", headerHash:" << itr->second->pow_hash_;

jobid2work_.erase(jobid2work_.find(itr->second->jobId_));
// c++11: returns an iterator to the next element in the map
itr = workMap_.erase(itr);
jobid2work_.erase(jobid2work_.find(itr->second->jobId_));
}
}
}
@@ -103,6 +102,8 @@ string JobMakerHandlerCkb::makeStratumJobMsg() {
}

shared_ptr<StratumJobCkb> sjob = jobid2work_.rbegin()->second;
DLOG(INFO) << "send job : " << sjob->jobId_
<< "job pow_hash : " << sjob->pow_hash_;
DLOG(INFO) << "sjob :" << sjob->serializeToJson();
return sjob->serializeToJson();
}

0 comments on commit bb62a3b

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