@@ -37,21 +37,19 @@ import fr.acinq.eclair.crypto.ShaChain
37
37
import fr .acinq .eclair .crypto .keymanager .ChannelKeyManager
38
38
import fr .acinq .eclair .db .DbEventHandler .ChannelEvent .EventType
39
39
import fr .acinq .eclair .db .PendingCommandsDb
40
- import fr .acinq .eclair .db .pg .PgUtils .PgLock .logger
41
40
import fr .acinq .eclair .io .Peer
42
41
import fr .acinq .eclair .payment .PaymentSettlingOnChain
43
42
import fr .acinq .eclair .router .Announcements
44
43
import fr .acinq .eclair .transactions .Transactions .{ClosingTx , TxOwner }
45
44
import fr .acinq .eclair .transactions ._
46
45
import fr .acinq .eclair .wire .protocol ._
47
- import org .sqlite .SQLiteException
48
46
import scodec .bits .ByteVector
49
47
50
48
import java .sql .SQLException
51
49
import scala .collection .immutable .Queue
52
50
import scala .concurrent .ExecutionContext
53
51
import scala .concurrent .duration ._
54
- import scala .util .{Failure , Success , Try }
52
+ import scala .util .{Failure , Random , Success , Try }
55
53
56
54
/**
57
55
* Created by PM on 20/08/2015.
@@ -125,6 +123,9 @@ object Channel {
125
123
*/
126
124
case class OutgoingMessage (msg : LightningMessage , peerConnection : ActorRef )
127
125
126
+ /** We don't immediately process [[CurrentBlockCount ]] to avoid herd effects */
127
+ case class ProcessCurrentBlockCount (c : CurrentBlockCount )
128
+
128
129
}
129
130
130
131
class Channel (val nodeParams : NodeParams , val wallet : EclairWallet , remoteNodeId : PublicKey , blockchain : typed.ActorRef [ZmqWatcher .Command ], relayer : ActorRef , txPublisherFactory : Channel .TxPublisherFactory , origin_opt : Option [ActorRef ] = None )(implicit ec : ExecutionContext = ExecutionContext .Implicits .global) extends FSM [State , Data ] with FSMDiagnosticActorLogging [State , Data ] {
@@ -150,6 +151,8 @@ class Channel(val nodeParams: NodeParams, val wallet: EclairWallet, remoteNodeId
150
151
151
152
// this will be used to detect htlc timeouts
152
153
context.system.eventStream.subscribe(self, classOf [CurrentBlockCount ])
154
+ // the constant delay by which we delay processing of blocks (it will be smoothened among all channels)
155
+ private val blockProcessingDelay = Random .nextLong(nodeParams.maxBlockProcessingDelay.toMillis + 1 ).millis
153
156
// this will be used to make sure the current commitment fee is up-to-date
154
157
context.system.eventStream.subscribe(self, classOf [CurrentFeerates ])
155
158
@@ -631,7 +634,7 @@ class Channel(val nodeParams: NodeParams, val wallet: EclairWallet, remoteNodeId
631
634
632
635
case Event (BITCOIN_FUNDING_PUBLISH_FAILED , d : DATA_WAIT_FOR_FUNDING_CONFIRMED ) => handleFundingPublishFailed(d)
633
636
634
- case Event (c : CurrentBlockCount , d : DATA_WAIT_FOR_FUNDING_CONFIRMED ) => d.fundingTx match {
637
+ case Event (ProcessCurrentBlockCount (c) , d : DATA_WAIT_FOR_FUNDING_CONFIRMED ) => d.fundingTx match {
635
638
case Some (_) => stay // we are funder, we're still waiting for the funding tx to be confirmed
636
639
case None if c.blockCount - d.waitingSinceBlock > FUNDING_TIMEOUT_FUNDEE =>
637
640
log.warning(s " funding tx hasn't been published in ${c.blockCount - d.waitingSinceBlock} blocks " )
@@ -943,7 +946,7 @@ class Channel(val nodeParams: NodeParams, val wallet: EclairWallet, remoteNodeId
943
946
}
944
947
}
945
948
946
- case Event (c : CurrentBlockCount , d : DATA_NORMAL ) => handleNewBlock(c, d)
949
+ case Event (ProcessCurrentBlockCount (c) , d : DATA_NORMAL ) => handleNewBlock(c, d)
947
950
948
951
case Event (c : CurrentFeerates , d : DATA_NORMAL ) => handleCurrentFeerate(c, d)
949
952
@@ -1221,7 +1224,7 @@ class Channel(val nodeParams: NodeParams, val wallet: EclairWallet, remoteNodeId
1221
1224
1222
1225
case Event (r : RevocationTimeout , d : DATA_SHUTDOWN ) => handleRevocationTimeout(r, d)
1223
1226
1224
- case Event (c : CurrentBlockCount , d : DATA_SHUTDOWN ) => handleNewBlock(c, d)
1227
+ case Event (ProcessCurrentBlockCount (c) , d : DATA_SHUTDOWN ) => handleNewBlock(c, d)
1225
1228
1226
1229
case Event (c : CurrentFeerates , d : DATA_SHUTDOWN ) => handleCurrentFeerate(c, d)
1227
1230
@@ -1519,7 +1522,7 @@ class Channel(val nodeParams: NodeParams, val wallet: EclairWallet, remoteNodeId
1519
1522
// note: this can only happen if state is NORMAL or SHUTDOWN
1520
1523
// -> in NEGOTIATING there are no more htlcs
1521
1524
// -> in CLOSING we either have mutual closed (so no more htlcs), or already have unilaterally closed (so no action required), and we can't be in OFFLINE state anyway
1522
- case Event (c : CurrentBlockCount , d : HasCommitments ) => handleNewBlock(c, d)
1525
+ case Event (ProcessCurrentBlockCount (c) , d : HasCommitments ) => handleNewBlock(c, d)
1523
1526
1524
1527
case Event (c : CurrentFeerates , d : HasCommitments ) =>
1525
1528
handleOfflineFeerate(c, d)
@@ -1710,7 +1713,7 @@ class Channel(val nodeParams: NodeParams, val wallet: EclairWallet, remoteNodeId
1710
1713
context.system.scheduler.scheduleOnce(5 seconds, self, remoteAnnSigs)
1711
1714
stay
1712
1715
1713
- case Event (c : CurrentBlockCount , d : HasCommitments ) => handleNewBlock(c, d)
1716
+ case Event (ProcessCurrentBlockCount (c) , d : HasCommitments ) => handleNewBlock(c, d)
1714
1717
1715
1718
case Event (c : CurrentFeerates , d : HasCommitments ) =>
1716
1719
handleOfflineFeerate(c, d)
@@ -1792,8 +1795,13 @@ class Channel(val nodeParams: NodeParams, val wallet: EclairWallet, remoteNodeId
1792
1795
// we only care about this event in NORMAL and SHUTDOWN state, and there may be cases where the task is not cancelled
1793
1796
case Event (_ : RevocationTimeout , _) => stay
1794
1797
1798
+ // we reschedule with a random delay to prevent herd effect when there are a lot of channels
1799
+ case Event (c : CurrentBlockCount , _) =>
1800
+ context.system.scheduler.scheduleOnce(blockProcessingDelay, self, ProcessCurrentBlockCount (c))
1801
+ stay
1802
+
1795
1803
// we only care about this event in NORMAL and SHUTDOWN state, and we never unregister to the event stream
1796
- case Event (CurrentBlockCount (_), _) => stay
1804
+ case Event (ProcessCurrentBlockCount (_), _) => stay
1797
1805
1798
1806
// we only care about this event in NORMAL and SHUTDOWN state, and we never unregister to the event stream
1799
1807
case Event (CurrentFeerates (_), _) => stay
@@ -2568,4 +2576,3 @@ class Channel(val nodeParams: NodeParams, val wallet: EclairWallet, remoteNodeId
2568
2576
2569
2577
}
2570
2578
2571
-
0 commit comments