Merged mining support
This commit is contained in:
141
src/main.cc
141
src/main.cc
@@ -17,6 +17,13 @@ using namespace node;
|
|||||||
using namespace v8;
|
using namespace v8;
|
||||||
using namespace cryptonote;
|
using namespace cryptonote;
|
||||||
|
|
||||||
|
const size_t TX_EXTRA_FIELD_TAG_BYTES = 1;
|
||||||
|
const size_t TX_MM_FIELD_SIZE_BYTES = 1;
|
||||||
|
const size_t MAX_VARINT_SIZE = 9;
|
||||||
|
const size_t TX_MM_TAG_MAX_BYTES = MAX_VARINT_SIZE + sizeof(crypto::hash);
|
||||||
|
const size_t MERGE_MINING_TAG_RESERVED_SIZE = TX_EXTRA_FIELD_TAG_BYTES + TX_MM_FIELD_SIZE_BYTES + TX_MM_TAG_MAX_BYTES;
|
||||||
|
const size_t POOL_NONCE_SIZE = 19;
|
||||||
|
|
||||||
blobdata uint64be_to_blob(uint64_t num) {
|
blobdata uint64be_to_blob(uint64_t num) {
|
||||||
blobdata res = " ";
|
blobdata res = " ";
|
||||||
res[0] = num >> 56 & 0xff;
|
res[0] = num >> 56 & 0xff;
|
||||||
@@ -29,7 +36,9 @@ blobdata uint64be_to_blob(uint64_t num) {
|
|||||||
res[7] = num & 0xff;
|
res[7] = num & 0xff;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
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;
|
||||||
@@ -40,6 +49,55 @@ static bool fillExtra(cryptonote::block& block1, const cryptonote::block& block2
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
static bool fillExtra(cryptonote::block& block1, const cryptonote::block& block2)
|
||||||
|
{
|
||||||
|
if (block2.timestamp > block1.timestamp) { // get the most recent timestamp (solve duplicated timestamps on child coin)
|
||||||
|
block1.timestamp = block2.timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
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::string extraAsString(reinterpret_cast<const char*>(extra.data()), extra.size());
|
||||||
|
|
||||||
|
std::string extraNonceTemplate;
|
||||||
|
extraNonceTemplate.push_back(TX_EXTRA_NONCE);
|
||||||
|
extraNonceTemplate.push_back(MERGE_MINING_TAG_RESERVED_SIZE_EX);
|
||||||
|
extraNonceTemplate.append(MERGE_MINING_TAG_RESERVED_SIZE, '\0');
|
||||||
|
|
||||||
|
size_t extraNoncePos = extraAsString.find(extraNonceTemplate);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::vector<uint8_t> extraNonceReplacement;
|
||||||
|
if (!cryptonote::append_mm_tag_to_extra(extraNonceReplacement, tag)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (MERGE_MINING_TAG_RESERVED_SIZE < extraNonceReplacement.size()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::copy(extraNonceReplacement.begin(), extraNonceReplacement.end(), extra.begin() + extraNoncePos);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static bool mergeBlocks(const cryptonote::block& block1, cryptonote::block& block2, const std::vector<crypto::hash>& branch2) {
|
static bool mergeBlocks(const cryptonote::block& block1, cryptonote::block& block2, const std::vector<crypto::hash>& branch2) {
|
||||||
block2.timestamp = block1.timestamp;
|
block2.timestamp = block1.timestamp;
|
||||||
@@ -70,6 +128,13 @@ static bool construct_parent_block(const cryptonote::block& b, cryptonote::block
|
|||||||
return fillExtra(parent_block, b);
|
return fillExtra(parent_block, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
NAN_METHOD(get_merge_mining_tag_reserved_size) {
|
||||||
|
Local<Integer> returnValue = Nan::New(static_cast<uint32_t>(MERGE_MINING_TAG_RESERVED_SIZE + POOL_NONCE_SIZE));
|
||||||
|
info.GetReturnValue().Set(returnValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
NAN_METHOD(convert_blob) {
|
NAN_METHOD(convert_blob) {
|
||||||
if (info.Length() < 1) return THROW_ERROR_EXCEPTION("You must provide one argument.");
|
if (info.Length() < 1) return THROW_ERROR_EXCEPTION("You must provide one argument.");
|
||||||
|
|
||||||
@@ -90,11 +155,25 @@ NAN_METHOD(convert_blob) {
|
|||||||
b.set_blob_type(blob_type);
|
b.set_blob_type(blob_type);
|
||||||
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);
|
||||||
|
if (info.Length() > 2) { // MM
|
||||||
|
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();
|
||||||
|
blobdata child_input = std::string(Buffer::Data(child_target), Buffer::Length(child_target));
|
||||||
|
if (!Buffer::HasInstance(child_target)) return THROW_ERROR_EXCEPTION("Argument (Child block) should be a buffer object.");
|
||||||
|
if (!parse_and_validate_block_from_blob(child_input, b2)) return THROW_ERROR_EXCEPTION("Failed to parse child block");
|
||||||
|
}
|
||||||
|
|
||||||
if (blob_type == BLOB_TYPE_FORKNOTE2) {
|
if (blob_type == BLOB_TYPE_FORKNOTE2) {
|
||||||
block parent_block;
|
block parent_block;
|
||||||
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 (!get_block_hashing_blob(parent_block, output)) return THROW_ERROR_EXCEPTION("Failed to create mining block");
|
if (!get_block_hashing_blob(parent_block, output)) return THROW_ERROR_EXCEPTION("Failed to create mining block");
|
||||||
} else {
|
} else {
|
||||||
|
if (BLOB_TYPE_CRYPTONOTE == blob_type && info.Length() > 2) { // MM
|
||||||
|
if (!fillExtra(b, b2)) {
|
||||||
|
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");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,13 +231,26 @@ NAN_METHOD(construct_block_blob) {
|
|||||||
b.set_blob_type(blob_type);
|
b.set_blob_type(blob_type);
|
||||||
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);
|
||||||
|
if (info.Length() > 3) { // MM
|
||||||
|
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();
|
||||||
|
blobdata child_input = std::string(Buffer::Data(child_target), Buffer::Length(child_target));
|
||||||
|
if (!Buffer::HasInstance(child_target)) return THROW_ERROR_EXCEPTION("Argument (Child block) should be a buffer object.");
|
||||||
|
if (!parse_and_validate_block_from_blob(child_input, b2)) return THROW_ERROR_EXCEPTION("Failed to parse child block");
|
||||||
|
}
|
||||||
|
|
||||||
b.nonce = nonce;
|
b.nonce = nonce;
|
||||||
if (blob_type == BLOB_TYPE_FORKNOTE2) {
|
if (blob_type == BLOB_TYPE_FORKNOTE2) {
|
||||||
block parent_block;
|
block parent_block;
|
||||||
b.parent_block.nonce = nonce;
|
b.parent_block.nonce = nonce;
|
||||||
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
|
||||||
|
if (!fillExtra(b, b2)) {
|
||||||
|
return THROW_ERROR_EXCEPTION("Failed to add merged mining tag to parent block extra");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!block_to_blob(b, output)) return THROW_ERROR_EXCEPTION("Failed to convert block to blob");
|
if (!block_to_blob(b, output)) return THROW_ERROR_EXCEPTION("Failed to convert block to blob");
|
||||||
|
|
||||||
@@ -227,12 +319,57 @@ NAN_METHOD(address_decode_integrated) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* var mmShareBuffer = merge_blocks(shareBuffer, coins, blobtype, "monetaverde");
|
||||||
|
*/
|
||||||
|
NAN_METHOD(merge_blocks) {
|
||||||
|
|
||||||
|
if (info.Length() < 2)
|
||||||
|
return THROW_ERROR_EXCEPTION("You must provide two arguments (shareBuffer, block2).");
|
||||||
|
|
||||||
|
Local<Object> block_template_buf = info[0]->ToObject();
|
||||||
|
Local<Object> child_block_template_buf = info[1]->ToObject();
|
||||||
|
|
||||||
|
if (!Buffer::HasInstance(block_template_buf))
|
||||||
|
return THROW_ERROR_EXCEPTION("First argument should be a buffer object.");
|
||||||
|
|
||||||
|
if (!Buffer::HasInstance(child_block_template_buf))
|
||||||
|
return THROW_ERROR_EXCEPTION("Second argument should be a buffer object.");
|
||||||
|
|
||||||
|
blobdata block_template_blob = std::string(Buffer::Data(block_template_buf), Buffer::Length(block_template_buf));
|
||||||
|
blobdata child_block_template_blob = std::string(Buffer::Data(child_block_template_buf), Buffer::Length(child_block_template_buf));
|
||||||
|
blobdata output = "";
|
||||||
|
|
||||||
|
block b = AUTO_VAL_INIT(b);
|
||||||
|
b.set_blob_type(BLOB_TYPE_CRYPTONOTE);
|
||||||
|
if (!parse_and_validate_block_from_blob(block_template_blob, b)) return THROW_ERROR_EXCEPTION("Failed to parse parent block (merge_blocks)");
|
||||||
|
|
||||||
|
block b2 = AUTO_VAL_INIT(b2);
|
||||||
|
b2.set_blob_type(BLOB_TYPE_FORKNOTE2);
|
||||||
|
if (!parse_and_validate_block_from_blob(child_block_template_blob, b2)) return THROW_ERROR_EXCEPTION("Failed to parse child block (merge_blocks)");
|
||||||
|
|
||||||
|
if (!mergeBlocks(b, b2, std::vector<crypto::hash>()))
|
||||||
|
return THROW_ERROR_EXCEPTION("mergeBlocks(b,b2): Failed to postprocess mining block");
|
||||||
|
|
||||||
|
if (!block_to_blob(b2, output)) {
|
||||||
|
return THROW_ERROR_EXCEPTION("Failed to convert block to blob (merge_blocks)");
|
||||||
|
}
|
||||||
|
|
||||||
|
v8::Local<v8::Value> returnValue = Nan::CopyBuffer((char*)output.data(), output.size()).ToLocalChecked();
|
||||||
|
info.GetReturnValue().Set(returnValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
NAN_MODULE_INIT(init) {
|
NAN_MODULE_INIT(init) {
|
||||||
Nan::Set(target, Nan::New("construct_block_blob").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(construct_block_blob)).ToLocalChecked());
|
Nan::Set(target, Nan::New("construct_block_blob").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(construct_block_blob)).ToLocalChecked());
|
||||||
Nan::Set(target, Nan::New("get_block_id").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(get_block_id)).ToLocalChecked());
|
Nan::Set(target, Nan::New("get_block_id").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(get_block_id)).ToLocalChecked());
|
||||||
Nan::Set(target, Nan::New("convert_blob").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(convert_blob)).ToLocalChecked());
|
Nan::Set(target, Nan::New("convert_blob").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(convert_blob)).ToLocalChecked());
|
||||||
Nan::Set(target, Nan::New("address_decode").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(address_decode)).ToLocalChecked());
|
Nan::Set(target, Nan::New("address_decode").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(address_decode)).ToLocalChecked());
|
||||||
Nan::Set(target, Nan::New("address_decode_integrated").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(address_decode_integrated)).ToLocalChecked());
|
Nan::Set(target, Nan::New("address_decode_integrated").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(address_decode_integrated)).ToLocalChecked());
|
||||||
|
|
||||||
|
Nan::Set(target, Nan::New("merge_blocks").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(merge_blocks)).ToLocalChecked());
|
||||||
|
Nan::Set(target, Nan::New("get_merge_mining_tag_reserved_size").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(get_merge_mining_tag_reserved_size)).ToLocalChecked());
|
||||||
}
|
}
|
||||||
|
|
||||||
NODE_MODULE(cryptoforknote, init)
|
NODE_MODULE(cryptoforknote, init)
|
||||||
|
|||||||
Reference in New Issue
Block a user