/
bitswap.go
153 lines (135 loc) · 4.34 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
package net
import (
"context"
"github.com/data-preservation-programs/RetrievalBot/pkg/task"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
blockstore "github.com/ipfs/go-ipfs-blockstore"
bsclient "github.com/ipfs/go-libipfs/bitswap/client"
bsmsg "github.com/ipfs/go-libipfs/bitswap/message"
bsnet "github.com/ipfs/go-libipfs/bitswap/network"
"github.com/ipfs/go-libipfs/blocks"
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/pkg/errors"
"golang.org/x/exp/slices"
"time"
)
type SingleContentRouter struct {
AddrInfo peer.AddrInfo
}
func (s SingleContentRouter) PutValue(context.Context, string, []byte, ...routing.Option) error {
return routing.ErrNotSupported
}
func (s SingleContentRouter) GetValue(context.Context, string, ...routing.Option) ([]byte, error) {
return nil, routing.ErrNotFound
}
func (s SingleContentRouter) SearchValue(ctx context.Context, key string, opts ...routing.Option) (
<-chan []byte, error) {
return nil, routing.ErrNotFound
}
func (s SingleContentRouter) Provide(context.Context, cid.Cid, bool) error {
return routing.ErrNotSupported
}
func (s SingleContentRouter) FindProvidersAsync(context.Context, cid.Cid, int) <-chan peer.AddrInfo {
ch := make(chan peer.AddrInfo)
go func() {
ch <- s.AddrInfo
close(ch)
}()
return ch
}
func (s SingleContentRouter) FindPeer(context.Context, peer.ID) (peer.AddrInfo, error) {
return peer.AddrInfo{}, routing.ErrNotFound
}
func (s SingleContentRouter) Bootstrap(context.Context) error {
return nil
}
func (s SingleContentRouter) Close() error {
return nil
}
type MessageReceiver struct {
BSClient *bsclient.Client
MessageHandler func(ctx context.Context, sender peer.ID, incoming bsmsg.BitSwapMessage)
}
func (m MessageReceiver) ReceiveMessage(
ctx context.Context,
sender peer.ID,
incoming bsmsg.BitSwapMessage) {
m.BSClient.ReceiveMessage(ctx, sender, incoming)
m.MessageHandler(ctx, sender, incoming)
}
func (m MessageReceiver) ReceiveError(err error) {
m.BSClient.ReceiveError(err)
}
func (m MessageReceiver) PeerConnected(id peer.ID) {
m.BSClient.PeerConnected(id)
}
func (m MessageReceiver) PeerDisconnected(id peer.ID) {
m.BSClient.PeerDisconnected(id)
}
type BitswapClient struct {
host host.Host
timeout time.Duration
}
func NewBitswapClient(host host.Host, timeout time.Duration) BitswapClient {
return BitswapClient{
host: host,
timeout: timeout,
}
}
func (c BitswapClient) Retrieve(
parent context.Context,
target peer.AddrInfo,
cid cid.Cid) (*task.RetrievalResult, error) {
logger := logging.Logger("bitswap_client").With("cid", cid).With("target", target)
network := bsnet.NewFromIpfsHost(c.host, SingleContentRouter{
AddrInfo: target,
})
bswap := bsclient.New(parent, network, blockstore.NewBlockstore(datastore.NewMapDatastore()))
notFound := make(chan struct{})
network.Start(MessageReceiver{BSClient: bswap, MessageHandler: func(
ctx context.Context, sender peer.ID, incoming bsmsg.BitSwapMessage) {
if sender == target.ID && slices.Contains(incoming.DontHaves(), cid) {
logger.Info("Block not found")
close(notFound)
}
}})
defer bswap.Close()
defer network.Stop()
connectContext, cancel := context.WithTimeout(parent, c.timeout)
defer cancel()
logger.Info("Connecting to target peer...")
err := c.host.Connect(connectContext, target)
if err != nil {
logger.With("err", err).Info("Failed to connect to target peer")
return task.NewErrorRetrievalResultWithErrorResolution(task.CannotConnect, err), nil
}
startTime := time.Now()
resultChan := make(chan blocks.Block)
errChan := make(chan error)
go func() {
logger.Info("Retrieving block...")
blk, err := bswap.GetBlock(connectContext, cid)
if err != nil {
logger.Info(err)
errChan <- err
} else {
resultChan <- blk
}
}()
select {
case <-notFound:
return task.NewErrorRetrievalResult(
task.NotFound, errors.New("DONT_HAVE received from the target peer")), nil
case blk := <-resultChan:
elapsed := time.Since(startTime)
var size = int64(len(blk.RawData()))
logger.With("size", size).With("elapsed", elapsed).Info("Retrieved block")
return task.NewSuccessfulRetrievalResult(elapsed, size, elapsed), nil
case err := <-errChan:
return task.NewErrorRetrievalResultWithErrorResolution(task.RetrievalFailure, err), nil
}
}