-
Notifications
You must be signed in to change notification settings - Fork 6
/
ipfs.go
349 lines (300 loc) · 8.06 KB
/
ipfs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package ipfslite
import (
"context"
"errors"
"fmt"
"io"
"strings"
"sync"
"time"
"github.com/bytecodealliance/wasmtime-go"
"github.com/ipfs/go-bitswap"
"github.com/ipfs/go-bitswap/network"
blockservice "github.com/ipfs/go-blockservice"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
blockstore "github.com/ipfs/go-ipfs-blockstore"
chunker "github.com/ipfs/go-ipfs-chunker"
offline "github.com/ipfs/go-ipfs-exchange-offline"
provider "github.com/ipfs/go-ipfs-provider"
"github.com/ipfs/go-ipfs-provider/queue"
"github.com/ipfs/go-ipfs-provider/simple"
cbor "github.com/ipfs/go-ipld-cbor"
ipld "github.com/ipfs/go-ipld-format"
logging "github.com/ipfs/go-log/v2"
"github.com/ipfs/go-merkledag"
"github.com/ipfs/go-unixfs/importer/balanced"
"github.com/ipfs/go-unixfs/importer/helpers"
"github.com/ipfs/go-unixfs/importer/trickle"
ufsio "github.com/ipfs/go-unixfs/io"
host "github.com/libp2p/go-libp2p-core/host"
peer "github.com/libp2p/go-libp2p-core/peer"
routing "github.com/libp2p/go-libp2p-core/routing"
multihash "github.com/multiformats/go-multihash"
)
func init() {
ipld.Register(cid.DagProtobuf, merkledag.DecodeProtobufBlock)
ipld.Register(cid.Raw, merkledag.DecodeRawBlock)
ipld.Register(cid.DagCBOR, cbor.DecodeBlock) // need to decode CBOR
}
var logger = logging.Logger("ipfslite")
var (
defaultReprovideInterval = 12 * time.Hour
)
// Config wraps configuration options for the Peer.
type Config struct {
// The DAGService will not announce or retrieve blocks from the network
Offline bool
// ReprovideInterval sets how often to reprovide records to the DHT
ReprovideInterval time.Duration
}
func (cfg *Config) setDefaults() {
if cfg.ReprovideInterval == 0 {
cfg.ReprovideInterval = defaultReprovideInterval
}
}
// Peer is an IPFS-Lite peer. It provides a DAG service that can fetch and put
// blocks from/to the IPFS network.
type Peer struct {
ctx context.Context
cfg *Config
host host.Host
dht routing.Routing
store datastore.Batching
ipld.DAGService // become a DAG service
bstore blockstore.Blockstore
bserv blockservice.BlockService
reprovider provider.System
runtime *wasmtime.Store
}
// New creates an IPFS-Lite Peer. It uses the given datastore, libp2p Host and
// Routing (usuall the DHT). The Host and the Routing may be nil if
// config.Offline is set to true, as they are not used in that case. Peer
// implements the ipld.DAGService interface.
func New(
ctx context.Context,
store datastore.Batching,
host host.Host,
dht routing.Routing,
cfg *Config,
) (*Peer, error) {
if cfg == nil {
cfg = &Config{}
}
cfg.setDefaults()
p := &Peer{
ctx: ctx,
cfg: cfg,
host: host,
dht: dht,
store: store,
}
err := p.setupBlockstore()
if err != nil {
return nil, err
}
err = p.setupBlockService()
if err != nil {
return nil, err
}
err = p.setupDAGService()
if err != nil {
p.bserv.Close()
return nil, err
}
err = p.setupReprovider()
if err != nil {
p.bserv.Close()
return nil, err
}
p.setupRuntime()
go p.autoclose()
return p, nil
}
func (p *Peer) setupRuntime() {
p.runtime = wasmtime.NewStore(wasmtime.NewEngine())
}
// Runtime return the peer's WASM runtime
func (p *Peer) Runtime() *wasmtime.Store {
return p.runtime
}
func (p *Peer) setupBlockstore() error {
bs := blockstore.NewBlockstore(p.store)
bs = blockstore.NewIdStore(bs)
cachedbs, err := blockstore.CachedBlockstore(p.ctx, bs, blockstore.DefaultCacheOpts())
if err != nil {
return err
}
p.bstore = cachedbs
return nil
}
func (p *Peer) setupBlockService() error {
if p.cfg.Offline {
p.bserv = blockservice.New(p.bstore, offline.Exchange(p.bstore))
return nil
}
bswapnet := network.NewFromIpfsHost(p.host, p.dht)
bswap := bitswap.New(p.ctx, bswapnet, p.bstore)
p.bserv = blockservice.New(p.bstore, bswap)
return nil
}
func (p *Peer) setupDAGService() error {
p.DAGService = merkledag.NewDAGService(p.bserv)
return nil
}
func (p *Peer) setupReprovider() error {
if p.cfg.Offline || p.cfg.ReprovideInterval < 0 {
p.reprovider = provider.NewOfflineProvider()
return nil
}
queue, err := queue.NewQueue(p.ctx, "repro", p.store)
if err != nil {
return err
}
prov := simple.NewProvider(
p.ctx,
queue,
p.dht,
)
reprov := simple.NewReprovider(
p.ctx,
p.cfg.ReprovideInterval,
p.dht,
simple.NewBlockstoreProvider(p.bstore),
)
p.reprovider = provider.NewSystem(prov, reprov)
p.reprovider.Run()
return nil
}
func (p *Peer) autoclose() {
<-p.ctx.Done()
p.reprovider.Close()
p.bserv.Close()
}
// Bootstrap is an optional helper to connect to the given peers and bootstrap
// the Peer DHT (and Bitswap). This is a best-effort function. Errors are only
// logged and a warning is printed when less than half of the given peers
// could be contacted. It is fine to pass a list where some peers will not be
// reachable.
func (p *Peer) Bootstrap(peers []peer.AddrInfo) {
connected := make(chan struct{})
var wg sync.WaitGroup
for _, pinfo := range peers {
//h.Peerstore().AddAddrs(pinfo.ID, pinfo.Addrs, peerstore.PermanentAddrTTL)
wg.Add(1)
go func(pinfo peer.AddrInfo) {
defer wg.Done()
err := p.host.Connect(p.ctx, pinfo)
if err != nil {
logger.Warn(err)
return
}
logger.Info("Connected to", pinfo.ID)
connected <- struct{}{}
}(pinfo)
}
go func() {
wg.Wait()
close(connected)
}()
i := 0
for range connected {
i++
}
if nPeers := len(peers); i < nPeers/2 {
logger.Warnf("only connected to %d bootstrap peers out of %d", i, nPeers)
}
err := p.dht.Bootstrap(p.ctx)
if err != nil {
logger.Error(err)
return
}
}
// ConnectPeer using AddrInfo.
func (p *Peer) ConnectPeer(peer peer.AddrInfo) error {
return p.host.Connect(p.ctx, peer)
}
// Session returns a session-based NodeGetter.
func (p *Peer) Session(ctx context.Context) ipld.NodeGetter {
ng := merkledag.NewSession(ctx, p.DAGService)
if ng == p.DAGService {
logger.Warn("DAGService does not support sessions")
}
return ng
}
// AddParams contains all of the configurable parameters needed to specify the
// importing process of a file.
type AddParams struct {
Layout string
Chunker string
RawLeaves bool
Hidden bool
Shard bool
NoCopy bool
HashFun string
}
// AddFile chunks and adds content to the DAGService from a reader. The content
// is stored as a UnixFS DAG (default for IPFS). It returns the root
// ipld.Node.
func (p *Peer) AddFile(ctx context.Context, r io.Reader, params *AddParams) (ipld.Node, error) {
if params == nil {
params = &AddParams{}
}
if params.HashFun == "" {
params.HashFun = "sha2-256"
}
prefix, err := merkledag.PrefixForCidVersion(1)
if err != nil {
return nil, fmt.Errorf("bad CID Version: %s", err)
}
hashFunCode, ok := multihash.Names[strings.ToLower(params.HashFun)]
if !ok {
return nil, fmt.Errorf("unrecognized hash function: %s", params.HashFun)
}
prefix.MhType = hashFunCode
prefix.MhLength = -1
prefix.Codec = cid.DagCBOR
dbp := helpers.DagBuilderParams{
Dagserv: p,
RawLeaves: params.RawLeaves,
Maxlinks: helpers.DefaultLinksPerBlock,
NoCopy: params.NoCopy,
CidBuilder: &prefix,
}
chnk, err := chunker.FromString(r, params.Chunker)
if err != nil {
return nil, err
}
dbh, err := dbp.New(chnk)
if err != nil {
return nil, err
}
var n ipld.Node
switch params.Layout {
case "trickle":
n, err = trickle.Layout(dbh)
case "balanced", "":
n, err = balanced.Layout(dbh)
default:
return nil, errors.New("invalid Layout")
}
return n, err
}
// GetFile returns a reader to a file as identified by its root CID. The file
// must have been added as a UnixFS DAG (default for IPFS).
func (p *Peer) GetFile(ctx context.Context, c cid.Cid) (ufsio.ReadSeekCloser, error) {
n, err := p.Get(ctx, c)
if err != nil {
return nil, err
}
return ufsio.NewDagReader(ctx, n, p)
}
// BlockStore offers access to the blockstore underlying the Peer's DAGService.
func (p *Peer) BlockStore() blockstore.Blockstore {
return p.bstore
}
// HasBlock returns whether a given block is available locally. It is
// a shorthand for .Blockstore().Has().
func (p *Peer) HasBlock(c cid.Cid) (bool, error) {
return p.BlockStore().Has(c)
}