2021-04-10 12:02:51 +00:00
|
|
|
package consumerstorage
|
|
|
|
|
|
|
|
import (
|
2022-07-04 13:24:51 +00:00
|
|
|
"fmt"
|
2021-04-10 12:02:51 +00:00
|
|
|
"sync"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/reputation"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/reputation/eigentrust"
|
|
|
|
eigentrustcalc "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/reputation/eigentrust/calculator"
|
|
|
|
apireputation "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/reputation"
|
2021-04-10 12:02:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Put saves intermediate trust of the consumer to daughter peer.
|
|
|
|
func (x *Storage) Put(trust eigentrust.IterationTrust) {
|
|
|
|
var s *iterationConsumersStorage
|
|
|
|
|
|
|
|
x.mtx.Lock()
|
|
|
|
|
|
|
|
{
|
|
|
|
epoch := trust.Epoch()
|
|
|
|
|
|
|
|
s = x.mItems[epoch]
|
|
|
|
if s == nil {
|
|
|
|
s = &iterationConsumersStorage{
|
|
|
|
mItems: make(map[uint32]*ConsumersStorage, 1),
|
|
|
|
}
|
|
|
|
|
|
|
|
x.mItems[epoch] = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
x.mtx.Unlock()
|
|
|
|
|
|
|
|
s.put(trust)
|
|
|
|
}
|
|
|
|
|
2021-04-14 13:40:52 +00:00
|
|
|
// Consumers returns the storage of trusts of the consumers of the daughter peers
|
2021-04-10 12:02:51 +00:00
|
|
|
// for particular iteration of EigenTrust calculation for particular epoch.
|
|
|
|
//
|
|
|
|
// Returns false if there is no data for the epoch and iter.
|
|
|
|
func (x *Storage) Consumers(epoch uint64, iter uint32) (*ConsumersStorage, bool) {
|
|
|
|
var (
|
|
|
|
s *iterationConsumersStorage
|
|
|
|
ok bool
|
|
|
|
)
|
|
|
|
|
|
|
|
x.mtx.Lock()
|
|
|
|
|
|
|
|
{
|
|
|
|
s, ok = x.mItems[epoch]
|
|
|
|
}
|
|
|
|
|
|
|
|
x.mtx.Unlock()
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.consumers(iter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// maps iteration numbers of EigenTrust algorithm to repositories
|
|
|
|
// of the trusts of the consumers of the daughter peers.
|
|
|
|
type iterationConsumersStorage struct {
|
|
|
|
mtx sync.RWMutex
|
|
|
|
|
|
|
|
mItems map[uint32]*ConsumersStorage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *iterationConsumersStorage) put(trust eigentrust.IterationTrust) {
|
|
|
|
var s *ConsumersStorage
|
|
|
|
|
|
|
|
x.mtx.Lock()
|
|
|
|
|
|
|
|
{
|
|
|
|
iter := trust.I()
|
|
|
|
|
|
|
|
s = x.mItems[iter]
|
|
|
|
if s == nil {
|
|
|
|
s = &ConsumersStorage{
|
2022-07-04 13:24:51 +00:00
|
|
|
mItems: make(map[string]*ConsumersTrusts, 1),
|
2021-04-10 12:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
x.mItems[iter] = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
x.mtx.Unlock()
|
|
|
|
|
|
|
|
s.put(trust)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *iterationConsumersStorage) consumers(iter uint32) (s *ConsumersStorage, ok bool) {
|
|
|
|
x.mtx.Lock()
|
|
|
|
|
|
|
|
{
|
|
|
|
s, ok = x.mItems[iter]
|
|
|
|
}
|
|
|
|
|
|
|
|
x.mtx.Unlock()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConsumersStorage represents in-memory storage of intermediate trusts
|
|
|
|
// of the peer consumers.
|
|
|
|
//
|
|
|
|
// Maps daughter peers to repositories of the trusts of their consumers.
|
|
|
|
type ConsumersStorage struct {
|
|
|
|
mtx sync.RWMutex
|
|
|
|
|
2022-07-04 13:24:51 +00:00
|
|
|
mItems map[string]*ConsumersTrusts
|
2021-04-10 12:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (x *ConsumersStorage) put(trust eigentrust.IterationTrust) {
|
|
|
|
var s *ConsumersTrusts
|
|
|
|
|
|
|
|
x.mtx.Lock()
|
|
|
|
|
|
|
|
{
|
2022-07-04 13:24:51 +00:00
|
|
|
daughter := trust.Peer().EncodeToString()
|
2021-04-10 12:02:51 +00:00
|
|
|
|
|
|
|
s = x.mItems[daughter]
|
|
|
|
if s == nil {
|
|
|
|
s = &ConsumersTrusts{
|
2022-07-04 13:24:51 +00:00
|
|
|
mItems: make(map[string]reputation.Trust, 1),
|
2021-04-10 12:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
x.mItems[daughter] = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
x.mtx.Unlock()
|
|
|
|
|
|
|
|
s.put(trust)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate passes IDs of the daughter peers with the trusts of their consumers to h.
|
|
|
|
//
|
|
|
|
// Returns errors from h directly.
|
2021-04-21 10:37:20 +00:00
|
|
|
func (x *ConsumersStorage) Iterate(h eigentrustcalc.PeerTrustsHandler) (err error) {
|
2021-04-10 12:02:51 +00:00
|
|
|
x.mtx.RLock()
|
|
|
|
|
|
|
|
{
|
2022-07-04 13:24:51 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-10 12:02:51 +00:00
|
|
|
if err = h(trusted, trusts); err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
x.mtx.RUnlock()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConsumersTrusts represents in-memory storage of the trusts
|
|
|
|
// of the consumer peers to some other peer.
|
|
|
|
type ConsumersTrusts struct {
|
|
|
|
mtx sync.RWMutex
|
|
|
|
|
2022-07-04 13:24:51 +00:00
|
|
|
mItems map[string]reputation.Trust
|
2021-04-10 12:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (x *ConsumersTrusts) put(trust eigentrust.IterationTrust) {
|
|
|
|
x.mtx.Lock()
|
|
|
|
|
|
|
|
{
|
2022-07-04 13:24:51 +00:00
|
|
|
x.mItems[trust.TrustingPeer().EncodeToString()] = trust.Trust
|
2021-04-10 12:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
x.mtx.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate passes all stored trusts to h.
|
|
|
|
//
|
|
|
|
// Returns errors from h directly.
|
|
|
|
func (x *ConsumersTrusts) Iterate(h reputation.TrustHandler) (err error) {
|
|
|
|
x.mtx.RLock()
|
|
|
|
|
|
|
|
{
|
|
|
|
for _, trust := range x.mItems {
|
|
|
|
if err = h(trust); err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
x.mtx.RUnlock()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|