|
22 | 22 | #include <util/translation.h> |
23 | 23 | #include <validation.h> |
24 | 24 | #include <versionbits.h> |
| 25 | +#include <pow.h> |
25 | 26 |
|
26 | 27 | #include <test/util/setup_common.h> |
27 | 28 |
|
@@ -666,7 +667,44 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) |
666 | 667 | CScript scriptPubKey = CScript() << "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"_hex << OP_CHECKSIG; |
667 | 668 | BlockAssembler::Options options; |
668 | 669 | options.coinbase_output_script = scriptPubKey; |
669 | | - std::unique_ptr<BlockTemplate> block_template; |
| 670 | + |
| 671 | + // Create and check a simple template |
| 672 | + std::unique_ptr<BlockTemplate> block_template = mining->createNewBlock(options); |
| 673 | + BOOST_REQUIRE(block_template); |
| 674 | + { |
| 675 | + CBlock block{block_template->getBlock()}; |
| 676 | + { |
| 677 | + std::string reason; |
| 678 | + std::string debug; |
| 679 | + BOOST_REQUIRE(!mining->checkBlock(block, {.check_pow = false}, reason, debug)); |
| 680 | + BOOST_REQUIRE_EQUAL(reason, "bad-txnmrklroot"); |
| 681 | + BOOST_REQUIRE_EQUAL(debug, "hashMerkleRoot mismatch"); |
| 682 | + } |
| 683 | + |
| 684 | + block.hashMerkleRoot = BlockMerkleRoot(block); |
| 685 | + |
| 686 | + { |
| 687 | + std::string reason; |
| 688 | + std::string debug; |
| 689 | + BOOST_REQUIRE(mining->checkBlock(block, {.check_pow = false}, reason, debug)); |
| 690 | + BOOST_REQUIRE_EQUAL(reason, ""); |
| 691 | + BOOST_REQUIRE_EQUAL(debug, ""); |
| 692 | + } |
| 693 | + |
| 694 | + { |
| 695 | + // A block template does not have proof-of-work, but it might pass |
| 696 | + // verifcation by coincidence. Grind the nonce if needed: |
| 697 | + while (CheckProofOfWork(block.GetHash(), block.nBits, Assert(m_node.chainman)->GetParams().GetConsensus())) { |
| 698 | + block.nNonce++; |
| 699 | + } |
| 700 | + |
| 701 | + std::string reason; |
| 702 | + std::string debug; |
| 703 | + BOOST_REQUIRE(!mining->checkBlock(block, {.check_pow = true}, reason, debug)); |
| 704 | + BOOST_REQUIRE_EQUAL(reason, "high-hash"); |
| 705 | + BOOST_REQUIRE_EQUAL(debug, "proof of work failed"); |
| 706 | + } |
| 707 | + } |
670 | 708 |
|
671 | 709 | // We can't make transactions until we have inputs |
672 | 710 | // Therefore, load 110 blocks :) |
|
0 commit comments