1
0
Fork 0
forked from lthn/blockchain

ca: validate_tx_semantic() refactored, bare balance check made explicit

This commit is contained in:
sowle 2023-02-21 01:35:03 +01:00
parent a813484a4f
commit b733e5561a
No known key found for this signature in database
GPG key ID: C07A24B2D89D49FC
2 changed files with 23 additions and 29 deletions

View file

@ -21,27 +21,27 @@ namespace currency
//-----------------------------------------------------------------------------------------------
bool check_tx_inputs_keyimages_diff(const transaction& tx)
{
std::unordered_set<crypto::key_image> ki;
for(const auto& in : tx.vin)
std::unordered_set<crypto::key_image> 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;
}
}

View file

@ -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);
}