Skip to content

Commit

Permalink
Merge pull request block-core#29 from block-core/ConnectToWallet
Browse files Browse the repository at this point in the history
Fixed code after debugging connection with other lightning nodes
  • Loading branch information
DavidGershony authored Feb 24, 2022
2 parents 3a2fb2c + 96dd0f5 commit 584add9
Show file tree
Hide file tree
Showing 51 changed files with 841 additions and 203 deletions.
2 changes: 1 addition & 1 deletion docker/test-network/c-lightning/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ RUN chmod +x /usr/local/bin/logtail.sh
ADD wait-for-bitcoind.sh /usr/local/bin
RUN chmod +x /usr/local/bin/wait-for-bitcoind.sh

RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - \
RUN curl -sL https://deb.nodesource.com/setup_14.x | bash - \
&& apt-get install -y nodejs \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
Expand Down
4 changes: 1 addition & 3 deletions docker/test-network/c-lightning/lightningd/config
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@ bitcoin-rpcport=18443
log-level=debug
log-file=/lightningd/lightningd.log

plugin=/rest/plugin.js
rest-port=3002
rest-docport=4001
plugin=/rest/plugin.js
3 changes: 2 additions & 1 deletion docker/test-network/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ services:
- "18443"
- "12005"
- "12006"
ports:
- "0.0.0.0:18443:18443"
restart: always

lnd-node:
Expand All @@ -34,7 +36,6 @@ services:

c-lightning-node:
container_name: c-lightning-node
hostname: c-lightning-node
hostname: lightning
build:
context: c-lightning
Expand Down
1 change: 1 addition & 0 deletions docker/test-network/eclair/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ADD wait-for-bitcoind.sh /usr/local/bin
RUN chmod +x /usr/local/bin/wait-for-bitcoind.sh

EXPOSE 9735
EXPOSE 8080
ENTRYPOINT ["/usr/local/bin/eclair-entrypoint.sh"]

# Show logs from beginning and keep following
Expand Down
1 change: 0 additions & 1 deletion docker/test-network/lnd/lnd/lnd.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ listen=0.0.0.0
nobootstrap=1
noseedbackup=1
debuglevel=info
port=9732

[bitcoin]
bitcoin.active=1
Expand Down
8 changes: 4 additions & 4 deletions src/Lyn.Protocol.Tests/Bolt1/InitMessageServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void ProcessMessageAddsPeerAndSendsResponse()
_sut.ProcessMessageAsync(message);

_repository.Verify(_ => _.AddOrUpdatePeerAsync(It.Is<Peer>(p
=> (ulong)p.Featurs == (ulong)parsedFeatures &&
=> (ulong)p.Features == (ulong)parsedFeatures &&
p.NodeId.Equals(message.NodeId))));
}

Expand Down Expand Up @@ -158,17 +158,17 @@ public void ProcessMessageUpdatesPeerAndSendsResponse()
var peer = new Peer
{
NodeId = message.NodeId,
Featurs = (Features) RandomMessages.GetRandomNumberUInt16()
Features = (Features) RandomMessages.GetRandomNumberUInt16()
};

_repository.Setup(_ => _.TryGetPeerAsync(message.NodeId))
.Returns(peer);
.Returns(Task.FromResult<Peer?>(peer));

_sut.ProcessMessageAsync(message);

_repository.Verify(_ => _.AddOrUpdatePeerAsync(peer));

