core.go 6.1 KB
Newer Older
1
/*
tavit ohanian's avatar
tavit ohanian committed
2
Package core implements the Dms3Node object and related methods.
3 4

Packages underneath core/ provide a (relatively) stable, low-level API
tavit ohanian's avatar
tavit ohanian committed
5 6
to carry out most DMS3-related tasks.  For more details on the other
interfaces and how core/... fits into the bigger DMS3 picture, see:
7

tavit ohanian's avatar
tavit ohanian committed
8
  $ godoc gitlab.dms3.io/dms3/go-dms3
9
*/
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10 11
package core

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12
import (
13
	"context"
14
	"io"
15

tavit ohanian's avatar
tavit ohanian committed
16 17
	"gitlab.dms3.io/dms3/go-filestore"

Raúl Kripalani's avatar
Raúl Kripalani committed
18
	goprocess "github.com/jbenet/goprocess"
tavit ohanian's avatar
tavit ohanian committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

	bserv "gitlab.dms3.io/dms3/go-blockservice"
	bstore "gitlab.dms3.io/dms3/go-dms3-blockstore"
	exchange "gitlab.dms3.io/dms3/go-dms3-exchange-interface"
	pin "gitlab.dms3.io/dms3/go-dms3-pinner"
	provider "gitlab.dms3.io/dms3/go-dms3-provider"
	"gitlab.dms3.io/dms3/go-graphsync"
	ld "gitlab.dms3.io/dms3/go-ld-format"
	logging "gitlab.dms3.io/dms3/go-log"
	mfs "gitlab.dms3.io/dms3/go-mfs"
	resolver "gitlab.dms3.io/dms3/go-path/resolver"

	ma "gitlab.dms3.io/mf/go-multiaddr"

	connmgr "gitlab.dms3.io/p2p/go-p2p-core/connmgr"
	ic "gitlab.dms3.io/p2p/go-p2p-core/crypto"
	p2phost "gitlab.dms3.io/p2p/go-p2p-core/host"
	metrics "gitlab.dms3.io/p2p/go-p2p-core/metrics"
	peer "gitlab.dms3.io/p2p/go-p2p-core/peer"
	pstore "gitlab.dms3.io/p2p/go-p2p-core/peerstore"
	routing "gitlab.dms3.io/p2p/go-p2p-core/routing"
	ddht "gitlab.dms3.io/p2p/go-p2p-kad-dht/dual"
	pubsub "gitlab.dms3.io/p2p/go-p2p-pubsub"
	psrouter "gitlab.dms3.io/p2p/go-p2p-pubsub-router"
	record "gitlab.dms3.io/p2p/go-p2p-record"
	"gitlab.dms3.io/p2p/go-p2p/p2p/discovery"
	p2pbhost "gitlab.dms3.io/p2p/go-p2p/p2p/host/basic"

	"gitlab.dms3.io/dms3/go-dms3/core/bootstrap"
	"gitlab.dms3.io/dms3/go-dms3/core/node"
	"gitlab.dms3.io/dms3/go-dms3/core/node/dms3p2p"
	"gitlab.dms3.io/dms3/go-dms3/fuse/mount"
	"gitlab.dms3.io/dms3/go-dms3/namesys"
	dms3nsrp "gitlab.dms3.io/dms3/go-dms3/namesys/republisher"
	"gitlab.dms3.io/dms3/go-dms3/p2p"
	"gitlab.dms3.io/dms3/go-dms3/peering"
	"gitlab.dms3.io/dms3/go-dms3/repo"
Łukasz Magiera's avatar
Łukasz Magiera committed
56
)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
57

Jeromy's avatar
Jeromy committed
58
var log = logging.Logger("core")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
59

