core.go 1.48 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
go fmt  
Juan Batiz-Benet committed
9
	peer "github.com/jbenet/go-ipfs/peer"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10 11
)

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

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

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

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

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

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

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

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

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

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

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

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

func NewIpfsNode(cfg *config.Config) (*IpfsNode, error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
50 51 52 53 54 55 56 57 58
	if cfg == nil {
		return nil, fmt.Errorf("configuration required.")
	}

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

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

64 65
	dag := &merkledag.DAGService{ Blocks: bs }

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

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