From 2339a65ff77c0393b74985100d3947b7d5eae5d3 Mon Sep 17 00:00:00 2001 From: sowle Date: Mon, 19 Feb 2024 23:18:22 +0100 Subject: [PATCH] calculate_asset_id refactored into get_or_calculate_asset_id (2) --- src/currency_core/blockchain_storage.cpp | 29 +++++++-------------- src/currency_core/currency_format_utils.cpp | 2 +- src/wallet/wallet2.cpp | 27 +++++++++---------- 3 files changed, 23 insertions(+), 35 deletions(-) diff --git a/src/currency_core/blockchain_storage.cpp b/src/currency_core/blockchain_storage.cpp index 466aa7f5..17ed1fbd 100644 --- a/src/currency_core/blockchain_storage.cpp +++ b/src/currency_core/blockchain_storage.cpp @@ -3828,14 +3828,7 @@ bool blockchain_storage::unprocess_blockchain_tx_extra(const transaction& tx) if (ei.m_asset_operation.operation_type != ASSET_DESCRIPTOR_OPERATION_UNDEFINED) { crypto::public_key asset_id = currency::null_pkey; - if (ei.m_asset_operation.operation_type == ASSET_DESCRIPTOR_OPERATION_REGISTER) - { - calculate_asset_id(ei.m_asset_operation.descriptor.owner, nullptr, &asset_id); - } - else - { - CHECK_AND_NO_ASSERT_MES(false, false, "asset operation not implemented"); - } + CHECK_AND_ASSERT_MES(get_or_calculate_asset_id(ei.m_asset_operation, nullptr, &asset_id), false, "get_or_calculate_asset_id failed"); r = pop_asset_info(asset_id); CHECK_AND_ASSERT_MES(r, false, "failed to pop_alias_info"); } @@ -4086,12 +4079,9 @@ bool blockchain_storage::pop_asset_info(const crypto::public_key& asset_id) //------------------------------------------------------------------ bool blockchain_storage::validate_ado_ownership(asset_op_verification_context& avc) { -// asset_id = AUTO_VAL_INIT(asset_id); -// CHECK_AND_ASSERT_MES(validate_asset_operation_balance_proof(tx, tx_id, ado, asset_id), false, "asset operation validation failed!"); asset_operation_ownership_proof aoop = AUTO_VAL_INIT(aoop); bool r = get_type_in_variant_container(avc.tx.proofs, aoop); - CHECK_AND_ASSERT_MES(r, false, "Ownership validation failed - missing signature"); - + CHECK_AND_ASSERT_MES(r, false, "Ownership validation failed - missing signature (asset_operation_ownership_proof)"); CHECK_AND_ASSERT_MES(avc.asset_op_history->size() != 0, false, "asset with id " << avc.asset_id << " has invalid history size() == 0"); @@ -4107,7 +4097,8 @@ bool blockchain_storage::put_asset_info(const transaction& tx, const crypto::has if (ado.operation_type == ASSET_DESCRIPTOR_OPERATION_REGISTER) { - calculate_asset_id(avc.ado.descriptor.owner, &avc.asset_id_pt, &avc.asset_id); + CHECK_AND_ASSERT_MES(get_or_calculate_asset_id(avc.ado, &avc.asset_id_pt, &avc.asset_id), false, "get_or_calculate_asset_id failed"); + avc.asset_op_history = m_db_assets.find(avc.asset_id); CHECK_AND_ASSERT_MES(!avc.asset_op_history, false, "asset with id " << avc.asset_id << " has already been registered"); @@ -4121,10 +4112,8 @@ bool blockchain_storage::put_asset_info(const transaction& tx, const crypto::has } else { - CHECK_AND_ASSERT_MES(avc.ado.opt_asset_id, false, "asset_id not provided for asset altering operation"); - avc.asset_op_history = m_db_assets.find(*avc.ado.opt_asset_id); - avc.asset_id = *avc.ado.opt_asset_id; // consider redisign - avc.asset_id_pt.from_public_key(avc.asset_id); + CHECK_AND_ASSERT_MES(get_or_calculate_asset_id(avc.ado, &avc.asset_id_pt, &avc.asset_id), false, "get_or_calculate_asset_id failed"); + avc.asset_op_history = m_db_assets.find(avc.asset_id); CHECK_AND_ASSERT_MES(avc.asset_op_history && avc.asset_op_history->size(), false, "asset with id " << avc.asset_id << " has not been registered"); // check ownership permission @@ -4159,7 +4148,7 @@ bool blockchain_storage::put_asset_info(const transaction& tx, const crypto::has } else { - LOG_ERROR("Unknown operation type: " << ado.operation_type); + LOG_ERROR("Unknown operation type: " << (int)ado.operation_type); return false; } @@ -4171,7 +4160,7 @@ bool blockchain_storage::put_asset_info(const transaction& tx, const crypto::has assets_container::t_value_type local_asset_history = *avc.asset_op_history; local_asset_history.push_back(ado); - m_db_assets.set(*avc.ado.opt_asset_id, local_asset_history); + m_db_assets.set(avc.asset_id, local_asset_history); switch(ado.operation_type) { @@ -4185,7 +4174,7 @@ bool blockchain_storage::put_asset_info(const transaction& tx, const crypto::has LOG_PRINT_MAGENTA("[ASSET_BURNT]: " << print_money_brief(avc.amount_to_validate, ado.descriptor.decimal_point) << ", " << avc.asset_id << ": " << ado.descriptor.ticker << ", \"" << ado.descriptor.full_name << "\"", LOG_LEVEL_1); break; default: - LOG_ERROR("Unknown operation type: " << ado.operation_type); + LOG_ERROR("Unknown operation type: " << (int)ado.operation_type); } } diff --git a/src/currency_core/currency_format_utils.cpp b/src/currency_core/currency_format_utils.cpp index b5b05421..89102d2e 100644 --- a/src/currency_core/currency_format_utils.cpp +++ b/src/currency_core/currency_format_utils.cpp @@ -2118,7 +2118,7 @@ namespace currency ado.operation_type == ASSET_DESCRIPTOR_OPERATION_UPDATE || ado.operation_type == ASSET_DESCRIPTOR_OPERATION_PUBLIC_BURN ) { - CHECK_AND_ASSERT_MES(ado.opt_asset_id.has_value(), false, "ado.opt_asset_id has no value, op type = " << (int)ado.operation_type); + CHECK_AND_ASSERT_MES(ado.opt_asset_id.has_value(), false, "ado.opt_asset_id has no value, op: " << (int)ado.operation_type << ", " << get_asset_operation_type_string(ado.operation_type)); if (p_result_pub_key) *p_result_pub_key = ado.opt_asset_id.get(); if (p_result_point) diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index cb498fca..d94b717a 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -400,14 +400,15 @@ void wallet2::process_ado_in_new_transaction(const currency::asset_descriptor_op { do { + crypto::public_key asset_id{}; + if (ado.operation_type != ASSET_DESCRIPTOR_OPERATION_UNDEFINED) + WLT_THROW_IF_FALSE_WALLET_CMN_ERR_EX(get_or_calculate_asset_id(ado, nullptr, &asset_id), "get_or_calculate_asset_id failed"); + if (ado.operation_type == ASSET_DESCRIPTOR_OPERATION_REGISTER) { if (ado.descriptor.owner != m_account.get_public_address().spend_public_key) - { break; - } - crypto::public_key asset_id{}; - calculate_asset_id(ado.descriptor.owner, nullptr, &asset_id); + WLT_THROW_IF_FALSE_WALLET_CMN_ERR_EX(m_own_asset_descriptors.count(asset_id) == 0, "asset with asset_id " << asset_id << " has already been registered in the wallet as own asset"); wallet_own_asset_context& asset_context = m_own_asset_descriptors[asset_id]; asset_context.asset_descriptor = ado.descriptor; @@ -429,8 +430,7 @@ void wallet2::process_ado_in_new_transaction(const currency::asset_descriptor_op } else if (ado.operation_type == ASSET_DESCRIPTOR_OPERATION_EMIT || ado.operation_type == ASSET_DESCRIPTOR_OPERATION_PUBLIC_BURN) { - WLT_THROW_IF_FALSE_WALLET_CMN_ERR_EX(ado.opt_asset_id, get_asset_operation_type_string(ado.operation_type) << " failed with empty opt_asset_id"); - auto it = m_own_asset_descriptors.find(*ado.opt_asset_id); + auto it = m_own_asset_descriptors.find(asset_id); if (it == m_own_asset_descriptors.end()) break; //asset had been updated @@ -439,20 +439,19 @@ void wallet2::process_ado_in_new_transaction(const currency::asset_descriptor_op } else if (ado.operation_type == ASSET_DESCRIPTOR_OPERATION_UPDATE ) { - WLT_THROW_IF_FALSE_WALLET_CMN_ERR_EX(ado.opt_asset_id, get_asset_operation_type_string(ado.operation_type) << " failed with empty opt_asset_id"); - auto it = m_own_asset_descriptors.find(*ado.opt_asset_id); + auto it = m_own_asset_descriptors.find(asset_id); if (it == m_own_asset_descriptors.end()) { if (ado.descriptor.owner == m_account.get_public_address().spend_public_key) { // ownership of the asset acquired - wallet_own_asset_context& asset_context = m_own_asset_descriptors[*ado.opt_asset_id]; + wallet_own_asset_context& asset_context = m_own_asset_descriptors[asset_id]; asset_context.asset_descriptor = ado.descriptor; std::stringstream ss; ss << "Asset ownership acquired:" - << ENDL << "asset id: " << *ado.opt_asset_id + << ENDL << "asset id: " << asset_id << ENDL << "Name: " << ado.descriptor.full_name << ENDL << "Ticker: " << ado.descriptor.ticker << ENDL << "Total Max Supply: " << print_asset_money(ado.descriptor.total_max_supply, ado.descriptor.decimal_point) @@ -460,7 +459,7 @@ void wallet2::process_ado_in_new_transaction(const currency::asset_descriptor_op << ENDL << "Decimal Point: " << ado.descriptor.decimal_point; - add_rollback_event(ptc.height, asset_register_event{ *ado.opt_asset_id }); + add_rollback_event(ptc.height, asset_register_event{ asset_id }); WLT_LOG_MAGENTA(ss.str(), LOG_LEVEL_0); if (m_wcallback) m_wcallback->on_message(i_wallet2_callback::ms_yellow, ss.str()); @@ -482,7 +481,7 @@ void wallet2::process_ado_in_new_transaction(const currency::asset_descriptor_op std::stringstream ss; ss << "Asset ownership lost:" - << ENDL << "asset id: " << *ado.opt_asset_id + << ENDL << "asset id: " << asset_id << ENDL << "New owner: " << ado.descriptor.owner << ENDL << "Name: " << ado.descriptor.full_name << ENDL << "Ticker: " << ado.descriptor.ticker @@ -490,7 +489,7 @@ void wallet2::process_ado_in_new_transaction(const currency::asset_descriptor_op << ENDL << "Current Supply: " << print_asset_money(ado.descriptor.current_supply, ado.descriptor.decimal_point) << ENDL << "Decimal Point: " << ado.descriptor.decimal_point; - add_rollback_event(ptc.height, asset_register_event{ *ado.opt_asset_id }); + add_rollback_event(ptc.height, asset_register_event{ asset_id }); WLT_LOG_MAGENTA(ss.str(), LOG_LEVEL_0); if (m_wcallback) m_wcallback->on_message(i_wallet2_callback::ms_yellow, ss.str()); @@ -4915,7 +4914,7 @@ void wallet2::deploy_new_asset(const currency::asset_descriptor_base& asset_info currency::asset_descriptor_operation ado = AUTO_VAL_INIT(ado); bool r = get_type_in_variant_container(result_tx.extra, ado); CHECK_AND_ASSERT_THROW_MES(r, "Failed find asset info in tx"); - calculate_asset_id(ado.descriptor.owner, nullptr, &new_asset_id); + CHECK_AND_ASSERT_THROW_MES(get_or_calculate_asset_id(ado, nullptr, &new_asset_id), "get_or_calculate_asset_id failed"); m_custom_assets[new_asset_id] = ado.descriptor; }