From 57cf53625817223733bdaf2f4e518ebae978713e Mon Sep 17 00:00:00 2001 From: Antonio Juarez Date: Tue, 29 Apr 2014 17:26:45 +0100 Subject: [PATCH] extra for transfers --- CMakeLists.txt | 22 ++- README | 4 +- ReleaseNotes.txt | 7 + contrib/epee/include/net/http_client.h | 2 +- .../epee/include/storages/portable_storage.h | 4 +- src/common/boost_serialization_helper.h | 48 +++++- ...unordered_containers_boost_serialization.h | 37 +++- src/common/util.h | 2 +- src/cryptonote_core/blockchain_storage.cpp | 63 ++++++- src/cryptonote_core/checkpoints_create.h | 2 + .../cryptonote_format_utils.cpp | 130 ++++++++------ src/cryptonote_core/cryptonote_format_utils.h | 23 ++- src/cryptonote_core/difficulty.cpp | 2 +- src/cryptonote_core/tx_extra.h | 87 +++++++++- src/cryptonote_core/tx_pool.h | 3 + src/miner/simpleminer.cpp | 1 + src/rpc/core_rpc_server.cpp | 3 +- src/serialization/serialization.h | 2 +- src/simplewallet/simplewallet.cpp | 163 ++++++++++++++++-- src/simplewallet/simplewallet.h | 3 + src/version.h.in | 4 +- src/wallet/wallet2.cpp | 111 +++++++++--- src/wallet/wallet2.h | 52 ++++-- src/wallet/wallet_errors.h | 23 --- src/wallet/wallet_rpc_server.cpp | 49 +++++- src/wallet/wallet_rpc_server.h | 10 +- src/wallet/wallet_rpc_server_commans_defs.h | 35 ++++ src/wallet/wallet_rpc_server_error_codes.h | 3 +- tests/core_tests/block_validation.cpp | 4 +- tests/core_tests/chaingen.cpp | 8 +- tests/core_tests/double_spend.inl | 4 +- tests/core_tests/integer_overflow.cpp | 8 +- tests/core_tests/transaction_tests.cpp | 6 +- .../transactions_flow_test.cpp | 4 +- .../gtest/include/gtest/internal/gtest-port.h | 2 - .../performance_tests/check_ring_signature.h | 2 +- tests/performance_tests/construct_tx.h | 2 +- tests/unit_tests/test_format_utils.cpp | 132 +++++++++++--- 38 files changed, 840 insertions(+), 227 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8510b369..9e3bb336 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,20 +3,19 @@ cmake_minimum_required(VERSION 2.8.6) set(VERSION "0.1") # $Format:Packaged from commit %H%nset(COMMIT %h)%nset(REFS "%d")$ -function(set_static_flags) -if (NOT APPLE) - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc -static-libstdc++") -endif() -endfunction(set_static_flags) - set_property(GLOBAL PROPERTY USE_FOLDERS ON) set(CMAKE_CONFIGURATION_TYPES "Debug;Release") enable_testing() -include_directories(src contrib/epee/include external "${CMAKE_BINARY_DIR}/version") +function(set_static_flags) + if (NOT APPLE) + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc -static-libstdc++") + endif() +endfunction(set_static_flags) +include_directories(src contrib/epee/include external "${CMAKE_BINARY_DIR}/version") if(APPLE) -include_directories(SYSTEM /usr/include/malloc) + include_directories(SYSTEM /usr/include/malloc) endif() set(STATIC ${MSVC} CACHE BOOL "Link libraries statically") @@ -61,6 +60,9 @@ else() endif() set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11 -D_GNU_SOURCE ${MINGW_FLAG} ${STATIC_ASSERT_FLAG} ${WARNINGS} ${C_WARNINGS} ${ARCH_FLAG}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -D_GNU_SOURCE ${MINGW_FLAG} ${WARNINGS} ${CXX_WARNINGS} ${ARCH_FLAG}") + if(APPLE) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DGTEST_HAS_TR1_TUPLE=0") + endif() if(CMAKE_C_COMPILER_ID STREQUAL "GNU" AND NOT (CMAKE_C_COMPILER_VERSION VERSION_LESS 4.8)) set(DEBUG_FLAGS "-g3 -Og") else() @@ -90,8 +92,10 @@ endif() include_directories(SYSTEM ${Boost_INCLUDE_DIRS}) if(MINGW) set(Boost_LIBRARIES "${Boost_LIBRARIES};ws2_32;mswsock") -elseif(NOT MSVC) +elseif(APPLE) set(Boost_LIBRARIES "${Boost_LIBRARIES}") +elseif(NOT MSVC) + set(Boost_LIBRARIES "${Boost_LIBRARIES};rt") endif() set(COMMIT_ID_IN_VERSION ON CACHE BOOL "Include commit ID in version") diff --git a/README b/README index 6d9478f2..87a89c58 100644 --- a/README +++ b/README @@ -1,4 +1,4 @@ --= Building CyberNote =- +-= Building CryptoNote =- On *nix: @@ -29,4 +29,4 @@ cd build cmake -G "Visual Studio 11 Win64" .. And then do Build. -Good luck! +Good luck! \ No newline at end of file diff --git a/ReleaseNotes.txt b/ReleaseNotes.txt index 0ce55002..ff2a2131 100644 --- a/ReleaseNotes.txt +++ b/ReleaseNotes.txt @@ -1,3 +1,10 @@ +Release notes 0.8.6 + +- Simplwallet can set extra for transfers +- Improvements in JSON RPC for wallet +- UX improvements in simplewallet +- Win32 compilation + Release notes 0.8.5 - Port mapping for daemon using UPnP diff --git a/contrib/epee/include/net/http_client.h b/contrib/epee/include/net/http_client.h index 5897a017..7e6b52d9 100644 --- a/contrib/epee/include/net/http_client.h +++ b/contrib/epee/include/net/http_client.h @@ -856,7 +856,7 @@ using namespace std; http::url_content u_c; bool res = parse_url(url, u_c); - if(!tr.is_connected() && !u_c.host.empty()) + if(!tr.is_connected()) { CHECK_AND_ASSERT_MES(res, false, "failed to parse url: " << url); diff --git a/contrib/epee/include/storages/portable_storage.h b/contrib/epee/include/storages/portable_storage.h index eb8ee2d1..c244dac1 100644 --- a/contrib/epee/include/storages/portable_storage.h +++ b/contrib/epee/include/storages/portable_storage.h @@ -210,7 +210,7 @@ namespace epee template bool portable_storage::get_value(const std::string& value_name, t_value& val, hsection hparent_section) { - //BOOST_MPL_ASSERT(( boost::mpl::contains )); + BOOST_MPL_ASSERT(( boost::mpl::contains )); //TRY_ENTRY(); if(!hparent_section) hparent_section = &m_root; storage_entry* pentry = find_storage_entry(value_name, hparent_section); @@ -476,4 +476,4 @@ namespace epee } //--------------------------------------------------------------------------------------------------------------- } -} +} \ No newline at end of file diff --git a/src/common/boost_serialization_helper.h b/src/common/boost_serialization_helper.h index 74016ae7..0bf92480 100644 --- a/src/common/boost_serialization_helper.h +++ b/src/common/boost_serialization_helper.h @@ -14,15 +14,55 @@ namespace tools bool serialize_obj_to_file(t_object& obj, const std::string& file_path) { TRY_ENTRY(); - std::ofstream data_file; - data_file.open( file_path , std::ios_base::binary | std::ios_base::out| std::ios::trunc); - if(data_file.fail()) +#if defined(_MSC_VER) + // Need to know HANDLE of file to call FlushFileBuffers + HANDLE data_file_handle = ::CreateFile(file_path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + if (INVALID_HANDLE_VALUE == data_file_handle) return false; + int data_file_descriptor = _open_osfhandle((intptr_t)data_file_handle, 0); + if (-1 == data_file_descriptor) + { + ::CloseHandle(data_file_handle); + return false; + } + + FILE* data_file_file = _fdopen(data_file_descriptor, "wb"); + if (0 == data_file_file) + { + // Call CloseHandle is not necessary + _close(data_file_descriptor); + return false; + } + + // HACK: undocumented constructor, this code may not compile + std::ofstream data_file(data_file_file); + if (data_file.fail()) + { + // Call CloseHandle and _close are not necessary + fclose(data_file_file); + return false; + } +#else + std::ofstream data_file; + data_file.open(file_path , std::ios_base::binary | std::ios_base::out| std::ios::trunc); + if (data_file.fail()) + return false; +#endif + boost::archive::binary_oarchive a(data_file); a << obj; + if (data_file.fail()) + return false; - return !data_file.fail(); + data_file.flush(); +#if defined(_MSC_VER) + // To make sure the file is fully stored on disk + ::FlushFileBuffers(data_file_handle); + fclose(data_file_file); +#endif + + return true; CATCH_ENTRY_L0("serialize_obj_to_file", false); } diff --git a/src/common/unordered_containers_boost_serialization.h b/src/common/unordered_containers_boost_serialization.h index a29896ee..84fa73b9 100644 --- a/src/common/unordered_containers_boost_serialization.h +++ b/src/common/unordered_containers_boost_serialization.h @@ -2,7 +2,7 @@ // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#pragma once +#pragma once #include #include @@ -41,6 +41,35 @@ namespace boost } + template + inline void save(Archive &a, const std::unordered_multimap &x, const boost::serialization::version_type ver) + { + size_t s = x.size(); + a << s; + BOOST_FOREACH(auto& v, x) + { + a << v.first; + a << v.second; + } + } + + template + inline void load(Archive &a, std::unordered_multimap &x, const boost::serialization::version_type ver) + { + x.clear(); + size_t s = 0; + a >> s; + for(size_t i = 0; i != s; i++) + { + h_key k; + hval v; + a >> k; + a >> v; + x.emplace(k, v); + } + } + + template inline void save(Archive &a, const std::unordered_set &x, const boost::serialization::version_type ver) { @@ -73,6 +102,12 @@ namespace boost split_free(a, x, ver); } + template + inline void serialize(Archive &a, std::unordered_multimap &x, const boost::serialization::version_type ver) + { + split_free(a, x, ver); + } + template inline void serialize(Archive &a, std::unordered_set &x, const boost::serialization::version_type ver) { diff --git a/src/common/util.h b/src/common/util.h index af92adf9..8a1f4b04 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -52,7 +52,7 @@ namespace tools private: #if defined(WIN32) - static BOOL win_handler(DWORD type) + static BOOL WINAPI win_handler(DWORD type) { if (CTRL_C_EVENT == type || CTRL_BREAK_EVENT == type) { diff --git a/src/cryptonote_core/blockchain_storage.cpp b/src/cryptonote_core/blockchain_storage.cpp index c1b9619f..0e20b454 100644 --- a/src/cryptonote_core/blockchain_storage.cpp +++ b/src/cryptonote_core/blockchain_storage.cpp @@ -582,6 +582,42 @@ bool blockchain_storage::create_block_template(block& b, const account_public_ad if (!m_tx_pool.fill_block_template(b, median_size, already_generated_coins, txs_size, fee)) { return false; } +#if defined(DEBUG_CREATE_BLOCK_TEMPLATE) + size_t real_txs_size = 0; + uint64_t real_fee = 0; + CRITICAL_REGION_BEGIN(m_tx_pool.m_transactions_lock); + BOOST_FOREACH(crypto::hash &cur_hash, b.tx_hashes) { + auto cur_res = m_tx_pool.m_transactions.find(cur_hash); + if (cur_res == m_tx_pool.m_transactions.end()) { + LOG_ERROR("Creating block template: error: transaction not found"); + continue; + } + tx_memory_pool::tx_details &cur_tx = cur_res->second; + real_txs_size += cur_tx.blob_size; + real_fee += cur_tx.fee; + if (cur_tx.blob_size != get_object_blobsize(cur_tx.tx)) { + LOG_ERROR("Creating block template: error: invalid transaction size"); + } + uint64_t inputs_amount; + if (!get_inputs_money_amount(cur_tx.tx, inputs_amount)) { + LOG_ERROR("Creating block template: error: cannot get inputs amount"); + } else if (cur_tx.fee != inputs_amount - get_outs_money_amount(cur_tx.tx)) { + LOG_ERROR("Creating block template: error: invalid fee"); + } + } + if (txs_size != real_txs_size) { + LOG_ERROR("Creating block template: error: wrongly calculated transaction size"); + } + if (fee != real_fee) { + LOG_ERROR("Creating block template: error: wrongly calculated fee"); + } + CRITICAL_REGION_END(); + LOG_PRINT_L1("Creating block template: height " << height << + ", median size " << median_size << + ", already generated coins " << already_generated_coins << + ", transaction size " << txs_size << + ", fee " << fee); +#endif /* two-phase miner transaction generation: we don't know exact block size until we prepare block, but we don't know reward until we know @@ -590,27 +626,32 @@ bool blockchain_storage::create_block_template(block& b, const account_public_ad //make blocks coin-base tx looks close to real coinbase tx to get truthful blob size bool r = construct_miner_tx(height, median_size, already_generated_coins, txs_size, fee, miner_address, b.miner_tx, ex_nonce, 11); CHECK_AND_ASSERT_MES(r, false, "Failed to construc miner tx, first chance"); -#ifdef _DEBUG - std::list try_val; - try_val.push_back(get_object_blobsize(b.miner_tx)); -#endif - size_t cumulative_size = txs_size + get_object_blobsize(b.miner_tx); +#if defined(DEBUG_CREATE_BLOCK_TEMPLATE) + LOG_PRINT_L1("Creating block template: miner tx size " << get_object_blobsize(b.miner_tx) << + ", cumulative size " << cumulative_size); +#endif for (size_t try_count = 0; try_count != 10; ++try_count) { r = construct_miner_tx(height, median_size, already_generated_coins, cumulative_size, fee, miner_address, b.miner_tx, ex_nonce, 11); -#ifdef _DEBUG - try_val.push_back(get_object_blobsize(b.miner_tx)); -#endif CHECK_AND_ASSERT_MES(r, false, "Failed to construc miner tx, second chance"); size_t coinbase_blob_size = get_object_blobsize(b.miner_tx); if (coinbase_blob_size > cumulative_size - txs_size) { cumulative_size = txs_size + coinbase_blob_size; +#if defined(DEBUG_CREATE_BLOCK_TEMPLATE) + LOG_PRINT_L1("Creating block template: miner tx size " << coinbase_blob_size << + ", cumulative size " << cumulative_size << " is greater then before"); +#endif continue; } if (coinbase_blob_size < cumulative_size - txs_size) { size_t delta = cumulative_size - txs_size - coinbase_blob_size; +#if defined(DEBUG_CREATE_BLOCK_TEMPLATE) + LOG_PRINT_L1("Creating block template: miner tx size " << coinbase_blob_size << + ", cumulative size " << txs_size + coinbase_blob_size << + " is less then before, adding " << delta << " zero bytes"); +#endif b.miner_tx.extra.insert(b.miner_tx.extra.end(), delta, 0); //here could be 1 byte difference, because of extra field counter is varint, and it can become from 1-byte len to 2-bytes len. if (cumulative_size != txs_size + get_object_blobsize(b.miner_tx)) { @@ -626,6 +667,10 @@ bool blockchain_storage::create_block_template(block& b, const account_public_ad } } CHECK_AND_ASSERT_MES(cumulative_size == txs_size + get_object_blobsize(b.miner_tx), false, "unexpected case: cumulative_size=" << cumulative_size << " is not equal txs_cumulative_size=" << txs_size << " + get_object_blobsize(b.miner_tx)=" << get_object_blobsize(b.miner_tx)); +#if defined(DEBUG_CREATE_BLOCK_TEMPLATE) + LOG_PRINT_L1("Creating block template: miner tx size " << coinbase_blob_size << + ", cumulative size " << cumulative_size << " is now good"); +#endif return true; } LOG_ERROR("Failed to create_block_template with " << 10 << " tries"); @@ -1603,4 +1648,4 @@ bool blockchain_storage::add_new_block(const block& bl_, block_verification_cont } return handle_block_to_main_chain(bl, id, bvc); -} +} \ No newline at end of file diff --git a/src/cryptonote_core/checkpoints_create.h b/src/cryptonote_core/checkpoints_create.h index 6046a011..207cd236 100644 --- a/src/cryptonote_core/checkpoints_create.h +++ b/src/cryptonote_core/checkpoints_create.h @@ -22,6 +22,8 @@ namespace cryptonote { ADD_CHECKPOINT(417000, "2dc96f8fc4d4a4d76b3ed06722829a7ab09d310584b8ecedc9b578b2c458a69f"); ADD_CHECKPOINT(427193, "00feabb08f2d5759ed04fd6b799a7513187478696bba2db2af10d4347134e311"); ADD_CHECKPOINT(453537, "d17de6916c5aa6ffcae575309c80b0f8fdcd0a84b5fa8e41a841897d4b5a4e97"); + ADD_CHECKPOINT(462250, "13468d210a5ec884cf839f0259f247ccf3efef0414ac45172033d32c739beb3e"); + ADD_CHECKPOINT(468000, "251bcbd398b1f593193a7210934a3d87f692b2cb0c45206150f59683dd7e9ba1"); return true; } diff --git a/src/cryptonote_core/cryptonote_format_utils.cpp b/src/cryptonote_core/cryptonote_format_utils.cpp index b160ab8d..aa2c82b2 100644 --- a/src/cryptonote_core/cryptonote_format_utils.cpp +++ b/src/cryptonote_core/cryptonote_format_utils.cpp @@ -61,8 +61,8 @@ namespace cryptonote keypair txkey = keypair::generate(); add_tx_pub_key_to_extra(tx, txkey.pub); - if(extra_nonce.size()) - if(!add_tx_extra_nonce(tx, extra_nonce)) + if(!extra_nonce.empty()) + if(!add_extra_nonce_to_tx_extra(tx.extra, extra_nonce)) return false; txin_gen in; @@ -74,6 +74,10 @@ namespace cryptonote LOG_PRINT_L0("Block is too big"); return false; } +#if defined(DEBUG_CREATE_BLOCK_TEMPLATE) + LOG_PRINT_L1("Creating block template: reward " << block_reward << + ", fee " << fee) +#endif block_reward += fee; std::vector out_amounts; @@ -204,51 +208,50 @@ namespace cryptonote return r; } //--------------------------------------------------------------- - crypto::public_key get_tx_pub_key_from_extra(const transaction& tx) + bool parse_tx_extra(const std::vector& tx_extra, std::vector& tx_extra_fields) { - crypto::public_key pk = null_pkey; - parse_and_validate_tx_extra(tx, pk); - return pk; + tx_extra_fields.clear(); + + if(tx_extra.empty()) + return true; + + std::string extra_str(reinterpret_cast(tx_extra.data()), tx_extra.size()); + std::istringstream iss(extra_str); + binary_archive ar(iss); + + bool eof = false; + while (!eof) + { + tx_extra_field field; + bool r = ::do_serialize(ar, field); + CHECK_AND_NO_ASSERT_MES(r, false, "failed to deserialize extra field. extra = " << string_tools::buff_to_hex_nodelimer(std::string(reinterpret_cast(tx_extra.data()), tx_extra.size()))); + tx_extra_fields.push_back(field); + + std::ios_base::iostate state = iss.rdstate(); + eof = (EOF == iss.peek()); + iss.clear(state); + } + CHECK_AND_NO_ASSERT_MES(::serialization::check_stream_state(ar), false, "failed to deserialize extra field. extra = " << string_tools::buff_to_hex_nodelimer(std::string(reinterpret_cast(tx_extra.data()), tx_extra.size()))); + + return true; } //--------------------------------------------------------------- - bool parse_and_validate_tx_extra(const transaction& tx, crypto::public_key& tx_pub_key) + crypto::public_key get_tx_pub_key_from_extra(const std::vector& tx_extra) { - tx_pub_key = null_pkey; - bool padding_started = false; //let the padding goes only at the end - bool tx_extra_tag_pubkey_found = false; - bool tx_extra_extra_nonce_found = false; - for(size_t i = 0; i != tx.extra.size();) - { - if(padding_started) - { - CHECK_AND_ASSERT_MES(!tx.extra[i], false, "Failed to parse transaction extra (not 0 after padding) in tx " << get_transaction_hash(tx)); - } - else if(tx.extra[i] == TX_EXTRA_TAG_PUBKEY) - { - CHECK_AND_ASSERT_MES(sizeof(crypto::public_key) <= tx.extra.size()-1-i, false, "Failed to parse transaction extra (TX_EXTRA_TAG_PUBKEY have not enough bytes) in tx " << get_transaction_hash(tx)); - CHECK_AND_ASSERT_MES(!tx_extra_tag_pubkey_found, false, "Failed to parse transaction extra (duplicate TX_EXTRA_TAG_PUBKEY entry) in tx " << get_transaction_hash(tx)); - tx_pub_key = *reinterpret_cast(&tx.extra[i+1]); - i += 1 + sizeof(crypto::public_key); - tx_extra_tag_pubkey_found = true; - continue; - }else if(tx.extra[i] == TX_EXTRA_NONCE) - { - //CHECK_AND_ASSERT_MES(is_coinbase(tx), false, "Failed to parse transaction extra (TX_EXTRA_NONCE can be only in coinbase) in tx " << get_transaction_hash(tx)); - CHECK_AND_ASSERT_MES(!tx_extra_extra_nonce_found, false, "Failed to parse transaction extra (duplicate TX_EXTRA_NONCE entry) in tx " << get_transaction_hash(tx)); - CHECK_AND_ASSERT_MES(tx.extra.size()-1-i >= 1, false, "Failed to parse transaction extra (TX_EXTRA_NONCE have not enough bytes) in tx " << get_transaction_hash(tx)); - ++i; - CHECK_AND_ASSERT_MES(tx.extra.size()-1-i >= tx.extra[i], false, "Failed to parse transaction extra (TX_EXTRA_NONCE have wrong bytes counter) in tx " << get_transaction_hash(tx)); - tx_extra_extra_nonce_found = true; - i += tx.extra[i];//actually don't need to extract it now, just skip - } - else if(!tx.extra[i]) - { - padding_started = true; - continue; - } - ++i; - } - return true; + std::vector tx_extra_fields; + if (!parse_tx_extra(tx_extra, tx_extra_fields)) + return null_pkey; + + tx_extra_pub_key pub_key_field; + if(!find_tx_extra_field_by_type(tx_extra_fields, pub_key_field)) + return null_pkey; + + return pub_key_field.pub_key; + } + //--------------------------------------------------------------- + crypto::public_key get_tx_pub_key_from_extra(const transaction& tx) + { + return get_tx_pub_key_from_extra(tx.extra); } //--------------------------------------------------------------- bool add_tx_pub_key_to_extra(transaction& tx, const crypto::public_key& tx_pub_key) @@ -259,32 +262,50 @@ namespace cryptonote return true; } //--------------------------------------------------------------- - bool add_tx_extra_nonce(transaction& tx, const blobdata& extra_nonce) + bool add_extra_nonce_to_tx_extra(std::vector& tx_extra, const blobdata& extra_nonce) { - CHECK_AND_ASSERT_MES(extra_nonce.size() <=255, false, "extra nonce could be 255 bytes max"); - size_t start_pos = tx.extra.size(); - tx.extra.resize(tx.extra.size() + 2 + extra_nonce.size()); + CHECK_AND_ASSERT_MES(extra_nonce.size() <= TX_EXTRA_NONCE_MAX_COUNT, false, "extra nonce could be 255 bytes max"); + size_t start_pos = tx_extra.size(); + tx_extra.resize(tx_extra.size() + 2 + extra_nonce.size()); //write tag - tx.extra[start_pos] = TX_EXTRA_NONCE; + tx_extra[start_pos] = TX_EXTRA_NONCE; //write len ++start_pos; - tx.extra[start_pos] = static_cast(extra_nonce.size()); + tx_extra[start_pos] = static_cast(extra_nonce.size()); //write data ++start_pos; - memcpy(&tx.extra[start_pos], extra_nonce.data(), extra_nonce.size()); + memcpy(&tx_extra[start_pos], extra_nonce.data(), extra_nonce.size()); return true; } //--------------------------------------------------------------- - bool construct_tx(const account_keys& sender_account_keys, const std::vector& sources, const std::vector& destinations, transaction& tx, uint64_t unlock_time) + void set_payment_id_to_tx_extra_nonce(blobdata& extra_nonce, const crypto::hash& payment_id) + { + extra_nonce.clear(); + extra_nonce.push_back(TX_EXTRA_NONCE_PAYMENT_ID); + const uint8_t* payment_id_ptr = reinterpret_cast(&payment_id); + std::copy(payment_id_ptr, payment_id_ptr + sizeof(payment_id), std::back_inserter(extra_nonce)); + } + //--------------------------------------------------------------- + bool get_payment_id_from_tx_extra_nonce(const blobdata& extra_nonce, crypto::hash& payment_id) + { + if(sizeof(crypto::hash) + 1 != extra_nonce.size()) + return false; + if(TX_EXTRA_NONCE_PAYMENT_ID != extra_nonce[0]) + return false; + payment_id = *reinterpret_cast(extra_nonce.data() + 1); + return true; + } + //--------------------------------------------------------------- + bool construct_tx(const account_keys& sender_account_keys, const std::vector& sources, const std::vector& destinations, std::vector extra, transaction& tx, uint64_t unlock_time) { tx.vin.clear(); tx.vout.clear(); tx.signatures.clear(); - tx.extra.clear(); tx.version = CURRENT_TRANSACTION_VERSION; tx.unlock_time = unlock_time; + tx.extra = extra; keypair txkey = keypair::generate(); add_tx_pub_key_to_extra(tx, txkey.pub); @@ -506,7 +527,10 @@ namespace cryptonote //--------------------------------------------------------------- bool lookup_acc_outs(const account_keys& acc, const transaction& tx, std::vector& outs, uint64_t& money_transfered) { - return lookup_acc_outs(acc, tx, get_tx_pub_key_from_extra(tx), outs, money_transfered); + crypto::public_key tx_pub_key = get_tx_pub_key_from_extra(tx); + if(null_pkey == tx_pub_key) + return false; + return lookup_acc_outs(acc, tx, tx_pub_key, outs, money_transfered); } //--------------------------------------------------------------- bool lookup_acc_outs(const account_keys& acc, const transaction& tx, const crypto::public_key& tx_pub_key, std::vector& outs, uint64_t& money_transfered) diff --git a/src/cryptonote_core/cryptonote_format_utils.h b/src/cryptonote_core/cryptonote_format_utils.h index 075202d6..138fb522 100644 --- a/src/cryptonote_core/cryptonote_format_utils.h +++ b/src/cryptonote_core/cryptonote_format_utils.h @@ -25,7 +25,7 @@ namespace cryptonote typedef std::pair output_entry; std::vector outputs; //index + key - size_t real_output; //index in outputs vector of real output_entry + size_t real_output; //index in outputs vector of real output_entry crypto::public_key real_out_tx_key; //incoming real tx public key size_t real_output_in_tx_index; //index in transaction outputs vector uint64_t amount; //money @@ -41,11 +41,26 @@ namespace cryptonote }; //--------------------------------------------------------------- - bool construct_tx(const account_keys& sender_account_keys, const std::vector& sources, const std::vector& destinations, transaction& tx, uint64_t unlock_time); - bool parse_and_validate_tx_extra(const transaction& tx, crypto::public_key& tx_pub_key); + bool construct_tx(const account_keys& sender_account_keys, const std::vector& sources, const std::vector& destinations, std::vector extra, transaction& tx, uint64_t unlock_time); + + template + bool find_tx_extra_field_by_type(const std::vector& tx_extra_fields, T& field) + { + auto it = std::find_if(tx_extra_fields.begin(), tx_extra_fields.end(), [](const tx_extra_field& f) { return typeid(T) == f.type(); }); + if(tx_extra_fields.end() == it) + return false; + + field = boost::get(*it); + return true; + } + + bool parse_tx_extra(const std::vector& tx_extra, std::vector& tx_extra_fields); + crypto::public_key get_tx_pub_key_from_extra(const std::vector& tx_extra); crypto::public_key get_tx_pub_key_from_extra(const transaction& tx); bool add_tx_pub_key_to_extra(transaction& tx, const crypto::public_key& tx_pub_key); - bool add_tx_extra_nonce(transaction& tx, const blobdata& extra_nonce); + bool add_extra_nonce_to_tx_extra(std::vector& tx_extra, const blobdata& extra_nonce); + void set_payment_id_to_tx_extra_nonce(blobdata& extra_nonce, const crypto::hash& payment_id); + bool get_payment_id_from_tx_extra_nonce(const blobdata& extra_nonce, crypto::hash& payment_id); bool is_out_to_acc(const account_keys& acc, const txout_to_key& out_key, const crypto::public_key& tx_pub_key, size_t output_index); bool lookup_acc_outs(const account_keys& acc, const transaction& tx, const crypto::public_key& tx_pub_key, std::vector& outs, uint64_t& money_transfered); bool lookup_acc_outs(const account_keys& acc, const transaction& tx, std::vector& outs, uint64_t& money_transfered); diff --git a/src/cryptonote_core/difficulty.cpp b/src/cryptonote_core/difficulty.cpp index 052f4666..3dde6ad6 100644 --- a/src/cryptonote_core/difficulty.cpp +++ b/src/cryptonote_core/difficulty.cpp @@ -24,7 +24,7 @@ namespace cryptonote { #include static inline void mul(uint64_t a, uint64_t b, uint64_t &low, uint64_t &high) { - low = UnsignedMultiply128(a, b, &high); + low = mul128(a, b, &high); } #else diff --git a/src/cryptonote_core/tx_extra.h b/src/cryptonote_core/tx_extra.h index 7e27cbc7..8cff085d 100644 --- a/src/cryptonote_core/tx_extra.h +++ b/src/cryptonote_core/tx_extra.h @@ -2,10 +2,93 @@ // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#pragma once +#pragma once +#define TX_EXTRA_PADDING_MAX_COUNT 255 +#define TX_EXTRA_NONCE_MAX_COUNT 255 -#define TX_EXTRA_PADDING_MAX_COUNT 40 +#define TX_EXTRA_TAG_PADDING 0x00 #define TX_EXTRA_TAG_PUBKEY 0x01 #define TX_EXTRA_NONCE 0x02 + +#define TX_EXTRA_NONCE_PAYMENT_ID 0x00 + +namespace cryptonote +{ + struct tx_extra_padding + { + size_t size; + + // load + template