def profiling(name_list): stm = DPay() set_shared_dpay_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_stream2(self): bts = self.bts b = Blockchain(dpay_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 stream_votes(stm, threading, thread_num): b = Blockchain(dpay_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_stream_threading(self): bts = self.bts b = Blockchain(dpay_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 test_stream_threading2(self): bts = self.bts b = Blockchain(dpay_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, threading=True, 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_stream_batch(self): bts = self.bts b = Blockchain(dpay_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"])
from __future__ import print_function import sys from datetime import timedelta import time import io from dpaycli.blockchain import Blockchain from dpaycli.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)
authorperm = construct_authorperm(vote_event["author"], vote_event["permlink"]) # print(authorperm) try: process_vote_content(Comment(authorperm)) except exceptions.ContentDoesNotExistsException: print("Could not find Comment: %s" % (authorperm)) al = list() if not vote_event["voter"] in self.looked_up: al.append(vote_event["voter"]) self.looked_up.add(vote_event["voter"]) if not vote_event["author"] in self.looked_up: al.append(vote_event["author"]) self.looked_up.add(vote_event["author"]) if len(al) > 0: lookup_accounts(al) if __name__ == "__main__": wtw = WatchingTheWatchers() tb = WatchingTheWatchersBot(wtw) blockchain = Blockchain() threading = True thread_num = 16 cur_block = blockchain.get_current_block() stop = cur_block.identifier startdate = cur_block.time() - timedelta(days=1) start = blockchain.get_estimated_block_num(startdate, accurate=True) for vote in blockchain.stream(opNames=["vote"], start=start, stop=stop, threading=threading, thread_num=thread_num): tb.vote(vote) wtw.report()
import sys from datetime import timedelta import time import io from dpaycli.blockchain import Blockchain from dpaycli.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)
def test_stream(self): bts = self.bts start = self.start stop = self.stop b = Blockchain(dpay_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)
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() for block in s_load_binary(gzip.open('blocks1.pkl')): print(block)