@f2a/network 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/ci.yml +113 -0
- package/.github/workflows/publish.yml +60 -0
- package/LICENSE +21 -0
- package/MONOREPO.md +58 -0
- package/README.md +280 -0
- package/SKILL.md +137 -0
- package/dist/adapters/openclaw.d.ts +103 -0
- package/dist/adapters/openclaw.d.ts.map +1 -0
- package/dist/adapters/openclaw.js +297 -0
- package/dist/adapters/openclaw.js.map +1 -0
- package/dist/cli/commands.d.ts +17 -0
- package/dist/cli/commands.d.ts.map +1 -0
- package/dist/cli/commands.js +107 -0
- package/dist/cli/commands.js.map +1 -0
- package/dist/cli/index.d.ts +6 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +203 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/core/autonomous-economy.d.ts +136 -0
- package/dist/core/autonomous-economy.d.ts.map +1 -0
- package/dist/core/autonomous-economy.js +255 -0
- package/dist/core/autonomous-economy.js.map +1 -0
- package/dist/core/connection-manager.d.ts +80 -0
- package/dist/core/connection-manager.d.ts.map +1 -0
- package/dist/core/connection-manager.js +235 -0
- package/dist/core/connection-manager.js.map +1 -0
- package/dist/core/connection-manager.test.d.ts +2 -0
- package/dist/core/connection-manager.test.d.ts.map +1 -0
- package/dist/core/connection-manager.test.js +52 -0
- package/dist/core/connection-manager.test.js.map +1 -0
- package/dist/core/e2ee-crypto.d.ts +90 -0
- package/dist/core/e2ee-crypto.d.ts.map +1 -0
- package/dist/core/e2ee-crypto.js +190 -0
- package/dist/core/e2ee-crypto.js.map +1 -0
- package/dist/core/f2a.d.ts +126 -0
- package/dist/core/f2a.d.ts.map +1 -0
- package/dist/core/f2a.js +425 -0
- package/dist/core/f2a.js.map +1 -0
- package/dist/core/identity.d.ts +47 -0
- package/dist/core/identity.d.ts.map +1 -0
- package/dist/core/identity.js +130 -0
- package/dist/core/identity.js.map +1 -0
- package/dist/core/identity.test.d.ts +2 -0
- package/dist/core/identity.test.d.ts.map +1 -0
- package/dist/core/identity.test.js +43 -0
- package/dist/core/identity.test.js.map +1 -0
- package/dist/core/p2p-network.d.ts +242 -0
- package/dist/core/p2p-network.d.ts.map +1 -0
- package/dist/core/p2p-network.js +1182 -0
- package/dist/core/p2p-network.js.map +1 -0
- package/dist/core/reputation-security.d.ts +168 -0
- package/dist/core/reputation-security.d.ts.map +1 -0
- package/dist/core/reputation-security.js +369 -0
- package/dist/core/reputation-security.js.map +1 -0
- package/dist/core/reputation.d.ts +179 -0
- package/dist/core/reputation.d.ts.map +1 -0
- package/dist/core/reputation.js +472 -0
- package/dist/core/reputation.js.map +1 -0
- package/dist/core/review-committee.d.ts +130 -0
- package/dist/core/review-committee.d.ts.map +1 -0
- package/dist/core/review-committee.js +251 -0
- package/dist/core/review-committee.js.map +1 -0
- package/dist/core/serverless.d.ts +155 -0
- package/dist/core/serverless.d.ts.map +1 -0
- package/dist/core/serverless.js +615 -0
- package/dist/core/serverless.js.map +1 -0
- package/dist/core/token-manager.d.ts +42 -0
- package/dist/core/token-manager.d.ts.map +1 -0
- package/dist/core/token-manager.js +122 -0
- package/dist/core/token-manager.js.map +1 -0
- package/dist/daemon/control-server.d.ts +55 -0
- package/dist/daemon/control-server.d.ts.map +1 -0
- package/dist/daemon/control-server.js +262 -0
- package/dist/daemon/control-server.js.map +1 -0
- package/dist/daemon/index.d.ts +35 -0
- package/dist/daemon/index.d.ts.map +1 -0
- package/dist/daemon/index.js +69 -0
- package/dist/daemon/index.js.map +1 -0
- package/dist/daemon/main.d.ts +6 -0
- package/dist/daemon/main.d.ts.map +1 -0
- package/dist/daemon/main.js +38 -0
- package/dist/daemon/main.js.map +1 -0
- package/dist/daemon/start.d.ts +6 -0
- package/dist/daemon/start.d.ts.map +1 -0
- package/dist/daemon/start.js +25 -0
- package/dist/daemon/start.js.map +1 -0
- package/dist/daemon/webhook.d.ts +30 -0
- package/dist/daemon/webhook.d.ts.map +1 -0
- package/dist/daemon/webhook.js +86 -0
- package/dist/daemon/webhook.js.map +1 -0
- package/dist/daemon/webhook.test.d.ts +2 -0
- package/dist/daemon/webhook.test.d.ts.map +1 -0
- package/dist/daemon/webhook.test.js +24 -0
- package/dist/daemon/webhook.test.js.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/protocol/messages.d.ts +739 -0
- package/dist/protocol/messages.d.ts.map +1 -0
- package/dist/protocol/messages.js +188 -0
- package/dist/protocol/messages.js.map +1 -0
- package/dist/protocol/messages.test.d.ts +2 -0
- package/dist/protocol/messages.test.d.ts.map +1 -0
- package/dist/protocol/messages.test.js +55 -0
- package/dist/protocol/messages.test.js.map +1 -0
- package/dist/types/index.d.ts +247 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +10 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/result.d.ts +28 -0
- package/dist/types/result.d.ts.map +1 -0
- package/dist/types/result.js +16 -0
- package/dist/types/result.js.map +1 -0
- package/dist/utils/benchmark.d.ts +67 -0
- package/dist/utils/benchmark.d.ts.map +1 -0
- package/dist/utils/benchmark.js +179 -0
- package/dist/utils/benchmark.js.map +1 -0
- package/dist/utils/logger.d.ts +105 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +275 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/middleware.d.ts +85 -0
- package/dist/utils/middleware.d.ts.map +1 -0
- package/dist/utils/middleware.js +173 -0
- package/dist/utils/middleware.js.map +1 -0
- package/dist/utils/rate-limiter.d.ts +71 -0
- package/dist/utils/rate-limiter.d.ts.map +1 -0
- package/dist/utils/rate-limiter.js +160 -0
- package/dist/utils/rate-limiter.js.map +1 -0
- package/dist/utils/signature.d.ts +57 -0
- package/dist/utils/signature.d.ts.map +1 -0
- package/dist/utils/signature.js +102 -0
- package/dist/utils/signature.js.map +1 -0
- package/dist/utils/validation.d.ts +504 -0
- package/dist/utils/validation.d.ts.map +1 -0
- package/dist/utils/validation.js +159 -0
- package/dist/utils/validation.js.map +1 -0
- package/docs/F2A-PROTOCOL.md +61 -0
- package/docs/MOBILE_BOOTSTRAP_DESIGN.md +126 -0
- package/docs/a2a-lessons.md +316 -0
- package/docs/middleware-guide.md +448 -0
- package/docs/readme-update-checklist.md +90 -0
- package/docs/reputation-guide.md +396 -0
- package/docs/rfcs/001-reputation-system.md +712 -0
- package/docs/security-design.md +247 -0
- package/install.sh +231 -0
- package/package.json +64 -0
- package/packages/openclaw-adapter/README.md +510 -0
- package/packages/openclaw-adapter/openclaw.plugin.json +106 -0
- package/packages/openclaw-adapter/package.json +40 -0
- package/packages/openclaw-adapter/src/announcement-queue.test.ts +449 -0
- package/packages/openclaw-adapter/src/announcement-queue.ts +403 -0
- package/packages/openclaw-adapter/src/capability-detector.test.ts +99 -0
- package/packages/openclaw-adapter/src/capability-detector.ts +183 -0
- package/packages/openclaw-adapter/src/claim-handlers.test.ts +974 -0
- package/packages/openclaw-adapter/src/claim-handlers.ts +482 -0
- package/packages/openclaw-adapter/src/connector.business.test.ts +583 -0
- package/packages/openclaw-adapter/src/connector.ts +795 -0
- package/packages/openclaw-adapter/src/index.test.ts +82 -0
- package/packages/openclaw-adapter/src/index.ts +18 -0
- package/packages/openclaw-adapter/src/integration.e2e.test.ts +829 -0
- package/packages/openclaw-adapter/src/logger.ts +51 -0
- package/packages/openclaw-adapter/src/network-client.test.ts +266 -0
- package/packages/openclaw-adapter/src/network-client.ts +251 -0
- package/packages/openclaw-adapter/src/network-recovery.test.ts +465 -0
- package/packages/openclaw-adapter/src/node-manager.test.ts +136 -0
- package/packages/openclaw-adapter/src/node-manager.ts +429 -0
- package/packages/openclaw-adapter/src/plugin.test.ts +439 -0
- package/packages/openclaw-adapter/src/plugin.ts +104 -0
- package/packages/openclaw-adapter/src/reputation.test.ts +221 -0
- package/packages/openclaw-adapter/src/reputation.ts +368 -0
- package/packages/openclaw-adapter/src/task-guard.test.ts +502 -0
- package/packages/openclaw-adapter/src/task-guard.ts +860 -0
- package/packages/openclaw-adapter/src/task-queue.concurrency.test.ts +462 -0
- package/packages/openclaw-adapter/src/task-queue.edge-cases.test.ts +284 -0
- package/packages/openclaw-adapter/src/task-queue.persistence.test.ts +408 -0
- package/packages/openclaw-adapter/src/task-queue.ts +668 -0
- package/packages/openclaw-adapter/src/tool-handlers.test.ts +906 -0
- package/packages/openclaw-adapter/src/tool-handlers.ts +574 -0
- package/packages/openclaw-adapter/src/types.ts +361 -0
- package/packages/openclaw-adapter/src/webhook-pusher.test.ts +188 -0
- package/packages/openclaw-adapter/src/webhook-pusher.ts +220 -0
- package/packages/openclaw-adapter/src/webhook-server.test.ts +580 -0
- package/packages/openclaw-adapter/src/webhook-server.ts +202 -0
- package/packages/openclaw-adapter/tsconfig.json +20 -0
- package/src/cli/commands.test.ts +157 -0
- package/src/cli/commands.ts +129 -0
- package/src/cli/index.test.ts +77 -0
- package/src/cli/index.ts +234 -0
- package/src/core/autonomous-economy.test.ts +291 -0
- package/src/core/autonomous-economy.ts +428 -0
- package/src/core/e2ee-crypto.test.ts +125 -0
- package/src/core/e2ee-crypto.ts +246 -0
- package/src/core/f2a.test.ts +269 -0
- package/src/core/f2a.ts +618 -0
- package/src/core/p2p-network.test.ts +199 -0
- package/src/core/p2p-network.ts +1432 -0
- package/src/core/reputation-security.test.ts +403 -0
- package/src/core/reputation-security.ts +562 -0
- package/src/core/reputation.test.ts +260 -0
- package/src/core/reputation.ts +576 -0
- package/src/core/review-committee.test.ts +380 -0
- package/src/core/review-committee.ts +401 -0
- package/src/core/token-manager.test.ts +133 -0
- package/src/core/token-manager.ts +140 -0
- package/src/daemon/control-server.test.ts +216 -0
- package/src/daemon/control-server.ts +292 -0
- package/src/daemon/index.test.ts +85 -0
- package/src/daemon/index.ts +89 -0
- package/src/daemon/main.ts +44 -0
- package/src/daemon/start.ts +29 -0
- package/src/daemon/webhook.test.ts +68 -0
- package/src/daemon/webhook.ts +105 -0
- package/src/index.test.ts +436 -0
- package/src/index.ts +72 -0
- package/src/types/index.test.ts +87 -0
- package/src/types/index.ts +341 -0
- package/src/types/result.ts +68 -0
- package/src/utils/benchmark.ts +237 -0
- package/src/utils/logger.ts +331 -0
- package/src/utils/middleware.ts +229 -0
- package/src/utils/rate-limiter.ts +207 -0
- package/src/utils/signature.ts +136 -0
- package/src/utils/validation.ts +186 -0
- package/tests/docker/Dockerfile.node +23 -0
- package/tests/docker/Dockerfile.runner +18 -0
- package/tests/docker/docker-compose.test.yml +73 -0
- package/tests/integration/message-passing.test.ts +109 -0
- package/tests/integration/multi-node.test.ts +92 -0
- package/tests/integration/p2p-connection.test.ts +83 -0
- package/tests/integration/test-config.ts +32 -0
- package/tsconfig.json +21 -0
- package/vitest.config.ts +26 -0
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 信誉安全机制测试
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, it, expect, beforeEach } from 'vitest';
|
|
6
|
+
import {
|
|
7
|
+
ChainSignatureManager,
|
|
8
|
+
InvitationManager,
|
|
9
|
+
ChallengeManager,
|
|
10
|
+
SignedReputationEvent,
|
|
11
|
+
} from './reputation-security.js';
|
|
12
|
+
import { ReputationManager } from './reputation.js';
|
|
13
|
+
|
|
14
|
+
describe('ChainSignatureManager', () => {
|
|
15
|
+
let chainManager: ChainSignatureManager;
|
|
16
|
+
|
|
17
|
+
beforeEach(() => {
|
|
18
|
+
chainManager = new ChainSignatureManager();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe('事件链管理', () => {
|
|
22
|
+
it('should create genesis event', () => {
|
|
23
|
+
const event: SignedReputationEvent = {
|
|
24
|
+
peerId: 'peer-1',
|
|
25
|
+
delta: 10,
|
|
26
|
+
prevHash: 'genesis',
|
|
27
|
+
timestamp: Date.now(),
|
|
28
|
+
signatures: [],
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const result = chainManager.addSignedEvent(event);
|
|
32
|
+
expect(result).toBe(true);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should chain events correctly', () => {
|
|
36
|
+
// 第一个事件
|
|
37
|
+
const event1: SignedReputationEvent = {
|
|
38
|
+
peerId: 'peer-1',
|
|
39
|
+
delta: 10,
|
|
40
|
+
prevHash: 'genesis',
|
|
41
|
+
timestamp: Date.now(),
|
|
42
|
+
signatures: [],
|
|
43
|
+
};
|
|
44
|
+
chainManager.addSignedEvent(event1);
|
|
45
|
+
|
|
46
|
+
// 第二个事件
|
|
47
|
+
const prevHash = chainManager.hashEvent(event1);
|
|
48
|
+
const event2: SignedReputationEvent = {
|
|
49
|
+
peerId: 'peer-1',
|
|
50
|
+
delta: -5,
|
|
51
|
+
prevHash,
|
|
52
|
+
timestamp: Date.now(),
|
|
53
|
+
signatures: [],
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const result = chainManager.addSignedEvent(event2);
|
|
57
|
+
expect(result).toBe(true);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('should reject event with wrong prevHash', () => {
|
|
61
|
+
// 第一个事件
|
|
62
|
+
const event1: SignedReputationEvent = {
|
|
63
|
+
peerId: 'peer-1',
|
|
64
|
+
delta: 10,
|
|
65
|
+
prevHash: 'genesis',
|
|
66
|
+
timestamp: Date.now(),
|
|
67
|
+
signatures: [],
|
|
68
|
+
};
|
|
69
|
+
chainManager.addSignedEvent(event1);
|
|
70
|
+
|
|
71
|
+
// 第二个事件,错误的 prevHash
|
|
72
|
+
const event2: SignedReputationEvent = {
|
|
73
|
+
peerId: 'peer-1',
|
|
74
|
+
delta: -5,
|
|
75
|
+
prevHash: 'wrong-hash',
|
|
76
|
+
timestamp: Date.now(),
|
|
77
|
+
signatures: [],
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const result = chainManager.addSignedEvent(event2);
|
|
81
|
+
expect(result).toBe(false);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
describe('链验证', () => {
|
|
86
|
+
it('should verify valid chain', () => {
|
|
87
|
+
const event1: SignedReputationEvent = {
|
|
88
|
+
peerId: 'peer-1',
|
|
89
|
+
delta: 10,
|
|
90
|
+
prevHash: 'genesis',
|
|
91
|
+
timestamp: Date.now(),
|
|
92
|
+
signatures: [],
|
|
93
|
+
};
|
|
94
|
+
chainManager.addSignedEvent(event1);
|
|
95
|
+
|
|
96
|
+
const event2: SignedReputationEvent = {
|
|
97
|
+
peerId: 'peer-1',
|
|
98
|
+
delta: -5,
|
|
99
|
+
prevHash: chainManager.hashEvent(event1),
|
|
100
|
+
timestamp: Date.now(),
|
|
101
|
+
signatures: [],
|
|
102
|
+
};
|
|
103
|
+
chainManager.addSignedEvent(event2);
|
|
104
|
+
|
|
105
|
+
expect(chainManager.verifyChain('peer-1')).toBe(true);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('should detect broken chain', () => {
|
|
109
|
+
// 先添加一个有效事件
|
|
110
|
+
const validEvent: SignedReputationEvent = {
|
|
111
|
+
peerId: 'peer-1',
|
|
112
|
+
delta: 10,
|
|
113
|
+
prevHash: 'genesis',
|
|
114
|
+
timestamp: Date.now(),
|
|
115
|
+
signatures: [],
|
|
116
|
+
};
|
|
117
|
+
chainManager.addSignedEvent(validEvent);
|
|
118
|
+
|
|
119
|
+
// 现在手动添加一个破坏的事件到链中
|
|
120
|
+
const chain = chainManager.getEventChain('peer-1');
|
|
121
|
+
const brokenEvent: SignedReputationEvent = {
|
|
122
|
+
peerId: 'peer-1',
|
|
123
|
+
delta: 100,
|
|
124
|
+
prevHash: 'wrong-hash', // 这个应该等于 validEvent 的 hash
|
|
125
|
+
timestamp: Date.now(),
|
|
126
|
+
signatures: [],
|
|
127
|
+
};
|
|
128
|
+
chain.push(brokenEvent);
|
|
129
|
+
|
|
130
|
+
expect(chainManager.verifyChain('peer-1')).toBe(false);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
describe('分数计算', () => {
|
|
135
|
+
it('should calculate score from chain', () => {
|
|
136
|
+
const event1: SignedReputationEvent = {
|
|
137
|
+
peerId: 'peer-1',
|
|
138
|
+
delta: 10,
|
|
139
|
+
prevHash: 'genesis',
|
|
140
|
+
timestamp: Date.now(),
|
|
141
|
+
signatures: [],
|
|
142
|
+
};
|
|
143
|
+
chainManager.addSignedEvent(event1);
|
|
144
|
+
|
|
145
|
+
const event2: SignedReputationEvent = {
|
|
146
|
+
peerId: 'peer-1',
|
|
147
|
+
delta: 20,
|
|
148
|
+
prevHash: chainManager.hashEvent(event1),
|
|
149
|
+
timestamp: Date.now(),
|
|
150
|
+
signatures: [],
|
|
151
|
+
};
|
|
152
|
+
chainManager.addSignedEvent(event2);
|
|
153
|
+
|
|
154
|
+
const score = chainManager.calculateScoreFromChain('peer-1', 70);
|
|
155
|
+
expect(score).toBe(100); // 70 + 10 + 20
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
describe('InvitationManager', () => {
|
|
161
|
+
let reputationManager: ReputationManager;
|
|
162
|
+
let invitationManager: InvitationManager;
|
|
163
|
+
|
|
164
|
+
beforeEach(() => {
|
|
165
|
+
reputationManager = new ReputationManager();
|
|
166
|
+
invitationManager = new InvitationManager(reputationManager);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
describe('创建邀请', () => {
|
|
170
|
+
it('should create invitation for high reputation inviter', () => {
|
|
171
|
+
// 提升邀请者信誉
|
|
172
|
+
for (let i = 0; i < 5; i++) {
|
|
173
|
+
reputationManager.recordSuccess('inviter-1', `task-${i}`);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const result = invitationManager.createInvitation('inviter-1', 'invitee-1');
|
|
177
|
+
expect(result.success).toBe(true);
|
|
178
|
+
expect(result.invitation).toBeDefined();
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('should accept invitation from default reputation inviter (70 > 60)', () => {
|
|
182
|
+
// 邀请者默认信誉是 70,而要求是 60
|
|
183
|
+
const result = invitationManager.createInvitation('inviter-1', 'invitee-1');
|
|
184
|
+
expect(result.success).toBe(true);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it('should reject invitation from very low reputation inviter', () => {
|
|
188
|
+
// 降低邀请者信誉到低于 60
|
|
189
|
+
for (let i = 0; i < 5; i++) {
|
|
190
|
+
reputationManager.recordFailure('inviter-1', `task-${i}`, 'test');
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const result = invitationManager.createInvitation('inviter-1', 'invitee-1');
|
|
194
|
+
// 检查信誉是否足够
|
|
195
|
+
const score = reputationManager.getReputation('inviter-1').score;
|
|
196
|
+
if (score < 60) {
|
|
197
|
+
expect(result.success).toBe(false);
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it('should reject when invitation quota exhausted', () => {
|
|
202
|
+
// 提升邀请者信誉
|
|
203
|
+
for (let i = 0; i < 5; i++) {
|
|
204
|
+
reputationManager.recordSuccess('inviter-1', `task-${i}`);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// 创建 5 个邀请(达到上限)
|
|
208
|
+
for (let i = 0; i < 5; i++) {
|
|
209
|
+
invitationManager.createInvitation('inviter-1', `invitee-${i}`);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// 第 6 个应该失败
|
|
213
|
+
const result = invitationManager.createInvitation('inviter-1', 'invitee-6');
|
|
214
|
+
expect(result.success).toBe(false);
|
|
215
|
+
expect(result.error).toContain('quota');
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it('should reject duplicate invitation', () => {
|
|
219
|
+
for (let i = 0; i < 5; i++) {
|
|
220
|
+
reputationManager.recordSuccess('inviter-1', `task-${i}`);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
invitationManager.createInvitation('inviter-1', 'invitee-1');
|
|
224
|
+
|
|
225
|
+
// 另一个邀请者邀请同一人
|
|
226
|
+
for (let i = 0; i < 5; i++) {
|
|
227
|
+
reputationManager.recordSuccess('inviter-2', `task-${i}`);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const result = invitationManager.createInvitation('inviter-2', 'invitee-1');
|
|
231
|
+
expect(result.success).toBe(false);
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
describe('初始信誉计算', () => {
|
|
236
|
+
it('should set initial score based on inviter reputation', () => {
|
|
237
|
+
// 提升邀请者信誉到高分
|
|
238
|
+
for (let i = 0; i < 10; i++) {
|
|
239
|
+
reputationManager.recordSuccess('inviter-1', `task-${i}`);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const inviterScore = reputationManager.getReputation('inviter-1').score;
|
|
243
|
+
|
|
244
|
+
invitationManager.createInvitation('inviter-1', 'invitee-1');
|
|
245
|
+
|
|
246
|
+
const inviteeScore = reputationManager.getReputation('invitee-1').score;
|
|
247
|
+
// 初始分数 = inviter分数 * 0.5
|
|
248
|
+
expect(inviteeScore).toBeGreaterThanOrEqual(30);
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
describe('连带责任', () => {
|
|
253
|
+
it('should apply joint liability penalty', () => {
|
|
254
|
+
for (let i = 0; i < 5; i++) {
|
|
255
|
+
reputationManager.recordSuccess('inviter-1', `task-${i}`);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
invitationManager.createInvitation('inviter-1', 'invitee-1');
|
|
259
|
+
|
|
260
|
+
const beforeScore = reputationManager.getReputation('inviter-1').score;
|
|
261
|
+
|
|
262
|
+
// 被邀请者作恶
|
|
263
|
+
invitationManager.applyJointLiability('invitee-1', 20);
|
|
264
|
+
|
|
265
|
+
const afterScore = reputationManager.getReputation('inviter-1').score;
|
|
266
|
+
expect(afterScore).toBeLessThan(beforeScore);
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
it('should not apply penalty when joint liability disabled', () => {
|
|
270
|
+
const noLiabilityManager = new InvitationManager(reputationManager, {
|
|
271
|
+
jointLiability: false,
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
for (let i = 0; i < 5; i++) {
|
|
275
|
+
reputationManager.recordSuccess('inviter-1', `task-${i}`);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
noLiabilityManager.createInvitation('inviter-1', 'invitee-1');
|
|
279
|
+
|
|
280
|
+
const beforeScore = reputationManager.getReputation('inviter-1').score;
|
|
281
|
+
|
|
282
|
+
noLiabilityManager.applyJointLiability('invitee-1', 20);
|
|
283
|
+
|
|
284
|
+
const afterScore = reputationManager.getReputation('inviter-1').score;
|
|
285
|
+
expect(afterScore).toBe(beforeScore);
|
|
286
|
+
});
|
|
287
|
+
});
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
describe('ChallengeManager', () => {
|
|
291
|
+
let reputationManager: ReputationManager;
|
|
292
|
+
let chainManager: ChainSignatureManager;
|
|
293
|
+
let challengeManager: ChallengeManager;
|
|
294
|
+
|
|
295
|
+
beforeEach(() => {
|
|
296
|
+
reputationManager = new ReputationManager();
|
|
297
|
+
chainManager = new ChainSignatureManager();
|
|
298
|
+
challengeManager = new ChallengeManager(reputationManager, chainManager);
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
describe('提交挑战', () => {
|
|
302
|
+
it('should submit challenge', () => {
|
|
303
|
+
const challenge = challengeManager.submitChallenge(
|
|
304
|
+
'challenger-1',
|
|
305
|
+
'target-1',
|
|
306
|
+
'invalid_history',
|
|
307
|
+
'Evidence text'
|
|
308
|
+
);
|
|
309
|
+
|
|
310
|
+
expect(challenge.challengerId).toBe('challenger-1');
|
|
311
|
+
expect(challenge.targetId).toBe('target-1');
|
|
312
|
+
expect(challenge.status).toBe('pending');
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it('should track challenges by target', () => {
|
|
316
|
+
challengeManager.submitChallenge('challenger-1', 'target-1', 'invalid_history', 'e1');
|
|
317
|
+
challengeManager.submitChallenge('challenger-2', 'target-1', 'collusion', 'e2');
|
|
318
|
+
|
|
319
|
+
const challenges = challengeManager.getChallenges('target-1');
|
|
320
|
+
expect(challenges.length).toBe(2);
|
|
321
|
+
});
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
describe('处理挑战', () => {
|
|
325
|
+
it('should detect invalid chain when prevHash is wrong', () => {
|
|
326
|
+
// 直接验证链,不依赖 processChallenge
|
|
327
|
+
const event: SignedReputationEvent = {
|
|
328
|
+
peerId: 'target-1',
|
|
329
|
+
delta: 10,
|
|
330
|
+
prevHash: 'wrong-hash', // 错误的 prevHash
|
|
331
|
+
timestamp: Date.now(),
|
|
332
|
+
signatures: [],
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
// 尝试添加无效事件
|
|
336
|
+
const result = chainManager.addSignedEvent(event);
|
|
337
|
+
|
|
338
|
+
// 由于没有 genesis 事件,prevHash 应该是 'genesis'
|
|
339
|
+
expect(result).toBe(false);
|
|
340
|
+
|
|
341
|
+
// 验证链应该是空的
|
|
342
|
+
expect(chainManager.verifyChain('target-1')).toBe(true);
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
it('should fail when chain is valid', () => {
|
|
346
|
+
// 创建有效的链
|
|
347
|
+
const event1: SignedReputationEvent = {
|
|
348
|
+
peerId: 'target-1',
|
|
349
|
+
delta: 10,
|
|
350
|
+
prevHash: 'genesis',
|
|
351
|
+
timestamp: Date.now(),
|
|
352
|
+
signatures: [],
|
|
353
|
+
};
|
|
354
|
+
chainManager.addSignedEvent(event1);
|
|
355
|
+
|
|
356
|
+
const challenge = challengeManager.submitChallenge(
|
|
357
|
+
'challenger-1',
|
|
358
|
+
'target-1',
|
|
359
|
+
'invalid_history',
|
|
360
|
+
'Chain looks suspicious'
|
|
361
|
+
);
|
|
362
|
+
|
|
363
|
+
const beforeScore = reputationManager.getReputation('challenger-1').score;
|
|
364
|
+
const result = challengeManager.processChallenge(challenge);
|
|
365
|
+
const afterScore = reputationManager.getReputation('challenger-1').score;
|
|
366
|
+
|
|
367
|
+
expect(result.success).toBe(false);
|
|
368
|
+
expect(afterScore).toBeLessThan(beforeScore); // 挑战失败,惩罚挑战者
|
|
369
|
+
});
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
describe('合谋检测', () => {
|
|
373
|
+
it('should detect high variance as potential collusion', () => {
|
|
374
|
+
// 创建分数波动大的历史
|
|
375
|
+
for (let i = 0; i < 10; i++) {
|
|
376
|
+
reputationManager.recordSuccess('suspicious-1', `task-${i}`, i % 2 === 0 ? 50 : -30);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
const score = challengeManager.detectCollusion('suspicious-1');
|
|
380
|
+
expect(score).toBeGreaterThan(0);
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
it('should return low score for normal behavior', () => {
|
|
384
|
+
// 正常行为
|
|
385
|
+
for (let i = 0; i < 10; i++) {
|
|
386
|
+
reputationManager.recordSuccess('normal-1', `task-${i}`);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
const score = challengeManager.detectCollusion('normal-1');
|
|
390
|
+
expect(score).toBeLessThan(0.5);
|
|
391
|
+
});
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
describe('待处理挑战', () => {
|
|
395
|
+
it('should list pending challenges', () => {
|
|
396
|
+
challengeManager.submitChallenge('c1', 't1', 'invalid_history', 'e1');
|
|
397
|
+
challengeManager.submitChallenge('c2', 't2', 'collusion', 'e2');
|
|
398
|
+
|
|
399
|
+
const pending = challengeManager.getPendingChallenges();
|
|
400
|
+
expect(pending.length).toBe(2);
|
|
401
|
+
});
|
|
402
|
+
});
|
|
403
|
+
});
|