From 37e6a685095131a3185f61ec06f6e4c1d9634239 Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Fri, 1 Sep 2023 11:59:23 +0200 Subject: [PATCH] attempt to fix issue with broken versioning in tx versioned structs --- src/currency_core/currency_basic.h | 15 ++++++++++++--- .../currency_protocol_handler.inl | 6 +++++- src/serialization/serialization.h | 18 ++++++++++++++---- src/wallet/wallet_public_structs_defs.h | 5 ++--- tests/performance_tests/construct_tx.h | 19 +++++++++++++++++++ tests/performance_tests/main.cpp | 4 ++-- 6 files changed, 54 insertions(+), 13 deletions(-) diff --git a/src/currency_core/currency_basic.h b/src/currency_core/currency_basic.h index b9b85f20..45506cec 100644 --- a/src/currency_core/currency_basic.h +++ b/src/currency_core/currency_basic.h @@ -748,8 +748,9 @@ namespace currency std::string meta_info; crypto::public_key owner = currency::null_pkey; // consider premultipling by 1/8 bool hidden_supply = false; + uint8_t version = 0; - BEGIN_VERSIONED_SERIALIZE(0) + BEGIN_VERSIONED_SERIALIZE(0, version) FIELD(total_max_supply) FIELD(current_supply) FIELD(decimal_point) @@ -809,6 +810,7 @@ namespace currency #define ASSET_DESCRIPTOR_OPERATION_UPDATE 3 #define ASSET_DESCRIPTOR_OPERATION_PUBLIC_BURN 4 +#define ASSET_DESCRIPTOR_OPERATION_STRUCTURE_VER 1 struct asset_descriptor_operation { @@ -817,8 +819,9 @@ namespace currency boost::optional opt_amount_commitment; // premultiplied by 1/8 boost::optional opt_proof; // operation proof - for update/emit boost::optional opt_asset_id; // target asset_id - for update/emit + uint8_t verion = ASSET_DESCRIPTOR_OPERATION_STRUCTURE_VER; - BEGIN_VERSIONED_SERIALIZE(1) + BEGIN_VERSIONED_SERIALIZE(ASSET_DESCRIPTOR_OPERATION_STRUCTURE_VER, verion) FIELD(operation_type) FIELD(descriptor) FIELD(opt_amount_commitment) @@ -842,8 +845,9 @@ namespace currency // linear composition proof for the fact amount_commitment = lin(asset_id, G) boost::optional opt_amount_commitment_composition_proof; // for hidden supply boost::optional opt_amount_commitment_g_proof; // for non-hidden supply, proofs that amount_commitment - supply * asset_id = lin(G) + uint8_t version = 0; - BEGIN_VERSIONED_SERIALIZE(0) + BEGIN_VERSIONED_SERIALIZE(0, version) FIELD(opt_amount_commitment_composition_proof) FIELD(opt_amount_commitment_g_proof) END_SERIALIZE() @@ -851,6 +855,8 @@ namespace currency BEGIN_BOOST_SERIALIZATION() BOOST_SERIALIZE(opt_amount_commitment_composition_proof) BOOST_SERIALIZE(opt_amount_commitment_g_proof) + BOOST_END_VERSION_UNDER(1) + BOOST_SERIALIZE(version) END_BOOST_SERIALIZATION() }; @@ -1160,6 +1166,9 @@ BLOB_SERIALIZER(currency::txout_to_key); VARIANT_TAG(json_archive, type_name, json_tag) +BOOST_CLASS_VERSION(currency::asset_descriptor_operation, 1); +BOOST_CLASS_VERSION(currency::asset_operation_proof, 1); + // txin_v variant currency SET_VARIANT_TAGS(currency::txin_gen, 0, "gen"); diff --git a/src/currency_protocol/currency_protocol_handler.inl b/src/currency_protocol/currency_protocol_handler.inl index 5c654e96..a23e85b5 100644 --- a/src/currency_protocol/currency_protocol_handler.inl +++ b/src/currency_protocol/currency_protocol_handler.inl @@ -561,6 +561,8 @@ namespace currency } context.m_priv.m_requested_objects.erase(req_it); + + LOG_PRINT_L4("[NOTIFY_RESPONSE_GET_OBJECTS] BLOCK " << get_block_hash(b) << "[" << get_block_height(b) << "/" << count << "], txs: " << b.tx_hashes.size()); } LOG_PRINT_CYAN("Block parsing time avr: " << (count > 0 ? total_blocks_parsing_time / count : 0) << " mcs, total for " << count << " blocks: " << total_blocks_parsing_time / 1000 << " ms", LOG_LEVEL_2); @@ -581,12 +583,13 @@ namespace currency for (const block_complete_entry& block_entry : arg.blocks) { CHECK_STOP_FLAG__DROP_AND_RETURN_IF_SET(1, "Blocks processing interrupted, connection dropped"); - block_verification_context bvc = boost::value_initialized(); //process transactions + size_t count_txs = 0; TIME_MEASURE_START(transactions_process_time); for (const auto& tx_blob : block_entry.txs) { + LOG_PRINT_L4("[NOTIFY_RESPONSE_GET_OBJECTS] BL/TX ["<< count << "/" << count_txs << "]: " << epst::buff_to_hex_nodelimer(tx_blob)); CHECK_STOP_FLAG__DROP_AND_RETURN_IF_SET(1, "Block txs processing interrupted, connection dropped"); crypto::hash tx_id = null_hash; transaction tx = AUTO_VAL_INIT(tx); @@ -598,6 +601,7 @@ namespace currency return 1; } bvc.m_onboard_transactions[tx_id] = tx; + count_txs++; // tx_verification_context tvc = AUTO_VAL_INIT(tvc); // m_core.handle_incoming_tx(tx_blob, tvc, true); // if(tvc.m_verification_failed) diff --git a/src/serialization/serialization.h b/src/serialization/serialization.h index df2ab1f7..bb61001e 100644 --- a/src/serialization/serialization.h +++ b/src/serialization/serialization.h @@ -102,12 +102,22 @@ do { \ do { \ _ser_ar.tag("VERSION"); \ if (!_ser_ar.stream().good()){break;} \ - s_version = s_current_version = ver; \ - _ser_ar.serialize_varint(s_version); \ + _ser_ar.serialize_varint(s_version); \ if (!_ser_ar.stream().good()) return false; \ if(s_version > s_current_version) return false; \ } while (0); +#define VERSION_TO_MEMBER(last_ver, this_version_member) \ +do { \ + _ser_ar.tag("VERSION"); \ + if (!_ser_ar.stream().good()){break;} \ + _ser_ar.serialize_varint(this_version_member); \ + if (!_ser_ar.stream().good()) return false; \ + if(this_version_member > last_ver) return false; \ + s_version = this_version_member; \ +} while (0); + + /* #define CURRENT_VERSION(v) \ do { \ @@ -120,9 +130,9 @@ do { \ if(s_version < x ) {return true;} -#define BEGIN_VERSIONED_SERIALIZE(ver) \ +#define BEGIN_VERSIONED_SERIALIZE(last_ver, this_version_member) \ BEGIN_SERIALIZE() \ - VERSION(ver) + VERSION_TO_MEMBER(last_ver, this_version_member) #define DEFINE_SERIALIZATION_VERSION(v) inline static uint32_t get_serialization_version() { return v; } diff --git a/src/wallet/wallet_public_structs_defs.h b/src/wallet/wallet_public_structs_defs.h index c5ec61cd..16706966 100644 --- a/src/wallet/wallet_public_structs_defs.h +++ b/src/wallet/wallet_public_structs_defs.h @@ -1347,7 +1347,7 @@ namespace wallet_public crypto::secret_key one_time_skey; BEGIN_SERIALIZE_OBJECT() - VERSION(0) + VERSION(0) //use VERSION_TO_MEMBER if it's more then 0 FIELD(gen_context) FIELD(one_time_skey) END_SERIALIZE() @@ -1358,9 +1358,8 @@ namespace wallet_public currency::transaction tx_template; std::string encrypted_context; //ionic_swap_proposal_context encrypted with derivation - BEGIN_SERIALIZE_OBJECT() - VERSION(0) + VERSION(0) //use VERSION_TO_MEMBER if it's more then 0 FIELD(tx_template) FIELD(encrypted_context) END_SERIALIZE() diff --git a/tests/performance_tests/construct_tx.h b/tests/performance_tests/construct_tx.h index 682a4b84..97376064 100644 --- a/tests/performance_tests/construct_tx.h +++ b/tests/performance_tests/construct_tx.h @@ -50,3 +50,22 @@ private: std::vector m_destinations; currency::transaction m_tx; }; + + + + +void parse_weird_tx() +{ + const std::string expected_hash = "62ee9fb89293f042cbcebb02d05fa15036ffca2bdeb4c48162f2d93a420e5974"; + std::string tx_hex_buff; + epee::file_io_utils::load_file_to_string("E:\\tmp\\tx_as_hex.txt", tx_hex_buff); + std::string tx_blob; + epee::string_tools::parse_hexstr_to_binbuff(tx_hex_buff, tx_blob); + currency::transaction tx; + bool r = currency::parse_and_validate_tx_from_blob(tx_blob, tx); + std::string res_hash = epee::string_tools::pod_to_hex(currency::get_transaction_hash(tx)); + if (res_hash != expected_hash) + { + std::cout << "this is sad"; + } +} \ No newline at end of file diff --git a/tests/performance_tests/main.cpp b/tests/performance_tests/main.cpp index e8d7a2ed..ca968b0f 100644 --- a/tests/performance_tests/main.cpp +++ b/tests/performance_tests/main.cpp @@ -35,8 +35,8 @@ int main(int argc, char** argv) epee::log_space::log_singletone::get_default_log_file().c_str(), epee::log_space::log_singletone::get_default_log_folder().c_str()); - - thread_pool_tests(); + parse_weird_tx(); + //thread_pool_tests(); // std::string buf1 = tools::get_varint_data(CURRENCY_PUBLIC_ADDRESS_BASE58_PREFIX); // std::string buf2 = tools::get_varint_data(CURRENCY_PUBLIC_INTEG_ADDRESS_BASE58_PREFIX);