| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
#include "chaingen.h" |
| 33 |
|
| 34 |
const size_t invalid_index_value = std::numeric_limits<size_t>::max(); |
| 35 |
const uint64_t FIRST_BLOCK_REWARD = 17592186044415; |
| 36 |
|
| 37 |
|
| 38 |
template<class concrete_test> |
| 39 |
class gen_double_spend_base : public test_chain_unit_base |
| 40 |
{ |
| 41 |
public: |
| 42 |
static const uint64_t send_amount = FIRST_BLOCK_REWARD - TESTS_DEFAULT_FEE; |
| 43 |
|
| 44 |
gen_double_spend_base(); |
| 45 |
|
| 46 |
bool check_tx_verification_context(const cryptonote::tx_verification_context& tvc, bool tx_added, size_t event_idx, const cryptonote::transaction& tx); |
| 47 |
bool check_block_verification_context(const cryptonote::block_verification_context& bvc, size_t event_idx, const cryptonote::block& block); |
| 48 |
|
| 49 |
bool mark_last_valid_block(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events); |
| 50 |
bool mark_invalid_tx(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events); |
| 51 |
bool mark_invalid_block(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events); |
| 52 |
bool check_double_spend(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events); |
| 53 |
|
| 54 |
private: |
| 55 |
cryptonote::block m_last_valid_block; |
| 56 |
size_t m_invalid_tx_index; |
| 57 |
size_t m_invalid_block_index; |
| 58 |
}; |
| 59 |
|
| 60 |
|
| 61 |
template<bool txs_keeped_by_block> |
| 62 |
struct gen_double_spend_in_tx : public gen_double_spend_base< gen_double_spend_in_tx<txs_keeped_by_block> > |
| 63 |
{ |
| 64 |
static const uint64_t send_amount = FIRST_BLOCK_REWARD - TESTS_DEFAULT_FEE; |
| 65 |
static const bool has_invalid_tx = true; |
| 66 |
static const size_t expected_pool_txs_count = 0; |
| 67 |
static const uint64_t expected_bob_balance = send_amount; |
| 68 |
static const uint64_t expected_alice_balance = 0; |
| 69 |
|
| 70 |
bool generate(std::vector<test_event_entry>& events) const; |
| 71 |
}; |
| 72 |
|
| 73 |
|
| 74 |
template<bool txs_keeped_by_block> |
| 75 |
struct gen_double_spend_in_the_same_block : public gen_double_spend_base< gen_double_spend_in_the_same_block<txs_keeped_by_block> > |
| 76 |
{ |
| 77 |
static const uint64_t send_amount = FIRST_BLOCK_REWARD - TESTS_DEFAULT_FEE; |
| 78 |
static const bool has_invalid_tx = !txs_keeped_by_block; |
| 79 |
static const size_t expected_pool_txs_count = has_invalid_tx ? 1 : 2; |
| 80 |
static const uint64_t expected_bob_balance = send_amount; |
| 81 |
static const uint64_t expected_alice_balance = 0; |
| 82 |
|
| 83 |
bool generate(std::vector<test_event_entry>& events) const; |
| 84 |
}; |
| 85 |
|
| 86 |
|
| 87 |
template<bool txs_keeped_by_block> |
| 88 |
struct gen_double_spend_in_different_blocks : public gen_double_spend_base< gen_double_spend_in_different_blocks<txs_keeped_by_block> > |
| 89 |
{ |
| 90 |
static const uint64_t send_amount = FIRST_BLOCK_REWARD - TESTS_DEFAULT_FEE; |
| 91 |
static const bool has_invalid_tx = !txs_keeped_by_block; |
| 92 |
static const size_t expected_pool_txs_count = has_invalid_tx ? 0 : 1; |
| 93 |
static const uint64_t expected_bob_balance = 0; |
| 94 |
static const uint64_t expected_alice_balance = send_amount - TESTS_DEFAULT_FEE; |
| 95 |
|
| 96 |
bool generate(std::vector<test_event_entry>& events) const; |
| 97 |
}; |
| 98 |
|
| 99 |
|
| 100 |
template<bool txs_keeped_by_block> |
| 101 |
struct gen_double_spend_in_alt_chain_in_the_same_block : public gen_double_spend_base< gen_double_spend_in_alt_chain_in_the_same_block<txs_keeped_by_block> > |
| 102 |
{ |
| 103 |
static const uint64_t send_amount = FIRST_BLOCK_REWARD - TESTS_DEFAULT_FEE; |
| 104 |
static const bool has_invalid_tx = !txs_keeped_by_block; |
| 105 |
static const size_t expected_pool_txs_count = has_invalid_tx ? 1 : 2; |
| 106 |
static const uint64_t expected_bob_balance = send_amount; |
| 107 |
static const uint64_t expected_alice_balance = 0; |
| 108 |
|
| 109 |
bool generate(std::vector<test_event_entry>& events) const; |
| 110 |
}; |
| 111 |
|
| 112 |
|
| 113 |
template<bool txs_keeped_by_block> |
| 114 |
struct gen_double_spend_in_alt_chain_in_different_blocks : public gen_double_spend_base< gen_double_spend_in_alt_chain_in_different_blocks<txs_keeped_by_block> > |
| 115 |
{ |
| 116 |
static const uint64_t send_amount = FIRST_BLOCK_REWARD - TESTS_DEFAULT_FEE; |
| 117 |
static const bool has_invalid_tx = !txs_keeped_by_block; |
| 118 |
static const size_t expected_pool_txs_count = has_invalid_tx ? 1 : 2; |
| 119 |
static const uint64_t expected_bob_balance = send_amount; |
| 120 |
static const uint64_t expected_alice_balance = 0; |
| 121 |
|
| 122 |
bool generate(std::vector<test_event_entry>& events) const; |
| 123 |
}; |
| 124 |
|
| 125 |
|
| 126 |
class gen_double_spend_in_different_chains : public test_chain_unit_base |
| 127 |
{ |
| 128 |
public: |
| 129 |
static const uint64_t send_amount = FIRST_BLOCK_REWARD - TESTS_DEFAULT_FEE; |
| 130 |
static const size_t expected_blockchain_height = 4 + 2 * CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW; |
| 131 |
|
| 132 |
gen_double_spend_in_different_chains(); |
| 133 |
|
| 134 |
bool generate(std::vector<test_event_entry>& events) const; |
| 135 |
|
| 136 |
bool check_double_spend(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events); |
| 137 |
}; |
| 138 |
|
| 139 |
|
| 140 |
#define INIT_DOUBLE_SPEND_TEST() \ |
| 141 |
uint64_t ts_start = 1338224400; \ |
| 142 |
GENERATE_ACCOUNT(miner_account); \ |
| 143 |
MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start); \ |
| 144 |
MAKE_ACCOUNT(events, bob_account); \ |
| 145 |
MAKE_ACCOUNT(events, alice_account); \ |
| 146 |
REWIND_BLOCKS(events, blk_0r, blk_0, miner_account); \ |
| 147 |
MAKE_TX(events, tx_0, miner_account, bob_account, send_amount, blk_0); \ |
| 148 |
MAKE_NEXT_BLOCK_TX1(events, blk_1, blk_0r, miner_account, tx_0); \ |
| 149 |
REWIND_BLOCKS(events, blk_1r, blk_1, miner_account); |
| 150 |
|
| 151 |
|
| 152 |
#include "double_spend.inl" |