Skip to content

Commit f43bd8c

Browse files
committed
Change BlockHeightOffset from 16bit to 32bit
It's true that, as mentioned in the code, OP_CSV only allows 16bit offsets. However it is possible to have blocks that are more than UINT16_MAX blocks apart, and lightning's accept_channel message contains a 32bit block height offset for the minimum_depth field. Because of this, the AcceptChannel type was mis-using the 32bit BlockHeight type to represent a relative block height, even though BlockHeight is intended to represent absolute block height. For the sake of using types consistently I've changed BlockHeightOffset to be 32bits and changed the type of AcceptChannel.MinimumDepth to be a BlockHeightOffset.
1 parent d6b136d commit f43bd8c

File tree

21 files changed

+106
-110
lines changed

21 files changed

+106
-110
lines changed

src/DotNetLightning.Core/Channel/Channel.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ module Channel =
253253
MaxHTLCValueInFlightMsat = localParams.MaxHTLCValueInFlightMSat
254254
ChannelReserveSatoshis = localParams.ChannelReserveSatoshis
255255
HTLCMinimumMSat = localParams.HTLCMinimumMSat
256-
MinimumDepth = cs.Config.ChannelHandshakeConfig.MinimumDepth.Value |> uint32 |> BlockHeight
256+
MinimumDepth = cs.Config.ChannelHandshakeConfig.MinimumDepth
257257
ToSelfDelay = localParams.ToSelfDelay
258258
MaxAcceptedHTLCs = localParams.MaxAcceptedHTLCs
259259
FundingPubKey = channelKeys.FundingKey.PubKey

src/DotNetLightning.Core/Channel/ChannelCommands.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ type RemoteParams = {
127127
DelayedPaymentBasePoint = msg.DelayedPaymentBasepoint
128128
HTLCBasePoint = msg.HTLCBasepoint
129129
Features = remoteInit.Features
130-
MinimumDepth = BlockHeightOffset <| uint16 msg.MinimumDepth.Value
130+
MinimumDepth = msg.MinimumDepth
131131
}
132132

133133
static member FromOpenChannel (nodeId) (remoteInit: Init) (msg: OpenChannel) (channelHandshakeConfig: ChannelHandshakeConfig) =

src/DotNetLightning.Core/Channel/ChannelConstants.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ module ChannelConstants =
2727

2828
/// The amount of time we require our counterparty wait to claim their money (i.e. time between when
2929
/// we, or our watchtower, mush check for them having a broadcast a theft transaction).
30-
let BREAKDOWN_TIMEOUT = BlockHeightOffset(6us * 24us * 7us) // one week
31-
let MAX_LOCAL_BREAKDOWN_TIMEOUT = BlockHeightOffset(6us * 24us * 14us) // two weeks
30+
let BREAKDOWN_TIMEOUT = BlockHeightOffset(6u * 24u * 7u) // one week
31+
let MAX_LOCAL_BREAKDOWN_TIMEOUT = BlockHeightOffset(6u * 24u * 14u) // two weeks
3232

3333
/// Specified in BOLT 11
34-
let MIN_CLTV_EXPIRY = 9us |> BlockHeightOffset
34+
let MIN_CLTV_EXPIRY = 9u |> BlockHeightOffset
3535

3636
let MAX_CLTV_EXPIRY = BREAKDOWN_TIMEOUT
3737

@@ -61,4 +61,4 @@ module ChannelConstants =
6161
let HTLC_SUCCESS_TX_WEIGHT = 703UL
6262

6363
[<Literal>]
64-
let HTLC_TIMEOUT_TX_WEIGHT = 663UL
64+
let HTLC_TIMEOUT_TX_WEIGHT = 663UL

src/DotNetLightning.Core/Channel/ChannelError.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ module internal AcceptChannelMsgValidation =
465465
let check4 = check msg.MaxAcceptedHTLCs (<) config.MinMaxAcceptedHTLCs "max accepted htlcs (%A) is less than the user specified limit (%A)"
466466
let check5 = check msg.DustLimitSatoshis (<) config.MinDustLimitSatoshis "dust limit satoshis (%A) is less then the user specified limit (%A)"
467467
let check6 = check msg.DustLimitSatoshis (>) config.MaxDustLimitSatoshis "dust limit satoshis (%A) is greater then the user specified limit (%A)"
468-
let check7 = check (msg.MinimumDepth.Value) (>) (config.MaxMinimumDepth.Value |> uint32) "We consider the minimum depth (%A) to be unreasonably large. Our max minimum depth is (%A)"
468+
let check7 = check msg.MinimumDepth (>) config.MaxMinimumDepth "We consider the minimum depth (%A) to be unreasonably large. Our max minimum depth is (%A)"
469469

