Skip to content

Commit

Permalink
Fixes failiing acceptance tests and improves setup
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantina Blazhukova <[email protected]>
  • Loading branch information
konstantinabl committed Oct 11, 2024
1 parent 52480dc commit 6efbb4a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ export class IPAddressHbarSpendingPlanRepository {
}
}

/**
* Gets all IP address spending plans from the cache.
*
* @param {RequestDetails} requestDetails - The request details for logging and tracking.
* @returns {Promise<IPAddressHbarSpendingPlan[]>} - A promise that resolves with an array of IP address spending plans.
*/
async getAllPlans(requestDetails: RequestDetails): Promise<IPAddressHbarSpendingPlan[]> {
const pattern = `${this.collectionKey}:*`;
const keys = await this.cache.keys(pattern, 'getAllPlans', requestDetails);
const plans = await Promise.all(
keys.map((key) => this.cache.getAsync<IIPAddressHbarSpendingPlan>(key, 'getAllPlans', requestDetails)),

Check warning on line 115 in packages/relay/src/lib/db/repositories/hbarLimiter/ipAddressHbarSpendingPlanRepository.ts

View check run for this annotation

Codecov / codecov/patch

packages/relay/src/lib/db/repositories/hbarLimiter/ipAddressHbarSpendingPlanRepository.ts#L112-L115

Added lines #L112 - L115 were not covered by tests
);
return plans.filter((plan) => plan !== null).map((plan) => new IPAddressHbarSpendingPlan(plan));

Check warning on line 117 in packages/relay/src/lib/db/repositories/hbarLimiter/ipAddressHbarSpendingPlanRepository.ts

View check run for this annotation

Codecov / codecov/patch

packages/relay/src/lib/db/repositories/hbarLimiter/ipAddressHbarSpendingPlanRepository.ts#L117

Added line #L117 was not covered by tests
}

