core.go 3.16 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 (
4 5 6
	"crypto"
	"crypto/rsa"
	"errors"
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
7
	"fmt"
8

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
9
	ds "github.com/jbenet/datastore.go"
10
	b58 "github.com/jbenet/go-base58"
11
	"github.com/jbenet/go-ipfs/bitswap"
12
	bserv "github.com/jbenet/go-ipfs/blockservice"
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
13
	config "github.com/jbenet/go-ipfs/config"
14
	"github.com/jbenet/go-ipfs/identify"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15
	merkledag "github.com/jbenet/go-ipfs/merkledag"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
16
	path "github.com/jbenet/go-ipfs/path"
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
17
	peer "github.com/jbenet/go-ipfs/peer"
18 19 20 21 22
	routing "github.com/jbenet/go-ipfs/routing"
	dht "github.com/jbenet/go-ipfs/routing/dht"
	swarm "github.com/jbenet/go-ipfs/swarm"
	u "github.com/jbenet/go-ipfs/util"
	ma "github.com/jbenet/go-multiaddr"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
23 24
)

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

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
28 29
	// the node's configuration
	Config *config.Config
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
31 32
	// the local node's identity
	Identity *peer.Peer
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
33

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
34 35
	// the map of other nodes (Peer instances)
	PeerMap *peer.Map
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
36

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
37 38
	// the local datastore
	Datastore ds.Datastore
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
39

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
40
	// the network message stream
41
	Swarm *swarm.Swarm
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
42

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
43
	// the routing system. recommend ipfs-dht
44
	Routing routing.IpfsRouting
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
45

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
46
	// the block exchange + strategy (bitswap)
47
	BitSwap *bitswap.BitSwap
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
48

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
49
	// the block service, get/add blocks.
50
	Blocks *bserv.BlockService
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
51

52 53 54
	// the merkle dag service, get/add objects.
	DAG *merkledag.DAGService

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
55
	// the path resolution system
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
56
	Resolver *path.Resolver
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
57

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
58 59
	// the name system, resolves paths to hashes
	// Namesys *namesys.Namesys
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
60 61
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
62
// NewIpfsNode constructs a new IpfsNode based on the given config.
63
func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
64
	if cfg == nil {
Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
65
		return nil, fmt.Errorf("configuration required")
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
66 67 68 69 70 71 72
	}

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

73 74
	var swap *bitswap.BitSwap
	if online {
75
		swap, err = loadBitswap(cfg, d)
76 77 78 79
		if err != nil {
			return nil, err
		}
	}
80 81

	bs, err := bserv.NewBlockService(d, swap)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
82 83 84 85
	if err != nil {
		return nil, err
	}

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

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
88 89
	n := &IpfsNode{
		Config:    cfg,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
90
		PeerMap:   &peer.Map{},
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
91
		Datastore: d,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
92
		Blocks:    bs,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
93 94
		DAG:       dag,
		Resolver:  &path.Resolver{DAG: dag},
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
95 96 97
	}

	return n, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
98
}
99 100

func loadBitswap(cfg *config.Config, d ds.Datastore) (*bitswap.BitSwap, error) {
101
	maddr, err := ma.NewMultiaddr(cfg.Identity.Address)
102 103 104 105
	if err != nil {
		return nil, err
	}

106 107 108 109 110 111 112 113 114 115 116 117 118
	pk, err := cfg.Identity.DecodePrivateKey("")
	if err != nil {
		return nil, err
	}

	var pubkey crypto.PublicKey
	switch k := pk.(type) {
	case *rsa.PrivateKey:
		pubkey = &k.PublicKey
	default:
		return nil, identify.ErrUnsupportedKeyType
	}

119
	local := &peer.Peer{
120
		ID:        peer.ID(b58.Decode(cfg.Identity.PeerID)),
121
		Addresses: []*ma.Multiaddr{maddr},
122 123
		PrivKey:   pk,
		PubKey:    pubkey,
124 125 126
	}

	if len(local.ID) == 0 {
127
		return nil, errors.New("No peer ID in config! (was ipfs init run?)")
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
	}

	net := swarm.NewSwarm(local)
	err = net.Listen()
	if err != nil {
		return nil, err
	}

	route := dht.NewDHT(local, net, d)
	route.Start()

	for _, p := range cfg.Peers {
		maddr, err := ma.NewMultiaddr(p.Address)
		if err != nil {
			u.PErr("error: %v\n", err)
			continue
		}

		_, err = route.Connect(maddr)
		if err != nil {
			u.PErr("Bootstrapping error: %v\n", err)
		}
	}

	return bitswap.NewBitSwap(local, net, d, route), nil
}