Skip to content

Commit

Permalink
Address PR feedback: rename CommitmentSpecLink, formatting and remote…
Browse files Browse the repository at this point in the history
… unused parameters.
  • Loading branch information
araspitzu committed Mar 24, 2020
1 parent 33e9ac5 commit dddac35
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ case object IN extends Direction { def opposite = OUT }
case object OUT extends Direction { def opposite = IN }
// @formatter:on

sealed trait CommitmentSpecLink
case object ToLocalLink extends CommitmentSpecLink
case object ToRemoteLink extends CommitmentSpecLink
case class DirectedHtlc(direction: Direction, add: UpdateAddHtlc) extends CommitmentSpecLink
sealed trait CommitmentOutput
object CommitmentOutput {
case object ToLocal extends CommitmentOutput
case object ToRemote extends CommitmentOutput
}
case class DirectedHtlc(direction: Direction, add: UpdateAddHtlc) extends CommitmentOutput

final case class CommitmentSpec(htlcs: Set[DirectedHtlc], feeratePerKw: Long, toLocal: MilliSatoshi, toRemote: MilliSatoshi) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import fr.acinq.bitcoin.Script._
import fr.acinq.bitcoin.SigVersion._
import fr.acinq.bitcoin._
import fr.acinq.eclair._
import fr.acinq.eclair.transactions.CommitmentOutput.ToLocal
import fr.acinq.eclair.transactions.Scripts._
import fr.acinq.eclair.wire.UpdateAddHtlc
import scodec.bits.ByteVector
Expand Down Expand Up @@ -206,14 +207,13 @@ object Transactions {

def decodeTxNumber(sequence: Long, locktime: Long): Long = ((sequence & 0xffffffL) << 24) + (locktime & 0xffffffL)


/**
* Represent a link between a commitment spec item (to-local, to-remote, htlc) and the actual output in the commit tx
* @param output transaction output
* @param redeemScript redeeem script that matches this output (most of them are p2wsh)
* @param redeemScript redeem script that matches this output (most of them are p2wsh)
* @param link commitment spec item this output is built from
*/
case class CommitmentOutputLink(output: TxOut, redeemScript: Seq[ScriptElt], link: CommitmentSpecLink)
case class CommitmentOutputLink(output: TxOut, redeemScript: Seq[ScriptElt], link: CommitmentOutput)

object CommitmentOutputLink {
def sort(a: CommitmentOutputLink, b: CommitmentOutputLink): Boolean = (a.link, b.link) match {
Expand All @@ -231,7 +231,7 @@ object Transactions {
remotePaymentPubkey: PublicKey,
localHtlcPubkey: PublicKey,
remoteHtlcPubkey: PublicKey,
spec: CommitmentSpec): IndexedSeq[CommitmentOutputLink] = {
spec: CommitmentSpec): Seq[CommitmentOutputLink] = {
val commitFee = commitTxFee(localDustLimit, spec)

val (toLocalAmount: Satoshi, toRemoteAmount: Satoshi) = if (localIsFunder) {
Expand All @@ -245,13 +245,13 @@ object Transactions {
CommitmentOutputLink(
TxOut(toLocalAmount, pay2wsh(toLocalDelayed(localRevocationPubkey, toLocalDelay, localDelayedPaymentPubkey))),
toLocalDelayed(localRevocationPubkey, toLocalDelay, localDelayedPaymentPubkey),
ToLocalLink))
CommitmentOutput.ToLocal))

if (toRemoteAmount >= localDustLimit) outputs.append(
CommitmentOutputLink(
TxOut(toRemoteAmount, pay2wpkh(remotePaymentPubkey)),
pay2pkh(remotePaymentPubkey),
ToRemoteLink))
CommitmentOutput.ToRemote))

trimOfferedHtlcs(localDustLimit, spec).foreach(htlc =>
outputs.append(CommitmentOutputLink(
Expand All @@ -265,15 +265,15 @@ object Transactions {
htlcReceived(localHtlcPubkey, remoteHtlcPubkey, localRevocationPubkey, ripemd160(htlc.add.paymentHash.bytes), htlc.add.cltvExpiry),
htlc)))

outputs.sortWith(CommitmentOutputLink.sort).toIndexedSeq
outputs.sortWith(CommitmentOutputLink.sort)
}

def makeCommitTx(commitTxInput: InputInfo,
commitTxNumber: Long,
localPaymentBasePoint: PublicKey,
remotePaymentBasePoint: PublicKey,
localIsFunder: Boolean,
outputs: IndexedSeq[CommitmentOutputLink]): CommitTx = {
outputs: Seq[CommitmentOutputLink]): CommitTx = {
val txnumber = obscuredCommitTxNumber(commitTxNumber, localIsFunder, localPaymentBasePoint, remotePaymentBasePoint)
val (sequence, locktime) = encodeTxNumber(txnumber)

Expand Down Expand Up @@ -339,7 +339,7 @@ object Transactions {
toLocalDelay: CltvExpiryDelta,
localDelayedPaymentPubkey: PublicKey,
feeratePerKw: Long,
outputs: IndexedSeq[CommitmentOutputLink]): (Seq[HtlcTimeoutTx], Seq[HtlcSuccessTx]) = {
outputs: Seq[CommitmentOutputLink]): (Seq[HtlcTimeoutTx], Seq[HtlcSuccessTx]) = {

val htlcTimeoutTxs = outputs.zipWithIndex.collect {
case (co@CommitmentOutputLink(_, _, DirectedHtlc(OUT, _)), outputIndex) =>
Expand All @@ -352,7 +352,7 @@ object Transactions {
(htlcTimeoutTxs, htlcSuccessTxs)
}

def makeClaimHtlcSuccessTx(commitTx: Transaction, outputs: IndexedSeq[CommitmentOutputLink], localDustLimit: Satoshi, localHtlcPubkey: PublicKey, remoteHtlcPubkey: PublicKey, remoteRevocationPubkey: PublicKey, localFinalScriptPubKey: ByteVector, htlc: UpdateAddHtlc, feeratePerKw: Long): ClaimHtlcSuccessTx = {
def makeClaimHtlcSuccessTx(commitTx: Transaction, outputs: Seq[CommitmentOutputLink], localDustLimit: Satoshi, localHtlcPubkey: PublicKey, remoteHtlcPubkey: PublicKey, remoteRevocationPubkey: PublicKey, localFinalScriptPubKey: ByteVector, htlc: UpdateAddHtlc, feeratePerKw: Long): ClaimHtlcSuccessTx = {
val redeemScript = htlcOffered(remoteHtlcPubkey, localHtlcPubkey, remoteRevocationPubkey, ripemd160(htlc.paymentHash.bytes))
val Some(outputIndex) = outputs.zipWithIndex.find {
case (CommitmentOutputLink(_, _, DirectedHtlc(OUT, outgoingHtlc)), _) => outgoingHtlc.id == htlc.id
Expand All @@ -378,7 +378,7 @@ object Transactions {
ClaimHtlcSuccessTx(input, tx1)
}

def makeClaimHtlcTimeoutTx(commitTx: Transaction, outputs: IndexedSeq[CommitmentOutputLink], localDustLimit: Satoshi, localHtlcPubkey: PublicKey, remoteHtlcPubkey: PublicKey, remoteRevocationPubkey: PublicKey, localFinalScriptPubKey: ByteVector, htlc: UpdateAddHtlc, feeratePerKw: Long): ClaimHtlcTimeoutTx = {
def makeClaimHtlcTimeoutTx(commitTx: Transaction, outputs: Seq[CommitmentOutputLink], localDustLimit: Satoshi, localHtlcPubkey: PublicKey, remoteHtlcPubkey: PublicKey, remoteRevocationPubkey: PublicKey, localFinalScriptPubKey: ByteVector, htlc: UpdateAddHtlc, feeratePerKw: Long): ClaimHtlcTimeoutTx = {
val redeemScript = htlcReceived(remoteHtlcPubkey, localHtlcPubkey, remoteRevocationPubkey, ripemd160(htlc.paymentHash.bytes), htlc.cltvExpiry)
val Some(outputIndex) = outputs.zipWithIndex.find {
case (CommitmentOutputLink(_, _, DirectedHtlc(IN, incomingHtlc)), _) => incomingHtlc.id == htlc.id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ import scala.util.{Failure, Random, Success, Try}
*/

class TransactionsSpec extends FunSuite with Logging {

val localFundingPriv = PrivateKey(randomBytes32 :+ 1.toByte)
val remoteFundingPriv = PrivateKey(randomBytes32 :+ 1.toByte)
val localRevocationPriv = PrivateKey(randomBytes32 :+ 1.toByte)
val localPaymentPriv = PrivateKey(randomBytes32 :+ 1.toByte)
val localDelayedPaymentPriv = PrivateKey(randomBytes32 :+ 1.toByte)
val remotePaymentPriv = PrivateKey(randomBytes32 :+ 1.toByte)
val localHtlcPriv = PrivateKey(randomBytes32 :+ 1.toByte)
val remoteHtlcPriv = PrivateKey(randomBytes32 :+ 1.toByte)
val localFundingPriv = PrivateKey(randomBytes32)
val remoteFundingPriv = PrivateKey(randomBytes32)
val localRevocationPriv = PrivateKey(randomBytes32)
val localPaymentPriv = PrivateKey(randomBytes32)
val localDelayedPaymentPriv = PrivateKey(randomBytes32)
val remotePaymentPriv = PrivateKey(randomBytes32)
val localHtlcPriv = PrivateKey(randomBytes32)
val remoteHtlcPriv = PrivateKey(randomBytes32)
val finalPubKeyScript = Script.write(Script.pay2wpkh(PrivateKey(randomBytes32).publicKey))
val commitInput = Funding.makeFundingInputInfo(randomBytes32, 0, Btc(1), localFundingPriv.publicKey, remoteFundingPriv.publicKey)
val toLocalDelay = 144
Expand Down Expand Up @@ -90,12 +89,6 @@ class TransactionsSpec extends FunSuite with Logging {
}

test("check pre-computed transaction weights") {
val localRevocationPriv = PrivateKey(randomBytes32)
val localPaymentPriv = PrivateKey(randomBytes32)
val remotePaymentPriv = PrivateKey(randomBytes32)
val localHtlcPriv = PrivateKey(randomBytes32)
val remoteHtlcPriv = PrivateKey(randomBytes32)
val localFinalPriv = PrivateKey(randomBytes32)
val finalPubKeyScript = Script.write(Script.pay2wpkh(PrivateKey(randomBytes32).publicKey))
val localDustLimit = 546 sat
val toLocalDelay = CltvExpiryDelta(144)
Expand Down Expand Up @@ -187,14 +180,6 @@ class TransactionsSpec extends FunSuite with Logging {
}

test("generate valid commitment and htlc transactions") {
val localFundingPriv = PrivateKey(randomBytes32 :+ 1.toByte)
val remoteFundingPriv = PrivateKey(randomBytes32 :+ 1.toByte)
val localRevocationPriv = PrivateKey(randomBytes32 :+ 1.toByte)
val localPaymentPriv = PrivateKey(randomBytes32 :+ 1.toByte)
val localDelayedPaymentPriv = PrivateKey(randomBytes32 :+ 1.toByte)
val remotePaymentPriv = PrivateKey(randomBytes32 :+ 1.toByte)
val localHtlcPriv = PrivateKey(randomBytes32 :+ 1.toByte)
val remoteHtlcPriv = PrivateKey(randomBytes32 :+ 1.toByte)
val finalPubKeyScript = Script.write(Script.pay2wpkh(PrivateKey(randomBytes32).publicKey))
val commitInput = Funding.makeFundingInputInfo(randomBytes32, 0, Btc(1), localFundingPriv.publicKey, remoteFundingPriv.publicKey)
val toLocalDelay = CltvExpiryDelta(144)
Expand Down Expand Up @@ -345,21 +330,15 @@ class TransactionsSpec extends FunSuite with Logging {
}

test("sort the htlc outputs using BIP69") {

val localFundingPriv = PrivateKey(hex"a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1" :+ 1.toByte)
val remoteFundingPriv = PrivateKey(hex"a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2" :+ 1.toByte)
val localRevocationPriv = PrivateKey(hex"a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3" :+ 1.toByte)
val localPaymentPriv = PrivateKey(hex"a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4" :+ 1.toByte)
val localDelayedPaymentPriv = PrivateKey(hex"a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5" :+ 1.toByte)
val remotePaymentPriv = PrivateKey(hex"a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6" :+ 1.toByte)
val localHtlcPriv = PrivateKey(hex"a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7" :+ 1.toByte)
val remoteHtlcPriv = PrivateKey(hex"a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8" :+ 1.toByte)
val finalPubKeyScript = Script.write(Script.pay2wpkh(PrivateKey(hex"a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9").publicKey))
val localFundingPriv = PrivateKey(hex"a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1")
val remoteFundingPriv = PrivateKey(hex"a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2")
val localRevocationPriv = PrivateKey(hex"a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3")
val localPaymentPriv = PrivateKey(hex"a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4")
val localDelayedPaymentPriv = PrivateKey(hex"a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5")
val remotePaymentPriv = PrivateKey(hex"a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6")
val localHtlcPriv = PrivateKey(hex"a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7")
val remoteHtlcPriv = PrivateKey(hex"a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8a8")
val commitInput = Funding.makeFundingInputInfo(ByteVector32(hex"a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0"), 0, Btc(1), localFundingPriv.publicKey, remoteFundingPriv.publicKey)
val toLocalDelay = CltvExpiryDelta(144)
val localDustLimit = Satoshi(546)
val feeratePerKw = 22000


// htlc1 and htlc2 are two regular incoming HTLCs with different amounts.
// htlc2 and htlc3 have the same amounts and should be sorted according to their scriptPubKey
Expand All @@ -381,7 +360,7 @@ class TransactionsSpec extends FunSuite with Logging {

val commitTxNumber = 0x404142434446L
val commitTx = {
val outputs = makeCommitTxOutputs(true, localDustLimit, localRevocationPriv.publicKey, toLocalDelay, localDelayedPaymentPriv.publicKey, remotePaymentPriv.publicKey, localHtlcPriv.publicKey, remoteHtlcPriv.publicKey, spec)
val outputs = makeCommitTxOutputs(true, localDustLimit, localRevocationPriv.publicKey, CltvExpiryDelta(toLocalDelay), localDelayedPaymentPriv.publicKey, remotePaymentPriv.publicKey, localHtlcPriv.publicKey, remoteHtlcPriv.publicKey, spec)
val txinfo = makeCommitTx(commitInput, commitTxNumber, localPaymentPriv.publicKey, remotePaymentPriv.publicKey, true, outputs)
val localSig = Transactions.sign(txinfo, localPaymentPriv)
val remoteSig = Transactions.sign(txinfo, remotePaymentPriv)
Expand Down

0 comments on commit dddac35

Please sign in to comment.