Skip to content

Commit

Permalink
Store block counter on Channel-DB for fundee entropy
Browse files Browse the repository at this point in the history
  • Loading branch information
araspitzu committed Jul 22, 2019
1 parent 5e3b8f4 commit b5c5cda
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import fr.acinq.eclair.channel.HasCommitments

trait ChannelsDb {

def getCounterFor(blockHeight: Long): Long

def addOrUpdateChannel(state: HasCommitments)

def removeChannel(channelId: ByteVector32)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package fr.acinq.eclair.db.sqlite

import java.sql.{Connection, Statement}
import java.sql.{Connection, SQLException, Statement}

import fr.acinq.bitcoin.ByteVector32
import fr.acinq.eclair.channel.HasCommitments
Expand All @@ -36,6 +36,7 @@ class SqliteChannelsDb(sqlite: Connection) extends ChannelsDb with Logging {

private def migration12(statement: Statement) = {
statement.executeUpdate("ALTER TABLE local_channels ADD COLUMN is_closed BOOLEAN NOT NULL DEFAULT 0")
statement.executeUpdate("CREATE TABLE IF NOT EXISTS block_key_counter (block_height INTEGER NOT NULL PRIMARY KEY, counter INTEGER NOT NULL)")
}

using(sqlite.createStatement()) { statement =>
Expand All @@ -49,11 +50,40 @@ class SqliteChannelsDb(sqlite: Connection) extends ChannelsDb with Logging {
statement.executeUpdate("CREATE TABLE IF NOT EXISTS local_channels (channel_id BLOB NOT NULL PRIMARY KEY, data BLOB NOT NULL, is_closed BOOLEAN NOT NULL DEFAULT 0)")
statement.executeUpdate("CREATE TABLE IF NOT EXISTS htlc_infos (channel_id BLOB NOT NULL, commitment_number BLOB NOT NULL, payment_hash BLOB NOT NULL, cltv_expiry INTEGER NOT NULL, FOREIGN KEY(channel_id) REFERENCES local_channels(channel_id))")
statement.executeUpdate("CREATE INDEX IF NOT EXISTS htlc_infos_idx ON htlc_infos(channel_id, commitment_number)")

statement.executeUpdate("CREATE TABLE IF NOT EXISTS block_key_counter (block_height INTEGER NOT NULL PRIMARY KEY, counter INTEGER NOT NULL)")
case unknownVersion => throw new RuntimeException(s"Unknown version of DB $DB_NAME found, version=$unknownVersion")
}
}

override def getCounterFor(blockHeight: Long): Long = synchronized {
val counterOld = getCounter(blockHeight)
setCounter(blockHeight, counterOld + 1)
counterOld
}

private def getCounter(blockHeight: Long): Long = {
using(sqlite.prepareStatement("SELECT counter FROM block_key_counter WHERE block_height=?")) { statement =>
statement.setLong(1, blockHeight)
val rs = statement.executeQuery()
if(rs.next()) rs.getLong("counter") else 0
}
}

private def setCounter(blockHeight: Long, counter: Long) = {
using(sqlite.prepareStatement("UPDATE block_key_counter SET counter=? WHERE block_height=?")) { statement =>
statement.setLong(1, counter)
statement.setLong(2, blockHeight)
if(statement.executeUpdate() != 1){
using(sqlite.prepareStatement("INSERT INTO block_key_counter VALUES(?, ?)")) { insert =>
insert.setLong(1, blockHeight)
insert.setLong(2, counter)
insert.executeUpdate()
}
}
}
}


override def addOrUpdateChannel(state: HasCommitments): Unit = {
val data = stateDataCodec.encode(state).require.toByteArray
using (sqlite.prepareStatement("UPDATE local_channels SET data=? WHERE channel_id=?")) { update =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,20 @@ class SqliteChannelsDbSpec extends FunSuite {
assert(getVersion(statement, "channels", 1) == 2) // version changed from 1 -> 2
}
assert(db.listLocalChannels() === List(channel))
assert(db.getCounterFor(123) === 0)
}

test("channel keypath counter should get and increment") {

val sqlite = TestConstants.sqliteInMemory()
val channelDb = new SqliteChannelsDb(sqlite)

assert(channelDb.getCounterFor(123) == 0)
assert(channelDb.getCounterFor(123) == 1)
assert(channelDb.getCounterFor(123) == 2)
assert(channelDb.getCounterFor(124) == 0)
assert(channelDb.getCounterFor(125) == 0)
assert(channelDb.getCounterFor(124) == 1)
}

}

0 comments on commit b5c5cda

Please sign in to comment.