@@ -13,7 +13,7 @@ use lightning::ln::features::NodeFeatures;
1313use lightning:: ln:: { PaymentHash , PaymentPreimage } ;
1414use serde:: { Deserialize , Serialize } ;
1515use tokio:: sync:: Mutex ;
16- use tonic_lnd:: lnrpc:: { payment:: PaymentStatus , GetInfoRequest , GetInfoResponse } ;
16+ use tonic_lnd:: lnrpc:: { payment:: PaymentStatus , GetInfoRequest } ;
1717use tonic_lnd:: lnrpc:: {
1818 ChannelGraphRequest , ListChannelsRequest , NodeInfoRequest , PaymentFailureReason ,
1919} ;
@@ -41,6 +41,7 @@ pub struct LndConnection {
4141pub struct LndNode {
4242 client : Mutex < Client > ,
4343 info : NodeInfo ,
44+ network : Network ,
4445}
4546
4647// TODO: We could even generalize this to parse any type of Features
@@ -68,29 +69,50 @@ impl LndNode {
6869 . await
6970 . map_err ( |err| LightningError :: ConnectionError ( err. to_string ( ) ) ) ?;
7071
71- let GetInfoResponse {
72- identity_pubkey,
73- features,
74- mut alias,
75- ..
76- } = client
72+ let info = client
7773 . lightning ( )
7874 . get_info ( GetInfoRequest { } )
7975 . await
8076 . map_err ( |err| LightningError :: GetInfoError ( err. to_string ( ) ) ) ?
8177 . into_inner ( ) ;
8278
83- let pubkey = PublicKey :: from_str ( & identity_pubkey)
79+ let mut alias = info. alias ;
80+ let pubkey = PublicKey :: from_str ( & info. identity_pubkey )
8481 . map_err ( |err| LightningError :: GetInfoError ( err. to_string ( ) ) ) ?;
8582 connection. id . validate ( & pubkey, & mut alias) ?;
8683
84+ let network = {
85+ if info. chains . is_empty ( ) {
86+ return Err ( LightningError :: GetInfoError (
87+ "node is not connected any chain" . to_string ( ) ,
88+ ) ) ;
89+ } else if info. chains . len ( ) > 1 {
90+ return Err ( LightningError :: GetInfoError ( format ! (
91+ "node is connected to more than one chain: {:?}" ,
92+ info. chains. iter( ) . map( |c| c. chain. to_string( ) )
93+ ) ) ) ;
94+ }
95+
96+ Network :: from_str ( match info. chains [ 0 ] . network . as_str ( ) {
97+ "mainnet" => "bitcoin" ,
98+ "simnet" => {
99+ return Err ( LightningError :: ValidationError (
100+ "simnet is not supported" . to_string ( ) ,
101+ ) )
102+ } ,
103+ x => x,
104+ } )
105+ . map_err ( |e| LightningError :: GetInfoError ( e. to_string ( ) ) ) ?
106+ } ;
107+
87108 Ok ( Self {
88109 client : Mutex :: new ( client) ,
89110 info : NodeInfo {
90111 pubkey,
91- features : parse_node_features ( features. keys ( ) . cloned ( ) . collect ( ) ) ,
112+ features : parse_node_features ( info . features . keys ( ) . cloned ( ) . collect ( ) ) ,
92113 alias,
93114 } ,
115+ network,
94116 } )
95117 }
96118}
@@ -104,38 +126,8 @@ impl LightningNode for LndNode {
104126 & self . info
105127 }
106128
107- async fn get_network ( & self ) -> Result < Network , LightningError > {
108- let mut client = self . client . lock ( ) . await ;
109- let info = client
110- . lightning ( )
111- . get_info ( GetInfoRequest { } )
112- . await
113- . map_err ( |err| LightningError :: GetInfoError ( err. to_string ( ) ) ) ?
114- . into_inner ( ) ;
115-
116- if info. chains . is_empty ( ) {
117- return Err ( LightningError :: ValidationError ( format ! (
118- "{} is not connected any chain" ,
119- self . get_info( )
120- ) ) ) ;
121- } else if info. chains . len ( ) > 1 {
122- return Err ( LightningError :: ValidationError ( format ! (
123- "{} is connected to more than one chain: {:?}" ,
124- self . get_info( ) ,
125- info. chains. iter( ) . map( |c| c. chain. to_string( ) )
126- ) ) ) ;
127- }
128-
129- Ok ( Network :: from_str ( match info. chains [ 0 ] . network . as_str ( ) {
130- "mainnet" => "bitcoin" ,
131- "simnet" => {
132- return Err ( LightningError :: ValidationError (
133- "simnet is not supported" . to_string ( ) ,
134- ) )
135- } ,
136- x => x,
137- } )
138- . map_err ( |err| LightningError :: ValidationError ( err. to_string ( ) ) ) ?)
129+ fn get_network ( & self ) -> Network {
130+ self . network
139131 }
140132
141133 async fn send_payment (
0 commit comments