-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
executable file
·149 lines (114 loc) · 5.09 KB
/
main.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
#!/usr/bin/env python2.7
"""
Nengcoin (NENG) blockchain has dynamic difficulty adjustment algorithm
which may trigger ASIC miners to get stuck on NENG blockchain finding no blocks
from several minutes to several hours.
Cheetah_cpuminer will automatically start mining at local PC when ASIC miners get stuck.
Cheeta_cpuminer will stop mining when ASIC miners are smoothly generating blocks.
A full node of Nengcoin is required to be running at local PC
"""
################################################################################
# Preamble.
################################################################################
# Python standard library imports.
import argparse
import time
import datetime
import itertools
import platform
import os.path
import re
import sys
# Modules in this package.
import cpuminer.cheetah as cheetah
import cpuminer.utils as utils
# Safety for cases when shebang is bypassed.
assert sys.version_info[0] == 2 and sys.version_info[1] >= 7, 'Python version 2.7 (or a later 2.x version) required; you have version: {}.{}.{}'.format(
*sys.version_info[0:3])
################################################################################
# Main Function
################################################################################
class UserInputException(Exception):
def __init__(self, message):
self.message = message
def __str__(self):
return repr(self.message)
def main(args):
"""Main execution functions.
"""
osname = platform.system()
print "Your Computer Platform is: {}".format(osname)
if osname == 'Linux':
tmpFile1 = os.path.join(os.path.expanduser("~"), '.nengcoin', 'nengcoin.conf')
tmpFile2 = os.path.join(os.path.dirname(
utils.getPathOfThisFile()), 'nengcoin.conf')
elif osname == 'Windows':
tmpFile1 = os.path.join(os.path.expandvars("%userprofile%"), 'AppData\Roaming\Nengcoin','nengcoin.conf')
tmpFile2 = os.path.join(os.path.dirname(
utils.getPathOfThisFile()), 'nengcoin.conf')
elif osname == 'Darwin':
tmpFile1 = os.path.join(os.path.expanduser("~"), 'Library/Application Support/Nengcoin', 'nengcoin.conf')
tmpFile2 = os.path.join(os.path.dirname(
utils.getPathOfThisFile()), 'nengcoin.conf')
else:
assert False, "Error: unsupported operating system: {}".format(osname)
if utils.isReadable(tmpFile1):
NengConfigFile = tmpFile1
print "config found: {}".format(tmpFile1)
elif utils.isReadable(tmpFile2):
NengConfigFile = tmpFile2
print "config found: {}".format(tmpFile2)
else:
raise UserInputException(
"Error in reading NENG Config File. Please copy or create file 'nengcoin.conf' using example file")
sys.exit(100)
# Loading configuration file
configFile = open(NengConfigFile, 'rU')
config = dict([(k, None) for k in ['rpcuser', 'rpcpassword', 'rpcport']])
config['rpcport'] = 6376
# TODO: Properly Loading config from file
for line in configFile:
if line.startswith('#') or re.match(r'^\s+$', line):
continue
m1 = re.search(
r'^rpcuser=(\S+)\s*$', line, re.M)
if m1 :
config['rpcuser'] = m1.group(1)
m2 = re.search(
r'^rpcpassword=(\S+)\s*$', line, re.M)
if m2:
config['rpcpassword'] = m2.group(1)
m3 = re.search(
r'^rpcport=(\d+)\s*$', line, re.M)
if m3:
config['rpcport'] = int(m3.group(1))
# window will crash on os.fync on read only file
# a simple close
configFile.close()
assert config['rpcuser'] is not None, "rpcuser missing!"
assert config['rpcpassword'] is not None, "rpcpassword missing!"
print "Bitcoin Digital Gold - HODL\nDogecoin to the Moon"
print "Here comes Cheetah running with Neng\nMobile Decentralization!\n"
print "Nengcoin cheetah_cpuminer started"
blocknum = 0
while True:
blocknum = cheetah.run_cheetah(blocknum,config['rpcuser'], config['rpcpassword'], config['rpcport'], args.cpu)
time.sleep(args.interval)
################################################################################
# Command Line executions - Argument Parsing.
################################################################################
if __name__ == '__main__':
# ===============================================================================
# Parse and validate command-line args and input config.
# ===============================================================================
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument('--interval', type=int, nargs='?', default=120 ,
help='seconds to wait between each check on NENG blockchain, [default: 120]')
parser.add_argument('--cpu', nargs='?', type=int, default=1 ,
help='How many cpu cores to be used for mining [default: 1]')
args = parser.parse_args()
# running main function
main(args)