Assert.Equal(peer.Featurs,parsedFeatures);
Assert.Equal(peer.Features,parsedFeatures);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
using Lyn.Protocol.Bolt1.Entities;
using Lyn.Protocol.Bolt2.ChannelEstablishment;
using Lyn.Protocol.Bolt2.ChannelEstablishment.Messages;
using Lyn.Protocol.Bolt2.Wallet;
using Lyn.Protocol.Bolt3;
using Lyn.Protocol.Bolt9;
using Lyn.Protocol.Common;
using Lyn.Protocol.Common.Blockchain;
using Lyn.Protocol.Common.Hashing;
using Lyn.Protocol.Common.Messages;
using Lyn.Protocol.Connection;
using Lyn.Types;
Expand Down Expand Up @@ -54,7 +56,8 @@ public AcceptChannelMessageServiceTests()
new ChainConfigProvider(),
_store.Object,
inMemoryPeerRepository,
_features.Object);
_features.Object,
new Mock<IWalletTransactions>().Object);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
Expand All @@ -6,10 +7,12 @@
using Lyn.Protocol.Bolt1.Messages;
using Lyn.Protocol.Bolt2.ChannelEstablishment;
using Lyn.Protocol.Bolt2.ChannelEstablishment.Messages;
using Lyn.Protocol.Bolt2.Wallet;
using Lyn.Protocol.Bolt3;
using Lyn.Protocol.Bolt9;
using Lyn.Protocol.Common;
using Lyn.Protocol.Common.Blockchain;
using Lyn.Protocol.Common.Hashing;
using Lyn.Protocol.Common.Messages;
using Lyn.Protocol.Connection;
using Lyn.Types;
Expand Down Expand Up @@ -45,6 +48,8 @@ public class FullChannelEstablishmentTest
private static UInt256 _chainHash =
new(Hex.FromString("06226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f"));

Mock<IWalletTransactions> walletLookup;

public FullChannelEstablishmentTest()
{
var loggerFactory = new LoggerFactory();
Expand All @@ -69,16 +74,21 @@ public FullChannelEstablishmentTest()
new TransactionInputSerializer(new OutPointSerializer()),
new TransactionOutputSerializer(),
new TransactionWitnessSerializer(new TransactionWitnessComponentSerializer())));

walletLookup = new Mock<IWalletTransactions>();

_openChannelService = new StartOpenChannelService(new Logger<OpenChannelMessageService>(loggerFactory),
walletLookup.Setup(_ => _.IsAmountAvailableAsync(It.IsAny<Satoshis>()))
.ReturnsAsync(true);

_openChannelService = new StartOpenChannelService(new Logger<StartOpenChannelService>(loggerFactory),
_randomNumberGenerator.Object,
_keyDerivation,
_candidateRepository,
_peerRepository,
new ChainConfigProvider(),
new LynImplementedBoltFeatures(parsingFeatures),
parsingFeatures,
new SecretStore());
new SecretStore(),
walletLookup.Object);

_acceptChannelMessageService = new AcceptChannelMessageService(
new Logger<AcceptChannelMessageService>(loggerFactory),
Expand All @@ -89,24 +99,25 @@ public FullChannelEstablishmentTest()
_candidateRepository,
new ChainConfigProvider(),
new SecretStore(),
_peerRepository, new LynImplementedBoltFeatures(parsingFeatures));
_peerRepository,
new LynImplementedBoltFeatures(parsingFeatures),
walletLookup.Object);

_fundingSignedMessageService = new FundingSignedMessageService(
new Logger<FundingSignedMessageService>(loggerFactory),
lightningTransactions,
hashCalculator,
_lightningScripts,
_keyDerivation,
_candidateRepository,
new ChainConfigProvider(),
new SecretStore(),
_peerRepository,
new LynImplementedBoltFeatures(parsingFeatures));
new LynImplementedBoltFeatures(parsingFeatures),
walletLookup.Object);


_peerRepository.AddNewPeerAsync(new Peer
{
Featurs = Features.InitialRoutingSync | Features.VarOnionOptin | Features.GossipQueriesEx |
Features = Features.InitialRoutingSync | Features.VarOnionOptin | Features.GossipQueriesEx |
Features.PaymentSecret | (Features)2,
Id = 0,
GlobalFeatures = Features.OptionDataLossProtect,
Expand Down Expand Up @@ -184,9 +195,30 @@ public FullChannelEstablishmentTest()
"0x7dda8bb401b0236edb0e97de360626ba64c8eebffc1399ca4ce17831c196d8b3232c4eea5db33d60811c1f6d131b4f4cd9a330d0f50dcb063daaf648e40a9b30")
};

