core.go 1.69 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
	u "github.com/jbenet/go-ipfs/util"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11 12
)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13
// 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 44
	// the path resolution system
	// Resolver *resolver.PathResolver
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 50
}

func NewIpfsNode(cfg *config.Config) (*IpfsNode, error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
51 52 53 54 55 56 57 58 59
	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
60 61 62 63 64
	bs, err := blocks.NewBlockService(d)
	if err != nil {
		return nil, err
	}

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

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

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

func (n *IpfsNode) AddDagNode(nd *merkledag.Node) (u.Key, error) {
79
	return n.DAG.Put(nd)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
80
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
81 82

func (n *IpfsNode) GetDagNode(k u.Key) (*merkledag.Node, error) {
83
	return n.DAG.Get(k)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
84
}