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)
        return

    def test_expected_hash_mismatch(self):
        #with self.assertRaises(Exception):
        #    Blockchain.verify_header(self.header, self.prev_hash, self.target,
        #                             expected_header_hash="foo")
        return

    def test_prev_hash_mismatch(self):
        #with self.assertRaises(Exception):
        #    Blockchain.verify_header(self.header, "foo", self.target)
        return

    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)
        return

    def test_insufficient_pow(self):
        #with self.assertRaises(Exception):
        #    self.header["nonce"] = 42
        #    Blockchain.verify_header(self.header, self.prev_hash, self.target)
        return

    def test_get_target(self):

        # before DGWv3 with checkpoint(height=2015)
        headers1 = {
            2015: {
                'version': 2,
                'prev_block_hash':
                'f9cba205f996e98f61f87e32ae57fc0a5befa6cd632dd257f3e239f390010622',
                'merkle_root':
                'af68c1f62b965172df1d81fba95f193cb8e42431bad79a4bfbcc370d301d5710',
                'timestamp': 1388536705,
                'bits': 503936911,
                'nonce': 780010496,
                'block_height': 2015
            }
        }
        bits = Blockchain.get_target(self, 2015, headers1)
        self.assertEqual(
            bits,
            65339010432214603900175979833807329994044402934458085644623414103638016
        )

        # before DGWv3 without checkpoint(height=2016)
        headers2 = {
            2015: {
                'version': 2,
                'prev_block_hash':
                'f9cba205f996e98f61f87e32ae57fc0a5befa6cd632dd257f3e239f390010622',
                'merkle_root':
                'af68c1f62b965172df1d81fba95f193cb8e42431bad79a4bfbcc370d301d5710',
                'timestamp': 1388536705,
                'bits': 503936911,
                'nonce': 780010496,
                'block_height': 2015
            }
        }
        bits = Blockchain.get_target(self, 2016, headers2)
        self.assertEqual(bits, 0)

        # after DGWv3 with checkpoint(height=461663)
        headers3 = {
            461663: {
                'version': 3,
                'prev_block_hash':
                '9c87f1e27717aec18617496970b9744dd855f997128fab6733e709fd95d97870',
                'merkle_root':
                '7f22e9001ab92b14a1b057ce07c4f2acecb693f3a645004f36c2246b7ea86c3b',
                'timestamp': 1444439492,
                'bits': 469801026,
                'nonce': 928239,
                'block_height': 461663
            }
        }
        bits = Blockchain.get_target(self, 461663, headers3)
        self.assertEqual(
            bits,
            62635231089126922960074598435273835921110428291665699134377033728)

        # after DGWv3 without checkpoint(height=461664)
        headers4 = {
            461663: {
                'version': 3,
                'prev_block_hash':
                '9c87f1e27717aec18617496970b9744dd855f997128fab6733e709fd95d97870',
                'merkle_root':
                '7f22e9001ab92b14a1b057ce07c4f2acecb693f3a645004f36c2246b7ea86c3b',
                'timestamp': 1444439492,
                'bits': 469801026,
                'nonce': 928239,
                'block_height': 461663
            }
        }
        bits = Blockchain.get_target(self, 461664, headers4)
        self.assertEqual(bits, 0)

        # after DGWv3 after checkpoint(1707577)
        headers5 = {
            1707552: {
                'version': 536870912,
                'prev_block_hash':
                'cbf4f8472308b332a146e224a029c8da8c247a5969d55120aeaebe09ddd3349d',
                'merkle_root':
                'e7072b993a857eac248968226de0442f653849b4e9e60389b1ff2f33a0198a6e',
                'timestamp': 1562248741,
                'bits': 438029645,
                'nonce': 2789703934,
                'block_height': 1707552
            },
            1707553: {
                'version': 536870912,
                'prev_block_hash':
                '48ff72a7d036694e9ed274d213f9a2426a50467071df3feda04ab04ad8680cfe',
                'merkle_root':
                '7a7ee20c7692dc24747543d03997c0bec5366dcdb9a94336144a236787ed2c04',
                'timestamp': 1562248829,
                'bits': 438095989,
                'nonce': 3164952393,
                'block_height': 1707553
            },
            1707554: {
                'version': 536870912,
                'prev_block_hash':
                '5a96121d84a93d7552096aaac67915301cb436bb9ce573039095d5dd493b653e',
                'merkle_root':
                '991e5bd597b65897c7f45d26416b61f8b14af8232c7a100fb114a8d0e89abbc9',
                'timestamp': 1562248935,
                'bits': 438157682,
                'nonce': 1445694404,
                'block_height': 1707554
            },
            1707555: {
                'version': 536870912,
                'prev_block_hash':
                '422a5fe3981f83287c5a9b0dccf19cc70c4e67b01e36bb1477d651f94fc48f8b',
                'merkle_root':
                'a2f7addc721296d3ca3fa1818f919ef9764e9ca373e5c8433de84437eb6165de',
                'timestamp': 1562248961,
                'bits': 438157688,
                'nonce': 1478829295,
                'block_height': 1707555
            },
            1707556: {
                'version': 536870912,
                'prev_block_hash':
                '584466ff1759343d10b78d0ab01e3629069ea7d10b89564b081e8d9ff5ff3c30',
                'merkle_root':
                'e321947646ac624c69f016d7230113770433ee428d42723d768eb7fa14d87e36',
                'timestamp': 1562249235,
                'bits': 437798855,
                'nonce': 3758975225,
                'block_height': 1707556
            },
            1707557: {
                'version': 536870912,
                'prev_block_hash':
                '50b5ca95fcac67407dcde71ede2db3ef53b1d1344156a692db44c413d7312a4a',
                'merkle_root':
                '1c145e28cd1a97ba5a3e49358ead6f11a9583f9cccc3a5da11213d9a6928e58c',
                'timestamp': 1562249522,
                'bits': 437953945,
                'nonce': 1836890567,
                'block_height': 1707557
            },
            1707558: {
                'version': 536870912,
                'prev_block_hash':
                '1fca0a8366b93b1c008d63a331056cacb6cf7422620dbd4f2152db0231194015',
                'merkle_root':
                '3888f38e1f8d355a23681510042a6b9c9da0f800bd1fe5d00d110360343b2992',
                'timestamp': 1562249540,
                'bits': 438060179,
                'nonce': 4102151957,
                'block_height': 1707558
            },
            1707559: {
                'version': 536870912,
                'prev_block_hash':
                '8694453c547aac5d3cb3303a1ce113a80d05034ef441de49ebe0e5a50f1d089a',
                'merkle_root':
                'ddf7dfc4978dca3a9e81130348d8e488eeca1b8d400b025fc977df89e308b50a',
                'timestamp': 1562249572,
                'bits': 438068511,
                'nonce': 4081611534,
                'block_height': 1707559
            },
            1707560: {
                'version': 536870912,
                'prev_block_hash':
                '010de19d9cf0f70e9c6bf8d81274d7bf083ee0f47e0dcc9a9339a3273cb0d8ff',
                'merkle_root':
                '5eae451db7fa02b440d73505de56152f31f5b7691a385a1f9d43cc188196d72a',
                'timestamp': 1562249587,
                'bits': 438067280,
                'nonce': 2683725381,
                'block_height': 1707560
            },
            1707561: {
                'version': 536870912,
                'prev_block_hash':
                'c32967fb0ce200bcc7ad8c6776d1db76b89ac6b2638b2b93c0eb7147c93eabb1',
                'merkle_root':
                'b4fb627299b2898be36c20e7e1952e62c25c9a9ae91502b9f0c31f58dd5ef148',
                'timestamp': 1562249642,
                'bits': 438076949,
                'nonce': 1851081340,
                'block_height': 1707561
            },
            1707562: {
                'version': 536870912,
                'prev_block_hash':
                '2b27d0a5a8907e8b4238e99bcc4c6181d41dd5991e34a48434ef3c41f7c74303',
                'merkle_root':
                'afc6babf8f3a9a4407efd8b35735d134afce2c1a05bc40e2f0cd7bbc0f2f9232',
                'timestamp': 1562249744,
                'bits': 438108094,
                'nonce': 336655862,
                'block_height': 1707562
            },
            1707563: {
                'version': 536870912,
                'prev_block_hash':
                'fcbddae87247934e8c5a37199e462aea5074649e64e52f510b006809b1ed4fba',
                'merkle_root':
                'e494390d0ec0c7667285561cb9a6f5fe33e962de95794c970ef2420a775d3002',
                'timestamp': 1562249861,
                'bits': 438168938,
                'nonce': 821061911,
                'block_height': 1707563
            },
            1707564: {
                'version': 536870912,
                'prev_block_hash':
                'a0a3313e2e27e6131b05fcbba565a346dda505272b24fc0d2af7cb01137c8bbc',
                'merkle_root':
                '8497a0c7158c85fa2020a2d53d70f89f3feddc5b2fcc2e61b5cf6624940b1c2e',
                'timestamp': 1562249982,
                'bits': 438084687,
                'nonce': 2147099528,
                'block_height': 1707564
            },
            1707565: {
                'version': 536870912,
                'prev_block_hash':
                '4b8425fdbb06945173be0cf489ee0b93c01f63cf0031bf5c8b80d54b85b650d4',
                'merkle_root':
                '0ff67721536e3cda4927cfbfcc9479e33d5ea37d59b2ceb6238ca28730e5ac92',
                'timestamp': 1562250002,
                'bits': 438168583,
                'nonce': 3843225892,
                'block_height': 1707565
            },
            1707566: {
                'version': 536870912,
                'prev_block_hash':
                '2397a2a53e95377393838f0fcc5e56058f67091f0f29f9052abd11cb08f8482d',
                'merkle_root':
                '99d2149c54966f66204218e537da0ca37870405007d2beb76db4c65db2c506db',
                'timestamp': 1562250012,
                'bits': 438161036,
                'nonce': 1959715855,
                'block_height': 1707566
            },
            1707567: {
                'version': 536870912,
                'prev_block_hash':
                'bd9785c0b5064156b5b679f432d7094ba549669eb0621589114f073318b9f079',
                'merkle_root':
                '841a902029ec01695a977a90fe18805fe595cc5b4fd99eb98d44113e7b615d2e',
                'timestamp': 1562250094,
                'bits': 438150641,
                'nonce': 2029322498,
                'block_height': 1707567
            },
            1707568: {
                'version': 536870912,
                'prev_block_hash':
                '971559f276f5035ba270ddcc43a4577d01512b9e8f15c03ac4adb2ce2410cd3f',
                'merkle_root':
                'b2c37f344fe96d0f6e6a6fa3a76ae16a9a20d05b95dfde43f54da267f377086f',
                'timestamp': 1562250273,
                'bits': 438184125,
                'nonce': 1309432981,
                'block_height': 1707568
            },
            1707569: {
                'version': 536870912,
                'prev_block_hash':
                '1a41cc2e2e3fff671ac76b4f3917bdad1173e9e95615b1157c058cf88c7901d8',
                'merkle_root':
                '012a385ae807323c9b84841066572c5e12b7f449e354ba57c08e2a053d0da91e',
                'timestamp': 1562250323,
                'bits': 438331729,
                'nonce': 2182159598,
                'block_height': 1707569
            },
            1707570: {
                'version': 536870912,
                'prev_block_hash':
                'ea486c2cf14cfa12bbede9796e3f908840d95bbd76e2c22e9c39c7e8d0541c63',
                'merkle_root':
                'dfca511e854e1772183803186d65fa89635d61ee8eda778e9d6e9ee2d75d8db6',
                'timestamp': 1562250360,
                'bits': 438380359,
                'nonce': 1378459661,
                'block_height': 1707570
            },
            1707571: {
                'version': 536870912,
                'prev_block_hash':
                '311f1543cde1d4d94e397369bb857839a68da7b5da220232a9e3e2778ff80fb6',
                'merkle_root':
                '8043d19f3312e455413ed9926ff3c4b1e80322be5bb7cbfd7769ebb31910595d',
                'timestamp': 1562250374,
                'bits': 438375809,
                'nonce': 1935902239,
                'block_height': 1707571
            },
            1707572: {
                'version': 536870912,
                'prev_block_hash':
                'bd1a7c8d557195ccb51da479c8742fd45eed6343f19035dd88128b4c1fe837e9',
                'merkle_root':
                'c7128f5abfa5859b9045e86f2733e954e7e36e476e93a2e74f9cb4663c6e9179',
                'timestamp': 1562250396,
                'bits': 438299099,
                'nonce': 141005783,
                'block_height': 1707572
            },
            1707573: {
                'version': 536870912,
                'prev_block_hash':
                '6c2628421ad9f65a3f5f534b9272003a68ae041909da7836561a6a405b92f16d',
                'merkle_root':
                '528e348a05b0ad3847d3de2daef21925f74db3d537b118fa692e2e49c2f57c77',
                'timestamp': 1562250490,
                'bits': 437795403,
                'nonce': 2301343266,
                'block_height': 1707573
            },
            1707574: {
                'version': 536870912,
                'prev_block_hash':
                '2fbf78a9bd6fecc27fab132c885da037355ffba8c29ae6fb666ecde4f8d2faa2',
                'merkle_root':
                '415f0822a45f274c46bafc0a1a1536bdeb8b1c098ccf8055cc1e270dd28d35af',
                'timestamp': 1562250959,
                'bits': 437859823,
                'nonce': 3905424503,
                'block_height': 1707574
            },
            1707575: {
                'version': 536870912,
                'prev_block_hash':
                '41e124c937a0923b80930f22580fb5b10c671588426bb9560798fa171747f75f',
                'merkle_root':
                '1ef7fb4536283b8ec1f96c0de96933506e6727f31eda3fa4cc6672507d395cf4',
                'timestamp': 1562251121,
                'bits': 438247867,
                'nonce': 980139439,
                'block_height': 1707575
            },
            1707576: {
                'version': 536870912,
                'prev_block_hash':
                '80bc982acc2fd07fda9eab66ff3bc4dda53832070cdfa4b39bab2f3323526d77',
                'merkle_root':
                '1f92db8180b4902260f10f2b60bdd558036478fe7955af3ae60ec98ee74efb39',
                'timestamp': 1562251156,
                'bits': 438316136,
                'nonce': 1154901627,
                'block_height': 1707576
            },
            1707577: {
                'version': 536870912,
                'prev_block_hash':
                '62aed78a2bba10217a30bd4343b6b1f0f7814d60c53011a2077bd8224d6c32e5',
                'merkle_root':
                '06a42184dcb2378dd07eee92a9d27efb577bc690845cc483549a679edce3f7f2',
                'timestamp': 1562251530,
                'bits': 438284469,
                'nonce': 2933328142,
                'block_height': 1707577
            }
        }
        bits = Blockchain.get_target(self, 1707577, headers5)
        self.assertEqual(
            bits,
            50924303622638816608903496611932117387916353275391498616157721)
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)
        return

    def test_expected_hash_mismatch(self):
        #with self.assertRaises(Exception):
        #    Blockchain.verify_header(self.header, self.prev_hash, self.target,
        #                             expected_header_hash="foo")
        return

    def test_prev_hash_mismatch(self):
        #with self.assertRaises(Exception):
        #    Blockchain.verify_header(self.header, "foo", self.target)
        return

    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)
        return

    def test_insufficient_pow(self):
        #with self.assertRaises(Exception):
        #    self.header["nonce"] = 42
        #    Blockchain.verify_header(self.header, self.prev_hash, self.target)
        return

    def test_get_target(self):

        # before DGWv3 with checkpoint(height=2015)
        headers1 = {2015: {'version': 2, 'prev_block_hash': 'f9cba205f996e98f61f87e32ae57fc0a5befa6cd632dd257f3e239f390010622', 'merkle_root': 'af68c1f62b965172df1d81fba95f193cb8e42431bad79a4bfbcc370d301d5710', 'timestamp': 1388536705, 'bits': 503936911, 'nonce': 780010496, 'block_height': 2015}}
        bits = Blockchain.get_target(self, 2015, headers1)
        self.assertEqual(bits, 65339010432214603900175979833807329994044402934458085644623414103638016)

        # before DGWv3 without checkpoint(height=2016)
        headers2 = {2015: {'version': 2, 'prev_block_hash': 'f9cba205f996e98f61f87e32ae57fc0a5befa6cd632dd257f3e239f390010622', 'merkle_root': 'af68c1f62b965172df1d81fba95f193cb8e42431bad79a4bfbcc370d301d5710', 'timestamp': 1388536705, 'bits': 503936911, 'nonce': 780010496, 'block_height': 2015}}
        bits = Blockchain.get_target(self, 2016, headers2)
        self.assertEqual(bits, 0)

        # after DGWv3 with checkpoint(height=461663)
        headers3 = {461663: {'version': 3, 'prev_block_hash': '9c87f1e27717aec18617496970b9744dd855f997128fab6733e709fd95d97870', 'merkle_root': '7f22e9001ab92b14a1b057ce07c4f2acecb693f3a645004f36c2246b7ea86c3b', 'timestamp': 1444439492, 'bits': 469801026, 'nonce': 928239, 'block_height': 461663}}
        bits = Blockchain.get_target(self, 461663, headers3)
        self.assertEqual(bits, 62635231089126922960074598435273835921110428291665699134377033728)

        # after DGWv3 without checkpoint(height=461664)
        headers4 = {461663: {'version': 3, 'prev_block_hash': '9c87f1e27717aec18617496970b9744dd855f997128fab6733e709fd95d97870', 'merkle_root': '7f22e9001ab92b14a1b057ce07c4f2acecb693f3a645004f36c2246b7ea86c3b', 'timestamp': 1444439492, 'bits': 469801026, 'nonce': 928239, 'block_height': 461663}}
        bits = Blockchain.get_target(self, 461664, headers4)
        self.assertEqual(bits, 0)

        # after DGWv3 after checkpoint(2041196)
        headers5 = {2041171: {'version': 536870912, 'prev_block_hash': '434a7dfbfcca0065c8f2ba6c4dca0d7d03a46b417411dcc120d94b07ddf52e7b', 'merkle_root': '8a596a2d547e1e56091beb2cd4e623fbb07102d6be1475b2e014d0b628507259', 'timestamp': 1593801117, 'bits': 436481665, 'nonce': 1743496880, 'block_height': 2041171}, 2041172: {'version': 536870912, 'prev_block_hash': 'd6b2ef885174325a2b1d58417ea130c2591be635de862ef24ad7432793f47441', 'merkle_root': '2c004b38a3c77b7f84c7c02b4b18de1e49211a86a84efca3c6975330ddb6b6f5', 'timestamp': 1593801160, 'bits': 436460739, 'nonce': 3581401239, 'block_height': 2041172}, 2041173: {'version': 536870912, 'prev_block_hash': '92af0c020abb87de989ddd569bef17a137fa9db4871514859eaf2057314f0821', 'merkle_root': '49eff882d7077d074dc345b5778407f91b5820735abc96bfdb9691ff23371c35', 'timestamp': 1593801199, 'bits': 436444198, 'nonce': 3329951871, 'block_height': 2041173}, 2041174: {'version': 536870912, 'prev_block_hash': '7a11206a1d5e43d5fc167094311c7559e666da7b3c760d2edb4fe9fa2b217a13', 'merkle_root': '0a110919bc1babcb67bfc44489275d287e0de2cba93f1c7413728f8a0f7bcd74', 'timestamp': 1593801279, 'bits': 436442310, 'nonce': 1727797376, 'block_height': 2041174}, 2041175: {'version': 536870912, 'prev_block_hash': 'faa2695bf6e93079e0f56fb9d42cfc127245d945f0bbe896f31e858c6b758a08', 'merkle_root': 'f240bc3a43f85a759abc51a1d3ca1dec26890c584dc01a5ba4e23355b0b2e031', 'timestamp': 1593801881, 'bits': 436439951, 'nonce': 1582910108, 'block_height': 2041175}, 2041176: {'version': 536870912, 'prev_block_hash': 'c605d43febc0fa96bca1aa6954b07c53341a38a8ac593bb966863565ad898aa7', 'merkle_root': '7e98aa670ad7a308f500afa6fc479a4242d0e149e0ed9028dc338829543932b4', 'timestamp': 1593801916, 'bits': 436514137, 'nonce': 3534411670, 'block_height': 2041176}, 2041177: {'version': 536870912, 'prev_block_hash': 'f8be68067855beac0c83ea81fa2cf3c3d9762f5880d6bd491836eb74a0cba029', 'merkle_root': '091932b736dd2740d5e18497aa7f20529fc668c6b4cb80332f76e986cdb0c90f', 'timestamp': 1593801934, 'bits': 436514147, 'nonce': 3643762728, 'block_height': 2041177}, 2041178: {'version': 536870912, 'prev_block_hash': '82c6e85d3f04e46c0d2cee639f270336ce4a5a8f8d705d8d588cfa22f09988ab', 'merkle_root': '31811783e61252f9cb34ed361643e24d53cc119f7a79c89ef647e952821258b4', 'timestamp': 1593801945, 'bits': 436506662, 'nonce': 3949576319, 'block_height': 2041178}, 2041179: {'version': 536870912, 'prev_block_hash': '867d26c9ad5bc5000167471f6a362366aebea2b088ff573cc711a6bd2cd7f9d5', 'merkle_root': 'b2f5a1bb3ed87a68e71898a5ecce57c7c775731e06c25fc8722d789e807db960', 'timestamp': 1593802003, 'bits': 436497614, 'nonce': 19476663, 'block_height': 2041179}, 2041180: {'version': 536870912, 'prev_block_hash': '6364958943dbcbaec05b9f936e0fb27bbef7ffe0e0a8270765ca5839ca9af2c9', 'merkle_root': 'bd69449bf37d0e2c68ea0503ac97775a911685568672f4dc39100042c1209a43', 'timestamp': 1593802046, 'bits': 436485451, 'nonce': 2712782926, 'block_height': 2041180}, 2041181: {'version': 536870912, 'prev_block_hash': '824d63360ee456b2b931589bf367aeaffec7b75dd742446cb988c0603d63fc6f', 'merkle_root': '2fed7bcebfd5c15bbbc052ffe6e9804a34dffd80ed711d5a5caa261b27264612', 'timestamp': 1593802052, 'bits': 436482450, 'nonce': 1226458892, 'block_height': 2041181}, 2041182: {'version': 536870912, 'prev_block_hash': '47bde237ec093cba3c73b3a2a83479ee47dff63cf82f98d061db8ca4a8eddac6', 'merkle_root': '3ac72176d7f6ddde78e1c8b2217d1a5f336d1242765c425ac14c02129c8a07d2', 'timestamp': 1593802092, 'bits': 436445473, 'nonce': 466506834, 'block_height': 2041182}, 2041183: {'version': 536870912, 'prev_block_hash': 'bfd4a858c5f48f14c237e16c3426184fe8f505683e3f1f7a52c93214566d39a5', 'merkle_root': '25e3f5572779cbe5a2eab11f4fe670f67e5c70377d60a9752e68c0087f965142', 'timestamp': 1593802176, 'bits': 436444975, 'nonce': 2715928887, 'block_height': 2041183}, 2041184: {'version': 536870912, 'prev_block_hash': 'b8ff5973cb096482ca7b687f74647796e2e5fb099d0ac1500fa1c1e8e0680e68', 'merkle_root': '8f211b790ad40e3451b70b1bf9313bcbd210008e344cae96cc0f27330f40e960', 'timestamp': 1593802218, 'bits': 436439930, 'nonce': 2582206894, 'block_height': 2041184}, 2041185: {'version': 536870912, 'prev_block_hash': '9068bd9309c10fa9c7282fa9ff05b687ff572f2f067a4caacbb6f1c11b51e6fc', 'merkle_root': '87db2e7b1255e34a816dd80f5d1d3be92986ccbd46118dfcb02862f9e805ab5b', 'timestamp': 1593802292, 'bits': 436429603, 'nonce': 1439646624, 'block_height': 2041185}, 2041186: {'version': 536870912, 'prev_block_hash': '21eeb845fc6e155a83862f8cd3813c984545adfcf6fe5ae608d6215c077bc633', 'merkle_root': '178bfaf9e7d671eece7ae9af0973ccbe86f63aca9e4538c7c3736d2896fd18ba', 'timestamp': 1593802295, 'bits': 436418149, 'nonce': 1480004439, 'block_height': 2041186}, 2041187: {'version': 536870912, 'prev_block_hash': '19a4adc778f41c0e224f51db7cdcf6fada007fbbc2c765fafae4b0c1686db20d', 'merkle_root': '8ac9068ea12272dcedd04dbfea921189f6de13de7778c4c88a685ea3721672ba', 'timestamp': 1593802508, 'bits': 436411061, 'nonce': 4042146187, 'block_height': 2041187}, 2041188: {'version': 536870912, 'prev_block_hash': 'ea1187c4c51143856eaebfe2734930720b91eee46fa04af76bdf1adf47da07e0', 'merkle_root': '0ffa65c1b8601a2d8010e7c5a7f6a3e1a2e221ec62d41eb3642bb64eeeba517e', 'timestamp': 1593802658, 'bits': 436423640, 'nonce': 3351417769, 'block_height': 2041188}, 2041189: {'version': 536870912, 'prev_block_hash': '7747ef4c8273b5cbe25d4c929552a9a27cf7ac9d6e049e0e9bfc66bd68268eab', 'merkle_root': '334ebd494644c5d244d650bddff6df16b673b12ac21cc5057570768adc4d2a80', 'timestamp': 1593802749, 'bits': 436433380, 'nonce': 3154212453, 'block_height': 2041189}, 2041190: {'version': 536870912, 'prev_block_hash': 'e76ff499246cb074b43b9fa7f0121e297dc4ba00a2a06287091e37f2a139f1f8', 'merkle_root': 'b3865c5697d5dae08cefa53668c5deb9d59a1228716fa26ac7acca69d25d0346', 'timestamp': 1593802864, 'bits': 436436814, 'nonce': 1476396096, 'block_height': 2041190}, 2041191: {'version': 536870912, 'prev_block_hash': '50f7983f3c0b77cdb73d1e632e4858c725cbe7b297e0934bd519ccf9ae6d3aa5', 'merkle_root': '5d559402ac8c315124f66d0c44f8b877bf4a591b1b1d175656ae183c86bd4341', 'timestamp': 1593802920, 'bits': 436441398, 'nonce': 1563286608, 'block_height': 2041191}, 2041192: {'version': 536870912, 'prev_block_hash': '34a7e2c5139ce0625a1a8713a1ad0547f071963867590d73d889579d4d94b51f', 'merkle_root': '06bfa74fed1fea66d79b24c4233cb5f4efcc971c83061fdcb21fe743034df682', 'timestamp': 1593802977, 'bits': 436437928, 'nonce': 1410343315, 'block_height': 2041192}, 2041193: {'version': 536870912, 'prev_block_hash': '96a58d077e8f43462e8b69dc399ac454d3012026e2ac9fa4d812aa1dadf19fe1', 'merkle_root': 'ef7dd0ce0cdc16be9474a4c29a5fdea7b76f23fcdb080add8c9df295b13b8970', 'timestamp': 1593803456, 'bits': 436439373, 'nonce': 3526226806, 'block_height': 2041193}, 2041194: {'version': 536870912, 'prev_block_hash': '98f86a2cb3fad84776b96686958967b236a892f242e1f94cb5a5382eab812053', 'merkle_root': '17ab70daa3322dd9c3edf8332f43d366e2e46791b181c85f6acea3cceae18891', 'timestamp': 1593803495, 'bits': 436488844, 'nonce': 2621837056, 'block_height': 2041194}, 2041195: {'version': 536870912, 'prev_block_hash': 'b2c08c5b854638dbd708955f0e78444c129f43820b2469da462bb502b4a8a5b6', 'merkle_root': '8cf9ad504f9e58120d234cb900e647d98f00373e263a8157a97f54f073d027a5', 'timestamp': 1593803534, 'bits': 436483214, 'nonce': 674434017, 'block_height': 2041195}}
        bits = Blockchain.get_target(self, 2041196, headers5)
        self.assertEqual(bits, 6741891227282744414704927191482180097961828903172103425159275)
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)
        return

    def test_expected_hash_mismatch(self):
        #with self.assertRaises(Exception):
        #    Blockchain.verify_header(self.header, self.prev_hash, self.target,
        #                             expected_header_hash="foo")
        return

    def test_prev_hash_mismatch(self):
        #with self.assertRaises(Exception):
        #    Blockchain.verify_header(self.header, "foo", self.target)
        return

    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)
        return

    def test_insufficient_pow(self):
        #with self.assertRaises(Exception):
        #    self.header["nonce"] = 42
        #    Blockchain.verify_header(self.header, self.prev_hash, self.target)
        return

    def test_get_target(self):

        # before DGWv3 with checkpoint(height=2015)
        headers1 = {
            2015: {
                'version': 2,
                'prev_block_hash':
                'f9cba205f996e98f61f87e32ae57fc0a5befa6cd632dd257f3e239f390010622',
                'merkle_root':
                'af68c1f62b965172df1d81fba95f193cb8e42431bad79a4bfbcc370d301d5710',
                'timestamp': 1388536705,
                'bits': 503936911,
                'nonce': 780010496,
                'block_height': 2015
            }
        }
        bits = Blockchain.get_target(self, 2015, headers1)
        self.assertEqual(
            bits,
            65339010432214603900175979833807329994044402934458085644623414103638016
        )

        # before DGWv3 without checkpoint(height=2016)
        headers2 = {
            2015: {
                'version': 2,
                'prev_block_hash':
                'f9cba205f996e98f61f87e32ae57fc0a5befa6cd632dd257f3e239f390010622',
                'merkle_root':
                'af68c1f62b965172df1d81fba95f193cb8e42431bad79a4bfbcc370d301d5710',
                'timestamp': 1388536705,
                'bits': 503936911,
                'nonce': 780010496,
                'block_height': 2015
            }
        }
        bits = Blockchain.get_target(self, 2016, headers2)
        self.assertEqual(bits, 0)

        # after DGWv3 with checkpoint(height=461663)
        headers3 = {
            461663: {
                'version': 3,
                'prev_block_hash':
                '9c87f1e27717aec18617496970b9744dd855f997128fab6733e709fd95d97870',
                'merkle_root':
                '7f22e9001ab92b14a1b057ce07c4f2acecb693f3a645004f36c2246b7ea86c3b',
                'timestamp': 1444439492,
                'bits': 469801026,
                'nonce': 928239,
                'block_height': 461663
            }
        }
        bits = Blockchain.get_target(self, 461663, headers3)
        self.assertEqual(
            bits,
            62635231089126922960074598435273835921110428291665699134377033728)

        # after DGWv3 without checkpoint(height=461664)
        headers4 = {
            461663: {
                'version': 3,
                'prev_block_hash':
                '9c87f1e27717aec18617496970b9744dd855f997128fab6733e709fd95d97870',
                'merkle_root':
                '7f22e9001ab92b14a1b057ce07c4f2acecb693f3a645004f36c2246b7ea86c3b',
                'timestamp': 1444439492,
                'bits': 469801026,
                'nonce': 928239,
                'block_height': 461663
            }
        }
        bits = Blockchain.get_target(self, 461664, headers4)
        self.assertEqual(bits, 0)

        # after DGWv3 after checkpoint(2206543)
        headers5 = {
            2206513: {
                'version': 536870912,
                'prev_block_hash':
                '79f9cf8a46f1c823db1005a5f879bbc5e0c3250c516986b80679a900a465b37f',
                'merkle_root':
                'df6f4798d813f2e2545c538c579a97b95c4af4f522ac49401483a19e0de8d47d',
                'timestamp': 1609444650,
                'bits': 436604928,
                'nonce': 2204928177,
                'block_height': 2206513
            },
            2206514: {
                'version': 536870912,
                'prev_block_hash':
                'a3a9fa4099bfb3b251490be1e9f5a509cad82dc44c217d9a7ff1ac44f6e1b2fb',
                'merkle_root':
                '06a9bb6b66584de4d3f9d5bfc44fcd894f7df4419787263918702370fb9cf0d7',
                'timestamp': 1609444702,
                'bits': 436625476,
                'nonce': 329943414,
                'block_height': 2206514
            },
            2206515: {
                'version': 536870912,
                'prev_block_hash':
                '109b0bd3ea80416d1a97c0340b277feac76f6ee0297f3dfba00ece9f53f836f7',
                'merkle_root':
                '71b4078000d4c47602222ff594c525d6b975c82dd25b3fd7fa67d3bad86d8386',
                'timestamp': 1609444825,
                'bits': 436632771,
                'nonce': 1451552657,
                'block_height': 2206515
            },
            2206516: {
                'version': 536870912,
                'prev_block_hash':
                '1fb8ed778b8e8102ee7ceea672dc06d7f6faccc47582e909530ce6e46d7374d4',
                'merkle_root':
                '57796791a3ea547a780a6bb8c6cab38c9565069af5f0616f7e205cc4f71cab04',
                'timestamp': 1609444842,
                'bits': 436624710,
                'nonce': 2528808502,
                'block_height': 2206516
            },
            2206517: {
                'version': 536870912,
                'prev_block_hash':
                '5d389d2b68474a5f19e6a92e2e92ce567e948ad5aa0bf4e459aa87f5a5fca637',
                'merkle_root':
                '5cbc97c215a95e75fe4bcbd5789bfb8a86792a738247b68426938b40e1504a4b',
                'timestamp': 1609444994,
                'bits': 436607628,
                'nonce': 3198985056,
                'block_height': 2206517
            },
            2206518: {
                'version': 536870912,
                'prev_block_hash':
                '84815942d62032fcb2c5bc3b8991c09dde040190f63b9d9ebe46b340b2cd3d6c',
                'merkle_root':
                'c0da13cb17bff5bc2a2331ad6d41ba30eba738efffdefd3c38183faa9234612c',
                'timestamp': 1609445310,
                'bits': 436632069,
                'nonce': 1627859591,
                'block_height': 2206518
            },
            2206519: {
                'version': 536870912,
                'prev_block_hash':
                'c45cc191df1a721c6fa95201cb5e731825160f254f3ca5ff1ca9760a5822ee8b',
                'merkle_root':
                '37860a177a12504292339d5c2f0e6ada37a3602b1d1734e6ae9d4c41ea59f54d',
                'timestamp': 1609445334,
                'bits': 436631610,
                'nonce': 2638329250,
                'block_height': 2206519
            },
            2206520: {
                'version': 536870912,
                'prev_block_hash':
                'adf2460927e1e4aad0bf1523323e07b127c35345add7a747f31a7c91121ff63e',
                'merkle_root':
                '9170108d7c0c9259592afa4de3d3e7050e08a735d124318c734e2abf8f6664a6',
                'timestamp': 1609445589,
                'bits': 436576387,
                'nonce': 3625981221,
                'block_height': 2206520
            },
            2206521: {
                'version': 536870912,
                'prev_block_hash':
                'a21753dbaf91b13f9201907987a94f24ec733c5c135c98a9e745802ddd99eeca',
                'merkle_root':
                '14fc0e2c98fddfcc4dfc4a06ecfa25789cef80a3db7a3eb6fe1c1f7b5c58a09e',
                'timestamp': 1609445713,
                'bits': 436624231,
                'nonce': 3913376559,
                'block_height': 2206521
            },
            2206522: {
                'version': 536870912,
                'prev_block_hash':
                'c015a44dca079df75b4359cef86a56c0c83daafd99ee7f4e4e6a3b973e3cf68d',
                'merkle_root':
                '846fff888522311773f8e799934ba37dff875e768540dcdf3096cff3faaa70d0',
                'timestamp': 1609445772,
                'bits': 436630768,
                'nonce': 2444283459,
                'block_height': 2206522
            },
            2206523: {
                'version': 536870912,
                'prev_block_hash':
                '4a68c57457302e8dfcce11b2bfd687dfd676d7ee73b6de4f8c7c1ea7c7caa8d5',
                'merkle_root':
                'f89d5a02a3e60fa0d0a76548755ee10f657fe8cf14d20c195df661c0395dc2df',
                'timestamp': 1609445813,
                'bits': 436632462,
                'nonce': 1189974575,
                'block_height': 2206523
            },
            2206524: {
                'version': 536870912,
                'prev_block_hash':
                'f8fb4f308a3059f427ab617976f0f8f997f9f85c23ee1155204b30897ae32004',
                'merkle_root':
                '1d4de9345567a065e1524fd56465f59eee2732fb3d4d29c41447ba2aa676dedc',
                'timestamp': 1609445829,
                'bits': 436624998,
                'nonce': 3623875361,
                'block_height': 2206524
            },
            2206525: {
                'version': 536870912,
                'prev_block_hash':
                '1a397adbec8fbabcbb17194f4785f2e47f4335b76c04a30421dfaa422f04cd15',
                'merkle_root':
                '428a9bc92c90cb7e924415f9d9677fc76edb82289b1145be99611200c3ee0a34',
                'timestamp': 1609445845,
                'bits': 436616917,
                'nonce': 632717637,
                'block_height': 2206525
            },
            2206526: {
                'version': 536870912,
                'prev_block_hash':
                'e3dc75e8dfa601bb91615cbb3d50b216f3d6c83f3f6e991d982d95aa88025ab4',
                'merkle_root':
                '836f81e989916f694c674fcf8da11167b8a6f26e86b1e04d51514c22c430bd23',
                'timestamp': 1609445997,
                'bits': 436606062,
                'nonce': 2130612094,
                'block_height': 2206526
            },
            2206527: {
                'version': 536870912,
                'prev_block_hash':
                '240eff2862051667cd689d214d896f8e78eec3a844a368be841ec00a6005eacf',
                'merkle_root':
                '1c685361ea5fbac52a4b876c4c7bf5c60d94f6c31523319dd31e3f1c87f4d01e',
                'timestamp': 1609446064,
                'bits': 436621155,
                'nonce': 3355082659,
                'block_height': 2206527
            },
            2206528: {
                'version': 536870912,
                'prev_block_hash':
                '17cf5b52259574e136520b7810928dcbf8e6d2725fca5271ecdb1288de78b79c',
                'merkle_root':
                'e53478cb75d397b9d168a23d9d6b18d241029ccce75966f2a1635d88d4f55507',
                'timestamp': 1609446097,
                'bits': 436622203,
                'nonce': 2427283207,
                'block_height': 2206528
            },
            2206529: {
                'version': 536870912,
                'prev_block_hash':
                'ceceb35eb792cd7728b779d8bde0c063948964608b2fb5fc08e28b97389f70b0',
                'merkle_root':
                '00e724c066d68c04fa033cc228f4faf9139360f16e24f11ff8f6bcb5d09c2f56',
                'timestamp': 1609446108,
                'bits': 436617031,
                'nonce': 12246852,
                'block_height': 2206529
            },
            2206530: {
                'version': 536870912,
                'prev_block_hash':
                '9fbeb81d66a9690389e6fa4af51fb5cb58b5e1985814a0e3c9282a0b743bc9c1',
                'merkle_root':
                '900a55f729f0ffc485573f14a2eb6ad98e09f062d3b4f14d29ee86b674f80631',
                'timestamp': 1609446465,
                'bits': 436610673,
                'nonce': 3113809169,
                'block_height': 2206530
            },
            2206531: {
                'version': 536870912,
                'prev_block_hash':
                '83e32b6cec70692839a31254d7e1e54cde9e4ebd609909aea911c1ccb6f3bcea',
                'merkle_root':
                '8753aefb0807ce00d6dd08af39c9546d9d99b5058b71325fcee16e825ebad1d5',
                'timestamp': 1609446495,
                'bits': 436652811,
                'nonce': 4034390885,
                'block_height': 2206531
            },
            2206532: {
                'version': 536870912,
                'prev_block_hash':
                '4972be8a70cc57ec5ac8797a70ddfe649e53399b956d9dc6f7c441980398a148',
                'merkle_root':
                'fba036cbf0ae13be324c0cadbb1f5394df5e31730d3f26446acbe71cfa8c8cad',
                'timestamp': 1609446538,
                'bits': 436651818,
                'nonce': 3995783605,
                'block_height': 2206532
            },
            2206533: {
                'version': 536870912,
                'prev_block_hash':
                'ee827023ec8ff367ca696f1ef15428a938f659eede017edb73c5e18926a4be45',
                'merkle_root':
                '3a77f5c65e241bc0f98e7c769856ba8ceab5ee6acac2f6e4179d5c8cccf7a9e7',
                'timestamp': 1609446687,
                'bits': 436624837,
                'nonce': 3037641522,
                'block_height': 2206533
            },
            2206534: {
                'version': 536870912,
                'prev_block_hash':
                '1ecbba9436fc30f6f754d3bf23dce7ea14b043c87b9f6ced3724687ae4c4e4a5',
                'merkle_root':
                'b35280e639c58d2fbd6c68259d6a36c36fa22079fd5c395c2671bedf3f7fb6e0',
                'timestamp': 1609446816,
                'bits': 436644078,
                'nonce': 570649684,
                'block_height': 2206534
            },
            2206535: {
                'version': 536870912,
                'prev_block_hash':
                '4f244738562a8d5043647226bf365562b21c7969307adfae62c27f55efd15bb7',
                'merkle_root':
                '9a5d1d4dfc02cd2deea1ad45fcf009e1c71aefdcf37b2e246e2984ef1de16900',
                'timestamp': 1609446927,
                'bits': 436667852,
                'nonce': 3209124518,
                'block_height': 2206535
            },
            2206536: {
                'version': 536870912,
                'prev_block_hash':
                '8458f4914392ca79bed665183325028a9dc923a5bd9adcffb71504418df4c85b',
                'merkle_root':
                'c8a0b1dc4eaf05a864a3e4b18d80c0a01241c5467efc77abfb6dbe8a62c0d17f',
                'timestamp': 1609446940,
                'bits': 436678347,
                'nonce': 1558666374,
                'block_height': 2206536
            },
            2206537: {
                'version': 536870912,
                'prev_block_hash':
                '950e1e6fde2374c4e6b5c6410de17aa06cc529e744f37dd0c32c64dd1d7bb746',
                'merkle_root':
                'aed89fdc09cdf3893d35a62df0182c943cc3a0309b4251d5f9923c85acc08c70',
                'timestamp': 1609447234,
                'bits': 436655004,
                'nonce': 1640074009,
                'block_height': 2206537
            },
            2206538: {
                'version': 536870912,
                'prev_block_hash':
                '28bc9fdd7ab0d15f567645e174c7e3ea7027ef90611e730b5462a6282946d0b9',
                'merkle_root':
                '72a363391d361998612c938f3ced45273d810eec5d23c1ba6154f4ed9725679b',
                'timestamp': 1609447281,
                'bits': 436703536,
                'nonce': 4023305371,
                'block_height': 2206538
            },
            2206539: {
                'version': 536870912,
                'prev_block_hash':
                '7a558e121576bf037355022758126a6dcff6eb1196d1a6637f9b0d66631f178f',
                'merkle_root':
                'b5508485f20a33c62f5b54c0ae8e93a5fb448f6d59493fed8e7c44a0c80d83c8',
                'timestamp': 1609447384,
                'bits': 436694408,
                'nonce': 739444754,
                'block_height': 2206539
            },
            2206540: {
                'version': 536870912,
                'prev_block_hash':
                '918ffd6a492a437afc25cc54921f9f62b7c8ba84529b5c4de36afc1693e7fe32',
                'merkle_root':
                'd5aeafdb55ea5e1ed86fc03b6789964bf2bc1f828782cd356d1a31ecb49b9887',
                'timestamp': 1609447413,
                'bits': 436713926,
                'nonce': 1895755322,
                'block_height': 2206540
            },
            2206541: {
                'version': 536870912,
                'prev_block_hash':
                '02755cd1cec2f837100165b0106050b2eb4ed7869b19e2605a7c828e7d515452',
                'merkle_root':
                '1ce0429268d986fdd324b6fbb3119126288bffd7adf71b50dc2abac011956d3f',
                'timestamp': 1609447455,
                'bits': 436694298,
                'nonce': 2787690507,
                'block_height': 2206541
            },
            2206542: {
                'version': 536870912,
                'prev_block_hash':
                'd227495a03fec3ae3ab4d33389f8ddff2924bf52b4e397fef064e47d2b80b3be',
                'merkle_root':
                'e59c530f17cd030c80090c5aff4a998890b92472d723843248f63ff04a3fd141',
                'timestamp': 1609447500,
                'bits': 436641834,
                'nonce': 3636446255,
                'block_height': 2206542
            },
            2206543: {
                'version': 536870912,
                'prev_block_hash':
                '30594681b22092a3ba73b532accc7835f117984098164cd10bb9a8c4272e33f3',
                'merkle_root':
                '66ad5a4a7c9037f69d47a1103a8a3ecc1103b75891b2ac0280567431cfccd549',
                'timestamp': 1609447533,
                'bits': 436644373,
                'nonce': 850699273,
                'block_height': 2206543
            }
        }
        bits = Blockchain.get_target(self, 2206543, headers5)
        self.assertEqual(
            bits,
            10709251786800936527318757626382864578020150972591414166005562)