private void WithTheTransactionReturnedFromTheWallet()
{
var expectedTransaction = new Transaction
{
Outputs = new TransactionOutput[]
{
new()
{
PublicKeyScript = _lightningScripts.FundingWitnessScript(_expectedOpenChannel.FundingPubkey,
_acceptChannel.FundingPubkey),
Value = _expectedOpenChannel.FundingSatoshis
}
}
};

walletLookup.Setup(_ => _.GenerateTransactionForOutputAsync(It.IsAny<TransactionOutput>()))
.Returns(Task.FromResult(expectedTransaction));
}

[Fact]
public async Task FullChannelEstablishmentScenarioCompletesSuccessfully()
{
WithTheTransactionReturnedFromTheWallet();

var openChannelResponse = await _openChannelService.CreateOpenChannelAsync(new CreateOpenChannelIn(_nodeId,
_chainHash, 16000000, 0, 1000, false));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private ChainParameters WithExistingPeerAndChainParameters(PeerMessage<OpenChann
},
};

_peerRepository.Setup(_ => _.TryGetPeerAsync(message.NodeId)).Returns(new Peer());
_peerRepository.Setup(_ => _.TryGetPeerAsync(message.NodeId)).Returns(Task.FromResult<Peer?>(new Peer()));
_chainConfigProvider.Setup(_ => _.GetConfiguration(message.MessagePayload.ChainHash)).Returns(chainParameters);
_lightningTransactions.Setup(_ => _.GetBaseFee(It.IsAny<Satoshis>(), It.IsAny<bool>(), 0)).Returns(100);

Expand Down
19 changes: 14 additions & 5 deletions src/Lyn.Protocol.Tests/Bolt2/StartOpenChannelServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Lyn.Protocol.Bolt2.Wallet;
using Xunit;

namespace Lyn.Protocol.Tests.Bolt2
Expand All @@ -25,13 +26,13 @@ public class StartOpenChannelServiceTests

private readonly Mock<IPeerRepository> _peerRepository = new();
private readonly Mock<IBoltFeatures> _features = new();
private readonly Mock<ILogger<OpenChannelMessageService>> _logger = new();
private readonly Mock<ILogger<StartOpenChannelService>> _logger = new();
private readonly Mock<IRandomNumberGenerator> _randomNumberGenerator = new();
private readonly LightningKeyDerivation _lightningKeyDerivation = new();
private readonly Mock<IChannelCandidateRepository> _channelStateRepository = new();
private readonly Mock<IChainConfigProvider> _chainConfigProvider = new();
private readonly Mock<IParseFeatureFlags> _parseFeatureFlags = new();
private readonly Mock<ISecretStore> _secretProvider = new();
private readonly Mock<IWalletTransactions> _transactionsLookups = new();

private readonly Secret _randomSecret;

Expand All @@ -40,8 +41,8 @@ public StartOpenChannelServiceTests()
_sut = new StartOpenChannelService(_logger.Object,
_randomNumberGenerator.Object, _lightningKeyDerivation,
_channelStateRepository.Object, _peerRepository.Object,
_chainConfigProvider.Object, _features.Object,
_parseFeatureFlags.Object, _secretProvider.Object);
_chainConfigProvider.Object, _features.Object, _secretProvider.Object,
_transactionsLookups.Object);

_randomSecret = new Secret(RandomMessages.GetRandomByteArray(32));

Expand Down Expand Up @@ -70,7 +71,7 @@ private ChainParameters WithExistingPeerAndChainParameters(CreateOpenChannelIn m
},
};

_peerRepository.Setup(_ => _.TryGetPeerAsync(message.NodeId)).Returns(new Peer());
_peerRepository.Setup(_ => _.TryGetPeerAsync(message.NodeId)).Returns(Task.FromResult<Peer?>(new Peer()));
_chainConfigProvider.Setup(_ => _.GetConfiguration(message.ChainHash)).Returns(chainParameters);

return chainParameters;
Expand All @@ -95,9 +96,17 @@ private CreateOpenChannelIn NewStartChannelMessage()
return (fundingPubkey, basepoints);
}

