2015-05-27 12:08:46 +00:00
|
|
|
// Copyright (c) 2012-2015, The CryptoNote developers, The Bytecoin developers
|
2014-08-13 10:38:35 +00:00
|
|
|
//
|
|
|
|
// This file is part of Bytecoin.
|
|
|
|
//
|
|
|
|
// Bytecoin is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Bytecoin is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with Bytecoin. If not, see <http://www.gnu.org/licenses/>.
|
2014-03-03 22:07:58 +00:00
|
|
|
|
|
|
|
#include "integer_overflow.h"
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
using namespace CryptoNote;
|
2014-03-03 22:07:58 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2014-08-13 10:51:37 +00:00
|
|
|
void split_miner_tx_outs(Transaction& miner_tx, uint64_t amount_1)
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
|
|
|
uint64_t total_amount = get_outs_money_amount(miner_tx);
|
|
|
|
uint64_t amount_2 = total_amount - amount_1;
|
2014-08-13 10:51:37 +00:00
|
|
|
TransactionOutputTarget target = miner_tx.vout[0].target;
|
2014-03-03 22:07:58 +00:00
|
|
|
|
|
|
|
miner_tx.vout.clear();
|
|
|
|
|
2014-08-13 10:51:37 +00:00
|
|
|
TransactionOutput out1;
|
2014-03-03 22:07:58 +00:00
|
|
|
out1.amount = amount_1;
|
|
|
|
out1.target = target;
|
|
|
|
miner_tx.vout.push_back(out1);
|
|
|
|
|
2014-08-13 10:51:37 +00:00
|
|
|
TransactionOutput out2;
|
2014-03-03 22:07:58 +00:00
|
|
|
out2.amount = amount_2;
|
|
|
|
out2.target = target;
|
|
|
|
miner_tx.vout.push_back(out2);
|
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
void append_tx_source_entry(std::vector<CryptoNote::tx_source_entry>& sources, const Transaction& tx, size_t out_idx)
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
2015-05-27 12:08:46 +00:00
|
|
|
CryptoNote::tx_source_entry se;
|
2014-03-03 22:07:58 +00:00
|
|
|
se.amount = tx.vout[out_idx].amount;
|
2015-05-27 12:08:46 +00:00
|
|
|
se.outputs.push_back(std::make_pair(0, boost::get<CryptoNote::TransactionOutputToKey>(tx.vout[out_idx].target).key));
|
2014-03-03 22:07:58 +00:00
|
|
|
se.real_output = 0;
|
|
|
|
se.real_out_tx_key = get_tx_pub_key_from_extra(tx);
|
|
|
|
se.real_output_in_tx_index = out_idx;
|
|
|
|
|
|
|
|
sources.push_back(se);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//======================================================================================================================
|
|
|
|
|
|
|
|
gen_uint_overflow_base::gen_uint_overflow_base()
|
|
|
|
: m_last_valid_block_event_idx(static_cast<size_t>(-1))
|
|
|
|
{
|
|
|
|
REGISTER_CALLBACK_METHOD(gen_uint_overflow_1, mark_last_valid_block);
|
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
bool gen_uint_overflow_base::check_tx_verification_context(const CryptoNote::tx_verification_context& tvc, bool tx_added, size_t event_idx, const CryptoNote::Transaction& /*tx*/)
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
|
|
|
return m_last_valid_block_event_idx < event_idx ? !tx_added && tvc.m_verifivation_failed : tx_added && !tvc.m_verifivation_failed;
|
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
bool gen_uint_overflow_base::check_block_verification_context(const CryptoNote::block_verification_context& bvc, size_t event_idx, const CryptoNote::Block& /*block*/)
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
|
|
|
return m_last_valid_block_event_idx < event_idx ? bvc.m_verifivation_failed | bvc.m_marked_as_orphaned : !bvc.m_verifivation_failed;
|
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
bool gen_uint_overflow_base::mark_last_valid_block(CryptoNote::core& c, size_t ev_index, const std::vector<test_event_entry>& events)
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
|
|
|
m_last_valid_block_event_idx = ev_index - 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//======================================================================================================================
|
|
|
|
|
|
|
|
bool gen_uint_overflow_1::generate(std::vector<test_event_entry>& events) const
|
|
|
|
{
|
2014-04-29 16:26:45 +00:00
|
|
|
uint64_t ts_start = 1338224400;
|
2014-03-03 22:07:58 +00:00
|
|
|
|
|
|
|
GENERATE_ACCOUNT(miner_account);
|
|
|
|
MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start);
|
|
|
|
DO_CALLBACK(events, "mark_last_valid_block");
|
|
|
|
MAKE_ACCOUNT(events, bob_account);
|
|
|
|
MAKE_ACCOUNT(events, alice_account);
|
|
|
|
|
|
|
|
// Problem 1. Miner tx output overflow
|
|
|
|
MAKE_MINER_TX_MANUALLY(miner_tx_0, blk_0);
|
2014-08-13 10:51:37 +00:00
|
|
|
split_miner_tx_outs(miner_tx_0, m_currency.moneySupply());
|
|
|
|
Block blk_1;
|
|
|
|
if (!generator.constructBlockManually(blk_1, blk_0, miner_account, test_generator::bf_miner_tx, 0, 0, 0, crypto::hash(), 0, miner_tx_0))
|
2014-03-03 22:07:58 +00:00
|
|
|
return false;
|
|
|
|
events.push_back(blk_1);
|
|
|
|
|
|
|
|
// Problem 1. Miner tx outputs overflow
|
|
|
|
MAKE_MINER_TX_MANUALLY(miner_tx_1, blk_1);
|
2014-08-13 10:51:37 +00:00
|
|
|
split_miner_tx_outs(miner_tx_1, m_currency.moneySupply());
|
|
|
|
Block blk_2;
|
|
|
|
if (!generator.constructBlockManually(blk_2, blk_1, miner_account, test_generator::bf_miner_tx, 0, 0, 0, crypto::hash(), 0, miner_tx_1))
|
2014-03-03 22:07:58 +00:00
|
|
|
return false;
|
|
|
|
events.push_back(blk_2);
|
|
|
|
|
|
|
|
REWIND_BLOCKS(events, blk_2r, blk_2, miner_account);
|
2014-08-13 10:51:37 +00:00
|
|
|
MAKE_TX_LIST_START(events, txs_0, miner_account, bob_account, m_currency.moneySupply(), blk_2);
|
|
|
|
MAKE_TX_LIST(events, txs_0, miner_account, bob_account, m_currency.moneySupply(), blk_2);
|
2014-03-03 22:07:58 +00:00
|
|
|
MAKE_NEXT_BLOCK_TX_LIST(events, blk_3, blk_2r, miner_account, txs_0);
|
|
|
|
REWIND_BLOCKS(events, blk_3r, blk_3, miner_account);
|
|
|
|
|
|
|
|
// Problem 2. total_fee overflow, block_reward overflow
|
2015-05-27 12:08:46 +00:00
|
|
|
std::list<CryptoNote::Transaction> txs_1;
|
2014-03-03 22:07:58 +00:00
|
|
|
// Create txs with huge fee
|
2015-05-27 12:08:46 +00:00
|
|
|
txs_1.push_back(construct_tx_with_fee(m_logger, events, blk_3, bob_account, alice_account, MK_COINS(1), m_currency.moneySupply() - MK_COINS(1)));
|
|
|
|
txs_1.push_back(construct_tx_with_fee(m_logger, events, blk_3, bob_account, alice_account, MK_COINS(1), m_currency.moneySupply() - MK_COINS(1)));
|
2014-03-03 22:07:58 +00:00
|
|
|
MAKE_NEXT_BLOCK_TX_LIST(events, blk_4, blk_3r, miner_account, txs_1);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//======================================================================================================================
|
|
|
|
|
|
|
|
bool gen_uint_overflow_2::generate(std::vector<test_event_entry>& events) const
|
|
|
|
{
|
2014-04-29 16:26:45 +00:00
|
|
|
uint64_t ts_start = 1338224400;
|
2014-03-03 22:07:58 +00:00
|
|
|
|
|
|
|
GENERATE_ACCOUNT(miner_account);
|
|
|
|
MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start);
|
|
|
|
MAKE_ACCOUNT(events, bob_account);
|
|
|
|
MAKE_ACCOUNT(events, alice_account);
|
|
|
|
REWIND_BLOCKS(events, blk_0r, blk_0, miner_account);
|
|
|
|
DO_CALLBACK(events, "mark_last_valid_block");
|
|
|
|
|
|
|
|
// Problem 1. Regular tx outputs overflow
|
2015-05-27 12:08:46 +00:00
|
|
|
std::vector<CryptoNote::tx_source_entry> sources;
|
2014-08-13 10:51:37 +00:00
|
|
|
for (size_t i = 0; i < blk_0.minerTx.vout.size(); ++i)
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
2014-08-13 10:51:37 +00:00
|
|
|
if (m_currency.minimumFee() < blk_0.minerTx.vout[i].amount)
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
2014-08-13 10:51:37 +00:00
|
|
|
append_tx_source_entry(sources, blk_0.minerTx, i);
|
2014-03-03 22:07:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sources.empty())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
std::vector<CryptoNote::tx_destination_entry> destinations;
|
2014-08-13 10:51:37 +00:00
|
|
|
const AccountPublicAddress& bob_addr = bob_account.get_keys().m_account_address;
|
|
|
|
destinations.push_back(tx_destination_entry(m_currency.moneySupply(), bob_addr));
|
|
|
|
destinations.push_back(tx_destination_entry(m_currency.moneySupply() - 1, bob_addr));
|
|
|
|
// sources.front().amount = destinations[0].amount + destinations[2].amount + destinations[3].amount + m_currency.minimumFee()
|
|
|
|
destinations.push_back(tx_destination_entry(sources.front().amount - m_currency.moneySupply() - m_currency.moneySupply() + 1 - m_currency.minimumFee(), bob_addr));
|
2014-03-03 22:07:58 +00:00
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
CryptoNote::Transaction tx_1;
|
|
|
|
if (!construct_tx(miner_account.get_keys(), sources, destinations, std::vector<uint8_t>(), tx_1, 0, m_logger))
|
2014-03-03 22:07:58 +00:00
|
|
|
return false;
|
|
|
|
events.push_back(tx_1);
|
|
|
|
|
|
|
|
MAKE_NEXT_BLOCK_TX1(events, blk_1, blk_0r, miner_account, tx_1);
|
|
|
|
REWIND_BLOCKS(events, blk_1r, blk_1, miner_account);
|
|
|
|
|
|
|
|
// Problem 2. Regular tx inputs overflow
|
|
|
|
sources.clear();
|
|
|
|
for (size_t i = 0; i < tx_1.vout.size(); ++i)
|
|
|
|
{
|
|
|
|
auto& tx_1_out = tx_1.vout[i];
|
2014-08-13 10:51:37 +00:00
|
|
|
if (tx_1_out.amount < m_currency.moneySupply() - 1)
|
2014-03-03 22:07:58 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
append_tx_source_entry(sources, tx_1, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
destinations.clear();
|
2015-05-27 12:08:46 +00:00
|
|
|
CryptoNote::tx_destination_entry de;
|
2014-03-03 22:07:58 +00:00
|
|
|
de.addr = alice_account.get_keys().m_account_address;
|
2014-08-13 10:51:37 +00:00
|
|
|
de.amount = m_currency.moneySupply() - m_currency.minimumFee();
|
2014-03-03 22:07:58 +00:00
|
|
|
destinations.push_back(de);
|
|
|
|
destinations.push_back(de);
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
CryptoNote::Transaction tx_2;
|
|
|
|
if (!construct_tx(bob_account.get_keys(), sources, destinations, std::vector<uint8_t>(), tx_2, 0, m_logger))
|
2014-03-03 22:07:58 +00:00
|
|
|
return false;
|
|
|
|
events.push_back(tx_2);
|
|
|
|
|
|
|
|
MAKE_NEXT_BLOCK_TX1(events, blk_2, blk_1r, miner_account, tx_2);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|