public async Task CanConnectNodesAndGetChannelDetails() { var configs = Enumerable.Range(0, 2).Select(_ => NodeConfig.CreateDefault()).ToList(); var nodes = await TestNodeNetwork.CreateNetwork(configs); var fundingTx = await nodes.CreateChannel(0, 1); var channels0 = nodes[0].ChannelManager.ListChannels(_pool); Assert.Single(channels0); var channels1 = nodes[1].ChannelManager.ListChannels(_pool); Assert.Single(channels1); Assert.False(channels0[0].IsLive); Assert.False(channels1[0].IsLive); Assert.Equal(channels0[0].RemoteNetworkId, nodes[1].NodeConfig.KeysManager.GetNodeSecret().PubKey); Assert.Equal(channels1[0].RemoteNetworkId, nodes[0].NodeConfig.KeysManager.GetNodeSecret().PubKey); // short channel id is not defined until funding tx gets confirmed. Assert.Equal(channels0[0].ShortChannelId, null); Assert.Equal(channels1[0].ShortChannelId, null); var block = configs[0].N.Consensus.ConsensusFactory.CreateBlock(); block.Transactions = new List <Transaction>(); block.Transactions.Add(fundingTx); nodes[0].BlockNotifier.BlockConnected(block, 1); nodes[1].BlockNotifier.BlockConnected(block, 1); channels0 = nodes[0].ChannelManager.ListChannels(_pool); channels1 = nodes[1].ChannelManager.ListChannels(_pool); var expectedChannelId = Primitives.ShortChannelId.TryParse("1x0x0").ResultValue; Assert.Equal(expectedChannelId, channels0[0].ShortChannelId); Assert.Equal(expectedChannelId, channels1[0].ShortChannelId); Assert.True(channels0[0].InboundCapacityMSat > 0); Assert.True(channels0[0].OutboundCapacityMSat > 0); Assert.True(channels1[0].InboundCapacityMSat > 0); Assert.True(channels1[0].OutboundCapacityMSat > 0); // Channel monitor deserialization tests. var monitor = nodes[0].NodeConfig.ManyChannelMonitor; var monitorB = monitor.Serialize(_pool); var(_, latestBlockHashes) = ManyChannelMonitor.Deserialize(nodes[0].NodeConfig.ManyChannelMonitorReadArgs, monitorB, _pool); Assert.Single(latestBlockHashes); Assert.Equal(latestBlockHashes.Keys.First().Item, fundingTx.Outputs.AsCoins().First().Outpoint); Assert.Equal(latestBlockHashes.Values.First(), block.GetHash()); // Channel manager deserialization tests. var managerB = nodes[0].ChannelManager.Serialize(_pool); var(latestBlockHash, manager2) = ChannelManager.Deserialize(managerB, nodes[0].NodeConfig.GetChannelManagerReadArgs(monitor), nodes[0].NodeConfig.ConfigProvider, _pool); Assert.Equal(block.GetHash(), latestBlockHash); Assert.Single(manager2.ListChannels(_pool)); Assert.Equal(manager2.ListChannels(_pool)[0], channels0[0]); }
public async Task <(ManyChannelMonitor, Dictionary <Primitives.LNOutPoint, uint256>)?> GetManyChannelMonitor(ManyChannelMonitorReadArgs readArgs, CancellationToken ct = default) { if (readArgs == null) { throw new ArgumentNullException(nameof(readArgs)); } using var tx = await _engine.OpenTransaction(ct); using var manyChannelMonitorRow = await tx.GetTable(DBKeys.ManyChannelMonitor).Get(DBKeys.ManyChannelMonitorVersion); if (manyChannelMonitorRow is null) { return(null); } var val = await manyChannelMonitorRow.ReadValue(); if (val.IsEmpty) { return(null); } return(ManyChannelMonitor.Deserialize(readArgs, val, _pool)); }
public void RegistrationTests() { var keySeed = new byte[] { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }; var keysInterface = new KeysManager(keySeed, DateTime.UnixEpoch); var logger = new TestLogger(); var broadcaster = new TestBroadcaster(); var feeEstiamtor = new TestFeeEstimator(); var n = NBitcoin.Network.TestNet; var chainWatchInterface = new ChainWatchInterfaceUtil(n); using var blockNotifier = BlockNotifier.Create(chainWatchInterface); using var manyChannelMonitor = ManyChannelMonitor.Create(n, chainWatchInterface, broadcaster, logger, feeEstiamtor); blockNotifier.RegisterManyChannelMonitor(manyChannelMonitor); using var channelManager = ChannelManager.Create(n, UserConfig.GetDefault(), chainWatchInterface, keysInterface, logger, broadcaster, feeEstiamtor, 0, manyChannelMonitor); blockNotifier.RegisterChannelManager(channelManager); // second block in testnet3 var block = (Block.Parse("0100000006128e87be8b1b4dea47a7247d5528d2702c96826c7a648497e773b800000000e241352e3bec0a95a6217e10c3abb54adfa05abb12c126695595580fb92e222032e7494dffff001d00d235340101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0e0432e7494d010e062f503253482fffffffff0100f2052a010000002321038a7f6ef1c8ca0c588aa53fa860128077c9e6c11e6830f4d7ee4e763a56b7718fac00000000", n)); blockNotifier.BlockConnected(block, 1); var b = manyChannelMonitor.Serialize(_pool); var(manyChannelMonitor2, keyToHeaderHash) = ManyChannelMonitor.Deserialize(new ManyChannelMonitorReadArgs(chainWatchInterface, broadcaster, logger, feeEstiamtor, n), b.AsMemory(), _pool); using (manyChannelMonitor2) { Assert.True(NBitcoin.Utils.ArrayEqual(b, manyChannelMonitor2.Serialize(_pool))); // without creating any channel, it will result to empty. Assert.Empty(keyToHeaderHash); } blockNotifier.UnregisterManyChannelMonitor(manyChannelMonitor); blockNotifier.UnregisterChannelManager(channelManager); }