From 19475df0e4028cb6b04b625213820f8ec67e7316 Mon Sep 17 00:00:00 2001 From: MoneroOcean Date: Sun, 25 Mar 2018 14:06:12 +0200 Subject: [PATCH] Added blob type selection --- src/cryptonote_config.h | 6 ++++ src/cryptonote_core/cryptonote_basic.h | 28 +++++-------------- .../cryptonote_format_utils.cpp | 3 +- src/main.cc | 25 ++++++++++++++--- 4 files changed, 35 insertions(+), 27 deletions(-) diff --git a/src/cryptonote_config.h b/src/cryptonote_config.h index e9cf60d..b8838fd 100644 --- a/src/cryptonote_config.h +++ b/src/cryptonote_config.h @@ -15,3 +15,9 @@ #define CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW 60 #define CRYPTONOTE_DISPLAY_DECIMAL_POINT 12 + +enum BLOB_TYPE { + BLOB_TYPE_CRYPTONOTE = 0, + BLOB_TYPE_FORKNOTE1 = 1, + BLOB_TYPE_FORKNOTE2 = 2, +}; \ No newline at end of file diff --git a/src/cryptonote_core/cryptonote_basic.h b/src/cryptonote_core/cryptonote_basic.h index 7d29bb9..fe1ceba 100644 --- a/src/cryptonote_core/cryptonote_basic.h +++ b/src/cryptonote_core/cryptonote_basic.h @@ -174,10 +174,6 @@ namespace cryptonote BEGIN_SERIALIZE() VARINT_FIELD(version) - //if(8 < version) { - // printf("!!! Current tx version %zu exceeds %u max", version, 8); - // return false; - //} VARINT_FIELD(unlock_time) FIELD(vin) FIELD(vout) @@ -475,6 +471,8 @@ namespace cryptonote struct block_header { + enum BLOB_TYPE blob_type; + uint8_t major_version; uint8_t minor_version; uint64_t timestamp; @@ -484,16 +482,9 @@ namespace cryptonote BEGIN_SERIALIZE() VARINT_FIELD(major_version) VARINT_FIELD(minor_version) - if (BLOCK_MAJOR_VERSION_2 != major_version && BLOCK_MAJOR_VERSION_3 != major_version) - { - printf("block_header: block version %u\n", major_version); - VARINT_FIELD(timestamp) - } + if (blob_type != BLOB_TYPE_FORKNOTE2) VARINT_FIELD(timestamp) FIELD(prev_id) - if (BLOCK_MAJOR_VERSION_2 != major_version && BLOCK_MAJOR_VERSION_3 != major_version) - { - FIELD(nonce) - } + if (blob_type != BLOB_TYPE_FORKNOTE2) FIELD(nonce) END_SERIALIZE() }; @@ -506,15 +497,10 @@ namespace cryptonote BEGIN_SERIALIZE_OBJECT() FIELDS(*static_cast(this)) - if (BLOCK_MAJOR_VERSION_2 == major_version || BLOCK_MAJOR_VERSION_3 == major_version) + if (blob_type == BLOB_TYPE_FORKNOTE2) { - printf("block: block version %u\n", major_version); - try { - auto sbb = make_serializable_bytecoin_block(*this, false, false); - FIELD_N("parent_block", sbb); - } catch(...) { - puts("Exception!!!"); - } + auto sbb = make_serializable_bytecoin_block(*this, false, false); + FIELD_N("parent_block", sbb); } FIELD(miner_tx) FIELD(tx_hashes) diff --git a/src/cryptonote_core/cryptonote_format_utils.cpp b/src/cryptonote_core/cryptonote_format_utils.cpp index 94f0eac..c45d787 100644 --- a/src/cryptonote_core/cryptonote_format_utils.cpp +++ b/src/cryptonote_core/cryptonote_format_utils.cpp @@ -509,9 +509,8 @@ namespace cryptonote if (!get_block_hashing_blob(b, blob)) return false; - if (BLOCK_MAJOR_VERSION_2 == b.major_version || BLOCK_MAJOR_VERSION_3 == b.major_version) + if (b.blob_type == BLOB_TYPE_FORKNOTE2) { - printf("get_block_hash: block version %u\n", b.major_version); blobdata parent_blob; auto sbb = make_serializable_bytecoin_block(b, true, false); if (!t_serializable_object_to_blob(sbb, parent_blob)) diff --git a/src/main.cc b/src/main.cc index ae6f60d..5532521 100644 --- a/src/main.cc +++ b/src/main.cc @@ -74,14 +74,20 @@ NAN_METHOD(convert_blob) { if (info.Length() < 1) return THROW_ERROR_EXCEPTION("You must provide one argument."); Local target = info[0]->ToObject(); - if (!Buffer::HasInstance(target)) return THROW_ERROR_EXCEPTION("Argument should be a buffer object."); blobdata input = std::string(Buffer::Data(target), Buffer::Length(target)); blobdata output = ""; + enum BLOB_TYPE blob_type = BLOB_TYPE_CRYPTONOTE; + if (info.Length() >= 2) { + if (!info[1]->IsNumber()) return THROW_ERROR_EXCEPTION("Argument 2 should be a number"); + blob_type = static_cast(Nan::To(info[1]).FromMaybe(0)); + } + //convert block b = AUTO_VAL_INIT(b); + b.blob_type = blob_type; if (!parse_and_validate_block_from_blob(input, b)) return THROW_ERROR_EXCEPTION("Failed to parse block"); if (b.major_version == BLOCK_MAJOR_VERSION_2 || b.major_version == BLOCK_MAJOR_VERSION_3) { @@ -100,13 +106,19 @@ NAN_METHOD(get_block_id) { if (info.Length() < 1) return THROW_ERROR_EXCEPTION("You must provide one argument."); Local target = info[0]->ToObject(); - if (!Buffer::HasInstance(target)) return THROW_ERROR_EXCEPTION("Argument should be a buffer object."); blobdata input = std::string(Buffer::Data(target), Buffer::Length(target)); blobdata output = ""; + enum BLOB_TYPE blob_type = BLOB_TYPE_CRYPTONOTE; + if (info.Length() >= 2) { + if (!info[1]->IsNumber()) return THROW_ERROR_EXCEPTION("Argument 2 should be a number"); + blob_type = static_cast(Nan::To(info[1]).FromMaybe(0)); + } + block b = AUTO_VAL_INIT(b); + b.block_type = blob_type; if (!parse_and_validate_block_from_blob(input, b)) return THROW_ERROR_EXCEPTION("Failed to parse block"); crypto::hash block_id; @@ -124,15 +136,20 @@ NAN_METHOD(construct_block_blob) { Local nonce_buf = info[1]->ToObject(); if (!Buffer::HasInstance(block_template_buf) || !Buffer::HasInstance(nonce_buf)) return THROW_ERROR_EXCEPTION("Both arguments should be buffer objects."); - if (Buffer::Length(nonce_buf) != 4) return THROW_ERROR_EXCEPTION("Nonce buffer has invalid size."); uint32_t nonce = *reinterpret_cast(Buffer::Data(nonce_buf)); - blobdata block_template_blob = std::string(Buffer::Data(block_template_buf), Buffer::Length(block_template_buf)); blobdata output = ""; + enum BLOB_TYPE blob_type = BLOB_TYPE_CRYPTONOTE; + if (info.Length() >= 3) { + if (!info[2]->IsNumber()) return THROW_ERROR_EXCEPTION("Argument 3 should be a number"); + blob_type = static_cast(Nan::To(info[2]).FromMaybe(0)); + } + block b = AUTO_VAL_INIT(b); + b.blob_type = blob_type; if (!parse_and_validate_block_from_blob(block_template_blob, b)) return THROW_ERROR_EXCEPTION("Failed to parse block"); b.nonce = nonce;