chore: add rateLimit to MembershipInfo

This commit is contained in:
Danish Arora 2025-03-27 10:54:51 +05:30
parent 5f980cb3a7
commit 76f86de9c7
No known key found for this signature in database
GPG Key ID: 1C6EF37CDAE1426E
3 changed files with 26 additions and 21 deletions

View File

@ -95,15 +95,7 @@ export class RLNContract {
contract
} = options;
if (
rateLimit < RATE_LIMIT_PARAMS.MIN_RATE ||
rateLimit > RATE_LIMIT_PARAMS.MAX_RATE
) {
throw new Error(
`Rate limit must be between ${RATE_LIMIT_PARAMS.MIN_RATE} and ${RATE_LIMIT_PARAMS.MAX_RATE} messages per epoch`
);
}
this.validateRateLimit(rateLimit);
this.rateLimit = rateLimit;
const initialRoot = rlnInstance.zerokit.getMerkleRoot();
@ -118,6 +110,21 @@ export class RLNContract {
this._membersExpiredFilter = this.contract.filters.MembershipExpired();
}
/**
* Validates that the rate limit is within the allowed range
* @throws Error if the rate limit is outside the allowed range
*/
private validateRateLimit(rateLimit: number): void {
if (
rateLimit < RATE_LIMIT_PARAMS.MIN_RATE ||
rateLimit > RATE_LIMIT_PARAMS.MAX_RATE
) {
throw new Error(
`Rate limit must be between ${RATE_LIMIT_PARAMS.MIN_RATE} and ${RATE_LIMIT_PARAMS.MAX_RATE} messages per epoch`
);
}
}
/**
* Gets the current rate limit for this contract instance
*/
@ -192,6 +199,7 @@ export class RLNContract {
* @param newRateLimit The new rate limit to use
*/
public async setRateLimit(newRateLimit: number): Promise<void> {
this.validateRateLimit(newRateLimit);
this.rateLimit = newRateLimit;
}
@ -469,7 +477,8 @@ export class RLNContract {
membership: {
address,
treeIndex: membershipId,
chainId: network.chainId
chainId: network.chainId,
rateLimit: decodedData.membershipRateLimit.toNumber()
}
};
} catch (error) {
@ -594,7 +603,8 @@ export class RLNContract {
membership: {
address,
treeIndex: membershipId,
chainId: network.chainId
chainId: network.chainId,
rateLimit: decodedData.membershipRateLimit.toNumber()
}
};
} catch (error) {
@ -669,16 +679,9 @@ export class RLNContract {
public async registerMembership(
idCommitment: string,
rateLimit: number = DEFAULT_RATE_LIMIT
rateLimit: number = this.rateLimit
): Promise<ethers.ContractTransaction> {
if (
rateLimit < RATE_LIMIT_PARAMS.MIN_RATE ||
rateLimit > RATE_LIMIT_PARAMS.MAX_RATE
) {
throw new Error(
`Rate limit must be between ${RATE_LIMIT_PARAMS.MIN_RATE} and ${RATE_LIMIT_PARAMS.MAX_RATE}`
);
}
this.validateRateLimit(rateLimit);
return this.contract.register(idCommitment, rateLimit, []);
}

View File

@ -274,7 +274,8 @@ export class Keystore {
membership: {
treeIndex: _.get(obj, "treeIndex"),
chainId: _.get(obj, "membershipContract.chainId"),
address: _.get(obj, "membershipContract.address")
address: _.get(obj, "membershipContract.address"),
rateLimit: _.get(obj, "membershipContract.rateLimit")
}
};
} catch (err) {

View File

@ -11,6 +11,7 @@ export type MembershipInfo = {
chainId: number;
address: string;
treeIndex: number;
rateLimit: number;
};
export type KeystoreEntity = {