2020-07-24 13:54:03 +00:00
|
|
|
package storage
|
2020-07-10 14:17:51 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
|
|
|
|
"github.com/multiformats/go-multiaddr"
|
2020-07-24 13:54:03 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network/peers"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
|
2020-07-10 14:17:51 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// AddressStoreComponent is an interface of encapsulated AddressStore and NodePublicKeyReceiver pair.
|
|
|
|
AddressStoreComponent interface {
|
|
|
|
AddressStore
|
|
|
|
NodePublicKeyReceiver
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddressStore is an interface of the container of local Multiaddr.
|
|
|
|
AddressStore interface {
|
|
|
|
SelfAddr() (multiaddr.Multiaddr, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodePublicKeyReceiver is an interface of Multiaddr to PublicKey converter.
|
|
|
|
NodePublicKeyReceiver interface {
|
|
|
|
PublicKey(multiaddr.Multiaddr) *ecdsa.PublicKey
|
|
|
|
}
|
|
|
|
|
|
|
|
addressStore struct {
|
|
|
|
ps peers.Store
|
|
|
|
|
|
|
|
log *zap.Logger
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
const addressStoreInstanceFailMsg = "could not create address store"
|
2020-07-10 14:17:51 +00:00
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
var (
|
|
|
|
errEmptyPeerStore = errors.New("empty peer store")
|
|
|
|
|
|
|
|
errEmptyAddressStore = errors.New("empty address store")
|
2020-07-10 14:17:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s addressStore) SelfAddr() (multiaddr.Multiaddr, error) { return s.ps.GetAddr(s.ps.SelfID()) }
|
|
|
|
|
|
|
|
func (s addressStore) PublicKey(mAddr multiaddr.Multiaddr) (res *ecdsa.PublicKey) {
|
|
|
|
if peerID, err := s.ps.AddressID(mAddr); err != nil {
|
|
|
|
s.log.Error("could not peer ID",
|
|
|
|
zap.Stringer("node", mAddr),
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
} else if res, err = s.ps.GetPublicKey(peerID); err != nil {
|
|
|
|
s.log.Error("could not receive public key",
|
|
|
|
zap.Stringer("peer", peerID),
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAddressStore wraps peer store and returns AddressStoreComponent.
|
|
|
|
func NewAddressStore(ps peers.Store, log *zap.Logger) (AddressStoreComponent, error) {
|
|
|
|
if ps == nil {
|
|
|
|
return nil, errors.Wrap(errEmptyPeerStore, addressStoreInstanceFailMsg)
|
|
|
|
} else if log == nil {
|
2020-07-24 13:54:03 +00:00
|
|
|
return nil, errors.Wrap(logger.ErrNilLogger, addressStoreInstanceFailMsg)
|
2020-07-10 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &addressStore{
|
|
|
|
ps: ps,
|
|
|
|
log: log,
|
|
|
|
}, nil
|
|
|
|
}
|