forked from TrueCloudLab/frostfs-node
[#428] reputation: Define basic types
Define PeerID, TrustValue and Trust types and basic methods on them. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
518f375dfd
commit
eadb3204f0
2 changed files with 97 additions and 0 deletions
17
pkg/services/reputation/peer.go
Normal file
17
pkg/services/reputation/peer.go
Normal file
|
@ -0,0 +1,17 @@
|
|||
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
|
||||
}
|
80
pkg/services/reputation/trust.go
Normal file
80
pkg/services/reputation/trust.go
Normal file
|
@ -0,0 +1,80 @@
|
|||
package reputation
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// TrustValue represents the numeric value of the node's trust.
|
||||
type TrustValue float64
|
||||
|
||||
// TrustOne is a trust value equal to one.
|
||||
const TrustOne = TrustValue(1)
|
||||
|
||||
// TrustValueFromFloat64 converts float64 to TrustValue.
|
||||
func TrustValueFromFloat64(v float64) TrustValue {
|
||||
return TrustValue(v)
|
||||
}
|
||||
|
||||
// TrustValueFromInt converts int to TrustValue.
|
||||
func TrustValueFromInt(v int) TrustValue {
|
||||
return TrustValue(v)
|
||||
}
|
||||
|
||||
func (v TrustValue) String() string {
|
||||
return strconv.FormatFloat(float64(v), 'f', -1, 64)
|
||||
}
|
||||
|
||||
// Float64 converts TrustValue to float64.
|
||||
func (v TrustValue) Float64() float64 {
|
||||
return float64(v)
|
||||
}
|
||||
|
||||
// Add adds v2 to v.
|
||||
func (v *TrustValue) Add(v2 TrustValue) {
|
||||
*v = *v + v2
|
||||
}
|
||||
|
||||
// Div returns the result of dividing v by v2.
|
||||
func (v TrustValue) Div(v2 TrustValue) TrustValue {
|
||||
return v / v2
|
||||
}
|
||||
|
||||
// IsZero returns true if v equal to zero.
|
||||
func (v TrustValue) IsZero() bool {
|
||||
return v == 0
|
||||
}
|
||||
|
||||
// Trust represents peer's trust (reputation).
|
||||
type Trust struct {
|
||||
peer PeerID
|
||||
|
||||
val TrustValue
|
||||
}
|
||||
|
||||
// TrustHandler describes the signature of the reputation.Trust
|
||||
// value handling function.
|
||||
//
|
||||
// Termination of processing without failures is usually signaled
|
||||
// with a zero error, while a specific value may describe the reason
|
||||
// for failure.
|
||||
type TrustHandler func(Trust) error
|
||||
|
||||
// Value returns peer's trust value.
|
||||
func (t Trust) Value() TrustValue {
|
||||
return t.val
|
||||
}
|
||||
|
||||
// SetValue sets peer's trust value.
|
||||
func (t *Trust) SetValue(val TrustValue) {
|
||||
t.val = val
|
||||
}
|
||||
|
||||
// Peer returns trusted peer ID.
|
||||
func (t Trust) Peer() PeerID {
|
||||
return t.peer
|
||||
}
|
||||
|
||||
// SetPeer sets trusted peer ID.
|
||||
func (t *Trust) SetPeer(id PeerID) {
|
||||
t.peer = id
|
||||
}
|
Loading…
Reference in a new issue