@@ -26,11 +26,14 @@ import fr.acinq.eclair.db.sqlite.SqliteChannelsDb
26
26
import fr .acinq .eclair .db .sqlite .SqliteUtils .ExtendedResultSet ._
27
27
import fr .acinq .eclair .wire .internal .channel .ChannelCodecs .stateDataCodec
28
28
import fr .acinq .eclair .wire .internal .channel .ChannelCodecsSpec
29
- import fr .acinq .eclair .{CltvExpiry , randomBytes32 }
29
+ import fr .acinq .eclair .{CltvExpiry , ShortChannelId , randomBytes32 }
30
30
import org .scalatest .funsuite .AnyFunSuite
31
31
import scodec .bits .ByteVector
32
32
33
33
import java .sql .SQLException
34
+ import java .util .concurrent .Executors
35
+ import scala .concurrent .{Await , ExecutionContext , ExecutionContextExecutor , Future }
36
+ import scala .concurrent .duration ._
34
37
35
38
class ChannelsDbSpec extends AnyFunSuite {
36
39
@@ -52,30 +55,51 @@ class ChannelsDbSpec extends AnyFunSuite {
52
55
val db = dbs.channels
53
56
dbs.pendingRelay // needed by db.removeChannel
54
57
55
- val channel = ChannelCodecsSpec .normal
58
+ val channel1 = ChannelCodecsSpec .normal
59
+ val channel2a = ChannelCodecsSpec .normal.modify(_.commitments.channelId).setTo(randomBytes32)
60
+ val channel2b = channel2a.modify(_.shortChannelId).setTo(ShortChannelId (189371 ))
56
61
57
62
val commitNumber = 42
58
63
val paymentHash1 = ByteVector32 .Zeroes
59
64
val cltvExpiry1 = CltvExpiry (123 )
60
65
val paymentHash2 = ByteVector32 (ByteVector .fill(32 )(1 ))
61
66
val cltvExpiry2 = CltvExpiry (656 )
62
67
63
- intercept[SQLException ](db.addHtlcInfo(channel .channelId, commitNumber, paymentHash1, cltvExpiry1)) // no related channel
68
+ intercept[SQLException ](db.addHtlcInfo(channel1 .channelId, commitNumber, paymentHash1, cltvExpiry1)) // no related channel
64
69
65
70
assert(db.listLocalChannels().toSet === Set .empty)
66
- db.addOrUpdateChannel(channel)
67
- db.addOrUpdateChannel(channel)
68
- assert(db.listLocalChannels() === List (channel))
69
-
70
- assert(db.listHtlcInfos(channel.channelId, commitNumber).toList == Nil )
71
- db.addHtlcInfo(channel.channelId, commitNumber, paymentHash1, cltvExpiry1)
72
- db.addHtlcInfo(channel.channelId, commitNumber, paymentHash2, cltvExpiry2)
73
- assert(db.listHtlcInfos(channel.channelId, commitNumber).toList.toSet == Set ((paymentHash1, cltvExpiry1), (paymentHash2, cltvExpiry2)))
74
- assert(db.listHtlcInfos(channel.channelId, 43 ).toList == Nil )
75
-
76
- db.removeChannel(channel.channelId)
71
+ db.addOrUpdateChannel(channel1)
72
+ db.addOrUpdateChannel(channel1)
73
+ assert(db.listLocalChannels() === List (channel1))
74
+ db.addOrUpdateChannel(channel2a)
75
+ assert(db.listLocalChannels() === List (channel1, channel2a))
76
+ db.addOrUpdateChannel(channel2b)
77
+ assert(db.listLocalChannels() === List (channel1, channel2b))
78
+
79
+ assert(db.listHtlcInfos(channel1.channelId, commitNumber).toList == Nil )
80
+ db.addHtlcInfo(channel1.channelId, commitNumber, paymentHash1, cltvExpiry1)
81
+ db.addHtlcInfo(channel1.channelId, commitNumber, paymentHash2, cltvExpiry2)
82
+ assert(db.listHtlcInfos(channel1.channelId, commitNumber).toList.toSet == Set ((paymentHash1, cltvExpiry1), (paymentHash2, cltvExpiry2)))
83
+ assert(db.listHtlcInfos(channel1.channelId, 43 ).toList == Nil )
84
+
85
+ db.removeChannel(channel1.channelId)
86
+ assert(db.listLocalChannels() === List (channel2b))
87
+ assert(db.listHtlcInfos(channel1.channelId, commitNumber).toList == Nil )
88
+ db.removeChannel(channel2b.channelId)
77
89
assert(db.listLocalChannels() === Nil )
78
- assert(db.listHtlcInfos(channel.channelId, commitNumber).toList == Nil )
90
+ }
91
+ }
92
+
93
+ test(" concurrent channel updates" ) {
94
+ forAllDbs { dbs =>
95
+ val db = dbs.channels
96
+ implicit val ec : ExecutionContextExecutor = ExecutionContext .fromExecutor(Executors .newFixedThreadPool(8 ))
97
+ val channel = ChannelCodecsSpec .normal
98
+ val futures = for (_ <- 0 until 10000 ) yield {
99
+ Future (db.addOrUpdateChannel(channel.modify(_.commitments.channelId).setTo(randomBytes32)))
100
+ }
101
+ val res = Future .sequence(futures)
102
+ Await .result(res, 60 seconds)
79
103
}
80
104
}
81
105
0 commit comments