Commit 41fb3cfc authored by tavit ohanian's avatar tavit ohanian

Merge branch 'port-2021-06-23'

parents 0ab2f099 40e5b433
Pipeline #740 failed with stages
in 6 seconds
blank_issues_enabled: false
contact_links:
- name: Getting Help on IPFS
url: https://ipfs.io/help
about: All information about how and where to get help on IPFS.
- name: IPFS Official Forum
url: https://discuss.ipfs.io
about: Please post general questions, support requests, and discussions here.
---
name: Open an issue
about: Only for actionable issues relevant to this repository.
title: ''
labels: need/triage
assignees: ''
---
<!--
Hello! To ensure this issue is correctly addressed as soon as possible by the IPFS team, please try to make sure:
- This issue is relevant to this repository's topic or codebase.
- A clear description is provided. It should includes as much relevant information as possible and clear scope for the issue to be actionable.
FOR GENERAL DISCUSSION, HELP OR QUESTIONS, please see the options at https://ipfs.io/help or head directly to https://discuss.ipfs.io.
(you can delete this section after reading)
-->
# Configuration for welcome - https://github.com/behaviorbot/welcome
# Configuration for new-issue-welcome - https://github.com/behaviorbot/new-issue-welcome
# Comment to be posted to on first time issues
newIssueWelcomeComment: >
Thank you for submitting your first issue to this repository! A maintainer
will be here shortly to triage and review.
In the meantime, please double-check that you have provided all the
necessary information to make this process easy! Any information that can
help save additional round trips is useful! We currently aim to give
initial feedback within **two business days**. If this does not happen, feel
free to leave a comment.
Please keep an eye on how this issue will be labeled, as labels give an
overview of priorities, assignments and additional actions requested by the
maintainers:
- "Priority" labels will show how urgent this is for the team.
- "Status" labels will show if this is ready to be worked on, blocked, or in progress.
- "Need" labels will indicate if additional input or analysis is required.
Finally, remember to use https://discuss.ipfs.io if you just need general
support.
# Configuration for new-pr-welcome - https://github.com/behaviorbot/new-pr-welcome
# Comment to be posted to on PRs from first time contributors in your repository
newPRWelcomeComment: >
Thank you for submitting this PR!
A maintainer will be here shortly to review it.
We are super grateful, but we are also overloaded! Help us by making sure
that:
* The context for this PR is clear, with relevant discussion, decisions
and stakeholders linked/mentioned.
* Your contribution itself is clear (code comments, self-review for the
rest) and in its best form. Follow the [code contribution
guidelines](https://github.com/ipfs/community/blob/master/CONTRIBUTING.md#code-contribution-guidelines)
if they apply.
Getting other community members to do a review would be great help too on
complex PRs (you can ask in the chats/forums). If you are unsure about
something, just leave us a comment.
Next steps:
* A maintainer will triage and assign priority to this PR, commenting on
any missing things and potentially assigning a reviewer for high
priority items.
* The PR gets reviews, discussed and approvals as needed.
* The PR is merged by maintainers when it has been approved and comments addressed.
We currently aim to provide initial feedback/triaging within **two business
days**. Please keep an eye on any labelling actions, as these will indicate
priorities and status of your contribution.
We are very grateful for your contribution!
# Configuration for first-pr-merge - https://github.com/behaviorbot/first-pr-merge
# Comment to be posted to on pull requests merged by a first time user
# Currently disabled
#firstPRMergeComment: ""
stages:
- build
- test
variables:
BUILD_DIR: "/tmp/$CI_CONCURRENT_PROJECT_ID"
before_script:
- mkdir -p $BUILD_DIR/src
- cd $BUILD_DIR/src
- if [ -d $CI_PROJECT_DIR ]
- then
- echo "soft link $CI_PROJECT_DIR exists"
- else
- echo "creating soft link $CI_PROJECT_DIR"
- ln -s $CI_PROJECT_DIR
- fi
- cd $CI_PROJECT_DIR
build:
stage: build
tags:
- testing
script:
- echo $CI_JOB_STAGE
- go build
test:
stage: test
tags:
- testing
script:
- echo $CI_JOB_STAGE
- go test -cover
coverage: '/coverage: \d+.\d+% of statements/'
0.1.18: QmZ22s3UgNi5vvYNH79jWJ63NPyQGiv4mdNaWCz4WKqMTZ
os:
- linux
language: go
go:
- 1.11.x
env:
global:
- GOTFLAGS="-race"
matrix:
- BUILD_DEPTYPE=gomod
# disable travis install
install:
- true
script:
- bash <(curl -s https://raw.githubusercontent.com/ipfs/ci-helpers/master/travis-ci/run-standard-tests.sh)
cache:
directories:
- $GOPATH/pkg/mod
- $HOME/.cache/go-build
notifications:
email: false
MIT License
Copyright (c) 2018 IPFS
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
all: deps
gx:
go get github.com/whyrusleeping/gx
go get github.com/whyrusleeping/gx-go
deps: gx
gx --verbose install --global
gx-go rewrite
test: deps
gx test -v -race ./...
rw:
gx-go rewrite
rwundo:
gx-go rewrite --undo
publish: rwundo
gx publish
.PHONY: all gx deps test rw rwundo publish
# go-dms3-routing
# go-ipfs-routing
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
[![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/)
[![standard-readme compliant](https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
[![Build Status](https://travis-ci.org/ipfs/go-ipfs-routing.svg?branch=master)](https://travis-ci.org/ipfs/go-ipfs-routing)
> go-ipfs-routing provides go-libp2p-routing implementations used in go-ipfs.
## Table of Contents
- [Install](#install)
- [Usage](#usage)
- [Contribute](#contribute)
- [License](#license)
## Install
`go-ipfs-routing` works like a set of regular Go packages:
```
> go get github.com/ipfs/go-ipfs-routing/...
```
This module uses [Gx](https://github.com/whyrusleeping/gx) to manage
dependencies. You can use `make all` to build it with the `gx` dependencies.
## Usage
This repo contains 3 different packages.
### Mock
[![GoDoc](https://godoc.org/github.com/ipfs/go-ipfs-routing/mock?status.svg)](https://godoc.org/github.com/ipfs/go-ipfs-routing/mock)
```
import "github.com/ipfs/go-ipfs-routing/mock"
```
Mock is a fake router useful for tests. It provides a mock client that
implements the `IpfsRouting` interface and a mock server from which the client
retrieves routing records.
### Offline
[![GoDoc](https://godoc.org/github.com/ipfs/go-ipfs-routing/offline?status.svg)](https://godoc.org/github.com/ipfs/go-ipfs-routing/offline)
```
import "github.com/ipfs/go-ipfs-routing/offline"
```
Offline is an offline router that can put and get records to and from a local
`Datastore` but can't retrieve them from the network.
### None
[![GoDoc](https://godoc.org/github.com/ipfs/go-ipfs-routing/none?status.svg)](https://godoc.org/github.com/ipfs/go-ipfs-routing/none)
```
import "github.com/ipfs/go-ipfs-routing/none"
```
None is a router no-op router that doesn't do anything. Puts always succeed and
lookups always fail.
## Contribute
PRs accepted.
Small note: If editing the README, please conform to the
[standard-readme](https://github.com/RichardLitt/standard-readme) specification.
## License
MIT © Protocol Labs, Inc.
This diff is collapsed.
package mockrouting
import (
"context"
"time"
cid "gitlab.dms3.io/dms3/public/go-cid"
logging "gitlab.dms3.io/dms3/public/go-log"
"gitlab.dms3.io/p2p/go-p2p-core/peer"
"gitlab.dms3.io/p2p/go-p2p-core/routing"
tnet "gitlab.dms3.io/p2p/go-p2p-testing/net"
ma "github.com/multiformats/go-multiaddr"
)
var log = logging.Logger("mockrouter")
type client struct {
vs routing.ValueStore
server server
peer tnet.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 ...routing.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 ...routing.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 ...routing.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) ([]peer.AddrInfo, error) {
return c.server.Providers(key), nil
}
func (c *client) FindPeer(ctx context.Context, pid peer.ID) (peer.AddrInfo, error) {
log.Debugf("FindPeer: %s", pid)
return peer.AddrInfo{}, nil
}
func (c *client) FindProvidersAsync(ctx context.Context, k cid.Cid, max int) <-chan peer.AddrInfo {
out := make(chan peer.AddrInfo)
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 := peer.AddrInfo{
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.Routing = &client{}
package mockrouting
import (
"context"
"math/rand"
"sync"
"time"
cid "gitlab.dms3.io/dms3/public/go-cid"
ds "gitlab.dms3.io/dms3/public/go-datastore"
dssync "gitlab.dms3.io/dms3/public/go-datastore/sync"
"gitlab.dms3.io/p2p/go-p2p-core/peer"
tnet "gitlab.dms3.io/p2p/go-p2p-testing/net"
offline "gitlab.dms3.io/dms3/public/go-dms3-routing/offline"
)
// server is the mockrouting.Client's private interface to the routing server
type server interface {
Announce(peer.AddrInfo, cid.Cid) error
Providers(cid.Cid) []peer.AddrInfo
Server
}
// s is an implementation of the private server interface
type s struct {
delayConf DelayConfig
lock sync.RWMutex
providers map[string]map[peer.ID]providerRecord
}
type providerRecord struct {
Peer peer.AddrInfo
Created time.Time
}
func (rs *s) Announce(p peer.AddrInfo, c cid.Cid) error {
rs.lock.Lock()
defer rs.lock.Unlock()
k := c.KeyString()
_, ok := rs.providers[k]
if !ok {
rs.providers[k] = make(map[peer.ID]providerRecord)
}
rs.providers[k][p.ID] = providerRecord{
Created: time.Now(),
Peer: p,
}
return nil
}
func (rs *s) Providers(c cid.Cid) []peer.AddrInfo {
rs.delayConf.Query.Wait() // before locking
rs.lock.RLock()
defer rs.lock.RUnlock()
k := c.KeyString()
var ret []peer.AddrInfo
records, ok := rs.providers[k]
if !ok {
return ret
}
for _, r := range records {
if time.Since(r.Created) > rs.delayConf.ValueVisibility.Get() {
ret = append(ret, r.Peer)
}
}
for i := range ret {
j := rand.Intn(i + 1)
ret[i], ret[j] = ret[j], ret[i]
}
return ret
}
func (rs *s) Client(p tnet.Identity) Client {
return rs.ClientWithDatastore(context.Background(), p, dssync.MutexWrap(ds.NewMapDatastore()))
}
func (rs *s) ClientWithDatastore(_ context.Context, p tnet.Identity, datastore ds.Datastore) Client {
return &client{
peer: p,
vs: offline.NewOfflineRouter(datastore, MockValidator{}),
server: rs,
}
}
package mockrouting
import (
"context"
"testing"
"time"
cid "gitlab.dms3.io/dms3/public/go-cid"
delay "gitlab.dms3.io/dms3/public/go-dms3-delay"
u "gitlab.dms3.io/dms3/public/go-dms3-util"
"gitlab.dms3.io/p2p/go-p2p-core/peer"
tnet "gitlab.dms3.io/p2p/go-p2p-testing/net"
)
func TestKeyNotFound(t *testing.T) {
var pi = tnet.RandIdentityOrFatal(t)
var key = cid.NewCidV0(u.Hash([]byte("mock key")))
var ctx = context.Background()
rs := NewServer()
providers := rs.Client(pi).FindProvidersAsync(ctx, key, 10)
_, ok := <-providers
if ok {
t.Fatal("should be closed")
}
}
func TestClientFindProviders(t *testing.T) {
pi := tnet.RandIdentityOrFatal(t)
rs := NewServer()
client := rs.Client(pi)
k := cid.NewCidV0(u.Hash([]byte("hello")))
err := client.Provide(context.Background(), k, true)
if err != nil {
t.Fatal(err)
}
// This is bad... but simulating networks is hard
time.Sleep(time.Millisecond * 300)
max := 100
providersFromClient := client.FindProvidersAsync(context.Background(), k, max)
isInClient := false
for pi := range providersFromClient {
if pi.ID == pi.ID { // <-- typo?
isInClient = true
}
}
if !isInClient {
t.Fatal("Despite client providing key, client didn't receive peer when finding providers")
}
}
func TestClientOverMax(t *testing.T) {
rs := NewServer()
k := cid.NewCidV0(u.Hash([]byte("hello")))
numProvidersForHelloKey := 100
for i := 0; i < numProvidersForHelloKey; i++ {
pi := tnet.RandIdentityOrFatal(t)
err := rs.Client(pi).Provide(context.Background(), k, true)
if err != nil {
t.Fatal(err)
}
}
max := 10
pi := tnet.RandIdentityOrFatal(t)
client := rs.Client(pi)
providersFromClient := client.FindProvidersAsync(context.Background(), k, max)
i := 0
for range providersFromClient {
i++
}
if i != max {
t.Fatal("Too many providers returned")
}
}
// TODO does dht ensure won't receive self as a provider? probably not.
func TestCanceledContext(t *testing.T) {
rs := NewServer()
k := cid.NewCidV0(u.Hash([]byte("hello")))
// avoid leaking goroutine, without using the context to signal
// (we want the goroutine to keep trying to publish on a
// cancelled context until we've tested it doesnt do anything.)
done := make(chan struct{})
defer func() { done <- struct{}{} }()
t.Log("async'ly announce infinite stream of providers for key")
i := 0
go func() { // infinite stream
for {
select {
case <-done:
t.Log("exiting async worker")
return
default:
}
pi, err := tnet.RandIdentity()
if err != nil {
t.Error(err)
}
err = rs.Client(pi).Provide(context.Background(), k, true)
if err != nil {
t.Error(err)
}
i++
}
}()
local := tnet.RandIdentityOrFatal(t)
client := rs.Client(local)
t.Log("warning: max is finite so this test is non-deterministic")
t.Log("context cancellation could simply take lower priority")
t.Log("and result in receiving the max number of results")
max := 1000
t.Log("cancel the context before consuming")
ctx, cancelFunc := context.WithCancel(context.Background())
cancelFunc()
providers := client.FindProvidersAsync(ctx, k, max)
numProvidersReturned := 0
for range providers {
numProvidersReturned++
}
t.Log(numProvidersReturned)
if numProvidersReturned == max {
t.Fatal("Context cancel had no effect")
}
}
func TestValidAfter(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
pi := tnet.RandIdentityOrFatal(t)
key := cid.NewCidV0(u.Hash([]byte("mock key")))
conf := DelayConfig{
ValueVisibility: delay.Fixed(1 * time.Hour),
Query: delay.Fixed(0),
}
rs := NewServerWithDelay(conf)
rs.Client(pi).Provide(ctx, key, true)
var providers []peer.AddrInfo
max := 100
providersChan := rs.Client(pi).FindProvidersAsync(ctx, key, max)
for p := range providersChan {
providers = append(providers, p)
}
if len(providers) > 0 {
t.Fail()
}
conf.ValueVisibility.Set(0)
time.Sleep(100 * time.Millisecond)
providersChan = rs.Client(pi).FindProvidersAsync(ctx, key, max)
t.Log("providers", providers)
for p := range providersChan {
providers = append(providers, p)
}
if len(providers) != 1 {
t.Fail()
}
}
// Package mockrouting provides a virtual routing server. To use it,
// create a virtual routing server and use the Client() method to get a
// routing client (Routing). The server quacks like a DHT but is
// really a local in-memory hash table.
package mockrouting
import (
"context"
ds "gitlab.dms3.io/dms3/public/go-datastore"
delay "gitlab.dms3.io/dms3/public/go-dms3-delay"
"gitlab.dms3.io/p2p/go-p2p-core/peer"
"gitlab.dms3.io/p2p/go-p2p-core/routing"
tnet "gitlab.dms3.io/p2p/go-p2p-testing/net"
)
// MockValidator is a record validator that always returns success.
type MockValidator struct{}
func (MockValidator) Validate(_ string, _ []byte) error { return nil }
func (MockValidator) Select(_ string, _ [][]byte) (int, error) { return 0, nil }
// Server provides mockrouting Clients
type Server interface {
Client(p tnet.Identity) Client
ClientWithDatastore(context.Context, tnet.Identity, ds.Datastore) Client
}
// Client implements Routing
type Client interface {
routing.Routing
}
// NewServer returns a mockrouting Server
func NewServer() Server {
return NewServerWithDelay(DelayConfig{
ValueVisibility: delay.Fixed(0),
Query: delay.Fixed(0),
})
}
// NewServerWithDelay returns a mockrouting Server with a delay!
func NewServerWithDelay(conf DelayConfig) Server {
return &s{
providers: make(map[string]map[peer.ID]providerRecord),
delayConf: conf,
}
}
// DelayConfig can be used to configured the fake delays of a mock server.
// Use with NewServerWithDelay().
type DelayConfig struct {
// ValueVisibility is the time it takes for a value to be visible in the network
// FIXME there _must_ be a better term for this
ValueVisibility delay.D
// Query is the time it takes to receive a response from a routing query
Query delay.D
}
// Package nilrouting implements a routing client that does nothing.
package nilrouting
import (
"context"
"errors"
cid "gitlab.dms3.io/dms3/public/go-cid"
ds "gitlab.dms3.io/dms3/public/go-datastore"
"gitlab.dms3.io/p2p/go-p2p-core/host"
"gitlab.dms3.io/p2p/go-p2p-core/peer"
"gitlab.dms3.io/p2p/go-p2p-core/routing"
record "gitlab.dms3.io/p2p/go-p2p-record"
)
type nilclient struct {
}
func (c *nilclient) PutValue(_ context.Context, _ string, _ []byte, _ ...routing.Option) error {
return nil
}
func (c *nilclient) GetValue(_ context.Context, _ string, _ ...routing.Option) ([]byte, error) {
return nil, errors.New("tried GetValue from nil routing")
}
func (c *nilclient) SearchValue(_ context.Context, _ string, _ ...routing.Option) (<-chan []byte, error) {
return nil, errors.New("tried SearchValue from nil routing")
}
func (c *nilclient) FindPeer(_ context.Context, _ peer.ID) (peer.AddrInfo, error) {
return peer.AddrInfo{}, nil
}
func (c *nilclient) FindProvidersAsync(_ context.Context, _ cid.Cid, _ int) <-chan peer.AddrInfo {
out := make(chan peer.AddrInfo)
defer close(out)
return out
}
func (c *nilclient) Provide(_ context.Context, _ cid.Cid, _ bool) error {
return nil
}
func (c *nilclient) Bootstrap(_ context.Context) error {
return nil
}
// ConstructNilRouting creates an Routing client which does nothing.
func ConstructNilRouting(_ context.Context, _ host.Host, _ ds.Batching, _ record.Validator) (routing.Routing, error) {
return &nilclient{}, nil
}
// ensure nilclient satisfies interface
var _ routing.Routing = &nilclient{}
// Package offline implements Routing 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 "gitlab.dms3.io/dms3/public/go-cid"
ds "gitlab.dms3.io/dms3/public/go-datastore"
dshelp "gitlab.dms3.io/dms3/public/go-dms3-ds-help"
"gitlab.dms3.io/p2p/go-p2p-core/peer"
"gitlab.dms3.io/p2p/go-p2p-core/routing"
record "gitlab.dms3.io/p2p/go-p2p-record"
pb "gitlab.dms3.io/p2p/go-p2p-record/pb"
)
// ErrOffline is returned when trying to perform operations that
// require connectivity.
var ErrOffline = errors.New("routing system in offline mode")
// NewOfflineRouter returns an Routing 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.Routing {
return &offlineRouting{
datastore: dstore,
validator: validator,
}
}
// offlineRouting implements the Routing 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, _ ...routing.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, _ ...routing.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, _ ...routing.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) (peer.AddrInfo, error) {
return peer.AddrInfo{}, ErrOffline
}
func (c *offlineRouting) FindProvidersAsync(ctx context.Context, k cid.Cid, max int) <-chan peer.AddrInfo {
out := make(chan peer.AddrInfo)
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 Routing interface
var _ routing.Routing = &offlineRouting{}
package offline
import (
"bytes"
"context"
"testing"
cid "gitlab.dms3.io/dms3/public/go-cid"
ds "gitlab.dms3.io/dms3/public/go-datastore"
"gitlab.dms3.io/p2p/go-p2p-core/routing"
"gitlab.dms3.io/p2p/go-p2p-core/test"
mh "github.com/multiformats/go-multihash"
)
type blankValidator struct{}
func (blankValidator) Validate(_ string, _ []byte) error { return nil }
func (blankValidator) Select(_ string, _ [][]byte) (int, error) { return 0, nil }
func TestOfflineRouterStorage(t *testing.T) {
ctx := context.Background()
nds := ds.NewMapDatastore()
offline := NewOfflineRouter(nds, blankValidator{})
if err := offline.PutValue(ctx, "key", []byte("testing 1 2 3")); err != nil {
t.Fatal(err)
}
val, err := offline.GetValue(ctx, "key")
if err != nil {
t.Fatal(err)
}
if !bytes.Equal([]byte("testing 1 2 3"), val) {
t.Fatal("OfflineRouter does not properly store")
}
_, err = offline.GetValue(ctx, "notHere")
if err == nil {
t.Fatal("Router should throw errors for unfound records")
}
local, err := offline.GetValue(ctx, "key", routing.Offline)
if err != nil {
t.Fatal(err)
}
_, err = offline.GetValue(ctx, "notHere", routing.Offline)
if err == nil {
t.Fatal("Router should throw errors for unfound records")
}
if !bytes.Equal([]byte("testing 1 2 3"), local) {
t.Fatal("OfflineRouter does not properly store")
}
}
func TestOfflineRouterLocal(t *testing.T) {
ctx := context.Background()
nds := ds.NewMapDatastore()
offline := NewOfflineRouter(nds, blankValidator{})
id, _ := test.RandPeerID()
_, err := offline.FindPeer(ctx, id)
if err != ErrOffline {
t.Fatal("OfflineRouting should alert that its offline")
}
h, _ := mh.Sum([]byte("test data1"), mh.SHA2_256, -1)
c1 := cid.NewCidV0(h)
pChan := offline.FindProvidersAsync(ctx, c1, 1)
p, ok := <-pChan
if ok {
t.Fatalf("FindProvidersAsync did not return a closed channel. Instead we got %+v !", p)
}
h2, _ := mh.Sum([]byte("test data1"), mh.SHA2_256, -1)
c2 := cid.NewCidV0(h2)
err = offline.Provide(ctx, c2, true)
if err != ErrOffline {
t.Fatal("OfflineRouting should alert that its offline")
}
err = offline.Bootstrap(ctx)
if err != nil {
t.Fatal("You shouldn't be able to bootstrap offline routing.")
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment