Skip to content

Commit 3f0a426

Browse files
committed
Peer storage
1 parent f02c98b commit 3f0a426

File tree

14 files changed

+247
-27
lines changed

14 files changed

+247
-27
lines changed

eclair-core/src/main/scala/fr/acinq/eclair/Features.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,11 @@ object Features {
275275
val mandatory = 38
276276
}
277277

278+
case object ProvideStorage extends Feature with InitFeature with NodeFeature {
279+
val rfcName = "option_provide_storage"
280+
val mandatory = 42
281+
}
282+
278283
case object ChannelType extends Feature with InitFeature with NodeFeature {
279284
val rfcName = "option_channel_type"
280285
val mandatory = 44
@@ -358,6 +363,7 @@ object Features {
358363
DualFunding,
359364
Quiescence,
360365
OnionMessages,
366+
ProvideStorage,
361367
ChannelType,
362368
ScidAlias,
363369
PaymentMetadata,

eclair-core/src/main/scala/fr/acinq/eclair/NodeParams.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ case class NodeParams(nodeKeyManager: NodeKeyManager,
9292
revokedHtlcInfoCleanerConfig: RevokedHtlcInfoCleaner.Config,
9393
willFundRates_opt: Option[LiquidityAds.WillFundRates],
9494
peerWakeUpConfig: PeerReadyNotifier.WakeUpConfig,
95-
onTheFlyFundingConfig: OnTheFlyFunding.Config) {
95+
onTheFlyFundingConfig: OnTheFlyFunding.Config,
96+
peerStorageWriteDelayMax: FiniteDuration) {
9697
val privateKey: Crypto.PrivateKey = nodeKeyManager.nodeKey.privateKey
9798

9899
val nodeId: PublicKey = nodeKeyManager.nodeId
@@ -680,6 +681,7 @@ object NodeParams extends Logging {
680681
onTheFlyFundingConfig = OnTheFlyFunding.Config(
681682
proposalTimeout = FiniteDuration(config.getDuration("on-the-fly-funding.proposal-timeout").getSeconds, TimeUnit.SECONDS),
682683
),
684+
peerStorageWriteDelayMax = 1 minute,
683685
)
684686
}
685687
}

eclair-core/src/main/scala/fr/acinq/eclair/db/DualDatabases.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import fr.acinq.eclair.router.Router
1515
import fr.acinq.eclair.wire.protocol.{ChannelAnnouncement, ChannelUpdate, NodeAddress, NodeAnnouncement}
1616
import fr.acinq.eclair.{CltvExpiry, MilliSatoshi, Paginated, RealShortChannelId, ShortChannelId, TimestampMilli}
1717
import grizzled.slf4j.Logging
18+
import scodec.bits.ByteVector
1819

1920
import java.io.File
2021
import java.util.UUID
@@ -292,6 +293,16 @@ case class DualPeersDb(primary: PeersDb, secondary: PeersDb) extends PeersDb {
292293
runAsync(secondary.getRelayFees(nodeId))
293294
primary.getRelayFees(nodeId)
294295
}
296+
297+
override def updateStorage(nodeId: PublicKey, data: ByteVector): Unit = {
298+
runAsync(secondary.updateStorage(nodeId, data))
299+
primary.updateStorage(nodeId, data)
300+
}
301+
302+
override def getStorage(nodeId: PublicKey): Option[ByteVector] = {
303+
runAsync(secondary.getStorage(nodeId))
304+
primary.getStorage(nodeId)
305+
}
295306
}
296307

