-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.py
More file actions
261 lines (220 loc) · 8.48 KB
/
Copy pathmain.py
File metadata and controls
261 lines (220 loc) · 8.48 KB
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
import json
import os
import hashlib
import struct
import re
import shutil
from datetime import datetime
from threading import Thread, Lock
from time import sleep
from flask import Flask, request, jsonify
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
app = Flask(__name__)
miners_lock = Lock()
# Load configuration
with open('config.json') as f:
config = json.load(f)
# Initialize RPC connection
rpc = AuthServiceProxy(
f"http://{config['rpc_user']}:{config['rpc_password']}@"
f"{config['rpc_host']}:{config['rpc_port']}"
)
# Helper functions
def validate_bitcoin_address(address):
pattern = r'^(bc1|[13])[a-zA-HJ-NP-Z0-9]{25,39}$'
return re.match(pattern, address) is not None
def double_sha256(data):
return hashlib.sha256(hashlib.sha256(data).digest()).digest()
def create_coinbase_tx(height, value, address):
value_sat = int(value * 10**8)
if address.startswith('bc1'):
script_pubkey = bytes.fromhex(f"0014{hashlib.new('ripemd160', hashlib.sha256(bytes.fromhex(address)).digest()).hex()}")
else:
script_pubkey = bytes.fromhex(f"76a914{hashlib.new('ripemd160', hashlib.sha256(bytes.fromhex(address)).digest()).hex()}88ac")
tx = struct.pack('<L', 1)
tx += b'\x01'
tx += b'\x00'*32
tx += b'\xff'*4
tx += struct.pack('<B', 4) + struct.pack('<L', height)
tx += b'\xff'*4
tx += b'\x01'
tx += struct.pack('<Q', value_sat)
tx += struct.pack('<B', len(script_pubkey))
tx += script_pubkey
tx += b'\x00'*4
return tx.hex()
def calculate_merkle_root(tx_hashes):
if not tx_hashes:
return '0'*64
hashes = [bytes.fromhex(h)[::-1] for h in tx_hashes]
while len(hashes) > 1:
if len(hashes) % 2 != 0:
hashes.append(hashes[-1])
hashes = [double_sha256(hashes[i] + hashes[i+1]) for i in range(0, len(hashes), 2)]
return hashes[0][::-1].hex()
# Database functions with backup system
def load_miners():
if not os.path.exists('miners.json'):
return {}
# Try to load from backup if main file is corrupted
try:
with open('miners.json') as f:
return json.load(f)
except:
try:
shutil.copy('miners.json.bak', 'miners.json')
with open('miners.json') as f:
return json.load(f)
except:
return {}
def save_miners(miners):
with miners_lock:
# Create backup first
if os.path.exists('miners.json'):
shutil.copy('miners.json', 'miners.json.bak')
# Save new data
with open('miners.json', 'w') as f:
json.dump(miners, f, indent=2)
# Mining endpoints with password protection
@app.route('/getwork/<address>', methods=['GET'])
def get_work(address):
password = request.args.get('password')
if password != config['join_password']:
return jsonify({'error': 'Invalid password'}), 401
if not validate_bitcoin_address(address):
return jsonify({'error': 'Invalid Bitcoin address'}), 400
try:
template = rpc.getblocktemplate({
"rules": ["segwit"],
"coinbasetxn": {"mineraddress": address}
})
coinbase_tx = create_coinbase_tx(
template['height'],
template['coinbasevalue'] / 10**8,
address
)
tx_hashes = [coinbase_tx] + [tx['hash'] for tx in template['transactions']]
merkle_root = calculate_merkle_root(tx_hashes)
work = {
'version': template['version'],
'previousblockhash': template['previousblockhash'],
'merkle_root': merkle_root,
'time': template['curtime'],
'bits': template['bits'],
'height': template['height'],
'target': int(template['target'], 16),
'coinbase': coinbase_tx,
'transactions': [tx['data'] for tx in template['transactions']]
}
return jsonify(work)
except JSONRPCException as e:
return jsonify({'error': str(e)}), 500
@app.route('/submit/<address>', methods=['POST'])
def submit_share(address):
password = request.json.get('password')
if password != config['join_password']:
return jsonify({'error': 'Invalid password'}), 401
if not validate_bitcoin_address(address):
return jsonify({'error': 'Invalid Bitcoin address'}), 400
miners = load_miners()
if address not in miners:
miners[address] = {
'shares': 0,
'blocks': [],
'immature_balance': 0.0,
'paid': 0.0,
'last_share': datetime.now().isoformat(),
'first_share': datetime.now().isoformat()
}
data = request.json
if not data or 'nonce' not in data:
return jsonify({'error': 'Invalid submission'}), 400
miners[address]['shares'] += 1
miners[address]['last_share'] = datetime.now().isoformat()
if miners[address]['shares'] % 1000000 == 0:
block_reward = 3.125 * (1 - config['pool_fee'])
miners[address]['blocks'].append({
'height': data.get('height'),
'hash': data.get('hash'),
'value': block_reward,
'status': 'immature',
'timestamp': datetime.now().isoformat()
})
miners[address]['immature_balance'] += block_reward
save_miners(miners)
return jsonify({
'result': 'Block found!',
'reward': block_reward,
'mature_in': config['confirmations_required']
})
save_miners(miners)
return jsonify({'result': 'Share accepted'})
@app.route('/stats/<address>', methods=['GET'])
def miner_stats(address):
password = request.args.get('password')
if password != config['join_password']:
return jsonify({'error': 'Invalid password'}), 401
if not validate_bitcoin_address(address):
return jsonify({'error': 'Invalid Bitcoin address'}), 400
miners = load_miners()
if address not in miners:
return jsonify({'error': 'Miner not found'}), 404
miner = miners[address]
mature_balance = sum(
block['value'] for block in miner['blocks']
if block.get('status') == 'confirmed'
)
return jsonify({
'address': address,
'shares': miner['shares'],
'blocks_found': len(miner['blocks']),
'immature_balance': miner['immature_balance'],
'mature_balance': mature_balance,
'total_paid': miner['paid'],
'last_share': miner['last_share'],
'first_share': miner.get('first_share')
})
# Background services
def payout_processor():
while True:
try:
miners = load_miners()
changed = False
for address, miner in miners.items():
for block in miner['blocks']:
if block['status'] == 'immature':
block_age = (datetime.now() - datetime.fromisoformat(block['timestamp'])).total_seconds()
if block_age > config['payout_interval']:
block['status'] = 'confirmed'
changed = True
if miner['immature_balance'] >= config['min_payout']:
try:
txid = rpc.sendtoaddress(address, miner['immature_balance'])
miner['paid'] += miner['immature_balance']
miner['immature_balance'] = 0
changed = True
print(f"Paid {miner['immature_balance']} BTC to {address} (TXID: {txid})")
except JSONRPCException as e:
print(f"Payout failed for {address}: {e}")
if changed:
save_miners(miners)
except Exception as e:
print(f"Error in payout processor: {e}")
sleep(config['payout_interval'])
def backup_service():
while True:
sleep(config['backup_interval'])
try:
miners = load_miners()
save_miners(miners) # This creates a backup automatically
except Exception as e:
print(f"Backup failed: {e}")
if __name__ == '__main__':
# Initialize database
if not os.path.exists('miners.json'):
save_miners({})
# Start background services
Thread(target=payout_processor, daemon=True).start()
Thread(target=backup_service, daemon=True).start()
# Start pool server
app.run(host='0.0.0.0', port=config['pool_port'])