-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
Signed-off-by: Ivo Yankov <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* | ||
* Hedera JSON RPC Relay | ||
* | ||
* Copyright (C) 2022-2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
import { IIPAddressHbarSpendingPlan } from '../../types/hbarLimiter/ipAddressHbarSpendingPlan'; | ||
|
||
export class IPAddressHbarSpendingPlan implements IIPAddressHbarSpendingPlan { | ||
ipAddress: string; | ||
planId: string; | ||
|
||
constructor(data: IIPAddressHbarSpendingPlan) { | ||
this.ipAddress = data.ipAddress; | ||
this.planId = data.planId; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* | ||
* Hedera JSON RPC Relay | ||
* | ||
* Copyright (C) 2022-2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
import { CacheService } from '../../../services/cacheService/cacheService'; | ||
import { Logger } from 'pino'; | ||
import { IIPAddressHbarSpendingPlan } from '../../types/hbarLimiter/ipAddressHbarSpendingPlan'; | ||
import { IPAddressHbarSpendingPlanNotFoundError } from '../../types/hbarLimiter/errors'; | ||
import { IPAddressHbarSpendingPlan } from '../../entities/hbarLimiter/ipAddressHbarSpendingPlan'; | ||
|
||
export class IPAddressHbarSpendingPlanRepository { | ||
private readonly collectionKey = 'ipAddressHbarSpendingPlan'; | ||
private readonly threeMonthsInMillis = 90 * 24 * 60 * 60 * 1000; | ||
Check warning on line 29 in packages/relay/src/lib/db/repositories/hbarLimiter/ipAddressHbarSpendingPlanRepository.ts
|
||
|
||
/** | ||
* The cache service used for storing data. | ||
* @private | ||
*/ | ||
private readonly cache: CacheService; | ||
|
||
/** | ||
* The logger used for logging all output from this class. | ||
* @private | ||
*/ | ||
private readonly logger: Logger; | ||
|
||
constructor(cache: CacheService, logger: Logger) { | ||
this.cache = cache; | ||
this.logger = logger; | ||
Check warning on line 45 in packages/relay/src/lib/db/repositories/hbarLimiter/ipAddressHbarSpendingPlanRepository.ts
|
||
} | ||
|
||
/** | ||
* Finds an {@link IPAddressHbarSpendingPlan} for an IP address. | ||
* | ||
* @param {string} ipAddress - The IP address to search for. | ||
* @returns {Promise<IPAddressHbarSpendingPlan>} - The associated plan for the IP address. | ||
*/ | ||
async findByAddress(ipAddress: string): Promise<IPAddressHbarSpendingPlan> { | ||
const key = this.getKey(ipAddress); | ||
const addressPlan = await this.cache.getAsync<IIPAddressHbarSpendingPlan>(key, 'findByAddress'); | ||
Check warning on line 56 in packages/relay/src/lib/db/repositories/hbarLimiter/ipAddressHbarSpendingPlanRepository.ts
|
||
if (!addressPlan) { | ||
throw new IPAddressHbarSpendingPlanNotFoundError(ipAddress); | ||
Check warning on line 58 in packages/relay/src/lib/db/repositories/hbarLimiter/ipAddressHbarSpendingPlanRepository.ts
|
||
} | ||
this.logger.trace(`Retrieved IPAddressHbarSpendingPlan with address ${ipAddress}`); | ||
return new IPAddressHbarSpendingPlan(addressPlan); | ||
Check warning on line 61 in packages/relay/src/lib/db/repositories/hbarLimiter/ipAddressHbarSpendingPlanRepository.ts
|
||
} | ||
|
||
/** | ||
* Saves an {@link IPAddressHbarSpendingPlan} to the cache, linking the plan to the IP address. | ||
* | ||
* @param {IIPAddressHbarSpendingPlan} addressPlan - The plan to save. | ||
* @returns {Promise<void>} - A promise that resolves when the IP address is linked to the plan. | ||
*/ | ||
async save(addressPlan: IIPAddressHbarSpendingPlan): Promise<void> { | ||
const key = this.getKey(addressPlan.ipAddress); | ||
await this.cache.set(key, addressPlan, 'save', this.threeMonthsInMillis); | ||
this.logger.trace(`Saved IPAddressHbarSpendingPlan with address ${addressPlan.ipAddress}`); | ||
Check warning on line 73 in packages/relay/src/lib/db/repositories/hbarLimiter/ipAddressHbarSpendingPlanRepository.ts
|
||
} | ||
|
||
/** | ||
* Deletes an {@link IPAddressHbarSpendingPlan} from the cache, unlinking the plan from the IP address. | ||
* | ||
* @param {string} ipAddress - The IP address to unlink the plan from. | ||
* @returns {Promise<void>} - A promise that resolves when the IP address is unlinked from the plan. | ||
*/ | ||
async delete(ipAddress: string): Promise<void> { | ||
const key = this.getKey(ipAddress); | ||
await this.cache.delete(key, 'delete'); | ||
this.logger.trace(`Deleted IPAddressHbarSpendingPlan with address ${ipAddress}`); | ||
Check warning on line 85 in packages/relay/src/lib/db/repositories/hbarLimiter/ipAddressHbarSpendingPlanRepository.ts
|
||
} | ||
|
||
/** | ||
* Gets the cache key for an {@link IPAddressHbarSpendingPlan}. | ||
* | ||
* @param {string} ipAddress - The IP address to get the key for. | ||
* @private | ||
*/ | ||
private getKey(ipAddress: string): string { | ||
return `${this.collectionKey}:${ipAddress}`; | ||
Check warning on line 95 in packages/relay/src/lib/db/repositories/hbarLimiter/ipAddressHbarSpendingPlanRepository.ts
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* | ||
* Hedera JSON RPC Relay | ||
* | ||
* Copyright (C) 2022-2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
export interface IIPAddressHbarSpendingPlan { | ||
ipAddress: string; | ||
planId: string; | ||
} |