297308
case class DualPaymentsDb(primary: PaymentsDb, secondary: PaymentsDb) extends PaymentsDb {

eclair-core/src/main/scala/fr/acinq/eclair/db/PeersDb.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package fr.acinq.eclair.db
1919
import fr.acinq.bitcoin.scalacompat.Crypto.PublicKey
2020
import fr.acinq.eclair.payment.relay.Relayer.RelayFees
2121
import fr.acinq.eclair.wire.protocol.NodeAddress
22+
import scodec.bits.ByteVector
2223

2324
trait PeersDb {
2425

@@ -34,4 +35,8 @@ trait PeersDb {
3435

3536
def getRelayFees(nodeId: PublicKey): Option[RelayFees]
3637

38+
def updateStorage(nodeId: PublicKey, data: ByteVector): Unit
39+
40+
def getStorage(nodeId: PublicKey): Option[ByteVector]
41+
3742
}

eclair-core/src/main/scala/fr/acinq/eclair/db/pg/PgPeersDb.scala

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ import fr.acinq.eclair.db.pg.PgUtils.PgLock
2626
import fr.acinq.eclair.payment.relay.Relayer.RelayFees
2727
import fr.acinq.eclair.wire.protocol._
2828
import grizzled.slf4j.Logging
29-
import scodec.bits.BitVector
29+
import scodec.bits.{BitVector, ByteVector}
3030

3131
import java.sql.Statement
3232
import javax.sql.DataSource
3333

3434
object PgPeersDb {
3535
val DB_NAME = "peers"
36-
val CURRENT_VERSION = 3
36+
val CURRENT_VERSION = 4
3737
}
3838

3939
class PgPeersDb(implicit ds: DataSource, lock: PgLock) extends PeersDb with Logging {
@@ -54,20 +54,28 @@ class PgPeersDb(implicit ds: DataSource, lock: PgLock) extends PeersDb with Logg
5454
statement.executeUpdate("CREATE TABLE local.relay_fees (node_id TEXT NOT NULL PRIMARY KEY, fee_base_msat BIGINT NOT NULL, fee_proportional_millionths BIGINT NOT NULL)")
5555
}
5656

57+
def migration34(statement: Statement): Unit = {
58+
statement.executeUpdate("CREATE TABLE local.peer_storage (node_id TEXT NOT NULL PRIMARY KEY, data BYTEA NOT NULL)")
59+
}
60+
5761
using(pg.createStatement()) { statement =>
5862
getVersion(statement, DB_NAME) match {
5963
case None =>
6064
statement.executeUpdate("CREATE SCHEMA IF NOT EXISTS local")
61-
statement.executeUpdate("CREATE TABLE local.peers (node_id TEXT NOT NULL PRIMARY KEY, data BYTEA NOT NULL)")
65+
statement.executeUpdate("CREATE TABLE local.peers (node_id TEXT NOT NULL PRIMARY KEY, data BYTEA NOT NULL, storage BYTEA)")
6266
statement.executeUpdate("CREATE TABLE local.relay_fees (node_id TEXT NOT NULL PRIMARY KEY, fee_base_msat BIGINT NOT NULL, fee_proportional_millionths BIGINT NOT NULL)")
63-
case Some(v@(1 | 2)) =>
67+
statement.executeUpdate("CREATE TABLE local.peer_storage (node_id TEXT NOT NULL PRIMARY KEY, data BYTEA NOT NULL)")
68+
case Some(v@(1 | 2 | 3)) =>
6469
logger.warn(s"migrating db $DB_NAME, found version=$v current=$CURRENT_VERSION")
6570
if (v < 2) {
6671
migration12(statement)
6772
}
6873
if (v < 3) {
6974
migration23(statement)
7075
}
76+
if (v < 4) {
77+
migration34(statement)
78+
}
7179
case Some(CURRENT_VERSION) => () // table is up-to-date, nothing to do
7280
case Some(unknownVersion) => throw new RuntimeException(s"Unknown version of DB $DB_NAME found, version=$unknownVersion")
7381
}
@@ -98,6 +106,10 @@ class PgPeersDb(implicit ds: DataSource, lock: PgLock) extends PeersDb with Logg
98106
statement.setString(1, nodeId.value.toHex)
99107
statement.executeUpdate()
100108
}
109+
using(pg.prepareStatement("DELETE FROM local.peer_storage WHERE node_id = ?")) { statement =>
110+
statement.setString(1, nodeId.value.toHex)
111+
statement.executeUpdate()
112+
}
101113
}
102114
}
103115

