Skip to content

Commit 5a9c205

Browse files
committed
wallet: handle storing and reveiving alt_addrs from db
1 parent 183ca77 commit 5a9c205

File tree

5 files changed

+157
-0
lines changed

5 files changed

+157
-0
lines changed

lightningd/channel_control.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,6 +1432,8 @@ static void handle_channeld_my_alt_addr(struct lightningd *ld, struct channel *c
14321432
return;
14331433
}
14341434

1435+
wallet_add_alt_addr(ld->wallet->db, &channel->peer->id,
1436+
(char *)my_alt_addr, true);
14351437
tal_free(my_alt_addr);
14361438
}
14371439

lightningd/connect_control.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ static void handle_peer_alt_addr(struct lightningd *ld, const u8 *msg)
565565
return;
566566
}
567567

568+
wallet_add_alt_addr(ld->wallet->db, &p_id, (char *)p_alt_addr, false);
568569
tal_free(p_alt_addr);
569570
}
570571

wallet/db.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,8 @@ static struct migration dbmigrations[] = {
10211021
{SQL("ALTER TABLE channels ADD remote_htlc_minimum_msat BIGINT DEFAULT NULL;"), NULL},
10221022
{SQL("ALTER TABLE channels ADD last_stable_connection BIGINT DEFAULT 0;"), NULL},
10231023
{NULL, migrate_initialize_alias_local},
1024+
{SQL("ALTER TABLE peers ADD COLUMN peer_alt_addr TEXT DEFAULT '';"), NULL},
1025+
{SQL("ALTER TABLE peers ADD COLUMN my_alt_addr TEXT DEFAULT '';"), NULL},
10241026
};
10251027

10261028
/**

wallet/wallet.c

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
10301152
static struct bitcoin_signature *
10311153
wallet_htlc_sigs_load(const tal_t *ctx, struct wallet *w, u64 channelid,
10321154
bool option_anchors)

wallet/wallet.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,4 +1797,34 @@ void wallet_remove_local_anchors(struct wallet *w,
17971797
struct local_anchor_info *wallet_get_local_anchors(const tal_t *ctx,
17981798
struct wallet *w,
17991799
u64 channel_id);
1800+
1801+
/**
1802+
* wallet_add_alt_addr - Update the alternative address for a peer connection
1803+
* @db: the database
1804+
* @node_id: the ID of the node
1805+
* @alt_addr: the alternative address to set
1806+
* @is_our_addr: flag to update `my_alt_addr` or `peer_alt_addr`
1807+
*
1808+
* This function updates the `my_alt_addr` or `peer_alt_addr` field of a peer
1809+
* in the database based on the value of the `is_our_addr` flag. If `is_our_addr`
1810+
* is true, it updates the `my_alt_addr` field; otherwise, it updates the
1811+
* `peer_alt_addr` field. If `alt_addr` is NULL, it just returns.
1812+
*/
1813+
void wallet_add_alt_addr(struct db *db, const struct node_id *node_id,
1814+
const char *alt_addr, bool is_my_alt_addr);
1815+
1816+
1817+
/**
1818+
* wallet_get_alt_addr - Retrieve alternative connection addresses
1819+
* @w: the wallet containing the database
1820+
* @node_id: the ID of the node whose alternative addresses is to be retrieved
1821+
* @use_my_alt_addr: a boolean flag to use `my_alt_addr` or `peer_alt_addr`
1822+
*
1823+
* Returns: A pointer to the `wireaddr_internal` struct containing alternative
1824+
* addresses, or NULL if no valid alternative address is found.
1825+
*/
1826+
struct wireaddr_internal *wallet_get_alt_addr(struct wallet *w,
1827+
const struct node_id *node_id,
1828+
bool use_my_alt_addr);
1829+
18001830
#endif /* LIGHTNING_WALLET_WALLET_H */

0 commit comments

Comments
 (0)