/**
* Finds an {@link IPAddressHbarSpendingPlan} for an IP address.
*
Expand Down
5 changes: 1 addition & 4 deletions packages/relay/src/lib/services/hbarLimitService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,7 @@ export class HbarLimitService implements IHbarLimitService {
}
}

//TODO: This is a temporary solution to allow for testing should be swapped and preferably not used in production
const shouldUseIP =
process.env.SHOULD_USE_IP_REPOSITORY !== undefined ? process.env.SHOULD_USE_IP_REPOSITORY : 'true';
if (ipAddress && !shouldUseIP) {
if (ipAddress) {
try {
return await this.getSpendingPlanByIPAddress(requestDetails);
} catch (error) {
Expand Down
31 changes: 27 additions & 4 deletions packages/server/tests/acceptance/hbarLimiter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ import { CacheService } from '@hashgraph/json-rpc-relay/dist/lib/services/cacheS
import { HbarSpendingPlanRepository } from '@hashgraph/json-rpc-relay/dist/lib/db/repositories/hbarLimiter/hbarSpendingPlanRepository';
import { EthAddressHbarSpendingPlanRepository } from '@hashgraph/json-rpc-relay/dist/lib/db/repositories/hbarLimiter/ethAddressHbarSpendingPlanRepository';
import { IPAddressHbarSpendingPlanRepository } from '@hashgraph/json-rpc-relay/dist/lib/db/repositories/hbarLimiter/ipAddressHbarSpendingPlanRepository';
import { SubscriptionType } from '@hashgraph/json-rpc-relay/dist/lib/db/types/hbarLimiter/subscriptionType';

config({ path: resolve(__dirname, '../localAcceptance.env') });
const DOT_ENV = dotenv.parse(fs.readFileSync(resolve(__dirname, '../localAcceptance.env')));
let hbarSpendingPlanRepository: HbarSpendingPlanRepository,
ethAddressSpendingPlanRepository: EthAddressHbarSpendingPlanRepository,
ipSpendingPlanRepository: IPAddressHbarSpendingPlanRepository;

describe('@hbarlimiter HBAR Limiter Acceptance Tests', function () {
// @ts-ignore
Expand Down Expand Up @@ -186,6 +190,10 @@ describe('@hbarlimiter HBAR Limiter Acceptance Tests', function () {
before(async function () {
// Restart the relay to reset the limits
await global.restartLocalRelay();
await cacheService.clear(requestDetails);
hbarSpendingPlanRepository = new HbarSpendingPlanRepository(cacheService, logger);
ethAddressSpendingPlanRepository = new EthAddressHbarSpendingPlanRepository(cacheService, logger);
ipSpendingPlanRepository = new IPAddressHbarSpendingPlanRepository(cacheService, logger);

logger.info(`${requestDetails.formattedRequestId} Creating accounts`);
logger.info(
Expand Down Expand Up @@ -386,9 +394,16 @@ describe('@hbarlimiter HBAR Limiter Acceptance Tests', function () {

const lastRemainingHbars = remainingHbarsBefore;
let spentToday = 0;
const hbarSpendingPlanRepository = new HbarSpendingPlanRepository(cacheService, logger);
const ethAddressSpendingPlanRepository = new EthAddressHbarSpendingPlanRepository(cacheService, logger);
const ipSpendingPlanRepository = new IPAddressHbarSpendingPlanRepository(cacheService, logger);

//Unlinking the ipAdress, since ipAddress when running tests in CI and locally is the same
const basicPlans = await hbarSpendingPlanRepository.findAllActiveBySubscriptionType(
SubscriptionType.BASIC,
requestDetails,
);
const ipPlans = await ipSpendingPlanRepository.getAllPlans(requestDetails);
const ipPlanWithId = await basicPlans.map((plan) => ipPlans.find((ipPlan) => ipPlan.planId === plan.id));
const ipAddress = ipPlanWithId[0].ipAddress;
await ipSpendingPlanRepository.delete(ipAddress, requestDetails);
expect(ethAddressSpendingPlanRepository.findByAddress(accounts[2].address, requestDetails)).to.be.rejected;
expect(remainingHbarsBefore).to.be.gt(0);
let spendingPlanId;
Expand Down Expand Up @@ -423,13 +438,21 @@ describe('@hbarlimiter HBAR Limiter Acceptance Tests', function () {
});

it('should create a BASIC spending plan for a new user', async function () {
const ethAddressSpendingPlanRepository = new EthAddressHbarSpendingPlanRepository(cacheService, logger);
const parentContract = await deployContract(parentContractJson, accounts[0].wallet);
const parentContractAddress = parentContract.target as string;
global.logger.trace(
`${requestDetails.formattedRequestId} Deploy parent contract on address ${parentContractAddress}`,
);

//Unlinking the ipAdress, since ipAddress when running tests in CI and locally is the same
const basicPlans = await hbarSpendingPlanRepository.findAllActiveBySubscriptionType(
SubscriptionType.BASIC,
requestDetails,
);
const ipPlans = await ipSpendingPlanRepository.getAllPlans(requestDetails);
const ipPlanWithId = await basicPlans.map((plan) => ipPlans.find((ipPlan) => ipPlan.planId === plan.id));
const ipAddress = ipPlanWithId[0].ipAddress;
await ipSpendingPlanRepository.delete(ipAddress, requestDetails);
expect(ethAddressSpendingPlanRepository.findByAddress(accounts[3].address, requestDetails)).to.be.rejected;
const gasPrice = await relay.gasPrice(requestId);
const transaction = {
Expand Down
1 change: 0 additions & 1 deletion packages/server/tests/localAcceptance.env
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@ SERVER_REQUEST_TIMEOUT_MS=60000
MEMWATCH_ENABLED=true
WRITE_SNAPSHOT_ON_MEMORY_LEAK=false
HBAR_DAILY_LIMIT_BASIC=11000000000
SHOULD_USE_IP_REPOSITORY = false

0 comments on commit 6efbb4a

Please sign in to comment.