-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSybilResistanceService.ts
More file actions
512 lines (452 loc) · 17.2 KB
/
SybilResistanceService.ts
File metadata and controls
512 lines (452 loc) · 17.2 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
/**
* Sybil Resistance Service
*
* Implements proof-of-stake and reputation-based Sybil resistance
* Prevents validator cartels and ensures economic security
*/
import { ILogger } from './utils/ILogger';
import { PrismaClient } from '@prisma/client';
import { PriceOracleService } from './PriceOracleService';
import { SupportedChain } from './types';
export interface SybilResistanceConfig {
minStakeUSD: number; // Minimum stake in USD to become validator
minReputation: number; // Minimum reputation to participate (0-100)
maxValidatorsPerAddress: number; // Maximum validator identities per address (default: 1)
reputationDecayRate: number; // Reputation decay per period (0-1)
stakeLockupPeriod: number; // Minimum stake lockup period (seconds)
}
export interface ValidatorQualification {
qualified: boolean;
reasons: string[];
stakeUSD: number;
reputation: number;
validatorCount: number;
}
export class SybilResistanceService {
private logger: ILogger;
private prisma: PrismaClient;
private config: SybilResistanceConfig;
private priceOracle: PriceOracleService;
constructor(prisma: PrismaClient, logger?: Logger, config?: Partial<SybilResistanceConfig>) {
this.prisma = prisma;
this.logger = logger || new Logger('SybilResistanceService');
this.priceOracle = new PriceOracleService(this.logger);
this.config = {
minStakeUSD: 100, // $100 minimum stake
minReputation: 70, // 70/100 minimum reputation
maxValidatorsPerAddress: 1, // One validator per address
reputationDecayRate: 0.01, // 1% decay per period
stakeLockupPeriod: 86400 * 7, // 7 days lockup
...config,
};
}
/**
* Check if validator qualifies (Sybil resistance)
* FIX #2: Now uses on-chain data first, falls back to database
*/
async checkValidatorQualification(
validatorAddress: string,
networkId: string,
validatorRegistryAddress?: string,
chain?: string
): Promise<ValidatorQualification> {
const reasons: string[] = [];
// FIX #2: Try on-chain first (if registry address provided)
if (validatorRegistryAddress && chain) {
try {
// Use on-chain validator registry
const onChainQualification = await this.checkOnChainQualification(
validatorAddress,
validatorRegistryAddress,
chain
);
if (onChainQualification.qualified) {
return onChainQualification;
}
// If on-chain check fails, add reasons and continue with database fallback
reasons.push(...onChainQualification.reasons);
} catch (error) {
this.logger.warn('On-chain qualification check failed, falling back to database', {
validatorAddress,
error
});
}
}
// Fallback to database (for backward compatibility)
// Check 1: Minimum stake
const stakeUSD = await this.getValidatorStakeUSD(validatorAddress, networkId);
if (stakeUSD < this.config.minStakeUSD) {
reasons.push(`Insufficient stake: $${stakeUSD.toFixed(2)} < $${this.config.minStakeUSD}`);
}
// Check 2: Minimum reputation
const reputation = await this.getValidatorReputation(validatorAddress);
if (reputation < this.config.minReputation) {
reasons.push(`Insufficient reputation: ${reputation}/100 < ${this.config.minReputation}/100`);
}
// Check 3: Maximum validators per address (Sybil resistance)
const validatorCount = await this.getValidatorCountForAddress(validatorAddress);
if (validatorCount >= this.config.maxValidatorsPerAddress) {
reasons.push(`Too many validator identities: ${validatorCount} >= ${this.config.maxValidatorsPerAddress}`);
}
const qualified = reasons.length === 0;
return {
qualified,
reasons,
stakeUSD,
reputation,
validatorCount,
};
}
/**
* FIX #2: Check qualification using on-chain validator registry
*/
private async checkOnChainQualification(
validatorAddress: string,
validatorRegistryAddress: string,
chain: string
): Promise<ValidatorQualification> {
const reasons: string[] = [];
try {
// Import ethers dynamically (to avoid issues if not available)
const { ethers } = await import('ethers');
// Get RPC URL directly (chain service doesn't expose getProvider in interface)
const rpcUrls: Record<string, string> = {
ethereum: process.env.ETHEREUM_RPC_URL || '',
polygon: process.env.POLYGON_RPC_URL || '',
bsc: process.env.BSC_RPC_URL || '',
arbitrum: process.env.ARBITRUM_RPC_URL || '',
base: process.env.BASE_RPC_URL || '',
avalanche: process.env.AVALANCHE_RPC_URL || '',
optimism: process.env.OPTIMISM_RPC_URL || '',
};
const rpcUrl = rpcUrls[chain.toLowerCase()];
if (!rpcUrl) {
throw new Error(`No RPC URL configured for chain: ${chain}`);
}
const provider = new ethers.JsonRpcProvider(rpcUrl);
// Load ValidatorRegistry contract ABI (simplified)
const validatorRegistryABI = [
'function getValidator(address) view returns (address, uint256, uint256, bool, uint256, string, bytes32)',
'function isValidator(address) view returns (bool)',
'function hasP2PEndpoint(address) view returns (bool)',
];
const registryContract = new ethers.Contract(
validatorRegistryAddress,
validatorRegistryABI,
provider
);
// Check if validator is registered
const isRegistered = await registryContract.isValidator(validatorAddress);
if (!isRegistered) {
reasons.push('Validator not registered on-chain');
return {
qualified: false,
reasons,
stakeUSD: 0,
reputation: 0,
validatorCount: 0,
};
}
// Get validator info
const validatorInfo = await registryContract.getValidator(validatorAddress);
const stake = validatorInfo[1]; // stake
const reputation = validatorInfo[4]; // reputation (0-100)
// Convert stake to USD using price oracle (with fallback)
const stakeAmount = parseFloat(ethers.formatEther(stake));
let nativeTokenPrice: number;
try {
nativeTokenPrice = await this.priceOracle.getNativeTokenPriceUSD(chain as SupportedChain);
// If price is 0 or invalid, use fallback
if (nativeTokenPrice <= 0 || !isFinite(nativeTokenPrice)) {
throw new Error('Invalid price from oracle');
}
} catch (error) {
this.logger.warn('Price oracle failed, using conservative fallback', { chain, error });
// FALLBACK: Use conservative default prices (prevents Sybil resistance from breaking)
const fallbackPrices: Record<string, number> = {
ethereum: 2000,
polygon: 0.5,
bsc: 300,
arbitrum: 2000,
base: 2000,
avalanche: 20,
optimism: 2000,
solana: 100,
tron: 0.1,
};
nativeTokenPrice = fallbackPrices[chain.toLowerCase()] || 1;
this.logger.warn('Using fallback price for Sybil resistance', { chain, fallbackPrice: nativeTokenPrice });
}
const stakeUSD = stakeAmount * nativeTokenPrice;
if (stakeUSD < this.config.minStakeUSD) {
reasons.push(`Insufficient on-chain stake: $${stakeUSD.toFixed(2)} < $${this.config.minStakeUSD}`);
}
// Check reputation
if (reputation < this.config.minReputation) {
reasons.push(`Insufficient on-chain reputation: ${reputation}/100 < ${this.config.minReputation}/100`);
}
// Check P2P endpoint (required for Phase 5)
const hasP2P = await registryContract.hasP2PEndpoint(validatorAddress);
if (!hasP2P) {
reasons.push('Validator missing P2P endpoint');
}
const qualified = reasons.length === 0;
return {
qualified,
reasons,
stakeUSD,
reputation: Number(reputation),
validatorCount: 1, // On-chain, one address = one validator
};
} catch (error) {
this.logger.error('Failed to check on-chain qualification', {
validatorAddress,
validatorRegistryAddress,
error
});
throw error;
}
}
/**
* Get validator total stake in USD (across all chains)
*/
private async getValidatorStakeUSD(validatorAddress: string, networkId: string): Promise<number> {
try {
// Query validator stakes from database
const validator = await this.prisma.codeValidator.findUnique({
where: { validatorId: validatorAddress },
include: {
stakes: true, // Get all stakes (networkId filtering not available in schema)
},
});
if (!validator || !validator.stakes || validator.stakes.length === 0) {
return 0;
}
// Filter stakes by networkId manually (since schema doesn't have networkId field)
// For now, use all stakes - networkId filtering can be added to schema later
const relevantStakes = validator.stakes;
// Convert all stakes to USD using price oracle
let totalStakeUSD = 0;
for (const stake of relevantStakes) {
// Use cached USD value if available
if (stake.amountUSD && stake.amountUSD > 0) {
totalStakeUSD += stake.amountUSD;
continue;
}
// Otherwise, fetch price and convert
const amount = parseFloat(stake.amount.toString());
let stakeUSD = 0;
if (stake.tokenAddress) {
// ERC20 token - fetch price (with fallback)
try {
stakeUSD = await this.priceOracle.convertToUSD(
stake.amount.toString(),
stake.tokenAddress,
stake.blockchain as SupportedChain,
stake.tokenSymbol
);
// If price is 0 or invalid, use fallback
if (stakeUSD <= 0 || !isFinite(stakeUSD)) {
throw new Error('Invalid price from oracle');
}
} catch (error) {
this.logger.warn('Price oracle failed for token, using conservative fallback', {
tokenAddress: stake.tokenAddress,
chain: stake.blockchain,
error
});
// FALLBACK: Use conservative estimate (1 USD per token minimum)
stakeUSD = amount * 1; // Conservative: assume 1 USD per token
this.logger.warn('Using fallback price for token stake', {
tokenAddress: stake.tokenAddress,
fallbackStakeUSD: stakeUSD
});
}
} else {
// Native token - fetch native token price (with fallback)
try {
const nativePrice = await this.priceOracle.getNativeTokenPriceUSD(
stake.blockchain as SupportedChain
);
if (nativePrice <= 0 || !isFinite(nativePrice)) {
throw new Error('Invalid price from oracle');
}
stakeUSD = amount * nativePrice;
} catch (error) {
this.logger.warn('Price oracle failed for native token, using fallback', {
chain: stake.blockchain,
error
});
// FALLBACK: Use conservative default prices
const fallbackPrices: Record<string, number> = {
ethereum: 2000,
polygon: 0.5,
bsc: 300,
arbitrum: 2000,
base: 2000,
avalanche: 20,
optimism: 2000,
solana: 100,
tron: 0.1,
};
const fallbackPrice = fallbackPrices[stake.blockchain.toLowerCase()] || 1;
stakeUSD = amount * fallbackPrice;
this.logger.warn('Using fallback price for native token stake', {
chain: stake.blockchain,
fallbackPrice,
fallbackStakeUSD: stakeUSD
});
}
}
totalStakeUSD += stakeUSD;
// Update database with USD value for future use
try {
await this.prisma.validatorStake.update({
where: { id: stake.id },
data: { amountUSD: stakeUSD },
});
} catch (error) {
this.logger.debug('Failed to update stake USD value', { stakeId: stake.id, error });
// Don't fail - USD update is optional
}
}
return totalStakeUSD;
} catch (error) {
this.logger.error('Failed to get validator stake', { validatorAddress, error });
return 0;
}
}
/**
* Get validator reputation
*/
private async getValidatorReputation(validatorAddress: string): Promise<number> {
try {
const validator = await this.prisma.codeValidator.findUnique({
where: { validatorId: validatorAddress },
});
if (!validator) {
return 0; // New validators start at 0
}
return validator.reputation;
} catch (error) {
this.logger.error('Failed to get validator reputation', { validatorAddress, error });
return 0;
}
}
/**
* Get number of validator identities for an address
* Sybil resistance: prevent one address from controlling multiple validators
*/
private async getValidatorCountForAddress(address: string): Promise<number> {
try {
const count = await this.prisma.codeValidator.count({
where: {
validatorId: {
contains: address.toLowerCase(),
},
},
});
return count;
} catch (error) {
this.logger.error('Failed to get validator count', { address, error });
return 0;
}
}
/**
* Update validator reputation after successful validation
* Reputation increases with correct validations
*/
async updateReputationAfterValidation(
validatorAddress: string,
wasCorrect: boolean,
consensusMatch: boolean
): Promise<number> {
try {
const validator = await this.prisma.codeValidator.findUnique({
where: { validatorId: validatorAddress },
});
if (!validator) {
throw new Error('Validator not found');
}
let newReputation = validator.reputation;
if (wasCorrect && consensusMatch) {
// Increase reputation for correct validations
newReputation = Math.min(100, validator.reputation + 1);
} else if (!wasCorrect) {
// Decrease reputation for incorrect validations
newReputation = Math.max(0, validator.reputation - 5);
}
// Apply reputation decay (prevent reputation inflation)
const decay = newReputation * this.config.reputationDecayRate;
newReputation = Math.max(0, newReputation - decay);
await this.prisma.codeValidator.update({
where: { validatorId: validatorAddress },
data: {
reputation: newReputation,
totalValidations: validator.totalValidations + 1,
correctValidations: wasCorrect ? validator.correctValidations + 1 : validator.correctValidations,
incorrectValidations: !wasCorrect ? validator.incorrectValidations + 1 : validator.incorrectValidations,
lastSeen: new Date(),
},
});
this.logger.info('Validator reputation updated', {
validatorAddress,
oldReputation: validator.reputation,
newReputation,
wasCorrect,
});
return newReputation;
} catch (error) {
this.logger.error('Failed to update validator reputation', { validatorAddress, error });
throw error;
}
}
/**
* Check if validator meets network-specific requirements
*/
async checkNetworkRequirements(
validatorAddress: string,
networkId: string,
requirements: {
minStake?: string;
minReputation?: number;
requiredChains?: string[];
}
): Promise<{ qualified: boolean; reasons: string[] }> {
const reasons: string[] = [];
// Check stake requirement
if (requirements.minStake) {
const stakeUSD = await this.getValidatorStakeUSD(validatorAddress, networkId);
const minStake = parseFloat(requirements.minStake);
if (stakeUSD < minStake) {
reasons.push(`Network requires minimum stake: ${requirements.minStake}, got ${stakeUSD}`);
}
}
// Check reputation requirement
if (requirements.minReputation !== undefined) {
const reputation = await this.getValidatorReputation(validatorAddress);
if (reputation < requirements.minReputation) {
reasons.push(`Network requires minimum reputation: ${requirements.minReputation}, got ${reputation}`);
}
}
// Check chain support
if (requirements.requiredChains && requirements.requiredChains.length > 0) {
const validator = await this.prisma.codeValidator.findUnique({
where: { validatorId: validatorAddress },
});
if (validator) {
const supportedChains = JSON.parse(validator.supportedBlockchains) as string[];
const missingChains = requirements.requiredChains.filter(
chain => !supportedChains.includes(chain)
);
if (missingChains.length > 0) {
reasons.push(`Network requires chains: ${missingChains.join(', ')}, validator supports: ${supportedChains.join(', ')}`);
}
}
}
return {
qualified: reasons.length === 0,
reasons,
};
}
}