@@ -155,4 +167,31 @@ class PgPeersDb(implicit ds: DataSource, lock: PgLock) extends PeersDb with Logg
155167
}
156168
}
157169
}
170+
171+
override def updateStorage(nodeId: PublicKey, data: ByteVector): Unit = withMetrics("peers/update-storage", DbBackends.Postgres) {
172+
withLock { pg =>
173+
using(pg.prepareStatement(
174+
"""
175+
INSERT INTO local.peer_storage (node_id, data)
176+
VALUES (?, ?)
177+
ON CONFLICT (node_id)
178+
DO UPDATE SET data = EXCLUDED.data
179+
""")) { statement =>
180+
statement.setString(1, nodeId.value.toHex)
181+
statement.setBytes(2, data.toArray)
182+
statement.executeUpdate()
183+
}
184+
}
185+
}
186+
187+
override def getStorage(nodeId: PublicKey): Option[ByteVector] = withMetrics("peers/get-storage", DbBackends.Postgres) {
188+
withLock { pg =>
189+
using(pg.prepareStatement("SELECT data FROM local.peer_storage WHERE node_id = ?")) { statement =>
190+
statement.setString(1, nodeId.value.toHex)
191+
statement.executeQuery()
192+
.headOption
193+
.map(rs => ByteVector(rs.getBytes("data")))
194+
}
195+
}
196+
}
158197
}

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

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ import fr.acinq.eclair.db.sqlite.SqliteUtils.{getVersion, setVersion, using}
2626
import fr.acinq.eclair.payment.relay.Relayer.RelayFees
2727
import fr.acinq.eclair.wire.protocol._
2828
import grizzled.slf4j.Logging
29-
import scodec.bits.BitVector
29+
import scodec.bits.{BitVector, ByteVector}
3030

3131
import java.sql.{Connection, Statement}
3232

3333
object SqlitePeersDb {
3434
val DB_NAME = "peers"
35-
val CURRENT_VERSION = 2
35+
val CURRENT_VERSION = 3
3636
}
3737

3838
class SqlitePeersDb(val sqlite: Connection) extends PeersDb with Logging {
@@ -46,13 +46,23 @@ class SqlitePeersDb(val sqlite: Connection) extends PeersDb with Logging {
4646
statement.executeUpdate("CREATE TABLE relay_fees (node_id BLOB NOT NULL PRIMARY KEY, fee_base_msat INTEGER NOT NULL, fee_proportional_millionths INTEGER NOT NULL)")
4747
}
4848

49+
def migration23(statement: Statement): Unit = {
50+
statement.executeUpdate("CREATE TABLE peer_storage (node_id BLOB NOT NULL PRIMARY KEY, data NOT NULL)")
51+
}
52+
4953
getVersion(statement, DB_NAME) match {
5054
case None =>
5155
statement.executeUpdate("CREATE TABLE peers (node_id BLOB NOT NULL PRIMARY KEY, data BLOB NOT NULL)")
5256
statement.executeUpdate("CREATE TABLE relay_fees (node_id BLOB NOT NULL PRIMARY KEY, fee_base_msat INTEGER NOT NULL, fee_proportional_millionths INTEGER NOT NULL)")
53-
case Some(v@1) =>
57+
statement.executeUpdate("CREATE TABLE peer_storage (node_id BLOB NOT NULL PRIMARY KEY, data NOT NULL)")
58+
case Some(v@(1 | 2)) =>
5459
logger.warn(s"migrating db $DB_NAME, found version=$v current=$CURRENT_VERSION")
55-
migration12(statement)
60+
if (v < 2) {
61+
migration12(statement)
62+
}
63+
if (v < 3) {
64+
migration23(statement)
65+
}
5666
case Some(CURRENT_VERSION) => () // table is up-to-date, nothing to do
5767
case Some(unknownVersion) => throw new RuntimeException(s"Unknown version of DB $DB_NAME found, version=$unknownVersion")
5868
}
@@ -128,4 +138,27 @@ class SqlitePeersDb(val sqlite: Connection) extends PeersDb with Logging {
128138
)
129139
}
130140
}
141+
142+
override def updateStorage(nodeId: PublicKey, data: ByteVector): Unit = withMetrics("peers/update-storage", DbBackends.Sqlite) {
143+
using(sqlite.prepareStatement("UPDATE peer_storage SET data = ? WHERE node_id = ?")) { update =>
144+
update.setBytes(1, data.toArray)
145+
update.setBytes(2, nodeId.value.toArray)
146+
if (update.executeUpdate() == 0) {
147+
using(sqlite.prepareStatement("INSERT INTO peer_storage VALUES (?, ?)")) { statement =>
148+
statement.setBytes(1, nodeId.value.toArray)
149+
statement.setBytes(2, data.toArray)
150+
statement.executeUpdate()
151+
}
152+
}
153+
}
154+
}
155+
156+
override def getStorage(nodeId: PublicKey): Option[ByteVector] = withMetrics("peers/get-storage", DbBackends.Sqlite) {
157+
using(sqlite.prepareStatement("SELECT data FROM peer_storage WHERE node_id = ?")) { statement =>
158+
statement.setBytes(1, nodeId.value.toArray)
159+
statement.executeQuery()
160+
.headOption
161+
.map(rs => ByteVector(rs.getBytes("data")))
162+
}
163+
}
131164
}

