offline.go 3.16 KB
Newer Older
Jeromy's avatar
Jeromy committed
1 2 3 4 5 6
package offline

import (
	"errors"
	"time"

7
	key "github.com/ipfs/go-ipfs/blocks/key"
8 9 10
	routing "github.com/ipfs/go-ipfs/routing"
	pb "github.com/ipfs/go-ipfs/routing/dht/pb"
	record "github.com/ipfs/go-ipfs/routing/record"
Jakub Sztandera's avatar
Jakub Sztandera committed
11
	ds "gx/ipfs/QmZ6A6P6AMo8SR3jXAwzTuSU6B9R2Y4eqW2yW9VvfUayDN/go-datastore"
Jeromy's avatar
Jeromy committed
12 13

	"gx/ipfs/QmQGwpJy9P4yXZySmqkZEXCmbBpJUb8xntCv8Ca4taZwDC/go-libp2p-peer"
14
	ci "gx/ipfs/QmUEUu1CM8bxBJxc3ZLojAi8evhTr4byQogWstABet79oY/go-libp2p-crypto"
Jeromy's avatar
Jeromy committed
15
	pstore "gx/ipfs/QmXHUpFsnpCmanRnacqYkFoLoFfEq5yS2nUgGkAjJ1Nj9j/go-libp2p-peerstore"
Jakub Sztandera's avatar
Jakub Sztandera committed
16
	logging "gx/ipfs/QmYtB7Qge8cJpXc4irsEp8zRqfnZMBeB7aTrMEkPk67DRv/go-log"
Jeromy's avatar
Jeromy committed
17
	proto "gx/ipfs/QmZ4Qi3GaRbjcx28Sme5eMH7RQjGkt8wHxt2a65oLaeFEV/gogo-protobuf/proto"
Jeromy's avatar
Jeromy committed
18
	context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
Jeromy's avatar
Jeromy committed
19 20
)

Jeromy's avatar
Jeromy committed
21
var log = logging.Logger("offlinerouting")
Jeromy's avatar
Jeromy committed
22 23 24 25 26 27 28 29 30 31

var ErrOffline = errors.New("routing system in offline mode")

func NewOfflineRouter(dstore ds.Datastore, privkey ci.PrivKey) routing.IpfsRouting {
	return &offlineRouting{
		datastore: dstore,
		sk:        privkey,
	}
}

Jeromy's avatar
Jeromy committed
32 33 34
// offlineRouting implements the IpfsRouting interface,
// but only provides the capability to Put and Get signed dht
// records to and from the local datastore.
Jeromy's avatar
Jeromy committed
35 36 37 38 39
type offlineRouting struct {
	datastore ds.Datastore
	sk        ci.PrivKey
}

40
func (c *offlineRouting) PutValue(ctx context.Context, key key.Key, val []byte) error {
41
	rec, err := record.MakePutRecord(c.sk, key, val, false)
Jeromy's avatar
Jeromy committed
42 43 44 45 46 47 48 49 50 51 52
	if err != nil {
		return err
	}
	data, err := proto.Marshal(rec)
	if err != nil {
		return err
	}

	return c.datastore.Put(key.DsKey(), data)
}

53
func (c *offlineRouting) GetValue(ctx context.Context, key key.Key) ([]byte, error) {
Jeromy's avatar
Jeromy committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	v, err := c.datastore.Get(key.DsKey())
	if err != nil {
		return nil, err
	}

	byt, ok := v.([]byte)
	if !ok {
		return nil, errors.New("value stored in datastore not []byte")
	}
	rec := new(pb.Record)
	err = proto.Unmarshal(byt, rec)
	if err != nil {
		return nil, err
	}

	return rec.GetValue(), nil
}

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
func (c *offlineRouting) GetValues(ctx context.Context, key key.Key, _ int) ([]routing.RecvdVal, error) {
	v, err := c.datastore.Get(key.DsKey())
	if err != nil {
		return nil, err
	}

	byt, ok := v.([]byte)
	if !ok {
		return nil, errors.New("value stored in datastore not []byte")
	}
	rec := new(pb.Record)
	err = proto.Unmarshal(byt, rec)
	if err != nil {
		return nil, err
	}

	return []routing.RecvdVal{
		{Val: rec.GetValue()},
	}, nil
}

Jeromy's avatar
Jeromy committed
93
func (c *offlineRouting) FindProviders(ctx context.Context, key key.Key) ([]pstore.PeerInfo, error) {
Jeromy's avatar
Jeromy committed
94 95 96
	return nil, ErrOffline
}

Jeromy's avatar
Jeromy committed
97 98
func (c *offlineRouting) FindPeer(ctx context.Context, pid peer.ID) (pstore.PeerInfo, error) {
	return pstore.PeerInfo{}, ErrOffline
Jeromy's avatar
Jeromy committed
99 100
}

Jeromy's avatar
Jeromy committed
101 102
func (c *offlineRouting) FindProvidersAsync(ctx context.Context, k key.Key, max int) <-chan pstore.PeerInfo {
	out := make(chan pstore.PeerInfo)
Jeromy's avatar
Jeromy committed
103 104 105 106
	close(out)
	return out
}

107
func (c *offlineRouting) Provide(_ context.Context, key key.Key) error {
Jeromy's avatar
Jeromy committed
108 109 110 111 112 113 114
	return ErrOffline
}

func (c *offlineRouting) Ping(ctx context.Context, p peer.ID) (time.Duration, error) {
	return 0, ErrOffline
}

Jeromy's avatar
Jeromy committed
115
func (c *offlineRouting) Bootstrap(context.Context) error {
116 117 118
	return nil
}

Jeromy's avatar
Jeromy committed
119
// ensure offlineRouting matches the IpfsRouting interface
Jeromy's avatar
Jeromy committed
120
var _ routing.IpfsRouting = &offlineRouting{}