def test_stream_threading(self): bts = self.bts b = Blockchain(crea_instance=bts) ops_stream_no_threading = [] opNames = ["transfer", "vote"] block_num_list2 = [] for op in b.stream(opNames=opNames, start=self.start, stop=self.stop, threading=False): ops_stream_no_threading.append(op) if op["block_num"] not in block_num_list2: block_num_list2.append(op["block_num"]) for n in range(5): ops_stream = [] block_num_list = [] for op in b.stream(opNames=opNames, start=self.start, stop=self.stop, threading=True, thread_num=8): ops_stream.append(op) if op["block_num"] not in block_num_list: block_num_list.append(op["block_num"]) self.assertEqual(ops_stream[0]["block_num"], ops_stream_no_threading[0]["block_num"]) self.assertEqual(ops_stream[-1]["block_num"], ops_stream_no_threading[-1]["block_num"]) self.assertEqual(len(ops_stream_no_threading), len(ops_stream)) self.assertEqual(len(block_num_list), len(block_num_list2)) for i in range(len(block_num_list)): self.assertEqual(block_num_list[i], block_num_list2[i])
def profiling(name_list): stm = Crea() set_shared_crea_instance(stm) del stm print("start") for name in name_list: print("account: %s" % (name)) acc = Account(name) max_index = acc.virtual_op_count() print(max_index) stopTime = datetime(2018, 4, 22, 0, 0, 0) hist_elem = None for h in acc.history_reverse(stop=stopTime): hist_elem = h print(hist_elem) print("blockchain") blockchain_object = Blockchain() current_num = blockchain_object.get_current_block_num() startBlockNumber = current_num - 20 endBlockNumber = current_num block_elem = None for o in blockchain_object.stream(start=startBlockNumber, stop=endBlockNumber): print("block %d" % (o["block_num"])) block_elem = o print(block_elem)
def test_signing_appbase(self): b = Blockchain(crea_instance=self.bts) st = None for block in b.blocks(start=25304468, stop=25304468): for trx in block.transactions: st = Signed_Transaction(trx.copy()) self.assertTrue(st is not None)
def test_stream_batch(self): bts = self.bts b = Blockchain(crea_instance=bts) ops_stream = [] opNames = ["transfer", "vote"] for op in b.stream(opNames=opNames, start=self.start, stop=self.stop, max_batch_size=self.max_batch_size, threading=False): ops_stream.append(op) self.assertTrue(ops_stream[0]["block_num"] >= self.start) self.assertTrue(ops_stream[-1]["block_num"] <= self.stop) op_stat = b.ops_statistics(start=self.start, stop=self.stop) self.assertEqual(op_stat["vote"] + op_stat["transfer"], len(ops_stream)) ops_blocks = [] for op in b.blocks(start=self.start, stop=self.stop, max_batch_size=self.max_batch_size, threading=False): ops_blocks.append(op) op_stat4 = {"transfer": 0, "vote": 0} self.assertTrue(len(ops_blocks) > 0) for block in ops_blocks: for tran in block["transactions"]: for op in tran['operations']: if isinstance(op, dict) and "type" in op and "value" in op: op_type = op["type"] if len(op_type) > 10 and op_type[len(op_type) - 10:] == "_operation": op_type = op_type[:-10] if op_type in opNames: op_stat4[op_type] += 1 elif op[0] in opNames: op_stat4[op[0]] += 1 self.assertTrue(block.identifier >= self.start) self.assertTrue(block.identifier <= self.stop) self.assertEqual(op_stat["transfer"], op_stat4["transfer"]) self.assertEqual(op_stat["vote"], op_stat4["vote"])
def test_stream2(self): bts = self.bts b = Blockchain(crea_instance=bts) stop_block = b.get_current_block_num() start_block = stop_block - 10 ops_stream = [] for op in b.stream(start=start_block, stop=stop_block): ops_stream.append(op) self.assertTrue(len(ops_stream) > 0)
def test_get_all_accounts(self): bts = self.bts b = Blockchain(crea_instance=bts) accounts = [] limit = 200 for acc in b.get_all_accounts(steps=100, limit=limit): accounts.append(acc) self.assertEqual(len(accounts), limit) self.assertEqual(len(set(accounts)), limit)
def test_stream_batch2(self): bts = self.bts b = Blockchain(crea_instance=bts) ops_stream = [] start_block = 25097000 stop_block = 25097100 opNames = ["account_create", "custom_json"] for op in b.stream(start=int(start_block), stop=int(stop_block), opNames=opNames, max_batch_size=50, threading=False, thread_num=8): ops_stream.append(op) self.assertTrue(ops_stream[0]["block_num"] >= start_block) self.assertTrue(ops_stream[-1]["block_num"] <= stop_block) op_stat = b.ops_statistics(start=start_block, stop=stop_block) self.assertEqual(op_stat["account_create"] + op_stat["custom_json"], len(ops_stream))
def test_block_threading(self): bts = self.bts b = Blockchain(crea_instance=bts) blocks_no_threading = [] for block in b.blocks(start=self.start, stop=self.stop, threading=False, thread_num=8): blocks_no_threading.append(block) for n in range(5): blocks = [] for block in b.blocks(start=self.start, stop=self.stop, threading=True, thread_num=8): blocks.append(block) for i in range(min(len(blocks), len(blocks_no_threading))): self.assertEqual(blocks[i]["block_id"], blocks_no_threading[i]["block_id"]) self.assertEqual(len(blocks_no_threading), len(blocks))
def stream_votes(stm, threading, thread_num): b = Blockchain(crea_instance=stm) opcount = 0 start_time = time.time() for op in b.stream(start=23483000, stop=23485000, threading=threading, thread_num=thread_num, opNames=['vote']): sys.stdout.write("\r%s" % op['block_num']) opcount += 1 now = time.time() total_duration = now - start_time print(" votes: %d, time %.2f" % (opcount, total_duration)) return opcount, total_duration
def test_blockchain(self, node_param): if node_param == "instance": set_shared_crea_instance(self.bts) o = Blockchain() self.assertIn(o.crea.rpc.url, self.urls) with self.assertRaises(RPCConnection): Blockchain(crea_instance=Crea( node="https://abc.d", autoconnect=False, num_retries=1)) else: set_shared_crea_instance( Crea(node="https://abc.d", autoconnect=False, num_retries=1)) stm = self.bts o = Blockchain(crea_instance=stm) self.assertIn(o.crea.rpc.url, self.urls) with self.assertRaises(RPCConnection): Blockchain()
def setUpClass(cls): nodelist = NodeList() nodelist.update_nodes(crea_instance=Crea( node=nodelist.get_nodes(exclude_limited=False), num_retries=10)) cls.bts = Crea(node=nodelist.get_nodes(exclude_limited=True), nobroadcast=True, keys={"active": wif}, num_retries=10) b = Blockchain(crea_instance=cls.bts) num = b.get_current_block_num() cls.start = num - 25 cls.stop = num # from getpass import getpass # self.bts.wallet.unlock(getpass()) set_shared_crea_instance(cls.bts)
def setUpClass(cls): nodelist = NodeList() nodelist.update_nodes(crea_instance=Crea(node=nodelist.get_nodes(exclude_limited=False), num_retries=10)) cls.bts = Crea( node=nodelist.get_nodes(exclude_limited=True), nobroadcast=True, timeout=30, num_retries=30, ) # from getpass import getpass # self.bts.wallet.unlock(getpass()) set_shared_crea_instance(cls.bts) cls.bts.set_default_account("test") b = Blockchain(crea_instance=cls.bts) num = b.get_current_block_num() # num = 23346630 cls.start = num - 25 cls.stop = num
def test_awaitTX(self): bts = self.bts b = Blockchain(crea_instance=bts) trans = { 'ref_block_num': 3855, 'ref_block_prefix': 1730859721, 'expiration': '2018-03-09T06:21:06', 'operations': [], 'extensions': [], 'signatures': [ '2033a872a8ad33c7d5b946871e4c9cc8f08a5809258355fc909058eac83' '20ac2a872517a52b51522930d93dd2c1d5eb9f90b070f75f838c881ff29b11af98d6a1b' ] } with self.assertRaises(Exception): b.awaitTxConfirmation(trans)
def setUpClass(cls): nodelist = NodeList() nodelist.update_nodes(crea_instance=Crea(node=nodelist.get_nodes(exclude_limited=False), num_retries=10)) cls.bts = Crea( node=nodelist.get_nodes(exclude_limited=True), nobroadcast=True, num_retries=10, timeout=30, use_condenser=False, keys={"active": wif}, ) # from getpass import getpass # self.bts.wallet.unlock(getpass()) set_shared_crea_instance(cls.bts) cls.bts.set_default_account("test") b = Blockchain(crea_instance=cls.bts) num = b.get_current_block_num() cls.start = num - 100 cls.stop = num cls.max_batch_size = 1 # appbase does not support batch rpc calls at the momement (internal error)
def test_hash_op(self): bts = self.bts b = Blockchain(crea_instance=bts) op1 = { 'type': 'vote_operation', 'value': { 'voter': 'ubg', 'author': 'yesslife', 'permlink': 'creait-sandwich-contest-week-25-2da-entry', 'weight': 100 } } op2 = [ 'vote', { 'voter': 'ubg', 'author': 'yesslife', 'permlink': 'creait-sandwich-contest-week-25-2da-entry', 'weight': 100 } ] hash1 = b.hash_op(op1) hash2 = b.hash_op(op2) self.assertEqual(hash1, hash2)
def test_blockchain(self): bts = self.bts b = Blockchain(crea_instance=bts) num = b.get_current_block_num() self.assertTrue(num > 0) self.assertTrue(isinstance(num, int)) block = b.get_current_block() self.assertTrue(isinstance(block, Block)) self.assertTrue((num - block.identifier) < 3) block_time = b.block_time(block.identifier) self.assertEqual(block.time(), block_time) block_timestamp = b.block_timestamp(block.identifier) timestamp = int(time.mktime(block.time().timetuple())) self.assertEqual(block_timestamp, timestamp)
def test_estimate_block_num(self): bts = self.bts b = Blockchain(crea_instance=bts) last_block = b.get_current_block() num = last_block.identifier old_block = Block(num - 60, crea_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_wait_for_and_get_block(self): bts = self.bts b = Blockchain(crea_instance=bts, max_block_wait_repetition=18) start_num = b.get_current_block_num() blocknum = start_num last_fetched_block_num = None for i in range(3): block = b.wait_for_and_get_block(blocknum) last_fetched_block_num = block.block_num blocknum = last_fetched_block_num + 1 self.assertEqual(last_fetched_block_num, start_num + 2) b2 = Blockchain(crea_instance=bts, max_block_wait_repetition=1) with self.assertRaises(BlockWaitTimeExceeded): for i in range(300): block = b2.wait_for_and_get_block(blocknum) last_fetched_block_num = block.block_num blocknum = last_fetched_block_num + 2
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/crea-pressure-4-need-for-speed|gandalf" [author, permlink, voter] = resolve_authorpermvoter(authorpermvoter) authorperm = construct_authorperm(author, permlink) last_block_id = 19273700 try: stm = Crea(node=node, num_retries=3, num_retries_call=3, timeout=30) blockchain = Blockchain(crea_instance=stm) blockchain_version = stm.get_blockchain_version() last_block = Block(last_block_id, crea_instance=stm) 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: stm = Crea(node=node, num_retries=3, num_retries_call=3, timeout=30) account = Account("gtg", crea_instance=stm) blockchain_version = stm.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: stm = Crea(node=node, num_retries=3, num_retries_call=3, timeout=30) account = Account("gtg", crea_instance=stm) blockchain_version = stm.get_blockchain_version() start = timer() Vote(authorpermvoter, crea_instance=stm) stop = timer() vote_time = stop - start start = timer() Comment(authorperm, crea_instance=stm) stop = timer() comment_time = stop - start start = timer() Account(author, crea_instance=stm) 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}
if __name__ == "__main__": node_setup = 1 threading = True thread_num = 8 timeout = 10 nodes = NodeList() nodes.update_nodes(weights={"block": 1}) node_list = nodes.get_nodes()[:5] vote_result = [] duration = [] stm = Crea(node=node_list, timeout=timeout) b = Blockchain(crea_instance=stm) block = b.get_current_block() block.set_cache_auto_clean(False) opcount, total_duration = stream_votes(stm, threading, thread_num) print("Finished!") block.set_cache_auto_clean(True) cache_len = len(list(block._cache)) start_time = time.time() block.clear_cache_from_expired_items() clear_duration = time.time() - start_time time.sleep(5) cache_len_after = len(list(block._cache)) start_time = time.time() print(str(block._cache)) clear_duration2 = time.time() - start_time print("Results:")
owner_privkey = owner_key.get_private_key() memo_privkey = memo_key.get_private_key() if useWallet: stm.wallet.addPrivateKey(owner_privkey) stm.wallet.addPrivateKey(active_privkey) stm.wallet.addPrivateKey(memo_privkey) stm.wallet.addPrivateKey(posting_privkey) else: stm = Crea(node=testnet_node, wif={'active': str(active_privkey), 'posting': str(posting_privkey), 'memo': str(memo_privkey)}) account = Account(username, crea_instance=stm) if account["name"] == "crea": account.disallow("crea1", permission='posting') account.allow('crea1', weight=1, permission='posting', account=None) account.follow("crea1") elif account["name"] == "crea5": account.allow('crea4', weight=2, permission='active', account=None) if useWallet: stm.wallet.getAccountFromPrivateKey(str(active_privkey)) # stm.create_account("crea1", creator=account, password=password1) account1 = Account("crea1", crea_instance=stm) b = Blockchain(crea_instance=stm) blocknum = b.get_current_block().identifier account.transfer("crea1", 1, "CBD", "test") b1 = Block(blocknum, crea_instance=stm)
from __future__ import print_function import sys from datetime import timedelta import time import io from crea.blockchain import Blockchain from crea.utils import parse_time import logging log = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) class DemoBot(object): def comment(self, comment_event): print('Comment by {} on post {} by {}:'.format(comment_event['author'], comment_event['parent_permlink'], comment_event['parent_author'])) print(comment_event['body']) print() if __name__ == "__main__": tb = DemoBot() blockchain = Blockchain() for vote in blockchain.stream(opNames=["comment"]): tb.comment(vote)
import sys from datetime import timedelta import time import io from crea.blockchain import Blockchain from crea.utils import parse_time import logging log = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) class DemoBot(object): def vote(self, vote_event): w = vote_event["weight"] if w > 0: print("Vote by", vote_event["voter"], "for", vote_event["author"]) else: if w < 0: print("Downvote by", vote_event["voter"], "for", vote_event["author"]) else: print("(Down)vote by", vote_event["voter"], "for", vote_event["author"], "CANCELED") if __name__ == "__main__": tb = DemoBot() blockchain = Blockchain() for vote in blockchain.stream(opNames=["vote"]): tb.vote(vote)
for line in file_obj: elt = loads(unhexlify(line[:-1].encode("latin"))) yield elt def s_load_binary(file_obj): '''load contents from file_obj, returning a generator that yields one element at a time''' for line in file_obj: elt = loads(unhexlify(line[:-1])) yield elt if __name__ == "__main__": blockchain = Blockchain() threading = True thread_num = 8 cur_block = blockchain.get_current_block() stop = cur_block.identifier startdate = cur_block.time() - timedelta(seconds=3600) start = blockchain.get_estimated_block_num(startdate, accurate=True) outf = gzip.open('blocks1.pkl', 'w') blocks = 0 for block in blockchain.stream(opNames=[], start=start, stop=stop, threading=threading, thread_num=thread_num): s_dump_binary(block, outf) blocks = blocks + 1 if blocks % 200 == 0: print(blocks, "blocks streamed") outf.close()
acc = Account(name, crea_instance=stm) acc_dict[name] = acc if clear_acc_cache: acc.clear_cache() acc_dict = {} if clear_all_cache: clear_cache() if not shared_instance: del stm.rpc if __name__ == "__main__": stm = Crea() print("Shared instance: " + str(stm)) set_shared_crea_instance(stm) b = Blockchain() account_list = [] for a in b.get_all_accounts(limit=500): account_list.append(a) shared_instance = False clear_acc_cache = False clear_all_cache = False node = "https://nodes.creary.net" n = 3 for i in range(1, n + 1): print("%d of %d" % (i, n)) profiling(node, account_list, shared_instance=shared_instance, clear_acc_cache=clear_acc_cache, clear_all_cache=clear_all_cache)
def test_stream(self): bts = self.bts start = self.start stop = self.stop b = Blockchain(crea_instance=bts) ops_stream = [] opNames = ["transfer", "vote"] for op in b.stream(opNames=opNames, start=start, stop=stop): ops_stream.append(op) self.assertTrue(len(ops_stream) > 0) ops_raw_stream = [] opNames = ["transfer", "vote"] for op in b.stream(opNames=opNames, raw_ops=True, start=start, stop=stop): ops_raw_stream.append(op) self.assertTrue(len(ops_raw_stream) > 0) only_ops_stream = [] opNames = ["transfer", "vote"] for op in b.stream(opNames=opNames, start=start, stop=stop, only_ops=True): only_ops_stream.append(op) self.assertTrue(len(only_ops_stream) > 0) only_ops_raw_stream = [] opNames = ["transfer", "vote"] for op in b.stream(opNames=opNames, raw_ops=True, start=start, stop=stop, only_ops=True): only_ops_raw_stream.append(op) self.assertTrue(len(only_ops_raw_stream) > 0) op_stat = b.ops_statistics(start=start, stop=stop) op_stat2 = {"transfer": 0, "vote": 0} for op in ops_stream: self.assertIn(op["type"], opNames) op_stat2[op["type"]] += 1 self.assertTrue(op["block_num"] >= start) self.assertTrue(op["block_num"] <= stop) self.assertEqual(op_stat["transfer"], op_stat2["transfer"]) self.assertEqual(op_stat["vote"], op_stat2["vote"]) op_stat3 = {"transfer": 0, "vote": 0} for op in ops_raw_stream: self.assertIn(op["op"][0], opNames) op_stat3[op["op"][0]] += 1 self.assertTrue(op["block_num"] >= start) self.assertTrue(op["block_num"] <= stop) self.assertEqual(op_stat["transfer"], op_stat3["transfer"]) self.assertEqual(op_stat["vote"], op_stat3["vote"]) op_stat5 = {"transfer": 0, "vote": 0} for op in only_ops_stream: self.assertIn(op["type"], opNames) op_stat5[op["type"]] += 1 self.assertTrue(op["block_num"] >= start) self.assertTrue(op["block_num"] <= stop) self.assertEqual(op_stat["transfer"], op_stat5["transfer"]) self.assertEqual(op_stat["vote"], op_stat5["vote"]) op_stat6 = {"transfer": 0, "vote": 0} for op in only_ops_raw_stream: self.assertIn(op["op"][0], opNames) op_stat6[op["op"][0]] += 1 self.assertTrue(op["block_num"] >= start) self.assertTrue(op["block_num"] <= stop) self.assertEqual(op_stat["transfer"], op_stat6["transfer"]) self.assertEqual(op_stat["vote"], op_stat6["vote"]) ops_blocks = [] for op in b.blocks(start=start, stop=stop): ops_blocks.append(op) op_stat4 = {"transfer": 0, "vote": 0} self.assertTrue(len(ops_blocks) > 0) for block in ops_blocks: for tran in block["transactions"]: for op in tran['operations']: if isinstance(op, list) and op[0] in opNames: op_stat4[op[0]] += 1 elif isinstance(op, dict): op_type = op["type"] if len(op_type) > 10 and op_type[len(op_type) - 10:] == "_operation": op_type = op_type[:-10] if op_type in opNames: op_stat4[op_type] += 1 self.assertTrue(block.identifier >= start) self.assertTrue(block.identifier <= stop) self.assertEqual(op_stat["transfer"], op_stat4["transfer"]) self.assertEqual(op_stat["vote"], op_stat4["vote"]) ops_blocks = [] for op in b.blocks(): ops_blocks.append(op) break self.assertTrue(len(ops_blocks) == 1)
how_many_minutes = 10 how_many_virtual_op = 10000 max_batch_size = None threading = False thread_num = 16 nodelist = NodeList() 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: stm = Crea(node=node, num_retries=3) blockchain = Blockchain(crea_instance=stm) account = Account("gtg", crea_instance=stm) virtual_op_count = account.virtual_op_count() blockchain_version = stm.get_blockchain_version() last_block_id = 19273700 last_block = Block(last_block_id, crea_instance=stm) 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()