forked from TrueCloudLab/frostfs-node
[#1570] Upgrade NeoFS SDK Go with changed reputation API
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
9a6da336db
commit
9a11a75b77
28 changed files with 146 additions and 158 deletions
|
@ -5,6 +5,7 @@ import (
|
|||
"io"
|
||||
|
||||
"github.com/nspcc-dev/neofs-node/pkg/services/reputation"
|
||||
apireputation "github.com/nspcc-dev/neofs-sdk-go/reputation"
|
||||
)
|
||||
|
||||
// Context wraps stdlib context
|
||||
|
@ -59,7 +60,7 @@ type WriterProvider interface {
|
|||
type ManagerBuilder interface {
|
||||
// BuildManagers must compose list of managers. It depends on
|
||||
// particular epoch and PeerID of the current route point.
|
||||
BuildManagers(epoch uint64, p reputation.PeerID) ([]ServerInfo, error)
|
||||
BuildManagers(epoch uint64, p apireputation.PeerID) ([]ServerInfo, error)
|
||||
}
|
||||
|
||||
// ServerInfo describes a set of
|
||||
|
|
|
@ -1,15 +1,13 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
|
||||
"github.com/nspcc-dev/hrw"
|
||||
netmapcore "github.com/nspcc-dev/neofs-node/pkg/core/netmap"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/services/reputation"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
|
||||
apiNetmap "github.com/nspcc-dev/neofs-sdk-go/netmap"
|
||||
apireputation "github.com/nspcc-dev/neofs-sdk-go/reputation"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
|
@ -73,10 +71,10 @@ func (x nodeServer) NumberOfAddresses() int {
|
|||
|
||||
// BuildManagers sorts nodes in NetMap with HRW algorithms and
|
||||
// takes the next node after the current one as the only manager.
|
||||
func (mb *managerBuilder) BuildManagers(epoch uint64, p reputation.PeerID) ([]ServerInfo, error) {
|
||||
func (mb *managerBuilder) BuildManagers(epoch uint64, p apireputation.PeerID) ([]ServerInfo, error) {
|
||||
mb.log.Debug("start building managers",
|
||||
zap.Uint64("epoch", epoch),
|
||||
zap.String("peer", hex.EncodeToString(p.Bytes())),
|
||||
zap.Stringer("peer", p),
|
||||
)
|
||||
|
||||
nm, err := mb.nmSrc.GetNetMapByEpoch(epoch)
|
||||
|
@ -94,7 +92,7 @@ func (mb *managerBuilder) BuildManagers(epoch uint64, p reputation.PeerID) ([]Se
|
|||
hrw.SortSliceByValue(nodes, epoch)
|
||||
|
||||
for i := range nodes {
|
||||
if bytes.Equal(nodes[i].PublicKey(), p.Bytes()) {
|
||||
if apireputation.ComparePeerKey(p, nodes[i].PublicKey()) {
|
||||
managerIndex := i + 1
|
||||
|
||||
if managerIndex == len(nodes) {
|
||||
|
|
|
@ -2,10 +2,10 @@ package eigentrustcalc
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
|
||||
"github.com/nspcc-dev/neofs-node/pkg/services/reputation"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/services/reputation/eigentrust"
|
||||
apireputation "github.com/nspcc-dev/neofs-sdk-go/reputation"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
|
@ -69,7 +69,7 @@ func (c *Calculator) Calculate(prm CalculatePrm) {
|
|||
// continue with initial iteration number
|
||||
ctx.SetI(iter)
|
||||
|
||||
err = consumersIter.Iterate(func(daughter reputation.PeerID, iter TrustIterator) error {
|
||||
err = consumersIter.Iterate(func(daughter apireputation.PeerID, iter TrustIterator) error {
|
||||
err := c.prm.WorkerPool.Submit(func() {
|
||||
c.iterateDaughter(iterDaughterPrm{
|
||||
lastIter: prm.last,
|
||||
|
@ -99,7 +99,7 @@ type iterDaughterPrm struct {
|
|||
|
||||
ctx Context
|
||||
|
||||
id reputation.PeerID
|
||||
id apireputation.PeerID
|
||||
|
||||
consumersIter TrustIterator
|
||||
}
|
||||
|
@ -108,7 +108,7 @@ func (c *Calculator) iterateDaughter(p iterDaughterPrm) {
|
|||
initTrust, err := c.prm.InitialTrustSource.InitialTrust(p.id)
|
||||
if err != nil {
|
||||
c.opts.log.Debug("get initial trust failure",
|
||||
zap.String("daughter", hex.EncodeToString(p.id.Bytes())),
|
||||
zap.Stringer("daughter", p.id),
|
||||
zap.String("error", err.Error()),
|
||||
)
|
||||
|
||||
|
@ -244,14 +244,14 @@ func (c *Calculator) sendInitialValues(ctx Context) {
|
|||
return
|
||||
}
|
||||
|
||||
err = daughterIter.Iterate(func(daughter reputation.PeerID, iterator TrustIterator) error {
|
||||
err = daughterIter.Iterate(func(daughter apireputation.PeerID, iterator TrustIterator) error {
|
||||
return iterator.Iterate(func(trust reputation.Trust) error {
|
||||
trusted := trust.Peer()
|
||||
|
||||
initTrust, err := c.prm.InitialTrustSource.InitialTrust(trusted)
|
||||
if err != nil {
|
||||
c.opts.log.Debug("get initial trust failure",
|
||||
zap.String("peer", hex.EncodeToString(trusted.Bytes())),
|
||||
zap.Stringer("peer", trusted),
|
||||
zap.String("error", err.Error()),
|
||||
)
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
|
||||
"github.com/nspcc-dev/neofs-node/pkg/services/reputation"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/services/reputation/eigentrust"
|
||||
apireputation "github.com/nspcc-dev/neofs-sdk-go/reputation"
|
||||
)
|
||||
|
||||
type Context interface {
|
||||
|
@ -22,7 +23,7 @@ type Context interface {
|
|||
// trusts to current node's daughter. Realization may depends
|
||||
// on daughter.
|
||||
type InitialTrustSource interface {
|
||||
InitialTrust(reputation.PeerID) (reputation.TrustValue, error)
|
||||
InitialTrust(apireputation.PeerID) (reputation.TrustValue, error)
|
||||
}
|
||||
|
||||
// TrustIterator must iterate over all retrieved(or calculated) trusts
|
||||
|
@ -31,7 +32,7 @@ type TrustIterator interface {
|
|||
Iterate(reputation.TrustHandler) error
|
||||
}
|
||||
|
||||
type PeerTrustsHandler func(reputation.PeerID, TrustIterator) error
|
||||
type PeerTrustsHandler func(apireputation.PeerID, TrustIterator) error
|
||||
|
||||
// PeerTrustsIterator must iterate over all nodes(PeerIDs) and provide
|
||||
// TrustIterator for iteration over node's Trusts to others peers.
|
||||
|
@ -43,7 +44,7 @@ type DaughterTrustIteratorProvider interface {
|
|||
// InitDaughterIterator must init TrustIterator
|
||||
// that iterates over received local trusts from
|
||||
// daughter p for ctx.Epoch() epoch.
|
||||
InitDaughterIterator(ctx Context, p reputation.PeerID) (TrustIterator, error)
|
||||
InitDaughterIterator(ctx Context, p apireputation.PeerID) (TrustIterator, error)
|
||||
// InitAllDaughtersIterator must init PeerTrustsIterator
|
||||
// that must iterate over all daughters of the current
|
||||
// node(manager) and all trusts received from them for
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
package consumerstorage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/nspcc-dev/neofs-node/pkg/services/reputation"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/services/reputation/eigentrust"
|
||||
eigentrustcalc "github.com/nspcc-dev/neofs-node/pkg/services/reputation/eigentrust/calculator"
|
||||
apireputation "github.com/nspcc-dev/neofs-sdk-go/reputation"
|
||||
)
|
||||
|
||||
// Put saves intermediate trust of the consumer to daughter peer.
|
||||
|
@ -76,7 +78,7 @@ func (x *iterationConsumersStorage) put(trust eigentrust.IterationTrust) {
|
|||
s = x.mItems[iter]
|
||||
if s == nil {
|
||||
s = &ConsumersStorage{
|
||||
mItems: make(map[reputation.PeerID]*ConsumersTrusts, 1),
|
||||
mItems: make(map[string]*ConsumersTrusts, 1),
|
||||
}
|
||||
|
||||
x.mItems[iter] = s
|
||||
|
@ -107,7 +109,7 @@ func (x *iterationConsumersStorage) consumers(iter uint32) (s *ConsumersStorage,
|
|||
type ConsumersStorage struct {
|
||||
mtx sync.RWMutex
|
||||
|
||||
mItems map[reputation.PeerID]*ConsumersTrusts
|
||||
mItems map[string]*ConsumersTrusts
|
||||
}
|
||||
|
||||
func (x *ConsumersStorage) put(trust eigentrust.IterationTrust) {
|
||||
|
@ -116,12 +118,12 @@ func (x *ConsumersStorage) put(trust eigentrust.IterationTrust) {
|
|||
x.mtx.Lock()
|
||||
|
||||
{
|
||||
daughter := trust.Peer()
|
||||
daughter := trust.Peer().EncodeToString()
|
||||
|
||||
s = x.mItems[daughter]
|
||||
if s == nil {
|
||||
s = &ConsumersTrusts{
|
||||
mItems: make(map[reputation.PeerID]reputation.Trust, 1),
|
||||
mItems: make(map[string]reputation.Trust, 1),
|
||||
}
|
||||
|
||||
x.mItems[daughter] = s
|
||||
|
@ -140,7 +142,16 @@ func (x *ConsumersStorage) Iterate(h eigentrustcalc.PeerTrustsHandler) (err erro
|
|||
x.mtx.RLock()
|
||||
|
||||
{
|
||||
for trusted, trusts := range x.mItems {
|
||||
for strTrusted, trusts := range x.mItems {
|
||||
var trusted apireputation.PeerID
|
||||
|
||||
if strTrusted != "" {
|
||||
err = trusted.DecodeString(strTrusted)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("decode peer ID string %s: %v", strTrusted, err))
|
||||
}
|
||||
}
|
||||
|
||||
if err = h(trusted, trusts); err != nil {
|
||||
break
|
||||
}
|
||||
|
@ -157,14 +168,14 @@ func (x *ConsumersStorage) Iterate(h eigentrustcalc.PeerTrustsHandler) (err erro
|
|||
type ConsumersTrusts struct {
|
||||
mtx sync.RWMutex
|
||||
|
||||
mItems map[reputation.PeerID]reputation.Trust
|
||||
mItems map[string]reputation.Trust
|
||||
}
|
||||
|
||||
func (x *ConsumersTrusts) put(trust eigentrust.IterationTrust) {
|
||||
x.mtx.Lock()
|
||||
|
||||
{
|
||||
x.mItems[trust.TrustingPeer()] = trust.Trust
|
||||
x.mItems[trust.TrustingPeer().EncodeToString()] = trust.Trust
|
||||
}
|
||||
|
||||
x.mtx.Unlock()
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
package daughters
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/nspcc-dev/neofs-node/pkg/services/reputation"
|
||||
eigentrustcalc "github.com/nspcc-dev/neofs-node/pkg/services/reputation/eigentrust/calculator"
|
||||
apireputation "github.com/nspcc-dev/neofs-sdk-go/reputation"
|
||||
)
|
||||
|
||||
// Put saves daughter peer's trust to its provider for the epoch.
|
||||
|
@ -17,7 +19,7 @@ func (x *Storage) Put(epoch uint64, trust reputation.Trust) {
|
|||
s = x.mItems[epoch]
|
||||
if s == nil {
|
||||
s = &DaughterStorage{
|
||||
mItems: make(map[reputation.PeerID]*DaughterTrusts, 1),
|
||||
mItems: make(map[string]*DaughterTrusts, 1),
|
||||
}
|
||||
|
||||
x.mItems[epoch] = s
|
||||
|
@ -32,7 +34,7 @@ func (x *Storage) Put(epoch uint64, trust reputation.Trust) {
|
|||
// DaughterTrusts returns daughter trusts for the epoch.
|
||||
//
|
||||
// Returns false if there is no data for the epoch and daughter.
|
||||
func (x *Storage) DaughterTrusts(epoch uint64, daughter reputation.PeerID) (*DaughterTrusts, bool) {
|
||||
func (x *Storage) DaughterTrusts(epoch uint64, daughter apireputation.PeerID) (*DaughterTrusts, bool) {
|
||||
var (
|
||||
s *DaughterStorage
|
||||
ok bool
|
||||
|
@ -69,7 +71,7 @@ func (x *Storage) AllDaughterTrusts(epoch uint64) (*DaughterStorage, bool) {
|
|||
type DaughterStorage struct {
|
||||
mtx sync.RWMutex
|
||||
|
||||
mItems map[reputation.PeerID]*DaughterTrusts
|
||||
mItems map[string]*DaughterTrusts
|
||||
}
|
||||
|
||||
// Iterate passes IDs of the daughter peers with their trusts to h.
|
||||
|
@ -79,7 +81,16 @@ func (x *DaughterStorage) Iterate(h eigentrustcalc.PeerTrustsHandler) (err error
|
|||
x.mtx.RLock()
|
||||
|
||||
{
|
||||
for daughter, daughterTrusts := range x.mItems {
|
||||
for strDaughter, daughterTrusts := range x.mItems {
|
||||
var daughter apireputation.PeerID
|
||||
|
||||
if strDaughter != "" {
|
||||
err = daughter.DecodeString(strDaughter)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("decode peer ID string %s: %v", strDaughter, err))
|
||||
}
|
||||
}
|
||||
|
||||
if err = h(daughter, daughterTrusts); err != nil {
|
||||
break
|
||||
}
|
||||
|
@ -97,12 +108,12 @@ func (x *DaughterStorage) put(trust reputation.Trust) {
|
|||
x.mtx.Lock()
|
||||
|
||||
{
|
||||
trusting := trust.TrustingPeer()
|
||||
trusting := trust.TrustingPeer().EncodeToString()
|
||||
|
||||
dt = x.mItems[trusting]
|
||||
if dt == nil {
|
||||
dt = &DaughterTrusts{
|
||||
mItems: make(map[reputation.PeerID]reputation.Trust, 1),
|
||||
mItems: make(map[string]reputation.Trust, 1),
|
||||
}
|
||||
|
||||
x.mItems[trusting] = dt
|
||||
|
@ -114,11 +125,11 @@ func (x *DaughterStorage) put(trust reputation.Trust) {
|
|||
dt.put(trust)
|
||||
}
|
||||
|
||||
func (x *DaughterStorage) daughterTrusts(id reputation.PeerID) (dt *DaughterTrusts, ok bool) {
|
||||
func (x *DaughterStorage) daughterTrusts(id apireputation.PeerID) (dt *DaughterTrusts, ok bool) {
|
||||
x.mtx.RLock()
|
||||
|
||||
{
|
||||
dt, ok = x.mItems[id]
|
||||
dt, ok = x.mItems[id.EncodeToString()]
|
||||
}
|
||||
|
||||
x.mtx.RUnlock()
|
||||
|
@ -133,14 +144,14 @@ func (x *DaughterStorage) daughterTrusts(id reputation.PeerID) (dt *DaughterTrus
|
|||
type DaughterTrusts struct {
|
||||
mtx sync.RWMutex
|
||||
|
||||
mItems map[reputation.PeerID]reputation.Trust
|
||||
mItems map[string]reputation.Trust
|
||||
}
|
||||
|
||||
func (x *DaughterTrusts) put(trust reputation.Trust) {
|
||||
x.mtx.Lock()
|
||||
|
||||
{
|
||||
x.mItems[trust.Peer()] = trust
|
||||
x.mItems[trust.Peer().EncodeToString()] = trust
|
||||
}
|
||||
|
||||
x.mtx.Unlock()
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"sync"
|
||||
|
||||
"github.com/nspcc-dev/neofs-node/pkg/services/reputation"
|
||||
apireputation "github.com/nspcc-dev/neofs-sdk-go/reputation"
|
||||
)
|
||||
|
||||
// UpdatePrm groups the parameters of Storage's Update operation.
|
||||
|
@ -13,7 +14,7 @@ type UpdatePrm struct {
|
|||
|
||||
epoch uint64
|
||||
|
||||
peer reputation.PeerID
|
||||
peer apireputation.PeerID
|
||||
}
|
||||
|
||||
// SetEpoch sets number of the epoch
|
||||
|
@ -24,7 +25,7 @@ func (p *UpdatePrm) SetEpoch(e uint64) {
|
|||
|
||||
// SetPeer sets identifier of the peer
|
||||
// with which the local node interacted.
|
||||
func (p *UpdatePrm) SetPeer(id reputation.PeerID) {
|
||||
func (p *UpdatePrm) SetPeer(id apireputation.PeerID) {
|
||||
p.peer = id
|
||||
}
|
||||
|
||||
|
@ -51,12 +52,13 @@ func newTrustValueStorage() *EpochTrustValueStorage {
|
|||
}
|
||||
}
|
||||
|
||||
func stringifyPeerID(id reputation.PeerID) string {
|
||||
return string(id.Bytes())
|
||||
func stringifyPeerID(id apireputation.PeerID) string {
|
||||
return string(id.PublicKey())
|
||||
}
|
||||
|
||||
func peerIDFromString(str string) reputation.PeerID {
|
||||
return reputation.PeerIDFromBytes([]byte(str))
|
||||
func peerIDFromString(str string) (res apireputation.PeerID) {
|
||||
res.SetPublicKey([]byte(str))
|
||||
return
|
||||
}
|
||||
|
||||
func (s *EpochTrustValueStorage) update(prm UpdatePrm) {
|
||||
|
|
|
@ -1,17 +0,0 @@
|
|||
package reputation
|
||||
|
||||
const peerIDLength = 33
|
||||
|
||||
// PeerID represents identifier of reputation system participant.
|
||||
type PeerID [peerIDLength]byte
|
||||
|
||||
// Bytes converts PeerID to []byte.
|
||||
func (id PeerID) Bytes() []byte {
|
||||
return id[:]
|
||||
}
|
||||
|
||||
// PeerIDFromBytes restores PeerID from []byte.
|
||||
func PeerIDFromBytes(data []byte) (id PeerID) {
|
||||
copy(id[:], data)
|
||||
return
|
||||
}
|
|
@ -2,6 +2,8 @@ package reputation
|
|||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/nspcc-dev/neofs-sdk-go/reputation"
|
||||
)
|
||||
|
||||
// TrustValue represents the numeric value of the node's trust.
|
||||
|
@ -56,7 +58,7 @@ func (v TrustValue) IsZero() bool {
|
|||
|
||||
// Trust represents peer's trust (reputation).
|
||||
type Trust struct {
|
||||
trusting, peer PeerID
|
||||
trusting, peer reputation.PeerID
|
||||
|
||||
val TrustValue
|
||||
}
|
||||
|
@ -80,21 +82,21 @@ func (t *Trust) SetValue(val TrustValue) {
|
|||
}
|
||||
|
||||
// Peer returns trusted peer ID.
|
||||
func (t Trust) Peer() PeerID {
|
||||
func (t Trust) Peer() reputation.PeerID {
|
||||
return t.peer
|
||||
}
|
||||
|
||||
// SetPeer sets trusted peer ID.
|
||||
func (t *Trust) SetPeer(id PeerID) {
|
||||
func (t *Trust) SetPeer(id reputation.PeerID) {
|
||||
t.peer = id
|
||||
}
|
||||
|
||||
// TrustingPeer returns trusting peer ID.
|
||||
func (t Trust) TrustingPeer() PeerID {
|
||||
func (t Trust) TrustingPeer() reputation.PeerID {
|
||||
return t.trusting
|
||||
}
|
||||
|
||||
// SetTrustingPeer sets trusting peer ID.
|
||||
func (t *Trust) SetTrustingPeer(id PeerID) {
|
||||
func (t *Trust) SetTrustingPeer(id reputation.PeerID) {
|
||||
t.trusting = id
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue