forked from cerc-io/ipld-eth-server
91 lines
2.3 KiB
Go
91 lines
2.3 KiB
Go
package mockrouting
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
cid "github.com/ipfs/go-cid"
|
|
logging "github.com/ipfs/go-log"
|
|
peer "github.com/libp2p/go-libp2p-peer"
|
|
pstore "github.com/libp2p/go-libp2p-peerstore"
|
|
routing "github.com/libp2p/go-libp2p-routing"
|
|
ropts "github.com/libp2p/go-libp2p-routing/options"
|
|
"github.com/libp2p/go-testutil"
|
|
ma "github.com/multiformats/go-multiaddr"
|
|
)
|
|
|
|
var log = logging.Logger("mockrouter")
|
|
|
|
type client struct {
|
|
vs routing.ValueStore
|
|
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 string, val []byte, opts ...ropts.Option) error {
|
|
log.Debugf("PutValue: %s", key)
|
|
return c.vs.PutValue(ctx, key, val, opts...)
|
|
}
|
|
|
|
// FIXME(brian): is this method meant to simulate getting a value from the network?
|
|
func (c *client) GetValue(ctx context.Context, key string, opts ...ropts.Option) ([]byte, error) {
|
|
log.Debugf("GetValue: %s", key)
|
|
return c.vs.GetValue(ctx, key, opts...)
|
|
}
|
|
|
|
func (c *client) SearchValue(ctx context.Context, key string, opts ...ropts.Option) (<-chan []byte, error) {
|
|
log.Debugf("SearchValue: %s", key)
|
|
return c.vs.SearchValue(ctx, key, opts...)
|
|
}
|
|
|
|
func (c *client) FindProviders(ctx context.Context, key cid.Cid) ([]pstore.PeerInfo, error) {
|
|
return c.server.Providers(key), nil
|
|
}
|
|
|
|
func (c *client) FindPeer(ctx context.Context, pid peer.ID) (pstore.PeerInfo, error) {
|
|
log.Debugf("FindPeer: %s", pid)
|
|
return pstore.PeerInfo{}, nil
|
|
}
|
|
|
|
func (c *client) FindProvidersAsync(ctx context.Context, k cid.Cid, max int) <-chan pstore.PeerInfo {
|
|
out := make(chan pstore.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 cid.Cid, brd bool) error {
|
|
if !brd {
|
|
return nil
|
|
}
|
|
info := pstore.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{}
|