Skip to content

Commit 8d023a4

Browse files
committed
Add encrypt-decrypt load test
1 parent 37d82f2 commit 8d023a4

File tree

3 files changed

+119
-20
lines changed

3 files changed

+119
-20
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
config:
2+
target: "dummy"
3+
phases:
4+
# Over 60s, ramp up to creating 50 vusers per second
5+
- duration: 60
6+
arrivalRate: 5
7+
rampTo: 80
8+
name: "Ramp Up"
9+
# Over 300s, create 50 vusers per second
10+
- duration: 300
11+
arrivalRate: 80
12+
name: "Sustained Encrypt & Decrypt"
13+
# Over 60s, ramp down to creating 5 vusers per second
14+
- duration: 60
15+
arrivalRate: 20
16+
name: "Ramp Down"
17+
processor: "../src/processors/multi-endpoints.ts"
18+
19+
scenarios:
20+
- name: "Encrypt & Decrypt Stress Test"
21+
weight: 100
22+
flow:
23+
- function: "runEncryptDecryptTest"
24+
- think: 0.1

e2e/artillery/src/processors/multi-endpoints.ts

Lines changed: 94 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
import { createAuthManager, storagePlugins } from "@lit-protocol/auth";
2-
import { createLitClient } from "@lit-protocol/lit-client";
3-
import { z } from "zod";
4-
import * as StateManager from "../StateManager";
5-
import * as NetworkManager from "../../../src/helper/NetworkManager";
6-
import * as AccountManager from "../AccountManager";
1+
import { createAuthManager, storagePlugins } from '@lit-protocol/auth';
2+
import { createLitClient } from '@lit-protocol/lit-client';
3+
import { z } from 'zod';
4+
import * as StateManager from '../StateManager';
5+
import * as NetworkManager from '../../../src/helper/NetworkManager';
6+
import * as AccountManager from '../AccountManager';
7+
import { createAccBuilder } from '@lit-protocol/access-control-conditions';
78

89
// PKP Sign Result Schema
910
const PkpSignResultSchema = z.object({
10-
signature: z.string().regex(/^0x[a-fA-F0-9]+$/, "Invalid hex signature"),
11-
verifyingKey: z.string().regex(/^0x[a-fA-F0-9]+$/, "Invalid hex verifying key"),
12-
signedData: z.string().regex(/^0x[a-fA-F0-9]+$/, "Invalid hex signed data"),
13-
recoveryId: z.number().int().min(0).max(3, "Recovery ID must be 0-3"),
14-
publicKey: z.string().regex(/^0x[a-fA-F0-9]+$/, "Invalid hex public key"),
15-
sigType: z.string().min(1, "Signature type cannot be empty"),
11+
signature: z.string().regex(/^0x[a-fA-F0-9]+$/, 'Invalid hex signature'),
12+
verifyingKey: z
13+
.string()
14+
.regex(/^0x[a-fA-F0-9]+$/, 'Invalid hex verifying key'),
15+
signedData: z.string().regex(/^0x[a-fA-F0-9]+$/, 'Invalid hex signed data'),
16+
recoveryId: z.number().int().min(0).max(3, 'Recovery ID must be 0-3'),
17+
publicKey: z.string().regex(/^0x[a-fA-F0-9]+$/, 'Invalid hex public key'),
18+
sigType: z.string().min(1, 'Signature type cannot be empty'),
1619
});
1720

1821
// Global variables to cache expensive operations
@@ -57,11 +60,15 @@ const createAuthContextFromState = async () => {
5760

5861
// Validate that master account authData and PKP exist
5962
if (!state.masterAccount.authData) {
60-
throw new Error('❌ Master account authData not found in state. Run init.ts first.');
63+
throw new Error(
64+
'❌ Master account authData not found in state. Run init.ts first.'
65+
);
6166
}
6267

6368
if (!state.masterAccount.pkp) {
64-
throw new Error('❌ Master account PKP not found in state. Run init.ts first.');
69+
throw new Error(
70+
'❌ Master account PKP not found in state. Run init.ts first.'
71+
);
6572
}
6673

6774
// Get the master account from environment (same as init.ts)
@@ -126,12 +133,78 @@ export async function runPkpSignTest() {
126133

127134
// For Artillery, just return - no need to call next()
128135
return;
136+
} catch (error) {
137+
const endTime = Date.now();
138+
const duration = endTime - startTime;
139+
140+
console.error(
141+
`❌ pkpSign failed in ${duration}ms:`,
142+
error instanceof Error ? error.message : String(error)
143+
);
144+
145+
// Throw the error to let Artillery handle it
146+
throw error;
147+
}
148+
}
129149

