def test_estimate_virtual_op_num(self): hv = self.bts account = Account("gtg", hive_instance=hv) block_num = 21248120 block = Block(block_num, hive_instance=hv) op_num1 = account.estimate_virtual_op_num(block.time(), stop_diff=1, max_count=100) op_num2 = account.estimate_virtual_op_num(block_num, stop_diff=1, max_count=100) op_num3 = account.estimate_virtual_op_num(block_num, stop_diff=100, max_count=100) op_num4 = account.estimate_virtual_op_num(block_num, stop_diff=0.00001, max_count=100) self.assertTrue(abs(op_num1 - op_num2) < 2) self.assertTrue(abs(op_num1 - op_num4) < 2) self.assertTrue(abs(op_num1 - op_num3) < 200) block_diff1 = 0 block_diff2 = 0 for h in account.get_account_history(op_num4 - 1, 0): block_diff1 = (block_num - h["block"]) for h in account.get_account_history(op_num4 + 1, 0): block_diff2 = (block_num - h["block"]) self.assertTrue(block_diff1 > 0) self.assertTrue(block_diff2 <= 0)
def test_estimate_block_num(self): bts = self.bts b = Blockchain(hive_instance=bts) last_block = b.get_current_block() num = last_block.identifier old_block = Block(num - 60, hive_instance=bts) date = old_block.time() est_block_num = b.get_estimated_block_num(date, accurate=False) self.assertTrue((est_block_num - (old_block.identifier)) < 10) est_block_num = b.get_estimated_block_num(date, accurate=True) self.assertTrue((est_block_num - (old_block.identifier)) < 2) est_block_num = b.get_estimated_block_num(date, estimateForwards=True, accurate=True) self.assertTrue((est_block_num - (old_block.identifier)) < 2) est_block_num = b.get_estimated_block_num(date, estimateForwards=True, accurate=False)
def test_block(self, node_param): if node_param == "instance": set_shared_hive_instance(self.bts) o = Block(1) self.assertIn(o.hive.rpc.url, self.urls) with self.assertRaises(RPCConnection): Block(1, hive_instance=Hive(node="https://abc.d", autoconnect=False, num_retries=1)) else: set_shared_hive_instance( Hive(node="https://abc.d", autoconnect=False, num_retries=1)) hv = self.bts o = Block(1, hive_instance=hv) self.assertIn(o.hive.rpc.url, self.urls) with self.assertRaises(RPCConnection): Block(1)
def test_block_only_ops(self): bts = self.bts test_block_id = self.test_block_id block = Block(test_block_id, only_ops=True, hive_instance=bts) self.assertEqual(block.identifier, test_block_id) self.assertTrue(isinstance(block.time(), datetime)) self.assertTrue(isinstance(block, dict)) self.assertTrue(len(block.operations)) self.assertTrue(isinstance(block.ops_statistics(), dict)) block2 = Block(test_block_id + 1, hive_instance=bts) self.assertTrue(block2.time() > block.time()) with self.assertRaises(exceptions.BlockDoesNotExistsException): Block(0, hive_instance=bts)
def test_verify_transaction(self): hv = self.hv block = Block(22005665, hive_instance=hv) trx = block.transactions[28] signed_tx = Signed_Transaction(trx) key = signed_tx.verify(chain=hv.chain_params, recover_parameter=False) public_key = format(Base58(key[0]), hv.prefix) self.assertEqual( public_key, "STM4tzr1wjmuov9ftXR6QNv7qDWsbShMBPQpuwatZsfSc5pKjRDfq")
def test_export(self): bts = self.bts block_num = 2000000 if bts.rpc.get_use_appbase(): block = bts.rpc.get_block({"block_num": block_num}, api="block") if block and "block" in block: block = block["block"] else: block = bts.rpc.get_block(block_num) b = Block(block_num, hive_instance=bts) keys = list(block.keys()) json_content = b.json() for k in keys: if k not in "json_metadata": if isinstance(block[k], dict) and isinstance( json_content[k], list): self.assertEqual(list(block[k].values()), json_content[k]) else: self.assertEqual(block[k], json_content[k]) if bts.rpc.get_use_appbase(): block = bts.rpc.get_block_header({"block_num": block_num}, api="block") if "header" in block: block = block["header"] else: block = bts.rpc.get_block_header(block_num) b = BlockHeader(block_num, hive_instance=bts) keys = list(block.keys()) json_content = b.json() for k in keys: if k not in "json_metadata": if isinstance(block[k], dict) and isinstance( json_content[k], list): self.assertEqual(list(block[k].values()), json_content[k]) else: self.assertEqual(block[k], json_content[k])
nodes = nodelist.get_nodes(normal=True, appbase=True, dev=True) t = PrettyTable([ "node", "10 blockchain minutes", "10000 virtual account op", "version" ]) t.align = "l" for node in nodes: print("Current node:", node) try: hv = Hive(node=node, num_retries=3) blockchain = Blockchain(hive_instance=hv) account = Account("gtg", hive_instance=hv) virtual_op_count = account.virtual_op_count() blockchain_version = hv.get_blockchain_version() last_block_id = 19273700 last_block = Block(last_block_id, hive_instance=hv) startTime = datetime.now() stopTime = last_block.time() + timedelta(seconds=how_many_minutes * 60) ltime = time.time() cnt = 0 total_transaction = 0 start_time = time.time() last_node = blockchain.hive.rpc.url for entry in blockchain.blocks(start=last_block_id, max_batch_size=max_batch_size, threading=threading, thread_num=thread_num):
if useWallet: hv.wallet.addPrivateKey(owner_privkey) hv.wallet.addPrivateKey(active_privkey) hv.wallet.addPrivateKey(memo_privkey) hv.wallet.addPrivateKey(posting_privkey) else: hv = Hive(node=testnet_node, wif={ 'active': str(active_privkey), 'posting': str(posting_privkey), 'memo': str(memo_privkey) }) account = Account(username, hive_instance=hv) if account["name"] == "bhive": account.disallow("bhive1", permission='posting') account.allow('bhive1', weight=1, permission='posting', account=None) account.follow("bhive1") elif account["name"] == "bhive5": account.allow('bhive4', weight=2, permission='active', account=None) if useWallet: hv.wallet.getAccountFromPrivateKey(str(active_privkey)) # hv.create_account("bhive1", creator=account, password=password1) account1 = Account("bhive1", hive_instance=hv) b = Blockchain(hive_instance=hv) blocknum = b.get_current_block().identifier account.transfer("bhive1", 1, "HBD", "test") b1 = Block(blocknum, hive_instance=hv)
def benchmark_node(node, how_many_minutes=10, how_many_seconds=30): block_count = 0 history_count = 0 access_time = 0 follow_time = 0 blockchain_version = u'0.0.0' successful = True error_msg = None start_total = timer() max_batch_size = None threading = False thread_num = 16 authorpermvoter = u"@gtg/hive-pressure-4-need-for-speed|gandalf" [author, permlink, voter] = resolve_authorpermvoter(authorpermvoter) authorperm = construct_authorperm(author, permlink) last_block_id = 19273700 try: hv = Hive(node=node, num_retries=3, num_retries_call=3, timeout=30) blockchain = Blockchain(hive_instance=hv) blockchain_version = hv.get_blockchain_version() last_block = Block(last_block_id, hive_instance=hv) stopTime = last_block.time() + timedelta(seconds=how_many_minutes * 60) total_transaction = 0 start = timer() for entry in blockchain.blocks(start=last_block_id, max_batch_size=max_batch_size, threading=threading, thread_num=thread_num): block_no = entry.identifier block_count += 1 if "block" in entry: trxs = entry["block"]["transactions"] else: trxs = entry["transactions"] for tx in trxs: for op in tx["operations"]: total_transaction += 1 if "block" in entry: block_time = parse_time(entry["block"]["timestamp"]) else: block_time = parse_time(entry["timestamp"]) if block_time > stopTime: last_block_id = block_no break if timer() - start > how_many_seconds or quit_thread: break except NumRetriesReached: error_msg = 'NumRetriesReached' block_count = -1 except KeyboardInterrupt: error_msg = 'KeyboardInterrupt' # quit = True except Exception as e: error_msg = str(e) block_count = -1 try: hv = Hive(node=node, num_retries=3, num_retries_call=3, timeout=30) account = Account("gtg", hive_instance=hv) blockchain_version = hv.get_blockchain_version() start = timer() for acc_op in account.history_reverse(batch_size=100): history_count += 1 if timer() - start > how_many_seconds or quit_thread: break except NumRetriesReached: error_msg = 'NumRetriesReached' history_count = -1 successful = False except KeyboardInterrupt: error_msg = 'KeyboardInterrupt' history_count = -1 successful = False # quit = True except Exception as e: error_msg = str(e) history_count = -1 successful = False try: hv = Hive(node=node, num_retries=3, num_retries_call=3, timeout=30) account = Account("gtg", hive_instance=hv) blockchain_version = hv.get_blockchain_version() start = timer() Vote(authorpermvoter, hive_instance=hv) stop = timer() vote_time = stop - start start = timer() Comment(authorperm, hive_instance=hv) stop = timer() comment_time = stop - start start = timer() Account(author, hive_instance=hv) stop = timer() account_time = stop - start start = timer() account.get_followers() stop = timer() follow_time = stop - start access_time = (vote_time + comment_time + account_time + follow_time) / 4.0 except NumRetriesReached: error_msg = 'NumRetriesReached' access_time = -1 except KeyboardInterrupt: error_msg = 'KeyboardInterrupt' # quit = True except Exception as e: error_msg = str(e) access_time = -1 return {'successful': successful, 'node': node, 'error': error_msg, 'total_duration': timer() - start_total, 'block_count': block_count, 'history_count': history_count, 'access_time': access_time, 'follow_time': follow_time, 'version': blockchain_version}