Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

We shouldn't use hardcoded values here #19

Open
wants to merge 7 commits into
base: geewalletLightningMilestone4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ jobs:
with:
dotnet-version: ${{ matrix.dotnet }}

- name: Run tests (BouncyCastle)
- name: Run tests (Portability)
# we want to run only once.
if: startsWith(matrix.os, 'ubuntu-18')
run: |
dotnet build tests/DotNetLightning.Core.Tests -p:BouncyCastle=True
dotnet build tests/DotNetLightning.Core.Tests -p:Portability=True
dotnet run --no-build --project tests/DotNetLightning.Core.Tests

- name: Clean to prepare for NSec build
Expand All @@ -40,6 +40,6 @@ jobs:
run: |
DEBIAN_FRONTEND=noninteractive sudo apt install -y msbuild fsharp

dotnet restore -p:BouncyCastle=True DotNetLightning.sln
msbuild src/DotNetLightning.Core/DotNetLightning.Core.fsproj -p:BouncyCastle=True -p:TargetFramework=netstandard2.0
dotnet restore -p:Portability=True DotNetLightning.sln
msbuild src/DotNetLightning.Core/DotNetLightning.Core.fsproj -p:Portability=True -p:TargetFramework=netstandard2.0

11 changes: 3 additions & 8 deletions .github/workflows/publish_master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,8 @@ jobs:
with:
dotnet-version: '3.1.200'

