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 contract
} = options; } = options;
if ( this.validateRateLimit(rateLimit);
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.rateLimit = rateLimit; this.rateLimit = rateLimit;
const initialRoot = rlnInstance.zerokit.getMerkleRoot(); const initialRoot = rlnInstance.zerokit.getMerkleRoot();
@ -118,6 +110,21 @@ export class RLNContract {
this._membersExpiredFilter = this.contract.filters.MembershipExpired(); 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 * Gets the current rate limit for this contract instance
*/ */
@ -192,6 +199,7 @@ export class RLNContract {
* @param newRateLimit The new rate limit to use * @param newRateLimit The new rate limit to use
*/ */
public async setRateLimit(newRateLimit: number): Promise<void> { public async setRateLimit(newRateLimit: number): Promise<void> {
this.validateRateLimit(newRateLimit);
this.rateLimit = newRateLimit; this.rateLimit = newRateLimit;
} }
@ -469,7 +477,8 @@ export class RLNContract {
membership: { membership: {
address, address,
treeIndex: membershipId, treeIndex: membershipId,
chainId: network.chainId chainId: network.chainId,
rateLimit: decodedData.membershipRateLimit.toNumber()
} }
}; };
} catch (error) { } catch (error) {
@ -594,7 +603,8 @@ export class RLNContract {
membership: { membership: {
address, address,
treeIndex: membershipId, treeIndex: membershipId,
chainId: network.chainId chainId: network.chainId,
rateLimit: decodedData.membershipRateLimit.toNumber()
} }
}; };
} catch (error) { } catch (error) {
@ -669,16 +679,9 @@ export class RLNContract {
public async registerMembership( public async registerMembership(
idCommitment: string, idCommitment: string,
rateLimit: number = DEFAULT_RATE_LIMIT rateLimit: number = this.rateLimit
): Promise<ethers.ContractTransaction> { ): Promise<ethers.ContractTransaction> {
if ( this.validateRateLimit(rateLimit);
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}`
);
}
return this.contract.register(idCommitment, rateLimit, []); return this.contract.register(idCommitment, rateLimit, []);
} }

View File

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

View File

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