-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproteus_node_monolith.py
More file actions
360 lines (294 loc) · 12 KB
/
proteus_node_monolith.py
File metadata and controls
360 lines (294 loc) · 12 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/usr/bin/env python3
"""
Proteus Secondary Node Monolith
A simple, self-contained node implementation for testing multi-node functionality
"""
import os
import sys
import json
import time
import logging
import requests
import threading
import websocket
from datetime import datetime, timezone
from eth_account import Account
from web3 import Web3
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger('proteus_node')
class ProteusNode:
"""Simple secondary node implementation"""
def __init__(self):
# Node identity
self.node_id = os.getenv('NODE_ID', f'node_{int(time.time())}')
self.node_name = os.getenv('NODE_NAME', 'Secondary Node')
# Network configuration
self.primary_node_url = os.getenv('PRIMARY_NODE_URL', 'http://localhost:5000')
self.ws_url = os.getenv('WS_URL', 'ws://localhost:5001')
# Blockchain configuration
self.rpc_url = os.getenv('BASE_RPC_URL', 'https://sepolia.base.org')
self.chain_id = int(os.getenv('CHAIN_ID', '84532'))
# Node wallet
self.private_key = os.getenv('NODE_PRIVATE_KEY')
self.address = os.getenv('NODE_ADDRESS')
# Contract addresses
self.contracts = {
'prediction_market': os.getenv('PREDICTION_MARKET_ADDRESS'),
'oracle': os.getenv('ORACLE_CONTRACT_ADDRESS'),
'node_registry': os.getenv('NODE_REGISTRY_ADDRESS'),
'payout_manager': os.getenv('PAYOUT_MANAGER_ADDRESS')
}
# Initialize Web3
self.w3 = Web3(Web3.HTTPProvider(self.rpc_url))
# Node state
self.is_registered = False
self.is_active = False
self.consensus_proposals = {}
self.health_metrics = {
'uptime': 0,
'last_heartbeat': None,
'consensus_participation': 0,
'oracle_submissions': 0
}
# WebSocket connection
self.ws = None
self.ws_connected = False
def initialize(self):
"""Initialize the node"""
logger.info(f"Initializing {self.node_name} ({self.node_id})")
# Verify wallet
if not self.private_key or not self.address:
logger.error("Node wallet not configured")
return False
# Verify blockchain connection
try:
block_number = self.w3.eth.block_number
logger.info(f"Connected to BASE Sepolia at block {block_number}")
except Exception as e:
logger.error(f"Failed to connect to blockchain: {e}")
return False
# Check node registration
self.check_registration()
return True
def check_registration(self):
"""Check if node is registered on-chain"""
try:
# Check with primary node API
response = requests.get(f"{self.primary_node_url}/api/nodes/{self.address}")
if response.status_code == 200:
data = response.json()
self.is_registered = data.get('is_registered', False)
self.is_active = data.get('is_active', False)
logger.info(f"Node registration status: registered={self.is_registered}, active={self.is_active}")
else:
logger.warning("Could not check registration status")
except Exception as e:
logger.error(f"Error checking registration: {e}")
def register_node(self):
"""Register this node with the network"""
if self.is_registered:
logger.info("Node already registered")
return True
try:
# Register via primary node API
data = {
'address': self.address,
'name': self.node_name,
'node_id': self.node_id,
'endpoint': f"http://{self.node_id}:5000"
}
response = requests.post(
f"{self.primary_node_url}/api/nodes/register",
json=data
)
if response.status_code == 200:
logger.info("Node registration submitted")
self.is_registered = True
return True
else:
logger.error(f"Registration failed: {response.text}")
return False
except Exception as e:
logger.error(f"Error registering node: {e}")
return False
def connect_websocket(self):
"""Connect to primary node via WebSocket"""
def on_message(ws, message):
try:
data = json.loads(message)
self.handle_ws_message(data)
except Exception as e:
logger.error(f"Error handling WebSocket message: {e}")
def on_error(ws, error):
logger.error(f"WebSocket error: {error}")
def on_close(ws, close_status_code, close_msg):
logger.info("WebSocket connection closed")
self.ws_connected = False
def on_open(ws):
logger.info("WebSocket connection established")
self.ws_connected = True
# Send node identification
self.send_ws_message({
'type': 'node_identify',
'node_id': self.node_id,
'address': self.address
})
try:
self.ws = websocket.WebSocketApp(
self.ws_url,
on_message=on_message,
on_error=on_error,
on_close=on_close,
on_open=on_open
)
# Run WebSocket in separate thread
ws_thread = threading.Thread(target=self.ws.run_forever)
ws_thread.daemon = True
ws_thread.start()
except Exception as e:
logger.error(f"Failed to connect WebSocket: {e}")
def send_ws_message(self, message):
"""Send message via WebSocket"""
if self.ws and self.ws_connected:
try:
self.ws.send(json.dumps(message))
except Exception as e:
logger.error(f"Error sending WebSocket message: {e}")
def handle_ws_message(self, data):
"""Handle incoming WebSocket messages"""
msg_type = data.get('type')
if msg_type == 'consensus_proposal':
self.handle_consensus_proposal(data)
elif msg_type == 'oracle_request':
self.handle_oracle_request(data)
elif msg_type == 'heartbeat_request':
self.send_heartbeat()
else:
logger.debug(f"Received message type: {msg_type}")
def handle_consensus_proposal(self, data):
"""Handle consensus proposal from network"""
proposal_id = data.get('proposal_id')
proposal_type = data.get('proposal_type')
data.get('data')
logger.info(f"Received consensus proposal {proposal_id} of type {proposal_type}")
# Simple voting logic - vote yes for now
vote = True
# Send vote
self.send_ws_message({
'type': 'consensus_vote',
'proposal_id': proposal_id,
'vote': vote,
'node_id': self.node_id
})
self.health_metrics['consensus_participation'] += 1
def handle_oracle_request(self, data):
"""Handle oracle data request"""
market_id = data.get('market_id')
logger.info(f"Received oracle request for market {market_id}")
# In a real implementation, would fetch and validate data
# For now, acknowledge receipt
self.send_ws_message({
'type': 'oracle_acknowledge',
'market_id': market_id,
'node_id': self.node_id
})
def send_heartbeat(self):
"""Send heartbeat to network"""
self.health_metrics['last_heartbeat'] = datetime.now(timezone.utc).isoformat()
heartbeat_data = {
'type': 'heartbeat',
'node_id': self.node_id,
'address': self.address,
'timestamp': self.health_metrics['last_heartbeat'],
'metrics': {
'uptime': self.health_metrics['uptime'],
'consensus_participation': self.health_metrics['consensus_participation'],
'oracle_submissions': self.health_metrics['oracle_submissions']
}
}
# Send via WebSocket
self.send_ws_message(heartbeat_data)
# Also send via HTTP API
try:
response = requests.post(
f"{self.primary_node_url}/api/nodes/heartbeat",
json=heartbeat_data
)
if response.status_code == 200:
logger.debug("Heartbeat sent successfully")
except Exception as e:
logger.error(f"Error sending heartbeat: {e}")
def monitor_contracts(self):
"""Monitor smart contracts for events"""
logger.info("Starting contract monitoring")
while True:
try:
# Get latest block
# Check for events (simplified)
# In real implementation, would use event filters
time.sleep(30) # Check every 30 seconds
except Exception as e:
logger.error(f"Error monitoring contracts: {e}")
time.sleep(60)
def run_health_monitor(self):
"""Run health monitoring loop"""
start_time = time.time()
while True:
try:
# Update uptime
self.health_metrics['uptime'] = int(time.time() - start_time)
# Send heartbeat every 60 seconds
self.send_heartbeat()
# Check registration status
if not self.is_registered:
self.check_registration()
time.sleep(60)
except Exception as e:
logger.error(f"Error in health monitor: {e}")
time.sleep(60)
def run(self):
"""Main node execution loop"""
logger.info(f"Starting {self.node_name}")
# Initialize node
if not self.initialize():
logger.error("Failed to initialize node")
return
# Register node if needed
if not self.is_registered:
self.register_node()
# Connect WebSocket
self.connect_websocket()
# Start monitoring threads
health_thread = threading.Thread(target=self.run_health_monitor)
health_thread.daemon = True
health_thread.start()
contract_thread = threading.Thread(target=self.monitor_contracts)
contract_thread.daemon = True
contract_thread.start()
logger.info("Node is running. Press Ctrl+C to stop.")
# Keep main thread alive
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
logger.info("Shutting down node...")
if self.ws:
self.ws.close()
if __name__ == "__main__":
# Generate wallet if not exists
if not os.getenv('NODE_PRIVATE_KEY'):
logger.info("Generating new node wallet...")
account = Account.create()
logger.info(f"Generated wallet address: {account.address}")
logger.warning("No NODE_PRIVATE_KEY set. Generated ephemeral key — set NODE_PRIVATE_KEY env var for persistence.")
sys.exit(1)
# Create and run node
node = ProteusNode()
node.run()