From 0ef07abdcc692961e6c23ee9e76efaa88b9f474f Mon Sep 17 00:00:00 2001 From: Rodrigo Castro Date: Sat, 22 Feb 2020 11:58:26 -0300 Subject: [PATCH] TicketWatcher now notify spendable (#49) * vote_block and spendable on Ticket * if Ticket voted request vote info * watcher check all voted_tickets before deleting voted and spendable tickets --- db/ticket.py | 24 +++++++++++++++++++++++- watchers/ticket.py | 21 +++++++++++++++++---- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/db/ticket.py b/db/ticket.py index f4bff6f..16758a7 100644 --- a/db/ticket.py +++ b/db/ticket.py @@ -4,7 +4,8 @@ import pendulum from mongoengine import ( Document, ReferenceField, - FloatField, DateTimeField, StringField) + FloatField, DateTimeField, StringField, + BooleanField, IntField) from bot.messages import TX_ID_ERROR from db.observer import Observer @@ -103,12 +104,15 @@ class Ticket(Document): tx_id = StringField(max_length=64, required=True) _status = ReferenceField(Status) vote_id = StringField(max_length=64) + vote_block = IntField() + spendable = BooleanField(default=False) def __str__(self): message = f"tx {self.tx_id}\n" \ f"status: {self.status}" if self.vote_id: message += f"\nvote: {self.vote_id}" + message += f"\nspendable: {self.spendable}" return message @property @@ -117,6 +121,7 @@ def html(self): f"status: {self.status}" if self.vote_id: message += f"\nvote: {self.vote_link}" + message += f"\nspendable: {self.spendable}" return message def is_same_status(self, new_status_name): @@ -162,7 +167,24 @@ def fetch(self): self.status = status if self.status == Status.voted(): self.vote_id = data.get('vote') + logger.debug(f"fetching vote {self}") + data = request_dcr_data(f"tx/{self.vote_id}/vinfo") + self.vote_block = data.get('block_validation').get('height') self.save() self.notify() return True + + def change_spendable(self, new_block): + if (new_block - self.vote_block) >= 256: + self.spendable = True + self.save() + self.notify() + + @classmethod + def voted(cls): + return cls.objects.filter(_status=Status.voted()) + + @classmethod + def voted_and_spendable(cls): + return cls.voted().filter(spendable=True) diff --git a/watchers/ticket.py b/watchers/ticket.py index 69a4ee5..bf007d5 100644 --- a/watchers/ticket.py +++ b/watchers/ticket.py @@ -2,13 +2,14 @@ from threading import Thread from time import sleep +from utils.dcrdata import request_dcr_data from bot.jack import JackBot -from db.ticket import Ticket, Status +from db.ticket import Ticket logging.basicConfig( format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', - level=logging.DEBUG) + level=logging.INFO) logger = logging.getLogger(__name__) @@ -23,8 +24,20 @@ def run(self): logger.debug(f"Fetching ticket {ticket}") ticket.fetch() - logger.info(f"Deleting all voted tickets...") - Ticket.objects.filter(_status=Status.voted()).delete() + logger.info(f"Getting latest block...") + data = request_dcr_data('block/best') + new_block = data.get('height') + logger.info(f"Latest block is {new_block}!") + + logger.info(f"Getting all voted tickets...") + voted_tickets = Ticket.voted() + logger.debug(f"Voted tickets: {tickets}") + for ticket in voted_tickets: + logger.debug(f"Checking if ticket {ticket} is expendable...") + ticket.change_spendable(new_block) + + logger.info(f"Deleting all voted expendable tickets...") + Ticket.voted_and_spendable().delete() logger.info(f"Sleeping for 1 hour...") sleep(60*60)