class TestVerifyHeader(ElectrumTestCase): # Data for Bitcoin block header #100. valid_header = "0100000095194b8567fe2e8bbda931afd01a7acd399b9325cb54683e64129bcd00000000660802c98f18fd34fd16d61c63cf447568370124ac5f3be626c2e1c3c9f0052d19a76949ffff001d33f3c25d" target = Blockchain.bits_to_target(0x1d00ffff) prev_hash = "00000000cd9b12643e6854cb25939b39cd7a1ad0af31a9bd8b2efe67854b1995" def setUp(self): super().setUp() self.header = deserialize_header(bfh(self.valid_header), 100) def test_valid_header(self): Blockchain.verify_header(self.header, self.prev_hash, self.target) def test_expected_hash_mismatch(self): with self.assertRaises(Exception): Blockchain.verify_header(self.header, self.prev_hash, self.target, expected_header_hash="foo") def test_prev_hash_mismatch(self): with self.assertRaises(Exception): Blockchain.verify_header(self.header, "foo", self.target) def test_target_mismatch(self): with self.assertRaises(Exception): other_target = Blockchain.bits_to_target(0x1d00eeee) Blockchain.verify_header(self.header, self.prev_hash, other_target) def test_insufficient_pow(self): with self.assertRaises(Exception): self.header["nonce"] = 42 Blockchain.verify_header(self.header, self.prev_hash, self.target)
class TestVerifyHeader(ElectrumTestCase): # Data for Koto block header #1056620. valid_header = "0500000046b175f0bdab4d7c2c4cf501c9bb192d037d7b7dcefcf34a8684c19dca62b82c3c550621d820a798277b8ec063811d8b4d26f74868d9da61f4ff7c0da548f7f50678fc5d729a0e1d000030e72a9614689e6408a6c11985046866a02d4ee68e4def996b27d84c98675697df2f" target = Blockchain.bits_to_target(0x1d0e9a72) prev_hash = "2cb862ca9dc184864af3fcce7d7b7d032d19bbc901f54c2c7c4dabbdf075b146" def setUp(self): super().setUp() self.header = deserialize_header(bfh(self.valid_header), 1056620) def test_valid_header(self): Blockchain.verify_header(self.header, self.prev_hash, self.target) def test_expected_hash_mismatch(self): with self.assertRaises(Exception): Blockchain.verify_header(self.header, self.prev_hash, self.target, expected_header_hash="foo") def test_prev_hash_mismatch(self): with self.assertRaises(Exception): Blockchain.verify_header(self.header, "foo", self.target) def test_target_mismatch(self): with self.assertRaises(Exception): other_target = Blockchain.bits_to_target(0x1d0eeeee) Blockchain.verify_header(self.header, self.prev_hash, other_target) def test_insufficient_pow(self): with self.assertRaises(Exception): self.header["nonce"] = 100 Blockchain.verify_header(self.header, self.prev_hash, self.target)
class TestVerifyHeader(ElectrumTestCase): # Data for Xaya block header #100. valid_header = "00000020fc0261d5ae27dfbfed6e429b73fb7dad01b7d9274e63bbf2736a44912948b6fdbd982b64718798c3ddb1039e386ba4bf42663465c76a2d80440468bc72d1fb3903a9485b000000000000000002c1b64e1d000000000000000000000000000000000000000000000000000000000000000000000000f4613e4c8bafeff8f90324301345ae8b6c912583235b4b344bb226fbb7226c200000000000000000014e95f2" target = Blockchain.bits_to_target(0x1d4eb6c1) prev_hash = "fdb6482991446a73f2bb634e27d9b701ad7dfb739b426eedbfdf27aed56102fc" def setUp(self): super().setUp() # Height must be above the checkpoint, because the AuxPoW branch # doesn't verify PoW below the checkpoint. self.header = deserialize_full_header( bfh(self.valid_header), constants.net.max_checkpoint() + 100) def test_valid_header(self): Blockchain.verify_header(self.header, self.prev_hash, self.target) def test_expected_hash_mismatch(self): with self.assertRaises(Exception): Blockchain.verify_header(self.header, self.prev_hash, self.target, expected_header_hash="foo") def test_prev_hash_mismatch(self): with self.assertRaises(Exception): Blockchain.verify_header(self.header, "foo", self.target) def test_target_mismatch(self): with self.assertRaises(Exception): other_target = Blockchain.bits_to_target(0x1d00eeee) Blockchain.verify_header(self.header, self.prev_hash, other_target) def test_insufficient_pow(self): with self.assertRaises(Exception): self.header["nonce"] = 42 Blockchain.verify_header(self.header, self.prev_hash, self.target)
def test_target_to_bits(self): # https://github.com/bitcoin/bitcoin/blob/7fcf53f7b4524572d1d0c9a5fdc388e87eb02416/src/arith_uint256.h#L269 self.assertEqual(0x05123456, Blockchain.target_to_bits(0x1234560000)) self.assertEqual(0x0600c0de, Blockchain.target_to_bits(0xc0de000000)) # tests from https://github.com/bitcoin/bitcoin/blob/a7d17daa5cd8bf6398d5f8d7e77290009407d6ea/src/test/arith_uint256_tests.cpp#L411 tuples = ( (0, 0x0000000000000000000000000000000000000000000000000000000000000000, 0), (0x00123456, 0x0000000000000000000000000000000000000000000000000000000000000000, 0), (0x01003456, 0x0000000000000000000000000000000000000000000000000000000000000000, 0), (0x02000056, 0x0000000000000000000000000000000000000000000000000000000000000000, 0), (0x03000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0), (0x04000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0), (0x00923456, 0x0000000000000000000000000000000000000000000000000000000000000000, 0), (0x01803456, 0x0000000000000000000000000000000000000000000000000000000000000000, 0), (0x02800056, 0x0000000000000000000000000000000000000000000000000000000000000000, 0), (0x03800000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0), (0x04800000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0), (0x01123456, 0x0000000000000000000000000000000000000000000000000000000000000012, 0x01120000), (0x02123456, 0x0000000000000000000000000000000000000000000000000000000000001234, 0x02123400), (0x03123456, 0x0000000000000000000000000000000000000000000000000000000000123456, 0x03123456), (0x04123456, 0x0000000000000000000000000000000000000000000000000000000012345600, 0x04123456), (0x05009234, 0x0000000000000000000000000000000000000000000000000000000092340000, 0x05009234), (0x20123456, 0x1234560000000000000000000000000000000000000000000000000000000000, 0x20123456), ) for nbits1, target, nbits2 in tuples: with self.subTest(original_compact_nbits=nbits1.to_bytes(length=4, byteorder="big").hex()): num = Blockchain.bits_to_target(nbits1) self.assertEqual(target, num) self.assertEqual(nbits2, Blockchain.target_to_bits(num)) # Make sure that we don't generate compacts with the 0x00800000 bit set self.assertEqual(0x02008000, Blockchain.target_to_bits(0x80)) with self.assertRaises(Exception): # target cannot be negative Blockchain.bits_to_target(0x01fedcba) with self.assertRaises(Exception): # target cannot be negative Blockchain.bits_to_target(0x04923456) with self.assertRaises(Exception): # overflow Blockchain.bits_to_target(0xff123456)
def test_target_mismatch(self): with self.assertRaises(Exception): other_target = Blockchain.bits_to_target(0x1d00eeee) Blockchain.verify_header(self.header, self.prev_hash, other_target)