This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 20
/
blockstore.go
67 lines (53 loc) · 1.46 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
package main
import (
"context"
"errors"
"time"
"github.com/ipfs/bifrost-gateway/lib"
blockstore "github.com/ipfs/boxo/blockstore"
exchange "github.com/ipfs/boxo/exchange"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"go.uber.org/zap/zapcore"
)
var errNotImplemented = errors.New("not implemented")
const GetBlockTimeout = time.Second * 60
func newExchange(bs blockstore.Blockstore) (exchange.Interface, error) {
return &exchangeBsWrapper{bstore: bs}, nil
}
type exchangeBsWrapper struct {
bstore blockstore.Blockstore
}
func (e *exchangeBsWrapper) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, error) {
ctx, cancel := context.WithTimeout(ctx, GetBlockTimeout)
defer cancel()
if goLog.Level().Enabled(zapcore.DebugLevel) {
goLog.Debugw("block requested from remote blockstore", "cid", c.String())
}
blk, err := e.bstore.Get(ctx, c)
if err != nil {
return nil, lib.GatewayError(err)
}
return blk, nil
}
func (e *exchangeBsWrapper) GetBlocks(ctx context.Context, cids []cid.Cid) (<-chan blocks.Block, error) {
out := make(chan blocks.Block)
go func() {
defer close(out)
for _, c := range cids {
blk, err := e.GetBlock(ctx, c)
if err != nil {
return
}
out <- blk
}
}()
return out, nil
}
func (e *exchangeBsWrapper) NotifyNewBlocks(ctx context.Context, blks ...blocks.Block) error {
return nil
}
func (e *exchangeBsWrapper) Close() error {
return nil
}
var _ exchange.Interface = (*exchangeBsWrapper)(nil)