@@ -1027,6 +1027,128 @@ static struct peer *wallet_peer_load(struct wallet *w, const u64 dbid)
10271027 return peer ;
10281028}
10291029
1030+ void wallet_add_alt_addr (struct db * db ,
1031+ const struct node_id * node_id ,
1032+ const char * alt_addr ,
1033+ bool is_my_alt_addr )
1034+ {
1035+ struct db_stmt * stmt ;
1036+
1037+ if (is_my_alt_addr )
1038+ stmt = db_prepare_v2 (db , SQL ("UPDATE peers"
1039+ " SET my_alt_addr=?"
1040+ " WHERE node_id = ?;" ));
1041+ else
1042+ stmt = db_prepare_v2 (db , SQL ("UPDATE peers"
1043+ " SET peer_alt_addr=?"
1044+ " WHERE node_id = ?;" ));
1045+
1046+ db_bind_text (stmt , alt_addr );
1047+ db_bind_node_id (stmt , node_id );
1048+ db_exec_prepared_v2 (take (stmt ));
1049+ }
1050+
1051+ static struct wireaddr_internal * handle_alt_addr_failure (struct wallet * w ,
1052+ struct db_stmt * stmt ,
1053+ bool transaction_started )
1054+ {
1055+ if (stmt )
1056+ tal_free (stmt );
1057+
1058+ if (transaction_started )
1059+ db_commit_transaction (w -> db );
1060+
1061+ return NULL ;
1062+ }
1063+
1064+ struct wireaddr_internal * wallet_get_alt_addr (struct wallet * w ,
1065+ const struct node_id * node_id ,
1066+ bool use_my_alt_addr )
1067+ {
1068+ struct db_stmt * stmt ;
1069+ struct wireaddr_internal * alt_addrs ;
1070+ bool transaction_started = false;
1071+ const char * addr_column = use_my_alt_addr
1072+ ? "my_alt_addr"
1073+ : "peer_alt_addr" ;
1074+
1075+ if (!db_in_transaction (w -> db )) {
1076+ db_begin_transaction (w -> db );
1077+ transaction_started = true;
1078+ }
1079+
1080+ if (use_my_alt_addr )
1081+ stmt = db_prepare_v2 (w -> db , SQL ("SELECT my_alt_addr"
1082+ " FROM peers"
1083+ " WHERE node_id = ?;" ));
1084+ else
1085+ stmt = db_prepare_v2 (w -> db , SQL ("SELECT peer_alt_addr"
1086+ " FROM peers"
1087+ " WHERE node_id = ?;" ));
1088+
1089+ if (!stmt ) {
1090+ log_debug (w -> log , "Failed to prepare statement for node_id %s" ,
1091+ fmt_node_id (tmpctx , node_id ));
1092+ return handle_alt_addr_failure (w , stmt , transaction_started );
1093+ }
1094+
1095+ db_bind_node_id (stmt , node_id );
1096+ db_query_prepared (stmt );
1097+
1098+ if (!db_step (stmt )) {
1099+ log_debug (w -> log ,
1100+ "No alternative address found for peer %s" ,
1101+ fmt_node_id (tmpctx , node_id ));
1102+ return handle_alt_addr_failure (w , stmt , transaction_started );
1103+ }
1104+
1105+ const char * addr_str = db_col_strdup (tmpctx , stmt , addr_column );
1106+ if (!addr_str ) {
1107+ log_debug (w -> log ,
1108+ "Invalid address string retrieved for peer %s" ,
1109+ fmt_node_id (tmpctx , node_id ));
1110+ return handle_alt_addr_failure (w , stmt , transaction_started );
1111+ }
1112+
1113+ if (* addr_str == '\0' ) {
1114+ log_debug (w -> log ,
1115+ "Empty address string retrieved for peer %s" ,
1116+ fmt_node_id (tmpctx , node_id ));
1117+ return handle_alt_addr_failure (w , stmt , transaction_started );
1118+ }
1119+
1120+ alt_addrs = tal_arr (tmpctx , struct wireaddr_internal , 0 );
1121+ char * * addr_list = tal_strsplit (tmpctx , addr_str , "," , STR_NO_EMPTY );
1122+ for (size_t i = 0 ; addr_list [i ] != NULL ; i ++ ) {
1123+ struct wireaddr_internal * alt_addr = tal (tmpctx ,
1124+ struct wireaddr_internal );
1125+ const char * err = parse_wireaddr_internal (tmpctx ,
1126+ addr_list [i ],
1127+ chainparams_get_ln_port (chainparams ),
1128+ false,
1129+ alt_addr );
1130+ if (err ) {
1131+ log_debug (w -> log ,
1132+ "Invalid alternative address %s for peer %s: %s" ,
1133+ addr_list [i ],
1134+ fmt_node_id (tmpctx , node_id ),
1135+ err );
1136+ tal_free (alt_addr );
1137+ continue ;
1138+ }
1139+ tal_arr_expand (& alt_addrs , * alt_addr );
1140+ }
1141+
1142+ tal_free (addr_list );
1143+ tal_free (addr_str );
1144+ tal_free (stmt );
1145+
1146+ if (transaction_started )
1147+ db_commit_transaction (w -> db );
1148+
1149+ return alt_addrs ;
1150+ }
1151+
10301152static struct bitcoin_signature *
10311153wallet_htlc_sigs_load (const tal_t * ctx , struct wallet * w , u64 channelid ,
10321154 bool option_anchors )
0 commit comments