470470
(check1 |> Validation.ofResult) *^> check2 *^> check3 *^> check4 *^> check5 *^> check6 *^> check7
471471

src/DotNetLightning.Core/Payment/PaymentRequest.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ type private Bolt11Data = {
451451
let schId = r.ReadULongBE(64) |> ShortChannelId.FromUInt64
452452
let feeBase = r.ReadULongBE(32) |> LNMoney.MilliSatoshis
453453
let feeProportional = r.ReadULongBE(32) |> uint32
454-
let cltvExpiryDelta = r.ReadULongBE(16) |> uint16 |> BlockHeightOffset
454+
let cltvExpiryDelta = r.ReadULongBE(16) |> uint32 |> BlockHeightOffset
455455
let hopInfo = { NodeId = nodeId
456456
ShortChannelId = schId
457457
FeeBase = feeBase

src/DotNetLightning.Core/Routing/Graph.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ module Graph =
268268
) ""
269269

270270
module internal RoutingHeuristics =
271-
let BLOCK_TIME_TWO_MONTHS = 8640us |> BlockHeightOffset
271+
let BLOCK_TIME_TWO_MONTHS = 8640u |> BlockHeightOffset
272272
let CAPACITY_CHANNEL_LOW = LNMoney.Satoshis(1000L)
273273
let CAPACITY_CHANNEL_HIGH = DotNetLightning.Channel.ChannelConstants.MAX_FUNDING_SATOSHIS.Satoshi |> LNMoney.Satoshis
274274

src/DotNetLightning.Core/Routing/Router.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module Routing =
5656
/// Defined in BOLT7
5757
let ROUTE_MAX_LENGTH = 20
5858
/// Max allowed cltv for a route
59-
let DEFAULT_ROUTE_MAX_CLTV = BlockHeightOffset(1008us)
59+
let DEFAULT_ROUTE_MAX_CLTV = BlockHeightOffset(1008u)
6060
/// The default number of routes we'll search for when findRoute is called with randomize = true
6161
[<Literal>]
6262
let private DEFAULT_ROUTES_COUNT = 3

src/DotNetLightning.Core/Serialize/Msgs/Msgs.fs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ with
453453
this.ChannelReserveSatoshis <- Money.Satoshis(ls.ReadInt64(false))
454454
this.HTLCMinimumMsat <- LNMoney.MilliSatoshis(ls.ReadInt64(false))
455455
this.FeeRatePerKw <- FeeRatePerKw(ls.ReadUInt32(false))
456-
this.ToSelfDelay <- BlockHeightOffset(ls.ReadUInt16(false))
456+
this.ToSelfDelay <- BlockHeightOffset(ls.ReadUInt16(false) |> uint32)
457457
this.MaxAcceptedHTLCs <- ls.ReadUInt16(false)
458458
this.FundingPubKey <- ls.ReadPubKey()
459459
this.RevocationBasepoint <- ls.ReadPubKey()
@@ -475,7 +475,7 @@ with
475475
ls.Write(this.ChannelReserveSatoshis.Satoshi, false)
476476
ls.Write(this.HTLCMinimumMsat.MilliSatoshi, false)
477477
ls.Write(this.FeeRatePerKw.Value, false)
478-
ls.Write(this.ToSelfDelay.Value, false)
478+
ls.Write(this.ToSelfDelay.Value |> uint16, false)
479479
ls.Write(this.MaxAcceptedHTLCs, false)
480480
ls.Write(this.FundingPubKey.ToBytes())
481481
ls.Write(this.RevocationBasepoint.ToBytes())
@@ -493,7 +493,7 @@ type AcceptChannel = {
493493
mutable MaxHTLCValueInFlightMsat: LNMoney
494494
mutable ChannelReserveSatoshis: Money
495495
mutable HTLCMinimumMSat: LNMoney
496-
mutable MinimumDepth: BlockHeight
496+
mutable MinimumDepth: BlockHeightOffset
497497
mutable ToSelfDelay: BlockHeightOffset
498498
mutable MaxAcceptedHTLCs: uint16
499499
mutable FundingPubKey: PubKey
@@ -513,8 +513,8 @@ with
513513
this.MaxHTLCValueInFlightMsat <- ls.ReadUInt64(false) |> LNMoney.MilliSatoshis
514514
this.ChannelReserveSatoshis <- ls.ReadUInt64(false) |> Money.Satoshis
515515
this.HTLCMinimumMSat <- ls.ReadUInt64(false) |> LNMoney.MilliSatoshis
516-
this.MinimumDepth <- ls.ReadUInt32(false) |> BlockHeight
517-
this.ToSelfDelay <- ls.ReadUInt16(false) |> BlockHeightOffset
516+
this.MinimumDepth <- ls.ReadUInt32(false) |> BlockHeightOffset
517+
this.ToSelfDelay <- ls.ReadUInt16(false) |> uint32 |> BlockHeightOffset
518518
this.MaxAcceptedHTLCs <- ls.ReadUInt16(false)
519519
this.FundingPubKey <- ls.ReadPubKey()
520520
this.RevocationBasepoint <- ls.ReadPubKey()
@@ -532,7 +532,7 @@ with
532532
ls.Write(this.ChannelReserveSatoshis.Satoshi, false)
533533
ls.Write(this.HTLCMinimumMSat.MilliSatoshi, false)
534534
ls.Write(this.MinimumDepth.Value, false)
535-
ls.Write(this.ToSelfDelay.Value, false)
535+
ls.Write(this.ToSelfDelay.Value |> uint16, false)
536536
ls.Write(this.MaxAcceptedHTLCs, false)
537537
ls.Write(this.FundingPubKey.ToBytes())
538538
ls.Write(this.RevocationBasepoint.ToBytes())
@@ -1094,7 +1094,7 @@ type UnsignedChannelUpdate = {
10941094
this.Timestamp <- ls.ReadUInt32(false)
10951095
this.MessageFlags <- ls.ReadByte()
10961096
this.ChannelFlags <- ls.ReadByte()
1097-
this.CLTVExpiryDelta <- ls.ReadUInt16(false) |> BlockHeightOffset
1097+
this.CLTVExpiryDelta <- ls.ReadUInt16(false) |> uint32 |> BlockHeightOffset
10981098
this.HTLCMinimumMSat <- ls.ReadUInt64(false) |> LNMoney.MilliSatoshis
10991099
this.FeeBaseMSat <- ls.ReadUInt32(false) |> uint64 |> LNMoney.MilliSatoshis
11001100
this.FeeProportionalMillionths <- ls.ReadUInt32(false)

src/DotNetLightning.Core/Utils/Config.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type ChannelHandshakeConfig = {
1616

1717
static member Zero =
1818
{
19-
MinimumDepth = BlockHeightOffset 6us
19+
MinimumDepth = BlockHeightOffset 6u
2020
}
2121

2222
/// Optional Channel limits which are applied during channel creation.
@@ -70,7 +70,7 @@ type ChannelHandshakeLimits = {
7070
MinMaxAcceptedHTLCs = 0us
7171
MinDustLimitSatoshis = Money.Satoshis(546m)
7272
MaxDustLimitSatoshis = Money.Coins(21_000_000m)
73-
MaxMinimumDepth = 144us |> BlockHeightOffset
73+
MaxMinimumDepth = 144u |> BlockHeightOffset
7474
ForceChannelAnnouncementPreference = true
7575
MaxClosingNegotiationIterations = 20
7676
}

src/DotNetLightning.Core/Utils/Primitives.fs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,31 @@ module Primitives =
2929
output.[1] <- byte d
3030
output
3131

32-
/// Absolute block height
32+
/// Absolute block height
3333
[<Struct>]
3434
type BlockHeight = | BlockHeight of uint32 with
3535
static member Zero = 0u |> BlockHeight
3636
static member One = 1u |> BlockHeight
3737
member x.Value = let (BlockHeight v) = x in v
38-
member x.AsOffset() =
39-
x.Value |> Checked.uint16 |> BlockHeightOffset
4038
static member (+) (a: BlockHeight, b: BlockHeightOffset) =
41-
a.Value + (uint32 b.Value ) |> BlockHeight
39+
a.Value + b.Value |> BlockHeight
4240

4341
static member (-) (a: BlockHeight, b: BlockHeightOffset) =
44-
a.Value - (uint32 b.Value) |> BlockHeight
42+
a.Value - b.Value |> BlockHeight
4543

4644
static member (-) (a: BlockHeight, b: BlockHeight) =
47-
a.Value - (b.Value) |> uint16 |> BlockHeightOffset
45+
a.Value - b.Value |> BlockHeightOffset
4846

4947
/// **Description**
5048
///
51-
/// Relative block height used for `OP_CSV` locks,
52-
/// Since OP_CSV allow only block number of 0 ~ 65535, it is safe
53-
/// to restrict into the range smaller than BlockHeight
54-
and [<Struct>] BlockHeightOffset = | BlockHeightOffset of uint16 with
49+
/// Relative block height used for `OP_CSV` locks.
50+
and [<Struct>] BlockHeightOffset = | BlockHeightOffset of uint32 with
5551
member x.Value = let (BlockHeightOffset v) = x in v
56-
static member op_Implicit (v: uint16) =
52+
static member op_Implicit (v: uint32) =
5753
BlockHeightOffset v
58-
static member One = BlockHeightOffset(1us)
59-
static member Zero = BlockHeightOffset(0us)
60-
static member MaxValue = UInt16.MaxValue |> BlockHeightOffset
54+
static member One = BlockHeightOffset(1u)
55+
static member Zero = BlockHeightOffset(0u)
56+
static member MaxValue = UInt32.MaxValue |> BlockHeightOffset
6157
static member (+) (a: BlockHeightOffset, b: BlockHeightOffset) =
6258
a.Value + b.Value |> BlockHeightOffset
6359
static member (-) (a: BlockHeightOffset, b: BlockHeightOffset) =

src/DotNetLightning.Infrastructure/ActorManagers/ChannelManager.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ type ChannelManager(log: ILogger<ChannelManager>,
119119
found <- true
120120
let txIndex = txInBlock |> fst |> TxIndexInBlock
121121
let (nodeId, _) = kv.Value
122-
let depth = 1us |> BlockHeightOffset
122+
let depth = 1u |> BlockHeightOffset
123123
let newValue = (nodeId, Some(txIndex, height))
124124
this.FundingTxs.TryUpdate(kv.Key, newValue, kv.Value) |> ignore
125125
for kv in this.Actors do

src/DotNetLightning.Infrastructure/ChainConfig.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ type ChainConfig() =
7777
/// if `htlc_minimum_msat` sent by other nodes `open_channel` is larger than this.
7878
/// re will refuse to open channel.
7979
member val MaxHTLCMinimumMSat: LNMoney = LNMoney.MilliSatoshis(100L) with get, set
80-
member val ToRemoteDelayBlocks: BlockHeightOffset = BlockHeightOffset(720us) with get, set
81-
member val MaxToLocalDelayBlocks: BlockHeightOffset = BlockHeightOffset(2016us) with get, set
80+
member val ToRemoteDelayBlocks: BlockHeightOffset = BlockHeightOffset(720u) with get, set
81+
member val MaxToLocalDelayBlocks: BlockHeightOffset = BlockHeightOffset(2016u) with get, set
8282

8383
/// The number of confirmation when we consider the funding tx is locked.
84-
member val MinimumDepth = BlockHeightOffset(3us) with get, set
84+
member val MinimumDepth = BlockHeightOffset(3u) with get, set
8585
/// We do not accept channel if other node wants to wait more than this (in block number) before funding tx is locked.
86-
member val MaxMinimumDepth = BlockHeightOffset(144us) with get, set
86+
member val MaxMinimumDepth = BlockHeightOffset(144u) with get, set
8787

8888
// member val SmartFeeNBlocks: BlockHeight
8989
member val FeeBaseMSat: LNMoney = LNMoney.MilliSatoshis(1000L) with get, set

tests/DotNetLightning.Core.Tests/Generators/Msgs.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ let openChannelGen =
8989
<*> (moneyGen)
9090
<*> (lnMoneyGen)
9191
<*> (FeeRatePerKw <!> Arb.generate<uint32>)
92-
<*> (BlockHeightOffset <!> Arb.generate<uint16>)
92+
<*> (BlockHeightOffset <!> Arb.generate<uint32>)
9393
<*> Arb.generate<uint16>
9494
<*> pubKeyGen
9595
<*> pubKeyGen
@@ -126,8 +126,8 @@ let acceptChannelGen =
126126
<*> lnMoneyGen
127127
<*> moneyGen
128128
<*> lnMoneyGen
129-
<*> (Arb.generate<uint32> |> Gen.map(BlockHeight))
130-
<*> (BlockHeightOffset <!> Arb.generate<uint16>)
129+
<*> (BlockHeightOffset <!> Arb.generate<uint32>)
130+
<*> (BlockHeightOffset <!> Arb.generate<uint32>)
131131
<*> Arb.generate<uint16>
132132
<*> pubKeyGen
133133
<*> pubKeyGen
@@ -420,7 +420,7 @@ let private unsignedChannelUpdateGen = gen {
420420
let! ts = Arb.generate<uint32>
421421
let! messageFlags = Arb.generate<uint8>
422422
let! channelFlags = Arb.generate<uint8>
423-
let! cltvE = BlockHeightOffset <!> Arb.generate<uint16>
423+
let! cltvE = BlockHeightOffset <!> Arb.generate<uint32>
424424
let! htlcMin = lnMoneyGen
425425
let! feeBase = lnMoneyGen
426426
let! feePM = Arb.generate<uint32>

tests/DotNetLightning.Core.Tests/GraphTests.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module Constants =
2020
let DEFAULT_ROUTE_PARAMS = { RouteParams.Randomize = false
2121
MaxFeeBase = LNMoney.MilliSatoshis(21000L)
2222
MaxFeePCT = 0.03
23-
RouteMaxCLTV = 2016us |> BlockHeightOffset
23+
RouteMaxCLTV = 2016u |> BlockHeightOffset
2424
RouteMaxLength = 6
2525
Ratios = None }
2626
let privKey1 = Key(hex.DecodeData("0101010101010101010101010101010101010101010101010101010101010101"))
@@ -52,7 +52,7 @@ let makeUpdateCore(shortChannelId: ShortChannelId,
5252
cltvDelta: BlockHeightOffset option
5353
): (ChannelDesc * UnsignedChannelUpdate) =
5454
let minHtlc = Option.defaultValue Constants.DEFAULT_AMOUNT_MSAT minHtlc
55-
let cltvDelta = Option.defaultValue (BlockHeightOffset(0us)) cltvDelta
55+
let cltvDelta = Option.defaultValue (BlockHeightOffset(0u)) cltvDelta
5656
let desc = { ChannelDesc.ShortChannelId = shortChannelId
5757
A = nodeid1
5858
B = nodeid2 }

tests/DotNetLightning.Core.Tests/KeyRepositoryTests.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ let tests =
9191
fundingAmount
9292

9393
let localDustLimit = Money.Satoshis(546L)
94-
let toLocalDelay = 200us |> BlockHeightOffset
94+
let toLocalDelay = 200u |> BlockHeightOffset
9595
let specBase = { CommitmentSpec.HTLCs = htlcMap; FeeRatePerKw = 15000u |> FeeRatePerKw;
9696
ToLocal = LNMoney.MilliSatoshis(6988000000L); ToRemote = 3000000000L |> LNMoney.MilliSatoshis}
9797
let commitTx =
@@ -115,7 +115,7 @@ let tests =
115115
Expect.isTrue (commitTx3.CanExtractTransaction()) (sprintf "failed to finalize commitTx %A" commitTx3)
116116

117117
let remoteDustLimit = Money.Satoshis(1000L)
118-
let remoteDelay = 160us |> BlockHeightOffset
118+
let remoteDelay = 160u |> BlockHeightOffset
119119
let remoteCommitTx =
120120
Transactions.makeCommitTx fundingSCoin
121121
0UL

tests/DotNetLightning.Core.Tests/PaymentTests.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,12 @@ let tests =
119119
ShortChannelId = ShortChannelId.FromUInt64(72623859790382856UL)
120120
FeeBase = 1L |> LNMoney.MilliSatoshis
121121
FeeProportionalMillionths = 20u
122-
CLTVExpiryDelta = 3us |> BlockHeightOffset }
122+
CLTVExpiryDelta = 3u |> BlockHeightOffset }
123123
{ ExtraHop.NodeId = "039e03a901b85534ff1e92c43c74431f7ce72046060fcf7a95c37e148f78c77255" |> hex.DecodeData |> PubKey |> NodeId
124124
ShortChannelId = 217304205466536202UL |> ShortChannelId.FromUInt64
125125
FeeBase = 2L |> LNMoney.MilliSatoshis
126126
FeeProportionalMillionths = 30u
127-
CLTVExpiryDelta = 4us |> BlockHeightOffset }
127+
CLTVExpiryDelta = 4u |> BlockHeightOffset }
128128
]
129129
Expect.equal pr.RoutingInfo ([routingInfo]) ""
130130
Expect.equal pr.TagsValue.Fields.Length 4 ""

0 commit comments

Comments
 (0)