Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sstone committed Aug 4, 2023
1 parent a53c5ec commit 2fd5509
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ class BitcoinCoreClient(val rpcClient: BitcoinJsonRPCClient, val onchainKeyManag
rpcClient.invoke("importdescriptors", descriptors).collect {
case JArray(results) => results.forall(item => {
val JBool(success) = item \ "success"
val JArray(_) = item \ "warnings"
success
})
}
Expand All @@ -74,7 +73,7 @@ class BitcoinCoreClient(val rpcClient: BitcoinJsonRPCClient, val onchainKeyManag
onchainKeyManager_opt match {
case None => Future.successful(true) // no eclair-backed wallet is configured
case Some(onchainKeyManager) if !rpcClient.wallet.contains(onchainKeyManager.wallet) => Future.successful(true) // configured wallet has a different name
case Some(onchainKeyManager) if !onchainKeyManager.getDescriptors(0).descriptors.forall(desc => TimestampSecond(desc.timestamp) >= TimestampSecond.now() - 2.hours) =>
case Some(onchainKeyManager) if onchainKeyManager.getWalletTimestamp() < (TimestampSecond.now() - 2.hours) =>
logger.warn(s"descriptors are too old, you will need to manually import them and select how far back to rescan")
Future.failed(new RuntimeException("Could not import descriptors, please check logs for details"))
case Some(onchainKeyManager) =>
Expand Down Expand Up @@ -342,7 +341,7 @@ class BitcoinCoreClient(val rpcClient: BitcoinJsonRPCClient, val onchainKeyManag
actualFees = kmp2scala(signed.psbt.computeFees())
_ = require(actualFees == fees, s"actual funding fees $actualFees do not match bitcoin core fee $fees")
fundingTx = kmp2scala(extracted.getRight)
actualFeerate = FeeratePerKw((actualFees * 1000) / fundingTx.weight())
actualFeerate = Transactions.fee2rate(actualFees, fundingTx.weight())
maxFeerate = requestedFeeRate + requestedFeeRate / 2
_ = require(actualFeerate < maxFeerate, s"actual feerate $actualFeerate is more than 50% above requested fee rate $targetFeerate")
_ = logger.debug(s"created funding txid=${extracted.getRight.txid} outputIndex=$outputIndex fee=${fees}")
Expand Down Expand Up @@ -617,7 +616,7 @@ class BitcoinCoreClient(val rpcClient: BitcoinJsonRPCClient, val onchainKeyManag
signedPsbt <- unlockIfFails(fundedTx.tx.txid, lockedOutputs)(signPsbt(new Psbt(fundedTx.tx), fundedTx.tx.txIn.indices, fundedTx.tx.txOut.indices.filterNot(_ == theirOutputPos)))
signedTx = signedPsbt.finalTx
actualFees = kmp2scala(signedPsbt.psbt.computeFees())
actualFeerate = FeeratePerKw((actualFees * 1000) / signedTx.weight())
actualFeerate = Transactions.fee2rate(actualFees, signedTx.weight())
maxFeerate = feeratePerKw + feeratePerKw / 2
_ = require(actualFeerate < maxFeerate, s"actual fee rate $actualFeerate is more than 50% above requested fee rate $feeratePerKw")
txid <- publishTransaction(signedTx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ private class ReplaceableTxFunder(nodeParams: NodeParams,
case _ => signedTx.weight()
}
}
val actualFeerate = FeeratePerKw((actualFees * 1000) / actualWeight)
val actualFeerate = Transactions.fee2rate(actualFees, actualWeight)
if (actualFeerate >= txFeerate * 2) {
SignWalletInputsFailed(new RuntimeException(s"actual fee rate $actualFeerate is more than twice the requested fee rate $txFeerate"))
} else {
Expand Down Expand Up @@ -462,7 +462,7 @@ private class ReplaceableTxFunder(nodeParams: NodeParams,
packageWeight = commitTx.weight() + dummySignedTx.tx.weight()
// above, we asked bitcoin core to use the package weight to estimate fees when it built and funded this transaction, so we
// use the same package weight here to compute the actual fee rate that we get
actualFeerate = FeeratePerKw((processPsbtResponse.psbt.computeFees() * 1000) / packageWeight)
actualFeerate = Transactions.fee2rate(processPsbtResponse.psbt.computeFees(), packageWeight)
_ = require(actualFeerate < targetFeerate * 2, s"actual fee rate $actualFeerate is more than twice the requested fee rate $targetFeerate")

anchorTxFee = weight2fee(targetFeerate, packageWeight) - weight2fee(commitment.localCommit.spec.commitTxFeerate, commitTx.weight())
Expand All @@ -489,7 +489,7 @@ private class ReplaceableTxFunder(nodeParams: NodeParams,
val actualFees: Satoshi = processPsbtResponse.psbt.computeFees()
require(actualFees == fundTxResponse.fee, s"Bitcoin Core fees (${fundTxResponse.fee} do not match ours ($actualFees)")
val packageWeight = fundTxResponse.tx.weight() + htlcInputWeight.weight
val actualFeerate = FeeratePerKw((fundTxResponse.fee * 1000) / packageWeight)
val actualFeerate = Transactions.fee2rate(fundTxResponse.fee, packageWeight.toInt)
require(actualFeerate < targetFeerate * 2, s"actual fee rate $actualFeerate is more than twice the requested fee rate $targetFeerate")

(unsignedTx, fundTxResponse.amountIn)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class LocalOnchainKeyManager(override val wallet: String, seed: ByteVector, time
DeterministicWallet.encode(accountPub, prefix)
}

override def getWalletTimestamp(): TimestampSecond = timestamp

override def getDescriptors(account: Long): Descriptors = {
val keyPath = s"$rootPath/$account'".replace('\'', 'h') // Bitcoin Core understands both ' and h suffix for hardened derivation, and h is much easier to parse for external tools
val prefix: Int = chainHash match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fr.acinq.eclair.crypto.keymanager
import fr.acinq.bitcoin.psbt.Psbt
import fr.acinq.bitcoin.scalacompat.Crypto.PublicKey
import fr.acinq.bitcoin.scalacompat.DeterministicWallet.KeyPath
import fr.acinq.eclair.TimestampSecond
import fr.acinq.eclair.blockchain.bitcoind.rpc.BitcoinCoreClient.Descriptors

import scala.util.Try
Expand All @@ -24,6 +25,12 @@ trait OnchainKeyManager {
*/
def getPublicKey(keyPath: KeyPath): (PublicKey, String)

/**
*
* @return the creation time of the wallet managed by this key manager
*/
def getWalletTimestamp(): TimestampSecond

/**
*
* @param account account number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ class BitcoinCoreClientSpec extends TestKitBaseClass with BitcoindService with A

bitcoinClient.getMempoolTx(tx.txid).pipeTo(sender.ref)
val mempoolTx = sender.expectMsgType[MempoolTx]
val currentFeerate = FeeratePerKw(mempoolTx.fees * 1000 / tx.weight())
val currentFeerate = Transactions.fee2rate(mempoolTx.fees, tx.weight())

bitcoinClient.getMempoolPackage(Set(tx.txid)).pipeTo(sender.ref)
sender.expectMsg(Map(tx.txid -> mempoolTx))
Expand Down Expand Up @@ -1126,7 +1126,7 @@ class BitcoinCoreClientSpec extends TestKitBaseClass with BitcoindService with A
assert(tx.txOut.length == 2) // there must be a change output
bitcoinClient.getMempoolTx(tx.txid).pipeTo(sender.ref)
val mempoolTx = sender.expectMsgType[MempoolTx]
val currentFeerate = FeeratePerKw(mempoolTx.fees * 1000 / tx.weight())
val currentFeerate = Transactions.fee2rate(mempoolTx.fees, tx.weight())

val targetFeerate = currentFeerate * 1.5
bitcoinClient.cpfp(Set(OutPoint(tx, 3)), targetFeerate).pipeTo(sender.ref)
Expand Down

0 comments on commit 2fd5509

Please sign in to comment.