-
Notifications
You must be signed in to change notification settings - Fork 2
/
PoolStore.py
189 lines (138 loc) · 4.67 KB
/
PoolStore.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
"""
PoolStore.py -- Is a abstraction over the mongodb api
to facilitate storing and upating of pool information
in the database.
by elchupathingy
"""
from Config import Config
from pymongo import Connection
from datetime import datetime
import time
from UserStore import UserStore
class PoolStore:
def __init__( self, config, users ):
self.config = Config( config )
self.address = self.config['mongo']['address']
self.port = self.config['mongo']['port']
self.database = "poolManager"
try:
self.database = self.config['mongo']['database']
except:
pass
self.collection = "pools"
try:
self.collection = self.config['mongo']['collection']
except:
pass
self.con = Connection( self.address, self.port, max_pool_size=10 )
self.db = self.con[self.database]
self.collection = self.db[self.collection]
self.collection.ensure_index( "name", unique=True )
self.users = users
print "Created"
def addPool( self, poolname, address, port, username, password, timeout ):
pool = {}
pool['name'] = poolname
pool['address'] = address
pool['port'] = int( port )
pool['username'] = username
pool['password'] = password
pool['timeout'] = timeout
pool['shares'] = 0
pool['lastShare'] = datetime.now()
pool['stales'] = 0
pool['lastStale'] = datetime.now()
pool['shareStore'] = []
pool['getworks'] = 0
pool['lastGetWork'] = datetime.now()
pool['blocksFound'] = 0
pool['lastBlockFound'] = datetime.now()
pool['users'] = []
return self.collection.save( pool )
def getPool( self, poolName ):
return self.collection.find_one( { "name": poolName } )
def updatePool( self, pool ):
return self.collection.save( pool )
def removePool( self, oid ):
return self.collection.remove( oid )
def getAll( self ):
return self.collection.find()
def incGetWork( self, pool ):
p = self.getPool( pool )
lastTimeStamp = time.mktime( p['lastGetWork'].timetuple() )
p['lastGetWork'] = datetime.now()
currentTimeStamp = time.mktime( p['lastGetWork'].timetuple() )
diff = currentTimeStamp - lastTimeStamp
if diff > 0.0:
hour = 60 * 60
perHour = hour / diff
p['getworks'] = ( p['getworks'] + perHour ) / 2
self.collection.save( p )
def incShares( self, pool ):
p = self.getPool( pool )
p['shares'] += 1
p['shares'] = int( p['shares'] )
p['lastShare'] = datetime.now()
self.collection.save( p )
def incStales( self, pool ):
p = self.getPool( pool )
if "stale" in p:
p['stales'] += 1
else:
p['stales'] = 1
p['lastStale'] = datetime.now()
self.collection.save( p )
def addShare( self, pool, share ):
p = self.getPool( pool )
stale = False
try:
if share['odata'] not in p['shareStore']:
p['shareStore'].append( str( share['odata'] ) )
else:
stale = True
except:
p['shareStore'] = []
p['shareStore'].append( str( share['odata'] ) )
self.collection.save( p )
return stale
def incBlocksFound( self, pool ):
p = self.getPool( pool )
p['blocksFound'] += 1
p['lastBlockFound'] = datetime.now()
p['shareStore'] = []
self.collection.save( p )
def addUser( self, poolname, username, password ):
user = self.users.getUser( username, password )
if user != None:
if self.collection.find( { "name": poolname, "users": { "$in": [ user['_id'] ] } } ).count() == 0:
self.collection.update( { "name": poolname }, { "$push": { "users": user['_id'] } } )
return True
return False
def totalPools( self ):
return self.collection.find().count()
def totalShares( self ):
shares = self.collection.group( ["shares"], { "shares": { "$gte": 0 } }, {'csum': 0 }, "function( obj, prev ) { prev.csum += obj.shares; }" );
total = 0
for s in shares:
total += int( s['shares'] )
return total
def clearShares( self, poolname ):
p = self.getPool( poolname )
p['shareStore'] = []
self.collection.save( p )
if __name__ == "__main__":
u = UserStore( "config.json" )
p = PoolStore( "config.json", u )
#pool = p.getPool( "mine" )
#pool['stales'] = 0
#pool['users'] = []
#pool['timeout'] = 5
#pool['target'] = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000"
#pool['getworks'] = 0
#pool['lastGetWork'] = datetime.now()
#pool['blocksFound'] = 0
#print p.updatePool( pool )
#print p.getPool( "mine" )
#print p.addUser( "mine", "testing" )
#print p.addUser( "mine", "testing2" )
pool = p.addPool( "eligius", "ra.mining.eligius.st", 8337, "1JJLw5zUps6EzRPu6T4BcMJNeU2JpoBHb3", "x", 10 )