Skip to content

Commit

Permalink
Merge branch 'main' into add-helper-method-for-overriding-env-variables
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Yanev <[email protected]>

# Conflicts:
#	packages/relay/tests/lib/repositories/hbarLimiter/ethAddressHbarSpendingPlanRepository.spec.ts
#	packages/relay/tests/lib/repositories/hbarLimiter/hbarSpendingPlanRepository.spec.ts
  • Loading branch information
victor-yanev committed Oct 8, 2024
2 parents d1bc498 + 4a69be9 commit 6c7e2c2
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 92 deletions.
11 changes: 9 additions & 2 deletions packages/relay/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*
*/

import { BigNumber } from 'bignumber.js';

enum CACHE_KEY {
ACCOUNT = 'account',
ETH_BLOCK_NUMBER = 'eth_block_number',
Expand Down Expand Up @@ -136,13 +138,18 @@ export default {
DURATION: 60000,
},

HBAR_RATE_LIMIT_DURATION: parseInt(process.env.HBAR_RATE_LIMIT_DURATION || '80000'),
HBAR_RATE_LIMIT_TINYBAR: parseInt(process.env.HBAR_RATE_LIMIT_TINYBAR || '11000000000'),
// TODO: Replace with actual values - https://github.com/hashgraph/hedera-json-rpc-relay/issues/2895
HBAR_RATE_LIMIT_DURATION: parseInt(process.env.HBAR_RATE_LIMIT_DURATION || '80000'), // 80 seconds
HBAR_RATE_LIMIT_TOTAL: BigNumber(process.env.HBAR_RATE_LIMIT_TINYBAR || '11000000000'), // 110 HBARs per 80 seconds
HBAR_RATE_LIMIT_BASIC: BigNumber(process.env.HBAR_DAILY_LIMIT_BASIC || '92592592'), // Equivalent of 1000 HBARs per day
HBAR_RATE_LIMIT_EXTENDED: BigNumber(process.env.HBAR_DAILY_LIMIT_EXTENDED || '925925925'), // Equivalent of 10000 HBARs per day
HBAR_RATE_LIMIT_PRIVILEGED: BigNumber(process.env.HBAR_DAILY_LIMIT_PRIVILEGED || '1851851850'), // Equivalent of 20000 HBARs per day
GAS_PRICE_TINY_BAR_BUFFER: parseInt(process.env.GAS_PRICE_TINY_BAR_BUFFER || '10000000000'),
WEB_SOCKET_PORT: process.env.WEB_SOCKET_PORT || 8546,
WEB_SOCKET_HTTP_PORT: process.env.WEB_SOCKET_HTTP_PORT || 8547,

RELAY_PORT: process.env.SERVER_PORT || 7546,
RELAY_HOST: process.env.SERVER_HOST || 'localhost',

FUNCTION_SELECTOR_CHAR_LENGTH: 10,
MIRROR_NODE_RETRY_DELAY_DEFAULT: 2000,
Expand Down
2 changes: 1 addition & 1 deletion packages/relay/tests/lib/ethGetBlockBy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ describe('eth_getBlockBy', async function () {
restMock = new MockAdapter(mirrorNodeInstance.getMirrorNodeRestInstance(), { onNoMatch: 'throwException' });

const duration = constants.HBAR_RATE_LIMIT_DURATION;
const total = constants.HBAR_RATE_LIMIT_TINYBAR;
const total = constants.HBAR_RATE_LIMIT_TOTAL.toNumber();
const hbarLimiter = new HbarLimit(logger.child({ name: 'hbar-rate-limit' }), Date.now(), total, duration, registry);
const eventEmitter = new EventEmitter();
hapiServiceInstance = new HAPIService(logger, registry, hbarLimiter, cacheService, eventEmitter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import chai, { expect } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import sinon from 'sinon';
import { EthAddressHbarSpendingPlanRepository } from '../../../../src/lib/db/repositories/hbarLimiter/ethAddressHbarSpendingPlanRepository';
import { CacheService } from '../../../../src/lib/services/cacheService/cacheService';
import pino from 'pino';
Expand All @@ -39,13 +40,16 @@ describe('EthAddressHbarSpendingPlanRepository', function () {
requestId: 'ethAddressHbarSpendingPlanRepositoryTest',
ipAddress: '0.0.0.0',
});
const ttl = 86_400_000; // 1 day

const tests = (isSharedCacheEnabled: boolean) => {
let cacheService: CacheService;
let cacheServiceSpy: sinon.SinonSpiedInstance<CacheService>;
let repository: EthAddressHbarSpendingPlanRepository;

before(() => {
before(async () => {
cacheService = new CacheService(logger.child({ name: 'CacheService' }), registry);
cacheServiceSpy = sinon.spy(cacheService);
repository = new EthAddressHbarSpendingPlanRepository(
cacheService,
logger.child({ name: 'EthAddressHbarSpendingPlanRepository' }),
Expand All @@ -54,10 +58,16 @@ describe('EthAddressHbarSpendingPlanRepository', function () {

if (isSharedCacheEnabled) {
useInMemoryRedisServer(logger, 6382);
} else {
overrideEnvsInMochaDescribe({ REDIS_ENABLED: 'false' });
}

after(() => {
cacheService.disconnectRedisClient();
afterEach(async () => {
await cacheService.clear(requestDetails);
});

after(async () => {
await cacheService.disconnectRedisClient();
});

describe('findByAddress', () => {
Expand All @@ -84,13 +94,21 @@ describe('EthAddressHbarSpendingPlanRepository', function () {
const ethAddress = '0x123';
const addressPlan: IEthAddressHbarSpendingPlan = { ethAddress, planId: uuidV4(randomBytes(16)) };

await repository.save(addressPlan, requestDetails);
await repository.save(addressPlan, requestDetails, ttl);
const result = await cacheService.getAsync<IEthAddressHbarSpendingPlan>(
`${repository['collectionKey']}:${ethAddress}`,
'test',
requestDetails,
);
expect(result).to.deep.equal(addressPlan);
sinon.assert.calledWith(
cacheServiceSpy.set,
`${repository['collectionKey']}:${ethAddress}`,
addressPlan,
'save',
requestDetails,
ttl,
);
});

it('overwrites an existing address plan', async () => {
Expand All @@ -100,13 +118,21 @@ describe('EthAddressHbarSpendingPlanRepository', function () {

const newPlanId = uuidV4(randomBytes(16));
const newAddressPlan: IEthAddressHbarSpendingPlan = { ethAddress, planId: newPlanId };
await repository.save(newAddressPlan, requestDetails);
await repository.save(newAddressPlan, requestDetails, ttl);
const result = await cacheService.getAsync<IEthAddressHbarSpendingPlan>(
`${repository['collectionKey']}:${ethAddress}`,
'test',
requestDetails,
);
expect(result).to.deep.equal(newAddressPlan);
sinon.assert.calledWith(
cacheServiceSpy.set,
`${repository['collectionKey']}:${ethAddress}`,
newAddressPlan,
'save',
requestDetails,
ttl,
);
});
});

Expand Down
Loading

0 comments on commit 6c7e2c2

Please sign in to comment.