- name: Upload nuget packages (BouncyCastle)
- name: Upload nuget packages (Portability)
if: startsWith(matrix.os, 'ubuntu')
run: |
dotnet pack ./src/DotNetLightning.Core -p:Configuration=Release --version-suffix date`date +%Y%m%d-%H%M`-git-`echo $GITHUB_SHA | head -c 7` -p:BouncyCastle=True
dotnet nuget push ./src/DotNetLightning.Core/bin/Release/DotNetLightning.1*.nupkg -k ${{ secrets.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json

- name: Upload nuget packages (native)
run: |
bash -c "dotnet pack ./src/DotNetLightning.Core -p:Configuration=Release --version-suffix date$(date +%Y%m%d-%H%M).git-$(git rev-parse --short=7 HEAD)-${{ matrix.RID }}"
bash -c "dotnet nuget push ./src/DotNetLightning.Core/bin/Release/DotNetLightning.Core.1*.nupkg -k ${{ secrets.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json"
dotnet pack ./src/DotNetLightning.Core -p:Configuration=Release --version-suffix date`date +%Y%m%d-%H%M`-git-`echo $GITHUB_SHA | head -c 7` -p:Portability=True
dotnet nuget push ./src/DotNetLightning.Core/bin/Release/DotNetLightning.Kiss.1*.nupkg -k ${{ secrets.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json
10 changes: 5 additions & 5 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@

<!-->Since NBitcoin.Secp256k1 does not support netstandard 2.0, we will fallback to BouncyCastle build<-->
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<BouncyCastle>True</BouncyCastle>
<Portability>True</Portability>
</PropertyGroup>


<Choose>
<When Condition="'$(BouncyCastle)'=='true'">
<When Condition="'$(Portability)'=='true'">
<PropertyGroup>
<OtherFlags>$(OtherFlags) -d:BouncyCastle</OtherFlags>
<DefineConstants>$(DefineConstants);BouncyCastle</DefineConstants>
<OtherFlags>$(OtherFlags) -d:NoDUsAsStructs -d:BouncyCastle</OtherFlags>
<DefineConstants>$(DefineConstants);NoDUsAsStructs;BouncyCastle</DefineConstants>
</PropertyGroup>
</When>
</Choose>

<ItemGroup>
<PackageReference Include="NBitcoin" Version="5.0.65" />
<PackageReference Condition="'$(BouncyCastle)'=='true'" Include="Portable.BouncyCastle" Version="1.8.6.7" />
<PackageReference Condition="'$(Portability)'=='true'" Include="Portable.BouncyCastle" Version="1.8.6.7" />
</ItemGroup>

<PropertyGroup>
Expand Down
28 changes: 12 additions & 16 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
MIT License
AGPL License

Copyright (c) 2020 Joe Miyamoto <joemphilips@gmail.com>
Copyright (c) 2020 Node Effect Ltd <andres@nodeffect.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
17 changes: 6 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@ The main entry point is `DotNetLightning.Core`.

## Installation

The package is compiled and published with two variants

* [`DotNetLightning`](https://www.nuget.org/packages/DotNetLightning/)
* This does not use native bindings for cryptographic operations.
* This is the one you want to use if you run your code everywhere, but possibly slower than below.
* [`DotNetLightning.Core`](https://www.nuget.org/packages/DotNetLightning.Core/)
* This uses a pre-compiled `libsodium` for cryptographic operations.
* It only supports `windows`, `mac` and `linux` environments.
* This is what you want if you need performance and the environments above are the only ones you are planning to support.

run `dotnet add package` with the one you want.
The package is compiled and published in nuget:

* [`DotNetLightning.Kiss`](https://www.nuget.org/packages/DotNetLightning.Kiss/)

It does not use native bindings for cryptographic operations.

Currently it is in alpha, so you probably want to install a latest version by specifying it with `--version`.
The version is prefixed with git commit hash and date. Please take a look at the nuget page.

Expand Down
37 changes: 33 additions & 4 deletions src/DotNetLightning.Core/Channel/Channel.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
namespace DotNetLightning.Channel

open ResultUtils

open DotNetLightning.Utils
open DotNetLightning.Utils.NBitcoinExtensions
open DotNetLightning.Utils.Aether
Expand All @@ -12,6 +10,8 @@ open DotNetLightning.Serialization.Msgs
open NBitcoin
open System

open ResultUtils
open ResultUtils.Portability

type ProvideFundingTx = IDestination * Money * FeeRatePerKw -> Result<FinalizedTx * TxOutIndex, string>
type Channel = {
Expand Down Expand Up @@ -443,6 +443,32 @@ module Channel =
[] |> Ok

// ---------- normal operation ---------
| ChannelState.Normal state, MonoHopUnidirectionalPayment op when state.LocalShutdown.IsSome || state.RemoteShutdown.IsSome ->
sprintf "Could not send mono-hop unidirectional payment %A since shutdown is already in progress." op
|> apiMisuse
| ChannelState.Normal state, MonoHopUnidirectionalPayment op ->
result {
let payment: MonoHopUnidirectionalPaymentMsg = {
ChannelId = state.Commitments.ChannelId
Amount = op.Amount
}
let commitments1 = state.Commitments.AddLocalProposal(payment)

let remoteCommit1 =
match commitments1.RemoteNextCommitInfo with
| RemoteNextCommitInfo.Waiting info -> info.NextRemoteCommit
| RemoteNextCommitInfo.Revoked _info -> commitments1.RemoteCommit
let! reduced = remoteCommit1.Spec.Reduce(commitments1.RemoteChanges.ACKed, commitments1.LocalChanges.Proposed) |> expectTransactionError
do! Validation.checkOurMonoHopUnidirectionalPaymentIsAcceptableWithCurrentSpec reduced commitments1 payment
return [ WeAcceptedOperationMonoHopUnidirectionalPayment(payment, commitments1) ]
}
| ChannelState.Normal state, ApplyMonoHopUnidirectionalPayment msg ->
result {
let commitments1 = state.Commitments.AddRemoteProposal(msg)
let! reduced = commitments1.LocalCommit.Spec.Reduce (commitments1.LocalChanges.ACKed, commitments1.RemoteChanges.Proposed) |> expectTransactionError
do! Validation.checkTheirMonoHopUnidirectionalPaymentIsAcceptableWithCurrentSpec reduced commitments1 msg
return [ WeAcceptedMonoHopUnidirectionalPayment commitments1 ]
}
| ChannelState.Normal state, AddHTLC op when state.LocalShutdown.IsSome || state.RemoteShutdown.IsSome ->
sprintf "Could not add new HTLC %A since shutdown is already in progress." op
|> apiMisuse
Expand Down Expand Up @@ -548,8 +574,7 @@ module Channel =
RemoteCommit = theirNextCommit
RemoteNextCommitInfo = RemoteNextCommitInfo.Revoked msg.NextPerCommitmentPoint
RemotePerCommitmentSecrets = remotePerCommitmentSecrets }
let _result = Ok [ WeAcceptedRevokeAndACK commitments1 ]
failwith "needs update"
Ok [ WeAcceptedRevokeAndACK(commitments1) ]

| ChannelState.Normal state, ChannelCommand.Close cmd ->
let localSPK = cmd.ScriptPubKey |> Option.defaultValue (state.Commitments.LocalParams.DefaultFinalScriptPubKey)
Expand Down Expand Up @@ -830,8 +855,12 @@ module Channel =
{ c with State = ChannelState.Normal data }

// ----- normal operation --------
| WeAcceptedOperationMonoHopUnidirectionalPayment(_, newCommitments), ChannelState.Normal normalData ->
{ c with State = ChannelState.Normal({ normalData with Commitments = newCommitments }) }
| WeAcceptedOperationAddHTLC(_, newCommitments), ChannelState.Normal d ->
{ c with State = ChannelState.Normal({ d with Commitments = newCommitments }) }
| WeAcceptedMonoHopUnidirectionalPayment(newCommitments), ChannelState.Normal normalData ->
{ c with State = ChannelState.Normal({ normalData with Commitments = newCommitments }) }
| WeAcceptedUpdateAddHTLC(newCommitments), ChannelState.Normal d ->
{ c with State = ChannelState.Normal({ d with Commitments = newCommitments }) }

Expand Down
52 changes: 50 additions & 2 deletions src/DotNetLightning.Core/Channel/ChannelError.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace DotNetLightning.Channel

open ResultUtils
open DotNetLightning.Utils
open NBitcoinExtensions
open DotNetLightning.Utils.OnionError
Expand All @@ -11,6 +10,9 @@ open DotNetLightning.Transactions

open NBitcoin

open ResultUtils
open ResultUtils.Portability

type ChannelError =
| CryptoError of CryptoError
| TransactionRelatedErrors of TransactionError list
Expand All @@ -36,6 +38,7 @@ type ChannelError =
// --- case they sent unacceptable msg ---
| InvalidOpenChannel of InvalidOpenChannelError
| InvalidAcceptChannel of InvalidAcceptChannelError
| InvalidMonoHopUnidirectionalPayment of InvalidMonoHopUnidirectionalPaymentError
| InvalidUpdateAddHTLC of InvalidUpdateAddHTLCError
| InvalidRevokeAndACK of InvalidRevokeAndACKError
| InvalidUpdateFee of InvalidUpdateFeeError
Expand Down Expand Up @@ -69,6 +72,7 @@ type ChannelError =
| TheyCannotAffordFee (_, _, _) -> Close
| InvalidOpenChannel _ -> DistrustPeer
| InvalidAcceptChannel _ -> DistrustPeer
| InvalidMonoHopUnidirectionalPayment _ -> Close
| InvalidUpdateAddHTLC _ -> Close
| InvalidRevokeAndACK _ -> Close
| InvalidUpdateFee _ -> Close
Expand Down Expand Up @@ -179,6 +183,18 @@ and InvalidAcceptChannelError = {
member this.Message =
String.concat "; " this.Errors

and InvalidMonoHopUnidirectionalPaymentError = {
NetworkMsg: MonoHopUnidirectionalPaymentMsg
Errors: string list
}
with
static member Create msg e = {
NetworkMsg = msg
Errors = e
}
member this.Message =
String.concat "; " this.Errors

and InvalidUpdateAddHTLCError = {
NetworkMsg: UpdateAddHTLCMsg
Errors: string list
Expand Down Expand Up @@ -507,6 +523,22 @@ module internal AcceptChannelMsgValidation =

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

module UpdateMonoHopUnidirectionalPaymentWithContext =
let internal checkWeHaveSufficientFunds (state: Commitments) (currentSpec) =
let fees =
if state.LocalParams.IsFunder then
Transactions.commitTxFee state.RemoteParams.DustLimitSatoshis currentSpec
else
Money.Zero
let missing = currentSpec.ToRemote.ToMoney() - state.RemoteParams.ChannelReserveSatoshis - fees
if missing < Money.Zero then
sprintf "We don't have sufficient funds to send mono-hop unidirectional payment. current to_remote amount is: %A. Remote Channel Reserve is: %A. and fee is %A"
(currentSpec.ToRemote.ToMoney())
state.RemoteParams.ChannelReserveSatoshis
fees
|> Error
else
Ok()

module UpdateAddHTLCValidation =
let internal checkExpiryIsNotPast (current: BlockHeight) (expiry) =
Expand All @@ -521,7 +553,23 @@ module UpdateAddHTLCValidation =
let internal checkAmountIsLargerThanMinimum (htlcMinimum: LNMoney) (amount) =
check (amount) (<) (htlcMinimum) "htlc value (%A) is too small. must be greater or equal to %A"


module internal MonoHopUnidirectionalPaymentValidationWithContext =
let checkWeHaveSufficientFunds (state: Commitments) (currentSpec) =
let fees =
if state.LocalParams.IsFunder then
Transactions.commitTxFee state.RemoteParams.DustLimitSatoshis currentSpec
else
Money.Zero
let missing = currentSpec.ToRemote.ToMoney() - state.RemoteParams.ChannelReserveSatoshis - fees
if missing < Money.Zero then
sprintf "We don't have sufficient funds to send mono-hop unidirectional payment. current to_remote amount is: %A. Remote Channel Reserve is: %A. and fee is %A"
(currentSpec.ToRemote.ToMoney())
state.RemoteParams.ChannelReserveSatoshis
fees
|> Error
else
Ok()

module internal UpdateAddHTLCValidationWithContext =
let checkLessThanHTLCValueInFlightLimit (currentSpec: CommitmentSpec) (limit) (add: UpdateAddHTLCMsg) =
let htlcValueInFlight = currentSpec.HTLCs |> Map.toSeq |> Seq.sumBy (fun (_, v) -> v.Add.Amount)
Expand Down
10 changes: 9 additions & 1 deletion src/DotNetLightning.Core/Channel/ChannelOperations.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace DotNetLightning.Channel

open ResultUtils
open DotNetLightning.Utils
open DotNetLightning.Utils.NBitcoinExtensions
open DotNetLightning.Utils.OnionError
Expand All @@ -13,6 +12,13 @@ open DotNetLightning.Serialization

open NBitcoin

open ResultUtils
open ResultUtils.Portability

type OperationMonoHopUnidirectionalPayment = {
Amount: LNMoney
}

type OperationAddHTLC = {
Amount: LNMoney
PaymentHash: PaymentHash
Expand Down Expand Up @@ -200,6 +206,8 @@ type ChannelCommand =
| CreateChannelReestablish

// normal
| MonoHopUnidirectionalPayment of OperationMonoHopUnidirectionalPayment
| ApplyMonoHopUnidirectionalPayment of msg: MonoHopUnidirectionalPaymentMsg
| AddHTLC of OperationAddHTLC
| ApplyUpdateAddHTLC of msg: UpdateAddHTLCMsg * currentHeight: BlockHeight
| FulfillHTLC of OperationFulfillHTLC
Expand Down
44 changes: 44 additions & 0 deletions src/DotNetLightning.Core/Channel/ChannelTypes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ type ChannelEvent =
| BothFundingLocked of nextState: Data.NormalData

// -------- normal operation ------
| WeAcceptedOperationMonoHopUnidirectionalPayment of msg: MonoHopUnidirectionalPaymentMsg * newCommitments: Commitments
| WeAcceptedMonoHopUnidirectionalPayment of newCommitments: Commitments

| WeAcceptedOperationAddHTLC of msg: UpdateAddHTLCMsg * newCommitments: Commitments
| WeAcceptedUpdateAddHTLC of newCommitments: Commitments

Expand Down Expand Up @@ -360,6 +363,26 @@ type ChannelState =
(fun v cc -> match cc with
| Normal _ -> Normal v
| _ -> cc )
member this.ChannelId: Option<ChannelId> =
match this with
| WaitForInitInternal
| WaitForOpenChannel _
| WaitForAcceptChannel _
| WaitForFundingCreated _ -> None
| WaitForFundingSigned data -> Some data.ChannelId
| WaitForFundingConfirmed data -> Some data.ChannelId
| WaitForFundingLocked data -> Some data.ChannelId
| Normal data -> Some data.ChannelId
| Shutdown data -> Some data.ChannelId
| Negotiating data -> Some data.ChannelId
| Closing data -> Some data.ChannelId
| Closed _
| Offline _
| Syncing _
| ErrFundingLost _
| ErrFundingTimeOut _
| ErrInformationLeak _ -> None

member this.Phase =
match this with
| WaitForInitInternal
Expand All @@ -379,3 +402,24 @@ type ChannelState =
| ErrFundingLost _
| ErrFundingTimeOut _
| ErrInformationLeak _ -> Abnormal

member this.Commitments: Option<Commitments> =
match this with
| WaitForInitInternal
| WaitForOpenChannel _
| WaitForAcceptChannel _
| WaitForFundingCreated _
| WaitForFundingSigned _ -> None
| WaitForFundingConfirmed data -> Some (data :> IHasCommitments).Commitments
| WaitForFundingLocked data -> Some (data :> IHasCommitments).Commitments
| Normal data -> Some (data :> IHasCommitments).Commitments
| Shutdown data -> Some (data :> IHasCommitments).Commitments
| Negotiating data -> Some (data :> IHasCommitments).Commitments
| Closing data -> Some (data :> IHasCommitments).Commitments
| Closed _
| Offline _
| Syncing _
| ErrFundingLost _
| ErrFundingTimeOut _
| ErrInformationLeak _ -> None

Loading