tavit ohanian's avatar
tavit ohanian committed
60 61
// Dms3Node is DMS3 Core module. It represents an DMS3 instance.
type Dms3Node struct {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
62

63
	// Self
64
	Identity peer.ID // the local node's identity
65

66
	Repo repo.Repo
67 68

	// Local node
tavit ohanian's avatar
tavit ohanian committed
69 70 71 72
	Pinning         pin.Pinner              // the pinning manager
	Mounts          Mounts                  `optional:"true"` // current mount state, if any.
	PrivateKey      ic.PrivKey              `optional:"true"` // the local node's private Key
	PNetFingerprint dms3p2p.PNetFingerprint `optional:"true"` // fingerprint of private network
73 74

	// Services
75 76 77 78 79 80
	Peerstore       pstore.Peerstore          `optional:"true"` // storage for other Peer instances
	Blockstore      bstore.GCBlockstore       // the block store (lower level)
	Filestore       *filestore.Filestore      `optional:"true"` // the filestore blockstore
	BaseBlocks      node.BaseBlocks           // the raw blockstore, no filestore wrapping
	GCLocker        bstore.GCLocker           // the locker used to protect the blockstore during gc
	Blocks          bserv.BlockService        // the block service, get/add blocks.
tavit ohanian's avatar
tavit ohanian committed
81
	DAG             ld.DAGService             // the merkle dag service, get/add objects.
82 83 84
	Resolver        *resolver.Resolver        // the path resolution system
	Reporter        *metrics.BandwidthCounter `optional:"true"`
	Discovery       discovery.Service         `optional:"true"`
85 86
	FilesRoot       *mfs.Root
	RecordValidator record.Validator
87 88

	// Online
89
	PeerHost      p2phost.Host            `optional:"true"` // the network host (server+client)
Steven Allen's avatar
Steven Allen committed
90
	Peering       peering.PeeringService  `optional:"true"`
Steven Allen's avatar
Steven Allen committed
91
	Filters       *ma.Filters             `optional:"true"`
92
	Bootstrapper  io.Closer               `optional:"true"` // the periodic bootstrapper
tavit ohanian's avatar
tavit ohanian committed
93
	Routing       routing.Routing         `optional:"true"` // the routing system. recommend dms3-dht
94 95 96
	Exchange      exchange.Interface      // the block exchange + strategy (bitswap)
	Namesys       namesys.NameSystem      // the name system, resolves paths to hashes
	Provider      provider.System         // the value provider system
tavit ohanian's avatar
tavit ohanian committed
97
	Dms3NsRepub   *dms3nsrp.Republisher   `optional:"true"`
98
	GraphExchange graphsync.GraphExchange `optional:"true"`
99

Łukasz Magiera's avatar
Łukasz Magiera committed
100
	PubSub   *pubsub.PubSub             `optional:"true"`
Łukasz Magiera's avatar
Łukasz Magiera committed
101
	PSRouter *psrouter.PubsubValueStore `optional:"true"`
Steven Allen's avatar
Steven Allen committed
102
	DHT      *ddht.DHT                  `optional:"true"`
Łukasz Magiera's avatar
Łukasz Magiera committed
103
	P2P      *p2p.P2P                   `optional:"true"`
Jeromy's avatar
Jeromy committed
104

105
	Process goprocess.Process
Łukasz Magiera's avatar
Łukasz Magiera committed
106
	ctx     context.Context
107

Łukasz Magiera's avatar
Łukasz Magiera committed
108
	stop func() error
Łukasz Magiera's avatar
Łukasz Magiera committed
109

110
	// Flags
Łukasz Magiera's avatar
Łukasz Magiera committed
111 112
	IsOnline bool `optional:"true"` // Online is set when networking is enabled.
	IsDaemon bool `optional:"true"` // Daemon is set when running on a long-running daemon.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
113 114
}

115 116 117 118
// Mounts defines what the node's mount state is. This should
// perhaps be moved to the daemon or mount. It's here because
// it needs to be accessible across daemon requests.
type Mounts struct {
tavit ohanian's avatar
tavit ohanian committed
119 120
	Dms3   mount.Mount
	Dms3Ns mount.Mount
121 122
}

Łukasz Magiera's avatar
Łukasz Magiera committed
123
// Close calls Close() on the App object
tavit ohanian's avatar
tavit ohanian committed
124
func (n *Dms3Node) Close() error {
Łukasz Magiera's avatar
Łukasz Magiera committed
125
	return n.stop()
126 127
}

tavit ohanian's avatar
tavit ohanian committed
128 129
// Context returns the Dms3Node context
func (n *Dms3Node) Context() context.Context {
130 131 132
	if n.ctx == nil {
		n.ctx = context.TODO()
	}
133 134 135
	return n.ctx
}

tavit ohanian's avatar
tavit ohanian committed
136 137
// Bootstrap will set and call the Dms3Nodes bootstrap function.
func (n *Dms3Node) Bootstrap(cfg bootstrap.BootstrapConfig) error {
138
	// TODO what should return value be when in offlineMode?
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
139 140 141 142
	if n.Routing == nil {
		return nil
	}

143 144 145 146 147 148 149
	if n.Bootstrapper != nil {
		n.Bootstrapper.Close() // stop previous bootstrap process.
	}

	// if the caller did not specify a bootstrap peer function, get the
	// freshest bootstrap peers from config. this responds to live changes.
	if cfg.BootstrapPeers == nil {
Raúl Kripalani's avatar
Raúl Kripalani committed
150
		cfg.BootstrapPeers = func() []peer.AddrInfo {
151
			ps, err := n.loadBootstrapPeers()
152
			if err != nil {
153
				log.Warn("failed to parse bootstrap peers from config")
154 155 156 157 158 159 160
				return nil
			}
			return ps
		}
	}

	var err error
Łukasz Magiera's avatar
Łukasz Magiera committed
161
	n.Bootstrapper, err = bootstrap.Bootstrap(n.Identity, n.PeerHost, n.Routing, cfg)
162
	return err
163 164
}

tavit ohanian's avatar
tavit ohanian committed
165
func (n *Dms3Node) loadBootstrapPeers() ([]peer.AddrInfo, error) {
166 167 168 169 170
	cfg, err := n.Repo.Config()
	if err != nil {
		return nil, err
	}

Steven Allen's avatar
Steven Allen committed
171
	return cfg.BootstrapPeers()
172
}
173

Kevin Atkinson's avatar
Kevin Atkinson committed
174
type ConstructPeerHostOpts struct {
175
	AddrsFactory      p2pbhost.AddrsFactory
vyzo's avatar
vyzo committed
176 177 178
	DisableNatPortMap bool
	DisableRelay      bool
	EnableRelayHop    bool
Raúl Kripalani's avatar
Raúl Kripalani committed
179
	ConnectionManager connmgr.ConnManager
Kevin Atkinson's avatar
Kevin Atkinson committed
180
}