Skip to content

Commit 0ef07ab

Browse files
authored
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
1 parent 0b97adc commit 0ef07ab

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

db/ticket.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import pendulum
55
from mongoengine import (
66
Document, ReferenceField,
7-
FloatField, DateTimeField, StringField)
7+
FloatField, DateTimeField, StringField,
8+
BooleanField, IntField)
89

910
from bot.messages import TX_ID_ERROR
1011
from db.observer import Observer
@@ -103,12 +104,15 @@ class Ticket(Document):
103104
tx_id = StringField(max_length=64, required=True)
104105
_status = ReferenceField(Status)
105106
vote_id = StringField(max_length=64)
107+
vote_block = IntField()
108+
spendable = BooleanField(default=False)
106109

107110
def __str__(self):
108111
message = f"tx {self.tx_id}\n" \
109112
f"status: {self.status}"
110113
if self.vote_id:
111114
message += f"\nvote: {self.vote_id}"
115+
message += f"\nspendable: {self.spendable}"
112116
return message
113117

114118
@property
@@ -117,6 +121,7 @@ def html(self):
117121
f"<b>status</b>: {self.status}"
118122
if self.vote_id:
119123
message += f"\n<b>vote</b>: {self.vote_link}"
124+
message += f"\n<b>spendable</b>: {self.spendable}"
120125
return message
121126

122127
def is_same_status(self, new_status_name):
@@ -162,7 +167,24 @@ def fetch(self):
162167
self.status = status
163168
if self.status == Status.voted():
164169
self.vote_id = data.get('vote')
170+
logger.debug(f"fetching vote {self}")
171+
data = request_dcr_data(f"tx/{self.vote_id}/vinfo")
172+
self.vote_block = data.get('block_validation').get('height')
165173

166174
self.save()
167175
self.notify()
168176
return True
177+
178+
def change_spendable(self, new_block):
179+
if (new_block - self.vote_block) >= 256:
180+
self.spendable = True
181+
self.save()
182+
self.notify()
183+
184+
@classmethod
185+
def voted(cls):
186+
return cls.objects.filter(_status=Status.voted())
187+
188+
@classmethod
189+
def voted_and_spendable(cls):
190+
return cls.voted().filter(spendable=True)

watchers/ticket.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
from threading import Thread
33
from time import sleep
44

5+
from utils.dcrdata import request_dcr_data
56
from bot.jack import JackBot
6-
from db.ticket import Ticket, Status
7+
from db.ticket import Ticket
78

89

910
logging.basicConfig(
1011
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
11-
level=logging.DEBUG)
12+
level=logging.INFO)
1213

1314
logger = logging.getLogger(__name__)
1415

@@ -23,8 +24,20 @@ def run(self):
2324
logger.debug(f"Fetching ticket {ticket}")
2425
ticket.fetch()
2526

26-
logger.info(f"Deleting all voted tickets...")
27-
Ticket.objects.filter(_status=Status.voted()).delete()
27+
logger.info(f"Getting latest block...")
28+
data = request_dcr_data('block/best')
29+
new_block = data.get('height')
30+
logger.info(f"Latest block is {new_block}!")
31+
32+
logger.info(f"Getting all voted tickets...")
33+
voted_tickets = Ticket.voted()
34+
logger.debug(f"Voted tickets: {tickets}")
35+
for ticket in voted_tickets:
36+
logger.debug(f"Checking if ticket {ticket} is expendable...")
37+
ticket.change_spendable(new_block)
38+
39+
logger.info(f"Deleting all voted expendable tickets...")
40+
Ticket.voted_and_spendable().delete()
2841

2942
logger.info(f"Sleeping for 1 hour...")
3043
sleep(60*60)

0 commit comments

Comments
 (0)