Skip to content

Commit 1bc2c18

Browse files
committed
Fix tests
1 parent a00eb27 commit 1bc2c18

File tree

4 files changed

+66
-61
lines changed

4 files changed

+66
-61
lines changed

backend/jest.config.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ const config: Config.InitialOptions = {
77
automock: false,
88
collectCoverage: true,
99
collectCoverageFrom: ["./src/**/**.ts"],
10-
coverageProvider: "v8",
10+
coverageProvider: "babel",
1111
coverageThreshold: {
1212
global: {
1313
lines: 1
1414
}
15-
}
15+
},
16+
setupFiles: [
17+
"./testSetup.ts",
18+
],
1619
}
1720
export default config;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { calcDifficultyAdjustment, DifficultyAdjustment } from '../../api/difficulty-adjustment';
2+
3+
test('should calculate Difficulty Adjustments properly', () => {
4+
const dt = (dtString) => {
5+
return Math.floor(new Date(dtString).getTime() / 1000);
6+
};
7+
8+
const vectors = [
9+
[ // Vector 1
10+
[ // Inputs
11+
dt('2022-08-18T11:07:00.000Z'), // Last DA time (in seconds)
12+
dt('2022-08-19T14:03:53.000Z'), // Current time (now) (in seconds)
13+
750134, // Current block height
14+
0.6280047707459726, // Previous retarget % (Passed through)
15+
'mainnet', // Network (if testnet, next value is non-zero)
16+
0, // If not testnet, not used
17+
],
18+
{ // Expected Result
19+
progressPercent: 9.027777777777777,
20+
difficultyChange: 12.562233927411782,
21+
estimatedRetargetDate: 1661895424692,
22+
remainingBlocks: 1834,
23+
remainingTime: 977591692,
24+
previousRetarget: 0.6280047707459726,
25+
nextRetargetHeight: 751968,
26+
timeAvg: 533038,
27+
timeOffset: 0,
28+
},
29+
],
30+
[ // Vector 2 (testnet)
31+
[ // Inputs
32+
dt('2022-08-18T11:07:00.000Z'), // Last DA time (in seconds)
33+
dt('2022-08-19T14:03:53.000Z'), // Current time (now) (in seconds)
34+
750134, // Current block height
35+
0.6280047707459726, // Previous retarget % (Passed through)
36+
'testnet', // Network
37+
dt('2022-08-19T13:52:46.000Z'), // Latest block timestamp in seconds
38+
],
39+
{ // Expected Result is same other than timeOffset
40+
progressPercent: 9.027777777777777,
41+
difficultyChange: 12.562233927411782,
42+
estimatedRetargetDate: 1661895424692,
43+
remainingBlocks: 1834,
44+
remainingTime: 977591692,
45+
previousRetarget: 0.6280047707459726,
46+
nextRetargetHeight: 751968,
47+
timeAvg: 533038,
48+
timeOffset: -667000, // 11 min 7 seconds since last block (testnet only)
49+
// If we add time avg to abs(timeOffset) it makes exactly 1200000 ms, or 20 minutes
50+
},
51+
],
52+
] as [[number, number, number, number, string, number], DifficultyAdjustment][];
53+
54+
for (const vector of vectors) {
55+
const result = calcDifficultyAdjustment(...vector[0]);
56+
// previousRetarget is passed through untouched
57+
expect(result.previousRetarget).toStrictEqual(vector[0][3]);
58+
expect(result).toStrictEqual(vector[1]);
59+
}
60+
});

backend/src/__tests__/config.test.ts

-59
Original file line numberDiff line numberDiff line change
@@ -136,63 +136,4 @@ describe('Mempool Backend Config', () => {
136136
expect(config.EXTERNAL_DATA_SERVER).toStrictEqual(fixture.EXTERNAL_DATA_SERVER);
137137
});
138138
});
139-
140-
test('should calculate Difficulty Adjustments properly', () => {
141-
jest.isolateModules(() => {
142-
const { calcDifficultyAdjustment } = jest.requireActual('../api/difficulty-adjustment');
143-
const dt = dtString => Math.floor(new Date(dtString).getTime() / 1000);
144-
const vectors = [
145-
[ // Vector 1
146-
[ // Inputs
147-
dt('2022-08-18T11:07:00.000Z'), // Last DA time (in seconds)
148-
dt('2022-08-19T14:03:53.000Z'), // Current time (now) (in seconds)
149-
750134, // Current block height
150-
0.6280047707459726, // Previous retarget % (Passed through)
151-
'mainnet', // Network (if testnet, next value is non-zero)
152-
0, // If not testnet, not used
153-
],
154-
{ // Expected Result
155-
progressPercent: 9.027777777777777,
156-
difficultyChange: 12.562233927411782,
157-
estimatedRetargetDate: 1661895424692,
158-
remainingBlocks: 1834,
159-
remainingTime: 977591692,
160-
previousRetarget: 0.6280047707459726,
161-
nextRetargetHeight: 751968,
162-
timeAvg: 533038,
163-
timeOffset: 0,
164-
},
165-
],
166-
[ // Vector 2 (testnet)
167-
[ // Inputs
168-
dt('2022-08-18T11:07:00.000Z'), // Last DA time (in seconds)
169-
dt('2022-08-19T14:03:53.000Z'), // Current time (now) (in seconds)
170-
750134, // Current block height
171-
0.6280047707459726, // Previous retarget % (Passed through)
172-
'testnet', // Network
173-
dt('2022-08-19T13:52:46.000Z'), // Latest block timestamp in seconds
174-
],
175-
{ // Expected Result is same other than timeOffset
176-
progressPercent: 9.027777777777777,
177-
difficultyChange: 12.562233927411782,
178-
estimatedRetargetDate: 1661895424692,
179-
remainingBlocks: 1834,
180-
remainingTime: 977591692,
181-
previousRetarget: 0.6280047707459726,
182-
nextRetargetHeight: 751968,
183-
timeAvg: 533038,
184-
timeOffset: -667000, // 11 min 7 seconds since last block (testnet only)
185-
// If we add time avg to abs(timeOffset) it makes exactly 1200000 ms, or 20 minutes
186-
},
187-
],
188-
] as [[number, number, number, number, string, number], any][];
189-
190-
for (const vector of vectors) {
191-
const result = calcDifficultyAdjustment(...vector[0]);
192-
// previousRetarget is passed through untouched
193-
expect(result.previousRetarget).toStrictEqual(vector[0][3]);
194-
expect(result).toStrictEqual(vector[1]);
195-
}
196-
});
197-
});
198139
});

backend/testSetup.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
jest.mock('./mempool-config.json', () => ({}), { virtual: true });

0 commit comments

Comments
 (0)