This repository has been archived by the owner on Apr 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
offline.go
128 lines (111 loc) · 3.7 KB
/
offline.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
// Package offline implements IpfsRouting with a client which
// is only able to perform offline operations.
package offline
import (
"bytes"
"context"
"errors"
"time"
proto "github.com/gogo/protobuf/proto"
cid "github.com/ipsn/go-ipfs/gxlibs/github.com/ipfs/go-cid"
ds "github.com/ipsn/go-ipfs/gxlibs/github.com/ipfs/go-datastore"
dshelp "github.com/ipsn/go-ipfs/gxlibs/github.com/ipfs/go-ipfs-ds-help"
"github.com/ipsn/go-ipfs/gxlibs/github.com/libp2p/go-libp2p-peer"
pstore "github.com/ipsn/go-ipfs/gxlibs/github.com/libp2p/go-libp2p-peerstore"
record "github.com/ipsn/go-ipfs/gxlibs/github.com/libp2p/go-libp2p-record"
pb "github.com/ipsn/go-ipfs/gxlibs/github.com/libp2p/go-libp2p-record/pb"
routing "github.com/ipsn/go-ipfs/gxlibs/github.com/libp2p/go-libp2p-routing"
ropts "github.com/ipsn/go-ipfs/gxlibs/github.com/libp2p/go-libp2p-routing/options"
)
// ErrOffline is returned when trying to perform operations that
// require connectivity.
var ErrOffline = errors.New("routing system in offline mode")
// NewOfflineRouter returns an IpfsRouting implementation which only performs
// offline operations. It allows to Put and Get signed dht
// records to and from the local datastore.
func NewOfflineRouter(dstore ds.Datastore, validator record.Validator) routing.IpfsRouting {
return &offlineRouting{
datastore: dstore,
validator: validator,
}
}
// offlineRouting implements the IpfsRouting interface,
// but only provides the capability to Put and Get signed dht
// records to and from the local datastore.
type offlineRouting struct {
datastore ds.Datastore
validator record.Validator
}
func (c *offlineRouting) PutValue(ctx context.Context, key string, val []byte, _ ...ropts.Option) error {
if err := c.validator.Validate(key, val); err != nil {
return err
}
if old, err := c.GetValue(ctx, key); err == nil {
// be idempotent to be nice.
if bytes.Equal(old, val) {
return nil
}
// check to see if the older record is better
i, err := c.validator.Select(key, [][]byte{val, old})
if err != nil {
// this shouldn't happen for validated records.
return err
}
if i != 0 {
return errors.New("can't replace a newer record with an older one")
}
}
rec := record.MakePutRecord(key, val)
data, err := proto.Marshal(rec)
if err != nil {
return err
}
return c.datastore.Put(dshelp.NewKeyFromBinary([]byte(key)), data)
}
func (c *offlineRouting) GetValue(ctx context.Context, key string, _ ...ropts.Option) ([]byte, error) {
buf, err := c.datastore.Get(dshelp.NewKeyFromBinary([]byte(key)))
if err != nil {
return nil, err
}
rec := new(pb.Record)
err = proto.Unmarshal(buf, rec)
if err != nil {
return nil, err
}
val := rec.GetValue()
err = c.validator.Validate(key, val)
if err != nil {
return nil, err
}
return val, nil
}
func (c *offlineRouting) SearchValue(ctx context.Context, key string, _ ...ropts.Option) (<-chan []byte, error) {
out := make(chan []byte, 1)
go func() {
defer close(out)
v, err := c.GetValue(ctx, key)
if err == nil {
out <- v
}
}()
return out, nil
}
func (c *offlineRouting) FindPeer(ctx context.Context, pid peer.ID) (pstore.PeerInfo, error) {
return pstore.PeerInfo{}, ErrOffline
}
func (c *offlineRouting) FindProvidersAsync(ctx context.Context, k cid.Cid, max int) <-chan pstore.PeerInfo {
out := make(chan pstore.PeerInfo)
close(out)
return out
}
func (c *offlineRouting) Provide(_ context.Context, k cid.Cid, _ bool) error {
return ErrOffline
}
func (c *offlineRouting) Ping(ctx context.Context, p peer.ID) (time.Duration, error) {
return 0, ErrOffline
}
func (c *offlineRouting) Bootstrap(context.Context) error {
return nil
}
// ensure offlineRouting matches the IpfsRouting interface
var _ routing.IpfsRouting = &offlineRouting{}