forked from harmony-community-node/hmybidder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hmybidder.py
178 lines (168 loc) · 9.14 KB
/
hmybidder.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
import sys
import time
import random
import argparse
import threading
import configparser
from sys import argv
from os import listdir
from os.path import isfile, join, exists
from utilities.hmybidder_logger import HmyBidderLog
from utilities.globals import Globals
from models import NetworkInfo, ValidatorInfo
from blockchain.hmyclient import HmyClient
from bidder.bidderclient import HMYBidder
from blockchain.validator import Validator
version = 'v1.0.0'
def main():
network_info = Validator.getNetworkLatestInfo()
if network_info != None:
dict_network_info = network_info.to_dict()
curretEpoch = Validator.getCurrentEpoch() # Dont run the bidding process if it is been run for current epoch
info_dict = {
'total_delegated' : f'{Validator.getTotalStakedToValidator(Globals._walletAddress)}',
'median_raw_stake' : dict_network_info['median_raw_staking'],
'leverage' : Globals._leverage,
'block_range' : f'{Globals._upperBlock}:{Globals._lowerBlock}',
'epoch_last_block' : dict_network_info['epoch_last_block'],
'block_number' : dict_network_info['block_number'],
'blocks_to_next_epoch' : dict_network_info['blocks_to_next_epoch']
}
HmyBidderLog.info(info_dict)
if network_info.blocks_to_next_epoch in range(Globals._lowerBlock, Globals._upperBlock) and curretEpoch != Globals._currentEpoch: # checking extra 5 block to make sure not missing the bidding process
#if True:
HmyBidderLog.info('Started Evaluating the BLS Keys')
HMYBidder.startBiddingProcess(network_info)
Globals._currentEpoch = Validator.getCurrentEpoch() # reset current epoch after running the bidding process
if Globals._refreshInSeconds == None or Globals._refreshInSeconds <= 0:
Globals._refreshInSeconds = 60
threading.Timer(Globals._refreshInSeconds, main).start() #Keep running the process every N seconds
def validateShardKey(shardKeys):
valid = False
parts = shardKeys.split(":")
if len(parts) == 8:
try:
Globals._shardsKeys = {}
for shardId in range(0, Globals._numberOfShards):
shardKey = f'shard{shardId}'
Globals._shardsKeys[shardKey] = []
for f in listdir(Globals._blsdirPath):
if isfile(join(Globals._blsdirPath, f)):
if f.endswith(".key"):
blsKey = f.replace(".key", "")
shard = int(HmyClient.getShardForBlsKey(blsKey))
if shard != -1:
if shard == 0:
if not blsKey in Globals._shardsKeys['shard0']:
Globals._shardsKeys['shard0'].append(blsKey)
elif shard == 1:
if not blsKey in Globals._shardsKeys['shard1']:
Globals._shardsKeys['shard1'].append(blsKey)
elif shard == 2:
if not blsKey in Globals._shardsKeys['shard2']:
Globals._shardsKeys['shard2'].append(blsKey)
elif shard == 3:
if not blsKey in Globals._shardsKeys['shard3']:
Globals._shardsKeys['shard3'].append(blsKey)
else:
HmyBidderLog.error(f" Error while getting the Shard for BLS Key {blsKey}")
#print(f'{len(Globals._shardsKeys["shard0"])} - {len(Globals._shardsKeys["shard1"])} - {len(Globals._shardsKeys["shard2"])} - {len(Globals._shardsKeys["shard3"])}')
if int(parts[1]) <= len(Globals._shardsKeys['shard0']) and int(parts[3]) <= len(Globals._shardsKeys['shard1']) and int(parts[5]) <= len(Globals._shardsKeys['shard2']) and int(parts[7]) <= len(Globals._shardsKeys['shard3']):
valid = True
else:
valid = False
except Exception as ex:
HmyBidderLog.error(f'Hmybidder validateShardKey {ex}')
valid = False
finally:
return valid
def getopts(argv):
showHelp = False
showVersion = False
while argv:
try:
if argv[0][0] == '-':
if argv[0].lower() == '-c' or argv[0].lower() == '--config':
stopScript = False
Globals._configFile = argv[1]
if not exists(Globals._configFile):
stopScript = True
HmyBidderLog.error('Missing config file')
config = configparser.ConfigParser()
config.read(Globals._configFile)
if config.get('DEFAULT','Network') != None:
Globals._network_type = config.get('DEFAULT','Network')
if config.get('DEFAULT', 'LogFilePath') != None:
Globals._logFile = config.get('DEFAULT', 'LogFilePath')
HmyBidderLog.setLogFileLocation(Globals._logFile)
if config.get('DEFAULT', 'HMYDir') != None:
Globals._hmyDirectory = config.get('DEFAULT', 'HMYDir')
if config.get('DEFAULT', 'BLSDir') != None:
Globals._blsdirPath = config.get('DEFAULT', 'BLSDir')
if config.get('DEFAULT', 'ONEADDRESS') != None:
Globals._walletAddress = config.get('DEFAULT', 'ONEADDRESS')
if not Validator.validateONEAddress(Globals._walletAddress):
HmyBidderLog.error('Wallet Address is in wrong format, please verify')
stopScript = True
if not HmyClient.checkIfAccountofWalletAddressExists(Globals._walletAddress):
HmyBidderLog.error('Wallet Address is not in wallet accounts')
stopScript = True
if config.get('DEFAULT', 'Leverage') != None:
Globals._leverage = int(config.get('DEFAULT', 'Leverage'))
if config.get('DEFAULT', 'ShardKeys') != None:
Globals._allowedShardKeys = config.get('DEFAULT', 'ShardKeys')
parts = Globals._allowedShardKeys.split(":")
if len(parts) == 8:
totalAllowedKeys = int(parts[1]) + int(parts[3]) + int(parts[5]) + int(parts[7])
if (totalAllowedKeys <= 0):
HmyBidderLog.error("total allowd -shards.keys cannot be zero or below, please verify")
stopScript = True
else:
HmyBidderLog.error("-shards.keys format seems be wrong, please verify (ex: 0:1:1:3:2:0:3:1)")
stopScript = True
if not validateShardKey(Globals._allowedShardKeys):
HmyBidderLog.error("BLS Keys set on --shards.keys don't match the minimum number of keys available on --blsdir")
stopScript = True
if config.get('DEFAULT', 'PassphraseFile') != None:
Globals._passphraseFile = config.get('DEFAULT', 'PassphraseFile')
if config.get('DEFAULT', 'Slots') != None:
Globals._totalSlots = int(config.get('DEFAULT', 'Slots'))
if config.get('DEFAULT', 'BlockRange') != None:
blockRange = config.get('DEFAULT', 'BlockRange')
parts = blockRange.split(":")
if len(parts) == 2:
Globals._upperBlock = int(parts[0])
if Globals._upperBlock <= 0:
Globals._upperBlock = 50
Globals._lowerBlock = int(parts[1])
if Globals._lowerBlock <= 0:
Globals._lowerBlock = 30
if config.get('DEFAULT', 'RefreshInSeconds') != None:
Globals._refreshInSeconds = int(config.get('DEFAULT', 'RefreshInSeconds'))
if stopScript:
#HmyClient.stopSystemdService("hmybidder.service")
return False
return True
elif argv[0].lower() == '-v' or argv[0].lower() == '--version':
showVersion = True
elif argv[0].lower() == '-h' or argv[0].lower() == '--help':
showHelp = True
except Exception as ex:
print(f'Command line input error {ex}')
finally:
argv = argv[1:]
if showHelp:
with open('help.txt', 'r') as file:
print(file.read())
return False
elif showVersion:
print(f'Version {version}')
return False
elif Globals._walletAddress == '':
with open('help.txt', 'r') as file:
print(file.read())
return False
return True
if __name__ == '__main__':
if getopts(argv):
main()