Exemple #1
0
 def __init__(self, name):
     self.name = name
     syslog.openlog(name, syslog.LOG_PID)
     syslog.syslog("START")
     steemd_nodes = [
         'http://steemd.pevo.science',
     ]
     self.steemd = steem.steemd.Steemd(nodes=steemd_nodes)
     self.blockchain = steem.blockchain.Blockchain(self.steemd)
     self.handlers = dict()
     self.handled = set()
 def __init__(self, name):
     self.name = name
     syslog.openlog(name, syslog.LOG_PID)
     syslog.syslog("START")
     self.handlers = dict()
     self.handled = set()
     self.config = dict()
     self.default_config = dict()
     try:
         with open("steempersist_config.json") as pjson:
             confs = json.loads(pjson.read())
             if "default" in confs and isinstance(confs["default"], dict):
                 self.default_config = confs["default"]
                 self.config = confs["default"]
             if name in confs and isinstance(confs[name], dict):
                 self.config = confs[name]
     except:
         syslog.syslog(
             "Error: unable to open or process steempersist_config.json")
     self.nodes = []
     if "nodes" in self.config:
         self.nodes = self.config["nodes"]
     else:
         if "nodes" in self.default_config:
             self.nodes = self.default_config["nodes"]
     syslog.syslog("Using nodes " + str(self.nodes))
     self.steemd = steem.steemd.Steemd(self.nodes)
     self.blockchain = steem.blockchain.Blockchain(self.steemd)
     syslog.syslog("setup done")
 def __init__(self, pbs, pname):
     self.filename = pname + ".json"
     try:
         #Read from the old JSON file if it exists.
         syslog.syslog("Fetching old state from JSON backup")
         with open(self.filename) as pjson:
             self.state = json.loads(pjson.read())
     except:
         #If it doesn't start off with a clean slate.
         syslog.syslog("No backup, setting up starting point")
         self.state = dict()
         self.state["block_no"] = pbs.get_current_block_num() - 140
         self.state["trx_no"] = 0
         self.state["op_no"] = 0
         self.state["rtime"] = time.time()
     syslog.syslog("New start, block " + str(self.state["block_no"]) + " tx " + \
                   str(self.state["trx_no"]) + " op " + str(self.state["op_no"]))
Exemple #4
0
 def vote(self, time, event):
     #Positive vote by a friend for a non comment while we are away and we don't want our voting strenth to go to waste.
     if event["voter"] in mycredentials.friends and event[
             "weight"] > 10 and event["permlink"][:3] != "re-" and must_vote(
                 mycredentials.account, 9700):
         #We need to use our posting key to upvote.
         stm = steem.Steem([], keys=mycredentials.keys)
         #The permlink of the spambot post we wish to downvote.
         postlink = "@" + event["author"] + "/" + event["permlink"]
         syslog.syslog("Upvoting upvoted comment by friend:" +
                       event["voter"] + "  :  " + postlink)
         try:
             #Try to downvote the upvoted comment spambot response that just got upvoted.
             stm.vote(postlink, event["weight"] * 1.0 / 100,
                      mycredentials.account)
             syslog.syslog("OK")
         except:
             syslog.syslog("FAIL")
def stream_blockchain_events(sbs, handled, pname):
    global p
    unhandled = set()
    #Run as long as we get 'expected' exceptions from BlockChain::stream_from main loop.
    while True:
        try:
            #Start of with a new JSON state sync object.
            p = Progress(sbs, pname)
            skipfirst = p.state["trx_no"]
            first = True
            block_no = -1
            trx_no = -1
            op_no = -1
            #A main loop that throws exceptions on a regular basis.
            #Start at the last block from the previous time this loop was run.
            syslog.syslog("Entering main loop.")
            for entry in sbs.stream_from(p.state["block_no"]):
                if first:
                    syslog.syslog("main loop started")
                block_no = entry["block"]
                trx_no = entry["trx_in_block"]
                op_no = entry["op_in_trx"]
                lop = entry["op"]
                if first is False and trx_no == 0 and op_no == 0:
                    skipfirst = -1
                first = False
                event_time = dateutil.parser.parse(entry["timestamp"])
                #Skip already processed transactions from block if needed.
                if skipfirst >= trx_no:
                    pass
                else:
                    sync = False
                    if lop[0] in handled:
                        yield [lop[0], event_time, lop[1]]
                        sync = True
                    else:
                        if not lop[0] in unhandled:
                            unhandled.add(lop[0])
                            syslog.syslog("Unhandled event type: " + lop[0])
                        if "other" in handled:
                            yield [
                                "other", event_time, {
                                    "type": lop[0],
                                    "event": lop[1]
                                }
                            ]
                    #Inform the Progress object of our latest progress.
                    if p(block_no, trx_no, op_no, sync):
                        #If our progress resulted in an explicit or implicit SYNC,
                        #do some extra stuff.
                        #Log how far are we behind on the HEAD of the blockchain?
                        behind = datetime.datetime.utcnow() - \
                                 dateutil.parser.parse(entry["timestamp"])
                        if op_no == 0 and trx_no == 0:
                            syslog.syslog("[block " + str(block_no) + " tx " + \
                                      str(trx_no) + "op" + str(op_no) +"] behind " + \
                                      str(behind))
                now = time.time()
                if "hour" in handled:
                    if (now - p.state["rtime"]) >= 3600:
                        # one hour (or more) passed.
                        p.state["rtime"] = now
                        yield ["hour", event_time, {}]
                        p.sync()
                else:
                    if "day" in handled:
                        if (now - p.state["rtime"]) >= 86400:
                            # one day (or more) passed.
                            p.state["rtime"] = now
                            yield ["day", event_time, {}]
                            p.sync()

        #Some expected exceptions from Blockchain::stream_from
        except TypeError as e:
            syslog.syslog("ERR " + str(e) + " " + str(block_no) + " " +
                          str(trx_no))
            p.sync()
        except AttributeError as e:
            syslog.syslog("ERR " + str(e) + " " + str(block_no) + " " +
                          str(trx_no))
            p.sync()
 def __call__(self):
     syslog.syslog("Run started")
     if len(self.handled) > 0:
         for r in stream_blockchain_events(self.blockchain, self.handled,
                                           self.name):
             self.handlers[r[0]](r[1], r[2])