eclair-core/src/main/scala/fr/acinq/eclair/io/Peer.scala

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ import fr.acinq.eclair.remote.EclairInternalsSerializer.RemoteTypes
4444
import fr.acinq.eclair.router.Router
4545
import fr.acinq.eclair.wire.protocol
4646
import fr.acinq.eclair.wire.protocol.FailureMessageCodecs.createBadOnionFailure
47-
import fr.acinq.eclair.wire.protocol.{AddFeeCredit, ChannelTlv, CurrentFeeCredit, Error, HasChannelId, HasTemporaryChannelId, LightningMessage, LiquidityAds, NodeAddress, OnTheFlyFundingFailureMessage, OnionMessage, OnionRoutingPacket, RecommendedFeerates, RoutingMessage, SpliceInit, TemporaryChannelFailure, TlvStream, TxAbort, UnknownMessage, Warning, WillAddHtlc, WillFailHtlc, WillFailMalformedHtlc}
47+
import fr.acinq.eclair.wire.protocol.{AddFeeCredit, ChannelTlv, CurrentFeeCredit, Error, HasChannelId, HasTemporaryChannelId, LightningMessage, LiquidityAds, NodeAddress, OnTheFlyFundingFailureMessage, OnionMessage, OnionRoutingPacket, PeerStorageRetrieval, PeerStorageStore, RecommendedFeerates, RoutingMessage, SpliceInit, TlvStream, TxAbort, UnknownMessage, Warning, WillAddHtlc, WillFailHtlc, WillFailMalformedHtlc}
48+
import scodec.bits.ByteVector
4849

