Files
oc-lib/models/peer/peer.go

154 lines
5.2 KiB
Go
Raw Normal View History

2024-08-12 12:03:58 +02:00
package peer
import (
2024-08-23 09:53:37 +02:00
"fmt"
2026-01-23 07:45:36 +01:00
"strings"
2026-03-24 12:49:37 +01:00
"time"
2024-08-12 12:03:58 +02:00
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/tools"
2024-08-12 12:03:58 +02:00
)
2026-01-15 12:15:04 +01:00
type PeerRelation int
const (
NONE PeerRelation = iota
2024-10-15 16:37:18 +02:00
SELF
PARTNER
BLACKLIST
2026-01-22 16:11:54 +01:00
PENDING_PARTNER
2024-10-15 16:37:18 +02:00
)
2026-01-23 07:51:15 +01:00
var path = []string{"unknown", "self", "partner", "blacklist", "partner"}
2026-01-23 07:45:36 +01:00
2026-01-23 07:48:04 +01:00
func GetRelationPath(str string) int {
2026-01-23 07:45:36 +01:00
for i, p := range path {
if str == p {
return i
}
}
return -1
}
2026-01-23 07:38:20 +01:00
func (m PeerRelation) Path() string {
2026-01-23 07:45:36 +01:00
return path[m]
2026-01-23 07:38:20 +01:00
}
2026-01-15 12:15:04 +01:00
func (m PeerRelation) String() string {
2026-01-23 07:45:36 +01:00
return strings.ToUpper(path[m])
2024-10-30 11:17:52 +01:00
}
2026-01-15 12:15:04 +01:00
func (m PeerRelation) EnumIndex() int {
2024-10-30 11:17:52 +01:00
return int(m)
}
2026-03-24 12:49:37 +01:00
// BehaviorWarning records a single misbehavior observed by a trusted service.
type BehaviorWarning struct {
At time.Time `json:"at" bson:"at"`
ReporterApp string `json:"reporter_app" bson:"reporter_app"`
Severity tools.BehaviorSeverity `json:"severity" bson:"severity"`
Reason string `json:"reason" bson:"reason"`
Evidence string `json:"evidence,omitempty" bson:"evidence,omitempty"`
}
// Peer is a struct that represents a peer
2024-08-12 12:03:58 +02:00
type Peer struct {
utils.AbstractObject
2026-01-15 13:27:57 +01:00
2026-01-29 13:12:15 +01:00
Verify bool `json:"verify" bson:"verify"`
PeerID string `json:"peer_id" bson:"peer_id" validate:"required"`
2026-01-29 13:20:33 +01:00
APIUrl string `json:"api_url" bson:"api_url" validate:"required"` // Url is the URL of the peer (base64url)
StreamAddress string `json:"stream_address" bson:"stream_address" validate:"required"` // Url is the URL of the peer (base64url)
NATSAddress string `json:"nats_address" bson:"nats_address" validate:"required"`
2026-01-22 15:58:38 +01:00
WalletAddress string `json:"wallet_address" bson:"wallet_address" validate:"required"` // WalletAddress is the wallet address of the peer
PublicKey string `json:"public_key" bson:"public_key" validate:"required"` // PublicKey is the public key of the peer
2026-02-05 15:17:47 +01:00
Relation PeerRelation `json:"relation" bson:"relation" default:"0"`
2026-01-22 15:58:38 +01:00
ServicesState map[string]int `json:"services_state,omitempty" bson:"services_state,omitempty"`
FailedExecution []PeerExecution `json:"failed_execution" bson:"failed_execution"` // FailedExecution is the list of failed executions, to be retried
2026-03-24 12:49:37 +01:00
// Trust scoring — maintained by oc-discovery from PEER_BEHAVIOR_EVENT reports.
TrustScore float64 `json:"trust_score" bson:"trust_score" default:"100"`
BlacklistReason string `json:"blacklist_reason,omitempty" bson:"blacklist_reason,omitempty"`
BehaviorWarnings []BehaviorWarning `json:"behavior_warnings,omitempty" bson:"behavior_warnings,omitempty"`
2024-08-23 09:53:37 +02:00
}
2026-01-13 16:04:31 +01:00
func (ao *Peer) VerifyAuth(callName string, request *tools.APIRequest) bool {
2025-02-04 09:00:55 +01:00
return true
}
2026-03-24 12:49:37 +01:00
// BlacklistThreshold is the trust score below which a peer is auto-blacklisted.
const BlacklistThreshold = 20.0
// ApplyBehaviorReport records a misbehavior, deducts the trust penalty, and
// returns true when the trust score has fallen below BlacklistThreshold so the
// caller can trigger the relation change.
func (p *Peer) ApplyBehaviorReport(r tools.PeerBehaviorReport) (shouldBlacklist bool) {
p.BehaviorWarnings = append(p.BehaviorWarnings, BehaviorWarning{
At: r.At,
ReporterApp: r.ReporterApp,
Severity: r.Severity,
Reason: r.Reason,
Evidence: r.Evidence,
})
if p.TrustScore == 0 {
p.TrustScore = 100 // initialise if never set
}
p.TrustScore -= r.Severity.Penalty()
if p.TrustScore < 0 {
p.TrustScore = 0
}
if p.TrustScore <= BlacklistThreshold {
p.BlacklistReason = r.Reason
return true
}
return false
}
// ResetTrust clears all behavior history and resets the trust score to 100.
// Must be called when a peer relation is manually set to NONE or PARTNER.
func (p *Peer) ResetTrust() {
p.TrustScore = 100
p.BlacklistReason = ""
p.BehaviorWarnings = nil
}
// AddExecution adds an execution to the list of failed executions
2024-08-23 09:53:37 +02:00
func (ao *Peer) AddExecution(exec PeerExecution) {
found := false
for _, v := range ao.FailedExecution { // Check if the execution is already in the list
2024-08-23 09:53:37 +02:00
if v.Url == exec.Url && v.Method == exec.Method && fmt.Sprint(v.Body) == fmt.Sprint(exec.Body) {
found = true
break
}
}
if !found {
ao.FailedExecution = append(ao.FailedExecution, exec)
}
}
// RemoveExecution removes an execution from the list of failed executions
2024-08-23 09:53:37 +02:00
func (ao *Peer) RemoveExecution(exec PeerExecution) {
new := []PeerExecution{}
for i, v := range ao.FailedExecution {
if !(v.Url == exec.Url && v.Method == exec.Method && fmt.Sprint(v.Body) == fmt.Sprint(exec.Body)) {
new = append(new, ao.FailedExecution[i])
}
}
ao.FailedExecution = new
2024-08-12 12:03:58 +02:00
}
// LaunchPeerExecution launches an execution on a peer
2025-06-24 11:29:04 +02:00
func (p *Peer) LaunchPeerExecution(peerID string, dataID string, dt tools.DataType, method tools.METHOD, body interface{}, caller *tools.HTTPCaller) (map[string]interface{}, error) {
2024-08-21 08:54:29 +02:00
p.UUID = peerID
return cache.LaunchPeerExecution(peerID, dataID, dt, method, body, caller) // Launch the execution on the peer through the cache
2024-08-13 14:33:26 +02:00
}
func (d *Peer) GetAccessor(request *tools.APIRequest) utils.Accessor {
data := NewAccessor(request) // Create a new instance of the accessor
2024-08-12 12:03:58 +02:00
return data
}
2024-12-04 12:14:55 +01:00
func (r *Peer) CanDelete() bool {
return false // only draft order can be deleted
2024-12-04 12:14:55 +01:00
}