From 903dacf470cb92bb82c684f55c56ca4c92ae7767 Mon Sep 17 00:00:00 2001 From: MoneroOcean Date: Tue, 2 Apr 2019 19:14:03 -0700 Subject: [PATCH] Fixed MM stuff --- src/cryptonote_core/tx_extra.h | 219 ++++++++++++++++++++++++++++++--- src/main.cc | 55 ++++++--- 2 files changed, 242 insertions(+), 32 deletions(-) diff --git a/src/cryptonote_core/tx_extra.h b/src/cryptonote_core/tx_extra.h index 37a04a4..2fba072 100644 --- a/src/cryptonote_core/tx_extra.h +++ b/src/cryptonote_core/tx_extra.h @@ -1,19 +1,55 @@ -// Copyright (c) 2012-2013 The Cryptonote developers -// Distributed under the MIT/X11 software license, see the accompanying -// file COPYING or http://www.opensource.org/licenses/mit-license.php. +// Copyright (c) 2014-2018, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers #pragma once +#define TX_EXTRA_PADDING_MAX_COUNT 255 +#define TX_EXTRA_NONCE_MAX_COUNT 255 -#define TX_EXTRA_PADDING_MAX_COUNT 255 -#define TX_EXTRA_NONCE_MAX_COUNT 255 +#define TX_EXTRA_TAG_PADDING 0x00 +#define TX_EXTRA_TAG_PUBKEY 0x01 +#define TX_EXTRA_NONCE 0x02 +#define TX_EXTRA_MERGE_MINING_TAG 0x03 +#define TX_EXTRA_TAG_ADDITIONAL_PUBKEYS 0x04 +#define TX_EXTRA_TAG_SERVICE_NODE_REGISTER 0x70 +#define TX_EXTRA_TAG_SERVICE_NODE_DEREGISTER 0x71 +#define TX_EXTRA_TAG_SERVICE_NODE_WINNER 0x72 +#define TX_EXTRA_TAG_SERVICE_NODE_CONTRIBUTOR 0x73 +#define TX_EXTRA_TAG_SERVICE_NODE_PUBKEY 0x74 +#define TX_EXTRA_TAG_TX_SECRET_KEY 0x75 +#define TX_EXTRA_TAG_TX_KEY_IMAGE_PROOFS 0x76 +#define TX_EXTRA_TAG_TX_KEY_IMAGE_UNLOCK 0x77 +#define TX_EXTRA_MYSTERIOUS_MINERGATE_TAG 0xDE -#define TX_EXTRA_TAG_PADDING 0x00 -#define TX_EXTRA_TAG_PUBKEY 0x01 -#define TX_EXTRA_NONCE 0x02 -#define TX_EXTRA_MERGE_MINING_TAG 0x03 - -#define TX_EXTRA_NONCE_PAYMENT_ID 0x00 +#define TX_EXTRA_NONCE_PAYMENT_ID 0x00 +#define TX_EXTRA_NONCE_ENCRYPTED_PAYMENT_ID 0x01 namespace cryptonote { @@ -131,14 +167,165 @@ namespace cryptonote } }; + // per-output additional tx pubkey for multi-destination transfers involving at least one subaddress + struct tx_extra_additional_pub_keys + { + std::vector data; + + BEGIN_SERIALIZE() + FIELD(data) + END_SERIALIZE() + }; + + struct tx_extra_mysterious_minergate + { + std::string data; + + BEGIN_SERIALIZE() + FIELD(data) + END_SERIALIZE() + }; + + struct tx_extra_service_node_winner + { + crypto::public_key m_service_node_key; + + BEGIN_SERIALIZE() + FIELD(m_service_node_key) + END_SERIALIZE() + }; + + struct tx_extra_service_node_pubkey + { + crypto::public_key m_service_node_key; + + BEGIN_SERIALIZE() + FIELD(m_service_node_key) + END_SERIALIZE() + }; + + + struct tx_extra_service_node_register + { + std::vector m_public_spend_keys; + std::vector m_public_view_keys; + uint64_t m_portions_for_operator; + std::vector m_portions; + uint64_t m_expiration_timestamp; + crypto::signature m_service_node_signature; + + BEGIN_SERIALIZE() + FIELD(m_public_spend_keys) + FIELD(m_public_view_keys) + FIELD(m_portions_for_operator) + FIELD(m_portions) + FIELD(m_expiration_timestamp) + FIELD(m_service_node_signature) + END_SERIALIZE() + }; + + struct tx_extra_service_node_contributor + { + crypto::public_key m_spend_public_key; + crypto::public_key m_view_public_key; + + BEGIN_SERIALIZE() + FIELD(m_spend_public_key) + FIELD(m_view_public_key) + END_SERIALIZE() + }; + + struct tx_extra_service_node_deregister + { + struct vote + { + crypto::signature signature; + uint32_t voters_quorum_index; + }; + + uint64_t block_height; + uint32_t service_node_index; + std::vector votes; + + BEGIN_SERIALIZE() + FIELD(block_height) + FIELD(service_node_index) + FIELD(votes) + END_SERIALIZE() + }; + + struct tx_extra_tx_secret_key + { + crypto::secret_key key; + + BEGIN_SERIALIZE() + FIELD(key) + END_SERIALIZE() + }; + + struct tx_extra_tx_key_image_proofs + { + struct proof + { + crypto::key_image key_image; + crypto::signature signature; + }; + + std::vector proofs; + + BEGIN_SERIALIZE() + FIELD(proofs) + END_SERIALIZE() + }; + + struct tx_extra_tx_key_image_unlock + { + crypto::key_image key_image; + crypto::signature signature; + uint32_t nonce; + + BEGIN_SERIALIZE() + FIELD(key_image) + FIELD(signature) + FIELD(nonce) + END_SERIALIZE() + }; + // tx_extra_field format, except tx_extra_padding and tx_extra_pub_key: // varint tag; // varint size; // varint data[]; - typedef boost::variant tx_extra_field; + typedef boost::variant tx_extra_field; } -VARIANT_TAG(binary_archive, cryptonote::tx_extra_padding, TX_EXTRA_TAG_PADDING); -VARIANT_TAG(binary_archive, cryptonote::tx_extra_pub_key, TX_EXTRA_TAG_PUBKEY); -VARIANT_TAG(binary_archive, cryptonote::tx_extra_nonce, TX_EXTRA_NONCE); -VARIANT_TAG(binary_archive, cryptonote::tx_extra_merge_mining_tag, TX_EXTRA_MERGE_MINING_TAG); +BLOB_SERIALIZER(cryptonote::tx_extra_service_node_deregister::vote); +BLOB_SERIALIZER(cryptonote::tx_extra_tx_key_image_proofs::proof); + +VARIANT_TAG(binary_archive, cryptonote::tx_extra_padding, TX_EXTRA_TAG_PADDING); +VARIANT_TAG(binary_archive, cryptonote::tx_extra_pub_key, TX_EXTRA_TAG_PUBKEY); +VARIANT_TAG(binary_archive, cryptonote::tx_extra_nonce, TX_EXTRA_NONCE); +VARIANT_TAG(binary_archive, cryptonote::tx_extra_merge_mining_tag, TX_EXTRA_MERGE_MINING_TAG); +VARIANT_TAG(binary_archive, cryptonote::tx_extra_additional_pub_keys, TX_EXTRA_TAG_ADDITIONAL_PUBKEYS); +VARIANT_TAG(binary_archive, cryptonote::tx_extra_mysterious_minergate, TX_EXTRA_MYSTERIOUS_MINERGATE_TAG); +VARIANT_TAG(binary_archive, cryptonote::tx_extra_service_node_register, TX_EXTRA_TAG_SERVICE_NODE_REGISTER); +VARIANT_TAG(binary_archive, cryptonote::tx_extra_service_node_deregister, TX_EXTRA_TAG_SERVICE_NODE_DEREGISTER); +VARIANT_TAG(binary_archive, cryptonote::tx_extra_service_node_contributor, TX_EXTRA_TAG_SERVICE_NODE_CONTRIBUTOR); +VARIANT_TAG(binary_archive, cryptonote::tx_extra_service_node_winner, TX_EXTRA_TAG_SERVICE_NODE_WINNER); +VARIANT_TAG(binary_archive, cryptonote::tx_extra_service_node_pubkey, TX_EXTRA_TAG_SERVICE_NODE_PUBKEY); +VARIANT_TAG(binary_archive, cryptonote::tx_extra_tx_secret_key, TX_EXTRA_TAG_TX_SECRET_KEY); +VARIANT_TAG(binary_archive, cryptonote::tx_extra_tx_key_image_proofs, TX_EXTRA_TAG_TX_KEY_IMAGE_PROOFS); +VARIANT_TAG(binary_archive, cryptonote::tx_extra_tx_key_image_unlock, TX_EXTRA_TAG_TX_KEY_IMAGE_UNLOCK); diff --git a/src/main.cc b/src/main.cc index 1bc2d74..d7efc95 100644 --- a/src/main.cc +++ b/src/main.cc @@ -17,9 +17,8 @@ using namespace node; using namespace v8; using namespace cryptonote; -// cryptonote::append_mm_tag_to_extra writes byte with TX_EXTRA_MERGE_MINING_TAG -// 2 bytes after are TX_EXTRA_NONCE and extra nonce size used by pool -const size_t MM_NONCE_SIZE = 1 + sizeof(crypto::hash) + 2; +// cryptonote::append_mm_tag_to_extra writes byte with TX_EXTRA_MERGE_MINING_TAG (1 here) and VARINT DEPTH (2 here) +const size_t MM_NONCE_SIZE = 1 + 2 + sizeof(crypto::hash); blobdata uint64be_to_blob(uint64_t num) { blobdata res = " "; @@ -45,15 +44,36 @@ static bool fillExtra(cryptonote::block& block1, const cryptonote::block& block2 return true; } + static bool fillExtraMM(cryptonote::block& block1, const cryptonote::block& block2) { - std::vector& extra_nonce = block1.miner_tx.extra; - if (extra_nonce.size() < MM_NONCE_SIZE) return false; - if (!fillExtra(block1, block2)) return false; + 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; + std::vector extra_nonce_replace; + if (!cryptonote::append_mm_tag_to_extra(extra_nonce_replace, mm_tag)) return false; - extra_nonce[MM_NONCE_SIZE-2] = TX_EXTRA_NONCE; - extra_nonce[MM_NONCE_SIZE-1] = extra_nonce.size() - MM_NONCE_SIZE; + if (extra_nonce_replace.size() != MM_NONCE_SIZE) return false; + + std::vector& extra = block1.miner_tx.extra; + size_t pos = 0; + + while (pos < extra.size() && extra[pos] != TX_EXTRA_NONCE) { + switch (extra[pos]) { + case TX_EXTRA_TAG_PUBKEY: pos += 1 + sizeof(crypto::public_key); break; + default: return false; + } + } + + if (pos + 1 >= extra.size()) return false; + + const int extra_nonce_size = extra[pos + 1]; + const int new_extra_nonce_size = extra_nonce_size - MM_NONCE_SIZE; + + if (new_extra_nonce_size < 0) return false; + + extra[pos + 1] = new_extra_nonce_size; + std::copy(extra_nonce_replace.begin(), extra_nonce_replace.end(), extra.begin() + pos + 1 + new_extra_nonce_size + 1); - if (block2.timestamp > block1.timestamp) block1.timestamp = block2.timestamp; return true; } @@ -259,7 +279,6 @@ NAN_METHOD(construct_mm_child_block_blob) { // (shareBuffer, blob_type, childBlo 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); @@ -271,13 +290,14 @@ NAN_METHOD(construct_mm_child_block_blob) { // (shareBuffer, blob_type, childBlo if (!mergeBlocks(b, b2, std::vector())) return THROW_ERROR_EXCEPTION("construct_mm_child_block_blob: Failed to postprocess mining block"); + blobdata output = ""; if (!block_to_blob(b2, output)) return THROW_ERROR_EXCEPTION("construct_mm_child_block_blob: Failed to convert child block to blob"); v8::Local returnValue = Nan::CopyBuffer((char*)output.data(), output.size()).ToLocalChecked(); info.GetReturnValue().Set(returnValue); } -NAN_METHOD(construct_mm_parent_block_extra_nonce) { // (parentBlockTemplate, blob_type, childBlockTemplate) +NAN_METHOD(construct_mm_parent_block_blob) { // (parentBlockTemplate, blob_type, childBlockTemplate) if (info.Length() < 3) return THROW_ERROR_EXCEPTION("You must provide three arguments (parentBlock, blob_type, childBlock)."); Local target = info[0]->ToObject(); @@ -294,15 +314,18 @@ NAN_METHOD(construct_mm_parent_block_extra_nonce) { // (parentBlockTemplate, blo block b = AUTO_VAL_INIT(b); b.set_blob_type(blob_type); - if (!parse_and_validate_block_from_blob(input, b)) return THROW_ERROR_EXCEPTION("construct_mm_parent_block_extra_nonce: Failed to parse prent block"); + if (!parse_and_validate_block_from_blob(input, b)) return THROW_ERROR_EXCEPTION("construct_mm_parent_block_blob: Failed to parse prent block"); block b2 = AUTO_VAL_INIT(b2); b2.set_blob_type(BLOB_TYPE_FORKNOTE2); - if (!parse_and_validate_block_from_blob(child_input, b2)) return THROW_ERROR_EXCEPTION("construct_mm_parent_block_extra_nonce: Failed to parse child block"); + if (!parse_and_validate_block_from_blob(child_input, b2)) return THROW_ERROR_EXCEPTION("construct_mm_parent_block_blob: Failed to parse child block"); - if (!fillExtraMM(b, b2)) return THROW_ERROR_EXCEPTION("construct_mm_parent_block_extra_nonce: Failed to add merged mining tag to parent block extra"); + if (!fillExtraMM(b, b2)) return THROW_ERROR_EXCEPTION("construct_mm_parent_block_blob: Failed to add merged mining tag to parent block extra"); - v8::Local returnValue = Nan::CopyBuffer((char*)&b.miner_tx.extra[0], b.miner_tx.extra.size()).ToLocalChecked(); + blobdata output = ""; + if (!block_to_blob(b, output)) return THROW_ERROR_EXCEPTION("construct_mm_parent_block_blob: Failed to convert child block to blob"); + + v8::Local returnValue = Nan::CopyBuffer((char*)output.data(), output.size()).ToLocalChecked(); info.GetReturnValue().Set(returnValue); } @@ -315,7 +338,7 @@ NAN_MODULE_INIT(init) { Nan::Set(target, Nan::New("get_merged_mining_nonce_size").ToLocalChecked(), Nan::GetFunction(Nan::New(get_merged_mining_nonce_size)).ToLocalChecked()); Nan::Set(target, Nan::New("construct_mm_child_block_blob").ToLocalChecked(), Nan::GetFunction(Nan::New(construct_mm_child_block_blob)).ToLocalChecked()); - Nan::Set(target, Nan::New("construct_mm_parent_block_extra_nonce").ToLocalChecked(), Nan::GetFunction(Nan::New(construct_mm_parent_block_extra_nonce)).ToLocalChecked()); + Nan::Set(target, Nan::New("construct_mm_parent_block_blob").ToLocalChecked(), Nan::GetFunction(Nan::New(construct_mm_parent_block_blob)).ToLocalChecked()); } NODE_MODULE(cryptoforknote, init)