Ejemplo n.º 1
0
        public void Block_Needed()
        {
            var bitSwap = new BitSwapService {
                SwarmService = new SwarmService {
                    LocalPeer = _self
                }
            };
            var cid1      = new DagNode(Encoding.UTF8.GetBytes("BitswapTest unknown block y")).Id;
            var cid2      = new DagNode(Encoding.UTF8.GetBytes("BitswapTest unknown block z")).Id;
            var cancel    = new CancellationTokenSource();
            var callCount = 0;

            bitSwap.BlockNeeded += (s, e) =>
            {
                Assert.True(cid1 == e.Id || cid2 == e.Id);
                ++callCount;
            };
            try
            {
                bitSwap.WantAsync(cid1, _self.Id, cancel.Token);
                bitSwap.WantAsync(cid1, _self.Id, cancel.Token);
                bitSwap.WantAsync(cid2, _self.Id, cancel.Token);
                bitSwap.WantAsync(cid2, _self.Id, cancel.Token);
                Assert.AreEqual(2, callCount);
            }
            finally
            {
                cancel.Cancel();
            }
        }
Ejemplo n.º 2
0
        public void Found()
        {
            var bitSwapService = new BitSwapService {
                SwarmService = new SwarmService {
                    LocalPeer = _self
                }
            };

            Assert.AreEqual(0, bitSwapService.PeerWants(_self.Id).Count());

            var a      = new DagNode(Encoding.UTF8.GetBytes("BitswapTest found block a"));
            var b      = new DagNode(Encoding.UTF8.GetBytes("BitswapTest found block b"));
            var cancel = new CancellationTokenSource();
            var task   = bitSwapService.WantAsync(a.Id, _self.Id, cancel.Token);

            Assert.False(task.IsCompleted);
            bitSwapService.PeerWants(_self.Id).ToArray().Should().Contain(a.Id);

            bitSwapService.Found(b);
            Assert.False(task.IsCompleted);
            bitSwapService.PeerWants(_self.Id).ToArray().Should().Contain(a.Id);

            bitSwapService.Found(a);
            Assert.True(task.IsCompleted);
            bitSwapService.PeerWants(_self.Id).ToArray().Should().NotContain(a.Id);
            a.DataBytes.Should().Contain(task.Result.DataBytes);
        }
Ejemplo n.º 3
0
        public DfsService(IBitSwapApi bitSwapApi,
                          BitSwapService bitSwapService,
                          IBlockApi blockApi,
                          IBlockRepositoryApi blockRepositoryApi,
                          IBootstrapApi bootstrapApi,
                          IConfigApi configApi,
                          IDagApi dagApi,
                          IDhtApi dhtApi,
                          IDnsApi dnsApi,
                          KatDhtService dhtService,
                          IUnixFsApi unixFsApi,
                          IKeyApi keyApi,
                          INameApi nameApi,
                          IObjectApi objectApi,
                          IPinApi pinApi,
                          Ping1 pingService,
                          IPubSubApi pubSubApi,
                          PubSubService pubSubService,
                          IStatsApi statsApi,
                          ISwarmApi swarmApi,
                          SwarmService swarmService,
                          DfsOptions dfsOptions,
                          IHashProvider hashProvider,
                          DfsState dfsState,
                          IPasswordManager passwordManager,
                          IMigrationManager migrationManager,
                          Peer localPeer)
        {
            BitSwapApi         = bitSwapApi;
            BitSwapService     = bitSwapService;
            BlockApi           = blockApi;
            BlockRepositoryApi = blockRepositoryApi;
            BootstrapApi       = bootstrapApi;
            ConfigApi          = configApi;
            DagApi             = dagApi;
            DhtApi             = dhtApi;
            DhtService         = dhtService;
            UnixFsApi          = unixFsApi;
            KeyApi             = keyApi;
            NameApi            = nameApi;
            ObjectApi          = objectApi;
            PinApi             = pinApi;
            PingService        = pingService;
            PubSubApi          = pubSubApi;
            PubSubService      = pubSubService;
            StatsApi           = statsApi;
            SwarmApi           = swarmApi;
            SwarmService       = swarmService;
            Options            = dfsOptions;
            _hashProvider      = hashProvider;
            _dfsState          = dfsState;
            DnsApi             = dnsApi;
            MigrationManager   = migrationManager;
            LocalPeer          = localPeer;

            InitAsync().Wait();
        }
Ejemplo n.º 4
0
        public void WantUnwantTests()
        {
            var bitSwapService = new BitSwapService {
                SwarmService = new SwarmService {
                    LocalPeer = _self
                }
            };
            var cid    = new DagNode(Encoding.UTF8.GetBytes("BitswapTest unknown block")).Id;
            var cancel = new CancellationTokenSource();
            var task   = bitSwapService.WantAsync(cid, _self.Id, cancel.Token);

            bitSwapService.PeerWants(_self.Id).ToArray().Should().Contain(cid);

            bitSwapService.Unwant(cid);
            Assert.True(task.IsCanceled);
            bitSwapService.PeerWants(_self.Id).ToArray().Should().NotContain(cid);
        }
Ejemplo n.º 5
0
        public void WantList()
        {
            var bitSwap = new BitSwapService {
                SwarmService = new SwarmService {
                    LocalPeer = _self
                }
            };

            Assert.AreEqual(0, bitSwap.PeerWants(_self.Id).Count());

            var cid    = new DagNode(Encoding.UTF8.GetBytes("BitswapTest unknown block")).Id;
            var cancel = new CancellationTokenSource();
            var _      = bitSwap.WantAsync(cid, _self.Id, cancel.Token);

            bitSwap.PeerWants(_self.Id).ToArray().Should().Contain(cid);

            bitSwap.Unwant(cid);
            bitSwap.PeerWants(_self.Id).ToArray().Should().NotContain(cid);
        }
Ejemplo n.º 6
0
        public void Found_Count()
        {
            var bitSwapService = new BitSwapService {
                SwarmService = new SwarmService {
                    LocalPeer = _self
                }
            };

            var a = new DagNode(Encoding.UTF8.GetBytes("BitswapTest found block a"));

            Assert.AreEqual(0, bitSwapService.Found(a));

            var cancel = new CancellationTokenSource();
            var task1  = bitSwapService.WantAsync(a.Id, _self.Id, cancel.Token);
            var task2  = bitSwapService.WantAsync(a.Id, _self.Id, cancel.Token);

            Assert.AreEqual(2, bitSwapService.Found(a));

            Assert.True(task1.IsCompleted);
            Assert.True(task2.IsCompleted);
        }
Ejemplo n.º 7
0
        /// <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");
        }
Ejemplo n.º 8
0
 public BitSwapApi(BitSwapService bitSwapService, IKeyApi keyApi, Peer localPeer)
 {
     _bitSwapService = bitSwapService;
     _localPeer      = localPeer;
 }