1
0
Fork 0
forked from lthn/blockchain

htlc: added tests for double spend(redeem and then refund after)

This commit is contained in:
cryptozoidberg 2021-02-21 17:56:53 +01:00
parent ef2f523bf1
commit 8fe41c8df2
No known key found for this signature in database
GPG key ID: 22DEB97A54C6FDEC

View file

@ -88,6 +88,7 @@ bool atomic_simple_test::c1(currency::core& c, size_t ev_index, const std::vecto
INIT_RUNTIME_WALLET(refund_test_instance);
INIT_RUNTIME_WALLET(refund_test_instance2);
#define AMOUNT_TO_TRANSFER_HTLC (TESTS_DEFAULT_FEE*10)
std::shared_ptr<tools::wallet2> miner_wlt = init_playtime_test_wallet(events, c, m_mining_accunt);
@ -108,6 +109,7 @@ bool atomic_simple_test::c1(currency::core& c, size_t ev_index, const std::vecto
miner_wlt->transfer(transfer_amount, refund_test_instance->get_account().get_public_address());
LOG_PRINT_MAGENTA("Transaction sent to Refund test: " << transfer_amount, LOG_LEVEL_0);
bool r = mine_next_pow_blocks_in_playtime(m_mining_accunt.get_public_address(), c, CURRENCY_MINED_MONEY_UNLOCK_WINDOW);
CHECK_AND_ASSERT_MES(r, false, "mine_next_pow_blocks_in_playtime failed");
@ -137,7 +139,6 @@ bool atomic_simple_test::c1(currency::core& c, size_t ev_index, const std::vecto
currency::transaction refund_res_tx = AUTO_VAL_INIT(refund_res_tx);
refund_test_instance->create_htlc_proposal(transfer_amount - TESTS_DEFAULT_FEE, miner_wlt->get_account().get_public_address(), 8, refund_res_tx, currency::null_hash, refund_origin);
//----------- rewinding -----------
r = mine_next_pow_blocks_in_playtime(m_mining_accunt.get_public_address(), c, CURRENCY_MINED_MONEY_UNLOCK_WINDOW);
CHECK_AND_ASSERT_MES(r, false, "mine_next_pow_blocks_in_playtime failed");
@ -326,6 +327,9 @@ bool atomic_test_wrong_redeem_wrong_refund::c1(currency::core& c, size_t ev_inde
INIT_RUNTIME_WALLET(alice_a_wlt_instance);
INIT_RUNTIME_WALLET(alice_b_wlt_instance);
INIT_RUNTIME_WALLET(alice_c_wlt_instance);
INIT_RUNTIME_WALLET(double_spend_test_instance_1);
INIT_RUNTIME_WALLET(double_spend_test_instance_2);
#define AMOUNT_TO_TRANSFER_HTLC (TESTS_DEFAULT_FEE*10)
@ -345,6 +349,11 @@ bool atomic_test_wrong_redeem_wrong_refund::c1(currency::core& c, size_t ev_inde
miner_wlt->transfer(transfer_amount, alice_b_wlt_instance->get_account().get_public_address());
LOG_PRINT_MAGENTA("Transaction sent to Alice A: " << transfer_amount, LOG_LEVEL_0);
miner_wlt->transfer(transfer_amount, double_spend_test_instance_1->get_account().get_public_address());
LOG_PRINT_MAGENTA("Transaction sent to Double spend test: " << transfer_amount, LOG_LEVEL_0);
bool r = mine_next_pow_blocks_in_playtime(m_mining_accunt.get_public_address(), c, CURRENCY_MINED_MONEY_UNLOCK_WINDOW);
CHECK_AND_ASSERT_MES(r, false, "mine_next_pow_blocks_in_playtime failed");
@ -352,6 +361,9 @@ bool atomic_test_wrong_redeem_wrong_refund::c1(currency::core& c, size_t ev_inde
alice_a_wlt_instance->refresh();
alice_b_wlt_instance->refresh();
alice_c_wlt_instance->refresh();
double_spend_test_instance_1->refresh();
double_spend_test_instance_2->refresh();
std::string alice_origin; //will be deterministically generated by Alice's A wallet
currency::transaction res_tx = AUTO_VAL_INIT(res_tx);
@ -361,6 +373,11 @@ bool atomic_test_wrong_redeem_wrong_refund::c1(currency::core& c, size_t ev_inde
currency::transaction res_tx_b = AUTO_VAL_INIT(res_tx_b);
alice_b_wlt_instance->create_htlc_proposal(transfer_amount - TESTS_DEFAULT_FEE, alice_c_wlt_instance->get_account().get_public_address(), 12, res_tx, currency::null_hash, alice_origin_b);
//c) htlc double spend test
std::string double_spend_origin; //will be deterministically generated by Alice's A wallet
currency::transaction double_spend_res_tx = AUTO_VAL_INIT(double_spend_res_tx);
double_spend_test_instance_1->create_htlc_proposal(transfer_amount - TESTS_DEFAULT_FEE, double_spend_test_instance_2->get_account().get_public_address(), 9, double_spend_res_tx, currency::null_hash, double_spend_origin);
//forward blockchain to create redeem transaction
r = mine_next_pow_blocks_in_playtime(m_mining_accunt.get_public_address(), c, CURRENCY_MINED_MONEY_UNLOCK_WINDOW);
@ -369,6 +386,7 @@ bool atomic_test_wrong_redeem_wrong_refund::c1(currency::core& c, size_t ev_inde
alice_a_wlt_instance->refresh();
alice_b_wlt_instance->refresh();
alice_c_wlt_instance->refresh();
double_spend_test_instance_1->refresh();
CHECK_AND_FORCE_ASSERT_MES(alice_a_wlt_instance->balance() == transfer_amount - TESTS_DEFAULT_FEE, false, "Incorrect balance");
@ -376,32 +394,63 @@ bool atomic_test_wrong_redeem_wrong_refund::c1(currency::core& c, size_t ev_inde
currency::transaction refund_tx = AUTO_VAL_INIT(refund_tx);
alice_a_wlt_instance->transfer(transfer_amount - TESTS_DEFAULT_FEE*2, alice_b_wlt_instance->get_account().get_public_address(), refund_tx);
//create double spend refund tx
currency::transaction ds_refund_tx = AUTO_VAL_INIT(refund_tx);
CHECK_AND_FORCE_ASSERT_MES(double_spend_test_instance_1->balance() == transfer_amount - TESTS_DEFAULT_FEE, false, "Incorrect balance");
double_spend_test_instance_1->transfer(transfer_amount - TESTS_DEFAULT_FEE * 2, double_spend_test_instance_2->get_account().get_public_address(), ds_refund_tx);
//create redeem tx
std::list<tools::wallet_public::htlc_entry_info> htlcs;
alice_c_wlt_instance->get_list_of_active_htlc(htlcs, true);
CHECK_AND_FORCE_ASSERT_MES(htlcs.size() == 1, false, "Epected htlc not found");
currency::transaction result_redeem_tx = AUTO_VAL_INIT(result_redeem_tx);
alice_c_wlt_instance->redeem_htlc(htlcs.front().tx_id, alice_origin_b, result_redeem_tx);
//truncte blockchain
c.get_tx_pool().clear();
c.get_blockchain_storage().truncate_blockchain(c.get_blockchain_storage().get_current_blockchain_size() - 8);
double_spend_test_instance_2->refresh();
//create redeem tx for ds
std::list<tools::wallet_public::htlc_entry_info> htlcs_ds;
double_spend_test_instance_2->get_list_of_active_htlc(htlcs_ds, true);
CHECK_AND_FORCE_ASSERT_MES(htlcs_ds.size() == 1, false, "Epected htlc not found");
currency::transaction result_redeem_tx_ds = AUTO_VAL_INIT(result_redeem_tx_ds);
double_spend_test_instance_2->redeem_htlc(htlcs_ds.front().tx_id, double_spend_origin, result_redeem_tx_ds);
//try to submit wrong transaction that do refund before expiration
std::vector<currency::transaction> txs;
txs.push_back(refund_tx);
r = mine_next_pow_block_in_playtime_with_given_txs(m_mining_accunt.get_public_address(), c, txs);
CHECK_AND_FORCE_ASSERT_MES(!r, false, "Blo k with wrong refund tx accepted");
CHECK_AND_FORCE_ASSERT_MES(!r, false, "Block with wrong refund tx accepted");
txs.clear();
txs.push_back(result_redeem_tx_ds);
r = mine_next_pow_block_in_playtime_with_given_txs(m_mining_accunt.get_public_address(), c, txs);
CHECK_AND_FORCE_ASSERT_MES(r, false, "Block with wrong refund tx accepted");
//forward blockchain and try to submit wrong tx that do redeem after expiration
r = mine_next_pow_blocks_in_playtime(m_mining_accunt.get_public_address(), c, CURRENCY_MINED_MONEY_UNLOCK_WINDOW);
CHECK_AND_ASSERT_MES(r, false, "mine_next_pow_blocks_in_playtime failed");
std::vector<currency::transaction> txs2;
txs2.push_back(result_redeem_tx);
r = mine_next_pow_block_in_playtime_with_given_txs(m_mining_accunt.get_public_address(), c, txs2);
//try to put wrong redeem tx (after expiration)
txs.clear();
txs.push_back(result_redeem_tx);
r = mine_next_pow_block_in_playtime_with_given_txs(m_mining_accunt.get_public_address(), c, txs);
CHECK_AND_FORCE_ASSERT_MES(!r, false, "Blo k with wrong refund tx accepted");
//try to put wrong refund tx (double spend)
txs.clear();
txs.push_back(ds_refund_tx);
r = mine_next_pow_block_in_playtime_with_given_txs(m_mining_accunt.get_public_address(), c, txs);
CHECK_AND_FORCE_ASSERT_MES(!r, false, "Blo k with wrong refund tx accepted");
return true;
}
//try to do double spend!!!! (redeem and refund), check if wallet see redeemed tx and don't let it be spent