2
2
import yaml
3
3
import time
4
4
import sys
5
- from datetime import timedelta
5
+ import datetime
6
6
from CodaClient import Client
7
7
8
8
9
+ def write_to_log (log_str : str , print_mode : str = "info" ):
10
+ now = datetime .datetime .now ()
11
+ if print_mode == "warning" :
12
+ logger .warning (log_str )
13
+ else :
14
+ logger .info (log_str )
15
+
16
+ with open ("snark_stopper.log" , "a" ) as log_f :
17
+ log_f .write (f'{ now .strftime ("%Y-%m-%d %H:%M:%S" )} | { log_str } \n ' )
18
+
19
+
9
20
def worker_manager (mode : str ):
10
21
data = None
11
22
if mode == "on" :
@@ -18,12 +29,16 @@ def worker_manager(mode: str):
18
29
status_worker_fee = daemon_status ["daemonStatus" ]["snarkWorkFee" ]
19
30
20
31
if status_worker_pub is None or int (status_worker_fee ) != WORKER_FEE :
21
- logger .info (f'Start worker: FEE { WORKER_FEE / 1e9 } MINA' )
32
+ write_to_log (f'Start worker: FEE { WORKER_FEE / 1e9 } MINA' )
33
+ worker_address = coda .get_daemon_status ()["daemonStatus" ]["snarkWorker" ]
34
+ write_to_log (f'Worker status check. worker_pub_key = { worker_address } ' )
22
35
data = coda .set_current_snark_worker (WORKER_PUB_KEY , WORKER_FEE )
23
36
24
37
elif mode == "off" :
25
- logger . info ("Turn off worker" )
38
+ write_to_log ("Turn off worker" )
26
39
data = coda .set_current_snark_worker (None , 0 )
40
+ worker_address = coda .get_daemon_status ()["daemonStatus" ]["snarkWorker" ]
41
+ write_to_log (f'Worker status check. worker_pub_key = { worker_address } ' )
27
42
return data
28
43
29
44
@@ -38,14 +53,14 @@ def parse_next_proposal_time():
38
53
logger .fatal (f'😿 Node is not synced yet. STATUS: { sync_status } | Height: { current_height } \\ { max_height } ' )
39
54
40
55
if "startTime" not in str (daemon_status ["daemonStatus" ]["nextBlockProduction" ]):
41
- worker_manager ("on" )
56
+ worker_manager (mode = "on" )
42
57
next_propos = "🙀 No proposal in this epoch"
43
58
else :
44
59
next_propos = int (daemon_status ["daemonStatus" ]["nextBlockProduction" ]["times" ][0 ]["startTime" ]) / 1000
45
60
return next_propos
46
61
47
62
except Exception as parseProposalErr :
48
- logger . exception (f'😱 parse_next_proposal_time Exception: { parseProposalErr } ' )
63
+ write_to_log (f'😱 parse_next_proposal_time Exception: { parseProposalErr } ' , print_mode = "warning" )
49
64
return f'parse_next_proposal_time() Exception: { parseProposalErr } '
50
65
51
66
@@ -66,15 +81,15 @@ def parse_worker_pubkey():
66
81
return BLOCK_PROD_KEY
67
82
68
83
else :
69
- logger . fatal ('😡 Enter the worker public key in config.yml' )
84
+ write_to_log ('😡 Enter the worker public key in config.yml' , print_mode = "warning" )
70
85
exit (1 )
71
86
72
87
73
88
# Configure Logging
74
89
logging .basicConfig (stream = sys .stdout , level = logging .INFO , format = '|%(asctime)s| %(message)s' )
75
90
logger = logging .getLogger (__name__ )
76
91
c = yaml .load (open ('config.yml' , encoding = 'utf8' ), Loader = yaml .SafeLoader )
77
- print ("version 1.2.4 " )
92
+ print ("version 1.2.5 " )
78
93
79
94
WORKER_PUB_KEY = str (c ["WORKER_PUB_KEY" ])
80
95
WORKER_FEE = float (c ["WORKER_FEE" ])
@@ -89,43 +104,41 @@ def parse_worker_pubkey():
89
104
try :
90
105
coda = Client (graphql_host = GRAPHQL_HOST , graphql_port = GRAPHQL_PORT )
91
106
WORKER_PUB_KEY = parse_worker_pubkey ()
107
+ write_to_log ("Snark-stopper launched" )
92
108
93
109
except :
94
- logger . fatal (f'😿 Can\' t connect to graphql { GRAPHQL_HOST } :{ GRAPHQL_PORT } .\n '
95
- f'Check troubleshooting manual - https://github.com/c29r3/mina-snark-stopper#troubleshooting' )
110
+ write_to_log (f'😿 Can\' t connect to graphql { GRAPHQL_HOST } :{ GRAPHQL_PORT } . '
111
+ f'Check troubleshooting manual - https://github.com/c29r3/mina-snark-stopper#troubleshooting' , print_mode = "warning" )
96
112
exit (1 )
97
113
98
-
99
114
print (f'Worker public key: { WORKER_PUB_KEY } \n '
100
115
f'Worker fee: { WORKER_FEE } \n '
101
116
f'Check period(sec): { CHECK_PERIOD_SEC } \n '
102
- f'Stop before(min): { STOP_WORKER_BEFORE_MIN } \n ' )
117
+ f'Stop before(min): { STOP_WORKER_BEFORE_MIN } \n '
118
+ f'https://github.com/c29r3/mina-snark-stopper\n '
119
+ f'http://staketab.com/\n ' )
103
120
104
121
while True :
105
122
try :
106
123
next_proposal = parse_next_proposal_time ()
107
124
while type (next_proposal ) is str :
108
- logger . info (next_proposal )
125
+ write_to_log (next_proposal )
109
126
time .sleep (CHECK_PERIOD_SEC )
110
127
next_proposal = parse_next_proposal_time ()
111
128
112
- time_to_wait = str (timedelta (seconds = int (next_proposal - time .time ())))
113
- logger .info (f'🕐 Next proposal via { time_to_wait } ' )
114
- if next_proposal - time .time () < STOP_WORKER_BEFORE_MIN * 60 :
115
- worker_off = worker_manager (mode = "off" )
116
- logger .info (worker_off )
117
-
129
+ time_to_wait = str (datetime .timedelta (seconds = int (next_proposal - time .time ())))
130
+ write_to_log (f'🕐 Next proposal via { time_to_wait } ' )
131
+ if next_proposal - time .time () <= STOP_WORKER_BEFORE_MIN * 60 :
132
+ worker_manager (mode = "off" )
118
133
logger .info (f'⏰ Waiting { STOP_WORKER_FOR_MIN } minutes' )
119
134
time .sleep (60 * STOP_WORKER_FOR_MIN )
120
-
121
- worker_on = worker_manager (mode = "on" )
122
- logger .info (worker_on )
135
+ worker_manager (mode = "on" )
123
136
124
137
else :
125
- worker_manager ("on" )
138
+ worker_manager (mode = "on" )
126
139
127
140
time .sleep (CHECK_PERIOD_SEC )
128
141
129
142
except (TypeError , Exception ) as parseErr :
130
- logger . exception (f'🤷♂️ Parse error: { parseErr } ' )
143
+ write_to_log (f'🤷♂️ Parse error: { parseErr } ' , print_mode = "warning" )
131
144
time .sleep (10 )
0 commit comments