core.go 1.62 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 5
	"fmt"
	ds "github.com/jbenet/datastore.go"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
6
	blocks "github.com/jbenet/go-ipfs/blocks"
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
7
	config "github.com/jbenet/go-ipfs/config"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
8
	merkledag "github.com/jbenet/go-ipfs/merkledag"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9
	path "github.com/jbenet/go-ipfs/path"
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
10
	peer "github.com/jbenet/go-ipfs/peer"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11 12
)

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

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

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

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
22 23
	// the book of other nodes (a map of Peer instances)
	PeerBook *peer.PeerBook
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
24

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

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

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

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

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

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

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

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

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

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

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

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

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

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