Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add a "smooth" fee provider It will return the moving average of fee estimates provided by its internal fee provider, within a user defined window that can be set with eclair.smooth-feerate-window. * Use a default fee rate smoothing window of 3 This should smooth our fee rate when there is a sudden change in onchain fees and prevent channels with c-lightning node from getting closed because they disagree with our fee rate.
- Loading branch information
Showing
5 changed files
with
92 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
eclair-core/src/main/scala/fr/acinq/eclair/blockchain/fee/SmoothFeeProvider.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package fr.acinq.eclair.blockchain.fee | ||
|
||
import scala.concurrent.{ExecutionContext, Future} | ||
|
||
class SmoothFeeProvider(provider: FeeProvider, windowSize: Int)(implicit ec: ExecutionContext) extends FeeProvider { | ||
require(windowSize > 0) | ||
|
||
var queue = List.empty[FeeratesPerKB] | ||
|
||
def append(rate: FeeratesPerKB): Unit = synchronized { | ||
queue = queue :+ rate | ||
if (queue.length > windowSize) queue = queue.drop(1) | ||
} | ||
|
||
override def getFeerates: Future[FeeratesPerKB] = { | ||
for { | ||
rate <- provider.getFeerates | ||
_ = append(rate) | ||
} yield SmoothFeeProvider.smooth(queue) | ||
} | ||
} | ||
|
||
object SmoothFeeProvider { | ||
|
||
def avg(i: Seq[Long]): Long = i.sum / i.size | ||
|
||
def smooth(rates: Seq[FeeratesPerKB]): FeeratesPerKB = | ||
FeeratesPerKB( | ||
block_1 = avg(rates.map(_.block_1)), | ||
blocks_2 = avg(rates.map(_.blocks_2)), | ||
blocks_6 = avg(rates.map(_.blocks_6)), | ||
blocks_12 = avg(rates.map(_.blocks_12)), | ||
blocks_36 = avg(rates.map(_.blocks_36)), | ||
blocks_72 = avg(rates.map(_.blocks_72))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
eclair-core/src/test/scala/fr/acinq/eclair/blockchain/fee/SmoothFeeProviderSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package fr.acinq.eclair.blockchain.fee | ||
|
||
import org.junit.runner.RunWith | ||
import org.scalatest.FunSuite | ||
import org.scalatest.junit.JUnitRunner | ||
|
||
import scala.concurrent.{Await, Future} | ||
import scala.concurrent.duration._ | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
|
||
@RunWith(classOf[JUnitRunner]) | ||
class SmoothFeeProviderSpec extends FunSuite { | ||
test("smooth fee rates") { | ||
val rates = Array( | ||
FeeratesPerKB(100, 200, 300, 400, 500, 600), | ||
FeeratesPerKB(200, 300, 400, 500, 600, 700), | ||
FeeratesPerKB(300, 400, 500, 600, 700, 800), | ||
FeeratesPerKB(300, 400, 500, 600, 700, 800), | ||
FeeratesPerKB(300, 400, 500, 600, 700, 800) | ||
) | ||
val provider = new FeeProvider { | ||
var index = 0 | ||
|
||
override def getFeerates: Future[FeeratesPerKB] = { | ||
val rate = rates(index) | ||
index = (index + 1) % rates.length | ||
Future.successful(rate) | ||
} | ||
} | ||
|
||
val smoothProvider = new SmoothFeeProvider(provider, windowSize = 3) | ||
val f = for { | ||
rate1 <- smoothProvider.getFeerates | ||
rate2 <- smoothProvider.getFeerates | ||
rate3 <- smoothProvider.getFeerates | ||
rate4 <- smoothProvider.getFeerates | ||
rate5 <- smoothProvider.getFeerates | ||
} yield (rate1, rate2, rate3, rate4, rate5) | ||
|
||
val (rate1, rate2, rate3, rate4, rate5) = Await.result(f, 5 seconds) | ||
assert(rate1 == rates(0)) | ||
assert(rate2 == SmoothFeeProvider.smooth(Seq(rates(0), rates(1)))) | ||
assert(rate3 == SmoothFeeProvider.smooth(Seq(rates(0), rates(1), rates(2)))) | ||
assert(rate3 == FeeratesPerKB(200, 300, 400, 500, 600, 700)) | ||
assert(rate4 == SmoothFeeProvider.smooth(Seq(rates(1), rates(2), rates(3)))) | ||
assert(rate5 == rates(4)) // since the last 3 values are the same | ||
} | ||
} |