Fred808 commited on
Commit
cf2ff2e
·
verified ·
1 Parent(s): 125b78e

Update parallel_miner_v3.py

Browse files
Files changed (1) hide show
  1. parallel_miner_v3.py +29 -10
parallel_miner_v3.py CHANGED
@@ -157,9 +157,9 @@ class NetworkIntegration:
157
  def __init__(self, wallet_address: str = None):
158
  # Multiple API endpoints for redundancy
159
  self.api_endpoints = [
 
160
  "https://blockchain.info",
161
- "https://api.blockchain.com/v3/btc",
162
- "https://api.blockchair.com/bitcoin",
163
  ]
164
  self.api_base = self.api_endpoints[0] # Default endpoint
165
  self.node = "seed.bitcoin.sipa.be"
@@ -169,9 +169,17 @@ class NetworkIntegration:
169
  self._template_cache = None
170
  self._last_cache_time = 0
171
 
 
 
 
 
 
 
 
 
 
172
  def connect(self) -> bool:
173
  """Connect to Bitcoin mainnet with fallback endpoints"""
174
- import requests
175
  import socket
176
 
177
  # First check internet connectivity
@@ -186,13 +194,20 @@ class NetworkIntegration:
186
  for endpoint in self.api_endpoints:
187
  try:
188
  self.api_base = endpoint
189
- response = requests.get(f"{endpoint}/blockchain/blocks/last", timeout=10)
 
 
 
 
 
 
 
190
  if response.status_code == 200:
191
  self.connected = True
192
  logging.info(f"✅ Connected to Bitcoin mainnet via {endpoint}")
193
  return True
194
- except requests.RequestException as e:
195
- logging.warning(f"Failed to connect to {endpoint}: {e}")
196
  continue
197
 
198
  logging.error("❌ Failed to connect to all available endpoints")
@@ -209,14 +224,18 @@ class NetworkIntegration:
209
  if self._template_cache and current_time - self._last_cache_time < 300:
210
  return self._template_cache
211
 
212
- # Get latest block info
213
- response = requests.get("https://blockchain.info/latestblock", timeout=10)
 
 
 
 
214
  if response.status_code != 200:
215
  raise Exception(f"Failed to get latest block: {response.status_code}")
216
 
217
  latest = response.json()
218
- height = latest['height']
219
- current_block = latest['hash']
220
 
221
  logging.info(f"📦 Current block height: {height}, hash: {current_block}")
222
 
 
157
  def __init__(self, wallet_address: str = None):
158
  # Multiple API endpoints for redundancy
159
  self.api_endpoints = [
160
+ "https://api.blockchain.com/v3",
161
  "https://blockchain.info",
162
+ "https://blockchair.com/api/stats",
 
163
  ]
164
  self.api_base = self.api_endpoints[0] # Default endpoint
165
  self.node = "seed.bitcoin.sipa.be"
 
169
  self._template_cache = None
170
  self._last_cache_time = 0
171
 
172
+ # Configure requests session with SSL verification and proper headers
173
+ import requests
174
+ self.session = requests.Session()
175
+ self.session.verify = True # Enable SSL verification
176
+ self.session.headers.update({
177
+ 'User-Agent': 'Mozilla/5.0 Bitcoin Miner',
178
+ 'Accept': 'application/json'
179
+ })
180
+
181
  def connect(self) -> bool:
182
  """Connect to Bitcoin mainnet with fallback endpoints"""
 
183
  import socket
184
 
185
  # First check internet connectivity
 
194
  for endpoint in self.api_endpoints:
195
  try:
196
  self.api_base = endpoint
197
+ if 'api.blockchain.com' in endpoint:
198
+ test_url = f"{endpoint}/blocks/latest"
199
+ elif 'blockchain.info' in endpoint:
200
+ test_url = f"{endpoint}/latestblock"
201
+ else:
202
+ test_url = endpoint
203
+
204
+ response = self.session.get(test_url, timeout=10)
205
  if response.status_code == 200:
206
  self.connected = True
207
  logging.info(f"✅ Connected to Bitcoin mainnet via {endpoint}")
208
  return True
209
+ except Exception as e:
210
+ logging.warning(f"Failed to connect to {endpoint}: {str(e)}")
211
  continue
212
 
213
  logging.error("❌ Failed to connect to all available endpoints")
 
224
  if self._template_cache and current_time - self._last_cache_time < 300:
225
  return self._template_cache
226
 
227
+ # Get latest block info based on endpoint
228
+ if 'api.blockchain.com' in self.api_base:
229
+ response = self.session.get(f"{self.api_base}/blocks/latest", timeout=10)
230
+ else:
231
+ response = self.session.get("https://blockchain.info/latestblock", timeout=10)
232
+
233
  if response.status_code != 200:
234
  raise Exception(f"Failed to get latest block: {response.status_code}")
235
 
236
  latest = response.json()
237
+ height = latest.get('height') or latest.get('block_index')
238
+ current_block = latest.get('hash') or latest.get('block_hash')
239
 
240
  logging.info(f"📦 Current block height: {height}, hash: {current_block}")
241