Repository navigation
Expand file tree
/
Copy pathsalocoind-cli.py
More file actions
272 lines (217 loc) · 7.72 KB
/
Copy pathsalocoind-cli.py
File metadata and controls
272 lines (217 loc) · 7.72 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
#!/usr/bin/env python3
"""
SALOCOIN Daemon CLI
Manage the SALOCOIN node daemon.
Usage:
salocoind start Start the node daemon
salocoind stop Stop the daemon
salocoind status Check daemon status
salocoind getinfo Get blockchain info
salocoind sync Sync blockchain from seed
"""
import sys
import os
import argparse
import json
import time
import signal
import subprocess
# Add project root to path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import config
from core.blockchain import Blockchain
from sync import BlockchainSync
SEED_NODE = "https://api.salocoin.org"
DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
PID_FILE = os.path.join(DATA_DIR, 'salocoind.pid')
SALO = config.COIN_UNIT
def is_daemon_running():
"""Check if daemon is running."""
if not os.path.exists(PID_FILE):
return False
try:
with open(PID_FILE, 'r') as f:
pid = int(f.read().strip())
# Check if process exists (Windows compatible)
if sys.platform == 'win32':
import ctypes
kernel32 = ctypes.windll.kernel32
handle = kernel32.OpenProcess(0x1000, False, pid) # PROCESS_QUERY_LIMITED_INFORMATION
if handle:
kernel32.CloseHandle(handle)
return True
return False
else:
os.kill(pid, 0)
return True
except (OSError, ValueError):
# Process not running, clean up pid file
if os.path.exists(PID_FILE):
os.remove(PID_FILE)
return False
def cmd_start(args):
"""Start the daemon."""
if is_daemon_running():
print("❌ Daemon is already running")
return
os.makedirs(DATA_DIR, exist_ok=True)
print("🚀 Starting SALOCOIN daemon...")
# Start the RPC server in background
if sys.platform == 'win32':
# Windows - use subprocess
script_path = os.path.join(os.path.dirname(__file__), 'salocoind.py')
proc = subprocess.Popen(
[sys.executable, script_path, '--daemon'],
creationflags=subprocess.CREATE_NO_WINDOW,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
# Save PID
with open(PID_FILE, 'w') as f:
f.write(str(proc.pid))
time.sleep(1)
print(f"✓ Daemon started (PID: {proc.pid})")
print(f" RPC server: http://localhost:{config.RPC_PORT}")
else:
# Unix - use fork
pid = os.fork()
if pid == 0:
# Child process
os.setsid()
# Run daemon
from rpc.server import RPCServer
blockchain = Blockchain(data_dir=DATA_DIR)
syncer = BlockchainSync(blockchain, seed_url=SEED_NODE)
syncer.start()
server = RPCServer(blockchain)
server.start()
else:
# Parent process
with open(PID_FILE, 'w') as f:
f.write(str(pid))
time.sleep(1)
print(f"✓ Daemon started (PID: {pid})")
print(f" RPC server: http://localhost:{config.RPC_PORT}")
def cmd_stop(args):
"""Stop the daemon."""
if not is_daemon_running():
print("❌ Daemon is not running")
return
try:
with open(PID_FILE, 'r') as f:
pid = int(f.read().strip())
print(f"Stopping daemon (PID: {pid})...")
if sys.platform == 'win32':
import ctypes
kernel32 = ctypes.windll.kernel32
handle = kernel32.OpenProcess(1, False, pid) # PROCESS_TERMINATE
if handle:
kernel32.TerminateProcess(handle, 0)
kernel32.CloseHandle(handle)
else:
os.kill(pid, signal.SIGTERM)
os.remove(PID_FILE)
print("✓ Daemon stopped")
except Exception as e:
print(f"❌ Failed to stop daemon: {e}")
def cmd_status(args):
"""Check daemon status."""
print("\n🔍 SALOCOIN Daemon Status")
print("═" * 50)
if is_daemon_running():
with open(PID_FILE, 'r') as f:
pid = f.read().strip()
print(f" Status: ✓ Running (PID: {pid})")
else:
print(f" Status: ✗ Not running")
# Check local blockchain
if os.path.exists(DATA_DIR):
blockchain = Blockchain(data_dir=DATA_DIR)
print(f" Local Height: {blockchain.get_height()}")
else:
print(f" Local Height: 0 (no data)")
# Check seed node
try:
blockchain = Blockchain(data_dir=DATA_DIR) if os.path.exists(DATA_DIR) else Blockchain()
syncer = BlockchainSync(blockchain, seed_url=SEED_NODE)
status = syncer.get_status()
if status:
print(f" Network Height: {status.get('height', 0)}")
print(f" Seed Node: ✓ Online")
else:
print(f" Seed Node: ✗ Offline")
except:
print(f" Seed Node: ✗ Unreachable")
print()
def cmd_getinfo(args):
"""Get blockchain info."""
os.makedirs(DATA_DIR, exist_ok=True)
blockchain = Blockchain(data_dir=DATA_DIR)
latest = blockchain.get_latest_block()
info = {
"version": config.PROTOCOL_VERSION,
"network": config.NETWORK_NAME,
"height": blockchain.get_height(),
"difficulty": blockchain.current_difficulty,
"best_block": latest.hash if latest else None,
"block_reward": config.calculate_block_reward(blockchain.get_height() + 1) / SALO,
"total_supply": sum(
config.calculate_block_reward(h) for h in range(1, blockchain.get_height() + 1)
) / SALO,
"mempool_size": len(blockchain.mempool.transactions),
"connections": 0, # TODO: implement peer tracking
}
print("\n📊 SALOCOIN Blockchain Info")
print("═" * 50)
for key, value in info.items():
print(f" {key}: {value}")
print()
def cmd_sync(args):
"""Sync blockchain from seed."""
os.makedirs(DATA_DIR, exist_ok=True)
blockchain = Blockchain(data_dir=DATA_DIR)
print(f"\n🔄 Syncing from {SEED_NODE}...")
try:
syncer = BlockchainSync(blockchain, seed_url=SEED_NODE)
status = syncer.get_status()
if not status:
print("❌ Seed node not reachable")
return
print(f" Local height: {blockchain.get_height()}")
print(f" Network height: {status.get('height', 0)}")
syncer.sync_from_seed()
blockchain.save()
print(f"\n✓ Synced to height {blockchain.get_height()}")
except Exception as e:
print(f"❌ Sync failed: {e}")
def main():
parser = argparse.ArgumentParser(
description='SALOCOIN Daemon CLI',
formatter_class=argparse.RawDescriptionHelpFormatter
)
subparsers = parser.add_subparsers(dest='command', help='Commands')
# start
subparsers.add_parser('start', help='Start the daemon')
# stop
subparsers.add_parser('stop', help='Stop the daemon')
# status
subparsers.add_parser('status', help='Check daemon status')
# getinfo
subparsers.add_parser('getinfo', help='Get blockchain info')
# sync
subparsers.add_parser('sync', help='Sync blockchain from seed')
args = parser.parse_args()
if args.command == 'start':
cmd_start(args)
elif args.command == 'stop':
cmd_stop(args)
elif args.command == 'status':
cmd_status(args)
elif args.command == 'getinfo':
cmd_getinfo(args)
elif args.command == 'sync':
cmd_sync(args)
else:
parser.print_help()
if __name__ == '__main__':
main()