Clear some bugs + support mining just a v2 coin
This commit is contained in:
85
src/main.cc
85
src/main.cc
@@ -38,7 +38,7 @@ blobdata uint64be_to_blob(uint64_t num) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
static bool fillExtra(cryptonote::block& block1, const cryptonote::block& block2) {
|
static bool fillExtra(cryptonote::block& block1, const cryptonote::block& block2) {
|
||||||
cryptonote::tx_extra_merge_mining_tag mm_tag;
|
cryptonote::tx_extra_merge_mining_tag mm_tag;
|
||||||
mm_tag.depth = 0;
|
mm_tag.depth = 0;
|
||||||
@@ -49,62 +49,51 @@ static bool fillExtra(cryptonote::block& block1, const cryptonote::block& block2
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
static bool fillExtra(cryptonote::block& block1, const cryptonote::block& block2)
|
static bool fillExtraMM(cryptonote::block& block1, const cryptonote::block& block2)
|
||||||
{
|
{
|
||||||
if (block2.timestamp > block1.timestamp) { // get the most recent timestamp (solve duplicated timestamps on child coin)
|
if (block2.timestamp > block1.timestamp) { // get the most recent timestamp (solve duplicated timestamps on child coin)
|
||||||
block1.timestamp = block2.timestamp;
|
block1.timestamp = block2.timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (BLOB_TYPE_CRYPTONOTE == block2.blob_type) { // Insert merged mining tag and block header hash into the miner extra
|
size_t MERGE_MINING_TAG_RESERVED_SIZE_EX = MERGE_MINING_TAG_RESERVED_SIZE + POOL_NONCE_SIZE;
|
||||||
size_t MERGE_MINING_TAG_RESERVED_SIZE_EX = MERGE_MINING_TAG_RESERVED_SIZE + POOL_NONCE_SIZE;
|
std::vector<uint8_t>& extra = block1.miner_tx.extra;
|
||||||
std::vector<uint8_t>& extra = block1.miner_tx.extra;
|
std::string extraAsString(reinterpret_cast<const char*>(extra.data()), extra.size());
|
||||||
std::string extraAsString(reinterpret_cast<const char*>(extra.data()), extra.size());
|
|
||||||
|
|
||||||
std::string extraNonceTemplate;
|
std::string extraNonceTemplate;
|
||||||
extraNonceTemplate.push_back(TX_EXTRA_NONCE);
|
extraNonceTemplate.push_back(TX_EXTRA_NONCE);
|
||||||
extraNonceTemplate.push_back(MERGE_MINING_TAG_RESERVED_SIZE_EX);
|
extraNonceTemplate.push_back(MERGE_MINING_TAG_RESERVED_SIZE_EX);
|
||||||
extraNonceTemplate.append(MERGE_MINING_TAG_RESERVED_SIZE, '\0');
|
extraNonceTemplate.append(MERGE_MINING_TAG_RESERVED_SIZE, '\0');
|
||||||
|
|
||||||
size_t extraNoncePos = extraAsString.find(extraNonceTemplate);
|
size_t extraNoncePos = extraAsString.find(extraNonceTemplate);
|
||||||
if (std::string::npos == extraNoncePos) {
|
if (std::string::npos == extraNoncePos) {
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
cryptonote::tx_extra_merge_mining_tag tag;
|
|
||||||
tag.depth = 0;
|
|
||||||
if (!cryptonote::get_block_header_hash(block2, tag.merkle_root)) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cryptonote::tx_extra_merge_mining_tag tag;
|
||||||
std::vector<uint8_t> extraNonceReplacement;
|
tag.depth = 0;
|
||||||
if (!cryptonote::append_mm_tag_to_extra(extraNonceReplacement, tag)) {
|
if (!cryptonote::get_block_header_hash(block2, tag.merkle_root)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (MERGE_MINING_TAG_RESERVED_SIZE < extraNonceReplacement.size()) {
|
std::vector<uint8_t> extraNonceReplacement;
|
||||||
|
if (!cryptonote::append_mm_tag_to_extra(extraNonceReplacement, tag)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t diff = (extraNonceTemplate.size() + POOL_NONCE_SIZE) - extraNonceReplacement.size();
|
if (MERGE_MINING_TAG_RESERVED_SIZE < extraNonceReplacement.size()) {
|
||||||
if (0 < diff) {
|
return false;
|
||||||
extraNonceReplacement.push_back(TX_EXTRA_NONCE);
|
}
|
||||||
extraNonceReplacement.push_back(static_cast<uint8_t>(diff - 2));
|
|
||||||
}
|
|
||||||
|
|
||||||
std::copy(extraNonceReplacement.begin(), extraNonceReplacement.end(), extra.begin() + extraNoncePos);
|
size_t diff = (extraNonceTemplate.size() + POOL_NONCE_SIZE) - extraNonceReplacement.size();
|
||||||
|
if (0 < diff) {
|
||||||
|
extraNonceReplacement.push_back(TX_EXTRA_NONCE);
|
||||||
|
extraNonceReplacement.push_back(static_cast<uint8_t>(diff - 2));
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
std::copy(extraNonceReplacement.begin(), extraNonceReplacement.end(), extra.begin() + extraNoncePos);
|
||||||
cryptonote::tx_extra_merge_mining_tag mm_tag;
|
|
||||||
mm_tag.depth = 0;
|
|
||||||
if (!cryptonote::get_block_header_hash(block2, mm_tag.merkle_root)) return false;
|
|
||||||
|
|
||||||
block1.miner_tx.extra.clear();
|
|
||||||
if (!cryptonote::append_mm_tag_to_extra(block1.miner_tx.extra, mm_tag)) return false;
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -174,7 +163,7 @@ NAN_METHOD(convert_blob) {
|
|||||||
if (!parse_and_validate_block_from_blob(input, b)) return THROW_ERROR_EXCEPTION("Failed to parse block");
|
if (!parse_and_validate_block_from_blob(input, b)) return THROW_ERROR_EXCEPTION("Failed to parse block");
|
||||||
|
|
||||||
block b2 = AUTO_VAL_INIT(b2);
|
block b2 = AUTO_VAL_INIT(b2);
|
||||||
if (info.Length() > 2) { // MM
|
if (info.Length() > 2 && !info[2]->IsNumber()) { // MM
|
||||||
b2.set_blob_type(BLOB_TYPE_FORKNOTE2); // Only forknote 2 blob types support being mm as child coin
|
b2.set_blob_type(BLOB_TYPE_FORKNOTE2); // Only forknote 2 blob types support being mm as child coin
|
||||||
Local<Object> child_target = info[2]->ToObject();
|
Local<Object> child_target = info[2]->ToObject();
|
||||||
blobdata child_input = std::string(Buffer::Data(child_target), Buffer::Length(child_target));
|
blobdata child_input = std::string(Buffer::Data(child_target), Buffer::Length(child_target));
|
||||||
@@ -188,9 +177,9 @@ NAN_METHOD(convert_blob) {
|
|||||||
if (!construct_parent_block(b, parent_block)) return THROW_ERROR_EXCEPTION("convert_blob: Failed to construct parent block");
|
if (!construct_parent_block(b, parent_block)) return THROW_ERROR_EXCEPTION("convert_blob: Failed to construct parent block");
|
||||||
if (!get_block_hashing_blob(parent_block, output)) return THROW_ERROR_EXCEPTION("convert_blob: Failed to create mining block");
|
if (!get_block_hashing_blob(parent_block, output)) return THROW_ERROR_EXCEPTION("convert_blob: Failed to create mining block");
|
||||||
} else {
|
} else {
|
||||||
if (BLOB_TYPE_CRYPTONOTE == blob_type && info.Length() > 2) { // MM
|
if (BLOB_TYPE_CRYPTONOTE == blob_type && info.Length() > 2 && !info[2]->IsNumber()) { // MM
|
||||||
if (!fillExtra(b, b2)) {
|
if (!fillExtraMM(b, b2)) {
|
||||||
return THROW_ERROR_EXCEPTION("convert_blob: Failed to add merged mining tag to parent block extra (convert_blob)");
|
return THROW_ERROR_EXCEPTION("convert_blob: Failed to add merged mining tag to parent block extra");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!get_block_hashing_blob(b, output)) return THROW_ERROR_EXCEPTION("convert_blob: Failed to create mining block");
|
if (!get_block_hashing_blob(b, output)) return THROW_ERROR_EXCEPTION("convert_blob: Failed to create mining block");
|
||||||
@@ -260,7 +249,7 @@ NAN_METHOD(construct_block_blob) {
|
|||||||
if (!parse_and_validate_block_from_blob(block_template_blob, b)) return THROW_ERROR_EXCEPTION("Failed to parse block");
|
if (!parse_and_validate_block_from_blob(block_template_blob, b)) return THROW_ERROR_EXCEPTION("Failed to parse block");
|
||||||
|
|
||||||
block b2 = AUTO_VAL_INIT(b2);
|
block b2 = AUTO_VAL_INIT(b2);
|
||||||
if (info.Length() > 3) { // MM
|
if (info.Length() > 3 && !info[3]->IsNumber()) { // MM
|
||||||
b2.set_blob_type(BLOB_TYPE_FORKNOTE2); // Only forknote 2 blob types support being mm as child coin
|
b2.set_blob_type(BLOB_TYPE_FORKNOTE2); // Only forknote 2 blob types support being mm as child coin
|
||||||
Local<Object> child_target = info[3]->ToObject();
|
Local<Object> child_target = info[3]->ToObject();
|
||||||
blobdata child_input = std::string(Buffer::Data(child_target), Buffer::Length(child_target));
|
blobdata child_input = std::string(Buffer::Data(child_target), Buffer::Length(child_target));
|
||||||
@@ -275,8 +264,8 @@ NAN_METHOD(construct_block_blob) {
|
|||||||
if (POW_TYPE_NOT_SET != pow_type) b.minor_version = pow_type;
|
if (POW_TYPE_NOT_SET != pow_type) b.minor_version = pow_type;
|
||||||
if (!construct_parent_block(b, parent_block)) return THROW_ERROR_EXCEPTION("Failed to construct parent block");
|
if (!construct_parent_block(b, parent_block)) return THROW_ERROR_EXCEPTION("Failed to construct parent block");
|
||||||
if (!mergeBlocks(parent_block, b, std::vector<crypto::hash>())) return THROW_ERROR_EXCEPTION("Failed to postprocess mining block");
|
if (!mergeBlocks(parent_block, b, std::vector<crypto::hash>())) return THROW_ERROR_EXCEPTION("Failed to postprocess mining block");
|
||||||
} else if (BLOB_TYPE_CRYPTONOTE == blob_type && info.Length() > 3) { // MM
|
} else if (BLOB_TYPE_CRYPTONOTE == blob_type && info.Length() > 3 && !info[3]->IsNumber()) { // MM
|
||||||
if (!fillExtra(b, b2)) {
|
if (!fillExtraMM(b, b2)) {
|
||||||
return THROW_ERROR_EXCEPTION("Failed to add merged mining tag to parent block extra");
|
return THROW_ERROR_EXCEPTION("Failed to add merged mining tag to parent block extra");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -420,7 +409,7 @@ NAN_METHOD(fill_extra) {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (!fillExtra(b, b2)) {
|
if (!fillExtraMM(b, b2)) {
|
||||||
return THROW_ERROR_EXCEPTION("Failed to add merged mining tag to parent block extra (convert_blob)");
|
return THROW_ERROR_EXCEPTION("Failed to add merged mining tag to parent block extra (convert_blob)");
|
||||||
}
|
}
|
||||||
if (!get_block_hashing_blob(b, output)) return THROW_ERROR_EXCEPTION("Failed to create mining block");
|
if (!get_block_hashing_blob(b, output)) return THROW_ERROR_EXCEPTION("Failed to create mining block");
|
||||||
@@ -442,4 +431,4 @@ NAN_MODULE_INIT(init) {
|
|||||||
Nan::Set(target, Nan::New("fill_extra").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(fill_extra)).ToLocalChecked());
|
Nan::Set(target, Nan::New("fill_extra").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(fill_extra)).ToLocalChecked());
|
||||||
}
|
}
|
||||||
|
|
||||||
NODE_MODULE(cryptoforknote, init)
|
NODE_MODULE(cryptoforknote, init)
|
||||||
Reference in New Issue
Block a user