danicoin/tests/core_tests/block_validation.h

361 lines
12 KiB
C
Raw Normal View History

2014-08-13 10:38:35 +00:00
// Copyright (c) 2012-2014, The CryptoNote developers, The Bytecoin developers
//
// 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
#pragma once
2014-08-13 10:51:37 +00:00
2014-03-03 22:07:58 +00:00
#include "chaingen.h"
2014-08-13 10:51:37 +00:00
const uint64_t UNDEF_HEIGHT = static_cast<uint64_t>(cryptonote::UpgradeDetectorBase::UNDEF_HEIGHT);
class CheckBlockPurged : public test_chain_unit_base {
2014-03-03 22:07:58 +00:00
public:
2014-08-13 10:51:37 +00:00
CheckBlockPurged(size_t invalidBlockIdx, uint8_t blockMajorVersion) :
m_invalidBlockIdx(invalidBlockIdx),
m_blockMajorVersion(blockMajorVersion) {
assert(blockMajorVersion == cryptonote::BLOCK_MAJOR_VERSION_1 || blockMajorVersion == cryptonote::BLOCK_MAJOR_VERSION_2);
cryptonote::CurrencyBuilder currencyBuilder;
currencyBuilder.upgradeHeight(blockMajorVersion == cryptonote::BLOCK_MAJOR_VERSION_1 ? UNDEF_HEIGHT : UINT64_C(0));
m_currency = currencyBuilder.currency();
REGISTER_CALLBACK("check_block_purged", CheckBlockPurged::check_block_purged);
REGISTER_CALLBACK("markInvalidBlock", CheckBlockPurged::markInvalidBlock);
2014-03-03 22:07:58 +00:00
}
2014-08-13 10:51:37 +00:00
bool check_block_verification_context(const cryptonote::block_verification_context& bvc, size_t eventIdx, const cryptonote::Block& /*blk*/) {
if (m_invalidBlockIdx == eventIdx) {
2014-03-03 22:07:58 +00:00
return bvc.m_verifivation_failed;
2014-08-13 10:51:37 +00:00
} else {
2014-03-03 22:07:58 +00:00
return !bvc.m_verifivation_failed;
2014-08-13 10:51:37 +00:00
}
2014-03-03 22:07:58 +00:00
}
2014-08-13 10:51:37 +00:00
bool check_block_purged(cryptonote::core& c, size_t eventIdx, const std::vector<test_event_entry>& events) {
DEFINE_TESTS_ERROR_CONTEXT("CheckBlockPurged::check_block_purged");
2014-03-03 22:07:58 +00:00
2014-08-13 10:51:37 +00:00
CHECK_TEST_CONDITION(m_invalidBlockIdx < eventIdx);
2014-03-03 22:07:58 +00:00
CHECK_EQ(0, c.get_pool_transactions_count());
2014-08-13 10:51:37 +00:00
CHECK_EQ(m_invalidBlockIdx, c.get_current_blockchain_height());
2014-03-03 22:07:58 +00:00
return true;
}
2014-08-13 10:51:37 +00:00
bool markInvalidBlock(cryptonote::core& c, size_t eventIdx, const std::vector<test_event_entry>& events) {
m_invalidBlockIdx = eventIdx + 1;
return true;
}
protected:
size_t m_invalidBlockIdx;
const uint8_t m_blockMajorVersion;
2014-03-03 22:07:58 +00:00
};
2014-08-13 10:51:37 +00:00
struct CheckBlockAccepted : public test_chain_unit_base {
CheckBlockAccepted(size_t expectedBlockchainHeight, uint8_t blockMajorVersion) :
m_expectedBlockchainHeight(expectedBlockchainHeight),
m_blockMajorVersion(blockMajorVersion) {
assert(blockMajorVersion == cryptonote::BLOCK_MAJOR_VERSION_1 || blockMajorVersion == cryptonote::BLOCK_MAJOR_VERSION_2);
cryptonote::CurrencyBuilder currencyBuilder;
currencyBuilder.upgradeHeight(blockMajorVersion == cryptonote::BLOCK_MAJOR_VERSION_1 ? UNDEF_HEIGHT : UINT64_C(0));
m_currency = currencyBuilder.currency();
REGISTER_CALLBACK("check_block_accepted", CheckBlockAccepted::check_block_accepted);
2014-03-03 22:07:58 +00:00
}
2014-08-13 10:51:37 +00:00
bool check_block_accepted(cryptonote::core& c, size_t /*eventIdx*/, const std::vector<test_event_entry>& /*events*/) {
DEFINE_TESTS_ERROR_CONTEXT("CheckBlockAccepted::check_block_accepted");
2014-03-03 22:07:58 +00:00
CHECK_EQ(0, c.get_pool_transactions_count());
2014-08-13 10:51:37 +00:00
CHECK_EQ(m_expectedBlockchainHeight, c.get_current_blockchain_height());
2014-03-03 22:07:58 +00:00
return true;
}
2014-08-13 10:51:37 +00:00
protected:
size_t m_expectedBlockchainHeight;
const uint8_t m_blockMajorVersion;
2014-03-03 22:07:58 +00:00
};
2014-08-13 10:51:37 +00:00
struct TestBlockMajorVersionAccepted : public CheckBlockAccepted {
TestBlockMajorVersionAccepted(uint8_t blockMajorVersion) :
CheckBlockAccepted(2, blockMajorVersion) {}
bool generate(std::vector<test_event_entry>& events) const;
};
struct TestBlockMajorVersionRejected : public CheckBlockPurged {
TestBlockMajorVersionRejected(uint8_t blockAcceptedVersion, uint8_t blockGeneratedVersion) :
CheckBlockPurged(1, blockAcceptedVersion), m_blockGeneratedVersion(blockGeneratedVersion) {}
const uint8_t m_blockGeneratedVersion;
bool generate(std::vector<test_event_entry>& events) const;
};
struct TestBlockBigMinorVersion : public CheckBlockAccepted {
TestBlockBigMinorVersion(uint8_t blockMajorVersion)
: CheckBlockAccepted(2, blockMajorVersion) {}
2014-03-03 22:07:58 +00:00
bool generate(std::vector<test_event_entry>& events) const;
};
2014-08-13 10:51:37 +00:00
struct gen_block_ts_not_checked : public CheckBlockAccepted
2014-03-03 22:07:58 +00:00
{
2014-08-13 10:51:37 +00:00
gen_block_ts_not_checked(uint8_t blockMajorVersion)
: CheckBlockAccepted(0, blockMajorVersion) {
m_expectedBlockchainHeight = m_currency.timestampCheckWindow();
}
2014-03-03 22:07:58 +00:00
bool generate(std::vector<test_event_entry>& events) const;
};
2014-08-13 10:51:37 +00:00
struct gen_block_ts_in_past : public CheckBlockPurged
2014-03-03 22:07:58 +00:00
{
2014-08-13 10:51:37 +00:00
gen_block_ts_in_past(uint8_t blockMajorVersion)
: CheckBlockPurged(0, blockMajorVersion) {
m_invalidBlockIdx = m_currency.timestampCheckWindow();
}
2014-03-03 22:07:58 +00:00
bool generate(std::vector<test_event_entry>& events) const;
};
2014-08-13 10:51:37 +00:00
struct gen_block_ts_in_future_rejected : public CheckBlockPurged
2014-03-03 22:07:58 +00:00
{
2014-08-13 10:51:37 +00:00
gen_block_ts_in_future_rejected(uint8_t blockMajorVersion)
: CheckBlockPurged(1, blockMajorVersion) {}
2014-03-03 22:07:58 +00:00
bool generate(std::vector<test_event_entry>& events) const;
};
2014-08-13 10:51:37 +00:00
struct gen_block_ts_in_future_accepted : public CheckBlockAccepted
2014-03-03 22:07:58 +00:00
{
2014-08-13 10:51:37 +00:00
gen_block_ts_in_future_accepted(uint8_t blockMajorVersion)
: CheckBlockAccepted(2, blockMajorVersion) {}
2014-03-03 22:07:58 +00:00
bool generate(std::vector<test_event_entry>& events) const;
};
2014-08-13 10:51:37 +00:00
struct gen_block_invalid_prev_id : public CheckBlockPurged
2014-03-03 22:07:58 +00:00
{
2014-08-13 10:51:37 +00:00
gen_block_invalid_prev_id(uint8_t blockMajorVersion)
: CheckBlockPurged(1, blockMajorVersion) {}
2014-03-03 22:07:58 +00:00
bool generate(std::vector<test_event_entry>& events) const;
2014-08-13 10:51:37 +00:00
bool check_block_verification_context(const cryptonote::block_verification_context& bvc, size_t event_idx, const cryptonote::Block& /*blk*/);
2014-03-03 22:07:58 +00:00
};
2014-08-13 10:51:37 +00:00
struct gen_block_invalid_nonce : public CheckBlockPurged
2014-03-03 22:07:58 +00:00
{
2014-08-13 10:51:37 +00:00
gen_block_invalid_nonce(uint8_t blockMajorVersion)
: CheckBlockPurged(3, blockMajorVersion) {}
2014-03-03 22:07:58 +00:00
bool generate(std::vector<test_event_entry>& events) const;
};
2014-08-13 10:51:37 +00:00
struct gen_block_no_miner_tx : public CheckBlockPurged
2014-03-03 22:07:58 +00:00
{
2014-08-13 10:51:37 +00:00
gen_block_no_miner_tx(uint8_t blockMajorVersion)
: CheckBlockPurged(1, blockMajorVersion) {}
2014-03-03 22:07:58 +00:00
bool generate(std::vector<test_event_entry>& events) const;
};
2014-08-13 10:51:37 +00:00
struct gen_block_unlock_time_is_low : public CheckBlockPurged
2014-03-03 22:07:58 +00:00
{
2014-08-13 10:51:37 +00:00
gen_block_unlock_time_is_low(uint8_t blockMajorVersion)
: CheckBlockPurged(1, blockMajorVersion) {}
2014-03-03 22:07:58 +00:00
bool generate(std::vector<test_event_entry>& events) const;
};
2014-08-13 10:51:37 +00:00
struct gen_block_unlock_time_is_high : public CheckBlockPurged
2014-03-03 22:07:58 +00:00
{
2014-08-13 10:51:37 +00:00
gen_block_unlock_time_is_high(uint8_t blockMajorVersion)
: CheckBlockPurged(1, blockMajorVersion) {}
2014-03-03 22:07:58 +00:00
bool generate(std::vector<test_event_entry>& events) const;
};
2014-08-13 10:51:37 +00:00
struct gen_block_unlock_time_is_timestamp_in_past : public CheckBlockPurged
2014-03-03 22:07:58 +00:00
{
2014-08-13 10:51:37 +00:00
gen_block_unlock_time_is_timestamp_in_past(uint8_t blockMajorVersion)
: CheckBlockPurged(1, blockMajorVersion) {}
2014-03-03 22:07:58 +00:00
bool generate(std::vector<test_event_entry>& events) const;
};
2014-08-13 10:51:37 +00:00
struct gen_block_unlock_time_is_timestamp_in_future : public CheckBlockPurged
2014-03-03 22:07:58 +00:00
{
2014-08-13 10:51:37 +00:00
gen_block_unlock_time_is_timestamp_in_future(uint8_t blockMajorVersion)
: CheckBlockPurged(1, blockMajorVersion) {}
2014-03-03 22:07:58 +00:00
bool generate(std::vector<test_event_entry>& events) const;
};
2014-08-13 10:51:37 +00:00
struct gen_block_height_is_low : public CheckBlockPurged
2014-03-03 22:07:58 +00:00
{
2014-08-13 10:51:37 +00:00
gen_block_height_is_low(uint8_t blockMajorVersion)
: CheckBlockPurged(1, blockMajorVersion) {}
2014-03-03 22:07:58 +00:00
bool generate(std::vector<test_event_entry>& events) const;
};
2014-08-13 10:51:37 +00:00
struct gen_block_height_is_high : public CheckBlockPurged
2014-03-03 22:07:58 +00:00
{
2014-08-13 10:51:37 +00:00
gen_block_height_is_high(uint8_t blockMajorVersion)
: CheckBlockPurged(1, blockMajorVersion) {}
2014-03-03 22:07:58 +00:00
bool generate(std::vector<test_event_entry>& events) const;
};
2014-08-13 10:51:37 +00:00
struct gen_block_miner_tx_has_2_tx_gen_in : public CheckBlockPurged
2014-03-03 22:07:58 +00:00
{
2014-08-13 10:51:37 +00:00
gen_block_miner_tx_has_2_tx_gen_in(uint8_t blockMajorVersion)
: CheckBlockPurged(1, blockMajorVersion) {}
2014-03-03 22:07:58 +00:00
bool generate(std::vector<test_event_entry>& events) const;
};
2014-08-13 10:51:37 +00:00
struct gen_block_miner_tx_has_2_in : public CheckBlockPurged
2014-03-03 22:07:58 +00:00
{
2014-08-13 10:51:37 +00:00
gen_block_miner_tx_has_2_in(uint8_t blockMajorVersion)
: CheckBlockPurged(0, blockMajorVersion) {
m_invalidBlockIdx = m_currency.minedMoneyUnlockWindow() + 1;
}
2014-03-03 22:07:58 +00:00
bool generate(std::vector<test_event_entry>& events) const;
};
2014-08-13 10:51:37 +00:00
struct gen_block_miner_tx_with_txin_to_key : public CheckBlockPurged
2014-03-03 22:07:58 +00:00
{
2014-08-13 10:51:37 +00:00
gen_block_miner_tx_with_txin_to_key(uint8_t blockMajorVersion)
: CheckBlockPurged(0, blockMajorVersion) {
m_invalidBlockIdx = m_currency.minedMoneyUnlockWindow() + 2;
}
2014-03-03 22:07:58 +00:00
bool generate(std::vector<test_event_entry>& events) const;
};
2014-08-13 10:51:37 +00:00
struct gen_block_miner_tx_out_is_small : public CheckBlockPurged
2014-03-03 22:07:58 +00:00
{
2014-08-13 10:51:37 +00:00
gen_block_miner_tx_out_is_small(uint8_t blockMajorVersion)
: CheckBlockPurged(1, blockMajorVersion) {}
2014-03-03 22:07:58 +00:00
bool generate(std::vector<test_event_entry>& events) const;
};
2014-08-13 10:51:37 +00:00
struct gen_block_miner_tx_out_is_big : public CheckBlockPurged
2014-03-03 22:07:58 +00:00
{
2014-08-13 10:51:37 +00:00
gen_block_miner_tx_out_is_big(uint8_t blockMajorVersion)
: CheckBlockPurged(1, blockMajorVersion) {}
2014-03-03 22:07:58 +00:00
bool generate(std::vector<test_event_entry>& events) const;
};
2014-08-13 10:51:37 +00:00
struct gen_block_miner_tx_has_no_out : public CheckBlockPurged
2014-03-03 22:07:58 +00:00
{
2014-08-13 10:51:37 +00:00
gen_block_miner_tx_has_no_out(uint8_t blockMajorVersion)
: CheckBlockPurged(1, blockMajorVersion) {}
2014-03-03 22:07:58 +00:00
bool generate(std::vector<test_event_entry>& events) const;
};
2014-08-13 10:51:37 +00:00
struct gen_block_miner_tx_has_out_to_alice : public CheckBlockAccepted
2014-03-03 22:07:58 +00:00
{
2014-08-13 10:51:37 +00:00
gen_block_miner_tx_has_out_to_alice(uint8_t blockMajorVersion)
: CheckBlockAccepted(2, blockMajorVersion) {}
2014-03-03 22:07:58 +00:00
bool generate(std::vector<test_event_entry>& events) const;
};
2014-08-13 10:51:37 +00:00
struct gen_block_has_invalid_tx : public CheckBlockPurged
2014-03-03 22:07:58 +00:00
{
2014-08-13 10:51:37 +00:00
gen_block_has_invalid_tx(uint8_t blockMajorVersion)
: CheckBlockPurged(1, blockMajorVersion) {}
2014-03-03 22:07:58 +00:00
bool generate(std::vector<test_event_entry>& events) const;
};
2014-08-13 10:51:37 +00:00
struct gen_block_is_too_big : public CheckBlockPurged
2014-03-03 22:07:58 +00:00
{
2014-08-13 10:51:37 +00:00
gen_block_is_too_big(uint8_t blockMajorVersion)
: CheckBlockPurged(1, blockMajorVersion) {}
bool generate(std::vector<test_event_entry>& events) const;
};
struct TestBlockCumulativeSizeExceedsLimit : public CheckBlockPurged {
TestBlockCumulativeSizeExceedsLimit(uint8_t blockMajorVersion)
: CheckBlockPurged(std::numeric_limits<size_t>::max(), blockMajorVersion) {
}
2014-03-03 22:07:58 +00:00
bool generate(std::vector<test_event_entry>& events) const;
};
struct gen_block_invalid_binary_format : public test_chain_unit_base
{
2014-08-13 10:51:37 +00:00
gen_block_invalid_binary_format(uint8_t blockMajorVersion);
2014-03-03 22:07:58 +00:00
bool generate(std::vector<test_event_entry>& events) const;
2014-08-13 10:51:37 +00:00
bool check_block_verification_context(const cryptonote::block_verification_context& bvc, size_t event_idx, const cryptonote::Block& /*blk*/);
2014-03-03 22:07:58 +00:00
bool check_all_blocks_purged(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events);
bool corrupt_blocks_boundary(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events);
private:
2014-08-13 10:51:37 +00:00
const uint8_t m_blockMajorVersion;
2014-03-03 22:07:58 +00:00
size_t m_corrupt_blocks_begin_idx;
};
2014-08-13 10:51:37 +00:00
struct TestMaxSizeOfParentBlock : public CheckBlockAccepted {
TestMaxSizeOfParentBlock() : CheckBlockAccepted(2, cryptonote::BLOCK_MAJOR_VERSION_2) {
}
bool generate(std::vector<test_event_entry>& events) const;
};
struct TestBigParentBlock : public CheckBlockPurged {
TestBigParentBlock() : CheckBlockPurged(1, cryptonote::BLOCK_MAJOR_VERSION_2) {
}
bool generate(std::vector<test_event_entry>& events) const;
};
struct TestBlock2ExtraEmpty : public CheckBlockPurged {
TestBlock2ExtraEmpty() : CheckBlockPurged(1, cryptonote::BLOCK_MAJOR_VERSION_2) {}
bool generate(std::vector<test_event_entry>& events) const;
};
struct TestBlock2ExtraWithoutMMTag : public CheckBlockPurged {
TestBlock2ExtraWithoutMMTag() : CheckBlockPurged(1, cryptonote::BLOCK_MAJOR_VERSION_2) {}
bool generate(std::vector<test_event_entry>& events) const;
};
struct TestBlock2ExtraWithGarbage : public CheckBlockAccepted {
TestBlock2ExtraWithGarbage() : CheckBlockAccepted(2, cryptonote::BLOCK_MAJOR_VERSION_2) {}
bool generate(std::vector<test_event_entry>& events) const;
};