-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidatorReputationService.ts
More file actions
908 lines (782 loc) · 27.3 KB
/
ValidatorReputationService.ts
File metadata and controls
908 lines (782 loc) · 27.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
/**
* Validator Reputation Service
*
* Implements reputation tracking and updates for validators.
*
* Core Principle: "Option 1 - Rejection without Slashing"
* - Invalid evaluations are rejected (no payment)
* - No funds are taken from validators
* - Reputation is updated based on performance
* - Low reputation = lower future rewards (soft enforcement)
* - Very low reputation = temporary ban (no slashing)
*
* This service tracks:
* - Validation accuracy
* - Consistency
* - Reliability
* - Failure patterns
*/
import { ILogger } from './utils/ILogger';
import { ValidatorEvaluation } from './EvaluationService';
import { RiskVector, TaskConditionedReputation, NetworkState } from './types';
import { AdversarialTestResult } from './AdversarialTestingService';
import { getInvariantChecker } from './InvariantChecker';
/**
* Validator Reputation Metrics
*/
export interface ValidatorReputationMetrics {
validatorAddress: string;
// Reputation score (0-100)
reputation: number;
// Performance metrics
totalValidations: number;
successfulValidations: number;
failedValidations: number;
rejectedValidations: number; // Invalid evaluations (rejected, no payment)
// Accuracy metrics
accuracy: number; // successfulValidations / totalValidations (0-1)
consistency: number; // How consistent evaluations are (0-1)
// Failure tracking
consecutiveFailures: number; // Consecutive failed/rejected validations
totalFailures: number; // Total failures (for ban threshold)
// Status
isBanned: boolean;
banUntil?: number; // Unix timestamp when ban expires
banReason?: string;
// Timestamps
lastValidation: number;
lastFailure: number;
createdAt: number;
updatedAt: number;
// NEW: Multi-dimensional risk vector
riskVector?: RiskVector;
// NEW: Task-conditioned reputation (per network/task type)
taskConditionedReputations?: Map<string, TaskConditionedReputation>; // key: networkId:taskType
// NEW: Surprisal tracking (for unpredictability)
surprisalHistory: number[]; // History of surprisal scores (entropy)
averageSurprisal: number; // Average surprisal (0-1)
// NEW: Temporal decay factor
temporalDecay: number; // Decay factor (0-1, lower = more decay)
lastDecayUpdate: number; // Last time decay was applied
}
/**
* Validation Result
*/
export interface ValidationResult {
valid: boolean;
reason?: string;
shouldReject: boolean; // Whether to reject (no payment)
reputationPenalty?: number; // Reputation points to deduct (0-100)
shouldBan?: boolean; // Whether to apply temporary ban
banDuration?: number; // Ban duration in seconds
}
/**
* Reputation Update Result
*/
export interface ReputationUpdateResult {
validatorAddress: string;
oldReputation: number;
newReputation: number;
reputationChange: number;
wasRejected: boolean;
wasBanned: boolean;
banUntil?: number;
}
/**
* Validator Reputation Service
*/
export class ValidatorReputationService {
private logger: ILogger;
private reputationMetrics: Map<string, ValidatorReputationMetrics> = new Map();
// Configuration
private readonly DEFAULT_REPUTATION = 50; // Starting reputation (neutral)
private readonly MIN_REPUTATION = 0; // Minimum reputation
private readonly MAX_REPUTATION = 100; // Maximum reputation
// Reputation changes
private readonly SUCCESS_BOOST = 1; // Reputation increase per successful validation
private readonly FAILURE_PENALTY = 5; // Reputation decrease per failed validation
private readonly REJECTION_PENALTY = 10; // Reputation decrease per rejected validation
// Ban thresholds
private readonly BAN_THRESHOLD_REPUTATION = 20; // Ban if reputation drops below this
private readonly BAN_THRESHOLD_FAILURES = 5; // Ban after N consecutive failures
private readonly DEFAULT_BAN_DURATION = 7 * 24 * 60 * 60 * 1000; // 7 days in milliseconds
// Recovery
private readonly RECOVERY_RATE = 0.1; // Reputation recovery per successful validation (10%)
// NEW: Temporal decay configuration
private readonly TEMPORAL_DECAY_RATE = 0.95; // Decay factor per day (0.95 = 5% decay per day)
private readonly DECAY_UPDATE_INTERVAL = 24 * 60 * 60 * 1000; // Update decay every 24 hours
// NEW: Surprisal configuration
private readonly MIN_SURPRISAL = 0.3; // Minimum surprisal to avoid penalty (0-1)
private readonly SURPRISAL_HISTORY_SIZE = 100; // Keep last N surprisal scores
// NEW: Risk vector weights (for aggregation)
private readonly RISK_VECTOR_WEIGHTS = {
exploration: 0.2,
consistency: 0.2,
reliability: 0.2,
diversity: 0.15,
surprisal: 0.15,
temporalStability: 0.05,
adversarialResistance: 0.05,
};
constructor(logger?: Logger) {
this.logger = logger || new Logger('ValidatorReputationService');
}
/**
* Validate evaluation and determine if it should be rejected
*
* Option 1: Rejection without slashing
* - Invalid evaluations are rejected (no payment)
* - No funds are taken
* - Reputation is penalized
*/
validateEvaluation(
evaluation: ValidatorEvaluation,
consensusResult: {
accepted: boolean;
consensusReached: boolean;
majorityScore: number;
}
): ValidationResult {
// Check if evaluation is valid
const isValid = this.isEvaluationValid(evaluation, consensusResult);
if (!isValid) {
// REJECT (Option 1: No slashing, just rejection)
return {
valid: false,
reason: 'Evaluation does not match consensus or is invalid',
shouldReject: true,
reputationPenalty: this.REJECTION_PENALTY,
};
}
// Check if validator agreed with consensus
const agreedWithConsensus = this.agreedWithConsensus(evaluation, consensusResult);
if (!agreedWithConsensus) {
// Failed validation (disagreed with consensus)
return {
valid: true,
reason: 'Evaluation disagreed with consensus',
shouldReject: false, // Still paid, but reputation penalty
reputationPenalty: this.FAILURE_PENALTY,
};
}
// Successful validation
return {
valid: true,
shouldReject: false,
// Reputation boost handled separately in updateReputation()
};
}
/**
* Check if evaluation is valid
*/
private isEvaluationValid(
evaluation: ValidatorEvaluation,
consensusResult: {
accepted: boolean;
consensusReached: boolean;
majorityScore: number;
}
): boolean {
// Basic validation checks
if (!evaluation.validatorAddress) return false;
if (!evaluation.outputId) return false;
if (evaluation.score < 0 || evaluation.score > 100) return false;
if (evaluation.confidence < 0 || evaluation.confidence > 1) return false;
if (!evaluation.signature) return false;
// If consensus reached, check if evaluation is reasonable
if (consensusResult.consensusReached) {
const scoreDifference = Math.abs(evaluation.score - consensusResult.majorityScore);
// If score difference is too large, might be invalid
if (scoreDifference > 50) {
return false; // Suspicious evaluation
}
}
return true;
}
/**
* Check if validator agreed with consensus
*/
private agreedWithConsensus(
evaluation: ValidatorEvaluation,
consensusResult: {
accepted: boolean;
consensusReached: boolean;
majorityScore: number;
}
): boolean {
if (!consensusResult.consensusReached) {
return true; // No consensus, can't determine agreement
}
// Check if evaluation score is close to majority score
const scoreDifference = Math.abs(evaluation.score - consensusResult.majorityScore);
const threshold = 10; // Within 10 points is considered agreement
return scoreDifference <= threshold;
}
/**
* Update validator reputation based on validation result
*
* Option 1: Rejection without slashing
* - Successful validations → reputation increase
* - Failed validations → reputation decrease
* - Rejected validations → reputation decrease (no payment)
* - Very low reputation → temporary ban (no slashing)
*/
async updateReputation(
validatorAddress: string,
validationResult: ValidationResult,
wasSuccessful: boolean
): Promise<ReputationUpdateResult> {
// Get current reputation metrics
let metrics = this.reputationMetrics.get(validatorAddress);
if (!metrics) {
// Initialize new validator
metrics = this.initializeValidator(validatorAddress);
}
const oldReputation = metrics.reputation;
let newReputation = oldReputation;
let wasRejected = false;
let wasBanned = false;
let banUntil: number | undefined;
// Update based on validation result
if (validationResult.shouldReject) {
// REJECTED (Option 1: No payment, no slashing)
wasRejected = true;
metrics.rejectedValidations++;
metrics.totalFailures++;
metrics.consecutiveFailures++;
// Apply reputation penalty
if (validationResult.reputationPenalty) {
newReputation = Math.max(
this.MIN_REPUTATION,
newReputation - validationResult.reputationPenalty
);
}
this.logger.warn('Validator evaluation rejected (no payment, no slashing)', {
validatorAddress,
reason: validationResult.reason,
reputationPenalty: validationResult.reputationPenalty,
oldReputation,
newReputation,
});
} else if (wasSuccessful) {
// SUCCESSFUL
metrics.successfulValidations++;
metrics.consecutiveFailures = 0; // Reset consecutive failures
// Apply reputation boost
newReputation = Math.min(
this.MAX_REPUTATION,
newReputation + this.SUCCESS_BOOST
);
// Recovery: If reputation was low, apply recovery rate
if (oldReputation < 50) {
const recovery = (50 - oldReputation) * this.RECOVERY_RATE;
newReputation = Math.min(
this.MAX_REPUTATION,
newReputation + recovery
);
}
} else {
// FAILED (disagreed with consensus, but still paid)
metrics.failedValidations++;
metrics.totalFailures++;
metrics.consecutiveFailures++;
// Apply reputation penalty
if (validationResult.reputationPenalty) {
newReputation = Math.max(
this.MIN_REPUTATION,
newReputation - validationResult.reputationPenalty
);
}
}
// Update metrics
metrics.reputation = newReputation;
metrics.totalValidations++;
metrics.accuracy = metrics.successfulValidations / metrics.totalValidations;
metrics.lastValidation = Date.now();
// Runtime invariant check: reputation must be in [0, 100]
const invariantChecker = getInvariantChecker();
invariantChecker.checkReputationBounds(
'ValidatorReputationService.updateReputation',
newReputation
);
if (!wasSuccessful) {
metrics.lastFailure = Date.now();
}
// Check if should ban
if (this.shouldBan(metrics, validationResult)) {
wasBanned = true;
const banDuration = validationResult.banDuration || this.DEFAULT_BAN_DURATION;
banUntil = Date.now() + banDuration;
metrics.isBanned = true;
metrics.banUntil = banUntil;
metrics.banReason = validationResult.reason || 'Low reputation or repeated failures';
this.logger.warn('Validator temporarily banned (no slashing)', {
validatorAddress,
reason: metrics.banReason,
banUntil: new Date(banUntil).toISOString(),
reputation: newReputation,
});
}
// Update timestamps
metrics.updatedAt = Date.now();
// Store updated metrics
this.reputationMetrics.set(validatorAddress, metrics);
return {
validatorAddress,
oldReputation,
newReputation,
reputationChange: newReputation - oldReputation,
wasRejected,
wasBanned,
banUntil,
};
}
/**
* Check if validator should be banned
*/
private shouldBan(
metrics: ValidatorReputationMetrics,
validationResult: ValidationResult
): boolean {
// Already banned
if (metrics.isBanned) {
if (metrics.banUntil && Date.now() < metrics.banUntil) {
return false; // Still banned
}
// Ban expired, check if should re-ban
metrics.isBanned = false;
metrics.banUntil = undefined;
}
// Check ban thresholds
if (metrics.reputation < this.BAN_THRESHOLD_REPUTATION) {
return true; // Reputation too low
}
if (metrics.consecutiveFailures >= this.BAN_THRESHOLD_FAILURES) {
return true; // Too many consecutive failures
}
if (validationResult.shouldBan) {
return true; // Explicit ban requested
}
return false;
}
/**
* Initialize new validator
*/
private initializeValidator(validatorAddress: string): ValidatorReputationMetrics {
const now = Date.now();
return {
validatorAddress,
reputation: this.DEFAULT_REPUTATION,
totalValidations: 0,
successfulValidations: 0,
failedValidations: 0,
rejectedValidations: 0,
accuracy: 0,
consistency: 1, // Start with perfect consistency
consecutiveFailures: 0,
totalFailures: 0,
isBanned: false,
lastValidation: now,
lastFailure: 0,
createdAt: now,
updatedAt: now,
// NEW: Initialize multi-dimensional risk vector
riskVector: {
exploration: 0.5,
consistency: 0.5,
reliability: 0.5,
diversity: 0.5,
surprisal: 0.5,
temporalStability: 0.5,
adversarialResistance: 0.5,
},
taskConditionedReputations: new Map(),
surprisalHistory: [],
averageSurprisal: 0.5,
temporalDecay: 1.0, // Start with no decay
lastDecayUpdate: now,
};
}
/**
* Get validator reputation
*/
getReputation(validatorAddress: string): ValidatorReputationMetrics | null {
return this.reputationMetrics.get(validatorAddress) || null;
}
/**
* Get reputation multiplier for reward calculation
*
* High reputation → 1.0x-2.0x multiplier
* Medium reputation → 0.5x-1.0x multiplier
* Low reputation → 0.1x-0.5x multiplier
* Very low reputation → 0x (banned)
*/
getReputationMultiplier(validatorAddress: string): number {
const metrics = this.reputationMetrics.get(validatorAddress);
if (!metrics) {
return 0.5; // Default for new validators
}
// Check if banned
if (metrics.isBanned) {
if (metrics.banUntil && Date.now() < metrics.banUntil) {
return 0; // Banned, no rewards
}
// Ban expired
metrics.isBanned = false;
metrics.banUntil = undefined;
}
// Calculate multiplier based on reputation
// Linear mapping: 0-100 reputation → 0.1-2.0 multiplier
const reputation = metrics.reputation;
if (reputation >= 80) {
// High reputation: 1.0x - 2.0x
return 1.0 + ((reputation - 80) / 20) * 1.0; // 1.0 to 2.0
} else if (reputation >= 50) {
// Medium reputation: 0.5x - 1.0x
return 0.5 + ((reputation - 50) / 30) * 0.5; // 0.5 to 1.0
} else if (reputation >= 20) {
// Low reputation: 0.1x - 0.5x
return 0.1 + ((reputation - 20) / 30) * 0.4; // 0.1 to 0.5
} else {
// Very low reputation: 0x (effectively banned)
return 0;
}
}
/**
* Check if validator is eligible to validate
*/
isEligible(validatorAddress: string): boolean {
const metrics = this.reputationMetrics.get(validatorAddress);
if (!metrics) {
return true; // New validators are eligible
}
// Check if banned
if (metrics.isBanned) {
if (metrics.banUntil && Date.now() < metrics.banUntil) {
return false; // Still banned
}
// Ban expired
metrics.isBanned = false;
metrics.banUntil = undefined;
return true;
}
// Check reputation threshold
if (metrics.reputation < this.BAN_THRESHOLD_REPUTATION) {
return false; // Reputation too low
}
return true;
}
/**
* Get all validator reputations
*/
getAllReputations(): Map<string, ValidatorReputationMetrics> {
return new Map(this.reputationMetrics);
}
/**
* Reset validator reputation (for testing/admin)
*/
resetReputation(validatorAddress: string): void {
this.reputationMetrics.delete(validatorAddress);
this.logger.info('Validator reputation reset', { validatorAddress });
}
// ========== NEW: Multi-Dimensional Risk Vector Methods ==========
/**
* Get multi-dimensional risk vector for validator
*/
getRiskVector(validatorAddress: string): RiskVector | null {
const metrics = this.reputationMetrics.get(validatorAddress);
return metrics?.riskVector || null;
}
/**
* Update risk vector based on validation result
*/
updateRiskVector(
validatorAddress: string,
validationResult: {
wasSuccessful: boolean;
outputDiversity?: number; // Diversity of outputs evaluated
surprisal?: number; // Surprisal score for this validation
adversarialTestResult?: AdversarialTestResult; // Result from adversarial test
},
networkState?: NetworkState
): RiskVector {
let metrics = this.reputationMetrics.get(validatorAddress);
if (!metrics) {
metrics = this.initializeValidator(validatorAddress);
}
if (!metrics.riskVector) {
metrics.riskVector = {
exploration: 0.5,
consistency: 0.5,
reliability: 0.5,
diversity: 0.5,
surprisal: 0.5,
temporalStability: 0.5,
adversarialResistance: 0.5,
};
}
const riskVector = metrics.riskVector;
const alpha = 0.1; // Learning rate
// Update reliability (success rate)
if (validationResult.wasSuccessful) {
riskVector.reliability = Math.min(1.0, riskVector.reliability + alpha * 0.1);
} else {
riskVector.reliability = Math.max(0.0, riskVector.reliability - alpha * 0.2);
}
// Update diversity
if (validationResult.outputDiversity !== undefined) {
riskVector.diversity = alpha * validationResult.outputDiversity + (1 - alpha) * riskVector.diversity;
}
// Update surprisal
if (validationResult.surprisal !== undefined) {
// Add to history
metrics.surprisalHistory.push(validationResult.surprisal);
if (metrics.surprisalHistory.length > this.SURPRISAL_HISTORY_SIZE) {
metrics.surprisalHistory.shift();
}
// Update average
metrics.averageSurprisal = metrics.surprisalHistory.reduce((a, b) => a + b, 0) / metrics.surprisalHistory.length;
riskVector.surprisal = metrics.averageSurprisal;
}
// Update adversarial resistance
if (validationResult.adversarialTestResult) {
if (validationResult.adversarialTestResult.passed) {
riskVector.adversarialResistance = Math.min(1.0, riskVector.adversarialResistance + alpha * 0.2);
} else {
riskVector.adversarialResistance = Math.max(0.0, riskVector.adversarialResistance - alpha * 0.3);
}
}
// Update exploration (based on network state)
if (networkState) {
// If network needs exploration, reward it
if (networkState.explorationBias > 0.5) {
riskVector.exploration = Math.min(1.0, riskVector.exploration + alpha * 0.1);
}
}
// Update temporal stability (consistency over time)
riskVector.temporalStability = this.calculateTemporalStability(metrics);
// Update consistency (variance in scores)
riskVector.consistency = metrics.consistency;
metrics.riskVector = riskVector;
this.reputationMetrics.set(validatorAddress, metrics);
// Runtime invariant check: risk vector dimensions must be in [0, 1]
const invariantChecker = getInvariantChecker();
invariantChecker.checkRiskVectorBounds(
'ValidatorReputationService.updateRiskVector',
riskVector
);
return riskVector;
}
/**
* Calculate temporal stability (consistency over time)
*/
private calculateTemporalStability(metrics: ValidatorReputationMetrics): number {
// Simple heuristic: more validations = more stable
if (metrics.totalValidations < 10) {
return 0.5; // Not enough data
}
// Stability increases with successful validations
const successRate = metrics.successfulValidations / metrics.totalValidations;
return successRate;
}
/**
* Calculate effective weight from risk vector
* Uses non-linear aggregation to prevent single-metric optimization
*/
calculateEffectiveWeight(riskVector: RiskVector, networkState?: NetworkState): number {
// Non-linear aggregation: geometric mean of key dimensions
const keyDimensions = [
riskVector.reliability,
riskVector.surprisal,
riskVector.adversarialResistance,
];
const geometricMean = Math.pow(
keyDimensions.reduce((a, b) => a * b, 1),
1 / keyDimensions.length
);
// Apply network state bias
let weight = geometricMean;
if (networkState) {
if (networkState.explorationBias > 0.5) {
// Reward exploration
weight = weight * (0.8 + 0.2 * riskVector.exploration);
} else {
// Reward reliability
weight = weight * (0.8 + 0.2 * riskVector.reliability);
}
}
return Math.max(0.0, Math.min(1.0, weight));
}
// ========== NEW: Task-Conditioned Reputation Methods ==========
/**
* Get task-conditioned reputation
*/
getTaskConditionedReputation(
validatorAddress: string,
networkId: string,
taskType: string
): TaskConditionedReputation | null {
const metrics = this.reputationMetrics.get(validatorAddress);
if (!metrics || !metrics.taskConditionedReputations) {
return null;
}
const key = `${networkId}:${taskType}`;
return metrics.taskConditionedReputations.get(key) || null;
}
/**
* Update task-conditioned reputation
*/
updateTaskConditionedReputation(
validatorAddress: string,
networkId: string,
taskType: string,
wasSuccessful: boolean
): TaskConditionedReputation {
let metrics = this.reputationMetrics.get(validatorAddress);
if (!metrics) {
metrics = this.initializeValidator(validatorAddress);
}
if (!metrics.taskConditionedReputations) {
metrics.taskConditionedReputations = new Map();
}
const key = `${networkId}:${taskType}`;
let taskRep = metrics.taskConditionedReputations.get(key);
if (!taskRep) {
// Initialize new task-conditioned reputation
taskRep = {
validatorAddress,
networkId,
taskType,
reputation: this.DEFAULT_REPUTATION,
riskVector: {
exploration: 0.5,
consistency: 0.5,
reliability: 0.5,
diversity: 0.5,
surprisal: 0.5,
temporalStability: 0.5,
adversarialResistance: 0.5,
},
totalTasks: 0,
successfulTasks: 0,
failedTasks: 0,
lastActivity: Date.now(),
temporalDecay: 1.0,
};
}
// Update reputation
if (wasSuccessful) {
taskRep.reputation = Math.min(100, taskRep.reputation + this.SUCCESS_BOOST);
taskRep.successfulTasks++;
} else {
taskRep.reputation = Math.max(0, taskRep.reputation - this.FAILURE_PENALTY);
taskRep.failedTasks++;
}
taskRep.totalTasks++;
taskRep.lastActivity = Date.now();
// Apply temporal decay
const daysSinceActivity = (Date.now() - taskRep.lastActivity) / (24 * 60 * 60 * 1000);
taskRep.temporalDecay = Math.pow(this.TEMPORAL_DECAY_RATE, daysSinceActivity);
metrics.taskConditionedReputations.set(key, taskRep);
this.reputationMetrics.set(validatorAddress, metrics);
return taskRep;
}
// ========== NEW: Surprisal Calculation Methods ==========
/**
* Calculate surprisal (unpredictability) for validator
* Higher surprisal = more unpredictable = better (prevents conservative farming)
*/
calculateSurprisal(
validatorAddress: string,
output: any,
taskType: string
): number {
const metrics = this.reputationMetrics.get(validatorAddress);
if (!metrics) {
return 0.5; // Default surprisal for new validators
}
// Simple surprisal calculation: entropy of output distribution
// In practice, this would use more sophisticated methods
const outputHash = this.hashOutput(output);
const hashEntropy = this.calculateEntropy(outputHash);
// Normalize to 0-1
return Math.min(1.0, hashEntropy / 8.0); // Assuming 8 bits of entropy max
}
/**
* Check if surprisal is too low (predictable = bad)
*/
isSurprisalTooLow(validatorAddress: string): boolean {
const metrics = this.reputationMetrics.get(validatorAddress);
if (!metrics) {
return false;
}
return metrics.averageSurprisal < this.MIN_SURPRISAL;
}
/**
* Apply surprisal penalty to reputation
*/
applySurprisalPenalty(validatorAddress: string): number {
const metrics = this.reputationMetrics.get(validatorAddress);
if (!metrics || !this.isSurprisalTooLow(validatorAddress)) {
return 0;
}
// Penalty proportional to how low surprisal is
const penalty = (this.MIN_SURPRISAL - metrics.averageSurprisal) * 10;
return Math.max(0, Math.min(20, penalty)); // Cap at 20 points
}
// ========== NEW: Temporal Decay Methods ==========
/**
* Apply temporal decay to reputation
* Reputation decays over time unless reinforced
*/
applyTemporalDecay(validatorAddress: string): void {
const metrics = this.reputationMetrics.get(validatorAddress);
if (!metrics) {
return;
}
const now = Date.now();
const timeSinceLastUpdate = now - metrics.lastDecayUpdate;
if (timeSinceLastUpdate < this.DECAY_UPDATE_INTERVAL) {
return; // Too soon to update
}
// Calculate decay
const daysSinceUpdate = timeSinceLastUpdate / (24 * 60 * 60 * 1000);
const decayFactor = Math.pow(this.TEMPORAL_DECAY_RATE, daysSinceUpdate);
metrics.temporalDecay = decayFactor;
metrics.lastDecayUpdate = now;
// Apply decay to reputation (soft decay, not hard)
// Only decay if no recent activity
const daysSinceActivity = (now - metrics.lastValidation) / (24 * 60 * 60 * 1000);
if (daysSinceActivity > 7) {
// Decay reputation if inactive for 7+ days
const decayAmount = (1 - decayFactor) * 5; // Max 5 points decay
metrics.reputation = Math.max(this.MIN_REPUTATION, metrics.reputation - decayAmount);
}
this.reputationMetrics.set(validatorAddress, metrics);
}
// ========== Helper Methods ==========
private hashOutput(output: any): string {
const str = JSON.stringify(output);
let hash = 0;
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash;
}
return Math.abs(hash).toString(16);
}
private calculateEntropy(hash: string): number {
// Simple entropy calculation based on character distribution
const charCounts = new Map<string, number>();
for (const char of hash) {
charCounts.set(char, (charCounts.get(char) || 0) + 1);
}
let entropy = 0;
const length = hash.length;
for (const count of charCounts.values()) {
const p = count / length;
entropy -= p * Math.log2(p);
}
return entropy;
}
}