#!/usr/bin/env python2 # Copyright (c) 2014 The Bitcoin Core developers # Distributed under the MIT software license, see the accompanying # file COPYING or http://www.opensource.org/licenses/mit-license.php. # # Test reindex code on bad data # from test_framework import BitcoinTestFramework from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException from util import * import io class ReindexTest(BitcoinTestFramework): def setup_network(self): self.nodes = [] self.nodes.append(start_node(0, self.options.tmpdir, ["-debug"])) self.nodes.append(start_node(1, self.options.tmpdir, ["-debug"])) self.nodes.append(start_node(2, self.options.tmpdir, ["-debug"])) self.is_network_split = False def run_test(self): #node 0 mines 2 med sized blocks print "Node 0 mines 2 medium sized blocks" for j in range(2): for k in range(20): random_transaction(self.nodes[0:1], Decimal("1"), Decimal("0.0001"), 0, 0) self.nodes[0].setgenerate(True, 1) #node 1 mines 3 small sized blocks print "Node 1 mines 3 small sized blocks on another chain" for j in range(3): for k in range (3): random_transaction(self.nodes[1:2], Decimal("1"), Decimal("0.0001"), 0, 0) self.nodes[1].setgenerate(True, 1) print "Node 0 reorgs to node 1's chain" connect_nodes_bi(self.nodes, 0, 1) sync_blocks(self.nodes[0:2]) print "Stop node 0 and munge its data, but only in the unused old stale blocks" stop_node(self.nodes[0],0) #munge node0's blk file filename = self.options.tmpdir+"/node0/regtest/blocks/blk00000.dat" with io.open(filename, 'r+b') as file: file.seek(40000) #This is somewhere around block 202 file.write("\0\0\0\0\0\0\0\0\0\0\0\0") self.nodes[0]=start_node(0, self.options.tmpdir, ["-debug"]) print "Starting node 0 again indicates no problem, see block count:" print self.nodes[0].getinfo()["blocks"] stop_node(self.nodes[0],0) print "Stopping/starting with reindex, displays no initial problem, see block count:" self.nodes[0]=start_node(0, self.options.tmpdir, ["-debug","-reindex"]) print self.nodes[0].getinfo()["blocks"] connect_nodes_bi(self.nodes, 0, 1) print "But as more blocks are mined, some active chain blocks btwn (201-203) are overwritten" for j in range(2): for k in range (20): random_transaction(self.nodes[1:2], Decimal("1"), Decimal("0.0001"), 0, 0) print self.nodes[1].setgenerate(True, 1) print "Node 2 mines a longer fork starting at 200" self.nodes[2].setgenerate(True, 10) print "And it's impossible to reorg back to that point (also startup check would fail)" connect_nodes_bi(self.nodes, 1, 2) sync_blocks(self.nodes[0:3]) print "block count node",1,self.nodes[1].getinfo()["blocks"] print "block count node",2,self.nodes[2].getinfo()["blocks"] print "block count node",0,self.nodes[0].getinfo()["blocks"] if __name__ == '__main__': ReindexTest().main()