Skip to content

Commit 6234c40

Browse files
committed
Redefine single-variant discriminated union types
A lot of types in DotNetLightning are defined as discriminated unions with a single constructor with the same name as the type. eg: type NodeId = | NodeId PubKey This commit redefines these types as: type NodeId(id: PubKey) = ... The main motivation for this is that having types and constructors with the same name breaks the json serialization library on older mono versions. It can also lead to confusing error messages. Besides that, it's also somewhat pointless to define a type as a discriminated union but then only give it one variant.
1 parent cae6b93 commit 6234c40

File tree

15 files changed

+83
-85
lines changed

15 files changed

+83
-85
lines changed

src/DotNetLightning.Core/Channel/ChannelError.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,9 @@ module private ValidationHelper =
253253
/// Helpers to create channel error
254254
[<AutoOpen>]
255255
module internal ChannelError =
256-
let feeRateMismatch (FeeRatePerKw remote, FeeRatePerKw local) =
257-
let remote = float remote
258-
let local = float local
256+
let feeRateMismatch (remote: FeeRatePerKw, local: FeeRatePerKw) =
257+
let remote = float remote.Value
258+
let local = float local.Value
259259
abs (2.0 * (remote - local) / (remote + local))
260260

261261
let inline feeDeltaTooHigh msg (actualDelta, maxAccepted) =

src/DotNetLightning.Core/Channel/ChannelHelpers.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ open NBitcoin
99
/// cousin of `ChannelHelpers` module which only includes very primitive function.
1010
module internal ChannelConstantHelpers =
1111
let deriveOurDustLimitSatoshis (feeEstimator: IFeeEstimator): Money =
12-
let (FeeRatePerKw atOpenBackGroundFee) = feeEstimator.GetEstSatPer1000Weight(ConfirmationTarget.Background)
13-
(Money.Satoshis((uint64 atOpenBackGroundFee) * B_OUTPUT_PLUS_SPENDING_INPUT_WEIGHT / 1000UL), Money.Satoshis(546UL))
12+
let atOpenBackGroundFee = feeEstimator.GetEstSatPer1000Weight(ConfirmationTarget.Background)
13+
(Money.Satoshis((uint64 atOpenBackGroundFee.Value) * B_OUTPUT_PLUS_SPENDING_INPUT_WEIGHT / 1000UL), Money.Satoshis(546UL))
1414
|> Money.Max
1515

1616
let getOurChannelReserve (channelValue: Money) =

src/DotNetLightning.Core/Channel/ChannelValidation.fs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ module internal ChannelHelpers =
2121
[| theirFundingPubKey; ourFundingKey |]
2222
PayToMultiSigTemplate.Instance.GenerateScriptPubKey(2, pks)
2323

24-
let getFundingScriptCoin (ck: ChannelPubKeys) (theirFundingPubKey: PubKey) (TxId fundingTxId) (TxOutIndex fundingOutputIndex) (fundingSatoshis): ScriptCoin =
24+
let getFundingScriptCoin (ck: ChannelPubKeys) (theirFundingPubKey: PubKey) (fundingTxId: TxId) (fundingOutputIndex: TxOutIndex) (fundingSatoshis): ScriptCoin =
2525
let redeem = getFundingRedeemScript ck theirFundingPubKey
26-
Coin(fundingTxId, uint32 fundingOutputIndex, fundingSatoshis, redeem.WitHash.ScriptPubKey)
26+
Coin(fundingTxId.Value, uint32 fundingOutputIndex.Value, fundingSatoshis, redeem.WitHash.ScriptPubKey)
2727
|> fun c -> ScriptCoin(c, redeem)
2828

2929
let private makeFlags (isNode1: bool, enable: bool) =
@@ -52,14 +52,14 @@ module internal ChannelHelpers =
5252
}
5353

5454
/// gets the fee we'd want to charge for adding an HTLC output to this channel
55-
let internal getOurFeeBaseMSat (feeEstimator: IFeeEstimator) (FeeRatePerKw feeRatePerKw) (isFunder: bool) =
55+
let internal getOurFeeBaseMSat (feeEstimator: IFeeEstimator) (feeRatePerKw: FeeRatePerKw) (isFunder: bool) =
5656
// for lack of a better metric, we calculate waht it would cost to consolidate the new HTLC
5757
// output value back into a transaction with the regular channel output:
5858

5959
// the fee cost of the HTLC-success/HTLC-Timout transaction
60-
let mutable res = uint64 feeRatePerKw * (max (ChannelConstants.HTLC_TIMEOUT_TX_WEIGHT) (ChannelConstants.HTLC_TIMEOUT_TX_WEIGHT)) |> fun r -> r / 1000UL
60+
let mutable res = uint64 feeRatePerKw.Value * (max (ChannelConstants.HTLC_TIMEOUT_TX_WEIGHT) (ChannelConstants.HTLC_TIMEOUT_TX_WEIGHT)) |> fun r -> r / 1000UL
6161
if (isFunder) then
62-
res <- res + uint64 feeRatePerKw * COMMITMENT_TX_WEIGHT_PER_HTLC / 1000UL
62+
res <- res + uint64 feeRatePerKw.Value * COMMITMENT_TX_WEIGHT_PER_HTLC / 1000UL
6363

6464
//+ the marginal cost of an input which spends the HTLC-Success/HTLC-Timeout output:
6565
res <-

