-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQtumMon 09-25-2018.py
1476 lines (1174 loc) · 60.6 KB
/
QtumMon 09-25-2018.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
version = "09/25/2018"
'''
QtumMon.py (QM)
Copyright (c) 2018 Jackson Belove
Beta software, use at your own risk
MIT License, free, open software for the Qtum Community
A program to monitor the Qtumd application, send emails and log activity.
QM uses qtum-cli to send RPC queries to the qtumd server application
to identify staking events, and log various activities of the application.
QM sends a query to check for a new block approximately every 4 seconds.
QM will require qtumd to be running and staking enabled (decrypted for
staking), or will stay in an error loop until these two conditions are met.
To run with Python 2.7 edit all the print statements marked by "#PY3" and "#PY2"
(about 30 lines). To run in Python 2.7 comment out all the print functions
marked "#PY3" and uncomment out all the adjacent print statements marked "#PY2".
A note on the terminology for "stake" and "staking". Strictly speaking, the
"stake" is the quantity of QTUM that your wallet selects to "send" to the
blockchain when your wallet is randomly chosen to receive a block
reward. Your wallet will choose one or more UTXOs (previous discrete transactions
received by the wallet) to commit to the stake. This amount of QTUM is "staked"
for 501 blocks, and is subtracted from your wallet's ongoing
staking weight for the 501 block interval. At the end of 501 blocks, the stake
amount is reenabled, and the stake ends.
The wallet can be "staking" and decrypted for "staking", but there is only a
"stake" defined during the block reward period. Just because the wallet is
staking doesn't mean it currently has a stake. In the code and comments below,
"stake" refers to the quantity of QTUM "staked" during the block reward period,
and "staking" refers to the capability of the wallet to be chosen for
a block reward.
Any examples that reference Skynet are idential for Mainnet Ignition.
To Add
Adjust to a whole second in delayBlockWaiting
Have send_email() return a string of the actual email sent (or not if during
a do not disturb period) so that the display is correct
Better recovery for loss of network connection (alerts on low connections now)
Revisions
09/25/2018 Removed new network weight
09/20/2018 Changed getinfo to -getinfo for v0.16.01
05/28/2018 Fixed dataLen bug
01/29/2018 Fixed sendmail, comment cleanup, condensed display
01/28/2018 Removed alert on missed block
12/25/2017 Added New Network Weight
12/23/2017 Removed unix time from display
10/27/2017 commented out immature
10/23/2017 adding secondsMovingAverage to catch 16 second cycle
10/13/2017 adding timing sequence string, for occasional missed block
10/12/2017 timer() based pacing for new block wating loop and Day/Hour waiting loop
10/09/2017 Bug fix, detect new stake
10/05/2017 Queued messages during doNotDisturb times
10/03/2017 Changed waiting loop to 3..6 seconds variable
09/30/2017 Added comments to run in Python 2 or 3
09/29/2017 Added doNotDisturb[], removed Windows functions
09/28/2017 Print formatting with commas
09/27/2017 Read config file on startup
09/27/2017 Adding check for staking turned on
09/26/2017 Using getblocknumber every 6 seconds for pacing
09/26/2017 Added functions for parsing numbers and letters
09/25/2017 Added multi (overlapping) reward alerts
09/24/2017 Adding logging .csv file to current directory
09/22/2017 All new (i.e., sections copied from my previous scripts)
Block reward sequence from Skynet (testnet) on September 22, 2017
Note the two alert points (when an email would be sent)
Overlapping reward periods would show up as a stake increase
while there was already a stake in place. In the example below,
if the stake went from 307.6 to 724.8 (not shown), this will generate
a "MULTI" alert.
Stake Block Relative
0 26476
0 26477
307.6 26478 1 Alert REWARD, 1st 0.4 placeholder payment received
307.6 26479 2
<snip>
307.6 26976 499
307.6 26977 500
0 26978 501 Alert RETURNED, a stake has been returned
0.4 26979 502 Additional payments received
0.8 26980 503
1.2 26981 504
1.6 26982 505
2.0 26983 506
2.4 26984 507
2.8 26985 508
3.2 26986 509
3.6 26987 510 All payments collected, but need to mature
3.6 26988 511
<snip>
3.6 27478 1001
3.2 27479 1002
2.8 27480 1003
2.4 27481 1004
2.0 27483 1005
1.6 27484 1006
1.2 27485 1007
0.8 27486 1008
0.4 27487 1009
0.0 27488 1010 All payments matured
Logging
A log is written as comma separated values (.csv) for easy importing into Excel.
The log filename is changed daily at 0000 UTC, and has the name QM_Log_DD_MMM_YYYY.csv.
The first column of the log file has these reference numbers:
000 Startup and system messages
100 New block received
200 End of the day
300
400 Block reward, staking actions
500
600
700
800 Missed block (commented out)
900 Errors
Example
000,Program,start,or,restart,version,10/09/2017
000,Logging,start,_0247,hours,GMT,11_Oct2017
000,unix time, date time, balance, stake, block, my weight, net weight, connections, staking, expectedHours, secondsLastBlock
100,1507690092,2017 10 11 10:48:12,000000.2,000000.0,24577, 00000.0,006918989,000000.2,8, yes ,0.0,47
100,1507690214,2017 10 11 10:50:14,000000.2,000000.0,24578, 00000.0,006955841,000000.2,8, yes ,0.0,121
100,1507690230,2017 10 11 19:50:30,000000.2,000000.0,24579, 00000.0,006992693,000000.2,8, yes ,0.0,15
Path and folders
qtumd and QM do not require any path setup. These files should be located
in the same directory/folder, and both qtumd and QM run from that directory.
QM will write the log files to that same directory.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Program Summary
function send_email()
send if doNotDisturb = False, send queued messages
else queue the message to send later
fuctions parse_number(), parse_alphanum() and parse_logical()
read configuration file
Initialize log file
make sure qtumd is running
if not, send alert and wait for qtumd to start
main program loop
qtum-cli -getinfo
qtum-cli getstakinginfo
detect new block reward - single or overlapping (multi)
make sure qtumd is staking
if not, send alert and wait for staking to be enabled
format all the data
send out alert if found:
new block reward - single
new block reward - multiple
stake returned
blocks getting stale (no new block in 30 minutes)
send an hourly status email
send queued messages if noNotDisturb == False
for a new block
log it
check for a skipped block (commented out)
wait here, 4 seconds from last block
new block waiting loop
qtum-cli -getinfo
if new block, exit new block waiting loop
Check for stale blocks
Date and Hours waiting loop, after a new block wait 4, 8, 12... seconds
wait 0.3 second
check the time, if a new day (UTC)
open new log file
check the time, if xx:59:56 set to do hourly email and exit new block waiting loop
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
'''
oldStake = 0 # the previous stake
oldBlock = 0 # the block last time it changed
stake = 0 # for startup
sendOneTimeQtumdOffline = False # send an alert once if qtumd is offline
qtumdIsRunning = False # set this on startup
labelsPrintedAtStartup = False # print the label row once at startup
logNewBlock = False # set to log new block
blocksToday = 0 # new blocks today, accurate if QM running 24 hours+
savedUnixBlockTime = 0 # for time since last block
sendOneTimeNotStaking = False # will check below if staking
sendEmailForNewHour = False # send a status email for new hour
secondsSinceLastBlock = 0 # for logging
didNewHour = False # switch to allow a few seconds to detect the hour change
didNewLog = False # did we just detect a UTC day change and opened a new log file
sendLowConnectionsAlert = False # send an alert for low connections
lowConnectionsAging = 0 # age the low connections alert to only send again after 7 blocks
staleBlockAging = 0 # age the stale block alert to only send again after 15 minutes
config_file_name = "qtumMonConfig.txt" # name of configuration file
firstTimeThrough = True # the first time through, do/don't do certain things
minimumStakeSize = 99 # value of minimum UTXO size, used to detect stakes
delayBlockWaiting = 3 # set on 10/14/2017, delay in seconds for the block waiting loop
# this parameter is critical for setting the delay until the next
# block check immediately after a new block is found
delayDateHours = 3.36 # set to 3.86 on 10/13/2017, 4 second delay,delay in seconds for the loop that detects date and hour change
# set to 3.36 on 10/15/2017 because still missing 4 second blocks
loopSeq = "" # log timing of loops, if miss block
savedLoopSeq = "" # for logging
oldestMAValue = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] # holds old moving average seconds
secondsMovingAverage = 0.0 # just something for first printout
MAcount= 0 # counter for 0..9 to calculate seconds moving average
firstBlockFound = True # we are going to find the first block
currentSecondsMod16 = 0.0 # for startup
printBlockMod10 = True # toggle to print the labels every 10 blocks
trueNetworkWeight = 15000000 # estimate
dDiff = trueNetworkWeight / 5.86 # slope from chart of simulated results, uniform wallets
startingDifficulty = dDiff # starting value for the four 121 block moving averages
# 30 million network weight 5693691
# 20 million network weight 3409395, 3900000 128 seconds
# 15 million 2824920
# 3.86 million 679000
# labels for the display and log
# 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
labelRow = " time | balance | stake | block | my weight| net weight | con | stking |exp hrs|secLast| offst| ave"
logLabels = '000,unix time, date time, balance, stake, block, weight, net weight, connections, staking, expected Hours, secondsLastBlock'
import subprocess
import time
from time import localtime, strftime, sleep
from datetime import datetime
import smtplib # email sending function
from email.mime.text import MIMEText # for email formatting
import os, sys # for file operations
from timeit import default_timer as timer
from array import * # for arrays
# doNotDisturb[] gives the hours when emails will not be sent.
# times given in GMT as follows, 0000 = 12 midnight, 2300 = 11:00 pm, etc.
# Enter the do not disturb hours here in GMT, and they will be
# converted to the local time zone by the configuration file parameter
# UTCtimezoneoffset. Enter a "1" in the array to suppress emails during
# that hour. For example, to suppress emails for 0800 hours, enter a "1"
# in the 9th element of the array, which is doNotDisturb[8]. UTCtimezoneoffset
# will convert this time to 0800 hours local time.
#
# 0000, 0100, 0200, 0300, 0400, 0500, 0600, 0700
# 0800, 0900, 1000, 1100, 1200, 1300, 1400, 1500
# 1600, 1700, 1800, 1900, 2000, 2100, 2200, 2300
#
doNotDisturb=array('b',\
[1, 1, 1, 1, 1, 1, 1, 0,\
0, 0, 0, 0, 0, 0, 0, 0,\
0, 0, 0, 0, 0, 0, 0, 1])
# need to send email info in two pieces (subject and message), the subject will be truncated
def send_email(subjectText, messageText):
"Send an email to an address which may be an email-to-text address for mobile operator"
# if in a doNotDisturb hour, do not send this email
# can get here up to 4 seconds early for the new hour, so make some adjustment here
# to index into the current (or immediately upcoming) hour for doNotDisturb[]
global doNotDisturbQueue
unixTime = int(time.time())
unixTimeEarly = unixTime + 4 # allow 4 seconds of being early
UTCHour = int((unixTimeEarly % 86400) / 3600) # 0..23, no half hour time zones
localHour = UTCHour + UTCTimeOffset
if localHour > 23:
localHour -= 24
elif localHour < 0:
localHour += 24
# print("localHour =", localHour, "doNotDisturb =", doNotDisturb[localHour])
if doNotDisturb[localHour] == 0: # go ahead and send email
if len(doNotDisturbQueue) > 0: # had some messages queued, add them
messageText += '\n'
messageText += doNotDisturbQueue
doNotDisturbQueue = '' # clear out for next time
msg = MIMEText(messageText) # use as message
server = smtplib.SMTP('smtp.gmail.com:587')
msg['Subject'] = subjectText # email subject
msg['From'] = fromAddress # from: email address
msg['To'] = toAddress # to: email address
server.starttls()
time.sleep(0.05)
server.login(username, password)
time.sleep(0.05)
# server.set_debuglevel(1)
server.sendmail(fromAddress, toAddress, msg.as_string())
time.sleep(0.05)
server.quit()
time.sleep(0.05)
else: # queue up the message for when doNotDisturb is over
if sendEmailForNewHour != True: # but don't queue up hourly status messages
now = datetime.now()
current_timeHMS = now.strftime("%H:%M:%S")
doNotDisturbQueue += current_timeHMS + subjectText + messageText + '\n'
# print("doNotDisturbQueue =", doNotDisturbQueue)
def parse_number(field, offset, lenData, periodAllowed):
'''
parse the global variable "data" which is the response from qtum-cli calls.
Search for the text "field", then get the digit characters starting
"offset" characters from the start of the field, and search through at
least "lenData" characters, and respond to a period "." if "periodAllowed"
is True.
For example, to find the balance from the qtum-cli command -getinfo:
periodAllowed = True
v
..."balance": 14698.3456000, \r\n...
^
offset = 10 characters from start of balance
'''
global data
temp = ' '
dataIndex = data.find(field, 0, lenData)
i = dataIndex + offset # point at the first digit
if dataIndex > 0: # found field
while i <= lenData - 1:
if data[i] >= '0' and data[i] <= '9':
temp += data[i]
elif data[i] == "." and periodAllowed == True: # if period allowed
temp += data[i]
elif data[i] == ",":
break
elif (i == dataIndex + offset) and (data[i] == "-"): # allow negative sign
temp += data[i] # first character only
else: # how to find \r at end of response, like for estimated time?
# print("QM error, bad character in ", field)
break
i += 1
if i >= lenData:
break
return(temp)
else:
return(-1) # an error
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# parse alphanumeric text like "True", "[email protected]" and "HP8200"
def parse_alphanum(field, offset, lenData):
# parse against the global variable "data"
global data
temp = ''
dataIndex = data.find(field, 0, lenData)
i = dataIndex + offset # point at the first digit
if dataIndex > 0: # found field
# allow characters 0..9, @..Z, a..z
while i <= lenData - 1:
# print("data[i] = ", data[i])
if (data[i] >= 'a' and data[i] <= 'z') or +\
(data[i] >= '@' and data[i] <= 'Z') or +\
(data[i] >= '0' and data[i] <= '9') or +\
(data[i] == '.') or +\
(data[i] == '-'):
temp += data[i]
elif data[i] == "," or data[i] == "'\'": # for -getinfo proof-of-stake
break
else:
# print("QM error, bad character in ", field) #PY3
# print "QM error, bad character in " + field #PY2
break
i += 1
if i >= lenData:
break
return(temp)
else:
return(-1) # an error
def parse_logical(field, offset, lenData):
# parse against the global variable "data"
global data
temp = ''
dataIndex = data.find(field, 0, lenData)
i = dataIndex + offset # point at the first digit
if dataIndex > 0: # found field
while i <= lenData - 1:
if data[i] >= 'a' and data[i] <= 'z':
temp += data[i]
elif data[i] == ",":
break
else:
print("QM error, bad character in ", field) #PY3
# print "QM error, bad character in " + field #PY2
break
i += 1
if i >= lenData:
break
# print("field =", field, "temp =", temp)
if temp == "true":
return(True)
elif temp == "false":
return(False)
else:
return(-1) # an error
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
print("QtumMon version", version) #PY3
# print "QtumMon version " + version #PY2
testPass = 0 # for testing
sendNewSingleBlockReward = False # send an alert when have all the info, new single reward
sendNewMultiBlockReward = False # send and alert for a new overlapping reward
sendStakeReturned = False # send an alert that the stake is returned
sendBlockStaleAlert = False # blocks are getting stale > 1/2 hours
doNotDisturbQueue = '' # alert messages queued up during doNotDisturb times
QMStartTime = int(time.time()) # time program started, after 30 minutes look for stale blocks
# and low connections
start = timer() # for timing block loop below, first time through
start2 = timer() # kludge, for loop timing
lastBlockCheckTime = timer() # for startup
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Testing response times of qtum-cli and qtumd:
# average of 100 loops in seconds, no display getinfo getstakingdata
# HP 8200 64 bit i5-2400 3.1 GHz, 32 bit qtumd 1.224 1.224
# HP 750-411 64 bit i5-6400 2.7 GHz, 64 bit qtumd 1.013 1.008
# HP 750-411 64 bit i5-6400 2.7 GHz, 64 bit qtumd 140301 1.007 1.008
'''
start = timer()
i = 0
while i < 100:
# output = subprocess.check_output('qtum-cli "-getinfo"', shell = True)
# output = subprocess.check_output('qtum-cli "getstakinginfo"', shell = True)
time.sleep(1.0)
i += 1
end = timer()
print(end - start)
sys.exit()
'''
# read config file - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
'''
hostname: "LD18",
sendemail: "true",
sendhourlyemail: "true",
setdomestic: "true",
domesticaddress: "[email protected]",
internationaladdress: "[email protected]",
enablelogging: "true",
emailaddress: "[email protected]",
emailusername: "mygmail",
emailpassword: "abcdefghijklmnop",
UTCtimezoneoffset: "8",
'''
try:
configFile = open(config_file_name, 'r') # check for success, or exit
except:
print("ERROR opening configuration file")
print('The configuration file "qtumMonConfig.txt" must be in the same directory with QM, qtumd and qtum-cli')
sys.exit()
data = configFile.read()
lenData = len(data)
configFile.close()
# print(data)
# parse the configuration values
hostName = parse_alphanum("hostname", 11, lenData)
sendEmail = parse_logical("sendemail", 12, lenData) # set to control sending of alert emails
sendHourlyEmail = parse_logical("sendhourlyemail", 18, lenData)
setDomestic = parse_logical("setdomestic", 14, lenData) # set for domestic vs. international
# email addresses can send a text to mobile phone, check your carrier
# domestic email address, could be a mobile number text address
# see http://www.makeuseof.com/tag/email-to-sms/
domesticAddress = parse_alphanum("domesticaddress", 18, lenData)
# international email address, could be a mobile number text address
internationalAddress = parse_alphanum("internationaladdress", 23, lenData)
if setDomestic == True:
toAddress = domesticAddress
else:
toAddress = internationalAddress
# print("toAddress =", toAddress)
enableLogging = parse_logical("enablelogging", 16, lenData)
# emails are sent from this email account, Gmail log in credentials
# See https://support.google.com/accounts/answer/185833?hl=en
# for Gmail, turn on 2FA and assign app password, make new device "Python"
fromAddress = parse_alphanum("emailaddress", 15, lenData)
# print("fromAddress =", fromAddress)
username = parse_alphanum("emailusername", 16, lenData)
# print("username =", username)
# Application Specific password from Python
password = parse_alphanum("emailpassword", 16, lenData)
# print("password =", password)
UTCTimeOffset = int(parse_number("UTCtimezoneoffset", 20, lenData, False))
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
print("hostName =", hostName) #PY3
# print "hostName =" + hostName #PY2
if sendEmail == True:
print("Email enabled, sending to", toAddress)
if sendHourlyEmail == True:
print("Sending hourly emails")
else:
print("But not sending hourly emails")
else:
print("Not sending emails")
if enableLogging == True:
print("Logging enabled")
else:
print("Logging not enabled")
# initialize log file - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Can set the Z drive, as an Administrator, go to System - Disk Management - All Tasks -
# Change Drive Letter and Paths
if enableLogging == True:
GMT = strftime("%a, %d %b %Y %H:%M:%S", time.gmtime()) # GMT
# open log file in the format "QM_Log_DD_MMM_YYYY.csv"
out_file_name_QM = 'QM_Log_'+GMT[5]+GMT[6]+'_'+GMT[8]+GMT[9]+GMT[10]+\
'_'+GMT[12]+GMT[13]+GMT[14]+GMT[15]+'.csv'
print("QM log file name =", out_file_name_QM) #PY3
# print "QM log file name =" + out_file_name_QM #PY2
try:
outFileQM = open(out_file_name_QM, 'a') # create or open log file for appending
print("QM log file open for appending")
# print("Opening file", out_file_name_QM, "on", GMT)
tempStr = '000,Program,start,or,restart,version,' + version
outFileQM.write(tempStr)
outFileQM.write('\n')
# log starting time:
tempStr = '000,Logging,start,' + '_' + GMT[17]+GMT[18]+GMT[20]+GMT[21]+',hours,GMT,'+\
GMT[5]+GMT[6]+'_'+GMT[8]+GMT[9]+GMT[10]+GMT[12]+GMT[13]+GMT[14]+GMT[15]
outFileQM.write(tempStr)
outFileQM.write('\n')
outFileQM.write(logLabels) # write a row of column labels to the log
outFileQM.write('\n')
time.sleep(0.01)
outFileQM.close()
except IOError: # NOT WORKING
print("QM ERROR: File didn't exist, open for appending")
fileOpenQM = False
# check that qtumd is running - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# look for a response from qtum-cli getblockcount
while True: # wait here if qtumd is not running
block = -1 # error condition, unless set by qtumd
try:
block = int(subprocess.check_output('qtum-cli "getblockcount"', shell = True))
except:
print("ERROR, no response from qtumd")
# print("In qtumd check,block =", block)
if block > 0:
if qtumdIsRunning == False: # on startup or restart
qtumdIsRunning = True
print("qtumd is running")
sendOneTimeQtumdOffline = False # reset to find next qtumd offline
# the first time through, set oldBlock so don't get double row print
if firstTimeThrough == True:
oldBlock = block
break
else:
print("qtumd is NOT RUNNING. Standby")
qtumdIsRunning = False
if sendOneTimeQtumdOffline == False: # send an alert and log once for this problem
sendOneTimeQtumdOffline = True
tempMessage = " on" + hostName
tempSubject = " QTUMD NOT RUNNING"
if sendEmail == True:
send_email(tempSubject, tempMessage)
print("Sending email" + tempSubject + tempMessage) #PY3
# print "Sending email" + tempSubject + tempMessage #PY2
if enableLogging == True:
tempStr = "900, QTUMD OFFLINE"
if fileOpenQM == False:
fileOpenQM = True
outFileQM = open(out_file_name_QM, 'a') # open log file
# print("Opening file")
outFileQM.write(tempStr)
outFileQM.write('\n')
# print(tempStr)
time.sleep(0.01)
outFileQM.close()
fileOpenQM = False
time.sleep(15) # give some time to start/restart qtumd
# MAIN PROGRAM LOOP = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
while True:
if labelsPrintedAtStartup == False: # print the label row once during startup
labelsPrintedAtStartup = True
if block % 10 != 0: # because if block % 10 == True at startup, it will print below
print(labelRow)
# print("Top of loop")
# check "getinfo" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
''' Typical response from Skynet 9/27/2017
b'{\r\n "version": 140200,\r\n
"protocolversion": 70015,\r\n
"walletversion": 130000,\r\n
"balance": 5651.98847287,\r\n
"stake": 304.00000000,\r\n
"blocks": 29798,\r\n
"timeoffset": 0,\r\n
"connections": 25,\r\n
"proxy": "",\r\n
"difficulty": {\r\n
"proof-of-work": 1.52587890625e-005,\r\n
"proof-of-stake": 16976158.39177359\r\n },\r\n
"testnet": false,\r\n
"moneysupply": 100099192,\r\n
"keypoololdest": 1505054992,\r\n
"keypoolsize": 101,\r\n
"unlocked_until": 1606442385,\r\n
"paytxfee": 0.00000000,\r\n
"relayfee": 0.00001000,\r\n
"errors": ""\r\n}\r\n'
'''
if firstTimeThrough == True: # for the display, otherwise -getinfo in the block waiting loop below
try:
output = subprocess.check_output('qtum-cli "-getinfo"', shell = True)
# print(output)
except:
print("NO RESPONSE from qtumd. EXITING")
sys.exit()
data = str(output)
'''
# for testing
if testPass == 0:
print("Test data 1")
data = '{"balance": 0.00000000,"stake": 0.00000000, "blocks": 13765, "timeoffset": 0"}'
testPass = 1
elif testPass == 1:
print("Test data 2")
data = '{"balance": 0.00000000,"stake": 549.87654321, "blocks": 13766, "timeoffset": 0"}'
'''
# parse data to get various values - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# temp = ''
lenData = len(data)
# print("lenData =", lenData) # lenData = 568
balance = float(parse_number("balance", 10, lenData, True)) # get balance
# print("balance = ", balance)
stake = float(parse_number("stake", 8, lenData, True)) # get stake
# print("stake = ", stake)
block = int(parse_number("blocks", 9, lenData, False)) # get the current block
# print("block = ", block)
proofOfWork = parse_alphanum("proof-of-work", 16, lenData)
# print("proofOfWork = ", proofOfWork)
dDiff = float(parse_alphanum("proof-of-stake", 17, lenData))
# print("proofOfStake = ", dDiff)
# detect new block reward and flag to send alert - - - - - - - - - - - - - - - - - - - - -
'''
testPass += 1 # testing stake detection below
if testPass == 2:
stake = 10.8 # some pesky 3.6s
elif testPass == 3:
stake = 500.5 # new single reward
elif testPass == 4:
stake = 500.5
elif testPass == 5:
stake = 1000.8 # new multi reward
elif testPass == 6:
stake = 1000.8
elif testPass == 7: # returned
stake = 500.5
elif testPass == 8: # returned
stake = 0
'''
if stake != oldStake: # found a stake change
# no stake alert the first time through, for a stake in progress
if stake >= oldStake + minimumStakeSize and firstTimeThrough == False: # filters out 0.4s
if oldStake < minimumStakeSize:
print("Found a new single reward, block =", block, "stake =", stake) #PY3
# print "Found a new single reward, block =" + block + "stake =" + stake #PY2
sendNewSingleBlockReward = True
elif oldStake >= minimumStakeSize: # at least double reward period
print("Found a new multi reward, block =", block, "stake =", stake) #PY3
# print "Found a new multi reward, block =" + block + "stake =" + stake #PY2
sendNewMultiBlockReward = True
# detect a stake return, and filter out the 0.4 stuff
elif stake <= oldStake - minimumStakeSize: # block 26978, stake returned, works for overlapping reward periods
print("Stake returned, block =", block, "stake =", stake) #PY3
# print "Stake returned, block =" + block + "stake =" + stake #PY2
sendStakeReturned = True
oldStake = stake
connections = int(parse_number("connections", 14, lenData, False)) # get connections
unixTime = int(time.time())
# low connections checked with every new block. If low connections is discovered
# which may be triggered if the network connection to the wallet PC is lost, or partially
# lost, wait 7 blocks (about 15 minutes) before resending, using lowConnectionsAging
if (unixTime - QMStartTime) >= 1800: # 30 minutes after QM start
if connections <= 5 and lowConnectionsAging <= 0: # is this a good error quantity?
print("ALERT: low connections, only", connections) #PY3
# print "ALERT: low connections, only" + connections #PY2
sendLowConnectionsAlert = True # send alert below
lowConnectionsAging = 6 # set for aging process
elif connections <= 5: # still in low connections situation
lowConnectionsAging -= 1
elif connections >= 6:
lowConnectionsAging = 0 # reset it, no more low connections
# print("connections = ", connections)
# check "getstakinginfo" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
''' Typical response from Skynet 9/27/2017 (there were some brutal network weights on Skynet)
b'{\r\n "enabled": true,\r\n
"staking": true,\r\n
"errors": "",\r\n
"currentblocksize": 0,\r\n
"currentblocktx": 1000,\r\n
"pooledtx": 0,\r\n
"difficulty": 21729576.68254313,\r\n
"search-interval": 19456,\r\n
"weight": 565198847287,\r\n
"netstakeweight": 7968576249052219,\r\n
"expectedtime": 119083\r\n}\r\n'
'''
try:
output = subprocess.check_output('qtum-cli "getstakinginfo"', shell = True)
# print(output)
except:
print("NO RESPONSE from qtumd. EXITING")
sys.exit()
data = str(output)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# parse output to get enabled, staking, weight and netstakeweight, calculate immature
'''
# for testing
if testPass == 0:
print("Test data 1")
data = '{"balance": 0.00000000,"stake": 0.00000000, "blocks": 13765, "timeoffset": 0"}'
testPass = 1
elif testPass == 1:
print("Test data 2")
data = '{"balance": 0.00000000,"stake": 549.87654321, "blocks": 13766, "timeoffset": 0"}'
'''
temp = ''
lenData = len(data)
# print("lenData =", lenData) # lenData = 297
enabled = parse_logical("enabled", 10, lenData) # get enabled
# print("enabled =", enabled)
# confirm that staking is enabled, or send alert and wait here - - - - - - - - - - - - - -
while True:
staking = parse_logical("staking", 10, lenData) # get staking
if hostName == "HP8200":
staking = True # force for testing, with no mature coins or 0.0 balance
if staking == True:
sendOneTimeNotStaking = False # reset to detect problem below, next pass
# print("staking is True")
break
else: # not staking :-(
if sendOneTimeNotStaking == False:
if sendEmail == True:
tempMessage = ".on " + hostName
tempSubject = ".NOT STAKING"
send_email(tempSubject, tempMessage)
print("Sending email", tempSubject, tempMessage) #PY3
# print "Sending email" + tempSubject + tempMessage #PY2
sendOneTimeNotStaking = True
if enableLogging == True:
tempStr = "900, NOT STAKING"
if fileOpenQM == False:
fileOpenQM = True
outFileQM = open(out_file_name_QM, 'a') # open log file
# print("Opening file")
outFileQM.write(tempStr)
outFileQM.write('\n')
# print(tempStr)
time.sleep(0.01)
outFileQM.close()
fileOpenQM = False
print('ERROR: qtumd not set for staking. Run qtum-cli walletpassphrase "passphrase" 99999999 true')
time.sleep(15) # give some time to fix this
# weight is "my weight", the mature coins staking in this wallet
weight = float(parse_number("weight", 9, lenData, True)) / 100000000 # get weight in QTUM
# print("weight =", weight)
# calculate immature since qtumd doesn't seem to provide it
immature = balance - weight # coins that will mature after 500 blocks, but don't use it
# get netstakeweight in millions, the calculated estimate of all QTUM currently being staked
longNetStakeWeight = float(parse_number("netstakeweight", 17, lenData, False)) / 100000000
netStakeWeight = int(longNetStakeWeight)
# print("netstakeweight = ", netStakeWeight)
# get expectedtime in hours, average time until the next block reward
expectedTimeHours = float(parse_number("expectedtime", 15, lenData, False)) / 3600
# print("expectedTimeHours = ", expectedTimeHours)
'''
Format the data for printing on a display. Add commas and calculate the pads to keep
the columns aligned. Numbers are right justified:
Left side:
time | balance | stake | block
22:21:41 | 1,000.0 | 120.0 | 17,430
22:21:41 | 10,000.0 | 210.0 | 97,430
22:21:41 | 23,000.0 | 999.9 | 999,999
\pad1 \pad2 \pad3
Right side:
|my weight | net weight | con | stking |exp hrs| secLast
| 1,000.0 | 3,653,699 | 4 | yes | 340.0 | 10
| 10,000.0 | 23,653,699 | 10 | yes | 340.0 | 258
| 23,000.0 | 93,653,699 | 25 | yes | 2.3 | 558
\pad4 \pad5 \pad7 \pad8 \pad9
'''
if balance <= 99999:
balanceWithCommas = ("{:,f}".format(round(balance, 1)))[:-5]
pad1 = " " * (8 - len(balanceWithCommas))
balancePadCommas = pad1 + balanceWithCommas
else:
balancePadCommas = "xxxxxxxx"
if stake <= 999:
stakeWithCommas = ("{:,f}".format(round(stake, 1)))[:-5]
pad2 = " " * (5 - len(stakeWithCommas))
stakePadCommas = pad2 + stakeWithCommas
else:
stakePadCommas = "xxxxx"
if block <= 999999:
blockWithCommas = "{:,d}".format(int(block))
pad3 = " " * (9 - len(blockWithCommas))
blockPadCommas = pad3 + blockWithCommas
else:
blockPadCommas = "xxxxxxxxx"
if weight <= 99999:
weightWithCommas = ("{:,f}".format(round(weight, 1)))[:-5]
pad4 = " " * (8 - len(weightWithCommas))
weightPadCommas = pad4 + weightWithCommas
else:
weightPadCommas = "xxxxxxxx"
if netStakeWeight <= 99999999:
netWeightWithCommas = "{:,d}".format(int(netStakeWeight))
pad5 = " " * (10 - len(netWeightWithCommas))
netWeightPadCommas = pad5 + netWeightWithCommas
else:
netWeightPadCommas = "xxxxxxxxxx"
netWeightWithCommas = "xxxxxxxxxx" # Skynet start > 100m, to hourly
'''
if immature <= 999999:
immatureWithCommas = ("{:,f}".format(round(immature, 1)))[:-5]
pad6 = " " * (9 - len(immatureWithCommas))
immaturePadCommas = pad6 + immatureWithCommas
else:
immaturePadCommas = "xxxxxxxxx"
'''
# connections is a low range integer, say 0 to 120, and doesn't need commas
if connections <= 999:
strConnections = str(connections)
pad7 = " " * (3 - len(strConnections))
connectionsPad = pad7 + strConnections
else:
connectionsPad = "xxx"
if expectedTimeHours <= 999:
exphrsWithCommas = ("{:,f}".format(round(expectedTimeHours, 1)))[:-5]
pad8 = " " * (5 - len(exphrsWithCommas))
exphrsPadCommas = pad8 + exphrsWithCommas
else:
exphrsPadCommas = "xxxxx"
if secondsSinceLastBlock <= 9999:
secLastWithCommas = "{:,d}".format(int(secondsSinceLastBlock))
pad9 = " " * (5 - len(secLastWithCommas))
secLastPadCommas = pad9 + secLastWithCommas
else:
secLastPadCommas = "xxxx"