mirror of
https://github.com/ipfs/kubo.git
synced 2025-09-09 23:42:20 +08:00

This commit adds a sequence number to the IpnsEntry protobuf that is used to determine which among a set of entries for the same key is the 'most correct'. GetValues has been added to the routing interface to retrieve a set of records from the dht, for the caller to select from. GetValue (singular) will call GetValues, select the 'best' record, and then update that record to peers we received outdated records from. This will help keep the dht consistent. License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
108 lines
2.7 KiB
Go
108 lines
2.7 KiB
Go
package mockrouting
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
|
|
ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
|
|
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
|
|
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
|
|
key "github.com/ipfs/go-ipfs/blocks/key"
|
|
peer "github.com/ipfs/go-ipfs/p2p/peer"
|
|
routing "github.com/ipfs/go-ipfs/routing"
|
|
"github.com/ipfs/go-ipfs/util/testutil"
|
|
logging "github.com/ipfs/go-ipfs/vendor/go-log-v1.0.0"
|
|
)
|
|
|
|
var log = logging.Logger("mockrouter")
|
|
|
|
type client struct {
|
|
datastore ds.Datastore
|
|
server server
|
|
peer testutil.Identity
|
|
}
|
|
|
|
// FIXME(brian): is this method meant to simulate putting a value into the network?
|
|
func (c *client) PutValue(ctx context.Context, key key.Key, val []byte) error {
|
|
log.Debugf("PutValue: %s", key)
|
|
return c.datastore.Put(key.DsKey(), val)
|
|
}
|
|
|
|
// FIXME(brian): is this method meant to simulate getting a value from the network?
|
|
func (c *client) GetValue(ctx context.Context, key key.Key) ([]byte, error) {
|
|
log.Debugf("GetValue: %s", key)
|
|
v, err := c.datastore.Get(key.DsKey())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
data, ok := v.([]byte)
|
|
if !ok {
|
|
return nil, errors.New("could not cast value from datastore")
|
|
}
|
|
|
|
return data, nil
|
|
}
|
|
|
|
func (c *client) GetValues(ctx context.Context, key key.Key, count int) ([]routing.RecvdVal, error) {
|
|
log.Debugf("GetValue: %s", key)
|
|
v, err := c.datastore.Get(key.DsKey())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
data, ok := v.([]byte)
|
|
if !ok {
|
|
return nil, errors.New("could not cast value from datastore")
|
|
}
|
|
|
|
return []routing.RecvdVal{{Val: data, From: c.peer.ID()}}, nil
|
|
}
|
|
|
|
func (c *client) FindProviders(ctx context.Context, key key.Key) ([]peer.PeerInfo, error) {
|
|
return c.server.Providers(key), nil
|
|
}
|
|
|
|
func (c *client) FindPeer(ctx context.Context, pid peer.ID) (peer.PeerInfo, error) {
|
|
log.Debugf("FindPeer: %s", pid)
|
|
return peer.PeerInfo{}, nil
|
|
}
|
|
|
|
func (c *client) FindProvidersAsync(ctx context.Context, k key.Key, max int) <-chan peer.PeerInfo {
|
|
out := make(chan peer.PeerInfo)
|
|
go func() {
|
|
defer close(out)
|
|
for i, p := range c.server.Providers(k) {
|
|
if max <= i {
|
|
return
|
|
}
|
|
select {
|
|
case out <- p:
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
return out
|
|
}
|
|
|
|
// Provide returns once the message is on the network. Value is not necessarily
|
|
// visible yet.
|
|
func (c *client) Provide(_ context.Context, key key.Key) error {
|
|
info := peer.PeerInfo{
|
|
ID: c.peer.ID(),
|
|
Addrs: []ma.Multiaddr{c.peer.Address()},
|
|
}
|
|
return c.server.Announce(info, key)
|
|
}
|
|
|
|
func (c *client) Ping(ctx context.Context, p peer.ID) (time.Duration, error) {
|
|
return 0, nil
|
|
}
|
|
|
|
func (c *client) Bootstrap(context.Context) error {
|
|
return nil
|
|
}
|
|
|
|
var _ routing.IpfsRouting = &client{}
|