From b733e5561abb763cbe5c3474030d29447fa5ad10 Mon Sep 17 00:00:00 2001 From: sowle Date: Tue, 21 Feb 2023 01:35:03 +0100 Subject: [PATCH] ca: validate_tx_semantic() refactored, bare balance check made explicit --- src/currency_core/tx_semantic_validation.cpp | 46 +++++++++----------- src/currency_core/tx_semantic_validation.h | 6 +-- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/src/currency_core/tx_semantic_validation.cpp b/src/currency_core/tx_semantic_validation.cpp index 525aad37..78feebe8 100644 --- a/src/currency_core/tx_semantic_validation.cpp +++ b/src/currency_core/tx_semantic_validation.cpp @@ -21,27 +21,27 @@ namespace currency //----------------------------------------------------------------------------------------------- bool check_tx_inputs_keyimages_diff(const transaction& tx) { - std::unordered_set ki; - for(const auto& in : tx.vin) + std::unordered_set key_images; + crypto::key_image ki{}; + for(const auto& in_v : tx.vin) { - if (in.type() == typeid(txin_to_key)) + if (get_key_image_from_txin_v(in_v, ki)) { - CHECKED_GET_SPECIFIC_VARIANT(in, const txin_to_key, tokey_in, false); - if (!ki.insert(tokey_in.k_image).second) - return false; - } - else if (in.type() == typeid(txin_htlc)) - { - CHECKED_GET_SPECIFIC_VARIANT(in, const txin_htlc, htlc_in, false); - if (!ki.insert(htlc_in.k_image).second) + if (!key_images.insert(ki).second) return false; } } return true; } //----------------------------------------------------------------------------------------------- - bool validate_tx_semantic(const transaction& tx, size_t tx_block_size) + bool validate_tx_semantic(const transaction& tx, size_t tx_blob_size) { + if (tx_blob_size >= CURRENCY_MAX_TRANSACTION_BLOB_SIZE) + { + LOG_PRINT_RED_L0("tx blob size is " << tx_blob_size << ", it is greater than or equal to allowed maximum of " << CURRENCY_MAX_TRANSACTION_BLOB_SIZE); + return false; + } + if (!tx.vin.size()) { LOG_PRINT_RED_L0("tx with empty inputs, rejected for tx id= " << get_transaction_hash(tx)); @@ -56,7 +56,7 @@ namespace currency if (!check_outs_valid(tx)) { - LOG_PRINT_RED_L0("tx with invalid outputs, rejected for tx id= " << get_transaction_hash(tx)); + LOG_PRINT_RED_L0("tx has invalid outputs, rejected for tx id= " << get_transaction_hash(tx)); return false; } @@ -66,18 +66,6 @@ namespace currency return false; } - if (!check_tx_balance(tx)) - { - LOG_PRINT_RED_L0("balance check failed for tx " << get_transaction_hash(tx)); - return false; - } - - if (tx_block_size >= CURRENCY_MAX_TRANSACTION_BLOB_SIZE) - { - LOG_PRINT_RED_L0("tx has too big size " << tx_block_size << ", expected no bigger than " << CURRENCY_BLOCK_GRANTED_FULL_REWARD_ZONE); - return false; - } - //check if tx use different key images if (!check_tx_inputs_keyimages_diff(tx)) { @@ -91,6 +79,14 @@ namespace currency return false; } + // inexpensive check for pre-HF4 txs + // post-HF4 txs balance are being checked in check_tx_balance() + if (!check_tx_bare_balance(tx)) + { + LOG_PRINT_RED_L0("balance check failed for tx " << get_transaction_hash(tx)); + return false; + } + return true; } } \ No newline at end of file diff --git a/src/currency_core/tx_semantic_validation.h b/src/currency_core/tx_semantic_validation.h index 2e6f4790..03a946c7 100644 --- a/src/currency_core/tx_semantic_validation.h +++ b/src/currency_core/tx_semantic_validation.h @@ -1,16 +1,14 @@ -// Copyright (c) 2018-2019 Zano Project +// Copyright (c) 2018-2023 Zano Project // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. - #pragma once - #include "include_base_utils.h" #include "currency_format_utils_transactions.h" namespace currency { - //check correct values, amounts and all lightweight checks not related with database + //check correct values, ins and outs types, amounts and all lightweight checks not related to the database bool validate_tx_semantic(const transaction& tx, size_t tx_block_size); }