Skip to content

Commit fd6c6c7

Browse files
authored
Add gas cache logic to release 0.1 (#182)
Release 0.1 is missing cache logic for gas calls. - Add cache for feeHistory - Add cache for gasPrice Signed-off-by: Nana-EC <[email protected]>
1 parent d145b0b commit fd6c6c7

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*-
2+
*
3+
* Hedera JSON RPC Relay
4+
*
5+
* Copyright (C) 2022 Hedera Hashgraph, LLC
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
enum CACHE_KEY {
22+
GAS_PRICE = 'gas_price',
23+
FEE_HISTORY = 'fee_history'
24+
}
25+
enum CACHE_TTL {
26+
ONE_HOUR = 3_600_000
27+
}
28+
29+
export default {
30+
TINYBAR_TO_WEIBAR_COEF: 10_000_000_000,
31+
CACHE_KEY,
32+
CACHE_TTL,
33+
};

packages/relay/src/lib/eth.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ import { Block, CachedBlock, Transaction, Log } from './model';
2626
import { MirrorNode } from './mirrorNode';
2727
import { MirrorNodeClient, SDKClient } from './clients';
2828
import { JsonRpcError, predefined } from './errors';
29+
import constants from './constants';
2930

31+
const cache = require('js-cache');
3032
const createHash = require('keccak');
3133

3234
/**
@@ -114,13 +116,21 @@ export class EthImpl implements Eth {
114116
* Gets the fee history.
115117
*/
116118
async feeHistory(blockCount: number, newestBlock: string, rewardPercentiles: Array<number> | null) {
117-
this.logger.trace('feeHistory()');
119+
this.logger.trace(`feeHistory(blockCount=${blockCount}, newestBlock=${newestBlock}, rewardPercentiles=${rewardPercentiles})`);
118120
try {
119-
const feeWeibars = await this.getFeeWeibars();
121+
let feeHistory: object | undefined = cache.get(constants.CACHE_KEY.FEE_HISTORY);
122+
if (!feeHistory) {
123+
const feeWeibars = await this.getFeeWeibars();
120124

121-
return await this.mirrorNode.getFeeHistory(feeWeibars, blockCount, newestBlock, rewardPercentiles);
125+
feeHistory = await this.mirrorNode.getFeeHistory(feeWeibars, blockCount, newestBlock, rewardPercentiles);
126+
127+
this.logger.trace(`caching ${constants.CACHE_KEY.FEE_HISTORY} for ${constants.CACHE_TTL.ONE_HOUR} ms`);
128+
cache.set(constants.CACHE_KEY.FEE_HISTORY, feeHistory, constants.CACHE_TTL.ONE_HOUR);
129+
}
130+
131+
return feeHistory;
122132
} catch (e) {
123-
this.logger.trace(e);
133+
this.logger.error(e, 'Error constructing default feeHistory');
124134
}
125135
}
126136

@@ -187,12 +197,21 @@ export class EthImpl implements Eth {
187197
// FIXME: This should come from the mainnet and get cached. The gas price does change dynamically based on
188198
// the price of the HBAR relative to the USD. It only needs to be updated hourly.
189199
this.logger.trace('gasPrice()');
190-
return this.getFeeWeibars()
191-
.then((weiBars) => EthImpl.numberTo0x((weiBars)))
192-
.catch((e: any) => {
193-
this.logger.trace(e);
194-
throw e;
195-
});
200+
try {
201+
let gasPrice: number | undefined = cache.get(constants.CACHE_KEY.GAS_PRICE);
202+
203+
if (!gasPrice) {
204+
gasPrice = await this.getFeeWeibars();
205+
206+
this.logger.trace(`caching ${constants.CACHE_KEY.GAS_PRICE} for ${constants.CACHE_TTL.ONE_HOUR} ms`);
207+
cache.set(constants.CACHE_KEY.GAS_PRICE, gasPrice, constants.CACHE_TTL.ONE_HOUR);
208+
}
209+
210+
return EthImpl.numberTo0x(gasPrice);
211+
} catch (error) {
212+
this.logger.trace(error);
213+
throw error;
214+
}
196215
}
197216

198217
/**

0 commit comments

Comments
 (0)