Skip to content

Commit

Permalink
Add non_initiator_pays_commit_fees channel flag
Browse files Browse the repository at this point in the history
We add a non-standard channel flag to `open_channel2` to allow wallets
to ask their peer to pay the commit tx fees, even when they're not the
channel opener. This is necessary for on-the-fly funding, until we can
move to 0-fee commit txs which will make it obsolete.
  • Loading branch information
t-bast committed Jun 27, 2024
1 parent 6fcaaca commit c06b9a1
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -666,8 +666,16 @@ case class RemoteParams(nodeId: PublicKey,
initFeatures: Features[InitFeature],
upfrontShutdownScript_opt: Option[ByteVector])

case class ChannelFlags(announceChannel: Boolean) {
override def toString: String = s"ChannelFlags(announceChannel=$announceChannel)"
/**
* The [[nonInitiatorPaysCommitFees]] parameter is set to true when the sender wants the receiver to pay the commitment transaction fees.
* This is not part of the BOLTs and won't be needed anymore once commitment transactions don't pay any on-chain fees.
*/
case class ChannelFlags(nonInitiatorPaysCommitFees: Boolean, announceChannel: Boolean) {
override def toString: String = s"ChannelFlags(announceChannel=$announceChannel, nonInitiatorPaysCommitFees=$nonInitiatorPaysCommitFees)"
}

object ChannelFlags {
def apply(announceChannel: Boolean): ChannelFlags = ChannelFlags(nonInitiatorPaysCommitFees = false, announceChannel = announceChannel)
}

/** Information about what triggered the opening of the channel */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ object CommonCodecs {

val listofsignatures: Codec[List[ByteVector64]] = listOfN(uint16, bytes64)

val channelflags: Codec[ChannelFlags] = (ignore(7) :: bool).as[ChannelFlags]
val channelflags: Codec[ChannelFlags] = (ignore(6) :: bool :: bool).as[ChannelFlags]

val ipv4address: Codec[Inet4Address] = bytes(4).xmap(b => InetAddress.getByAddress(b.toArray).asInstanceOf[Inet4Address], a => ByteVector(a.getAddress))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
}
},
"channelFlags" : {
"nonInitiatorPaysCommitFees" : false,
"announceChannel" : true
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
}
},
"channelFlags" : {
"nonInitiatorPaysCommitFees" : false,
"announceChannel" : true
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
}
},
"channelFlags" : {
"nonInitiatorPaysCommitFees" : false,
"announceChannel" : true
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
}
},
"channelFlags" : {
"nonInitiatorPaysCommitFees" : false,
"announceChannel" : false
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
}
},
"channelFlags" : {
"nonInitiatorPaysCommitFees" : false,
"announceChannel" : false
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
}
},
"channelFlags" : {
"nonInitiatorPaysCommitFees" : false,
"announceChannel" : false
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ class JsonSerializersSpec extends TestKitBaseClass with AnyFunSuiteLike with Mat
| "initFeatures": { "activated": {}, "unknown": [] }
| },
| "channelFlags": {
| "nonInitiatorPaysCommitFees": false,
| "announceChannel": true
| }
| },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,20 @@ class CommonCodecsSpec extends AnyFunSuite {

test("encode/decode channel flags") {
val testCases = Map(
bin"00000000" -> ChannelFlags(announceChannel = false),
bin"00000001" -> ChannelFlags(announceChannel = true),
bin"00000000" -> ChannelFlags(nonInitiatorPaysCommitFees = false, announceChannel = false),
bin"00000001" -> ChannelFlags(nonInitiatorPaysCommitFees = false, announceChannel = true),
bin"00000010" -> ChannelFlags(nonInitiatorPaysCommitFees = true, announceChannel = false),
bin"00000011" -> ChannelFlags(nonInitiatorPaysCommitFees = true, announceChannel = true),
)
testCases.foreach { case (bin, obj) =>
assert(channelflags.decode(bin).require == DecodeResult(obj, BitVector.empty))
assert(channelflags.encode(obj).require == bin)
}

// BOLT 2: The receiving node MUST [...] ignore undefined bits in channel_flags.
assert(channelflags.decode(bin"11111111").require == DecodeResult(ChannelFlags(announceChannel = true), BitVector.empty))
assert(channelflags.decode(bin"11111110").require == DecodeResult(ChannelFlags(announceChannel = false), BitVector.empty))
assert(channelflags.decode(bin"11111111").require == DecodeResult(ChannelFlags(nonInitiatorPaysCommitFees = true, announceChannel = true), BitVector.empty))
assert(channelflags.decode(bin"11111110").require == DecodeResult(ChannelFlags(nonInitiatorPaysCommitFees = true, announceChannel = false), BitVector.empty))
assert(channelflags.decode(bin"11111100").require == DecodeResult(ChannelFlags(nonInitiatorPaysCommitFees = false, announceChannel = false), BitVector.empty))
}

test("encode/decode with rgb codec") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,9 @@ class LightningMessageCodecsSpec extends AnyFunSuite {
val defaultOpen = OpenDualFundedChannel(BlockHash(ByteVector32.Zeroes), ByteVector32.One, FeeratePerKw(5000 sat), FeeratePerKw(4000 sat), 250_000 sat, 500 sat, UInt64(50_000), 15 msat, CltvExpiryDelta(144), 483, 650_000, publicKey(1), publicKey(2), publicKey(3), publicKey(4), publicKey(5), publicKey(6), publicKey(7), ChannelFlags(true))
val defaultEncodedWithoutFlags = hex"0040 0000000000000000000000000000000000000000000000000000000000000000 0100000000000000000000000000000000000000000000000000000000000000 00001388 00000fa0 000000000003d090 00000000000001f4 000000000000c350 000000000000000f 0090 01e3 0009eb10 031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f 024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d0766 02531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe337 03462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b 0362c0a046dacce86ddd0343c6d3c7c79c2208ba0d9c9cf24a6d046d21d21f90f7 03f006a18d5653c4edf5391ff23a61f03ff83d237e880ee61187fa9f379a028e0a 02989c0b76cb563971fdc9bef31ec06c3560f3249d6ee9e5d83c57625596e05f6f"
val testCases = Seq(
defaultEncodedWithoutFlags ++ hex"00" -> ChannelFlags(false),
defaultEncodedWithoutFlags ++ hex"a2" -> ChannelFlags(false),
defaultEncodedWithoutFlags ++ hex"ff" -> ChannelFlags(true),
defaultEncodedWithoutFlags ++ hex"00" -> ChannelFlags(nonInitiatorPaysCommitFees = false, announceChannel = false),
defaultEncodedWithoutFlags ++ hex"a2" -> ChannelFlags(nonInitiatorPaysCommitFees = true, announceChannel = false),
defaultEncodedWithoutFlags ++ hex"ff" -> ChannelFlags(nonInitiatorPaysCommitFees = true, announceChannel = true),
)
testCases.foreach { case (bin, flags) =>
val decoded = lightningMessageCodec.decode(bin.bits).require.value
Expand Down

0 comments on commit c06b9a1

Please sign in to comment.