Skip to content

Commit

Permalink
TicketWatcher now notify spendable (#49)
Browse files Browse the repository at this point in the history
* vote_block and spendable on Ticket
* if Ticket voted request vote info
* watcher check all voted_tickets before deleting voted and spendable tickets
  • Loading branch information
rodrigondec authored Feb 22, 2020
1 parent 0b97adc commit 0ef07ab
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
24 changes: 23 additions & 1 deletion db/ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -117,6 +121,7 @@ def html(self):
f"<b>status</b>: {self.status}"
if self.vote_id:
message += f"\n<b>vote</b>: {self.vote_link}"
message += f"\n<b>spendable</b>: {self.spendable}"
return message

def is_same_status(self, new_status_name):
Expand Down Expand Up @@ -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)
21 changes: 17 additions & 4 deletions watchers/ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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)
Expand Down

0 comments on commit 0ef07ab

Please sign in to comment.