From 7d8259f58c9d8d391a85b87dd9bef7dc280551ef Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Thu, 29 Sep 2022 19:18:05 +0200 Subject: [PATCH] added code for handling chain detachment(reorg) --- src/currency_core/currency_format_utils.cpp | 40 ++++++++++++++------- src/wallet/wallet2.cpp | 13 +++++-- src/wallet/wallet2.h | 2 ++ 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/src/currency_core/currency_format_utils.cpp b/src/currency_core/currency_format_utils.cpp index d0e5c424..34ba13b0 100644 --- a/src/currency_core/currency_format_utils.cpp +++ b/src/currency_core/currency_format_utils.cpp @@ -1711,6 +1711,13 @@ namespace currency return true; } + + crypto::hash get_asset_id_from_descriptor(const asset_descriptor_base& adb) + { + return get_hash_from_POD_objects(CRYPTO_HDS_ASSET_ID, adb.owner); + } + + bool construct_tx(const account_keys& sender_account_keys, const finalize_tx_param& ftp, finalized_tx& result) { const std::vector& sources = ftp.sources; @@ -1797,6 +1804,19 @@ namespace currency //std::vector zc_sources; //std::vector NLSAG_sources; + crypto::hash asset_id_for_destinations = currency::null_hash; + if (tx.version > TRANSACTION_VERSION_PRE_HF4) + { + asset_descriptor_operation ado = AUTO_VAL_INIT(ado); + if (get_type_in_variant_container(tx.extra, ado)) + { + crypto::secret_key stub = AUTO_VAL_INIT(stub); + bool r = derive_key_pair_from_key_pair(sender_account_keys.account_address.spend_public_key, one_time_secret_key, stub, ado.descriptor.owner, CRYPTO_HDS_ASSET_CONTROL_KEY); + CHECK_AND_ASSERT_MES(r, false, "Failed to derive_public_key_from_tx_and_account_pub_key()"); + //also assign this asset id to destinations + asset_id_for_destinations = get_asset_id_from_descriptor(ado.descriptor); + } + } std::vector in_contexts; @@ -1927,6 +1947,14 @@ namespace currency // "Shuffle" outs std::vector shuffled_dsts(destinations); + if (asset_id_for_destinations != currency::null_hash) + { + //must be asset publication + for (auto& item : shuffled_dsts) + { + item.asset_id = asset_id_for_destinations; + } + } if (shuffle) std::sort(shuffled_dsts.begin(), shuffled_dsts.end(), [](const tx_destination_entry& de1, const tx_destination_entry& de2) { return de1.amount < de2.amount; }); @@ -1981,18 +2009,6 @@ namespace currency } } - - if (tx.version > TRANSACTION_VERSION_PRE_HF4) - { - asset_descriptor_operation ado = AUTO_VAL_INIT(ado); - if (get_type_in_variant_container(tx.extra, ado)) - { - crypto::secret_key stub = AUTO_VAL_INIT(stub); - bool r = derive_key_pair_from_key_pair(sender_account_keys.account_address.spend_public_key, one_time_secret_key, stub, ado.descriptor.owner, CRYPTO_HDS_ASSET_CONTROL_KEY); - CHECK_AND_ASSERT_MES(r, false, "Failed to derive_public_key_from_tx_and_account_pub_key()"); - } - } - //sort inputs and mapping if it's zarcanum hardfork if (tx.version > TRANSACTION_VERSION_PRE_HF4) { diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 9462b9dc..b95a535c 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -775,10 +775,10 @@ void wallet2::process_new_transaction(const currency::transaction& tx, uint64_t LOG_ERROR("Public key from asset_descriptor_operation(" << ado.descriptor.owner << ") not much with derived public key(" << self_check << "), for tx" << get_transaction_hash(tx)); } else - { - + { wallet_own_asset_context& asset_context = m_own_asset_descriptors[get_hash_from_POD_objects(CRYPTO_HDS_ASSET_ID, ado.descriptor.owner)]; asset_context.asset_descriptor = ado.descriptor; + asset_context.height = height; std::stringstream ss; ss << "New Asset Registered:" << ENDL << "Name: " << asset_context.asset_descriptor.full_name @@ -2550,6 +2550,15 @@ void wallet2::detach_blockchain(uint64_t including_height) ++it; } + //asset descriptors + for (auto it = m_own_asset_descriptors.begin(); it != m_own_asset_descriptors.end(); ) + { + if (including_height <= it->second.height) + it = m_own_asset_descriptors.erase(it); + else + ++it; + } + WLT_LOG_L0("Detached blockchain on height " << including_height << ", transfers detached " << transfers_detached << ", blocks detached " << blocks_detached); } //---------------------------------------------------------------------------------------------------- diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 27acfc69..2d45cc8c 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -510,10 +510,12 @@ namespace tools { currency::asset_descriptor_base asset_descriptor; crypto::secret_key control_key; + uint64_t height = 0; BEGIN_BOOST_SERIALIZATION() BOOST_SERIALIZE(asset_descriptor) BOOST_SERIALIZE(control_key) + BOOST_SERIALIZE(height) END_BOOST_SERIALIZATION() };