|
| 1 | +// Copyright (c) 2017-2017 Blockstream Inc. |
| 2 | +// Distributed under the MIT/X11 software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#include "arith_uint256.h" |
| 6 | +#include "blind.h" |
| 7 | +#include "uint256.h" |
| 8 | +#include "main.h" |
| 9 | + |
| 10 | +#include "test/test_bitcoin.h" |
| 11 | + |
| 12 | +#include <boost/test/unit_test.hpp> |
| 13 | + |
| 14 | + |
| 15 | +BOOST_FIXTURE_TEST_SUITE(amount_tests, TestingSetup) |
| 16 | + |
| 17 | +BOOST_AUTO_TEST_CASE(verify_coinbase_test) |
| 18 | +{ |
| 19 | + // Generate some asset types and feemap |
| 20 | + std::vector<CAsset> types; |
| 21 | + CAmountMap mapFees; |
| 22 | + for (unsigned int i = 1; i < 4; i++) { |
| 23 | + types.emplace_back(GetRandHash()); |
| 24 | + mapFees[types.back()] = i; |
| 25 | + } |
| 26 | + |
| 27 | + // Make coinbase transaction |
| 28 | + CMutableTransaction tx; |
| 29 | + tx.vin.push_back(CTxIn(COutPoint(), CScript(), 0)); |
| 30 | + |
| 31 | + // Blank amount check |
| 32 | + BOOST_CHECK(VerifyCoinbaseAmount(tx, CAmountMap())); |
| 33 | + |
| 34 | + // Check against non-null fee, surplus is ok |
| 35 | + BOOST_CHECK(VerifyCoinbaseAmount(tx, mapFees)); |
| 36 | + |
| 37 | + // Add non-fee outputs to coinbase |
| 38 | + for (unsigned int i = 1; i < 4; i++) { |
| 39 | + BOOST_CHECK(VerifyCoinbaseAmount(tx, mapFees)); |
| 40 | + tx.vout.push_back(CTxOut(types[i-1], i, CScript() << OP_TRUE)); |
| 41 | + } |
| 42 | + // All outputs added, should still balance |
| 43 | + BOOST_CHECK(VerifyCoinbaseAmount(tx, mapFees)); |
| 44 | + |
| 45 | + // Adding 0-value unspendable output should not change balance |
| 46 | + tx.vout.push_back(CTxOut(types.back(), 0, CScript() << OP_RETURN)); |
| 47 | + BOOST_CHECK(VerifyCoinbaseAmount(tx, mapFees)); |
| 48 | + |
| 49 | + // But you cannot add spendable 0-value output |
| 50 | + tx.vout.push_back(CTxOut(types.back(), 0, CScript() << OP_TRUE)); |
| 51 | + BOOST_CHECK(!VerifyCoinbaseAmount(tx, mapFees)); |
| 52 | + tx.vout.pop_back(); |
| 53 | + BOOST_CHECK(VerifyCoinbaseAmount(tx, mapFees)); |
| 54 | + |
| 55 | + // Adding values outside MAX_MONEY also causes failure |
| 56 | + mapFees[CAsset()] = MAX_MONEY+1; |
| 57 | + tx.vout.push_back(CTxOut(CAsset(), MAX_MONEY+1, CScript() << OP_RETURN)); |
| 58 | + BOOST_CHECK(!VerifyCoinbaseAmount(tx, mapFees)); |
| 59 | + tx.vout.pop_back(); |
| 60 | + mapFees[CAsset()] = 0; |
| 61 | + BOOST_CHECK(VerifyCoinbaseAmount(tx, mapFees)); |
| 62 | + |
| 63 | + // Change the unspendable 0-value output to have asset commitment |
| 64 | + secp256k1_generator gen; |
| 65 | + const unsigned char blind[32] = {0}; |
| 66 | + CAsset asset = tx.vout.back().nAsset.GetAsset(); |
| 67 | + |
| 68 | + BlindAsset(tx.vout.back().nAsset, gen, asset, blind); |
| 69 | + |
| 70 | + BOOST_CHECK(!VerifyCoinbaseAmount(tx, mapFees)); |
| 71 | + tx.vout.back().nAsset = types.back(); |
| 72 | + BOOST_CHECK(VerifyCoinbaseAmount(tx, mapFees)); |
| 73 | + |
| 74 | + // Change the same output's value to an unblinded commitment to non-zero value (unblinded zero is blank commitment) |
| 75 | + secp256k1_pedersen_commitment commit; |
| 76 | + CreateValueCommitment(tx.vout.back().nValue, commit, blind, gen, 1); |
| 77 | + BOOST_CHECK(!VerifyCoinbaseAmount(tx, mapFees)); |
| 78 | + |
| 79 | + tx.vout.pop_back(); |
| 80 | + BOOST_CHECK(VerifyCoinbaseAmount(tx, mapFees)); |
| 81 | + |
| 82 | + // Negative output |
| 83 | + tx.vout.push_back(CTxOut(CAsset(), -1, CScript() << OP_RETURN)); |
| 84 | + BOOST_CHECK(!VerifyCoinbaseAmount(tx, mapFees)); |
| 85 | + tx.vout.pop_back(); |
| 86 | + |
| 87 | + // Transaction claiming too much fees |
| 88 | + tx.vout.push_back(CTxOut(CAsset(), 1, CScript() << OP_RETURN)); |
| 89 | + BOOST_CHECK(!VerifyCoinbaseAmount(tx, mapFees)); |
| 90 | + tx.vout.pop_back(); |
| 91 | +} |
| 92 | + |
| 93 | +BOOST_AUTO_TEST_SUITE_END() |
0 commit comments