-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathminer.py
266 lines (222 loc) · 8.45 KB
/
miner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# -*- coding: utf-8 -*-
# Zilliqa Mining Proxy
# Copyright (C) 2019 Gully Chen
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from collections import defaultdict
from datetime import datetime, timedelta
import mongoengine as mg
from mongoengine import Q
from .basemodel import ModelMixin
"""
A Miner -> Many Workers
A Worker -> Time series of hashrate
"""
class Miner(ModelMixin, mg.Document):
meta = {"collection": "zil_miners"}
wallet_address = mg.StringField(max_length=128, required=True, unique=True)
rewards = mg.FloatField(default=0.0)
paid = mg.FloatField(default=0.0)
authorized = mg.BooleanField(default=True)
nick_name = mg.StringField(max_length=64, default="")
email = mg.StringField(max_length=128)
email_verified = mg.BooleanField(default=False)
join_date = mg.DateTimeField()
workers_name = mg.ListField()
# stats
work_submitted = mg.IntField(default=0)
work_failed = mg.IntField(default=0)
work_finished = mg.IntField(default=0)
work_verified = mg.IntField(default=1)
total_paid = mg.IntField(default=1)
last_paid = mg.IntField(default=1)
bonus_paid = mg.IntField(default=1)
def __str__(self):
return f"[Miner: {self.wallet_address}, {self.authorized}]"
@classmethod
def get_or_create(cls, wallet_address: str, worker_name: str,
nick_name="", email="", authorized=True):
worker = Worker.get_or_create(wallet_address, worker_name)
if worker:
miner = cls.objects(
wallet_address=wallet_address
).modify(
upsert=True, new=True,
set__wallet_address=wallet_address,
set__authorized=authorized,
set__nick_name=nick_name,
set__email=email
)
if miner.join_date is None:
miner.join_date = datetime.utcnow()
if worker_name not in miner.workers_name:
miner.workers_name.append(worker_name)
return miner.save()
return None
@property
def workers(self):
return Worker.get_all(wallet_address=self.wallet_address)
def works_stats(self):
return {
"work_submitted": self.work_submitted,
"work_failed": self.work_failed,
"work_finished": self.work_finished,
"work_verified": self.work_verified,
"total_paid": self.total_paid,
"last_paid": self.last_paid,
"bonus_paid": self.bonus_paid,
}
def update_stat(self, inc_submitted=0, inc_failed=0, inc_finished=0, inc_verified=0, inc_paid=0, inc_last=0,inc_bonus=0):
update_kwargs = {
"inc__work_submitted": inc_submitted,
"inc__work_failed": inc_failed,
"inc__work_finished": inc_finished,
"inc__work_verified": inc_verified,
"inc__total_paid": inc_paid,
"inc__last_paid": inc_last,
"inc__bonus_paid": inc_bonus
}
update_kwargs = {key: value for (key, value) in update_kwargs.items() if value > 0}
return self.update(**update_kwargs)
class Worker(ModelMixin, mg.Document):
meta = {"collection": "zil_mine_workers"}
wallet_address = mg.StringField(max_length=128, required=True)
worker_name = mg.StringField(max_length=64, default="")
work_submitted = mg.IntField(default=0)
work_failed = mg.IntField(default=0)
work_finished = mg.IntField(default=0)
work_verified = mg.IntField(default=1)
total_paid = mg.IntField(default=1)
last_paid = mg.IntField(default=1)
bonus_paid = mg.IntField(default=1)
def __str__(self):
return f"[Worker: {self.worker_name}.{self.wallet_address}]"
@property
def miner(self):
return Miner.get_one(wallet_address=self.wallet_address)
@classmethod
def get_or_create(cls, wallet_address: str, worker_name: str):
worker = cls.objects(
wallet_address=wallet_address,
worker_name=worker_name
).modify(
upsert=True, new=True,
set__wallet_address=wallet_address,
set__worker_name=worker_name
)
return worker
@classmethod
def active_count(cls):
three_hours = datetime.utcnow() - timedelta(hours=3)
match = {
"updated_time": {
"$gte": three_hours,
}
}
group = {
"_id": {"wallet_address": "$wallet_address",
"worker_name": "$worker_name"},
}
return HashRate.aggregate_count(match, group)
def update_stat(self, inc_submitted=0, inc_failed=0, inc_finished=0, inc_verified=0, inc_paid=0, inc_last=0, inc_bonus=0):
update_kwargs = {
"inc__work_submitted": inc_submitted,
"inc__work_failed": inc_failed,
"inc__work_finished": inc_finished,
"inc__work_verified": inc_verified,
"inc__total_paid": inc_paid,
"inc__last_paid": inc_last,
"inc__bonus_paid": inc_bonus,
}
update_kwargs = {key: value for (key, value) in update_kwargs.items() if value > 0}
if self.update(**update_kwargs):
# update miner's stats
self.miner.update_stat(
inc_submitted=inc_submitted,
inc_failed=inc_failed,
inc_finished=inc_finished,
inc_verified=inc_verified,
inc_paid=inc_paid,
inc_last=inc_last,
inc_bonus=inc_bonus
)
def works_stats(self):
return {
"work_submitted": self.work_submitted,
"work_failed": self.work_failed,
"work_finished": self.work_finished,
"work_verified": self.work_verified,
"total_paid": self.total_paid,
"last_paid": self.last_paid,
"bonus_paid": self.bonus_paid,
}
class HashRate(ModelMixin, mg.Document):
meta = {"collection": "zil_mine_hashrate"}
wallet_address = mg.StringField(max_length=128, required=True)
worker_name = mg.StringField(max_length=64, default="")
hashrate = mg.IntField(default=0.0, required=True)
updated_time = mg.DateTimeField()
@classmethod
def log(cls, hashrate: int, wallet_address: str, worker_name: str):
if hashrate < 0:
return False
_miner = Miner.get(wallet_address=wallet_address)
if not _miner:
return False
_worker = Worker.get_or_create(wallet_address, worker_name)
if not _worker:
return False
hr = cls(wallet_address=wallet_address, worker_name=worker_name,
hashrate=hashrate, updated_time=datetime.utcnow())
return hr.save()
@classmethod
def epoch_hashrate(cls, block_num=None, wallet_address=None, worker_name=None):
from .pow import PoWWindow
pow_start, pow_end = PoWWindow.get_pow_window(block_num)
if not pow_start or not pow_end:
return 0
match = {
"updated_time": {
"$gte": pow_start,
"$lte": pow_end,
}
}
if wallet_address is not None:
match.update({
"wallet_address": {
"$eq": wallet_address,
}
})
if worker_name is not None:
match.update({
"worker_name": {
"$eq": worker_name,
}
})
group = {
"_id": {"wallet_address": "$wallet_address",
"worker_name": "$worker_name", },
"hashrate": {"$max": "$hashrate"}
}
group_sum = {
"_id": None,
"hashrate": {"$sum": "$hashrate"}
}
pipeline = [
{"$match": match},
{"$group": group},
{"$group": group_sum}
]
res = list(cls.objects.aggregate(*pipeline))
return res[0]["hashrate"] if res else 0