def build(self, only_ops=[], exclude_ops=[], enable_rewards=False, enable_out_votes=False, enable_in_votes=False): """ Builds the account history based on all account operations :param array only_ops: Limit generator by these operations (*optional*) :param array exclude_ops: Exclude these operations from generator (*optional*) """ if len(self.timestamps) > 0: start_timestamp = self.timestamps[-1] else: start_timestamp = None for op in sorted(self, key=lambda k: k['timestamp']): ts = parse_time(op['timestamp']) if start_timestamp is not None and start_timestamp > ts: continue # print(op) if op['type'] in exclude_ops: continue if len(only_ops) > 0 and op['type'] not in only_ops: continue self.ops_statistics[op['type']] += 1 self.parse_op(op, only_ops=only_ops, enable_rewards=enable_rewards, enable_out_votes=enable_out_votes, enable_in_votes=enable_in_votes)
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 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: total_duration = formatTimedelta(datetime.now() - startTime) last_block_id = block_no avtran = total_transaction / (last_block_id - 19273700) break start_time = time.time() stopOP = virtual_op_count - how_many_virtual_op + 1 i = 0 for acc_op in account.history_reverse(stop=stopOP): i += 1
def parse_op(self, op, only_ops=[], enable_rewards=False, enable_out_votes=False, enable_in_votes=False): """ Parse account history operation""" ts = parse_time(op['timestamp']) if op['type'] == "account_create": fee_steem = Amount(op['fee'], hive_instance=self.hive).amount fee_vests = self.hive.hp_to_vests(Amount(op['fee'], hive_instance=self.hive).amount, timestamp=ts) # print(fee_vests) if op['new_account_name'] == self.account["name"]: self.update(ts, fee_vests, 0, 0) return if op['creator'] == self.account["name"]: self.update(ts, 0, 0, 0, fee_steem * (-1), 0) return elif op['type'] == "account_create_with_delegation": fee_steem = Amount(op['fee'], hive_instance=self.hive).amount fee_vests = self.hive.hp_to_vests(Amount(op['fee'], hive_instance=self.hive).amount, timestamp=ts) if op['new_account_name'] == self.account["name"]: if Amount(op['delegation'], hive_instance=self.hive).amount > 0: delegation = {'account': op['creator'], 'amount': Amount(op['delegation'], hive_instance=self.hive)} else: delegation = None self.update(ts, fee_vests, delegation, 0) return if op['creator'] == self.account["name"]: delegation = {'account': op['new_account_name'], 'amount': Amount(op['delegation'], hive_instance=self.hive)} self.update(ts, 0, 0, delegation, fee_steem * (-1), 0) return elif op['type'] == "delegate_vesting_shares": vests = Amount(op['vesting_shares'], hive_instance=self.hive) # print(op) if op['delegator'] == self.account["name"]: delegation = {'account': op['delegatee'], 'amount': vests} self.update(ts, 0, 0, delegation) return if op['delegatee'] == self.account["name"]: delegation = {'account': op['delegator'], 'amount': vests} self.update(ts, 0, delegation, 0) return elif op['type'] == "transfer": amount = Amount(op['amount'], hive_instance=self.hive) # print(op) if op['from'] == self.account["name"]: if amount.symbol == self.hive.steem_symbol: self.update(ts, 0, 0, 0, amount * (-1), 0) elif amount.symbol == self.hive.sbd_symbol: self.update(ts, 0, 0, 0, 0, amount * (-1)) if op['to'] == self.account["name"]: if amount.symbol == self.hive.steem_symbol: self.update(ts, 0, 0, 0, amount, 0) elif amount.symbol == self.hive.sbd_symbol: self.update(ts, 0, 0, 0, 0, amount) # print(op, vests) # self.update(ts, vests, 0, 0) return elif op['type'] == "fill_order": current_pays = Amount(op["current_pays"], hive_instance=self.hive) open_pays = Amount(op["open_pays"], hive_instance=self.hive) if op["current_owner"] == self.account["name"]: if current_pays.symbol == self.hive.steem_symbol: self.update(ts, 0, 0, 0, current_pays * (-1), open_pays) elif current_pays.symbol == self.hive.sbd_symbol: self.update(ts, 0, 0, 0, open_pays, current_pays * (-1)) if op["open_owner"] == self.account["name"]: if current_pays.symbol == self.hive.steem_symbol: self.update(ts, 0, 0, 0, current_pays, open_pays * (-1)) elif current_pays.symbol == self.hive.sbd_symbol: self.update(ts, 0, 0, 0, open_pays * (-1), current_pays) # print(op) return elif op['type'] == "transfer_to_vesting": hive = Amount(op['amount'], hive_instance=self.hive) vests = self.hive.hp_to_vests(hive.amount, timestamp=ts) if op['from'] == self.account["name"] and op['to'] == self.account["name"]: self.update(ts, vests, 0, 0, hive * (-1), 0) # power up from and to given account elif op['from'] != self.account["name"] and op['to'] == self.account["name"]: self.update(ts, vests, 0, 0, 0, 0) # power up from another account else: # op['from'] == self.account["name"] and op['to'] != self.account["name"] self.update(ts, 0, 0, 0, hive * (-1), 0) # power up to another account return elif op['type'] == "fill_vesting_withdraw": # print(op) vests = Amount(op['withdrawn'], hive_instance=self.hive) self.update(ts, vests * (-1), 0, 0) return elif op['type'] == "return_vesting_delegation": delegation = {'account': None, 'amount': Amount(op['vesting_shares'], hive_instance=self.hive)} self.update(ts, 0, 0, delegation) return elif op['type'] == "claim_reward_balance": vests = Amount(op['reward_vests'], hive_instance=self.hive) hive = Amount(op['reward_steem'], hive_instance=self.hive) hbd = Amount(op['reward_sbd'], hive_instance=self.hive) self.update(ts, vests, 0, 0, hive, hbd) return elif op['type'] == "curation_reward": if "curation_reward" in only_ops or enable_rewards: vests = Amount(op['reward'], hive_instance=self.hive) if "curation_reward" in only_ops: self.update(ts, vests, 0, 0) if enable_rewards: self.update_rewards(ts, vests, 0, 0, 0) return elif op['type'] == "author_reward": if "author_reward" in only_ops or enable_rewards: # print(op) vests = Amount(op['vesting_payout'], hive_instance=self.hive) hive = Amount(op['steem_payout'], hive_instance=self.hive) hbd = Amount(op['sbd_payout'], hive_instance=self.hive) if "author_reward" in only_ops: self.update(ts, vests, 0, 0, hive, hbd) if enable_rewards: self.update_rewards(ts, 0, vests, hive, hbd) return elif op['type'] == "producer_reward": vests = Amount(op['vesting_shares'], hive_instance=self.hive) self.update(ts, vests, 0, 0) return elif op['type'] == "comment_benefactor_reward": if op['benefactor'] == self.account["name"]: if "reward" in op: vests = Amount(op['reward'], hive_instance=self.hive) self.update(ts, vests, 0, 0) else: vests = Amount(op['vesting_payout'], hive_instance=self.hive) hive = Amount(op['steem_payout'], hive_instance=self.hive) hbd = Amount(op['sbd_payout'], hive_instance=self.hive) self.update(ts, vests, 0, 0, hive, hbd) return else: return elif op['type'] == "fill_convert_request": amount_in = Amount(op["amount_in"], hive_instance=self.hive) amount_out = Amount(op["amount_out"], hive_instance=self.hive) if op["owner"] == self.account["name"]: self.update(ts, 0, 0, 0, amount_out, amount_in * (-1)) return elif op['type'] == "interest": interest = Amount(op["interest"], hive_instance=self.hive) self.update(ts, 0, 0, 0, 0, interest) return elif op['type'] == "vote": if "vote" in only_ops or enable_out_votes: weight = int(op['weight']) if op["voter"] == self.account["name"]: self.update_out_vote(ts, weight) if "vote" in only_ops or enable_in_votes and op["author"] == self.account["name"]: weight = int(op['weight']) self.update_in_vote(ts, weight, op) return elif op['type'] in ['comment', 'feed_publish', 'shutdown_witness', 'account_witness_vote', 'witness_update', 'custom_json', 'limit_order_create', 'account_update', 'account_witness_proxy', 'limit_order_cancel', 'comment_options', 'delete_comment', 'interest', 'recover_account', 'pow', 'fill_convert_request', 'convert', 'request_account_recovery']: return
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}