-
Notifications
You must be signed in to change notification settings - Fork 58
/
ipfs.go
339 lines (294 loc) · 8.24 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
// Package ipfslite is a lightweight IPFS peer which runs the minimal setup to
// provide an `ipld.DAGService`, "Add" and "Get" UnixFS files from IPFS.
package ipfslite
import (
"context"
"errors"
"fmt"
"io"
"strings"
"sync"
"time"
"github.com/ipfs/boxo/bitswap"
"github.com/ipfs/boxo/bitswap/network"
"github.com/ipfs/boxo/blockservice"
blockstore "github.com/ipfs/boxo/blockstore"
chunker "github.com/ipfs/boxo/chunker"
exchange "github.com/ipfs/boxo/exchange"
offline "github.com/ipfs/boxo/exchange/offline"
"github.com/ipfs/boxo/ipld/merkledag"
"github.com/ipfs/boxo/ipld/unixfs/importer/balanced"
"github.com/ipfs/boxo/ipld/unixfs/importer/helpers"
"github.com/ipfs/boxo/ipld/unixfs/importer/trickle"
ufsio "github.com/ipfs/boxo/ipld/unixfs/io"
provider "github.com/ipfs/boxo/provider"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
ipld "github.com/ipfs/go-ipld-format"
logging "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/routing"
"github.com/multiformats/go-multihash"
)
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
// Disables wrapping the blockstore in an ARC cache + Bloomfilter. Use
// when the given blockstore or datastore already has caching, or when
// caching is not needed.
UncachedBlockstore bool
}
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
exch exchange.Interface
bstore blockstore.Blockstore
bserv blockservice.BlockService
reprovider provider.System
}
// New creates an IPFS-Lite Peer. It uses the given datastore, blockstore,
// libp2p Host and Routing (usuall the DHT). If the blockstore is nil, the
// given datastore will be wrapped to create one. 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,
datastore datastore.Batching,
blockstore blockstore.Blockstore,
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: datastore,
}
err := p.setupBlockstore(blockstore)
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
}
go p.autoclose()
return p, nil
}
func (p *Peer) setupBlockstore(bs blockstore.Blockstore) error {
var err error
if bs == nil {
bs = blockstore.NewBlockstore(p.store)
}
// Support Identity multihashes.
bs = blockstore.NewIdStore(bs)
if !p.cfg.UncachedBlockstore {
bs, err = blockstore.CachedBlockstore(p.ctx, bs, blockstore.DefaultCacheOpts())
if err != nil {
return err
}
}
p.bstore = bs
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)
p.exch = 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.NewNoopProvider()
return nil
}
prov, err := provider.New(p.store,
provider.DatastorePrefix(datastore.NewKey("repro")),
provider.Online(p.dht),
provider.ReproviderInterval(p.cfg.ReprovideInterval),
provider.KeyProvider(provider.NewBlockstoreProvider(p.bstore)))
if err != nil {
return err
}
p.reprovider = prov
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
}
}
// 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
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(ctx context.Context, c cid.Cid) (bool, error) {
return p.BlockStore().Has(ctx, c)
}
// Exchange returns the underlying exchange implementation.
func (p *Peer) Exchange() exchange.Interface {
return p.exch
}
// BlockService returns the underlying blockservice implementation.
func (p *Peer) BlockService() blockservice.BlockService {
return p.bserv
}