-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProtocolServiceRefactored.ts
More file actions
537 lines (479 loc) · 21.4 KB
/
ProtocolServiceRefactored.ts
File metadata and controls
537 lines (479 loc) · 21.4 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
/**
* Refactored Protocol Service
*
* Core orchestration service for Tenseuron Protocol with dependency injection
* Uses interface-based architecture for database-agnostic and runtime-agnostic operation
*/
import { ILogger } from './utils/ILogger';
import {
NetworkManifest,
NetworkCreationRequest,
NetworkDeploymentStatus,
SupportedChain,
} from './types';
import { NetworkManifestGenerator } from './NetworkManifestGenerator';
import { DecentralizedRegistryService } from './DecentralizedRegistryService';
import { SettlementService } from './SettlementService';
import { ScamDefenseService } from './ScamDefenseService';
import { RiskScoringService, RiskParameters } from './RiskScoringService';
import { MoneyFlowService } from './MoneyFlowService';
import { PenaltyConfigValidator } from './PenaltyConfigValidator';
import {
INetworkRepository,
ITaskRepository,
IValidatorRepository,
IStorageProvider,
IBlockchainProvider,
IAIModuleRepository,
ICreatorReputationService,
} from './interfaces';
export interface ProtocolServiceDependencies {
// Core repositories (database-agnostic)
networkRepo: INetworkRepository;
taskRepo?: ITaskRepository;
validatorRepo?: IValidatorRepository;
aiModuleRepo: IAIModuleRepository;
creatorReputationService: ICreatorReputationService;
// Storage and blockchain providers
storage: IStorageProvider;
blockchain: IBlockchainProvider;
// Other services
decentralizedRegistry: DecentralizedRegistryService;
settlementService: SettlementService;
scamDefenseService: ScamDefenseService;
riskScoringService: RiskScoringService;
moneyFlowService: MoneyFlowService;
}
export class ProtocolServiceRefactored {
private logger: ILogger;
private networkRepo: INetworkRepository;
private taskRepo?: ITaskRepository;
private validatorRepo?: IValidatorRepository;
private aiModuleRepo: IAIModuleRepository;
private creatorReputationService: ICreatorReputationService;
private storage: IStorageProvider;
private blockchain: IBlockchainProvider;
private decentralizedRegistry: DecentralizedRegistryService;
private settlementService: SettlementService;
private scamDefenseService: ScamDefenseService;
private riskScoringService: RiskScoringService;
private moneyFlowService: MoneyFlowService;
constructor(logger: ILogger, dependencies: ProtocolServiceDependencies) {
this.logger = logger;
this.networkRepo = dependencies.networkRepo;
this.taskRepo = dependencies.taskRepo;
this.validatorRepo = dependencies.validatorRepo;
this.aiModuleRepo = dependencies.aiModuleRepo;
this.creatorReputationService = dependencies.creatorReputationService;
this.storage = dependencies.storage;
this.blockchain = dependencies.blockchain;
this.decentralizedRegistry = dependencies.decentralizedRegistry;
this.settlementService = dependencies.settlementService;
this.scamDefenseService = dependencies.scamDefenseService;
this.riskScoringService = dependencies.riskScoringService;
this.moneyFlowService = dependencies.moneyFlowService;
}
/**
* Create a new AI Network using the Tenseuron Protocol
* This is the main entry point for network creation
*/
async createNetwork(request: NetworkCreationRequest): Promise<{
networkId: string;
manifest: NetworkManifest;
deploymentStatus: NetworkDeploymentStatus;
creationFees: {
bond: string;
deploymentCost: string;
registryFee: string;
total: string;
};
}> {
this.logger.info('Creating new AI Network', {
name: request.name,
moduleId: request.moduleId,
});
try {
// Step 1: Load AI Module and pre-fill schemas
const module = await this.aiModuleRepo.getModuleById(request.moduleId);
if (!module) {
throw new Error(`AI Module not found: ${request.moduleId}`);
}
if (!module.isActive) {
throw new Error(`AI Module is not active: ${request.moduleId}`);
}
this.logger.info('AI Module loaded', {
moduleId: module.moduleId,
name: module.name,
category: module.category,
});
// Pre-fill task schemas from module
const taskInputSchema = request.taskInputSchema || module.taskInputSchema;
const taskOutputSchema = request.taskOutputSchema || module.taskOutputSchema;
const taskTimeout = request.taskTimeout || module.taskTimeout;
const scoringType = request.scoringType || (module.scoringType as any);
const scoringModuleHash = request.scoringModuleHash || module.scoringModuleHash || '';
const scoringModuleUrl = request.scoringModuleUrl || module.scoringModuleUrl || '';
const evaluationMode = request.evaluationMode || module.evaluationMode;
// Step 2: Calculate risk score and required costs
const riskParams: RiskParameters = {
payoutCap: request.riskParameters.payoutCap,
settlementDelay: request.riskParameters.settlementDelay,
taskSchemaFixed: request.riskParameters.taskSchemaFixed,
customScoring: request.riskParameters.customScoring,
instantPayout: request.riskParameters.instantPayout,
singleValidator: request.riskParameters.singleValidator,
nonDeterministic: request.riskParameters.nonDeterministic,
validatorSelfSelect: request.riskParameters.validatorSelfSelect,
maxPayoutPerTask: request.riskParameters.maxPayoutPerTask,
minValidators: request.minValidators,
consensusThreshold: request.consensusThreshold,
disputeWindow: request.disputeWindow,
stakeRequired: request.stakeRequired,
};
// Step 3: Check creator reputation
const creationCheck = await this.creatorReputationService.canCreateNetwork(
request.creatorAddress
);
if (!creationCheck.allowed) {
throw new Error(`Network creation not allowed: ${creationCheck.reason}`);
}
const reputation = await this.creatorReputationService.getCreatorReputation(
request.creatorAddress
);
// Step 4: Validate inputs
const { InputValidator } = await import('./InputValidator');
const inputValidator = new InputValidator(this.logger);
const inputValidation = await inputValidator.validateNetworkCreationRequest(request);
if (!inputValidation.valid) {
throw new Error(`Invalid network creation request: ${inputValidation.errors.join(', ')}`);
}
// Step 5: Calculate risk and costs
const riskScore = this.riskScoringService.calculateRiskScore(riskParams);
const baseRequiredCosts = this.riskScoringService.calculateRequiredCosts(
riskScore,
request.riskParameters.maxPayoutPerTask,
false
);
// Validate penalty configuration
if (request.penaltyConfig) {
const penaltyValidator = new PenaltyConfigValidator(this.logger);
const penaltyValidation = penaltyValidator.validate(request.penaltyConfig);
if (!penaltyValidation.valid) {
throw new Error(
`Invalid penalty configuration: ${penaltyValidation.errors.join(', ')}`
);
}
}
const hasCustomPenaltyConfig =
request.penaltyConfig &&
request.penaltyConfig.mechanism !== 'none' &&
!this.isDefaultPenaltyConfig(request.penaltyConfig, baseRequiredCosts);
const requiredCosts = this.riskScoringService.calculateRequiredCosts(
riskScore,
request.riskParameters.maxPayoutPerTask,
hasCustomPenaltyConfig
);
// Step 6: Validate money flow
const moneyFlowValidation = this.moneyFlowService.validateMoneyFlowConfig(
request.moneyFlow
);
if (!moneyFlowValidation.valid) {
throw new Error(
`Invalid money flow configuration: ${moneyFlowValidation.errors.join(', ')}`
);
}
// Step 7: Determine settlement chain
const settlementChain: SupportedChain = request.token ? 'polygon' : 'polygon';
// Step 8: Calculate creation fees
const creationFees = this.scamDefenseService.calculateCreationFees(
settlementChain,
requiredCosts
);
const estimatedNetworkValue = parseFloat(requiredCosts.requiredStake) * 10;
const requiredBond = await this.creatorReputationService.calculateRequiredBond(
request.creatorAddress,
estimatedNetworkValue.toString(),
0.01
);
const totalFees = parseFloat(creationFees.total) + parseFloat(requiredBond);
// Step 9: Generate network manifest
const manifestRequest = {
...request,
moduleId: request.moduleId,
taskInputSchema,
taskOutputSchema,
taskTimeout,
scoringType,
scoringModuleHash,
scoringModuleUrl,
evaluationMode,
settlementChain,
};
const manifest = NetworkManifestGenerator.generateManifest(manifestRequest, module);
manifest.riskAssessment = {
totalRisk: riskScore.totalRisk,
riskCategory: riskScore.riskCategory,
requiredCosts: requiredCosts,
};
const validation = NetworkManifestGenerator.validateManifest(manifest);
if (!validation.valid) {
throw new Error(`Invalid manifest: ${validation.errors.join(', ')}`);
}
// Step 10: Deploy network token (if applicable)
let tokenAddress: string | undefined;
let contractAddress: string | undefined;
const isEarnOnlyToken = request.token?.design === 'earn-only';
if (request.token && !isEarnOnlyToken) {
this.logger.info('Deploying network token', {
networkId: manifest.networkId,
chain: settlementChain,
});
tokenAddress = await this.settlementService.deployNetworkToken({
networkId: manifest.networkId,
chain: settlementChain,
token: request.token,
});
this.logger.info('Network token deployed', {
networkId: manifest.networkId,
tokenAddress,
});
}
// Step 11: Deploy settlement contract
if (request.settlementMode === 'escrow') {
this.logger.info('Deploying escrow contract', {
networkId: manifest.networkId,
chain: settlementChain,
});
contractAddress = await this.settlementService.deployEscrowContract({
networkId: manifest.networkId,
chain: settlementChain,
disputeWindow: request.disputeWindow,
minValidators: request.minValidators,
consensusThreshold: Math.round(request.consensusThreshold * 10000),
networkToken: tokenAddress || undefined,
validatorRegistry: undefined,
challengeOracle: request.oracleAddress || undefined,
creatorAddress: request.creatorAddress,
purposeBoundSinks: this.moneyFlowService.getPurposeBoundSinksAddress(),
usageCutEnabled: request.moneyFlow.usageCut.enabled,
usageCutPercentage: request.moneyFlow.usageCut.percentage * 100,
usageCutMin: request.moneyFlow.usageCut.minCut,
usageCutMax: request.moneyFlow.usageCut.maxCut,
validatorPaymentEnabled: request.moneyFlow.validatorPayment.enabled,
validatorPaymentPercentage: request.moneyFlow.validatorPayment.percentage * 100,
validatorPaymentMin: request.moneyFlow.validatorPayment.minPayment,
validatorPaymentMax: request.moneyFlow.validatorPayment.maxPayment,
penaltyConfig: request.penaltyConfig || {
mechanism: requiredCosts.slashingEnabled ? 'slashing' : 'none',
slashing: requiredCosts.slashingEnabled
? {
enabled: true,
rate: requiredCosts.slashingRate,
minStakeRequired: request.stakeRequired || '0',
cooldownPeriod: 7 * 24 * 60 * 60,
}
: undefined,
},
});
this.logger.info('Escrow contract deployed', {
networkId: manifest.networkId,
contractAddress,
});
}
// Step 12: Update manifest with deployment results
const updatedManifest = NetworkManifestGenerator.updateManifestWithDeployment(
manifest,
contractAddress,
tokenAddress
);
// Step 13: Upload manifest to storage (IPFS/R2/etc)
const manifestCid = await this.storage.upload(updatedManifest, {
name: `network-${updatedManifest.networkId}.json`,
type: 'application/json',
});
const finalManifest = NetworkManifestGenerator.updateManifestWithDeployment(
updatedManifest,
contractAddress,
tokenAddress,
manifestCid
);
this.logger.info('Network manifest uploaded to storage', {
networkId: finalManifest.networkId,
manifestCid,
storageType: this.storage.getType(),
});
// Step 14: Register network in local index
await this.decentralizedRegistry.registerNetworkInLocalIndex(finalManifest);
// Step 15: Store network in database using abstraction layer
await this.networkRepo.create({
networkId: finalManifest.networkId,
name: finalManifest.name,
description: finalManifest.description,
category: finalManifest.category,
creatorAddress: finalManifest.creatorAddress,
manifestCid,
contractAddress,
validatorRegistryAddress: undefined,
settlementChain,
status: 'deployed',
moduleId: request.moduleId,
createdAt: new Date(),
updatedAt: new Date(),
});
this.logger.info('Network stored in database', {
networkId: finalManifest.networkId,
databaseType: 'abstraction-layer',
});
// Step 16: Create deployment status
const deploymentStatus: NetworkDeploymentStatus = {
networkId: finalManifest.networkId,
status: 'deployed',
progress: 100,
steps: [
{ id: 'manifest', name: 'Generate Manifest', status: 'completed' },
{
id: 'contract',
name: 'Deploy Settlement Contract',
status: contractAddress ? 'completed' : 'pending',
},
{
id: 'token',
name: 'Deploy Network Token',
status: tokenAddress ? 'completed' : 'pending',
},
{ id: 'storage', name: 'Upload to Storage', status: 'completed' },
{ id: 'registry', name: 'Register Network', status: 'completed' },
{ id: 'database', name: 'Store in Database', status: 'completed' },
],
contractAddress,
tokenAddress,
ipfsCid: manifestCid,
};
// Step 17: Initialize reputation
await this.scamDefenseService.initializeReputation(finalManifest.networkId);
await this.creatorReputationService.recordNetworkCreation(
request.creatorAddress,
finalManifest.networkId
);
this.logger.info('Network created successfully', {
networkId: finalManifest.networkId,
contractAddress,
tokenAddress,
manifestCid,
});
return {
networkId: finalManifest.networkId,
manifest: finalManifest,
deploymentStatus,
creationFees: {
...creationFees,
creatorBond: requiredBond,
total: totalFees.toString(),
},
};
} catch (error) {
this.logger.error('Failed to create network', error);
throw error;
}
}
/**
* Get network manifest by network ID or storage CID
*/
async getNetworkManifest(networkIdOrCid: string): Promise<NetworkManifest | null> {
// If it looks like a CID, fetch directly from storage
if (networkIdOrCid.startsWith('Qm') || networkIdOrCid.startsWith('baf')) {
try {
const manifest = await this.storage.download(networkIdOrCid);
return manifest as NetworkManifest;
} catch (error) {
this.logger.warn('Failed to fetch manifest from storage', { cid: networkIdOrCid, error });
}
}
// Otherwise, look up in database
const network = await this.networkRepo.findById(networkIdOrCid);
if (!network || !network.manifestCid) {
this.logger.warn('Network not found in database', { networkId: networkIdOrCid });
return null;
}
// Fetch manifest from storage
try {
const manifest = await this.storage.download(network.manifestCid);
return manifest as NetworkManifest;
} catch (error) {
this.logger.error('Failed to fetch manifest from storage', {
networkId: networkIdOrCid,
manifestCid: network.manifestCid,
error,
});
return null;
}
}
/**
* List all networks
*/
async listNetworks(filters?: {
status?: 'pending' | 'deploying' | 'deployed' | 'active' | 'graduated';
category?: string;
limit?: number;
offset?: number;
}): Promise<string[]> {
const networks = await this.networkRepo.list(filters);
return networks.map((n) => n.networkId);
}
/**
* Get deployment status for a network
*/
async getDeploymentStatus(networkId: string): Promise<NetworkDeploymentStatus | null> {
const network = await this.networkRepo.findById(networkId);
if (!network) {
return null;
}
// Get task statistics if task repository is available
let taskStats = {
totalTasks: 0,
activeTasks: 0,
completedTasks: 0,
};
if (this.taskRepo) {
try {
const allTasks = await this.taskRepo.findByNetwork(networkId);
taskStats = {
totalTasks: allTasks.length,
activeTasks: allTasks.filter((t) =>
['submitted', 'mining', 'evaluating'].includes(t.status)
).length,
completedTasks: allTasks.filter((t) => t.status === 'paid').length,
};
} catch (error) {
this.logger.warn('Failed to get task statistics', { networkId, error });
}
}
return {
networkId: network.networkId,
status: network.status,
progress: network.status === 'deployed' ? 100 : 50,
steps: [
{ id: 'manifest', name: 'Generate Manifest', status: 'completed' },
{
id: 'contract',
name: 'Deploy Settlement Contract',
status: network.contractAddress ? 'completed' : 'pending',
},
{ id: 'storage', name: 'Upload to Storage', status: 'completed' },
{ id: 'registry', name: 'Register Network', status: 'completed' },
],
contractAddress: network.contractAddress,
ipfsCid: network.manifestCid,
taskStats,
};
}
/**
* Helper: Check if penalty config is default
*/
private isDefaultPenaltyConfig(config: any, costs: any): boolean {
if (config.mechanism === 'slashing' && costs.slashingEnabled) {
return config.slashing?.rate === costs.slashingRate;
}
return config.mechanism === 'none' && !costs.slashingEnabled;
}
}