def _test_class(self, class_name, class_type): # TODO Clean old nodes # Setup a clean node to run each test self.stop_nodes() for i in range(len(self.nodes)): datadir = get_datadir_path(self.options.tmpdir, i) shutil.rmtree(datadir) old_pos_files = [ "initial_nodes.json", "genesis_file", "waypoint_config", "public_key" ] for f in old_pos_files: os.remove(os.path.join(self.options.tmpdir, f)) shutil.rmtree(os.path.join(self.options.tmpdir, "private_keys")) self.nodes = [] self.add_nodes(1) node_index = len(self.nodes) - 1 initialize_datadir(self.options.tmpdir, node_index, PortMin.n, self.conf_parameters) self.start_node(node_index) obj = class_type(self.nodes[node_index]) for name in dir(obj): m = getattr(obj, name) if type(m) is types.MethodType and name.startswith("test_"): self.log.info("Test " + class_name + "." + name) m()
def add_second_node(self): initialize_datadir(self.options.tmpdir, 1) # self.nodes.append(start_node(1, self.options.tmpdir, extra_args=['-regtestprotectcoinbase','-debug=zrpc'])) self.nodes.append( start_node(1, self.options.tmpdir, extra_args=['-debug=zrpc'])) self.nodes[1].setmocktime(starttime + 9000) connect_nodes_bi(self.nodes, 0, 1) self.sync_all()
def _test_class(self, class_name, class_type): # TODO Clean old nodes # Setup a clean node to run each test self.stop_nodes() self.add_nodes(1) node_index = len(self.nodes) - 1 initialize_datadir(self.options.tmpdir, node_index, PortMin.n, self.conf_parameters) self.start_node(node_index) obj = class_type(self.nodes[node_index]) for name in dir(obj): m = getattr(obj, name) if type(m) is types.MethodType and name.startswith("test_"): self.log.info("Test " + class_name + "." + name) m()
def run_test(self): p, v = self.nodes self.setup_stake_coins(p, v) self.generate_sync(p) self.log.info("Setup deposit") setup_deposit(self, p, [v]) self.log.info("Generate few epochs") self.generate_epoch(p, v, count=2) assert_equal(p.getblockcount(), 35) assert_finalizationstate( p, { 'currentEpoch': 7, 'lastJustifiedEpoch': 6, 'lastFinalizedEpoch': 5, 'validators': 1 }) self.log.info("Restarting proposer") self.restart_node(p) # check it doesn't have peers -- i.e., loaded data from disk assert_equal(p.getpeerinfo(), []) self.log.info("Generate few epochs more") generate_block(p, count=9) assert_equal(p.getblockcount(), 44) # it is not connected to validator so that finalization shouldn't move assert_finalizationstate( p, { 'currentEpoch': 9, 'lastJustifiedEpoch': 6, 'lastFinalizedEpoch': 5, 'validators': 1 }) # connect validator and check how it votes self.wait_for_vote_and_disconnect(v, p) generate_block(p, count=1) assert_equal(p.getblockcount(), 45) assert_finalizationstate( p, { 'currentEpoch': 9, 'lastJustifiedEpoch': 8, 'lastFinalizedEpoch': 5, 'validators': 1 }) self.generate_epoch(p, v, count=2) assert_equal(p.getblockcount(), 55) assert_finalizationstate( p, { 'currentEpoch': 11, 'lastJustifiedEpoch': 10, 'lastFinalizedEpoch': 9, 'validators': 1 }) self.log.info("Restarting validator") self.restart_node(v) # check it doesn't have peers -- i.e., loaded data from disk assert_equal(v.getpeerinfo(), []) self.log.info("Generate more epochs") self.generate_epoch(p, v, count=2) assert_equal(p.getblockcount(), 65) assert_finalizationstate( p, { 'currentEpoch': 13, 'lastJustifiedEpoch': 12, 'lastFinalizedEpoch': 11, 'validators': 1 }) connect_nodes(p, v.index) sync_blocks([p, v]) self.log.info("Restart proposer from empty cache") self.stop_node(p.index) cleanup_datadir(self.options.tmpdir, p.index) initialize_datadir(self.options.tmpdir, p.index) self.start_node(p.index) assert_equal(p.getblockcount(), 0) connect_nodes(p, v.index) sync_blocks([p, v]) assert_finalizationstate( p, { 'currentEpoch': 13, 'lastJustifiedEpoch': 12, 'lastFinalizedEpoch': 11, 'validators': 1 }) self.log.info("Restart proposer") self.restart_node(p) assert_finalizationstate( p, { 'currentEpoch': 13, 'lastJustifiedEpoch': 12, 'lastFinalizedEpoch': 11, 'validators': 1 })
def add_second_node(self): initialize_datadir(self.options.tmpdir, 1) self.nodes.append(start_node(1, self.options.tmpdir, extra_args=['-regtestprotectcoinbase','-debug=zrpc'])) self.nodes[1].setmocktime(starttime + 9000) connect_nodes_bi(self.nodes,0,1) self.sync_all()
def run_test(self): test_data = os.path.join(TESTSDIR, self.options.test_data) genesis_block = os.path.join(TESTSDIR, self.options.genesis_block) if self.options.gen_test_data: self.log.info("Generating new genesis block to %s " % genesis_block) with open(genesis_block, 'w', encoding="utf8") as f: f.write(bytes_to_hex_str(self.genesisBlock.serialize())) self.log.info("Generating new test data to %s " % test_data) self.generate_test_data(test_data) else: self.log.info("Loading genesis block from %s" % genesis_block) for i in range(0, self.num_nodes): self.stop_node(i) shutil.rmtree(os.path.join(self.nodes[i].datadir, "regtest")) initialize_datadir(self.options.tmpdir, 0, self.signblockpubkeys, self.signblockthreshold) shutil.copyfile( genesis_block, os.path.join(self.nodes[i].datadir, "genesis.dat")) self.start_node(i) connect_nodes_bi(self.nodes, 0, 1) self.log.info("Loading test data from %s" % test_data) self.load_test_data(test_data) self.sync_all() stats = self.get_stats() expected_stats_noindex = [] for stat_row in stats: expected_stats_noindex.append({ k: v for k, v in stat_row.items() if k not in self.STATS_NEED_TXINDEX }) # Make sure all valid statistics are included but nothing else is expected_keys = self.expected_stats[0].keys() assert_equal(set(stats[0].keys()), set(expected_keys)) assert_equal(stats[0]['height'], self.start_height) assert_equal(stats[self.max_stat_pos]['height'], self.start_height + self.max_stat_pos) for i in range(self.max_stat_pos + 1): self.log.info('Checking block %d\n' % (i)) assert_equal(stats[i], self.expected_stats[i]) # Check selecting block by hash too blockhash = self.expected_stats[i]['blockhash'] stats_by_hash = self.nodes[0].getblockstats( hash_or_height=blockhash) assert_equal(stats_by_hash, self.expected_stats[i]) # Check with the node that has no txindex stats_no_txindex = self.nodes[1].getblockstats( hash_or_height=blockhash, stats=list(expected_stats_noindex[i].keys())) assert_equal(stats_no_txindex, expected_stats_noindex[i]) # Make sure each stat can be queried on its own for stat in expected_keys: for i in range(self.max_stat_pos + 1): result = self.nodes[0].getblockstats( hash_or_height=self.start_height + i, stats=[stat]) assert_equal(list(result.keys()), [stat]) if result[stat] != self.expected_stats[i][stat]: self.log.info( 'result[%s] (%d) failed, %r != %r' % (stat, i, result[stat], self.expected_stats[i][stat])) assert_equal(result[stat], self.expected_stats[i][stat]) # Make sure only the selected statistics are included (more than one) some_stats = {'minfee', 'maxfee'} stats = self.nodes[0].getblockstats(hash_or_height=1, stats=list(some_stats)) assert_equal(set(stats.keys()), some_stats) # Test invalid parameters raise the proper json exceptions tip = self.start_height + self.max_stat_pos assert_raises_rpc_error(-8, 'Target block height %d after current tip %d' % (tip + 1, tip), self.nodes[0].getblockstats, hash_or_height=tip + 1) assert_raises_rpc_error(-8, 'Target block height %d is negative' % (-1), self.nodes[0].getblockstats, hash_or_height=-1) # Make sure not valid stats aren't allowed inv_sel_stat = 'asdfghjkl' inv_stats = [ [inv_sel_stat], ['minfee', inv_sel_stat], [inv_sel_stat, 'minfee'], ['minfee', inv_sel_stat, 'maxfee'], ] for inv_stat in inv_stats: assert_raises_rpc_error(-8, 'Invalid selected statistic %s' % inv_sel_stat, self.nodes[0].getblockstats, hash_or_height=1, stats=inv_stat) # Make sure we aren't always returning inv_sel_stat as the culprit stat assert_raises_rpc_error(-8, 'Invalid selected statistic aaa%s' % inv_sel_stat, self.nodes[0].getblockstats, hash_or_height=1, stats=['minfee', 'aaa%s' % inv_sel_stat]) assert_raises_rpc_error( -8, 'One or more of the selected stats requires -txindex enabled', self.nodes[1].getblockstats, hash_or_height=self.start_height + self.max_stat_pos) # Mainchain's genesis block shouldn't be found on regtest assert_raises_rpc_error( -5, 'Block not found', self.nodes[0].getblockstats, hash_or_height= '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f')