Skip to content

Commit 9a3a0d1

Browse files
committed
Use text instead of blobs for Sqlite closed_channels table
We're using blobs for the `local_channels` table for backwards-compat and performance reasons, but it's easier to use text for the new `closed_channels` table to perform analysis on past data.
1 parent 6722331 commit 9a3a0d1

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

eclair-core/src/main/scala/fr/acinq/eclair/db/sqlite/SqliteChannelsDb.scala

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class SqliteChannelsDb(val sqlite: Connection) extends ChannelsDb with Logging {
6464
statement.executeUpdate("CREATE INDEX htlc_infos_channel_id_idx ON htlc_infos(channel_id)")
6565
statement.executeUpdate("CREATE INDEX htlc_infos_commitment_number_idx ON htlc_infos(commitment_number)")
6666
// We can now move closed channels to a dedicated table.
67-
statement.executeUpdate("CREATE TABLE closed_channels (channel_id BLOB NOT NULL PRIMARY KEY, remote_node_id BLOB NOT NULL, funding_txid BLOB NOT NULL, funding_output_index INTEGER NOT NULL, funding_tx_index INTEGER NOT NULL, funding_key_path TEXT NOT NULL, channel_features TEXT NOT NULL, is_channel_opener BOOLEAN NOT NULL, commitment_format TEXT NOT NULL, announced BOOLEAN NOT NULL, capacity_satoshis INTEGER NOT NULL, closing_txid BLOB NOT NULL, closing_type TEXT NOT NULL, closing_script BLOB NOT NULL, local_balance_msat INTEGER NOT NULL, remote_balance_msat INTEGER NOT NULL, closing_amount_satoshis INTEGER NOT NULL, created_at INTEGER NOT NULL, closed_at INTEGER NOT NULL)")
67+
statement.executeUpdate("CREATE TABLE closed_channels (channel_id TEXT NOT NULL PRIMARY KEY, remote_node_id TEXT NOT NULL, funding_txid TEXT NOT NULL, funding_output_index INTEGER NOT NULL, funding_tx_index INTEGER NOT NULL, funding_key_path TEXT NOT NULL, channel_features TEXT NOT NULL, is_channel_opener BOOLEAN NOT NULL, commitment_format TEXT NOT NULL, announced BOOLEAN NOT NULL, capacity_satoshis INTEGER NOT NULL, closing_txid TEXT NOT NULL, closing_type TEXT NOT NULL, closing_script TEXT NOT NULL, local_balance_msat INTEGER NOT NULL, remote_balance_msat INTEGER NOT NULL, closing_amount_satoshis INTEGER NOT NULL, created_at INTEGER NOT NULL, closed_at INTEGER NOT NULL)")
6868
statement.executeUpdate("CREATE INDEX closed_channels_remote_node_id_idx ON closed_channels(remote_node_id)")
6969
// We migrate closed channels from the local_channels table to the new closed_channels table, whenever possible.
7070
val insertStatement = sqlite.prepareStatement("INSERT INTO closed_channels VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
@@ -97,9 +97,9 @@ class SqliteChannelsDb(val sqlite: Connection) extends ChannelsDb with Logging {
9797
}
9898
data_opt match {
9999
case Some(data) =>
100-
insertStatement.setBytes(1, channelId.toArray)
101-
insertStatement.setBytes(2, data.remoteNodeId.value.toArray)
102-
insertStatement.setBytes(3, data.fundingTxId.value.toArray)
100+
insertStatement.setString(1, channelId.toHex)
101+
insertStatement.setString(2, data.remoteNodeId.toHex)
102+
insertStatement.setString(3, data.fundingTxId.value.toHex)
103103
insertStatement.setLong(4, data.fundingOutputIndex)
104104
insertStatement.setLong(5, data.fundingTxIndex)
105105
insertStatement.setString(6, data.fundingKeyPath)
@@ -108,9 +108,9 @@ class SqliteChannelsDb(val sqlite: Connection) extends ChannelsDb with Logging {
108108
insertStatement.setString(9, data.commitmentFormat)
109109
insertStatement.setBoolean(10, data.announced)
110110
insertStatement.setLong(11, data.capacity.toLong)
111-
insertStatement.setBytes(12, data.closingTxId.value.toArray)
111+
insertStatement.setString(12, data.closingTxId.value.toHex)
112112
insertStatement.setString(13, data.closingType)
113-
insertStatement.setBytes(14, data.closingScript.toArray)
113+
insertStatement.setString(14, data.closingScript.toHex)
114114
insertStatement.setLong(15, data.localBalance.toLong)
115115
insertStatement.setLong(16, data.remoteBalance.toLong)
116116
insertStatement.setLong(17, data.closingAmount.toLong)
@@ -138,7 +138,7 @@ class SqliteChannelsDb(val sqlite: Connection) extends ChannelsDb with Logging {
138138
getVersion(statement, DB_NAME) match {
139139
case None =>
140140
statement.executeUpdate("CREATE TABLE local_channels (channel_id BLOB NOT NULL PRIMARY KEY, data BLOB NOT NULL, created_timestamp INTEGER, last_payment_sent_timestamp INTEGER, last_payment_received_timestamp INTEGER, last_connected_timestamp INTEGER)")
141-
statement.executeUpdate("CREATE TABLE closed_channels (channel_id BLOB NOT NULL PRIMARY KEY, remote_node_id BLOB NOT NULL, funding_txid BLOB NOT NULL, funding_output_index INTEGER NOT NULL, funding_tx_index INTEGER NOT NULL, funding_key_path TEXT NOT NULL, channel_features TEXT NOT NULL, is_channel_opener BOOLEAN NOT NULL, commitment_format TEXT NOT NULL, announced BOOLEAN NOT NULL, capacity_satoshis INTEGER NOT NULL, closing_txid BLOB NOT NULL, closing_type TEXT NOT NULL, closing_script BLOB NOT NULL, local_balance_msat INTEGER NOT NULL, remote_balance_msat INTEGER NOT NULL, closing_amount_satoshis INTEGER NOT NULL, created_at INTEGER NOT NULL, closed_at INTEGER NOT NULL)")
141+
statement.executeUpdate("CREATE TABLE closed_channels (channel_id TEXT NOT NULL PRIMARY KEY, remote_node_id TEXT NOT NULL, funding_txid TEXT NOT NULL, funding_output_index INTEGER NOT NULL, funding_tx_index INTEGER NOT NULL, funding_key_path TEXT NOT NULL, channel_features TEXT NOT NULL, is_channel_opener BOOLEAN NOT NULL, commitment_format TEXT NOT NULL, announced BOOLEAN NOT NULL, capacity_satoshis INTEGER NOT NULL, closing_txid TEXT NOT NULL, closing_type TEXT NOT NULL, closing_script TEXT NOT NULL, local_balance_msat INTEGER NOT NULL, remote_balance_msat INTEGER NOT NULL, closing_amount_satoshis INTEGER NOT NULL, created_at INTEGER NOT NULL, closed_at INTEGER NOT NULL)")
142142
statement.executeUpdate("CREATE TABLE htlc_infos (channel_id BLOB NOT NULL, commitment_number INTEGER NOT NULL, payment_hash BLOB NOT NULL, cltv_expiry INTEGER NOT NULL)")
143143
statement.executeUpdate("CREATE TABLE htlc_infos_to_remove (channel_id BLOB NOT NULL PRIMARY KEY, before_commitment_number INTEGER NOT NULL)")
144144
// Note that we use two distinct indices instead of a composite index on (channel_id, commitment_number).
@@ -218,9 +218,9 @@ class SqliteChannelsDb(val sqlite: Connection) extends ChannelsDb with Logging {
218218
statement.executeQuery().flatMap(rs => rs.getLongNullable("created_timestamp")).headOption
219219
}
220220
using(sqlite.prepareStatement("INSERT OR IGNORE INTO closed_channels VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) { statement =>
221-
statement.setBytes(1, channelId.toArray)
222-
statement.setBytes(2, data.remoteNodeId.value.toArray)
223-
statement.setBytes(3, data.fundingTxId.value.toArray)
221+
statement.setString(1, channelId.toHex)
222+
statement.setString(2, data.remoteNodeId.toHex)
223+
statement.setString(3, data.fundingTxId.value.toHex)
224224
statement.setLong(4, data.fundingOutputIndex)
225225
statement.setLong(5, data.fundingTxIndex)
226226
statement.setString(6, data.fundingKeyPath)
@@ -229,9 +229,9 @@ class SqliteChannelsDb(val sqlite: Connection) extends ChannelsDb with Logging {
229229
statement.setString(9, data.commitmentFormat)
230230
statement.setBoolean(10, data.announced)
231231
statement.setLong(11, data.capacity.toLong)
232-
statement.setBytes(12, data.closingTxId.value.toArray)
232+
statement.setString(12, data.closingTxId.value.toHex)
233233
statement.setString(13, data.closingType)
234-
statement.setBytes(14, data.closingScript.toArray)
234+
statement.setString(14, data.closingScript.toHex)
235235
statement.setLong(15, data.localBalance.toLong)
236236
statement.setLong(16, data.remoteBalance.toLong)
237237
statement.setLong(17, data.closingAmount.toLong)
@@ -303,12 +303,12 @@ class SqliteChannelsDb(val sqlite: Connection) extends ChannelsDb with Logging {
303303
case None => "SELECT * FROM closed_channels ORDER BY closed_at DESC"
304304
}
305305
using(sqlite.prepareStatement(limited(sql, paginated_opt))) { statement =>
306-
remoteNodeId_opt.foreach(remoteNodeId => statement.setBytes(1, remoteNodeId.value.toArray))
306+
remoteNodeId_opt.foreach(remoteNodeId => statement.setString(1, remoteNodeId.toHex))
307307
statement.executeQuery().map { rs =>
308308
DATA_CLOSED(
309-
channelId = rs.getByteVector32("channel_id"),
310-
remoteNodeId = PublicKey(rs.getByteVector("remote_node_id")),
311-
fundingTxId = TxId(rs.getByteVector32("funding_txid")),
309+
channelId = rs.getByteVector32FromHex("channel_id"),
310+
remoteNodeId = PublicKey(rs.getByteVectorFromHex("remote_node_id")),
311+
fundingTxId = TxId(rs.getByteVector32FromHex("funding_txid")),
312312
fundingOutputIndex = rs.getLong("funding_output_index"),
313313
fundingTxIndex = rs.getLong("funding_tx_index"),
314314
fundingKeyPath = rs.getString("funding_key_path"),
@@ -317,9 +317,9 @@ class SqliteChannelsDb(val sqlite: Connection) extends ChannelsDb with Logging {
317317
commitmentFormat = rs.getString("commitment_format"),
318318
announced = rs.getBoolean("announced"),
319319
capacity = Satoshi(rs.getLong("capacity_satoshis")),
320-
closingTxId = TxId(rs.getByteVector32("closing_txid")),
320+
closingTxId = TxId(rs.getByteVector32FromHex("closing_txid")),
321321
closingType = rs.getString("closing_type"),
322-
closingScript = rs.getByteVector("closing_script"),
322+
closingScript = rs.getByteVectorFromHex("closing_script"),
323323
localBalance = MilliSatoshi(rs.getLong("local_balance_msat")),
324324
remoteBalance = MilliSatoshi(rs.getLong("remote_balance_msat")),
325325
closingAmount = Satoshi(rs.getLong("closing_amount_satoshis"))

0 commit comments

Comments
 (0)