@waku/rln 0.0.2-3670e82.0 → 0.0.2-3ab8023.0

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.
@@ -264,8 +264,23 @@ class RLNContract {
264
264
  console.log("registerWithIdentity - IDCommitmentBigInt:", identity.IDCommitmentBigInt.toString());
265
265
  console.log("registerWithIdentity - rate limit:", this.rateLimit);
266
266
  log.info(`Registering identity with rate limit: ${this.rateLimit} messages/epoch`);
267
+ // Check if the ID commitment is already registered
268
+ const existingIndex = await this.getMemberIndex(identity.IDCommitmentBigInt.toString());
269
+ if (existingIndex) {
270
+ console.error(`ID commitment is already registered with index ${existingIndex}`);
271
+ throw new Error(`ID commitment is already registered with index ${existingIndex}`);
272
+ }
273
+ // Check if there's enough remaining rate limit
274
+ const remainingRateLimit = await this.getRemainingTotalRateLimit();
275
+ if (remainingRateLimit < this.rateLimit) {
276
+ console.error(`Not enough remaining rate limit. Requested: ${this.rateLimit}, Available: ${remainingRateLimit}`);
277
+ throw new Error(`Not enough remaining rate limit. Requested: ${this.rateLimit}, Available: ${remainingRateLimit}`);
278
+ }
267
279
  console.log("registerWithIdentity - calling contract.register");
268
- const txRegisterResponse = await this.contract.register(identity.IDCommitmentBigInt, this.rateLimit, [], { gasLimit: 300000 });
280
+ // Estimate gas for the transaction
281
+ const estimatedGas = await this.contract.estimateGas.register(identity.IDCommitmentBigInt, this.rateLimit, []);
282
+ const gasLimit = estimatedGas.add(10000);
283
+ const txRegisterResponse = await this.contract.register(identity.IDCommitmentBigInt, this.rateLimit, [], { gasLimit });
269
284
  console.log("registerWithIdentity - txRegisterResponse:", txRegisterResponse);
270
285
  console.log("registerWithIdentity - hash:", txRegisterResponse.hash);
271
286
  console.log("registerWithIdentity - waiting for transaction confirmation...");
@@ -274,12 +289,17 @@ class RLNContract {
274
289
  console.log("registerWithIdentity - transaction status:", txRegisterReceipt.status);
275
290
  console.log("registerWithIdentity - block number:", txRegisterReceipt.blockNumber);
276
291
  console.log("registerWithIdentity - gas used:", txRegisterReceipt.gasUsed.toString());
292
+ // Check transaction status
293
+ if (txRegisterReceipt.status === 0) {
294
+ console.error("Transaction failed on-chain");
295
+ throw new Error("Transaction failed on-chain");
296
+ }
277
297
  const memberRegistered = txRegisterReceipt.events?.find((event) => event.event === "MembershipRegistered");
278
298
  console.log("registerWithIdentity - memberRegistered event:", memberRegistered);
279
299
  if (!memberRegistered || !memberRegistered.args) {
280
300
  console.log("registerWithIdentity - ERROR: no memberRegistered event found");
281
301
  console.log("registerWithIdentity - all events:", txRegisterReceipt.events);
282
- log.error("Failed to register membership: No MembershipRegistered event found");
302
+ console.error("Failed to register membership: No MembershipRegistered event found");
283
303
  return undefined;
284
304
  }
285
305
  console.log("registerWithIdentity - memberRegistered args:", memberRegistered.args);
@@ -314,10 +334,34 @@ class RLNContract {
314
334
  }
315
335
  catch (error) {
316
336
  console.log("registerWithIdentity - ERROR:", error);
317
- console.log("registerWithIdentity - error message:", error.message);
318
- console.log("registerWithIdentity - error stack:", error.stack);
319
- log.error(`Error in registerWithIdentity: ${error.message}`);
320
- return undefined;
337
+ // Improved error handling to decode contract errors
338
+ if (error instanceof Error) {
339
+ const errorMessage = error.message;
340
+ console.log("registerWithIdentity - error message:", errorMessage);
341
+ console.log("registerWithIdentity - error stack:", error.stack);
342
+ // Try to extract more specific error information
343
+ if (errorMessage.includes("CannotExceedMaxTotalRateLimit")) {
344
+ console.error("Registration failed: Cannot exceed maximum total rate limit");
345
+ }
346
+ else if (errorMessage.includes("InvalidIdCommitment")) {
347
+ console.error("Registration failed: Invalid ID commitment");
348
+ }
349
+ else if (errorMessage.includes("InvalidMembershipRateLimit")) {
350
+ console.error("Registration failed: Invalid membership rate limit");
351
+ }
352
+ else if (errorMessage.includes("execution reverted")) {
353
+ // Generic revert
354
+ console.error("Contract execution reverted. Check contract requirements.");
355
+ }
356
+ else {
357
+ console.error(`Error in registerWithIdentity: ${errorMessage}`);
358
+ }
359
+ }
360
+ else {
361
+ console.error("Unknown error in registerWithIdentity");
362
+ }
363
+ // Re-throw the error to allow callers to handle it
364
+ throw error;
321
365
  }
322
366
  }
323
367
  /**
@@ -426,14 +470,21 @@ class RLNContract {
426
470
  return await tx.wait();
427
471
  }
428
472
  async registerMembership(idCommitment, rateLimit = DEFAULT_RATE_LIMIT) {
429
- if (rateLimit < RATE_LIMIT_PARAMS.MIN_RATE ||
430
- rateLimit > RATE_LIMIT_PARAMS.MAX_RATE) {
431
- throw new Error(`Rate limit must be between ${RATE_LIMIT_PARAMS.MIN_RATE} and ${RATE_LIMIT_PARAMS.MAX_RATE}`);
473
+ try {
474
+ if (rateLimit < RATE_LIMIT_PARAMS.MIN_RATE ||
475
+ rateLimit > RATE_LIMIT_PARAMS.MAX_RATE) {
476
+ throw new Error(`Rate limit must be between ${RATE_LIMIT_PARAMS.MIN_RATE} and ${RATE_LIMIT_PARAMS.MAX_RATE}`);
477
+ }
478
+ // Try to register
479
+ return this.contract.register(idCommitment, rateLimit, []);
480
+ }
481
+ catch (error) {
482
+ console.error("Error in registerMembership:", error);
483
+ // Run debug to help diagnose the issue
484
+ await this.debugRegistration(idCommitment, rateLimit);
485
+ // Re-throw the error
486
+ throw error;
432
487
  }
433
- console.log("registering membership", idCommitment, rateLimit);
434
- const txn = this.contract.register(idCommitment, rateLimit, []);
435
- console.log("txn", txn);
436
- return txn;
437
488
  }
438
489
  async getMemberIndex(idCommitment) {
439
490
  try {
@@ -448,6 +499,74 @@ class RLNContract {
448
499
  return undefined;
449
500
  }
450
501
  }
502
+ /**
503
+ * Debug function to check why a registration might fail
504
+ * @param idCommitment The ID commitment to check
505
+ * @param rateLimit The rate limit to check
506
+ */
507
+ async debugRegistration(idCommitment, rateLimit) {
508
+ console.log("=== DEBUG REGISTRATION ===");
509
+ console.log(`ID Commitment: ${idCommitment}`);
510
+ console.log(`Rate Limit: ${rateLimit}`);
511
+ // Check if ID commitment is already registered
512
+ try {
513
+ const existingIndex = await this.getMemberIndex(idCommitment);
514
+ if (existingIndex) {
515
+ console.error(`ERROR: ID commitment is already registered with index ${existingIndex}`);
516
+ }
517
+ else {
518
+ console.log("ID commitment is not yet registered ✓");
519
+ }
520
+ }
521
+ catch (error) {
522
+ console.error("Error checking if ID commitment is registered:", error);
523
+ }
524
+ // Check rate limit constraints
525
+ try {
526
+ const minRateLimit = await this.getMinRateLimit();
527
+ const maxRateLimit = await this.getMaxRateLimit();
528
+ const maxTotalRateLimit = await this.getMaxTotalRateLimit();
529
+ const currentTotalRateLimit = await this.getCurrentTotalRateLimit();
530
+ const remainingRateLimit = await this.getRemainingTotalRateLimit();
531
+ console.log(`Min Rate Limit: ${minRateLimit}`);
532
+ console.log(`Max Rate Limit: ${maxRateLimit}`);
533
+ console.log(`Max Total Rate Limit: ${maxTotalRateLimit}`);
534
+ console.log(`Current Total Rate Limit: ${currentTotalRateLimit}`);
535
+ console.log(`Remaining Rate Limit: ${remainingRateLimit}`);
536
+ if (rateLimit < minRateLimit) {
537
+ console.error(`ERROR: Rate limit ${rateLimit} is below minimum ${minRateLimit}`);
538
+ }
539
+ if (rateLimit > maxRateLimit) {
540
+ console.error(`ERROR: Rate limit ${rateLimit} exceeds maximum ${maxRateLimit}`);
541
+ }
542
+ if (rateLimit > remainingRateLimit) {
543
+ console.error(`ERROR: Rate limit ${rateLimit} exceeds remaining capacity ${remainingRateLimit}`);
544
+ }
545
+ }
546
+ catch (error) {
547
+ console.error("Error checking rate limit constraints:", error);
548
+ }
549
+ // Try to estimate gas for the transaction to see if it would revert
550
+ try {
551
+ await this.contract.estimateGas.register(idCommitment, rateLimit, []);
552
+ console.log("Transaction gas estimation succeeded ✓");
553
+ }
554
+ catch (error) {
555
+ console.error("Transaction would revert with error:", error);
556
+ // Try to extract more specific error information
557
+ const errorMessage = error.message;
558
+ if (errorMessage.includes("CannotExceedMaxTotalRateLimit")) {
559
+ console.error("Cannot exceed maximum total rate limit");
560
+ }
561
+ else if (errorMessage.includes("InvalidIdCommitment")) {
562
+ console.error("Invalid ID commitment format");
563
+ }
564
+ else if (errorMessage.includes("InvalidMembershipRateLimit")) {
565
+ console.error("Invalid membership rate limit");
566
+ }
567
+ }
568
+ console.log("=== END DEBUG ===");
569
+ }
451
570
  }
452
571
  // These values should be tested on other networks
453
572
  const FETCH_CHUNK = 5;