-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock_processor.py
More file actions
executable file
·231 lines (202 loc) · 8.18 KB
/
Copy pathblock_processor.py
File metadata and controls
executable file
·231 lines (202 loc) · 8.18 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
import os
import sys
import time
import json
import signal
import logging
import requests
from pathlib import Path
from typing import Optional, Dict, Tuple
from datetime import datetime
# Custom JSON Formatter
class JsonFormatter(logging.Formatter):
def format(self, record):
if isinstance(record.msg, dict):
json_record = record.msg
else:
json_record = {'message': record.msg}
json_record.update({
'timestamp': self.formatTime(record),
'level': record.levelname
})
return json.dumps(json_record)
# Set up JSON logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
handler.setFormatter(JsonFormatter())
logger.handlers = [handler]
class BlockExplorer:
def __init__(self):
self.storage_path = Path(os.getenv('STORAGE_PATH', '/data'))
self.tendermint_url = os.getenv('TM_NODE')
self.blocks_file = self.storage_path / 'blocks.csv'
self.last_processed_height = 1
self.running = True
# Ensure storage directory exists
self.storage_path.mkdir(parents=True, exist_ok=True)
# Check if we can acquire the lock
if not self._check_and_acquire_lock():
logger.info({
"event": "init",
"message": "Exiting due to existing processor"
})
sys.exit(0)
# Initialize or read existing blocks file
self._init_blocks_file()
# Setup signal handlers
signal.signal(signal.SIGTERM, self.handle_shutdown)
signal.signal(signal.SIGINT, self.handle_shutdown)
def _check_and_acquire_lock(self):
"""Check if another processor is running and acquire lock if not"""
self.state_file = self.storage_path / 'processor.state'
if self.state_file.exists():
with open(self.state_file, 'r') as f:
state = f.read().strip()
if state == "processing":
logger.info({
"event": "init",
"message": "Another processor is running, exiting"
})
return False
with open(self.state_file, 'w') as f:
f.write("processing\n")
return True
def _init_blocks_file(self):
"""Initialize blocks.csv file and get last processed height"""
if not self.blocks_file.exists():
# Create file with header
with open(self.blocks_file, 'w') as f:
f.write("height,hash\n")
logger.info({
"event": "init",
"message": "Created new blocks.csv file"
})
else:
# Read last processed height
try:
with open(self.blocks_file, 'r') as f:
lines = f.readlines()
if len(lines) > 1: # If we have data beyond header
last_line = lines[-1].strip()
self.last_processed_height = int(last_line.split(',')[0])
logger.info({
"event": "init",
"message": "Found existing blocks.csv",
"last_height": self.last_processed_height
})
except Exception as e:
logger.error({
"event": "error",
"message": "Error reading blocks.csv",
"error": str(e)
})
def handle_shutdown(self, signum, frame):
"""Handle shutdown signals gracefully"""
logger.info({
"event": "shutdown_initiated",
"message": f"Received signal {signum}, initiating graceful shutdown",
"last_processed_height": self.last_processed_height
})
# Remove state file on shutdown
if hasattr(self, 'state_file') and self.state_file.exists():
self.state_file.unlink()
self.running = False
def get_block(self, height: Optional[int] = None) -> Tuple[Optional[int], Optional[str]]:
"""Get block information from Tendermint node"""
try:
url = f"{self.tendermint_url}/block"
if height is not None:
url = f"{url}?height={height}"
logger.info({
"event": "request",
"message": "Making request to tendermint",
"url": url
})
response = requests.get(url, timeout=5)
response.raise_for_status()
data = response.json()
block_height = int(data['result']['block']['header']['height'])
block_hash = data['result']['block_id']['hash']
return block_height, block_hash
except Exception as e:
logger.error({
"event": "error",
"message": "Error fetching block",
"height": height,
"error": str(e)
})
return None, None
def append_block(self, height: int, block_hash: str) -> bool:
"""Append block information to blocks.csv"""
try:
with open(self.blocks_file, 'a') as f:
f.write(f"{height},{block_hash}\n")
self.last_processed_height = height
return True
except Exception as e:
logger.error({
"event": "error",
"message": "Error appending block",
"height": height,
"error": str(e)
})
return False
def process_missing_blocks(self, current_height: int):
"""Process any missing blocks between last processed and current"""
if self.last_processed_height < current_height:
for height in range(self.last_processed_height + 1, current_height + 1):
if not self.running:
logger.info({
"event": "shutdown_processing",
"message": "Stopping block processing due to shutdown signal",
"last_height": self.last_processed_height
})
return
block_height, block_hash = self.get_block(height)
if block_height is not None and block_hash is not None:
if self.append_block(block_height, block_hash):
logger.info({
"event": "block_processed",
"height": block_height,
"hash": block_hash
})
else:
# If we fail to append, stop processing to maintain linearity
break
else:
# If we can't get a block, stop processing to maintain linearity
break
def run(self):
"""Main processing loop"""
logger.info({
"event": "startup",
"message": "Starting block explorer",
"tendermint_url": self.tendermint_url,
"storage_path": str(self.storage_path)
})
while self.running:
try:
# Get latest block
current_height, current_hash = self.get_block()
if current_height is not None:
# Process any missing blocks
self.process_missing_blocks(current_height)
# Wait a bit before next check
time.sleep(1)
except Exception as e:
logger.error({
"event": "error",
"message": "Error in main loop",
"error": str(e)
})
time.sleep(5)
# Shutdown sequence
logger.info({
"event": "shutdown_complete",
"message": "Block explorer shutdown complete",
"final_height": self.last_processed_height
})
if __name__ == "__main__":
explorer = BlockExplorer()
explorer.run()