public async Task Provide() { Cid cid = "zBunRGrmCGokA1oMESGGTfrtcMFsVA8aEtcNzM54akPWXF97uXCqTjF3GZ9v8YzxHrG66J8QhtPFWwZebRZ2zeUEELu67"; var swarm = new SwarmService { LocalPeer = self }; var dht = new DhtService { SwarmService = swarm }; await dht.StartAsync(); try { await swarm.StartAsync(); await swarm.StartListeningAsync("/ip4/127.0.0.1/tcp/0"); await dht.ProvideAsync(cid); var peers = (await dht.FindProvidersAsync(cid, 1)).ToArray(); Assert.AreEqual(1, peers.Length); Assert.AreEqual(self, peers[0]); } finally { await dht.StopAsync(); await swarm.StopAsync(); } }
public async Task ProcessGetProvidersMessage_HasCloserPeers() { var swarm = new SwarmService { LocalPeer = self }; var dht = new DhtService { SwarmService = swarm }; await dht.StartAsync(); try { dht.RoutingTable.Add(other); Cid cid = "zBunRGrmCGokA1oMESGGTfrtcMFsVA8aEtcNzM54akPWXF97uXCqTjF3GZ9v8YzxHrG66J8QhtPFWwZebRZ2zeUEELu67"; var request = new DhtMessage { Type = MessageType.GetProviders, Key = cid.Hash.ToArray() }; var response = dht.ProcessGetProviders(request, new DhtMessage()); Assert.AreNotEqual(0, response.CloserPeers.Length); } finally { await dht.StopAsync(); } }
public async Task FindPeer_Closest() { var unknownPeer = new MultiHash("QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCxxx"); var swarm = new SwarmService { LocalPeer = self }; await swarm.StartAsync(); var dht = new DhtService { SwarmService = swarm }; await dht.StartAsync(); dht.RoutingTable.Add(other); try { var peer = await dht.FindPeerAsync(unknownPeer); Assert.AreEqual(other, peer); } finally { await swarm.StopAsync(); await dht.StopAsync(); } }
public async Task ProcessGetProvidersMessage_HasProvider() { var swarm = new SwarmService { LocalPeer = self }; var dht = new DhtService { SwarmService = swarm }; await dht.StartAsync(); try { swarm.RegisterPeer(other); Cid cid = "zBunRGrmCGokA1oMESGGTfrtcMFsVA8aEtcNzM54akPWXF97uXCqTjF3GZ9v8YzxHrG66J8QhtPFWwZebRZ2zeUEELu67"; dht.ContentRouter.Add(cid, other.Id); var request = new DhtMessage { Type = MessageType.GetProviders, Key = cid.Hash.ToArray() }; var response = dht.ProcessGetProviders(request, new DhtMessage()); Assert.AreEqual(1, response.ProviderPeers.Length); response.ProviderPeers[0].TryToPeer(out var found); Assert.AreEqual(other, found); Assert.AreNotEqual(0, found.Addresses.Count()); } finally { await dht.StopAsync(); } }
public async Task ProcessFindNodeMessage_NoOtherPeers() { var swarm = new SwarmService { LocalPeer = self }; var dht = new DhtService { SwarmService = swarm }; await dht.StartAsync(); try { var request = new DhtMessage { Type = MessageType.FindNode, Key = new MultiHash("QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1h").ToArray() }; var response = dht.ProcessFindNode(request, new DhtMessage()); Assert.AreEqual(0, response.CloserPeers.Length); } finally { await dht.StopAsync(); } }
public async Task ProcessFindNodeMessage_BadNodeId() { var swarm = new SwarmService { LocalPeer = self }; swarm.RegisterPeerAddress("/ip4/127.0.0.1/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1a"); swarm.RegisterPeerAddress("/ip4/127.0.0.2/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1b"); swarm.RegisterPeerAddress("/ip4/127.0.0.3/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1c"); swarm.RegisterPeerAddress("/ip4/127.0.0.4/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1d"); swarm.RegisterPeerAddress("/ip4/127.0.0.5/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1e"); var dht = new DhtService { SwarmService = swarm, CloserPeerCount = 3 }; await dht.StartAsync(); try { dht.RoutingTable.Add(other); var request = new DhtMessage { Type = MessageType.FindNode, Key = new byte[] { 0xFF, 1, 2, 3 } }; var response = dht.ProcessFindNode(request, new DhtMessage()); Assert.AreEqual(3, response.CloserPeers.Length); } finally { await dht.StopAsync(); } }
public async Task ProcessFindNodeMessage_InSwarm() { var swarmA = new SwarmService { LocalPeer = self }; var swarmB = swarmA.RegisterPeerAddress( "/ip4/127.0.0.1/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1h"); var dht = new DhtService { SwarmService = swarmA }; await dht.StartAsync(); try { dht.RoutingTable.Add(swarmB); var request = new DhtMessage { Type = MessageType.FindNode, Key = swarmB.Id.ToArray() }; var response = dht.ProcessFindNode(request, new DhtMessage()); Assert.AreEqual(1, response.CloserPeers.Length); var ok = response.CloserPeers[0].TryToPeer(out var found); Assert.IsTrue(ok); Assert.AreEqual(swarmB, found); CollectionAssert.AreEqual( swarmB.Addresses.Select(a => a.WithoutPeerId()).ToArray(), found.Addresses.Select(a => a.WithoutPeerId()).ToArray()); } finally { await dht.StopAsync(); } }
public async Task ProcessFindNodeMessage_InRoutingTable() { var swarm = new SwarmService { LocalPeer = self }; var dht = new DhtService { SwarmService = swarm }; await dht.StartAsync(); try { dht.RoutingTable.Add(other); var request = new DhtMessage { Type = MessageType.FindNode, Key = other.Id.ToArray() }; var response = dht.ProcessFindNode(request, new DhtMessage()); Assert.AreEqual(1, response.CloserPeers.Length); var ok = response.CloserPeers[0].TryToPeer(out var found); Assert.IsTrue(ok); Assert.AreEqual(other, found); CollectionAssert.AreEqual(other.Addresses.ToArray(), found.Addresses.Select(a => a.WithoutPeerId()).ToArray()); } finally { await dht.StopAsync(); } }
public async Task ProcessFindNodeMessage_Self() { var swarm = new SwarmService { LocalPeer = self }; var dht = new DhtService { SwarmService = swarm }; await dht.StartAsync(); try { var request = new DhtMessage { Type = MessageType.FindNode, Key = self.Id.ToArray() }; var response = dht.ProcessFindNode(request, new DhtMessage()); Assert.AreEqual(1, response.CloserPeers.Length); var ok = response.CloserPeers[0].TryToPeer(out var found); Assert.IsTrue(ok); Assert.AreEqual(self, found); } finally { await dht.StopAsync(); } }
public async Task StoppedEventRaised() { var swarm = new SwarmService { LocalPeer = self }; var dht = new DhtService { SwarmService = swarm }; var stopped = false; dht.Stopped += (s, e) => { stopped = true; }; await dht.StartAsync(); await dht.StopAsync(); Assert.IsTrue(stopped); }
public async Task ProcessAddProviderMessage() { var swarm = new SwarmService { LocalPeer = self }; var dht = new DhtService { SwarmService = swarm }; await dht.StartAsync(); try { Cid cid = "zBunRGrmCGokA1oMESGGTfrtcMFsVA8aEtcNzM54akPWXF97uXCqTjF3GZ9v8YzxHrG66J8QhtPFWwZebRZ2zeUEELu67"; var request = new DhtMessage { Type = MessageType.AddProvider, Key = cid.Hash.ToArray(), ProviderPeers = new[] { new DhtPeerMessage { Id = other.Id.ToArray(), Addresses = other.Addresses.Select(a => a.ToArray()).ToArray() } } }; var response = dht.ProcessAddProvider(other, request, new DhtMessage()); Assert.IsNull(response); var providers = dht.ContentRouter.Get(cid).ToArray(); Assert.AreEqual(1, providers.Length); Assert.AreEqual(other.Id, providers[0]); var provider = swarm.KnownPeers.Single(p => p == other); Assert.AreNotEqual(0, provider.Addresses.Count()); } finally { await dht.StopAsync(); } }
public async Task AddDiscoveredPeerToRoutingTable() { var swarm = new SwarmService { LocalPeer = self }; var dht = new DhtService { SwarmService = swarm }; await dht.StartAsync(); try { var peer = swarm.RegisterPeerAddress( "/ip4/127.0.0.1/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1h"); Assert.IsTrue(dht.RoutingTable.Contains(peer)); } finally { await dht.StopAsync(); } }
public async Task FindPeer_NoPeers() { var unknownPeer = new MultiHash("QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCxxx"); var swarm = new SwarmService { LocalPeer = self }; var dht = new DhtService { SwarmService = swarm }; await dht.StartAsync(); try { var peer = await dht.FindPeerAsync(unknownPeer); Assert.IsNull(peer); } finally { await dht.StopAsync(); } }
public async Task QueryIsCancelled_WhenDhtStops() { var unknownPeer = new MultiHash("QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCxxx"); var swarm = new SwarmService { LocalPeer = self }; swarm.RegisterPeerAddress( "/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd"); swarm.RegisterPeerAddress( "/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64"); var dht = new DhtService { SwarmService = swarm }; await dht.StartAsync(); await dht.FindPeerAsync(unknownPeer); await Task.Delay(400).ConfigureAwait(false); await dht.StopAsync(); }
public async Task Add_FindProviders() { Cid cid = "zBunRGrmCGokA1oMESGGTfrtcMFsVA8aEtcNzM54akPWXF97uXCqTjF3GZ9v8YzxHrG66J8QhtPFWwZebRZ2zeUEELu67"; var swarm = new SwarmService { LocalPeer = self }; var dht = new DhtService { SwarmService = swarm }; await dht.StartAsync(); try { dht.ContentRouter.Add(cid, other.Id); var peers = (await dht.FindProvidersAsync(cid, 1)).ToArray(); Assert.AreEqual(1, peers.Length); Assert.AreEqual(other, peers[0]); } finally { await dht.StopAsync(); } }
/// <summary> /// Starts the network services. /// </summary> /// <returns> /// A task that represents the asynchronous operation. /// </returns> /// <remarks> /// Starts the various IPFS and Lib.P2P network services. This should /// be called after any configuration changes. /// </remarks> /// <exception cref="Exception"> /// When the engine is already started. /// </exception> public async Task StartAsync() { _dfsState.IsStarted = true; if (_stopTasks.Count > 0) { throw new Exception("IPFS engine is already started."); } // Repository must be at the correct version. await MigrationManager.MirgrateToVersionAsync(MigrationManager.LatestVersion).ConfigureAwait(false); Log.Debug("starting " + LocalPeer.Id); // Everybody needs the swarm. _stopTasks.Add(async() => { await SwarmService.StopAsync().ConfigureAwait(false); }); await SwarmService.StartAsync().ConfigureAwait(false); var peerManager = new PeerManager { SwarmService = SwarmService }; await peerManager.StartAsync().ConfigureAwait(false); _stopTasks.Add(async() => { await peerManager.StopAsync().ConfigureAwait(false); }); // Start the primary services. var tasks = new List <Func <Task> > { async() => { _stopTasks.Add(async() => await BitSwapService.StopAsync().ConfigureAwait(false)); await BitSwapService.StartAsync().ConfigureAwait(false); }, async() => { _stopTasks.Add(async() => await DhtService.StopAsync().ConfigureAwait(false)); await DhtService.StartAsync().ConfigureAwait(false); }, async() => { _stopTasks.Add(async() => await PingService.StopAsync().ConfigureAwait(false)); await PingService.StartAsync().ConfigureAwait(false); }, async() => { _stopTasks.Add(async() => await PubSubService.StopAsync().ConfigureAwait(false)); await PubSubService.StartAsync().ConfigureAwait(false); } }; Log.Debug("waiting for services to start"); await Task.WhenAll(tasks.Select(t => t())).ConfigureAwait(false); // Starting listening to the swarm. var json = await ConfigApi.GetAsync("Addresses.Swarm").ConfigureAwait(false); var numberListeners = 0; foreach (string a in json) { try { await SwarmService.StartListeningAsync(a).ConfigureAwait(false); ++numberListeners; } catch (Exception e) { Log.Warning($"Listener failure for '{a}'", e); } } if (numberListeners == 0) { Log.Error("No listeners were created."); } // Now that the listener addresses are established, the discovery // services can begin. MulticastService multicast = null; if (!Options.Discovery.DisableMdns) { multicast = new MulticastService(); #pragma warning disable CS1998 _stopTasks.Add(async() => multicast.Dispose()); #pragma warning restore CS1998 } var autodialer = new AutoDialer(SwarmService) { MinConnections = Options.Swarm.MinConnections }; #pragma warning disable CS1998 _stopTasks.Add(async() => autodialer.Dispose()); #pragma warning restore CS1998 tasks = new List <Func <Task> > { // Bootstrap discovery async() => { var bootstrap = new Bootstrap { Addresses = await BootstrapApi.ListAsync() }; bootstrap.PeerDiscovered += OnPeerDiscovered; _stopTasks.Add(async() => await bootstrap.StopAsync().ConfigureAwait(false)); await bootstrap.StartAsync().ConfigureAwait(false); }, async() => { if (Options.Discovery.DisableRandomWalk) { return; } var randomWalk = new RandomWalk { Dht = DhtApi }; _stopTasks.Add(async() => await randomWalk.StopAsync().ConfigureAwait(false)); await randomWalk.StartAsync().ConfigureAwait(false); } }; Log.Debug("waiting for discovery services to start"); await Task.WhenAll(tasks.Select(t => t())).ConfigureAwait(false); multicast?.Start(); Log.Debug("started"); }