Fix test setup

This commit is contained in:
junderw 2022-08-20 09:46:18 +09:00
parent 1bc2c18167
commit d700b5f145
No known key found for this signature in database
GPG Key ID: A9273B5AD3E47B45
2 changed files with 59 additions and 53 deletions

View File

@ -1,60 +1,62 @@
import { calcDifficultyAdjustment, DifficultyAdjustment } from '../../api/difficulty-adjustment'; import { calcDifficultyAdjustment, DifficultyAdjustment } from '../../api/difficulty-adjustment';
test('should calculate Difficulty Adjustments properly', () => { describe('Mempool Difficulty Adjustment', () => {
const dt = (dtString) => { test('should calculate Difficulty Adjustments properly', () => {
return Math.floor(new Date(dtString).getTime() / 1000); const dt = (dtString) => {
}; return Math.floor(new Date(dtString).getTime() / 1000);
};
const vectors = [ const vectors = [
[ // Vector 1 [ // Vector 1
[ // Inputs [ // Inputs
dt('2022-08-18T11:07:00.000Z'), // Last DA time (in seconds) dt('2022-08-18T11:07:00.000Z'), // Last DA time (in seconds)
dt('2022-08-19T14:03:53.000Z'), // Current time (now) (in seconds) dt('2022-08-19T14:03:53.000Z'), // Current time (now) (in seconds)
750134, // Current block height 750134, // Current block height
0.6280047707459726, // Previous retarget % (Passed through) 0.6280047707459726, // Previous retarget % (Passed through)
'mainnet', // Network (if testnet, next value is non-zero) 'mainnet', // Network (if testnet, next value is non-zero)
0, // If not testnet, not used 0, // If not testnet, not used
],
{ // Expected Result
progressPercent: 9.027777777777777,
difficultyChange: 12.562233927411782,
estimatedRetargetDate: 1661895424692,
remainingBlocks: 1834,
remainingTime: 977591692,
previousRetarget: 0.6280047707459726,
nextRetargetHeight: 751968,
timeAvg: 533038,
timeOffset: 0,
},
], ],
{ // Expected Result [ // Vector 2 (testnet)
progressPercent: 9.027777777777777, [ // Inputs
difficultyChange: 12.562233927411782, dt('2022-08-18T11:07:00.000Z'), // Last DA time (in seconds)
estimatedRetargetDate: 1661895424692, dt('2022-08-19T14:03:53.000Z'), // Current time (now) (in seconds)
remainingBlocks: 1834, 750134, // Current block height
remainingTime: 977591692, 0.6280047707459726, // Previous retarget % (Passed through)
previousRetarget: 0.6280047707459726, 'testnet', // Network
nextRetargetHeight: 751968, dt('2022-08-19T13:52:46.000Z'), // Latest block timestamp in seconds
timeAvg: 533038, ],
timeOffset: 0, { // Expected Result is same other than timeOffset
}, progressPercent: 9.027777777777777,
], difficultyChange: 12.562233927411782,
[ // Vector 2 (testnet) estimatedRetargetDate: 1661895424692,
[ // Inputs remainingBlocks: 1834,
dt('2022-08-18T11:07:00.000Z'), // Last DA time (in seconds) remainingTime: 977591692,
dt('2022-08-19T14:03:53.000Z'), // Current time (now) (in seconds) previousRetarget: 0.6280047707459726,
750134, // Current block height nextRetargetHeight: 751968,
0.6280047707459726, // Previous retarget % (Passed through) timeAvg: 533038,
'testnet', // Network timeOffset: -667000, // 11 min 7 seconds since last block (testnet only)
dt('2022-08-19T13:52:46.000Z'), // Latest block timestamp in seconds // If we add time avg to abs(timeOffset) it makes exactly 1200000 ms, or 20 minutes
},
], ],
{ // Expected Result is same other than timeOffset ] as [[number, number, number, number, string, number], DifficultyAdjustment][];
progressPercent: 9.027777777777777,
difficultyChange: 12.562233927411782,
estimatedRetargetDate: 1661895424692,
remainingBlocks: 1834,
remainingTime: 977591692,
previousRetarget: 0.6280047707459726,
nextRetargetHeight: 751968,
timeAvg: 533038,
timeOffset: -667000, // 11 min 7 seconds since last block (testnet only)
// If we add time avg to abs(timeOffset) it makes exactly 1200000 ms, or 20 minutes
},
],
] as [[number, number, number, number, string, number], DifficultyAdjustment][];
for (const vector of vectors) { for (const vector of vectors) {
const result = calcDifficultyAdjustment(...vector[0]); const result = calcDifficultyAdjustment(...vector[0]);
// previousRetarget is passed through untouched // previousRetarget is passed through untouched
expect(result.previousRetarget).toStrictEqual(vector[0][3]); expect(result.previousRetarget).toStrictEqual(vector[0][3]);
expect(result).toStrictEqual(vector[1]); expect(result).toStrictEqual(vector[1]);
} }
});
}); });

View File

@ -1 +1,5 @@
jest.mock('./mempool-config.json', () => ({}), { virtual: true }); jest.mock('./mempool-config.json', () => ({}), { virtual: true });
jest.mock('./src/logger.ts', () => ({}), { virtual: true });
jest.mock('./src/api/rbf-cache.ts', () => ({}), { virtual: true });
jest.mock('./src/api/mempool.ts', () => ({}), { virtual: true });
jest.mock('./src/api/memory-cache.ts', () => ({}), { virtual: true });