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 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 14
// 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
go fmt  
Juan Batiz-Benet committed
23 24
	// the book of other nodes (a map of Peer instances)
	PeerBook *peer.PeerBook
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.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
39
	Blocks *blocks.BlockService
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
40

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

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

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

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
63 64 65 66
	n := &IpfsNode{
		Config:    cfg,
		PeerBook:  &peer.PeerBook{},
		Datastore: d,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
67
		Blocks:    bs,
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
68 69 70
	}

	return n, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
71
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85

func (n *IpfsNode) AddDagNode(nd *merkledag.Node) (u.Key, error) {
	d, err := nd.Encoded(false)
	if err != nil {
		return "", err
	}

	b, err := blocks.NewBlock(d)
	if err != nil {
		return "", err
	}

	return n.Blocks.AddBlock(b)
}