src/DotNetLightning.Core/Payment/LSAT/MacaroonIdentifier.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type MacaroonIdentifier =
3535
| 0us ->
3636
if (b.Length <> 2 + 32 + 32) then e else
3737
{
38-
PaymentHash = PaymentHash.PaymentHash(uint256(b.[2..33], false))
38+
PaymentHash = PaymentHash(uint256(b.[2..33], false))
3939
TokenId = uint256(b.[34..], false)
4040
}
4141
|> V0

src/DotNetLightning.Core/Payment/PaymentRequest.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,11 @@ type TaggedField =
258258
| DescriptionHashTaggedField h ->
259259
let dBase32 = h.ToBytes(false) |> Helpers.convert8BitsTo5
260260
this.WriteField(writer, dBase32)
261-
| NodeIdTaggedField(NodeId pk) ->
262-
let dBase32 = pk.ToBytes() |> Helpers.convert8BitsTo5
261+
| NodeIdTaggedField(nodeId) ->
262+
let dBase32 = nodeId.Value.ToBytes() |> Helpers.convert8BitsTo5
263263
this.WriteField(writer, dBase32)
264-
| MinFinalCltvExpiryTaggedField (BlockHeightOffset32 c) ->
265-
let dBase32 = c |> uint64 |> Helpers.uint64ToBase32
264+
| MinFinalCltvExpiryTaggedField (blockHeightOffset32) ->
265+
let dBase32 = blockHeightOffset32.Value |> uint64 |> Helpers.uint64ToBase32
266266
this.WriteField(writer, dBase32)
267267
| ExpiryTaggedField x ->
268268
let dBase32 = ((x.ToUnixTimeSeconds() |> uint64) - timestamp) |> Helpers.uint64ToBase32

src/DotNetLightning.Core/Peer/PeerChannelEncryptor.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,11 @@ module PeerChannelEncryptor =
279279

280280

281281
module PeerChannelEncryptor =
282-
let newOutBound (NodeId theirNodeId, ourNodeSecret: Key) =
283-
let hashInput = Array.concat[| NOISE_H; theirNodeId.ToBytes()|]
282+
let newOutBound (theirNodeId: NodeId, ourNodeSecret: Key) =
283+
let hashInput = Array.concat[| NOISE_H; theirNodeId.Value.ToBytes()|]
284284
let h = uint256(Hashes.SHA256(hashInput))
285285
{
286-
TheirNodeId = Some(NodeId theirNodeId)
286+
TheirNodeId = Some(theirNodeId)
287287
NoiseState = InProgress {
288288
State = PreActOne
289289
DirectionalState = OutBound ({ IE = ourNodeSecret })

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1370,7 +1370,7 @@ type ErrorMsg =
13701370
this.Data <- ls.ReadWithLen()
13711371
member this.Serialize(ls) =
13721372
match this.ChannelId with
1373-
| SpecificChannel (ChannelId id) -> ls.Write(id.ToBytes())
1373+
| SpecificChannel id -> ls.Write(id.Value.ToBytes())
13741374
| All -> ls.Write(Array.zeroCreate 32)
13751375
ls.WriteWithLen(this.Data)
13761376

src/DotNetLightning.Core/Transactions/Scripts.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ module Scripts =
1414
let multiSigOfM_2 (sort) (pks) =
1515
PayToMultiSigTemplate.Instance.GenerateScriptPubKey(2, sort, pks)
1616

17-
let toLocalDelayed (revocationPubKey: PubKey) (BlockHeightOffset16 toSelfDelay) (localDelayedPaymentPubkey: PubKey): Script =
17+
let toLocalDelayed (revocationPubKey: PubKey) (toSelfDelay: BlockHeightOffset16) (localDelayedPaymentPubkey: PubKey): Script =
1818
let opList = ResizeArray<Op>()
1919
opList.Add(!> OpcodeType.OP_IF)
2020
opList.Add(Op.GetPushOp(revocationPubKey.ToBytes()))
2121
opList.Add(!> OpcodeType.OP_ELSE)
22-
opList.Add(Op.GetPushOp(int64 toSelfDelay))
22+
opList.Add(Op.GetPushOp(int64 toSelfDelay.Value))
2323
opList.Add(!> OpcodeType.OP_CHECKSEQUENCEVERIFY)
2424
opList.Add(!> OpcodeType.OP_DROP)
2525
opList.Add(Op.GetPushOp(localDelayedPaymentPubkey.ToBytes()))

src/DotNetLightning.Core/Transactions/Transactions.fs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,9 @@ type ClosingTx = ClosingTx of PSBT
176176

177177

178178
/// Tx already verified and it can be published anytime
179-
type FinalizedTx =
180-
FinalizedTx of Transaction
181-
with
182-
member this.Value = let (FinalizedTx v) = this in v
179+
[<Struct>]
180+
type FinalizedTx(tx: Transaction) =
181+
member this.Value = tx
183182

184183
type InputInfo = {
185184
OutPoint: OutPoint

src/DotNetLightning.Core/Utils/ChannelId.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ namespace DotNetLightning.Utils
22

33
open NBitcoin
44

5-
type ChannelId = | ChannelId of uint256 with
6-
member x.Value = let (ChannelId v) = x in v
7-
5+
[<Struct>]
6+
type ChannelId(id: uint256) =
7+
member x.Value = id
88
static member Zero = uint256.Zero |> ChannelId
99

0 commit comments

Comments
 (0)