-
Notifications
You must be signed in to change notification settings - Fork 9
/
bitswap.go
201 lines (172 loc) · 5.32 KB
/
bitswap.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
package utils
import (
"context"
"os"
"path/filepath"
"time"
bs "github.com/ipfs/go-bitswap"
bsnet "github.com/ipfs/go-bitswap/network"
"github.com/ipfs/go-blockservice"
"github.com/ipfs/go-cid"
ds "github.com/ipfs/go-datastore"
delayed "github.com/ipfs/go-datastore/delayed"
ds_sync "github.com/ipfs/go-datastore/sync"
badgerds "github.com/ipfs/go-ds-badger2"
blockstore "github.com/ipfs/go-ipfs-blockstore"
delay "github.com/ipfs/go-ipfs-delay"
files "github.com/ipfs/go-ipfs-files"
nilrouting "github.com/ipfs/go-ipfs-routing/none"
ipld "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-merkledag"
unixfile "github.com/ipfs/go-unixfs/file"
"github.com/ipfs/go-unixfs/importer/helpers"
"github.com/libp2p/go-libp2p-core/host"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
dgbadger "github.com/dgraph-io/badger/v2"
)
type NodeType int
const (
// Seeds data
Seed NodeType = iota
// Fetches data from seeds
Leech
// Doesn't seed or fetch data
Passive
)
func (nt NodeType) String() string {
return [...]string{"Seed", "Leech", "Passive"}[nt]
}
// Adapted from the netflix/p2plab repo under an Apache-2 license.
// Original source code located at https://github.com/Netflix/p2plab/blob/master/peer/peer.go
type BitswapNode struct {
bitswap *bs.Bitswap
blockStore blockstore.Blockstore
dserv ipld.DAGService
h host.Host
}
func (n *BitswapNode) Close() error {
return n.bitswap.Close()
}
func CreateBlockstore(ctx context.Context, dStore ds.Batching) (blockstore.Blockstore, error) {
return blockstore.CachedBlockstore(ctx,
blockstore.NewBlockstore(dStore),
blockstore.DefaultCacheOpts())
}
// CreateDatastore creates a data store to use for the transfer.
// If diskStore=false, it returns an in-memory store that uses the given delay for each read/write.
// If diskStore=true, it returns a Badger data store and ignores the bsdelay param.
func CreateDatastore(diskStore bool, bsdelay time.Duration) (ds.Batching, error) {
if !diskStore {
dstore := ds_sync.MutexWrap(delayed.New(ds.NewMapDatastore(), delay.Fixed(bsdelay)))
return dstore, nil
}
// create temporary directory for badger datastore
path := filepath.Join(os.TempDir(), "datastore")
if _, err := os.Stat(path); os.IsNotExist(err) {
if err := os.MkdirAll(path, 0755); err != nil {
return nil, err
}
} else if err != nil {
return nil, err
}
// create disk based badger datastore
defopts := badgerds.DefaultOptions
defopts.Options = dgbadger.DefaultOptions("").WithTruncate(true).
WithValueThreshold(1 << 10)
datastore, err := badgerds.NewDatastore(path, &defopts)
if err != nil {
return nil, err
}
return datastore, nil
}
func ClearBlockstore(ctx context.Context, bstore blockstore.Blockstore) error {
ks, err := bstore.AllKeysChan(ctx)
if err != nil {
return err
}
g := errgroup.Group{}
for k := range ks {
c := k
g.Go(func() error {
return bstore.DeleteBlock(c)
})
}
return g.Wait()
}
func CreateBitswapNode(ctx context.Context, h host.Host, bstore blockstore.Blockstore) (*BitswapNode, error) {
routing, err := nilrouting.ConstructNilRouting(ctx, nil, nil, nil)
if err != nil {
return nil, err
}
net := bsnet.NewFromIpfsHost(h, routing)
bitswap := bs.New(ctx, net, bstore).(*bs.Bitswap)
bserv := blockservice.New(bstore, bitswap)
dserv := merkledag.NewDAGService(bserv)
return &BitswapNode{bitswap, bstore, dserv, h}, nil
}
func (n *BitswapNode) Add(ctx context.Context, fileNode files.Node) (cid.Cid, error) {
settings := AddSettings{
Layout: "balanced",
Chunker: "size-262144",
RawLeaves: false,
NoCopy: false,
HashFunc: "sha2-256",
MaxLinks: helpers.DefaultLinksPerBlock,
}
adder, err := NewDAGAdder(ctx, n.dserv, settings)
if err != nil {
return cid.Undef, err
}
ipldNode, err := adder.Add(fileNode)
if err != nil {
return cid.Undef, err
}
return ipldNode.Cid(), nil
}
func (n *BitswapNode) ClearDatastore(ctx context.Context, _ cid.Cid) error {
return ClearBlockstore(ctx, n.blockStore)
}
func (n *BitswapNode) EmitMetrics(recorder MetricsRecorder) error {
stats, err := n.bitswap.Stat()
if err != nil {
return err
}
recorder.Record("msgs_rcvd", float64(stats.MessagesReceived))
recorder.Record("data_sent", float64(stats.DataSent))
recorder.Record("data_rcvd", float64(stats.DataReceived))
recorder.Record("block_data_rcvd", float64(stats.BlockDataReceived))
recorder.Record("dup_data_rcvd", float64(stats.DupDataReceived))
recorder.Record("blks_sent", float64(stats.BlocksSent))
recorder.Record("blks_rcvd", float64(stats.BlocksReceived))
recorder.Record("dup_blks_rcvd", float64(stats.DupBlksReceived))
return err
}
func (n *BitswapNode) Fetch(ctx context.Context, c cid.Cid, _ []PeerInfo) (files.Node, error) {
err := merkledag.FetchGraph(ctx, c, n.dserv)
if err != nil {
return nil, err
}
nd, err := n.dserv.Get(ctx, c)
if err != nil {
return nil, errors.Wrapf(err, "failed to get file %q", c)
}
return unixfile.NewUnixfsFile(ctx, n.dserv, nd)
}
func (n *BitswapNode) DAGService() ipld.DAGService {
return n.dserv
}
func (n *BitswapNode) Host() host.Host {
return n.h
}
func (n *BitswapNode) EmitKeepAlive(recorder MessageRecorder) error {
stats, err := n.bitswap.Stat()
if err != nil {
return err
}
recorder.RecordMessage("I am still alive! Total In: %d - TotalOut: %d",
stats.DataReceived,
stats.DataSent)
return nil
}
var _ Node = &BitswapNode{}