18 return sum(os.path.getsize(blockdir+f)
for f
in os.listdir(blockdir)
if os.path.isfile(blockdir+f)) / (1024. * 1024.)
28 print(
"Initializing test directory "+self.options.tmpdir)
36 self.
nodes.append(
start_node(0, self.options.tmpdir, [
"-debug",
"-maxreceivebuffer=20000",
"-blockmaxsize=999000",
"-checkblocks=5"], timewait=900))
37 self.
nodes.append(
start_node(1, self.options.tmpdir, [
"-debug",
"-maxreceivebuffer=20000",
"-blockmaxsize=999000",
"-checkblocks=5"], timewait=900))
40 self.
nodes.append(
start_node(2, self.options.tmpdir, [
"-debug",
"-maxreceivebuffer=20000",
"-prune=550"], timewait=900))
41 self.
prunedir = self.options.tmpdir+
"/node2/regtest/blocks/" 66 if not os.path.isfile(self.
prunedir+
"blk00000.dat"):
67 raise AssertionError(
"blk00000.dat is missing, pruning too early")
69 print "Though we're already using more than 550MiB, current usage:",
calc_usage(self.
prunedir)
70 print "Mining 25 more blocks should cause the first block file to be pruned" 75 waitstart = time.time()
76 while os.path.isfile(self.
prunedir+
"blk00000.dat"):
78 if time.time() - waitstart > 10:
79 raise AssertionError(
"blk00000.dat not pruned when it should be")
83 print "Usage should be below target:", usage
85 raise AssertionError(
"Pruning target not being met")
89 print "Mine 24 (stale) blocks on Node 1, followed by 25 (main chain) block reorg from Node 0, for 12 rounds" 96 self.
nodes[0]=
start_node(0, self.options.tmpdir, [
"-debug",
"-maxreceivebuffer=20000",
"-blockmaxsize=999000",
"-checkblocks=5"], timewait=900)
115 print "Usage can be over target because of high stale rate:",
calc_usage(self.
prunedir)
123 self.
nodes[1]=
start_node(1, self.options.tmpdir, [
"-debug",
"-maxreceivebuffer=20000",
"-blockmaxsize=5000",
"-checkblocks=5",
"-disablesafemode"], timewait=900)
126 print "Current block height:", height
128 invalidheight = height-287
130 print "Invalidating block at height:",invalidheight,badhash
137 while curhash != mainchainhash:
146 self.
nodes[1]=
start_node(1, self.options.tmpdir, [
"-debug",
"-maxreceivebuffer=20000",
"-blockmaxsize=5000",
"-checkblocks=5",
"-disablesafemode"], timewait=900)
148 print "Generating new longer chain of 300 more blocks" 151 print "Reconnect nodes" 157 print "Usage possibly still high bc of stale blocks in block files:",
calc_usage(self.
prunedir)
159 print "Mine 220 more blocks so we have requisite history (some blocks will be big and cause pruning of previous chain)" 164 print "Usage should be below target:", usage
166 raise AssertionError(
"Pruning target not being met")
168 return invalidheight,badhash
174 raise AssertionError(
"Old block wasn't pruned so can't test redownload")
175 except JSONRPCException
as e:
176 print "Will need to redownload block",self.forkheight
200 print "Rewind node 0 to prev main chain to mine longer chain to trigger redownload. Blocks needed:", blocks_to_mine
205 goalbestheight = first_reorg_height + 1
207 print "Verify node 2 reorged back to the main chain, some blocks of which it had to redownload" 208 waitstart = time.time()
211 if time.time() - waitstart > 900:
212 raise AssertionError(
"Node 2 didn't reorg to proper height")
215 assert(self.
nodes[2].
getblock(self.forkhash)[
"height"] == self.forkheight)
221 if len(self.
utxo) < 14:
222 self.
utxo = node.listunspent()
226 inputs.append({
"txid" : t[
"txid"],
"vout" : t[
"vout"]})
227 remchange = t[
"amount"] - 100*self.
relayfee 228 outputs[address]=remchange
232 rawtx = node.createrawtransaction(inputs, outputs)
234 newtx = newtx + self.
txouts 235 newtx = newtx + rawtx[94:]
237 signresult = node.signrawtransaction(newtx,
None,
None,
"NONE")
238 txid = node.sendrawtransaction(signresult[
"hex"],
True)
244 print "Warning! This test requires 4GB of disk space and takes over 30 mins (up to 2 hours)" 245 print "Mining a big blockchain of 995 blocks" 256 print "Check that we haven't started pruning yet because we're below PruneAfterHeight" 261 print "Check that we'll exceed disk space target if we have a very high stale block rate" 285 print "Check that we can survive a 288 block reorg still" 286 (self.forkheight,self.forkhash) = self.
reorg_test()
318 print "Test that we can rerequest a block we previously pruned if needed for a reorg" 340 if __name__ ==
'__main__':
def mine_full_block(self, node, address)
def test_height_min(self)
UniValue getblock(const UniValue ¶ms, bool fHelp)
UniValue listunspent(const UniValue ¶ms, bool fHelp)
def connect_nodes(from_connection, node_num)
UniValue getnewaddress(const UniValue ¶ms, bool fHelp)
UniValue getblockhash(const UniValue ¶ms, bool fHelp)
UniValue getblockcount(const UniValue ¶ms, bool fHelp)
def initialize_chain_clean(test_dir, num_nodes)
def start_node(i, dirname, extra_args=None, rpchost=None, timewait=None, binary=None)
UniValue generate(const UniValue ¶ms, bool fHelp)
def sync_blocks(rpc_connections, wait=1)
def create_big_chain(self)
def create_chain_with_staleblocks(self)
UniValue getbestblockhash(const UniValue ¶ms, bool fHelp)
UniValue getnetworkinfo(const UniValue ¶ms, bool fHelp)