@@ -103,6 +103,28 @@ def to_lnd_chanpolicy(self, capacity):
103
103
"min_htlc_msat_specified" : True ,
104
104
}
105
105
106
+ # Create a custom formatter
107
+ class ColorFormatter (logging .Formatter ):
108
+ """Custom formatter to add color based on log level."""
109
+ # Define ANSI color codes
110
+ RED = '\033 [91m'
111
+ YELLOW = '\033 [93m'
112
+ GREEN = '\033 [92m'
113
+ RESET = '\033 [0m'
114
+
115
+ FORMATS = {
116
+ logging .DEBUG : f"{ RESET } %(asctime)s - (name)-8s - Thread-%(thread)d - %(message)s{ RESET } " ,
117
+ logging .INFO : f"{ RESET } %(asctime)s - (name)-8s - %(message)s{ RESET } " ,
118
+ logging .WARNING : f"{ YELLOW } %(asctime)s - (name)-8s - %(message)s{ RESET } " ,
119
+ logging .ERROR : f"{ RED } %(asctime)s - (name)-8s - %(message)s{ RESET } " ,
120
+ logging .CRITICAL : f"{ RED } ##%(asctime)s - (name)-8s - %(message)s##{ RESET } "
121
+ }
122
+
123
+ def format (self , record ):
124
+ log_fmt = self .FORMATS .get (record .levelno )
125
+ formatter = logging .Formatter (log_fmt )
126
+ return formatter .format (record )
127
+
106
128
class LNNode (ABC ):
107
129
@abstractmethod
108
130
def __init__ (self , pod_name ):
@@ -111,8 +133,7 @@ def __init__(self, pod_name):
111
133
# Configure logger if it has no handlers
112
134
if not self .log .handlers :
113
135
handler = logging .StreamHandler ()
114
- formatter = logging .Formatter ("%(asctime)s - %(name)s - %(levelname)s - %(message)s" )
115
- handler .setFormatter (formatter )
136
+ handler .setFormatter (ColorFormatter ())
116
137
self .log .addHandler (handler )
117
138
self .log .setLevel (logging .INFO )
118
139
@@ -165,7 +186,7 @@ def rpc(self, method: str, params: list[str] = [], namespace: Optional[str] = "d
165
186
continue
166
187
return response
167
188
except Exception as e :
168
- self .log .info (f"CLN rpc error: { e } " )
189
+ self .log .error (f"CLN rpc error: { e } , wait and retry... " )
169
190
sleep (2 )
170
191
return None
171
192
@@ -181,7 +202,7 @@ def newaddress(self, max_tries=2):
181
202
if "bech32" in res :
182
203
return True , res ["bech32" ]
183
204
else :
184
- self .log .info (
205
+ self .log .warning (
185
206
f"Couldn't get wallet address from { self .name } :\n { res } \n wait and retry..."
186
207
)
187
208
sleep (2 )
@@ -207,23 +228,21 @@ def walletbalance(self, max_tries=2):
207
228
208
229
def connect (self , target_uri , max_tries = 5 ):
209
230
attempt = 0
210
- self .log .info (f"CLN connect { self .name } to { target_uri } " )
211
231
while attempt < max_tries :
212
232
attempt += 1
213
233
response = self .rpc ("connect" , [target_uri ])
214
234
if response :
215
235
res = json .loads (response )
216
236
if "id" in res :
217
- self .log .debug (f"finished connect response: { response } " )
218
237
return {}
219
238
elif "code" in res and res ["code" ] == 402 :
220
- self .log .info (f"failed connect response : { response } " )
239
+ self .log .warning (f"failed connect 402 : { response } , wait and retry... " )
221
240
sleep (5 )
222
241
else :
223
242
return res
224
243
else :
225
- self .log .debug (f"connect response: { response } " )
226
- sleep (5 )
244
+ self .log .debug (f"connect response: { response } , wait and retry... " )
245
+ sleep (2 )
227
246
return ""
228
247
229
248
def channel (self , pk , capacity , push_amt , fee_rate , max_tries = 5 ):
@@ -240,13 +259,13 @@ def channel(self, pk, capacity, push_amt, fee_rate, max_tries=5):
240
259
if response :
241
260
res = json .loads (response )
242
261
if "txid" in res :
243
- self .log .debug (f"open channel succeeded: { res } " )
244
262
return {"txid" : res ["txid" ], "outpoint" : f'{ res ["txid" ]} :{ res ["outnum" ]} ' }
245
263
else :
246
- self .log .info (f"unable to open channel: { res } " )
264
+ self .log .warning (f"unable to open channel: { res } , wait and retry..." )
265
+ sleep (1 )
247
266
else :
248
- self .log .debug (f"channel response: { response } " )
249
- sleep (5 )
267
+ self .log .debug (f"channel response: { response } , wait and retry... " )
268
+ sleep (2 )
250
269
return ""
251
270
252
271
def graph (self , max_tries = 2 ):
@@ -257,34 +276,25 @@ def graph(self, max_tries=2):
257
276
if response :
258
277
res = json .loads (response )
259
278
if "channels" in res :
260
- return {"edges" : res ["channels" ]}
279
+ # Map to desired output
280
+ filtered_channels = [ch for ch in res ['channels' ] if ch ['direction' ] == 1 ]
281
+ # Sort by short_channel_id - block -> index -> output
282
+ sorted_channels = sorted (filtered_channels , key = lambda x : x ['short_channel_id' ])
283
+ # Add capacity by dividing amount_msat by 1000
284
+ for channel in sorted_channels :
285
+ channel ['capacity' ] = channel ['amount_msat' ] // 1000
286
+ return {'edges' : sorted_channels }
261
287
else :
262
- self .log .info (f"unable to open channel: { res } " )
288
+ self .log .warning (f"unable to open channel: { res } , wait and retry..." )
289
+ sleep (1 )
263
290
else :
264
- self .log .debug (f"channel response: { response } " )
265
- sleep (5 )
291
+ self .log .debug (f"channel response: { response } , wait and retry... " )
292
+ sleep (2 )
266
293
return ""
267
294
268
295
def update (self , txid_hex : str , policy : dict , capacity : int , max_tries = 2 ):
269
- self .log .info ("Channel Policy Updates not supported by CLN yet!" )
296
+ self .log .warning ("Channel Policy Updates not supported by CLN yet!" )
270
297
return
271
- # ln_policy = Policy.from_dict(policy).to_lnd_chanpolicy(capacity)
272
- # data = {"chan_point": {"funding_txid_str": txid_hex, "output_index": 0}, **ln_policy}
273
- # attempt=0
274
- # while attempt < max_tries:
275
- # attempt+=1
276
- # response = self.rpc("setchannel")
277
- # if response:
278
- # res = json.loads(response)
279
- # if "channels" in res:
280
- # print(f"graph succeeded: {res}")
281
- # return {"edges": res["channels"]}
282
- # else:
283
- # print(f"unable to open channel: {res}")
284
- # else:
285
- # print(f"channel response: {response}")
286
- # sleep(5)
287
- # return ""
288
298
289
299
class LND (LNNode ):
290
300
def __init__ (self , pod_name ):
@@ -350,7 +360,7 @@ def newaddress(self, max_tries=10):
350
360
if "address" in res :
351
361
return True , res ["address" ]
352
362
else :
353
- self .log .info (
363
+ self .log .warning (
354
364
f"Couldn't get wallet address from { self .name } :\n { res } \n wait and retry..."
355
365
)
356
366
sleep (1 )
@@ -388,9 +398,8 @@ def channel(self, pk, capacity, push_amt, fee_rate):
388
398
if "result" in res :
389
399
res ["txid" ] = self .b64_to_hex (res ["result" ]["chan_pending" ]["txid" ], reverse = True )
390
400
res ["outpoint" ] = f'{ res ["txid" ]} :{ res ["result" ]["chan_pending" ]["output_index" ]} '
391
- self .log .info (f"LND channel RESPONSE: { res } " )
392
401
except Exception as e :
393
- self .log .info (f"Error opening LND channel: { e } " )
402
+ self .log .error (f"Error opening LND channel: { e } " )
394
403
return res
395
404
396
405
def update (self , txid_hex : str , policy : dict , capacity : int ):
0 commit comments