From 9288e0ca9fc80dd969551408a5b29a3e78f658eb Mon Sep 17 00:00:00 2001 From: lukelee-sl <109538178+lukelee-sl@users.noreply.github.com> Date: Mon, 25 Jul 2022 07:50:46 -0700 Subject: [PATCH] remove mirrorNode.ts and dependencies (#374) * remove mirrorNode.ts and dependencies Signed-off-by: lukelee-sl * address sonarcloud issue Signed-off-by: lukelee-sl --- packages/relay/src/lib/eth.ts | 31 +----- packages/relay/src/lib/mirrorNode.ts | 143 --------------------------- packages/relay/src/lib/model.ts | 21 ---- packages/relay/src/lib/relay.ts | 4 - packages/relay/tests/lib/eth.spec.ts | 5 +- 5 files changed, 4 insertions(+), 200 deletions(-) delete mode 100755 packages/relay/src/lib/mirrorNode.ts diff --git a/packages/relay/src/lib/eth.ts b/packages/relay/src/lib/eth.ts index 7bedc9023..5a51a7062 100644 --- a/packages/relay/src/lib/eth.ts +++ b/packages/relay/src/lib/eth.ts @@ -22,8 +22,7 @@ import { Eth } from '../index'; import { ContractId, Status, Hbar, EthereumTransaction } from '@hashgraph/sdk'; import { BigNumber } from '@hashgraph/sdk/lib/Transfer'; import { Logger } from 'pino'; -import { Block, CachedBlock, Transaction, Log } from './model'; -import { MirrorNode } from './mirrorNode'; +import { Block, Transaction, Log } from './model'; import { MirrorNodeClient, SDKClient } from './clients'; import { JsonRpcError, predefined } from './errors'; import constants from './constants'; @@ -76,12 +75,6 @@ export class EthImpl implements Eth { */ private readonly sdkClient: SDKClient; - /** - * The mirror node mock - * @private - */ - private readonly mirrorNode: MirrorNode; - /** * The interface through which we interact with the mirror node * @private @@ -109,20 +102,17 @@ export class EthImpl implements Eth { /** * Create a new Eth implementation. * @param nodeClient - * @param mirrorNode * @param mirrorNodeClient * @param logger * @param chain */ constructor( nodeClient: SDKClient, - mirrorNode: MirrorNode, mirrorNodeClient: MirrorNodeClient, logger: Logger, chain: string ) { this.sdkClient = nodeClient; - this.mirrorNode = mirrorNode; this.mirrorNodeClient = mirrorNodeClient; this.logger = logger; this.chain = chain; @@ -669,24 +659,7 @@ export class EthImpl implements Eth { if (record.ethereumHash == null) { throw new Error('The ethereumHash can never be null for an ethereum transaction, and yet it was!!'); } - const txHash = EthImpl.prepend0x(Buffer.from(record.ethereumHash).toString('hex')); - - // If the transaction succeeded, create a new block for the transaction. - const mostRecentBlock = await this.mirrorNode.getMostRecentBlock(); - this.logger.debug('mostRecentBlock=%o', mostRecentBlock); - let block = mostRecentBlock; - if (record.receipt.status == Status.Success) { - block = new CachedBlock(mostRecentBlock, txHash); - this.mirrorNode.storeBlock(block); - } - - // Create a receipt. Register the receipt in the cache and return the tx hash - if (block == null) { - this.logger.error('Failed to get a block for transaction'); - return ''; - } - - return txHash; + return EthImpl.prepend0x(Buffer.from(record.ethereumHash).toString('hex')); } catch (e) { this.logger.error(e, 'Failed sendRawTransaction during record retrieval for transaction %s, returning computed hash', transaction); diff --git a/packages/relay/src/lib/mirrorNode.ts b/packages/relay/src/lib/mirrorNode.ts deleted file mode 100755 index 6d60ed6b9..000000000 --- a/packages/relay/src/lib/mirrorNode.ts +++ /dev/null @@ -1,143 +0,0 @@ -/*- - * - * Hedera JSON RPC Relay - * - * Copyright (C) 2022 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 { Logger } from "pino"; -// Used for temporary purposes to store block info. As the mirror node supports the APIs, we will remove this. -import { Block, CachedBlock } from './model'; - -export class MirrorNode { - // A FAKE implementation until mirror node is integrated and ready. - // Keeps all blocks in memory. We're going to do our own bookkeeping - // to keep track of blocks and only create them once per transaction. - // So it may have been 20 minutes and if there were no transactions - // then we don't advance the block list. The first block is block 0, - // so we can quickly look them up in the array. Yes, we will eventually - // end up running out of memory. - private static MOST_RECENT_BLOCK_NUMBER_KEY = "mostRecentBlockNumber"; - private static MOST_RECENT_BLOCK_KEY = "mostRecentBlock"; - private readonly store: Map = new Map(); - - - /** - * The logger used for logging all output from this class. - * @private - */ - private readonly logger: Logger; - - constructor(logger: Logger) { - this.logger = logger; - - // FIXME: Create an empty genesis block (which has no transactions!) - // to preload the system. - if (this.store.has(MirrorNode.MOST_RECENT_BLOCK_KEY)) { - this.logger.trace("Cache recent block"); - } else { - this.logger.trace("Fresh start, creating genesis block with no transactions"); - const genesisBlock = new CachedBlock(null, null); - this.storeBlock(genesisBlock); - } - } - - public async getFeeHistory(fee: number, _blockCount: number, _newestBlock: string, rewardPercentiles: Array | null) { - // FIXME: This is a fake implementation. It works for now, but should - // actually delegate to the mirror node. - this.logger.trace('getFeeHistory()'); - - const mostRecentBlockNumber = await this.getMostRecentBlockNumber(); - this.logger.debug('computing fee history for mostRecentBlockNumber=%d', mostRecentBlockNumber); - const mostRecentBlocks: Block[] = []; - for (let blockNumber = Math.max(0, mostRecentBlockNumber - 9); blockNumber <= mostRecentBlockNumber; blockNumber++) { - const block = await this.getBlockByNumber(blockNumber); - this.logger.trace("block for %d is %o", blockNumber, block); - if (block != null) { - mostRecentBlocks.push(block); - } else { - this.logger.error('Error: unable to find block by number %d', blockNumber); - } - } - this.logger.debug('Computing fee history based on the last %d blocks', mostRecentBlocks.length); - - const feeHistoryResponse = { - baseFeePerGasArray: Array(mostRecentBlocks.length).fill('0x' + fee.toString(16)), - gasUsedRatioArray: Array(mostRecentBlocks.length).fill('0.5'), - oldestBlockNumber: mostRecentBlocks[0].number - }; - - if (rewardPercentiles) { - feeHistoryResponse['reward'] = Array(mostRecentBlocks.length).fill(Array(rewardPercentiles.length).fill("0x0")); - } - - return feeHistoryResponse; - } - - // FIXME this is for demo/temp purposes, remove it when the mirror node has real blocks - // that they get from the main net nodes - public storeBlock(block: CachedBlock) { - this.store.set(MirrorNode.MOST_RECENT_BLOCK_NUMBER_KEY, block.getNum()); - this.store.set(MirrorNode.MOST_RECENT_BLOCK_KEY, block); - this.store.set(block.getNum().toString(), block); - this.store.set(block.transactionHashes[0], block); - } - - public async getMostRecentBlockNumber(): Promise { - // FIXME: Fake implementation for now. Should go to the mirror node. - this.logger.trace('getMostRecentBlockNumber()'); - const num = this.store.get(MirrorNode.MOST_RECENT_BLOCK_NUMBER_KEY); - this.logger.debug('Latest block number: %s', num); - return num === undefined ? 0 : Number(num); - } - - public async getMostRecentBlock(): Promise { - // FIXME: Fake implementation for now. Should go to the mirror node. - this.logger.trace('getMostRecentBlock()'); - const block = this.store.get(MirrorNode.MOST_RECENT_BLOCK_KEY); - if (block === undefined) { - this.logger.debug("No blocks retrievable"); - return null; - } else { - this.logger.debug("Retrieved block number: %s", block.getNum()); - return block; - } - } - - // TODO: mirror node method is not yet implemented - public async getBlockByNumber(blockNumber: number): Promise { - // FIXME: This needs to be reimplemented to go to the mirror node. - // return this.request(`blocks/${blockNumber}`); - this.logger.trace('getBlockByNumber(blockNumber=%d)', blockNumber); - const block = this.store.get(blockNumber.toString()); - return block === undefined ? null : block; - } - - public async getBlockByHash(hash: string, showDetails: boolean): Promise { - // FIXME: This needs to be reimplemented to go to the mirror node. - this.logger.trace('getBlockByHash(hash=%s, showDetails=%o)', hash, showDetails); - - // We don't support this yet, so log a warning in case somebody tries to use it - // we can learn of that usage. - if (showDetails) { - this.logger.warn('getBlockByHash does not yet support "showDetails"'); - } - - // Look up the block number by hash - const block = this.store.get(hash); - return block === undefined ? null : block; - } -} diff --git a/packages/relay/src/lib/model.ts b/packages/relay/src/lib/model.ts index e8311c177..7a998e743 100644 --- a/packages/relay/src/lib/model.ts +++ b/packages/relay/src/lib/model.ts @@ -80,27 +80,6 @@ export class Block { } } -export class CachedBlock extends Block { - public readonly parentBlock:Block|null; - public readonly transactionHashes:string[] = []; - - constructor(parentBlock:(null | Block), transactionHash:(string|null), args?:any) { - super(args); - this.parentBlock = parentBlock; - - const num = parentBlock == null ? 0 : parentBlock.getNum() + 1; - this.number = '0x' + Number(num).toString(16); - this.parentHash = parentBlock == null ? '0x0' : parentBlock.hash; - if (transactionHash) { - this.transactionHashes.push(transactionHash); - } - - const numberAsString = num.toString(); - const baseHash = "0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b"; - this.hash = baseHash.slice(0, baseHash.length - numberAsString.length) + numberAsString; - } -} - export class Receipt { public readonly transactionHash:string; public readonly transactionIndex:string; diff --git a/packages/relay/src/lib/relay.ts b/packages/relay/src/lib/relay.ts index 88fb59fc5..aab5fd899 100644 --- a/packages/relay/src/lib/relay.ts +++ b/packages/relay/src/lib/relay.ts @@ -26,7 +26,6 @@ import { NetImpl } from './net'; import { EthImpl } from './eth'; import { AccountId, Client, PrivateKey } from '@hashgraph/sdk'; import { Logger } from 'pino'; -import { MirrorNode } from './mirrorNode'; import { MirrorNodeClient, SDKClient } from './clients'; import { Registry } from 'prom-client'; @@ -57,8 +56,6 @@ export class RelayImpl implements Relay { this.web3Impl = new Web3Impl(this.clientMain); this.netImpl = new NetImpl(this.clientMain, chainId); - const mirrorNode = new MirrorNode(logger.child({ name: `mirror-node` })); - const mirrorNodeClient = new MirrorNodeClient( process.env.MIRROR_NODE_URL || '', logger.child({ name: `mirror-node` }), @@ -69,7 +66,6 @@ export class RelayImpl implements Relay { this.ethImpl = new EthImpl( sdkClient, - mirrorNode, mirrorNodeClient, logger.child({ name: 'relay-eth' }), chainId); diff --git a/packages/relay/tests/lib/eth.spec.ts b/packages/relay/tests/lib/eth.spec.ts index fb7c2e449..b853a46db 100644 --- a/packages/relay/tests/lib/eth.spec.ts +++ b/packages/relay/tests/lib/eth.spec.ts @@ -30,7 +30,6 @@ dotenv.config({ path: path.resolve(__dirname, '../test.env') }); import { RelayImpl } from '@hashgraph/json-rpc-relay'; import { EthImpl } from '../../src/lib/eth'; import { MirrorNodeClient } from '../../src/lib/clients/mirrorNodeClient'; -import { MirrorNode } from '../../src/lib/mirrorNode'; import { expectUnsupportedMethod } from '../helpers'; import pino from 'pino'; @@ -92,7 +91,7 @@ describe('Eth calls using MirrorNode', async function () { mirrorNodeInstance = new MirrorNodeClient(process.env.MIRROR_NODE_URL, logger.child({ name: `mirror-node` }), registry, instance); sdkClientStub = sinon.createStubInstance(SDKClient); // @ts-ignore - ethImpl = new EthImpl(sdkClientStub, new MirrorNode(logger.child({ name: `mirror-node-faux` })), mirrorNodeInstance, logger, '0x12a'); + ethImpl = new EthImpl(sdkClientStub, mirrorNodeInstance, logger, '0x12a'); }); this.beforeEach(() => { @@ -1326,7 +1325,7 @@ describe('Eth', async function () { let ethImpl: EthImpl; this.beforeAll(() => { // @ts-ignore - ethImpl = new EthImpl(null, null, mirrorNodeInstance, logger); + ethImpl = new EthImpl(null, mirrorNodeInstance, logger); }); const defaultTxHash = '0x4a563af33c4871b51a8b108aa2fe1dd5280a30dfb7236170ae5e5e7957eb6392';