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

Printing bzzaddress (#316)

* bzz address print
parent 79dc5a2c
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
"encoding/binary" "encoding/binary"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"github.com/ethersphere/bee/pkg/crypto" "github.com/ethersphere/bee/pkg/crypto"
"github.com/ethersphere/bee/pkg/swarm" "github.com/ethersphere/bee/pkg/swarm"
...@@ -86,34 +87,44 @@ func (a *Address) Equal(b *Address) bool { ...@@ -86,34 +87,44 @@ func (a *Address) Equal(b *Address) bool {
return a.Overlay.Equal(b.Overlay) && a.Underlay.Equal(b.Underlay) && bytes.Equal(a.Signature, b.Signature) return a.Overlay.Equal(b.Overlay) && a.Underlay.Equal(b.Underlay) && bytes.Equal(a.Signature, b.Signature)
} }
func (p *Address) MarshalJSON() ([]byte, error) { func (a *Address) MarshalJSON() ([]byte, error) {
return json.Marshal(&addressJSON{ return json.Marshal(&addressJSON{
Overlay: p.Overlay.String(), Overlay: a.Overlay.String(),
Underlay: p.Underlay.String(), Underlay: a.Underlay.String(),
Signature: base64.StdEncoding.EncodeToString(p.Signature), Signature: base64.StdEncoding.EncodeToString(a.Signature),
}) })
} }
func (p *Address) UnmarshalJSON(b []byte) error { func (a *Address) UnmarshalJSON(b []byte) error {
v := &addressJSON{} v := &addressJSON{}
err := json.Unmarshal(b, v) err := json.Unmarshal(b, v)
if err != nil { if err != nil {
return err return err
} }
a, err := swarm.ParseHexAddress(v.Overlay) addr, err := swarm.ParseHexAddress(v.Overlay)
if err != nil { if err != nil {
return err return err
} }
p.Overlay = a a.Overlay = addr
m, err := ma.NewMultiaddr(v.Underlay) m, err := ma.NewMultiaddr(v.Underlay)
if err != nil { if err != nil {
return err return err
} }
p.Underlay = m a.Underlay = m
p.Signature, err = base64.StdEncoding.DecodeString(v.Signature) a.Signature, err = base64.StdEncoding.DecodeString(v.Signature)
return err return err
} }
func (a *Address) String() string {
return fmt.Sprintf("[Underlay: %v, Overlay %v, Signature %x]", a.Underlay, a.Overlay, a.Signature)
}
// ShortString returns shortened versions of bzz address in a format: [Overlay, Underlay]
// It can be used for logging
func (a *Address) ShortString() string {
return fmt.Sprintf("[Overlay: %s, Underlay: %s]", a.Overlay.String(), a.Underlay.String())
}
...@@ -149,8 +149,8 @@ func (k *Kad) manage() { ...@@ -149,8 +149,8 @@ func (k *Kad) manage() {
err = k.connect(ctx, peer, bzzAddr.Underlay, po) err = k.connect(ctx, peer, bzzAddr.Underlay, po)
if err != nil { if err != nil {
k.logger.Debugf("error connecting to peer from kademlia %s %+v: %v", peer, bzzAddr, err) k.logger.Debugf("error connecting to peer from kademlia %s %s: %v", peer, bzzAddr, err)
k.logger.Errorf("connecting to peer %s: %v", peer, err) k.logger.Errorf("connecting to peer %s: %v", bzzAddr.ShortString(), err)
// continue to next // continue to next
return false, false, nil return false, false, nil
} }
......
...@@ -241,7 +241,7 @@ func New(ctx context.Context, signer beecrypto.Signer, networkID uint64, overlay ...@@ -241,7 +241,7 @@ func New(ctx context.Context, signer beecrypto.Signer, networkID uint64, overlay
} }
s.metrics.HandledStreamCount.Inc() s.metrics.HandledStreamCount.Inc()
s.logger.Infof("peer %s connected", i.BzzAddress.Overlay) s.logger.Infof("successfully connected to peer %s", i.BzzAddress.ShortString())
}) })
h.Network().SetConnHandler(func(_ network.Conn) { h.Network().SetConnHandler(func(_ network.Conn) {
...@@ -377,7 +377,7 @@ func (s *Service) Connect(ctx context.Context, addr ma.Multiaddr) (address *bzz. ...@@ -377,7 +377,7 @@ func (s *Service) Connect(ctx context.Context, addr ma.Multiaddr) (address *bzz.
} }
s.metrics.CreatedConnectionCount.Inc() s.metrics.CreatedConnectionCount.Inc()
s.logger.Infof("peer %s connected", i.BzzAddress.Overlay) s.logger.Infof("successfully connected to peer %s", i.BzzAddress.ShortString())
return i.BzzAddress, nil return i.BzzAddress, nil
} }
......
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