Skip to content
Snippets Groups Projects
Commit 82267483 authored by Holger Nahrstaedt's avatar Holger Nahrstaedt
Browse files

New example and fix unit test

parent 13225ce2
Branches
Tags
No related merge requests found
......@@ -102,10 +102,7 @@ class Blockchain(object):
last_block = self.get_current_block()
time_diff = last_block.time() - date
block_number = math.floor(last_block.identifier - time_diff.total_seconds() / block_time_seconds)
if block_number > last_block.identifier:
return last_block.identifier
else:
block_number
return block_number
def block_time(self, block_num):
""" Returns a datetime of the block with the given block
......
from __future__ import print_function
import sys
sys.path.append('../')
from datetime import timedelta
import time
import io
from beem.notify import Notify
from beem.blockchain import Blockchain
from beem.utils import parse_time
import logging
log = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
class TestBot:
def __init__(self):
self.notify = None
self.blocks = 0
self.hourcount = 0
self.start = time.time()
self.last = time.time()
def new_block(self,block):
print(block["timestamp"])
timestamp = parse_time(block["timestamp"])
chunk = 10
self.blocks = self.blocks + 1
if self.blocks % chunk == 0:
self.notify.close()
now = time.time()
duration = now - self.last
total_duration = now - self.start
speed = int(chunk*1000.0/duration)*1.0/1000
avspeed = int(self.blocks*1000/total_duration)*1.0/1000
self.last = now
def hour(self):
self.hourcount = self.hourcount + 1
now = time.time()
total_duration = str(timedelta(seconds=now-self.start))
print("* HOUR mark: Processed "+str(self.hourcount)+ " blockchain hours in "+ total_duration)
if self.hourcount == 1*24:
print("Ending eventloop")
self.notify.close()
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 = TestBot()
notify = Notify(on_block=tb.new_block)
tb.notify = notify
notify.listen()
\ No newline at end of file
tb = DemoBot()
blockchain = Blockchain()
for vote in blockchain.stream(opNames=["vote"]):
tb.vote(vote)
......@@ -4,11 +4,12 @@ from __future__ import print_function
from __future__ import unicode_literals
from builtins import super
import unittest
from datetime import datetime, timedelta
import pytz
from pprint import pprint
from beem import Steem
from beem.blockchain import Blockchain
from beem.block import Block
from datetime import datetime
from beem.instance import set_shared_steem_instance
wif = "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3"
......@@ -40,3 +41,13 @@ class Testcases(unittest.TestCase):
block = b.get_current_block()
self.assertTrue(isinstance(block, Block))
self.assertEqual(num, block.identifier)
def test_estimate_block_num(self):
bts = self.bts
b = Blockchain(steem_instance=bts)
last_block = b.get_current_block()
num = last_block.identifier
now = last_block.time()
date = now - timedelta(seconds=60 * 3)
est_block_num = b.get_estimated_block_num(date)
self.assertEqual(est_block_num, num - 60)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment