Skip to content

Commit

Permalink
fix: occasional test failed in signStakingTx when building (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
totraev authored Sep 16, 2024
1 parent a538496 commit fad93d7
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 48 deletions.
14 changes: 8 additions & 6 deletions tests/utils/delegations/createStakingTx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { createStakingTx } from "@/utils/delegations/signStakingTx";

import { DEFAULT_TEST_FEE_RATE, testingNetworks } from "../../helper";

describe("utils/delegations/createStakingTx", () => {
initBTCCurve();
testingNetworks.forEach(({ network, dataGenerator: dataGen }) => {
[true, false].forEach((isFixed) => {
initBTCCurve();

describe.each(testingNetworks)(
"utils/delegations/createStakingTx",
({ network, dataGenerator: dataGen }) => {
describe.each([true, false])("isFixed %s", (isFixed) => {
const randomFpKeys = dataGen.generateRandomKeyPair();
const randomStakerKeys = dataGen.generateRandomKeyPair();
const feeRate = DEFAULT_TEST_FEE_RATE;
Expand Down Expand Up @@ -205,5 +207,5 @@ describe("utils/delegations/createStakingTx", () => {
});
}
});
});
});
},
);
10 changes: 6 additions & 4 deletions tests/utils/delegations/fee.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { txFeeSafetyCheck } from "@/utils/delegations/fee";

import { testingNetworks } from "../../helper";

describe("txFeeSafetyCheck", () => {
testingNetworks.map(({ networkName, dataGenerator }) => {
describe.each(testingNetworks)(
"txFeeSafetyCheck",
({ networkName, dataGenerator }) => {
const feeRate = dataGenerator.generateRandomFeeRates();
const globalParams = dataGenerator.generateGlobalPramsVersions(
dataGenerator.getRandomIntegerBetween(1, 10),
Expand All @@ -17,6 +18,7 @@ describe("txFeeSafetyCheck", () => {
randomParam.activationHeight + 1,
);
const tx = signedPsbt.extractTransaction();

describe(`on ${networkName} - `, () => {
test("should not throw an error if the estimated fee is within the acceptable range", () => {
let estimatedFee = (tx.virtualSize() * feeRate) / 2 + 1;
Expand Down Expand Up @@ -58,5 +60,5 @@ describe("txFeeSafetyCheck", () => {
}).not.toThrow();
});
});
});
});
},
);
50 changes: 17 additions & 33 deletions tests/utils/delegations/signStakingTx.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import { initBTCCurve } from "btc-staking-ts";

import { signStakingTx } from "@/utils/delegations/signStakingTx";

import { DEFAULT_TEST_FEE_RATE, testingNetworks } from "../../helper";

// Mock the signPsbtTransaction function
jest.mock("@/app/common/utils/psbt", () => ({
signPsbtTransaction: jest.fn(),
}));
jest.mock("@/utils/delegations/fee", () => ({
txFeeSafetyCheck: jest.fn().mockReturnValue(undefined),
}));

describe("utils/delegations/signStakingTx", () => {
initBTCCurve();
testingNetworks.forEach(({ network, dataGenerator: dataGen }) => {
import { signPsbtTransaction } from "@/app/common/utils/psbt";
import { signStakingTx } from "@/utils/delegations/signStakingTx";

import { DEFAULT_TEST_FEE_RATE, testingNetworks } from "../../helper";

describe.each(testingNetworks)(
"txFeeSafetyCheck",
({ network, dataGenerator: dataGen }) => {
const randomFpKeys = dataGen.generateRandomKeyPair();
const randomUserKeys = dataGen.generateRandomKeyPair();
const feeRate = DEFAULT_TEST_FEE_RATE;
const randomParam = dataGen.generateRandomGlobalParams(true);
const randomStakingAmount = dataGen.getRandomIntegerBetween(
randomParam.minStakingAmountSat,
Expand All @@ -31,31 +28,22 @@ describe("utils/delegations/signStakingTx", () => {
dataGen.getAddressAndScriptPubKey(
dataGen.generateRandomKeyPair().publicKey,
).taproot;

const randomInputUTXOs = dataGen.generateRandomUTXOs(
randomStakingAmount + Math.floor(Math.random() * 100000000),
Math.floor(Math.random() * 10) + 1,
scriptPubKey,
);
const txHex = dataGen.generateRandomTxId();

// mock the btcWallet
const btcWallet = {
signPsbt: jest.fn(),
getWalletProviderName: jest.fn(),
pushTx: jest.fn().mockResolvedValue(true),
};

it("should successfully sign a staking transaction", async () => {
// Import the mock function
const { signPsbtTransaction } = require("@/app/common/utils/psbt");
signPsbtTransaction.mockImplementationOnce(() => {
return async () => {
return {
toHex: () => txHex,
};
};
});
(signPsbtTransaction as any).mockImplementationOnce(() => async () => ({
toHex: () => txHex,
}));

const result = await signStakingTx(
btcWallet as any,
Expand All @@ -66,7 +54,7 @@ describe("utils/delegations/signStakingTx", () => {
network,
randomTaprootAddress,
randomUserKeys.noCoordPublicKey,
feeRate,
DEFAULT_TEST_FEE_RATE,
randomInputUTXOs,
);

Expand All @@ -79,12 +67,8 @@ describe("utils/delegations/signStakingTx", () => {
});

it("should throw an error when signing a staking transaction", async () => {
// Import the mock function
const { signPsbtTransaction } = require("@/app/common/utils/psbt");
signPsbtTransaction.mockImplementationOnce(() => {
return async () => {
throw new Error("signing error");
};
(signPsbtTransaction as any).mockImplementationOnce(() => async () => {
throw new Error("signing error");
});

try {
Expand All @@ -97,13 +81,13 @@ describe("utils/delegations/signStakingTx", () => {
network,
randomTaprootAddress,
randomUserKeys.noCoordPublicKey,
feeRate,
DEFAULT_TEST_FEE_RATE,
randomInputUTXOs,
);
} catch (error: any) {
expect(error).toBeDefined();
expect(error.message).toBe("signing error");
}
});
});
});
},
);
2 changes: 0 additions & 2 deletions tests/utils/delegations/signUnbondingTx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ describe("signUnbondingTx", () => {
randomStakingTxHeight,
stakerKeys,
);

const signedPsbtTx = signedPsbt.extractTransaction();

const mockedDelegationApi = [
{
stakingTxHashHex: randomTxId,
Expand Down
9 changes: 6 additions & 3 deletions tests/utils/globalParams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,11 @@ describe("globalParams", () => {
expect(result.isApprochingNextVersion).toBeFalsy();
});

it("should return correct values for params version that are not first or last", () => {
for (let index = 1; index < globalParams.length - 2; index++) {
describe("should return correct values for params version that are not first or last", () => {
const globalParams = dataGenerator.generateGlobalPramsVersions(10);
const paramIndexes = globalParams.map((_, i) => i).slice(1, -1);

it.each(paramIndexes)("param #%i", (index) => {
const param = globalParams[index];
// before approaching
const lastConfirmationDepth = globalParams[index - 1].confirmationDepth;
Expand Down Expand Up @@ -131,6 +134,6 @@ describe("globalParams", () => {
expect(result.currentVersion).toEqual(param);
expect(result.nextVersion).toEqual(globalParams[index + 1]);
expect(result.isApprochingNextVersion).toBeFalsy();
}
});
});
});

0 comments on commit fad93d7

Please sign in to comment.