core.go 1.61 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2
package core

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
3
import (
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
4
	"fmt"
5

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
6
	ds "github.com/jbenet/datastore.go"
7
	bserv "github.com/jbenet/go-ipfs/blockservice"
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
8
	config "github.com/jbenet/go-ipfs/config"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9
	merkledag "github.com/jbenet/go-ipfs/merkledag"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10
	path "github.com/jbenet/go-ipfs/path"
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
11
	peer "github.com/jbenet/go-ipfs/peer"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12 13
)

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
14
// IpfsNode is IPFS Core module. It represents an IPFS instance.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15 16
type IpfsNode struct {

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
17 18
	// the node's configuration
	Config *config.Config
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
19

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
20 21
	// the local node's identity
	Identity *peer.Peer
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
22

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
23 24
	// the map of other nodes (Peer instances)
	PeerMap *peer.Map
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
25

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
26 27
	// the local datastore
	Datastore ds.Datastore
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
28

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
29 30
	// the network message stream
	// Network *netmux.Netux
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
31

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
32 33
	// the routing system. recommend ipfs-dht
	// Routing *routing.Routing
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
34

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
35 36
	// the block exchange + strategy (bitswap)
	// BitSwap *bitswap.BitSwap
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
37

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
38
	// the block service, get/add blocks.
39
	Blocks *bserv.BlockService
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
40

41 42 43
	// the merkle dag service, get/add objects.
	DAG *merkledag.DAGService

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
44
	// the path resolution system
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
45
	Resolver *path.Resolver
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
46

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
47 48
	// the name system, resolves paths to hashes
	// Namesys *namesys.Namesys
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
49 50
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
51
// NewIpfsNode constructs a new IpfsNode based on the given config.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
52
func NewIpfsNode(cfg *config.Config) (*IpfsNode, error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
53
	if cfg == nil {
Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
54
		return nil, fmt.Errorf("configuration required")
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
55 56 57 58 59 60 61
	}

	d, err := makeDatastore(cfg.Datastore)
	if err != nil {
		return nil, err
	}

Jeromy's avatar
Jeromy committed
62
	bs, err := bserv.NewBlockService(d, nil)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
63 64 65 66
	if err != nil {
		return nil, err
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
67
	dag := &merkledag.DAGService{Blocks: bs}
68

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
69 70
	n := &IpfsNode{
		Config:    cfg,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
71
		PeerMap:   &peer.Map{},
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
72
		Datastore: d,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
73
		Blocks:    bs,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
74 75
		DAG:       dag,
		Resolver:  &path.Resolver{DAG: dag},
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
76 77 78
	}

	return n, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
79
}