Commit ec83fd1d authored by Petar Radovic's avatar Petar Radovic Committed by GitHub

Addressbook get - not found (#319)

* addressbook get not found
parent 33198376
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
package addressbook package addressbook
import ( import (
"errors"
"fmt" "fmt"
"strings" "strings"
...@@ -17,6 +18,8 @@ const keyPrefix = "addressbook_entry_" ...@@ -17,6 +18,8 @@ const keyPrefix = "addressbook_entry_"
var _ Interface = (*store)(nil) var _ Interface = (*store)(nil)
var ErrNotFound = errors.New("addressbook: not found")
type Interface interface { type Interface interface {
GetPutter GetPutter
Remover Remover
...@@ -30,7 +33,7 @@ type GetPutter interface { ...@@ -30,7 +33,7 @@ type GetPutter interface {
} }
type Getter interface { type Getter interface {
Get(overlay swarm.Address) (addr bzz.Address, err error) Get(overlay swarm.Address) (addr *bzz.Address, err error)
} }
type Putter interface { type Putter interface {
...@@ -51,12 +54,16 @@ func New(storer storage.StateStorer) Interface { ...@@ -51,12 +54,16 @@ func New(storer storage.StateStorer) Interface {
} }
} }
func (s *store) Get(overlay swarm.Address) (bzz.Address, error) { func (s *store) Get(overlay swarm.Address) (*bzz.Address, error) {
key := keyPrefix + overlay.String() key := keyPrefix + overlay.String()
v := bzz.Address{} v := &bzz.Address{}
err := s.store.Get(key, &v) err := s.store.Get(key, &v)
if err != nil { if err != nil {
return bzz.Address{}, err if err == storage.ErrNotFound {
return nil, ErrNotFound
}
return nil, err
} }
return v, nil return v, nil
} }
......
...@@ -22,7 +22,6 @@ func TestInMem(t *testing.T) { ...@@ -22,7 +22,6 @@ func TestInMem(t *testing.T) {
run(t, func(t *testing.T) addressbook.Interface { run(t, func(t *testing.T) addressbook.Interface {
store := mock.NewStateStore() store := mock.NewStateStore()
book := addressbook.New(store) book := addressbook.New(store)
return book return book
}) })
} }
...@@ -56,13 +55,17 @@ func run(t *testing.T, f bookFunc) { ...@@ -56,13 +55,17 @@ func run(t *testing.T, f bookFunc) {
t.Fatal(err) t.Fatal(err)
} }
_, err = store.Get(addr2) if !bzzAddr.Equal(v) {
if err == nil { t.Fatalf("expectted: %s, want %s", v, multiaddr)
t.Fatal("value found in store but should not have been") }
notFound, err := store.Get(addr2)
if err != addressbook.ErrNotFound {
t.Fatal(err)
} }
if !bzzAddr.Equal(&v) { if notFound != nil {
t.Fatalf("value retrieved from store not equal to original stored address: %v, want %v", v, multiaddr) t.Fatalf("expected nil got %s", v)
} }
overlays, err := store.Overlays() overlays, err := store.Overlays()
......
...@@ -17,7 +17,6 @@ import ( ...@@ -17,7 +17,6 @@ import (
"github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest" "github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest"
"github.com/ethersphere/bee/pkg/p2p" "github.com/ethersphere/bee/pkg/p2p"
"github.com/ethersphere/bee/pkg/p2p/mock" "github.com/ethersphere/bee/pkg/p2p/mock"
"github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/swarm" "github.com/ethersphere/bee/pkg/swarm"
topmock "github.com/ethersphere/bee/pkg/topology/mock" topmock "github.com/ethersphere/bee/pkg/topology/mock"
ma "github.com/multiformats/go-multiaddr" ma "github.com/multiformats/go-multiaddr"
...@@ -59,7 +58,10 @@ func TestConnect(t *testing.T) { ...@@ -59,7 +58,10 @@ func TestConnect(t *testing.T) {
}) })
bzzAddr, err := testServer.Addressbook.Get(overlay) bzzAddr, err := testServer.Addressbook.Get(overlay)
if err != nil && errors.Is(err, storage.ErrNotFound) && !bzzAddress.Equal(&bzzAddr) { if err != nil {
t.Fatal(err)
}
if !bzzAddress.Equal(bzzAddr) {
t.Fatalf("found wrong underlay. expected: %+v, found: %+v", bzzAddress, bzzAddr) t.Fatalf("found wrong underlay. expected: %+v, found: %+v", bzzAddress, bzzAddr)
} }
}) })
...@@ -99,7 +101,10 @@ func TestConnect(t *testing.T) { ...@@ -99,7 +101,10 @@ func TestConnect(t *testing.T) {
}) })
bzzAddr, err := testServer.Addressbook.Get(overlay) bzzAddr, err := testServer.Addressbook.Get(overlay)
if err != nil && errors.Is(err, storage.ErrNotFound) && !bzzAddress.Equal(&bzzAddr) { if err != nil {
t.Fatal(err)
}
if !bzzAddress.Equal(bzzAddr) {
t.Fatalf("found wrong underlay. expected: %+v, found: %+v", bzzAddress, bzzAddr) t.Fatalf("found wrong underlay. expected: %+v, found: %+v", bzzAddress, bzzAddr)
} }
......
...@@ -6,7 +6,6 @@ package hive ...@@ -6,7 +6,6 @@ package hive
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"time" "time"
...@@ -16,7 +15,6 @@ import ( ...@@ -16,7 +15,6 @@ import (
"github.com/ethersphere/bee/pkg/logging" "github.com/ethersphere/bee/pkg/logging"
"github.com/ethersphere/bee/pkg/p2p" "github.com/ethersphere/bee/pkg/p2p"
"github.com/ethersphere/bee/pkg/p2p/protobuf" "github.com/ethersphere/bee/pkg/p2p/protobuf"
"github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/swarm" "github.com/ethersphere/bee/pkg/swarm"
) )
...@@ -97,10 +95,11 @@ func (s *Service) sendPeers(ctx context.Context, peer swarm.Address, peers []swa ...@@ -97,10 +95,11 @@ func (s *Service) sendPeers(ctx context.Context, peer swarm.Address, peers []swa
for _, p := range peers { for _, p := range peers {
addr, err := s.addressBook.Get(p) addr, err := s.addressBook.Get(p)
if err != nil { if err != nil {
if errors.Is(err, storage.ErrNotFound) { if err == addressbook.ErrNotFound {
s.logger.Debugf("Peer not found %s", p) s.logger.Debugf("hive broadcast peers: peer not found in the addressbook. Skipping peer %s", p)
continue continue
} }
return err return err
} }
......
...@@ -16,7 +16,6 @@ import ( ...@@ -16,7 +16,6 @@ import (
"github.com/ethersphere/bee/pkg/kademlia/pslice" "github.com/ethersphere/bee/pkg/kademlia/pslice"
"github.com/ethersphere/bee/pkg/logging" "github.com/ethersphere/bee/pkg/logging"
"github.com/ethersphere/bee/pkg/p2p" "github.com/ethersphere/bee/pkg/p2p"
"github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/swarm" "github.com/ethersphere/bee/pkg/swarm"
"github.com/ethersphere/bee/pkg/topology" "github.com/ethersphere/bee/pkg/topology"
ma "github.com/multiformats/go-multiaddr" ma "github.com/multiformats/go-multiaddr"
...@@ -134,17 +133,16 @@ func (k *Kad) manage() { ...@@ -134,17 +133,16 @@ func (k *Kad) manage() {
bzzAddr, err := k.addressBook.Get(peer) bzzAddr, err := k.addressBook.Get(peer)
if err != nil { if err != nil {
// either a peer is not known in the address book, in which case it if err == addressbook.ErrNotFound {
// should be removed, or that some severe I/O problem is at hand
if errors.Is(err, storage.ErrNotFound) {
k.logger.Errorf("failed to get address book entry for peer: %s", peer.String()) k.logger.Errorf("failed to get address book entry for peer: %s", peer.String())
peerToRemove = peer peerToRemove = peer
return false, false, errMissingAddressBookEntry return false, false, errMissingAddressBookEntry
} }
// either a peer is not known in the address book, in which case it
// should be removed, or that some severe I/O problem is at hand
return false, false, err return false, false, err
} }
k.logger.Debugf("kademlia dialing to peer %s", peer.String()) k.logger.Debugf("kademlia dialing to peer %s", peer.String())
err = k.connect(ctx, peer, bzzAddr.Underlay, po) err = k.connect(ctx, peer, bzzAddr.Underlay, po)
......
...@@ -377,7 +377,7 @@ func TestAddressBookPrune(t *testing.T) { ...@@ -377,7 +377,7 @@ func TestAddressBookPrune(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if !nonConnPeer.Equal(&p) { if !nonConnPeer.Equal(p) {
t.Fatalf("expected %+v, got %+v", nonConnPeer, p) t.Fatalf("expected %+v, got %+v", nonConnPeer, p)
} }
...@@ -392,7 +392,7 @@ func TestAddressBookPrune(t *testing.T) { ...@@ -392,7 +392,7 @@ func TestAddressBookPrune(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if !nonConnPeer.Equal(&p) { if !nonConnPeer.Equal(p) {
t.Fatalf("expected %+v, got %+v", nonConnPeer, p) t.Fatalf("expected %+v, got %+v", nonConnPeer, p)
} }
...@@ -407,7 +407,7 @@ func TestAddressBookPrune(t *testing.T) { ...@@ -407,7 +407,7 @@ func TestAddressBookPrune(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if !nonConnPeer.Equal(&p) { if !nonConnPeer.Equal(p) {
t.Fatalf("expected %+v, got %+v", nonConnPeer, p) t.Fatalf("expected %+v, got %+v", nonConnPeer, p)
} }
...@@ -417,13 +417,9 @@ func TestAddressBookPrune(t *testing.T) { ...@@ -417,13 +417,9 @@ func TestAddressBookPrune(t *testing.T) {
waitCounter(t, &conns, 1) waitCounter(t, &conns, 1)
waitCounter(t, &failedConns, 1) waitCounter(t, &failedConns, 1)
p, err = ab.Get(nonConnPeer.Overlay) _, err = ab.Get(nonConnPeer.Overlay)
if err == nil { if err != addressbook.ErrNotFound {
t.Fatal("expected not found error") t.Fatal(err)
}
if nonConnPeer.Equal(&p) {
t.Fatal("peer found in addressbook")
} }
} }
......
...@@ -17,7 +17,6 @@ import ( ...@@ -17,7 +17,6 @@ import (
"github.com/ethersphere/bee/pkg/discovery" "github.com/ethersphere/bee/pkg/discovery"
"github.com/ethersphere/bee/pkg/logging" "github.com/ethersphere/bee/pkg/logging"
"github.com/ethersphere/bee/pkg/p2p" "github.com/ethersphere/bee/pkg/p2p"
"github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/swarm" "github.com/ethersphere/bee/pkg/swarm"
"github.com/ethersphere/bee/pkg/topology" "github.com/ethersphere/bee/pkg/topology"
) )
...@@ -71,7 +70,7 @@ func (d *driver) AddPeer(ctx context.Context, addr swarm.Address) error { ...@@ -71,7 +70,7 @@ func (d *driver) AddPeer(ctx context.Context, addr swarm.Address) error {
connectedPeers := d.p2pService.Peers() connectedPeers := d.p2pService.Peers()
bzzAddress, err := d.addressBook.Get(addr) bzzAddress, err := d.addressBook.Get(addr)
if err != nil { if err != nil {
if errors.Is(err, storage.ErrNotFound) { if err == addressbook.ErrNotFound {
return topology.ErrNotFound return topology.ErrNotFound
} }
return err return err
......
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