This repository has been archived by the owner on Oct 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
failover1.py
208 lines (195 loc) · 7.63 KB
/
failover1.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
#VIT Witness Failover
import os
import sys
import json
import datetime
import time
from pprint import pprint
from beem.witness import Witness
from beem import Steem
from beem.instance import set_shared_steem_instance
from beem.transactionbuilder import TransactionBuilder
from beembase import operations
from beem.amount import Amount
from beem.asset import Asset
from dotenv import load_dotenv
import yagmail
import status_logger
load_dotenv()
#Variables:
CUSTOM_CHAINS = json.loads(os.environ.get('CUSTOM_CHAINS', '{}'))
NATIVE_SYMBOL = os.environ.get('NATIVE_SYMBOL', 'VIT')
NATIVE_PREFIX = os.environ.get('NATIVE_PREFIX', 'VIT')
NATIVE_VESTED = os.environ.get('NATIVE_VESTED', 'VESTS')
UNLOCK = os.getenv('UNLOCK')
ACCT = os.getenv('ACCOUNT')
NO_BROADCAST = bool(int(os.getenv('NO_BROADCAST')))
P_THRESHOLD = int(os.getenv('P_THRESHOLD'))
B_THRESHOLD = int(os.getenv('B_THRESHOLD'))
BACKUP_KEY = os.getenv('BACKUP_KEY')
WIF = os.getenv('WIF')
FROM = os.getenv('FROM_ADDRESS')
FROM_PASS = os.getenv('FROM_PASS')
TO = os.getenv('TO_ADDRESS')
yag = yagmail.SMTP(FROM, FROM_PASS)
# Email Alert Variables:
primary_subject = "Primary witness server failed, failover activated"
primary_body = f"Your primary VIT Witness server missed more than your set primary threshold of {P_THRESHOLD} total missed blocks." \
"The failover script disabled your primary witness server."
failover_subject = "Backup witness server activated"
failover_body = "Your backup VIT Witness server was succesfully activated by the failover script. " \
f"The killswitch will shut down the backup server if your backup threshold of {B_THRESHOLD} total missed blocks is reached." \
backup_subject = "Backup witness server failed, killswitch activated"
backup_body = f"Your backup VIT Witness server missed more than your set backup threshold of {B_THRESHOLD} total missed blocks." \
"The failover script activated killswitch and disabled your backup witness server."
stm = Steem(
node=["https://peer.vit.tube/"],
bundle=True,
blocking="head",
nobroadcast=NO_BROADCAST, #set True for testing
custom_chains=CUSTOM_CHAINS,
keys={'active': WIF},
)
#stm.wallet.unlock(UNLOCK)
while True:
try:
set_shared_steem_instance(stm)
currentdatetime = datetime.datetime.now()
w1 = Witness(ACCT)
json_string = json.dumps(w1.json(), indent=4)
data = json.loads(json_string)
total_missed = data["total_missed"]
if total_missed >= P_THRESHOLD:
#Disable witness.
tx = TransactionBuilder(steem_instance=stm)
update_witness = {
"owner": ACCT,
"url": "No website yet",
"block_signing_key": NATIVE_PREFIX + '1111111111111111111111111111111114T1Anm',
"props": {
"account_creation_fee": Amount("0.100 %s" % (NATIVE_SYMBOL)),
"maximum_block_size":131072,
},
"fee": Amount("0.000 %s" % (NATIVE_SYMBOL)),
"prefix": NATIVE_PREFIX,
}
op = operations.Witness_update(**update_witness)
tx.appendOps(op)
#tx.appendSigner(ACCT, "active")
tx.appendWif(WIF)
signed_tx = tx.sign()
broadcast_tx = tx.broadcast()
status_logger.logger.warning("Total missed at or above threshold. Disabling primary witness server. \nOperation: " + json.dumps(broadcast_tx, indent=4))
yag.send(TO, primary_subject, primary_body)
break
else:
status_logger.logger.info("Witness " + ACCT + " current total missed: " + str(total_missed) + "\nPrimary witness server operational\n--------------------------")
time.sleep(60) #Pause script for 60 seconds.
except KeyboardInterrupt:
sys.exit()
except Exception as WitnessDoesNotExistsException:
status_logger.logger.exception("Exception occured\n")
status_logger.logger.info("Reconnecting...")
stm = Steem(
node=["https://peer.vit.tube/"],
bundle=True,
blocking="head",
nobroadcast=NO_BROADCAST, #set True for testing
custom_chains=CUSTOM_CHAINS,
keys={'active': WIF},
)
#stm.wallet.unlock(UNLOCK)
except Exception as e:
status_logger.logger.exception("Exception occured\n")
yag = yagmail.SMTP(FROM, FROM_PASS)
e_subject = "VIT Witness Killswitch Error Occured"
e_body = "Exception occured. Killswitch script has stopped running. Please login and review log file."
yag.send(TO, e_subject, e_body)
sys.exit()
#Enable backup witness server.
try:
#Pause script momentarily after disabling primary witness.
time.sleep(60)
tx = TransactionBuilder(steem_instance=stm)
update_witness = {
"owner": ACCT,
"url": "No website yet",
"block_signing_key": NATIVE_PREFIX + BACKUP_KEY,
"props": {
"account_creation_fee": Amount("0.100 %s" % (NATIVE_SYMBOL)),
"maximum_block_size":131072,
},
"fee": Amount("0.000 %s" % (NATIVE_SYMBOL)),
"prefix": NATIVE_PREFIX,
}
op = operations.Witness_update(**update_witness)
tx.appendOps(op)
#tx.appendSigner(ACCT, "active")
tx.appendWif(WIF)
signed_tx = tx.sign()
broadcast_tx = tx.broadcast()
status_logger.logger.info("Activated Backup Witness Server \nOperation: " + json.dumps(broadcast_tx, indent=4))
yag.send(TO, failover_subject, failover_body)
time.sleep(10) #Seconds you want to wait before you start monitoring the backup server.
except Exception as e:
status_logger.logger.exception("Exception occured\n")
yag = yagmail.SMTP(FROM, FROM_PASS)
e_subject = "VIT Witness Killswitch Error Occured"
e_body = "Exception occured. Killswitch script has stopped running. Please login and review log file."
yag.send(TO, e_subject, e_body)
sys.exit()
while True:
try:
set_shared_steem_instance(stm)
currentdatetime = datetime.datetime.now()
w1 = Witness(ACCT)
json_string = json.dumps(w1.json(), indent=4)
data = json.loads(json_string)
total_missed = data["total_missed"]
if total_missed >= B_THRESHOLD:
#Disable witness.
tx = TransactionBuilder(steem_instance=stm)
update_witness = {
"owner": ACCT,
"url": "No website yet",
"block_signing_key": NATIVE_PREFIX + '1111111111111111111111111111111114T1Anm',
"props": {
"account_creation_fee": Amount("0.100 %s" % (NATIVE_SYMBOL)),
"maximum_block_size":131072,
},
"fee": Amount("0.000 %s" % (NATIVE_SYMBOL)),
"prefix": NATIVE_PREFIX,
}
op = operations.Witness_update(**update_witness)
tx.appendOps(op)
#tx.appendSigner(ACCT, "active")
tx.appendWif(WIF)
signed_tx = tx.sign()
broadcast_tx = tx.broadcast()
status_logger.logger.warning("Total missed at or above threshold. Disabling backup witness server. \nOperation: " + json.dumps(broadcast_tx, indent=4))
yag.send(TO, backup_subject, backup_body)
sys.exit()
else:
status_logger.logger.info("Witness " + ACCT + " current total missed: " + str(total_missed) + "\nBackup witness server operational\n--------------------------")
time.sleep(60) #Pause script for 60 seconds.
except KeyboardInterrupt:
sys.exit()
except Exception as WitnessDoesNotExistsException:
status_logger.logger.exception("Exception occured\n")
status_logger.logger.info("Reconnecting...")
stm = Steem(
node=["https://peer.vit.tube/"],
bundle=True,
blocking="head",
nobroadcast=NO_BROADCAST, #set True for testing
custom_chains=CUSTOM_CHAINS,
keys={'active': WIF},
)
#stm.wallet.unlock(UNLOCK)
except Exception as e:
status_logger.logger.exception("Exception occured\n")
yag = yagmail.SMTP(FROM, FROM_PASS)
e_subject = "VIT Witness Killswitch Error Occured"
e_body = "Exception occured. Killswitch script has stopped running. Please login and review log file."
yag.send(TO, e_subject, e_body)
sys.exit()