Skip to content

Commit

Permalink
Merge pull request bitcoin#600 from gandrewstone/dev3
Browse files Browse the repository at this point in the history
small misc stuff: fix size of total transactions, remove unused decl, and create test case for invalidateblock sync issue
  • Loading branch information
gandrewstone authored May 19, 2017
2 parents d60df16 + 6b0d3a2 commit b4b10c3
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 9 deletions.
82 changes: 77 additions & 5 deletions qa/rpc-tests/invalidateblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,38 @@
# Test InvalidateBlock code
#

import time

from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *


def retryWhile(fn, exc, excStr=None):
while 1:
try:
fn()
break
except exc as e:
if (not excStr is None):
if not excStr in str(e):
raise
time.sleep(.5)


class InvalidateTest(BitcoinTestFramework):


def setup_chain(self):
print("Initializing test directory "+self.options.tmpdir)
initialize_chain_clean(self.options.tmpdir, 3)
initialize_chain_clean(self.options.tmpdir, 4)

def setup_network(self):
self.nodes = []
self.is_network_split = False
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"]))

def run_test(self):
print("Make sure we repopulate setBlockIndexCandidates after InvalidateBlock:")
print("Mine 4 blocks on Node 0")
Expand Down Expand Up @@ -72,5 +87,62 @@ def run_test(self):
if node1height < 4:
raise AssertionError("Node 1 reorged to a lower height: %d"%node1height)

self.testChainSyncWithLongerInvalid()


def testChainSyncWithLongerInvalid(self):
print("verify that IBD continues on a separate chain after a block is invalidated")

ret = self.nodes[0].generate(50)
# after the headers propagate, invalidate the block
retryWhile(lambda: self.nodes[1].invalidateblock(ret[0]), JSONRPCException, "Block not found")
# now generate a competing chain
ret1 = self.nodes[1].generate(25)

# now start up a new node to sync with one of the chains
self.nodes.append(start_node(3, self.options.tmpdir, ["-debug"]))
connect_nodes_bi(self.nodes,0,3)
connect_nodes_bi(self.nodes,1,3)
# invalidate the longest chain
self.nodes[3].invalidateblock(ret[0])
# give it time to sync with the shorter chain on node 1
print("allowing node 3 to sync")
time.sleep(5)
blocks1 = self.nodes[1].getblockcount()
nblocks = self.nodes[3].getblockcount()
# test if it is synced
if nblocks != blocks1:
print("ERROR: node 3 did not sync with longest valid chain")
print("chain tips on 0: %s" % str(self.nodes[0].getchaintips()))
print("chain tips on 1: %s" % str(self.nodes[1].getchaintips()))
print("chain tips on 3: %s" % str(self.nodes[3].getchaintips()))
print("longest chain on 3: %s" % str(self.nodes[3].getblockcount()))
# enable when fixed: assert(nblocks == blocks1); # since I invalidated a block on 0's chain, I should be caught up with 1

print("Now make the other chain (with no invalid blocks) longer")
ret1 = self.nodes[1].generate(50)
time.sleep(5)
blocks1 = self.nodes[1].getblockcount()
nblocks = self.nodes[3].getblockcount()
# test if it is synced
if nblocks != blocks1:
print("node 3 did not sync up")
print("chain tips on 0: %s" % str(self.nodes[0].getchaintips()))
print("chain tips on 1: %s" % str(self.nodes[1].getchaintips()))
print("chain tips on 3: %s" % str(self.nodes[3].getchaintips()))
print("longest chain on 3: %s" % str(self.nodes[3].getblockcount()))
else:
print("node 1 synced with longest chain")



if __name__ == '__main__':
InvalidateTest().main()

def Test():
t = InvalidateTest()
bitcoinConf = {
"debug":["net","blk","thin","mempool","req","bench","evict"], # "lck"
"blockprioritysize":2000000 # we don't want any transactions rejected due to insufficient fees...
}
t.main(["--nocleanup","--noshutdown", "--tmpdir=/ramdisk/test"],bitcoinConf,None)
3 changes: 1 addition & 2 deletions src/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ class CBlockIndex

//! (memory only) Number of transactions in the chain up to and including this block.
//! This value will be non-zero only if and only if transactions for this block and all its parents are available.
//! Change to 64-bit type when necessary; won't happen before 2030
unsigned int nChainTx;
uint64_t nChainTx;

//! Verification status of this block. See enum BlockStatus
unsigned int nStatus;
Expand Down
2 changes: 1 addition & 1 deletion src/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ typedef std::map<int, uint256> MapCheckpoints;
struct CCheckpointData {
MapCheckpoints mapCheckpoints;
int64_t nTimeLastCheckpoint;
int64_t nTransactionsLastCheckpoint;
uint64_t nTransactionsLastCheckpoint;
double fTransactionsPerDay;
};

Expand Down
1 change: 0 additions & 1 deletion src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ CNode *FindNode(const CSubNet &subNet);
CNode *FindNode(const std::string &addrName);
CNode *FindNode(const CService &ip);
int DisconnectSubNetNodes(const CSubNet &subNet);
CNode *ConnectNode(CAddress addrConnect, const char *pszDest = NULL);
bool OpenNetworkConnection(const CAddress &addrConnect,
bool fCountFailure,
CSemaphoreGrant *grantOutbound = NULL,
Expand Down

0 comments on commit b4b10c3

Please sign in to comment.