Skip to content

Commit

Permalink
fix: integer overflow isuue for defaultMaxQueryPayment field
Browse files Browse the repository at this point in the history
Signed-off-by: svetoslav-nikol0v <[email protected]>
  • Loading branch information
svetoslav-nikol0v committed Mar 19, 2024
1 parent 7682060 commit 2387d4e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import LedgerId from "../LedgerId.js";
import FileId from "../file/FileId.js";
import CACHE from "../Cache.js";
import Logger from "../logger/Logger.js"; // eslint-disable-line
import { convertToNumber } from "../util.js";

/**
* @typedef {import("../channel/Channel.js").default} Channel
Expand Down Expand Up @@ -449,7 +450,9 @@ export default class Client {
* @returns {Client<ChannelT, MirrorChannelT>}
*/
setDefaultMaxQueryPayment(defaultMaxQueryPayment) {
if (defaultMaxQueryPayment.toTinybars().toInt() < 0) {
const isMaxQueryPaymentNegative =
convertToNumber(defaultMaxQueryPayment.toTinybars()) < 0;
if (isMaxQueryPaymentNegative) {
throw new Error("defaultMaxQueryPayment must be non-negative");
}
this._defaultMaxQueryPayment = defaultMaxQueryPayment;
Expand Down
18 changes: 18 additions & 0 deletions test/integration/ClientIntegrationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,24 @@ describe("ClientIntegration", function () {
expect(clientTestnet.isTransportSecurity()).to.be.an("boolean");
});

it("should return the following error message `defaultMaxQueryPayment must be non-negative` when the user tries to set a negative value to the defaultMaxQueryPayment field", async function () {
this.timeout(120000);
try {
env.client.setDefaultMaxQueryPayment(new Hbar(1).negated());
} catch (error) {
expect(error.message).to.be.equal(
"defaultMaxQueryPayment must be non-negative",
);
}
});

it("should set defaultMaxQueryPayment field", async function () {
this.timeout(120000);
const value = new Hbar(100);
env.client.setDefaultMaxQueryPayment(value);
expect(env.client.defaultMaxQueryPayment).to.be.equal(value);
});

after(async function () {
await env.close();
clientTestnet.close();
Expand Down

0 comments on commit 2387d4e

Please sign in to comment.