def testNewDifficultyMatch(self): prevBlockHash = 0x000000000000000005d1e9e192a43a19e2fbd933ffb27df2623187ad5ce10adc startBlock = 342720 self.c.setInitialParent( prevBlockHash, startBlock - 1, 1 ) # start at 0, for difficultyAdjustment tests otherwise getBlockHash out of bounds count = 2016 with open("test/headers/blockchain_headers") as f: f.seek(80 * startBlock) bhBytes = f.read(80 * count) assert self.c.bulkStoreHeader(bhBytes, count) == startBlock - 1 + count assert self.c.getLastBlockHeight() == startBlock - 1 + count assert self.c.getCumulativeDifficulty( ) == count * 44455415962 + 1 # score starts at 1 # adding a low difficulty block should fail since bits!=newBits version = 2 hashMerkleRoot = 0 # doesn't matter time = 1424648937 # same as real block 344736 bits = 0x207FFFFF # REGTEST_EASIEST_DIFFICULTY nonce = 0 hashPrevBlock = 0x00000000000000000f9e30784bd647e91f6923263a674c9c5c18084fe79a41f8 # block 344735 bhBytes = getHeaderBytes(version, hashPrevBlock, hashMerkleRoot, time, bits, nonce) assert dblSha256Flip( bhBytes ) == 0x0016127022e6debe87071eb0091918d2fccbcef0d46a046bcaf71309b0528044 assert self.c.getBlockchainHead() == hashPrevBlock eventArr = [] self.s.block.log_listeners.append( lambda x: eventArr.append(self.c._translator.listen(x))) res = self.c.storeBlockHeader(bhBytes) assert res == self.ERR_RETARGET assert eventArr == [{ '_event_type': 'Failure', 'errCode': self.ERR_RETARGET }] eventArr.pop() # add the real block version = 2 hashMerkleRoot = 0x734c8b7ea7767005409c23a4b907d07329dadc4bac573b2c809f642eec4de26b time = 1424648937 bits = 404196666 nonce = 550403378 hashPrevBlock = 0x00000000000000000f9e30784bd647e91f6923263a674c9c5c18084fe79a41f8 # block 344735 bhBytes = getHeaderBytes(version, hashPrevBlock, hashMerkleRoot, time, bits, nonce) assert dblSha256Flip( bhBytes ) == 0x00000000000000001097f043cca218169e00623c9962b25a0b159eaca2d8ca25 assert self.c.getBlockchainHead() == hashPrevBlock assert self.c.storeBlockHeader(bhBytes) == startBlock - 1 + count + 1 assert count + 1 == 2017
def testStoreNewHead(self): parent = 0x00000000000000000a15bda775b0004fca6368c5b8a61cb0d3b793670b9369c2 height = 357368 chainWork = 1981747039106115 # 1981795846593359 - 48807487244 (diff at 357369) self.c.setInitialParent(parent, height, chainWork) orphanStr = '03000000c269930b6793b7d3b01ca6b8c56863ca4f00b075a7bd150a0000000000000000391c579bd59cb0199baf96dc1bc1066de0dc202bbe18f062a20aa25f78729376ba6f5d55f586161826f45178' bhBytes = orphanStr.decode('hex') orphanHash = 0x00000000000000000db6ab0aa23c28fc707f05f1646d25dba684ffe316bcf24d assert dblSha256Flip(bhBytes) == orphanHash assert self.c.storeBlockHeader(bhBytes) == height + 1 assert self.c.getBlockchainHead() == orphanHash assert self.c.getLastBlockHeight() == height + 1 assert self.c.getChainWork() == 1981795846593359 # real 357369 headerStr = '03000000c269930b6793b7d3b01ca6b8c56863ca4f00b075a7bd150a00000000000000004bdc09e5405944a6319baf5e90335f221d5b91d44f5212c05bb1e751b997cc74db6f5d55f5861618351ec186' bhBytes = headerStr.decode('hex') hash357369 = 0x000000000000000007f379bc159a38fa5ccec4689336f32eba9d148b5c190439 assert dblSha256Flip(bhBytes) == hash357369 assert self.c.storeBlockHeader(bhBytes) == height + 1 assert self.c.getBlockchainHead() == hash357369 assert self.c.getLastBlockHeight() == height + 1 assert self.c.getChainWork() == 1981795846593359
def testLeechVerify(self): block100kPrev = 0x000000000002d01c1fccc21636b607dfd930d31d01c3a62104612a1719011250 self.c.setInitialParent(block100kPrev, 99999, 1) headers = [ "0100000050120119172a610421a6c3011dd330d9df07b63616c2cc1f1cd00200000000006657a9252aacd5c0b2940996ecff952228c3067cc38d4885efb5a4ac4247e9f337221b4d4c86041b0f2b5710", "0100000006e533fd1ada86391f3f6c343204b0d278d4aaec1c0b20aa27ba0300000000006abbb3eb3d733a9fe18967fd7d4c117e4ccbbac5bec4d910d900b3ae0793e77f54241b4d4c86041b4089cc9b", "0100000090f0a9f110702f808219ebea1173056042a714bad51b916cb6800000000000005275289558f51c9966699404ae2294730c3c9f9bda53523ce50e9b95e558da2fdb261b4d4c86041b1ab1bf93", "01000000aff7e0c7dc29d227480c2aa79521419640a161023b51cdb28a3b0100000000003779fc09d638c4c6da0840c41fa625a90b72b125015fd0273f706d61f3be175faa271b4d4c86041b142dca82", "01000000e1c5ba3a6817d53738409f5e7229ffd098d481147b002941a7a002000000000077ed2af87aa4f9f450f8dbd15284720c3fd96f565a13c9de42a3c1440b7fc6a50e281b4d4c86041b08aecda2", "0100000079cda856b143d9db2c1caff01d1aecc8630d30625d10e8b4b8b0000000000000b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f196367291b4d4c86041b8fa45d63", "0100000045dc58743362fe8d8898a7506faa816baed7d391c9bc0b13b0da00000000000021728a2f4f975cc801cb3c672747f1ead8a946b2702b7bd52f7b86dd1aa0c975c02a1b4d4c86041b7b47546d" ] blockHeaderBytes = map(lambda x: x.decode('hex'), headers) for i in range(7): res = self.c.storeBlockHeader(blockHeaderBytes[i]) # print('@@@@ real chain score: ' + str(self.c.getChainWork())) assert res == i + 100000 chainWork = self.c.getChainWork() # block hashes b0 = 0x000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506 b1 = 0x00000000000080b66c911bd5ba14a74260057311eaeb1982802f7010f1a9f090 # block #100001 b2 = 0x0000000000013b8ab2cd513b0261a14096412195a72a0c4827d229dcc7e0f7af b3 = 0x000000000002a0a74129007b1481d498d0ff29725e9f403837d517683abac5e1 b4 = 0x000000000000b0b8b4e8105d62300d63c8ec1a1df0af1c2cdbd943b156a8cd79 b5 = 0x000000000000dab0130bbcc991d3d7ae6b81aa6f50a798888dfe62337458dc45 b6 = 0x0000000000009b958a82c10804bd667722799cc3b457bc061cd4b7779110cd60 # values are from block 100K rawTx = '01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff08044c86041b020602ffffffff0100f2052a010000004341041b0e8c2567c12536aa13357b79a073dc4444acb83c4ec7a0e2f99dd7457516c5817242da796924ca4e99947d087fedf9ce467cb9f7c6287078f801df276fdf84ac00000000'.decode( 'hex') txHash = 0x8c14f0db3df150123e6f3dbbf30f8b955a8249b62ac1d1ff16284aefa3d06d87 assert dblSha256Flip(rawTx) == txHash txIndex = 0 sibling = [None] * 2 sibling[ 0] = 0xfff2525b8931402dd09222c50775608f75787bd2b87e56995a7bdd30f79702c4 sibling[ 1] = 0x8e30899078ca1813be036a073bbf80b86cdddde1c96e9e9c99e9e3782df4ae49 # verifyTx should only return 1 for b0 txBlockHash = b0 assert dblSha256Flip(blockHeaderBytes[0]) == txBlockHash assert 2222 == self.leech.freeVerifyTx(self.c.address, rawTx, txIndex, sibling, blockHeaderBytes[0], 100001) # wrong blockHeight res = self.leech.freeVerifyTx(self.c.address, rawTx, txIndex, sibling, blockHeaderBytes[0], 100000, profiling=True) print('GAS: ' + str(res['gas'])) assert res['output'] == 2222
def testLeechVerify(self): block100kPrev = 0x000000000002d01c1fccc21636b607dfd930d31d01c3a62104612a1719011250 self.c.setInitialParent(block100kPrev, 99999, 1) headers = [ "0100000050120119172a610421a6c3011dd330d9df07b63616c2cc1f1cd00200000000006657a9252aacd5c0b2940996ecff952228c3067cc38d4885efb5a4ac4247e9f337221b4d4c86041b0f2b5710", "0100000006e533fd1ada86391f3f6c343204b0d278d4aaec1c0b20aa27ba0300000000006abbb3eb3d733a9fe18967fd7d4c117e4ccbbac5bec4d910d900b3ae0793e77f54241b4d4c86041b4089cc9b", "0100000090f0a9f110702f808219ebea1173056042a714bad51b916cb6800000000000005275289558f51c9966699404ae2294730c3c9f9bda53523ce50e9b95e558da2fdb261b4d4c86041b1ab1bf93", "01000000aff7e0c7dc29d227480c2aa79521419640a161023b51cdb28a3b0100000000003779fc09d638c4c6da0840c41fa625a90b72b125015fd0273f706d61f3be175faa271b4d4c86041b142dca82", "01000000e1c5ba3a6817d53738409f5e7229ffd098d481147b002941a7a002000000000077ed2af87aa4f9f450f8dbd15284720c3fd96f565a13c9de42a3c1440b7fc6a50e281b4d4c86041b08aecda2", "0100000079cda856b143d9db2c1caff01d1aecc8630d30625d10e8b4b8b0000000000000b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f196367291b4d4c86041b8fa45d63", "0100000045dc58743362fe8d8898a7506faa816baed7d391c9bc0b13b0da00000000000021728a2f4f975cc801cb3c672747f1ead8a946b2702b7bd52f7b86dd1aa0c975c02a1b4d4c86041b7b47546d" ] blockHeaderBytes = map(lambda x: x.decode('hex'), headers) for i in range(7): res = self.c.storeBlockHeader(blockHeaderBytes[i]) # print('@@@@ real chain score: ' + str(self.c.getChainWork())) assert res == i+100000 chainWork = self.c.getChainWork() # block hashes b0 = 0x000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506 b1 = 0x00000000000080b66c911bd5ba14a74260057311eaeb1982802f7010f1a9f090 # block #100001 b2 = 0x0000000000013b8ab2cd513b0261a14096412195a72a0c4827d229dcc7e0f7af b3 = 0x000000000002a0a74129007b1481d498d0ff29725e9f403837d517683abac5e1 b4 = 0x000000000000b0b8b4e8105d62300d63c8ec1a1df0af1c2cdbd943b156a8cd79 b5 = 0x000000000000dab0130bbcc991d3d7ae6b81aa6f50a798888dfe62337458dc45 b6 = 0x0000000000009b958a82c10804bd667722799cc3b457bc061cd4b7779110cd60 # values are from block 100K rawTx = '01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff08044c86041b020602ffffffff0100f2052a010000004341041b0e8c2567c12536aa13357b79a073dc4444acb83c4ec7a0e2f99dd7457516c5817242da796924ca4e99947d087fedf9ce467cb9f7c6287078f801df276fdf84ac00000000'.decode('hex') txHash = 0x8c14f0db3df150123e6f3dbbf30f8b955a8249b62ac1d1ff16284aefa3d06d87 assert dblSha256Flip(rawTx) == txHash txIndex = 0 sibling = [None] * 2 sibling[0] = 0xfff2525b8931402dd09222c50775608f75787bd2b87e56995a7bdd30f79702c4 sibling[1] = 0x8e30899078ca1813be036a073bbf80b86cdddde1c96e9e9c99e9e3782df4ae49 # verifyTx should only return 1 for b0 txBlockHash = b0 assert dblSha256Flip(blockHeaderBytes[0]) == txBlockHash assert 2222 == self.leech.freeVerifyTx(self.c.address, rawTx, txIndex, sibling, blockHeaderBytes[0], 100001) # wrong blockHeight res = self.leech.freeVerifyTx(self.c.address, rawTx, txIndex, sibling, blockHeaderBytes[0], 100000, profiling=True) print('GAS: '+str(res['gas'])) assert res['output'] == 2222
def testNewDifficultyMatch(self): prevBlockHash = 0x000000000000000005d1e9e192a43a19e2fbd933ffb27df2623187ad5ce10adc startBlock = 342720 self.c.setInitialParent(prevBlockHash, startBlock-1, 1) # start at 0, for difficultyAdjustment tests otherwise getBlockHash out of bounds count = 2016 with open("test/headers/blockchain_headers") as f: f.seek(80 * startBlock) bhBytes = f.read(80 * count) assert self.c.bulkStoreHeader(bhBytes, count) == startBlock-1+count assert self.c.getLastBlockHeight() == startBlock-1+count assert self.c.getChainWork() == count*44455415962 + 1 # score starts at 1 # adding a low difficulty block should fail since bits!=newBits version = 2 hashMerkleRoot = 0 # doesn't matter time = 1424648937 # same as real block 344736 bits = 0x207FFFFF # REGTEST_EASIEST_DIFFICULTY nonce = 0 hashPrevBlock = 0x00000000000000000f9e30784bd647e91f6923263a674c9c5c18084fe79a41f8 # block 344735 bhBytes = getHeaderBytes(version, hashPrevBlock, hashMerkleRoot, time, bits, nonce) blockHash = dblSha256Flip(bhBytes) assert blockHash == 0x0016127022e6debe87071eb0091918d2fccbcef0d46a046bcaf71309b0528044 assert self.c.getBlockchainHead() == hashPrevBlock eventArr = [] self.s.block.log_listeners.append(lambda x: eventArr.append(self.c._translator.listen(x))) assert self.c.storeBlockHeader(bhBytes) == 0 assert eventArr == [{'_event_type': 'StoreHeader', 'blockHash': blockHash, 'returnCode': self.ERR_RETARGET }] eventArr.pop() # add the real block version = 2 hashMerkleRoot = 0x734c8b7ea7767005409c23a4b907d07329dadc4bac573b2c809f642eec4de26b time = 1424648937 bits = 404196666 nonce = 550403378 hashPrevBlock = 0x00000000000000000f9e30784bd647e91f6923263a674c9c5c18084fe79a41f8 # block 344735 bhBytes = getHeaderBytes(version, hashPrevBlock, hashMerkleRoot, time, bits, nonce) assert dblSha256Flip(bhBytes) == 0x00000000000000001097f043cca218169e00623c9962b25a0b159eaca2d8ca25 assert self.c.getBlockchainHead() == hashPrevBlock assert self.c.storeBlockHeader(bhBytes) == startBlock-1+count + 1 assert count + 1 == 2017
def testDifficultyShouldBeSame(self): prevBlockHash = 0x000000000000000005d1e9e192a43a19e2fbd933ffb27df2623187ad5ce10adc startBlock = 342720 self.c.setInitialParent(prevBlockHash, startBlock-1, 1) count = 3 with open("test/headers/blockchain_headers") as f: f.seek(80 * startBlock) bhBytes = f.read(80 * count) assert self.c.bulkStoreHeader(bhBytes, count) == startBlock-1+count assert self.c.getLastBlockHeight() == startBlock-1+count assert self.c.getChainWork() == count*44455415962 + 1 # score starts at 1 # adding a low difficulty block should fail since bits!=prevBits version = 1 hashMerkleRoot = 0 # doesn't matter time = 1423499049 # same as real block 342723 bits = 0x207FFFFF # REGTEST_EASIEST_DIFFICULTY nonce = 0 hashPrevBlock = 0x000000000000000004db26747ccd2feb6341c18282a33e2c5d8eb84a1b12d951 # block 342722 bhBytes = getHeaderBytes(version, hashPrevBlock, hashMerkleRoot, time, bits, nonce) blockHash = dblSha256Flip(bhBytes) assert blockHash == 0x2afa6cec2435698406ff2138ae6162469857524c7849970e5bb82039e90a099b eventArr = [] self.s.block.log_listeners.append(lambda x: eventArr.append(self.c._translator.listen(x))) assert self.c.storeBlockHeader(bhBytes) == 0 assert eventArr == [{'_event_type': 'StoreHeader', 'blockHash': blockHash, 'returnCode': self.ERR_DIFFICULTY }] eventArr.pop()
def testDifficultyShouldBeSame(self): prevBlockHash = 0x000000000000000005d1e9e192a43a19e2fbd933ffb27df2623187ad5ce10adc startBlock = 342720 self.c.setInitialParent(prevBlockHash, startBlock-1, 1) count = 3 with open("test/headers/blockchain_headers") as f: f.seek(80 * startBlock) bhBytes = f.read(80 * count) assert self.c.bulkStoreHeader(bhBytes, count) == startBlock-1+count assert self.c.getLastBlockHeight() == startBlock-1+count assert self.c.getCumulativeDifficulty() == count*44455415962 + 1 # score starts at 1 # adding a low difficulty block should fail since bits!=prevBits version = 1 hashMerkleRoot = 0 # doesn't matter time = 1423499049 # same as real block 342723 bits = 0x207FFFFF # REGTEST_EASIEST_DIFFICULTY nonce = 0 hashPrevBlock = 0x000000000000000004db26747ccd2feb6341c18282a33e2c5d8eb84a1b12d951 # block 342722 bhBytes = getHeaderBytes(version, hashPrevBlock, hashMerkleRoot, time, bits, nonce) assert dblSha256Flip(bhBytes) == 0x2afa6cec2435698406ff2138ae6162469857524c7849970e5bb82039e90a099b eventArr = [] self.s.block.log_listeners.append(lambda x: eventArr.append(self.c._translator.listen(x))) res = self.c.storeBlockHeader(bhBytes) assert res == self.ERR_DIFFICULTY assert eventArr == [{'_event_type': 'Failure', 'errCode': self.ERR_DIFFICULTY }] eventArr.pop()
def storeGenesisBlock(self): self.c.setInitialParent(0, 0, 1) blockHeaderStr = ("0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c") bhBytes = blockHeaderStr.decode('hex') res = self.c.storeBlockHeader(bhBytes, profiling=True) res['blockHash'] = dblSha256Flip(bhBytes) return res
def testStoreInvalidPoW(self): res = self.storeGenesisBlock() assert res['output'] == 1 eventArr = [] self.s.block.log_listeners.append(lambda x: eventArr.append(self.c._translator.listen(x))) # nonce is 0, leads to a blockHash that is greater than target, thus invalid PoW invalidPoWHeaderStr = "010000006fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d61900000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d00000000" bhBytes = invalidPoWHeaderStr.decode('hex') assert self.c.storeBlockHeader(bhBytes) == 0 assert eventArr == [{'_event_type': 'StoreHeader', 'blockHash': dblSha256Flip(bhBytes), 'returnCode': self.ERR_PROOF_OF_WORK }] eventArr.pop()
def testStoreBlockOverflowFee(self): startBlockNum = 333000 block333K = 0x000000000000000008360c20a2ceff91cc8c4f357932377f48659b37bb86c759 self.c.setInitialParent(block333K, startBlockNum, 1) weiFee = 0xffffffffffffffffffffffff + 1 blockHeaderStr = '0200000059c786bb379b65487f373279354f8ccc91ffcea2200c36080000000000000000dd9d7757a736fec629ab0ed0f602ba23c77afe7edec85a7026f641fd90bcf8f658ca8154747b1b1894fc742f' bhBytes = blockHeaderStr.decode('hex') res = self.c.storeBlockWithFee(bhBytes, weiFee, sender=tester.k1) assert res == startBlockNum + 1 blockHash = dblSha256Flip(bhBytes) expFeeRecipient = int(tester.a1.encode('hex'), 16) assert self.c.getFeeAmount(blockHash) == weiFee - 1 assert self.c.getFeeRecipient(blockHash) == expFeeRecipient
def storeBlock2(self): # block 2 (from test/headers/firstEleven.txt) blockHeaderStr = ("010000004860eb18bf1b1620e37e9490fc8a427514416fd75159ab86688e9a8300000000d5fdcc541e25de1c7a5addedf24858b8bb665c9f36ef744ee42c316022c90f9bb0bc6649ffff001d08d2bd61") bhBytes = blockHeaderStr.decode('hex') return self.c.storeBlockHeader(bhBytes), dblSha256Flip(bhBytes)
def testVerifyFailAfterReorg(self): block100kPrev = 0x000000000002d01c1fccc21636b607dfd930d31d01c3a62104612a1719011250 self.c.setInitialParent(block100kPrev, 99999, 1) # tx for verification: values are from block 100K tx = 0x8c14f0db3df150123e6f3dbbf30f8b955a8249b62ac1d1ff16284aefa3d06d87 txIndex = 0 sibling = [None] * 2 sibling[0] = 0xfff2525b8931402dd09222c50775608f75787bd2b87e56995a7bdd30f79702c4 sibling[1] = 0x8e30899078ca1813be036a073bbf80b86cdddde1c96e9e9c99e9e3782df4ae49 # insert (easy) blocks # using script/mine.py these are the next 7 blocks # nonce: 0 blockhash: 11bb7c5555b8eab7801b1c4384efcab0d869230fcf4a8f043abad255c99105f8 # nonce: 0 blockhash: 178930a916fa91dd29b2716387b7e024a6b3b2d2efa86bc45c86be223b07a4e5 # nonce: 0 blockhash: 7b3c348edbb3645b34b30259105a941890e95e0ecc0a1c243ff48260d746e456 # nonce: 0 blockhash: 02c67135bd91986f9aaf3f0818baab439202fe5c34400c2c10bff6cd1336d436 # nonce: 1 blockhash: 6e60065cc981914c23897143c75f0cde6e456df65f23afd41ddc6e6ce86b2b63 # nonce: 1 blockhash: 38a052cdf4ef0fddf2de88e687163db7f39cb8de738fa9f5e871a72fc74c57c1 # nonce: 0 blockhash: 2b80a2f4b68e9ebfd4975f5f14a340501d24c3adf041ad9be4cd2576e827328c REGTEST_EASIEST_DIFFICULTY = 0x207fFFFFL version = 1 # real merkle of block100k hashMerkleRoot = 0xf3e94742aca4b5ef85488dc37c06c3282295ffec960994b2c0d5ac2a25a95766 time = 1293623863 # from block100k bits = REGTEST_EASIEST_DIFFICULTY nonce = 1 hashPrevBlock = block100kPrev for i in range(7): nonce = 1 if (i in [4,5]) else 0 blockHeaderBytes = getHeaderBytes(version, hashPrevBlock, hashMerkleRoot, time, bits, nonce) res = self.c.storeBlockHeader(blockHeaderBytes) hashPrevBlock = dblSha256Flip(blockHeaderBytes) assert res == i+100000 # testingonlySetHeaviest is needed because the difficulty from # REGTEST_EASIEST_DIFFICULTY becomes 0 and so the score does not # increase, meaning that heaviesBlock also does not change self.c.testingonlySetHeaviest(0x2b80a2f4b68e9ebfd4975f5f14a340501d24c3adf041ad9be4cd2576e827328c) firstEasyBlock = 0x11bb7c5555b8eab7801b1c4384efcab0d869230fcf4a8f043abad255c99105f8 res = self.c.helperVerifyHash__(tx, txIndex, sibling, firstEasyBlock) assert res == 1 # reorg: add headers with more PoW (difficulty) # add headers one by one and tx should only be verified when the # last header is added, ie tx has enough confirmations headers = [ "0100000050120119172a610421a6c3011dd330d9df07b63616c2cc1f1cd00200000000006657a9252aacd5c0b2940996ecff952228c3067cc38d4885efb5a4ac4247e9f337221b4d4c86041b0f2b5710", "0100000006e533fd1ada86391f3f6c343204b0d278d4aaec1c0b20aa27ba0300000000006abbb3eb3d733a9fe18967fd7d4c117e4ccbbac5bec4d910d900b3ae0793e77f54241b4d4c86041b4089cc9b", "0100000090f0a9f110702f808219ebea1173056042a714bad51b916cb6800000000000005275289558f51c9966699404ae2294730c3c9f9bda53523ce50e9b95e558da2fdb261b4d4c86041b1ab1bf93", "01000000aff7e0c7dc29d227480c2aa79521419640a161023b51cdb28a3b0100000000003779fc09d638c4c6da0840c41fa625a90b72b125015fd0273f706d61f3be175faa271b4d4c86041b142dca82", "01000000e1c5ba3a6817d53738409f5e7229ffd098d481147b002941a7a002000000000077ed2af87aa4f9f450f8dbd15284720c3fd96f565a13c9de42a3c1440b7fc6a50e281b4d4c86041b08aecda2", "0100000079cda856b143d9db2c1caff01d1aecc8630d30625d10e8b4b8b0000000000000b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f196367291b4d4c86041b8fa45d63", "0100000045dc58743362fe8d8898a7506faa816baed7d391c9bc0b13b0da00000000000021728a2f4f975cc801cb3c672747f1ead8a946b2702b7bd52f7b86dd1aa0c975c02a1b4d4c86041b7b47546d" ] blockHeaderBytes = map(lambda x: x.decode('hex'), headers) block100k = 0x000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506 for i in range(7): res = self.c.storeBlockHeader(blockHeaderBytes[i]) assert res == i+100000 # firstEasyBlock should no longer verify since it is no longer on the main chain res = self.c.helperVerifyHash__(tx, txIndex, sibling, firstEasyBlock) assert res == self.ERR_CHAIN # block100k should only verify when it has enough confirmations res = self.c.helperVerifyHash__(tx, txIndex, sibling, block100k) exp = 1 if i==6 else self.ERR_CONFIRMATIONS assert res == exp
def testVerifySuccessAfterReorg(self): forkGrandParent = 0x000000000000000011e40c5deb1a1e3438b350ea3db3fa2dedd285db9650a6e6 forkPrevHash = 0x000000000000000006a320d752b46b532ec0f3f815c5dae467aff5715a6e579e forkPrevNum = 363730 self.c.setInitialParent(forkGrandParent, forkPrevNum-1, 1) forkPrevHashHeader = "03000000e6a65096db85d2ed2dfab33dea50b338341e1aeb5d0ce411000000000000000098420532fa55a0bca5f043f8f8f16a2b73761e822178692cb99ced039e2a32a0ea3f97558e4116183a9cc34a" assert self.c.storeBlockHeader(forkPrevHashHeader.decode('hex')) == forkPrevNum # insert the 6 blocks from the fork that will be orphaned fakeb1 = 0 fakeb6 = 0 with open('test/headers/fork/20150704/6headers.bin') as dataFile: for i in range(1, 7): blockHeaderBytes = dataFile.read(80) res = self.c.storeBlockHeader(blockHeaderBytes) fakeb1 = dblSha256Flip(blockHeaderBytes) if i==1 else fakeb1 fakeb6 = dblSha256Flip(blockHeaderBytes) if i==6 else fakeb6 # print('@@@@ chain score: ' + str(self.c.getChainWork())) assert res == i+forkPrevNum chainWork = self.c.getChainWork() assert chainWork == 49402014931*7 + 1 # 1 is the initial score # # fakeB1 is within6confirms so should NOT verify # txIndex = 1 fakeB1 = 0x00000000000000000c28e23330c29046f19e817fe8fe039f4044b2b2882aef53 fakeTxB1 = 0x3f637aff97a70c89dd279fe14dbd45fda00fe22027f7937d43737464baa247a7 # Ran this offline since 99 txs in fake 363731 # merkleProof = merkle_prove('3f637aff97a70c89dd279fe14dbd45fda00fe22027f7937d43737464baa247a7') merkleProof = {'siblings': ['54a0adf1fa1645aece159cc08593c2302b032e6e0cb5ab437df7134f1cbb59db', 'd27b7c208f92e9ac0caa6d2011cf09271c25a2a63a4a1df4dee5f451ca47b680', '626a0941ee37afccd19015a429f96df232f9ff54c350fc760ba3a13ae3efd609', '64fd56ba7d5f5e3f7d3e9f3c98a6b6ff487e22ddb64aeecddf5eb048db516b17', 'c6ef6fe56abb6d1cbbe042bf1fb304bb708a1f05a4805c451e3680b671c01a7d', '30b0f61f406f37346f002d9eed518dd22aed68c111c3e6543d013703813f2662', '26ec8b0a87da3335f773c35c308c85867733f7e93c185c164aab3f9d3e42730a', '31535344a3d72372df8321540c38f58881d6e49b0b64a69a9bf2b80fa4e31c0f', '96e441b6876b3f72394b4c1e5e4f851df4ba4cd186801879b13873183bbb1968', 'c23c775e159c2af628e4a90d4657557b2cb15212e0ef3b23caf1187e8d779646', 'c6a1286ef43a26b5f9bed22ddad0d9fcae79fb380bdeb43236e8f41e1deab13f'], 'hash': u'3f637aff97a70c89dd279fe14dbd45fda00fe22027f7937d43737464baa247a7', 'header': {'nonce': 992806987, 'hash': u'00000000000000000c28e23330c29046f19e817fe8fe039f4044b2b2882aef53', 'timestamp': 1435977973, 'merkle_root': u'cf159bb2dc81a0237359fa31606aa89ae8b9abd0e08e8e86e5cb6c42fc2b8622', 'version': 3, 'prevhash': u'000000000000000006a320d752b46b532ec0f3f815c5dae467aff5715a6e579e', 'bits': 404111758}} assert int(merkleProof['hash'], 16) == fakeTxB1 assert int(merkleProof['header']['hash'], 16) == fakeB1 eventArr = [] self.s.block.log_listeners.append(lambda x: eventArr.append(self.c._translator.listen(x))) argsArr = argsForVerifyTx(merkleProof, txIndex) res = self.c.helperVerifyHash__(*argsArr) assert eventArr == [{'_event_type': 'VerifyTransaction', 'txHash': argsArr[0], 'returnCode': self.ERR_CHAIN }] eventArr.pop() assert res == self.ERR_CHAIN # verifyTx should only return 1 for b0 txInBlockZero = argsForVerifyTx(*self.tx1ofBlock363730()) assert self.c.helperVerifyHash__(*txInBlockZero) == 1 eventArr.pop() # pop the VerifyTransaction success event assert fakeb6 == self.c.getBlockchainHead() # # insert the blocks that will cause reorg as main chain # headers = [ "030000009e576e5a71f5af67e4dac515f8f3c02e536bb452d720a306000000000000000022862bfc426ccbe5868e8ee0d0abb9e89aa86a6031fa597323a081dcb29b15cff54897558e4116184b082d3b", "0300000053ef2a88b2b244409f03fee87f819ef14690c23033e2280c00000000000000009e90b9ad092c3f37393f163f70fcc054cbc7dc38210f213840fa9cf9791493b3954997558e4116186d536b51", "03000000d691c32ec84e22c0b9c8fbec184c0ec5f113b16e21e04212000000000000000092ccf4a5399e2436948a6917471135340a51967704bff3c55e57f5f0af6ca7d4275397558e411618d0abe918", "03000000eea345978c6b095148670d6128e1cc9069ac6bb3075c35060000000000000000b00ecf72f6d247a60eca5fc70d760939139cc0bc008d483c90b43e22596e0ed1dc5497558e41161884e655a3", "0300000036191cd0a5e5b1f04dec4cbb97170883fa621013e18a35150000000000000000d8f418aa2714981e26938ccd1620649a5c6fbe839eabc133ac0fac49deafe7dcb75597558e41161810d85d32", "03000000deab448a286a2873fcb3eac032aa1fbb13b7c96a3f24950600000000000000005df3fffaf0b0d3db741bf96cbf35830e3497f0634c819779281b4a2e5d301d65cd5697558e411618983a2772", "0300000033c784021ffbbdfff3bea3b4b9a7caa7f4f8c60713f7bc0300000000000000006e28294eb3195a9fe49845bd090bd69afe1e2b9301a8da1c27fd14a819d86da9a25797558e4116181625905a" ] blockHeaderBytes = map(lambda x: x.decode('hex'), headers) for i in range(6): # only 6 blocks first res = self.c.storeBlockHeader(blockHeaderBytes[i]) # print('@@@@ real chain score: ' + str(self.c.getChainWork())) assert res == i+1+forkPrevNum eventArr.pop() # pop the StoreHeader success event assert self.c.getChainWork() == chainWork # chainWork should not change assert self.c.getBlockchainHead() == dblSha256Flip(blockHeaderBytes[-2]) txInBlockOne = argsForVerifyTx(*self.tx1ofBlock363731()) assert self.c.helperVerifyHash__(*txInBlockOne) == self.ERR_CONFIRMATIONS assert eventArr == [{'_event_type': 'VerifyTransaction', 'txHash': txInBlockOne[0], 'returnCode': self.ERR_CONFIRMATIONS }] eventArr.pop() # b0 should still verify assert self.c.helperVerifyHash__(*txInBlockZero) == 1 # add another header and b1 should now verify assert self.c.storeBlockHeader(blockHeaderBytes[-1]) == 363737 assert self.c.helperVerifyHash__(*txInBlockOne) == 1 # b0 should still verify assert self.c.helperVerifyHash__(*txInBlockZero) == 1
def testHeadersFrom100K(self): block100kPrev = 0x000000000002d01c1fccc21636b607dfd930d31d01c3a62104612a1719011250 self.c.setInitialParent(block100kPrev, 99999, 1) headers = [ "0100000050120119172a610421a6c3011dd330d9df07b63616c2cc1f1cd00200000000006657a9252aacd5c0b2940996ecff952228c3067cc38d4885efb5a4ac4247e9f337221b4d4c86041b0f2b5710", "0100000006e533fd1ada86391f3f6c343204b0d278d4aaec1c0b20aa27ba0300000000006abbb3eb3d733a9fe18967fd7d4c117e4ccbbac5bec4d910d900b3ae0793e77f54241b4d4c86041b4089cc9b", "0100000090f0a9f110702f808219ebea1173056042a714bad51b916cb6800000000000005275289558f51c9966699404ae2294730c3c9f9bda53523ce50e9b95e558da2fdb261b4d4c86041b1ab1bf93", "01000000aff7e0c7dc29d227480c2aa79521419640a161023b51cdb28a3b0100000000003779fc09d638c4c6da0840c41fa625a90b72b125015fd0273f706d61f3be175faa271b4d4c86041b142dca82", "01000000e1c5ba3a6817d53738409f5e7229ffd098d481147b002941a7a002000000000077ed2af87aa4f9f450f8dbd15284720c3fd96f565a13c9de42a3c1440b7fc6a50e281b4d4c86041b08aecda2", "0100000079cda856b143d9db2c1caff01d1aecc8630d30625d10e8b4b8b0000000000000b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f196367291b4d4c86041b8fa45d63", "0100000045dc58743362fe8d8898a7506faa816baed7d391c9bc0b13b0da00000000000021728a2f4f975cc801cb3c672747f1ead8a946b2702b7bd52f7b86dd1aa0c975c02a1b4d4c86041b7b47546d" ] blockHeaderBytes = map(lambda x: x.decode('hex'), headers) for i in range(7): res = self.c.storeBlockHeader(blockHeaderBytes[i]) # print('@@@@ real chain score: ' + str(self.c.getChainWork())) assert res == i+100000 chainWork = self.c.getChainWork() # block hashes b0 = 0x000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506 b1 = 0x00000000000080b66c911bd5ba14a74260057311eaeb1982802f7010f1a9f090 # block #100001 b2 = 0x0000000000013b8ab2cd513b0261a14096412195a72a0c4827d229dcc7e0f7af b3 = 0x000000000002a0a74129007b1481d498d0ff29725e9f403837d517683abac5e1 b4 = 0x000000000000b0b8b4e8105d62300d63c8ec1a1df0af1c2cdbd943b156a8cd79 b5 = 0x000000000000dab0130bbcc991d3d7ae6b81aa6f50a798888dfe62337458dc45 b6 = 0x0000000000009b958a82c10804bd667722799cc3b457bc061cd4b7779110cd60 # values are from block 100K tx = 0x8c14f0db3df150123e6f3dbbf30f8b955a8249b62ac1d1ff16284aefa3d06d87 txIndex = 0 sibling = [None] * 2 sibling[0] = 0xfff2525b8931402dd09222c50775608f75787bd2b87e56995a7bdd30f79702c4 sibling[1] = 0x8e30899078ca1813be036a073bbf80b86cdddde1c96e9e9c99e9e3782df4ae49 txBlockHash = 0xdead res = self.c.helperVerifyHash__(tx, txIndex, sibling, txBlockHash) assert res == self.ERR_CHAIN # b1 is within6confirms so should NOT verify # TODO use proper tx, txIndex, sibling. Should also test that # when another header is added, the tx does verify txBlockHash = b1 res = self.c.helperVerifyHash__(tx, txIndex, sibling, txBlockHash) assert res == self.ERR_CONFIRMATIONS # verifyTx should only return 1 for b0 txBlockHash = b0 res = self.c.helperVerifyHash__(tx, txIndex, sibling, txBlockHash) assert res == 1 assert b6 == self.c.getBlockchainHead() # insert (fake) blocks that will not be on main chain # using script/mine.py (commit 3908709) these are the next 7 blocks # nonce: 0 blockhash: 11bb7c5555b8eab7801b1c4384efcab0d869230fcf4a8f043abad255c99105f8 # nonce: 0 blockhash: 178930a916fa91dd29b2716387b7e024a6b3b2d2efa86bc45c86be223b07a4e5 # nonce: 0 blockhash: 7b3c348edbb3645b34b30259105a941890e95e0ecc0a1c243ff48260d746e456 # nonce: 0 blockhash: 02c67135bd91986f9aaf3f0818baab439202fe5c34400c2c10bff6cd1336d436 # nonce: 1 blockhash: 6e60065cc981914c23897143c75f0cde6e456df65f23afd41ddc6e6ce86b2b63 # nonce: 1 blockhash: 38a052cdf4ef0fddf2de88e687163db7f39cb8de738fa9f5e871a72fc74c57c1 # nonce: 0 blockhash: 2b80a2f4b68e9ebfd4975f5f14a340501d24c3adf041ad9be4cd2576e827328c # https://bitcoin.org/en/developer-reference#target-nbits # Difficulty 1, the minimum allowed difficulty, is represented on # mainnet and the current testnet by the nBits value 0x1d00ffff. # Regtest mode uses a different difficulty 1 value of 0x207fffff, # the highest possible value below uint32_max which can be encoded; # this allows near-instant building of blocks in regtest mode. REGTEST_EASIEST_DIFFICULTY = 0x207fFFFFL version = 1 # real merkle of block100k hashMerkleRoot = 0xf3e94742aca4b5ef85488dc37c06c3282295ffec960994b2c0d5ac2a25a95766 time = 1293623863 # from block100k bits = REGTEST_EASIEST_DIFFICULTY nonce = 1 hashPrevBlock = block100kPrev for i in range(7): nonce = 1 if (i in [4,5]) else 0 blockHeaderBytes = getHeaderBytes(version, hashPrevBlock, hashMerkleRoot, time, bits, nonce) res = self.c.storeBlockHeader(blockHeaderBytes) hashPrevBlock = dblSha256Flip(blockHeaderBytes) # print('@@@@ fake chain score: ' + str(self.c.getChainWork())) assert res == i+100000 # fake blocks are stored since there is possibility they can become the main chain assert self.c.getChainWork() == chainWork # chainWork should not change assert b6 == self.c.getBlockchainHead() # forked block should NOT verify txBlockHash = 0x11bb7c5555b8eab7801b1c4384efcab0d869230fcf4a8f043abad255c99105f8 res = self.c.helperVerifyHash__(tx, txIndex, sibling, txBlockHash) assert res == self.ERR_CHAIN # b0 should still verify txBlockHash = b0 res = self.c.helperVerifyHash__(tx, txIndex, sibling, txBlockHash) assert res == 1
def testTwiceReorg(self): # we want to store real headers from 363729 so that at 363735 we # can verify a tx in 363730 (which requires examining 363729, and # will raise TransactionFailed unless 363729 has been stored) forkGrandParent = 0x000000000000000011e40c5deb1a1e3438b350ea3db3fa2dedd285db9650a6e6 forkPrevHash = 0x000000000000000006a320d752b46b532ec0f3f815c5dae467aff5715a6e579e forkPrevNum = 363730 self.c.setInitialParent(forkGrandParent, forkPrevNum-1, 1) forkPrevHashHeader = "03000000e6a65096db85d2ed2dfab33dea50b338341e1aeb5d0ce411000000000000000098420532fa55a0bca5f043f8f8f16a2b73761e822178692cb99ced039e2a32a0ea3f97558e4116183a9cc34a" assert self.c.storeBlockHeader(forkPrevHashHeader.decode('hex')) == forkPrevNum headers = [ "030000009e576e5a71f5af67e4dac515f8f3c02e536bb452d720a306000000000000000022862bfc426ccbe5868e8ee0d0abb9e89aa86a6031fa597323a081dcb29b15cff54897558e4116184b082d3b", "0300000053ef2a88b2b244409f03fee87f819ef14690c23033e2280c00000000000000009e90b9ad092c3f37393f163f70fcc054cbc7dc38210f213840fa9cf9791493b3954997558e4116186d536b51", "03000000d691c32ec84e22c0b9c8fbec184c0ec5f113b16e21e04212000000000000000092ccf4a5399e2436948a6917471135340a51967704bff3c55e57f5f0af6ca7d4275397558e411618d0abe918", "03000000eea345978c6b095148670d6128e1cc9069ac6bb3075c35060000000000000000b00ecf72f6d247a60eca5fc70d760939139cc0bc008d483c90b43e22596e0ed1dc5497558e41161884e655a3", "0300000036191cd0a5e5b1f04dec4cbb97170883fa621013e18a35150000000000000000d8f418aa2714981e26938ccd1620649a5c6fbe839eabc133ac0fac49deafe7dcb75597558e41161810d85d32", "03000000deab448a286a2873fcb3eac032aa1fbb13b7c96a3f24950600000000000000005df3fffaf0b0d3db741bf96cbf35830e3497f0634c819779281b4a2e5d301d65cd5697558e411618983a2772", "0300000033c784021ffbbdfff3bea3b4b9a7caa7f4f8c60713f7bc0300000000000000006e28294eb3195a9fe49845bd090bd69afe1e2b9301a8da1c27fd14a819d86da9a25797558e4116181625905a" ] blockHeaderBytes = map(lambda x: x.decode('hex'), headers) for i in range(5): # store only 5 blocks first res = self.c.storeBlockHeader(blockHeaderBytes[i]) # print('@@@@ real chain score: ' + str(self.c.getChainWork())) assert res == i+1+forkPrevNum chainWork = self.c.getChainWork() assert chainWork == 49402014931*6 + 1 # 1 is the initial score # # store 5 'fake' blocks # with open('test/headers/fork/20150704/6headers.bin') as dataFile: for i in range(5): res = self.c.storeBlockHeader(dataFile.read(80)) # print('@@@@ chain score: ' + str(self.c.getChainWork())) assert res == i+1+forkPrevNum assert self.c.getChainWork() == chainWork txInBlockZero = argsForVerifyTx(*self.tx1ofBlock363730()) assert self.c.helperVerifyHash__(*txInBlockZero) == self.ERR_CONFIRMATIONS # # add 6th (fake) block and txInBlockZero should succeed verification # with open('test/headers/fork/20150704/6headers.bin') as dataFile: dataFile.seek(80*5) res = self.c.storeBlockHeader(dataFile.read(80)) assert res == forkPrevNum + 6 assert self.c.getChainWork() == 49402014931*7 + 1 assert self.c.helperVerifyHash__(*txInBlockZero) == 1 # # add newBlock6 with same difficulty as current block6 # and txInBlockZero should still succeed verification # assert self.c.storeBlockHeader(blockHeaderBytes[-2]) == 363736 assert self.c.getBlockchainHead() == dblSha256Flip(blockHeaderBytes[-2]) assert self.c.helperVerifyHash__(*txInBlockZero) == 1 txInBlockOne = argsForVerifyTx(*self.tx1ofBlock363731()) assert self.c.helperVerifyHash__(*txInBlockOne) == self.ERR_CONFIRMATIONS # # add block7 and txInBlockOne should now succeed verification # assert self.c.storeBlockHeader(blockHeaderBytes[-1]) == 363737 assert self.c.getBlockchainHead() == dblSha256Flip(blockHeaderBytes[-1]) assert self.c.helperVerifyHash__(*txInBlockOne) == 1