// startOnlineServicesWithHost is the set of services which need to be // initialized with the host and _before_ we start listening. func (n *IpfsNode) startOnlineServicesWithHost(ctx context.Context, host p2phost.Host, routingOption RoutingOption) error { // setup diagnostics service n.Diagnostics = diag.NewDiagnostics(n.Identity, host) n.Ping = ping.NewPingService(host) // setup routing service r, err := routingOption(ctx, host, n.Repo.Datastore()) if err != nil { return err } n.Routing = r // Wrap standard peer host with routing system to allow unknown peer lookups n.PeerHost = rhost.Wrap(host, n.Routing) // setup exchange service const alwaysSendToPeer = true // use YesManStrategy bitswapNetwork := bsnet.NewFromIpfsHost(n.PeerHost, n.Routing) n.Exchange = bitswap.New(ctx, n.Identity, bitswapNetwork, n.Blockstore, alwaysSendToPeer) // setup name system n.Namesys = namesys.NewNameSystem(n.Routing) return nil }
func setupIpnsTest(t *testing.T, node *core.IpfsNode) (*core.IpfsNode, *fstest.Mount) { maybeSkipFuseTests(t) var err error if node == nil { node, err = core.NewNode(context.Background(), nil) if err != nil { t.Fatal(err) } err = node.LoadPrivateKey() if err != nil { t.Fatal(err) } node.Routing = offroute.NewOfflineRouter(node.Repo.Datastore(), node.PrivateKey) node.Namesys = namesys.NewNameSystem(node.Routing, node.Repo.Datastore(), 0) err = InitializeKeyspace(node, node.PrivateKey) if err != nil { t.Fatal(err) } } fs, err := NewFileSystem(node, node.PrivateKey, "", "") if err != nil { t.Fatal(err) } mnt, err := fstest.MountedT(t, fs) if err != nil { t.Fatal(err) } return node, mnt }
// NewMockNode constructs an IpfsNode for use in tests. func NewMockNode() (*core.IpfsNode, error) { ctx := context.Background() // Generate Identity ident, err := testutil.RandIdentity() if err != nil { return nil, err } p := ident.ID() c := config.Config{ Identity: config.Identity{ PeerID: p.String(), }, } nd, err := core.Offline(&repo.Mock{ C: c, D: ds2.CloserWrap(syncds.MutexWrap(datastore.NewMapDatastore())), })(ctx) if err != nil { return nil, err } nd.PrivateKey = ident.PrivateKey() nd.Peerstore = peer.NewPeerstore() nd.Peerstore.AddPrivKey(p, ident.PrivateKey()) nd.Peerstore.AddPubKey(p, ident.PublicKey()) nd.Identity = p nd.PeerHost, err = mocknet.New(nd.Context()).AddPeer(ident.PrivateKey(), ident.Address()) // effectively offline if err != nil { return nil, err } // Routing nd.Routing = offrt.NewOfflineRouter(nd.Repo.Datastore(), nd.PrivateKey) // Bitswap bstore := blockstore.NewBlockstore(nd.Repo.Datastore()) bserv, err := blockservice.New(bstore, offline.Exchange(bstore)) if err != nil { return nil, err } nd.DAG = mdag.NewDAGService(bserv) nd.Pinning = pin.NewPinner(nd.Repo.Datastore(), nd.DAG) // Namespace resolver nd.Namesys = nsys.NewNameSystem(nd.Routing) // Path resolver nd.Resolver = &path.Resolver{DAG: nd.DAG} return nd, nil }
// SetupOfflineRouting loads the local nodes private key and // uses it to instantiate a routing system in offline mode. // This is primarily used for offline ipns modifications. func (n *IpfsNode) SetupOfflineRouting() error { err := n.LoadPrivateKey() if err != nil { return err } n.Routing = offroute.NewOfflineRouter(n.Repo.Datastore(), n.PrivateKey) n.Namesys = namesys.NewNameSystem(n.Routing) return nil }
// SetupOfflineRouting loads the local nodes private key and // uses it to instantiate a routing system in offline mode. // This is primarily used for offline ipns modifications. func (n *IpfsNode) SetupOfflineRouting() error { if n.Routing != nil { // Routing was already set up return nil } err := n.LoadPrivateKey() if err != nil { return err } n.Routing = offroute.NewOfflineRouter(n.Repo.Datastore(), n.PrivateKey) size, err := n.getCacheSize() if err != nil { return err } n.Namesys = namesys.NewNameSystem(n.Routing, n.Repo.Datastore(), size) return nil }
// default to nodes namesys resolver var resolver namesys.Resolver = n.Namesys if local && nocache { res.SetError(errors.New("cannot specify both local and nocache"), cmds.ErrNormal) return } if local { offroute := offline.NewOfflineRouter(n.Repo.Datastore(), n.PrivateKey) resolver = namesys.NewRoutingResolver(offroute, 0) } if nocache { resolver = namesys.NewNameSystem(n.Routing, n.Repo.Datastore(), 0) } var name string if len(req.Arguments()) == 0 { if n.Identity == "" { res.SetError(errors.New("Identity not loaded!"), cmds.ErrNormal) return } name = n.Identity.Pretty() } else { name = req.Arguments()[0] } recursive, _, _ := req.Option("recursive").Bool()
// Test externally unmounting, then trying to unmount in code func TestExternalUnmount(t *testing.T) { if testing.Short() { t.SkipNow() } // TODO: needed? maybeSkipFuseTests(t) node, err := core.NewNode(context.Background(), nil) if err != nil { t.Fatal(err) } err = node.LoadPrivateKey() if err != nil { t.Fatal(err) } node.Routing = offroute.NewOfflineRouter(node.Repo.Datastore(), node.PrivateKey) node.Namesys = namesys.NewNameSystem(node.Routing, node.Repo.Datastore(), 0) err = ipns.InitializeKeyspace(node, node.PrivateKey) if err != nil { t.Fatal(err) } // get the test dir paths (/tmp/fusetestXXXX) dir, err := ioutil.TempDir("", "fusetest") if err != nil { t.Fatal(err) } ipfsDir := dir + "/ipfs" ipnsDir := dir + "/ipns" mkdir(t, ipfsDir) mkdir(t, ipnsDir) err = Mount(node, ipfsDir, ipnsDir) if err != nil { t.Fatal(err) } // Run shell command to externally unmount the directory cmd := "fusermount" args := []string{"-u", ipnsDir} if err := exec.Command(cmd, args...).Run(); err != nil { t.Fatal(err) } // TODO(noffle): it takes a moment for the goroutine that's running fs.Serve to be notified and do its cleanup. time.Sleep(time.Millisecond * 100) // Attempt to unmount IPNS; check that it was already unmounted. err = node.Mounts.Ipns.Unmount() if err != mount.ErrNotMounted { t.Fatal("Unmount should have failed") } // Attempt to unmount IPFS; it should unmount successfully. err = node.Mounts.Ipfs.Unmount() if err != nil { t.Fatal(err) } }
func TestRepublish(t *testing.T) { // set cache life to zero for testing low-period repubs ctx, cancel := context.WithCancel(context.Background()) defer cancel() // create network mn := mocknet.New(ctx) var nodes []*core.IpfsNode for i := 0; i < 10; i++ { nd, err := core.NewNode(ctx, &core.BuildCfg{ Online: true, Host: mock.MockHostOption(mn), }) if err != nil { t.Fatal(err) } nd.Namesys = namesys.NewNameSystem(nd.Routing, nd.Repo.Datastore(), 0) nodes = append(nodes, nd) } mn.LinkAll() bsinf := core.BootstrapConfigWithPeers( []pstore.PeerInfo{ nodes[0].Peerstore.PeerInfo(nodes[0].Identity), }, ) for _, n := range nodes[1:] { if err := n.Bootstrap(bsinf); err != nil { t.Fatal(err) } } // have one node publish a record that is valid for 1 second publisher := nodes[3] p := path.FromString("/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn") // does not need to be valid rp := namesys.NewRoutingPublisher(publisher.Routing, publisher.Repo.Datastore()) err := rp.PublishWithEOL(ctx, publisher.PrivateKey, p, time.Now().Add(time.Second)) if err != nil { t.Fatal(err) } name := "/ipns/" + publisher.Identity.Pretty() if err := verifyResolution(nodes, name, p); err != nil { t.Fatal(err) } // Now wait a second, the records will be invalid and we should fail to resolve time.Sleep(time.Second) if err := verifyResolutionFails(nodes, name); err != nil { t.Fatal(err) } // The republishers that are contained within the nodes have their timeout set // to 12 hours. Instead of trying to tweak those, we're just going to pretend // they dont exist and make our own. repub := NewRepublisher(publisher.Routing, publisher.Repo.Datastore(), publisher.Peerstore) repub.Interval = time.Second repub.RecordLifetime = time.Second * 5 repub.AddName(publisher.Identity) proc := goprocess.Go(repub.Run) defer proc.Close() // now wait a couple seconds for it to fire time.Sleep(time.Second * 2) // we should be able to resolve them now if err := verifyResolution(nodes, name, p); err != nil { t.Fatal(err) } }