private void WithEnoughFundsInTheWallet()
{
_transactionsLookups.Setup(_ => _.IsAmountAvailableAsync(It.IsAny<Satoshis>()))
.Returns(Task.FromResult<bool>(true));
}

[Fact]
public async Task StartOpenChannelSuccess()
{
WithEnoughFundsInTheWallet();

var message = NewStartChannelMessage();

var config = WithExistingPeerAndChainParameters(message);
Expand Down
4 changes: 3 additions & 1 deletion src/Lyn.Protocol.Tests/Bolt3/Bolt3CommitmentTestContext.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Buffers;
using System.Linq;
using FluentAssertions;
using Lyn.Protocol.Bolt3;
using Lyn.Protocol.Bolt3.Types;
using Lyn.Protocol.Common;
using Lyn.Protocol.Common.Hashing;
using Lyn.Protocol.Common.Messages;
using Lyn.Types;
using Lyn.Types.Bitcoin;
Expand Down Expand Up @@ -135,7 +137,7 @@ public Bolt3CommitmentTestContext()

// dotnet has no uint48 types so we use ulong instead, however ulong (which is uint64) has two
// more bytes in the array then just drop the last to bytes form the array to compute the hex
Assert.Equal("0x2bb038521914", Hex.ToString(BitConverter.GetBytes(CnObscurer).Reverse().ToArray().AsSpan().Slice(2)));
Assert.Equal("0x2bb038521914", $"0x{Hex.ToString(BitConverter.GetBytes(CnObscurer).Reverse().ToArray().AsSpan().Slice(2))}");

Keyset = new Keyset(RemoteRevocationKey, LocalHtlckey, RemoteHtlckey, LocalDelayedkey, Remotekey);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Lyn.Protocol.Tests/Bolt3/Bolt3CommitmentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void Bolt3CommitmentAndHtlcTransactionTest(Bolt3CommitmentTestVectors vec

byte[] localTransactionBytes = Context.SerializationFactory.Serialize(localTransaction);

Assert.Equal(vectors.OutputCommitTx, Hex.ToString(localTransactionBytes.AsSpan()).Substring(2));
Assert.Equal(vectors.OutputCommitTx, Hex.ToString(localTransactionBytes.AsSpan()));

/* FIXME: naming here is kind of backwards: local revocation key
* is derived from remote revocation basepoint, but it's local */
Expand Down Expand Up @@ -243,7 +243,7 @@ public void Bolt3CommitmentAndHtlcTransactionTest(Bolt3CommitmentTestVectors vec

byte[] htlcTransactionBytes = Context.SerializationFactory.Serialize(htlcTransaction);

Assert.Equal(expectedHtlcHex, Hex.ToString(htlcTransactionBytes.AsSpan()).Substring(2));
Assert.Equal(expectedHtlcHex, Hex.ToString(htlcTransactionBytes.AsSpan()));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Lyn.Protocol.Tests/Bolt3/Bolt3FundingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void AppendixBCreateFundingTransactionTest()
}
};

var newtrxToSign = Transaction.Parse(Hex.ToString(serializationFactory.Serialize(newtrx)).Substring(2), NBitcoin.Network.RegTest);
var newtrxToSign = Transaction.Parse(Hex.ToString(serializationFactory.Serialize(newtrx)), NBitcoin.Network.RegTest);

uint256? newtrxToSignHash = newtrxToSign.GetSignatureHash(
block1.Transactions[0].Outputs[0].ScriptPubKey,
Expand All @@ -104,7 +104,7 @@ public void AppendixBCreateFundingTransactionTest()
Op.GetPushOp(block1Privkey.PubKey.ToBytes()))
.ToBytes();

Assert.Equal(trx.ToHex(), Hex.ToString(serializationFactory.Serialize(newtrx)).Substring(2));
Assert.Equal(trx.ToHex(), Hex.ToString(serializationFactory.Serialize(newtrx)));

// Check that the funding transaction scripts are equal.
Assert.Equal(trx.Outputs[0].ScriptPubKey, scriptWit);
Expand Down
Loading

0 comments on commit 584add9

Please sign in to comment.