forked from ipfs/boxo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockstore.go
267 lines (222 loc) · 6.76 KB
/
blockstore.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
package gateway
import (
"context"
"errors"
"fmt"
"io"
"math/rand"
"net/http"
"time"
"github.com/ipfs/go-cid"
format "github.com/ipfs/go-ipld-format"
blocks "github.com/ipfs/go-block-format"
blockstore "github.com/littlespeechless/boxo/blockstore"
"github.com/littlespeechless/boxo/util"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/prometheus/client_golang/prometheus"
uatomic "go.uber.org/atomic"
"go.uber.org/zap/zapcore"
)
type cacheBlockStore struct {
cache *lru.TwoQueueCache[string, []byte]
rehash *uatomic.Bool
cacheHitsMetric prometheus.Counter
cacheRequestsMetric prometheus.Counter
}
var _ blockstore.Blockstore = (*cacheBlockStore)(nil)
// NewCacheBlockStore creates a new [blockstore.Blockstore] that caches blocks
// in memory using a two queue cache. It can be useful, for example, when paired
// with a proxy blockstore (see [NewRemoteBlockstore]).
//
// If the given [prometheus.Registerer] is nil, a new one will be created using
// [prometheus.NewRegistry].
func NewCacheBlockStore(size int, reg prometheus.Registerer) (blockstore.Blockstore, error) {
c, err := lru.New2Q[string, []byte](size)
if err != nil {
return nil, err
}
if reg == nil {
reg = prometheus.NewRegistry()
}
cacheHitsMetric := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "ipfs",
Subsystem: "http",
Name: "blockstore_cache_hit",
Help: "The number of global block cache hits.",
})
cacheRequestsMetric := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "ipfs",
Subsystem: "http",
Name: "blockstore_cache_requests",
Help: "The number of global block cache requests.",
})
err = reg.Register(cacheHitsMetric)
if err != nil {
return nil, err
}
err = reg.Register(cacheRequestsMetric)
if err != nil {
return nil, err
}
return &cacheBlockStore{
cache: c,
rehash: uatomic.NewBool(false),
cacheHitsMetric: cacheHitsMetric,
cacheRequestsMetric: cacheRequestsMetric,
}, nil
}
func (l *cacheBlockStore) DeleteBlock(ctx context.Context, c cid.Cid) error {
l.cache.Remove(string(c.Hash()))
return nil
}
func (l *cacheBlockStore) Has(ctx context.Context, c cid.Cid) (bool, error) {
return l.cache.Contains(string(c.Hash())), nil
}
func (l *cacheBlockStore) Get(ctx context.Context, c cid.Cid) (blocks.Block, error) {
l.cacheRequestsMetric.Add(1)
blkData, found := l.cache.Get(string(c.Hash()))
if !found {
if log.Level().Enabled(zapcore.DebugLevel) {
log.Debugw("block not found in cache", "cid", c.String())
}
return nil, format.ErrNotFound{Cid: c}
}
// It's a HIT!
l.cacheHitsMetric.Add(1)
if log.Level().Enabled(zapcore.DebugLevel) {
log.Debugw("block found in cache", "cid", c.String())
}
if l.rehash.Load() {
rbcid, err := c.Prefix().Sum(blkData)
if err != nil {
return nil, err
}
if !rbcid.Equals(c) {
return nil, blockstore.ErrHashMismatch
}
}
return blocks.NewBlockWithCid(blkData, c)
}
func (l *cacheBlockStore) GetSize(ctx context.Context, c cid.Cid) (int, error) {
blkData, found := l.cache.Get(string(c.Hash()))
if !found {
return -1, format.ErrNotFound{Cid: c}
}
return len(blkData), nil
}
func (l *cacheBlockStore) Put(ctx context.Context, blk blocks.Block) error {
l.cache.Add(string(blk.Cid().Hash()), blk.RawData())
return nil
}
func (l *cacheBlockStore) PutMany(ctx context.Context, blks []blocks.Block) error {
for _, b := range blks {
if err := l.Put(ctx, b); err != nil {
return err
}
}
return nil
}
func (l *cacheBlockStore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
return nil, errors.New("not implemented")
}
func (l *cacheBlockStore) HashOnRead(enabled bool) {
l.rehash.Store(enabled)
}
type remoteBlockstore struct {
httpClient *http.Client
gatewayURL []string
rand *rand.Rand
validate bool
}
// NewRemoteBlockstore creates a new [blockstore.Blockstore] that is backed by one
// or more gateways that support [RAW block] requests. See the [Trustless Gateway]
// specification for more details. You can optionally pass your own [http.Client].
//
// [Trustless Gateway]: https://specs.ipfs.tech/http-gateways/trustless-gateway/
// [RAW block]: https://www.iana.org/assignments/media-types/application/vnd.ipld.raw
func NewRemoteBlockstore(gatewayURL []string, httpClient *http.Client) (blockstore.Blockstore, error) {
if len(gatewayURL) == 0 {
return nil, errors.New("missing remote block backend URL")
}
if httpClient == nil {
httpClient = newRemoteHTTPClient()
}
return &remoteBlockstore{
gatewayURL: gatewayURL,
httpClient: httpClient,
rand: rand.New(rand.NewSource(time.Now().Unix())),
// Enables block validation by default. Important since we are
// proxying block requests to untrusted gateways.
validate: true,
}, nil
}
func (ps *remoteBlockstore) fetch(ctx context.Context, c cid.Cid) (blocks.Block, error) {
urlStr := fmt.Sprintf("%s/ipfs/%s?format=raw", ps.getRandomGatewayURL(), c)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, urlStr, nil)
if err != nil {
return nil, err
}
log.Debugw("raw fetch", "url", req.URL)
req.Header.Set("Accept", "application/vnd.ipld.raw")
resp, err := ps.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http error from remote block backend: %s", resp.Status)
}
rb, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if ps.validate {
nc, err := c.Prefix().Sum(rb)
if err != nil {
return nil, blocks.ErrWrongHash
}
if !nc.Equals(c) {
return nil, blocks.ErrWrongHash
}
}
return blocks.NewBlockWithCid(rb, c)
}
func (ps *remoteBlockstore) Has(ctx context.Context, c cid.Cid) (bool, error) {
blk, err := ps.fetch(ctx, c)
if err != nil {
return false, err
}
return blk != nil, nil
}
func (ps *remoteBlockstore) Get(ctx context.Context, c cid.Cid) (blocks.Block, error) {
blk, err := ps.fetch(ctx, c)
if err != nil {
return nil, err
}
return blk, nil
}
func (ps *remoteBlockstore) GetSize(ctx context.Context, c cid.Cid) (int, error) {
blk, err := ps.fetch(ctx, c)
if err != nil {
return 0, err
}
return len(blk.RawData()), nil
}
func (ps *remoteBlockstore) HashOnRead(enabled bool) {
ps.validate = enabled
}
func (c *remoteBlockstore) Put(context.Context, blocks.Block) error {
return util.ErrNotImplemented
}
func (c *remoteBlockstore) PutMany(context.Context, []blocks.Block) error {
return util.ErrNotImplemented
}
func (c *remoteBlockstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
return nil, util.ErrNotImplemented
}
func (c *remoteBlockstore) DeleteBlock(context.Context, cid.Cid) error {
return util.ErrNotImplemented
}
func (ps *remoteBlockstore) getRandomGatewayURL() string {
return ps.gatewayURL[ps.rand.Intn(len(ps.gatewayURL))]
}