-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathshare_monitor.py
executable file
·96 lines (80 loc) · 3.5 KB
/
share_monitor.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
#!/usr/bin/env python2
#
# Copyright (C) 2011 Patrick "p2k" Schneider <patrick.p2k.schneider@gmail.com>
#
# This file is part of ecoinpool.
#
# ecoinpool 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.
#
# ecoinpool 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 ecoinpool. If not, see <http://www.gnu.org/licenses/>.
#
# -- Configure here: --
SERVER_URL = "http://ecoinpool.p2k-network.org:5984/"
SHARES_DB = "sc-livetest3"
CONFIG_DB = "ecoinpool"
# ---------------------
from couchdb.client import Server
class ShareMonitor(object):
def __init__(self, server_url, shares_db, config_db):
self.server = Server(server_url)
self.shares_db = self.server[shares_db]
self.config_db = self.server[config_db]
self.worker_dict = {}
def getWorker(self, worker_id):
worker = self.worker_dict.get(worker_id)
if worker is None:
worker = self.config_db.get(worker_id)
self.worker_dict[worker_id] = worker
return worker
def run(self):
update_seq = self.shares_db.info()["update_seq"]
current_block = None
print "* Share Monitor activated *"
while True:
update_seq, current_block = self._run(update_seq, current_block)
def _run(self, update_seq, current_block):
for row in self.shares_db.changes(feed="continuous", since=update_seq, include_docs=True):
if not row.has_key("id"):
if row.has_key("last_seq"):
update_seq = row["last_seq"]
else:
print "?", row
continue
doc = row["doc"]
if not doc.has_key("state"):
continue
if doc.has_key("block_num") and current_block != doc["block_num"]:
print "Current block: %d" % doc["block_num"]
current_block = doc["block_num"]
worker = self.getWorker(doc["worker_id"])
text = None
if doc["state"] == "valid":
text = "+ Valid share from %s/%s."
elif doc["state"] == "candidate":
text = "+++ Candidate share from %s/%s! +++"
elif doc["state"] == "invalid":
if doc["reject_reason"] == "stale":
text = "- Stale share from %s/%s!"
elif doc["reject_reason"] == "duplicate":
text = "- Duplicate work from %s/%s!"
elif doc["reject_reason"] == "target":
text = "- Invalid hash from %s/%s!"
elif doc["reject_reason"] == "data":
text = "- Wrong data from %s/%s!"
if text is not None:
timestamp = "%d-%02d-%02d %02d:%02d:%02d" % tuple(doc["timestamp"])
print ("[%s] " + text) % (timestamp, worker["name"], doc["ip"])
return update_seq, current_block
if __name__ == "__main__":
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)
ShareMonitor(SERVER_URL, SHARES_DB, CONFIG_DB).run()