150+
// test '/web/encryption/sign/v2' endpoint
151+
export async function runEncryptDecryptTest() {
152+
const startTime = Date.now();
153+
154+
try {
155+
// 1. Initialise shared resources (only happens once)
156+
await initialiseSharedResources();
157+
158+
// 2. Read state
159+
const state = await StateManager.readFile();
160+
161+
// Create auth context
162+
const authContext = await createAuthContextFromState();
163+
164+
// Set up access control conditions requiring wallet ownership
165+
const addressToUse = authContext.account.address;
166+
const builder = createAccBuilder();
167+
const accs = builder
168+
.requireWalletOwnership(addressToUse)
169+
.on('ethereum')
170+
.build();
171+
172+
// Encrypt data with the access control conditions
173+
const dataToEncrypt = 'Hello from PKP encrypt-decrypt test!';
174+
const encryptedData = await litClient.encrypt({
175+
dataToEncrypt,
176+
unifiedAccessControlConditions: accs,
177+
chain: 'ethereum',
178+
});
179+
180+
// Decrypt the data using the appropriate auth context
181+
const decryptedData = await litClient.decrypt({
182+
data: encryptedData,
183+
unifiedAccessControlConditions: accs,
184+
chain: 'ethereum',
185+
authContext,
186+
});
187+
188+
// Assert that the decrypted data is the same as the original data
189+
if (decryptedData.convertedData !== dataToEncrypt) {
190+
throw new Error('❌ Decrypted data does not match the original data');
191+
}
192+
193+
const endTime = Date.now();
194+
const duration = endTime - startTime;
195+
196+
console.log(`✅ encrypt & decrypt successful in ${duration}ms`);
197+
198+
// For Artillery, just return - no need to call next()
199+
return;
130200
} catch (error) {
131201
const endTime = Date.now();
132202
const duration = endTime - startTime;
133203

134-
console.error(`❌ pkpSign failed in ${duration}ms:`, error instanceof Error ? error.message : String(error));
204+
console.error(
205+
`❌ encrypt & decrypt failed in ${duration}ms:`,
206+
error instanceof Error ? error.message : String(error)
207+
);
135208

136209
// Throw the error to let Artillery handle it
137210
throw error;
@@ -140,7 +213,6 @@ export async function runPkpSignTest() {
140213

141214
// test '/web/sign_session_key' endpoint
142215
export async function runSignSessionKeyTest() {
143-
144216
// ❗️ IT'S IMPORTANT TO SET THIS TO FALSE FOR TESTING
145217
const DELEGATION_AUTH_SIG_CACHE = false;
146218

@@ -167,18 +239,20 @@ export async function runSignSessionKeyTest() {
167239
litClient: litClient,
168240
cache: {
169241
delegationAuthSig: DELEGATION_AUTH_SIG_CACHE,
170-
}
242+
},
171243
});
172244

173245
// console.log('✅ Master Account PKP Auth Context:', masterAccountPkpAuthContext);
174-
175246
} catch (error) {
176247
const endTime = Date.now();
177248
const duration = endTime - startTime;
178249

179-
console.error(`❌ signSessionKey failed in ${duration}ms:`, error instanceof Error ? error.message : String(error));
250+
console.error(
251+
`❌ signSessionKey failed in ${duration}ms:`,
252+
error instanceof Error ? error.message : String(error)
253+
);
180254

181255
// Throw the error to let Artillery handle it
182256
throw error;
183257
}
184-
}
258+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"artillery:init": "bun run ./e2e/artillery/src/init.ts",
2828
"artillery:balance-status": "LOG_LEVEL=silent bun run ./e2e/artillery/src/balance-status.ts",
2929
"artillery:pkp-sign": "DEBUG_HTTP=true LOG_LEVEL=silent dotenvx run --env-file=.env -- sh -c 'artillery run ./e2e/artillery/configs/pkp-sign.yml ${ARTILLERY_KEY:+--record --key $ARTILLERY_KEY}'",
30+
"artillery:encrypt-decrypt": "DEBUG_HTTP=true LOG_LEVEL=silent dotenvx run --env-file=.env -- sh -c 'artillery run ./e2e/artillery/configs/encrypt-decrypt.yml ${ARTILLERY_KEY:+--record --key $ARTILLERY_KEY}'",
3031
"artillery:sign-session-key": "DEBUG_HTTP=true LOG_LEVEL=silent dotenvx run --env-file=.env -- sh -c 'artillery run ./e2e/artillery/configs/sign-session-key.yml ${ARTILLERY_KEY:+--record --key $ARTILLERY_KEY}'"
3132
},
3233
"private": true,

0 commit comments

Comments
 (0)