|
| 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 | +import path from 'path'; |
| 22 | +import dotenv from 'dotenv'; |
| 23 | +import { expect } from 'chai'; |
| 24 | +import sinon from 'sinon'; |
| 25 | +import { Registry } from 'prom-client'; |
| 26 | +dotenv.config({ path: path.resolve(__dirname, '../test.env') }); |
| 27 | +import {SDKClient} from '../../src/lib/clients/sdkClient'; |
| 28 | +const registry = new Registry(); |
| 29 | +import pino from 'pino'; |
| 30 | +import {AccountId, Client, ContractCallQuery, PrivateKey, TransactionId, Hbar, Status} from "@hashgraph/sdk"; |
| 31 | +const logger = pino(); |
| 32 | +import constants from '../../src/lib/constants'; |
| 33 | + |
| 34 | +describe('SdkClient', async function () { |
| 35 | + this.timeout(20000); |
| 36 | + let sdkClient, client; |
| 37 | + |
| 38 | + before(() => { |
| 39 | + client = Client.forNetwork(JSON.parse(process.env.HEDERA_NETWORK)); |
| 40 | + client = client.setOperator( |
| 41 | + AccountId.fromString(process.env.OPERATOR_ID_MAIN), |
| 42 | + PrivateKey.fromString(process.env.OPERATOR_KEY_MAIN) |
| 43 | + ); |
| 44 | + |
| 45 | + sdkClient = new SDKClient(client, logger.child({ name: `consensus-node` }), registry); |
| 46 | + }) |
| 47 | + |
| 48 | + describe('increaseCostAndRetryExecution', async () => { |
| 49 | + let queryStub, contractCallQuery; |
| 50 | + const successResponse = "0x00001"; |
| 51 | + const costTinybars = 1000; |
| 52 | + const baseCost = Hbar.fromTinybars(costTinybars); |
| 53 | + |
| 54 | + beforeEach(() => { |
| 55 | + contractCallQuery = new ContractCallQuery() |
| 56 | + .setContractId('0.0.1010') |
| 57 | + .setPaymentTransactionId(TransactionId.generate(client.operatorAccountId)) |
| 58 | + queryStub = sinon.stub(contractCallQuery, 'execute'); |
| 59 | + }) |
| 60 | + |
| 61 | + it('executes the query', async () => { |
| 62 | + queryStub.returns(successResponse); |
| 63 | + let {resp, cost} = await sdkClient.increaseCostAndRetryExecution(contractCallQuery, baseCost, client, 3, 0); |
| 64 | + expect(resp).to.eq(successResponse); |
| 65 | + expect(cost.toTinybars().toNumber()).to.eq(costTinybars); |
| 66 | + expect(queryStub.callCount).to.eq(1); |
| 67 | + }); |
| 68 | + |
| 69 | + it('increases the cost when INSUFFICIENT_TX_FEE is thrown', async () => { |
| 70 | + queryStub.onCall(0).throws({ |
| 71 | + status: Status.InsufficientTxFee |
| 72 | + }); |
| 73 | + |
| 74 | + queryStub.onCall(1).returns(successResponse); |
| 75 | + let {resp, cost} = await sdkClient.increaseCostAndRetryExecution(contractCallQuery, baseCost, client, 3, 0); |
| 76 | + expect(resp).to.eq(successResponse); |
| 77 | + expect(cost.toTinybars().toNumber()).to.eq(costTinybars * constants.QUERY_COST_INCREMENTATION_STEP); |
| 78 | + expect(queryStub.callCount).to.eq(2); |
| 79 | + }); |
| 80 | + |
| 81 | + it('increases the cost when INSUFFICIENT_TX_FEE is thrown on every repeat', async () => { |
| 82 | + queryStub.onCall(0).throws({ |
| 83 | + status: Status.InsufficientTxFee |
| 84 | + }); |
| 85 | + |
| 86 | + queryStub.onCall(1).throws({ |
| 87 | + status: Status.InsufficientTxFee |
| 88 | + }); |
| 89 | + |
| 90 | + queryStub.onCall(2).returns(successResponse); |
| 91 | + |
| 92 | + let {resp, cost} = await sdkClient.increaseCostAndRetryExecution(contractCallQuery, baseCost, client, 3, 0); |
| 93 | + expect(resp).to.eq(successResponse); |
| 94 | + expect(cost.toTinybars().toNumber()).to.eq(Math.floor(costTinybars * Math.pow(constants.QUERY_COST_INCREMENTATION_STEP, 2))); |
| 95 | + expect(queryStub.callCount).to.eq(3); |
| 96 | + }); |
| 97 | + |
| 98 | + it('is repeated at most 4 times', async () => { |
| 99 | + try { |
| 100 | + queryStub.throws({ |
| 101 | + status: Status.InsufficientTxFee |
| 102 | + }); |
| 103 | + |
| 104 | + let {resp, cost} = await sdkClient.increaseCostAndRetryExecution(contractCallQuery, baseCost, client, 3, 0); |
| 105 | + } |
| 106 | + catch(e: any) { |
| 107 | + expect(queryStub.callCount).to.eq(4); |
| 108 | + expect(e.status).to.eq(Status.InsufficientTxFee); |
| 109 | + } |
| 110 | + }); |
| 111 | + }) |
| 112 | +}); |
0 commit comments