4950
/**
5051
* This actor represents a logical peer. There is one [[Peer]] per unique remote node id at all time.
@@ -84,7 +85,7 @@ class Peer(val nodeParams: NodeParams,
8485
FinalChannelId(state.channelId) -> channel
8586
}.toMap
8687
context.system.eventStream.publish(PeerCreated(self, remoteNodeId))
87-
goto(DISCONNECTED) using DisconnectedData(channels) // when we restart, we will attempt to reconnect right away, but then we'll wait
88+
goto(DISCONNECTED) using DisconnectedData(channels, PeerStorage(nodeParams.db.peers.getStorage(remoteNodeId), written = true, TimestampMilli.min)) // when we restart, we will attempt to reconnect right away, but then we'll wait
8889
}
8990

9091
when(DISCONNECTED) {
@@ -93,7 +94,7 @@ class Peer(val nodeParams: NodeParams,
9394
stay()
9495

9596
case Event(connectionReady: PeerConnection.ConnectionReady, d: DisconnectedData) =>
96-
gotoConnected(connectionReady, d.channels.map { case (k: ChannelId, v) => (k, v) })
97+
gotoConnected(connectionReady, d.channels.map { case (k: ChannelId, v) => (k, v) }, d.peerStorage)
9798

9899
case Event(Terminated(actor), d: DisconnectedData) if d.channels.values.toSet.contains(actor) =>
99100
// we have at most 2 ids: a TemporaryChannelId and a FinalChannelId
@@ -465,7 +466,7 @@ class Peer(val nodeParams: NodeParams,
465466
stopPeer()
466467
} else {
467468
d.channels.values.toSet[ActorRef].foreach(_ ! INPUT_DISCONNECTED) // we deduplicate with toSet because there might be two entries per channel (tmp id and final id)
468-
goto(DISCONNECTED) using DisconnectedData(d.channels.collect { case (k: FinalChannelId, v) => (k, v) })
469+
goto(DISCONNECTED) using DisconnectedData(d.channels.collect { case (k: FinalChannelId, v) => (k, v) }, d.peerStorage)
469470
}
470471

471472
case Event(Terminated(actor), d: ConnectedData) if d.channels.values.toSet.contains(actor) =>
@@ -484,7 +485,7 @@ class Peer(val nodeParams: NodeParams,
484485
log.debug(s"got new connection, killing current one and switching")
485486
d.peerConnection ! PeerConnection.Kill(KillReason.ConnectionReplaced)
486487
d.channels.values.toSet[ActorRef].foreach(_ ! INPUT_DISCONNECTED) // we deduplicate with toSet because there might be two entries per channel (tmp id and final id)
487-
gotoConnected(connectionReady, d.channels)
488+
gotoConnected(connectionReady, d.channels, d.peerStorage)
488489

489490
case Event(msg: OnionMessage, _: ConnectedData) =>
490491
OnionMessages.process(nodeParams.privateKey, msg) match {
@@ -517,6 +518,21 @@ class Peer(val nodeParams: NodeParams,
517518
d.peerConnection forward unknownMsg
518519
stay()
519520

521+
case Event(store: PeerStorageStore, d: ConnectedData) if nodeParams.features.hasFeature(Features.ProvideStorage) && d.channels.nonEmpty =>
522+
val timeSinceLastWrite = TimestampMilli.now() - d.peerStorage.lastWrite
523+
val peerStorage = if (timeSinceLastWrite >= nodeParams.peerStorageWriteDelayMax) {
524+
nodeParams.db.peers.updateStorage(remoteNodeId, store.blob)
525+
PeerStorage(Some(store.blob), written = true, TimestampMilli.now())
526+
} else {
527+
startSingleTimer("peer-storage-write", WritePeerStorage, nodeParams.peerStorageWriteDelayMax - timeSinceLastWrite)
528+
PeerStorage(Some(store.blob), written = false, d.peerStorage.lastWrite)
529+
}
530+
stay() using d.copy(peerStorage = peerStorage)
531+
532+
case Event(WritePeerStorage, d: ConnectedData) =>
533+
d.peerStorage.data.foreach(nodeParams.db.peers.updateStorage(remoteNodeId, _))
534+
stay() using d.copy(peerStorage = PeerStorage(d.peerStorage.data, written = true, TimestampMilli.now()))
535+
520536
case Event(unhandledMsg: LightningMessage, _) =>
521537
log.warning("ignoring message {}", unhandledMsg)
522538
stay()
@@ -748,7 +764,7 @@ class Peer(val nodeParams: NodeParams,
748764
context.system.eventStream.publish(PeerDisconnected(self, remoteNodeId))
749765
}
750766

751-
private def gotoConnected(connectionReady: PeerConnection.ConnectionReady, channels: Map[ChannelId, ActorRef]): State = {
767+
private def gotoConnected(connectionReady: PeerConnection.ConnectionReady, channels: Map[ChannelId, ActorRef], peerStorage: PeerStorage): State = {
752768
require(remoteNodeId == connectionReady.remoteNodeId, s"invalid nodeId: $remoteNodeId != ${connectionReady.remoteNodeId}")
753769
log.debug("got authenticated connection to address {}", connectionReady.address)
754770

@@ -758,6 +774,9 @@ class Peer(val nodeParams: NodeParams,
758774
nodeParams.db.peers.addOrUpdatePeer(remoteNodeId, connectionReady.address)
759775
}
760776

777+
// If we have some data stored from our peer, we send it to them before doing anything else.
778+
peerStorage.data.foreach(connectionReady.peerConnection ! PeerStorageRetrieval(_))
779+
761780
// let's bring existing/requested channels online
762781
channels.values.toSet[ActorRef].foreach(_ ! INPUT_RECONNECTED(connectionReady.peerConnection, connectionReady.localInit, connectionReady.remoteInit)) // we deduplicate with toSet because there might be two entries per channel (tmp id and final id)
763782

@@ -775,7 +794,7 @@ class Peer(val nodeParams: NodeParams,
775794
connectionReady.peerConnection ! CurrentFeeCredit(nodeParams.chainHash, feeCredit.getOrElse(0 msat))
776795
}
777796

778-
goto(CONNECTED) using ConnectedData(connectionReady.address, connectionReady.peerConnection, connectionReady.localInit, connectionReady.remoteInit, channels, feerates, None)
797+
goto(CONNECTED) using ConnectedData(connectionReady.address, connectionReady.peerConnection, connectionReady.localInit, connectionReady.remoteInit, channels, feerates, None, peerStorage)
779798
}
780799

781800
/**
@@ -910,12 +929,18 @@ object Peer {
910929
case class TemporaryChannelId(id: ByteVector32) extends ChannelId
911930
case class FinalChannelId(id: ByteVector32) extends ChannelId
912931

932+
case class PeerStorage(data: Option[ByteVector], written: Boolean, lastWrite: TimestampMilli)
933+
913934
sealed trait Data {
914935
def channels: Map[_ <: ChannelId, ActorRef] // will be overridden by Map[FinalChannelId, ActorRef] or Map[ChannelId, ActorRef]
936+
def peerStorage: PeerStorage
915937
}
916-
case object Nothing extends Data { override def channels = Map.empty }
917-
case class DisconnectedData(channels: Map[FinalChannelId, ActorRef]) extends Data
918-
case class ConnectedData(address: NodeAddress, peerConnection: ActorRef, localInit: protocol.Init, remoteInit: protocol.Init, channels: Map[ChannelId, ActorRef], currentFeerates: RecommendedFeerates, previousFeerates_opt: Option[RecommendedFeerates]) extends Data {
938+
case object Nothing extends Data {
939+
override def channels = Map.empty
940+
override def peerStorage: PeerStorage = PeerStorage(None, written = true, TimestampMilli.min)
941+
}
942+
case class DisconnectedData(channels: Map[FinalChannelId, ActorRef], peerStorage: PeerStorage) extends Data
943+
case class ConnectedData(address: NodeAddress, peerConnection: ActorRef, localInit: protocol.Init, remoteInit: protocol.Init, channels: Map[ChannelId, ActorRef], currentFeerates: RecommendedFeerates, previousFeerates_opt: Option[RecommendedFeerates], peerStorage: PeerStorage) extends Data {
919944
val connectionInfo: ConnectionInfo = ConnectionInfo(address, peerConnection, localInit, remoteInit)
920945
def localFeatures: Features[InitFeature] = localInit.features
921946
def remoteFeatures: Features[InitFeature] = remoteInit.features
@@ -1028,5 +1053,7 @@ object Peer {
10281053
case class RelayOnionMessage(messageId: ByteVector32, msg: OnionMessage, replyTo_opt: Option[typed.ActorRef[Status]])
10291054

10301055
case class RelayUnknownMessage(unknownMessage: UnknownMessage)
1056+
1057+
case object WritePeerStorage
10311058
// @formatter:on
10321059
}

0 commit comments

Comments
 (0)