-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSybilResistanceServiceRefactored.ts
More file actions
356 lines (306 loc) · 12 KB
/
SybilResistanceServiceRefactored.ts
File metadata and controls
356 lines (306 loc) · 12 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
/**
* Sybil Resistance Service Refactored
*
* Database-agnostic version using repository pattern
* Implements proof-of-stake and reputation-based Sybil resistance
*/
import { ILogger } from './utils/ILogger';
import { IValidatorRepository, ValidatorData } from './interfaces/IValidatorRepository';
import { OnChainValidatorService } from './OnChainValidatorService';
import { PriceOracleService } from './PriceOracleService';
import { NetworkManifest, SupportedChain } from './types';
export interface SybilResistanceConfig {
minStakeUSD: number;
minReputation: number;
maxValidatorsPerAddress: number;
reputationDecayRate: number;
stakeLockupPeriod: number;
}
export interface ValidatorQualification {
qualified: boolean;
reasons: string[];
stakeUSD: number;
reputation: number;
validatorCount: number;
}
export interface SybilResistanceServiceDependencies {
validatorRepository: IValidatorRepository;
onChainValidatorService: OnChainValidatorService;
priceOracleService: PriceOracleService;
}
export class SybilResistanceServiceRefactored {
private logger: ILogger;
private validatorRepo: IValidatorRepository;
private onChainValidatorService: OnChainValidatorService;
private priceOracle: PriceOracleService;
private config: SybilResistanceConfig;
constructor(
logger: ILogger,
dependencies: SybilResistanceServiceDependencies,
config?: Partial<SybilResistanceConfig>
) {
this.logger = logger;
this.validatorRepo = dependencies.validatorRepository;
this.onChainValidatorService = dependencies.onChainValidatorService;
this.priceOracle = dependencies.priceOracleService;
this.config = {
minStakeUSD: 100,
minReputation: 70,
maxValidatorsPerAddress: 1,
reputationDecayRate: 0.01,
stakeLockupPeriod: 86400 * 7,
...config,
};
}
/**
* Check if validator qualifies (Sybil resistance)
* Uses repository instead of direct Prisma calls
*/
async checkValidatorQualification(
validatorAddress: string,
networkId: string,
validatorRegistryAddress?: string,
chain?: string
): Promise<ValidatorQualification> {
const reasons: string[] = [];
// Try on-chain first (if registry address provided)
if (validatorRegistryAddress && chain) {
try {
const onChainQualification = await this.checkOnChainQualification(
validatorAddress,
validatorRegistryAddress,
chain
);
if (onChainQualification.qualified) {
return onChainQualification;
}
reasons.push(...onChainQualification.reasons);
} catch (error) {
this.logger.warn('On-chain qualification check failed, falling back to database', {
validatorAddress,
error
});
}
}
// Fallback to database (using repository)
const validator = await this.validatorRepo.findByAddress(validatorAddress, networkId);
if (!validator) {
return {
qualified: false,
reasons: ['Validator not registered'],
stakeUSD: 0,
reputation: 0,
validatorCount: 0,
};
}
// Check 1: Minimum stake
const stakeUSD = parseFloat(validator.stake);
if (stakeUSD < this.config.minStakeUSD) {
reasons.push(`Insufficient stake: $${stakeUSD.toFixed(2)} < $${this.config.minStakeUSD}`);
}
// Check 2: Minimum reputation
if (validator.reputation < this.config.minReputation) {
reasons.push(`Insufficient reputation: ${validator.reputation}/100 < ${this.config.minReputation}/100`);
}
// Check 3: Validator is active
if (!validator.isActive) {
reasons.push('Validator is not active');
}
// Check 4: Validator is not banned
if (validator.isBanned) {
reasons.push('Validator is banned');
}
const qualified = reasons.length === 0;
return {
qualified,
reasons,
stakeUSD,
reputation: validator.reputation,
validatorCount: 1,
};
}
/**
* Check qualification using on-chain validator registry
*/
private async checkOnChainQualification(
validatorAddress: string,
validatorRegistryAddress: string,
chain: string
): Promise<ValidatorQualification> {
const reasons: string[] = [];
try {
const { ethers } = await import('ethers');
// Get RPC URL
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
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];
const reputation = validatorInfo[4];
// Convert stake to USD
const stakeAmount = parseFloat(ethers.formatEther(stake));
let nativeTokenPrice: number;
try {
nativeTokenPrice = await this.priceOracle.getNativeTokenPriceUSD(chain as SupportedChain);
if (nativeTokenPrice <= 0 || !isFinite(nativeTokenPrice)) {
throw new Error('Invalid price from oracle');
}
} catch (error) {
// Fallback prices
const fallbackPrices: Record<string, number> = {
ethereum: 2000,
polygon: 0.5,
bsc: 300,
arbitrum: 2000,
base: 2000,
avalanche: 20,
optimism: 2000,
};
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}`);
}
if (reputation < this.config.minReputation) {
reasons.push(`Insufficient on-chain reputation: ${reputation}/100 < ${this.config.minReputation}/100`);
}
// Check P2P endpoint
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,
};
} catch (error) {
this.logger.error('Failed to check on-chain qualification', {
validatorAddress,
validatorRegistryAddress,
error
});
throw error;
}
}
/**
* Update validator reputation after validation
* Uses repository instead of direct Prisma calls
*/
async updateReputationAfterValidation(
validatorAddress: string,
networkId: string,
wasCorrect: boolean,
consensusMatch: boolean
): Promise<number> {
try {
const validator = await this.validatorRepo.findByAddress(validatorAddress, networkId);
if (!validator) {
throw new Error('Validator not found');
}
let newReputation = validator.reputation;
if (wasCorrect && consensusMatch) {
newReputation = Math.min(100, validator.reputation + 1);
} else if (!wasCorrect) {
newReputation = Math.max(0, validator.reputation - 5);
}
// Apply reputation decay
const decay = newReputation * this.config.reputationDecayRate;
newReputation = Math.max(0, newReputation - decay);
// Update reputation via repository
await this.validatorRepo.updateReputation(validatorAddress, networkId, newReputation);
// Update last active timestamp
await this.validatorRepo.updateLastActive(validatorAddress, networkId);
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
* Uses repository instead of direct Prisma calls
*/
async checkNetworkRequirements(
validatorAddress: string,
networkId: string,
requirements: {
minStake?: string;
minReputation?: number;
requiredChains?: string[];
}
): Promise<{ qualified: boolean; reasons: string[] }> {
const reasons: string[] = [];
const validator = await this.validatorRepo.findByAddress(validatorAddress, networkId);
if (!validator) {
return {
qualified: false,
reasons: ['Validator not found'],
};
}
// Check stake requirement
if (requirements.minStake) {
const stakeUSD = parseFloat(validator.stake);
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) {
if (validator.reputation < requirements.minReputation) {
reasons.push(`Network requires minimum reputation: ${requirements.minReputation}, got ${validator.reputation}`);
}
}
return {
qualified: reasons.length === 0,
reasons,
};
}
}