Skip to content

Commit

Permalink
fix: fetchMinRewardBlocks fails when reward_min_blocks is 0 (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreabadesso committed Jun 25, 2024
1 parent 6ec32e9 commit 4a8c1a1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
42 changes: 41 additions & 1 deletion packages/daemon/__tests__/services/services.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ afterEach(() => {
});

describe('fetchInitialState', () => {
beforeAll(() => {
beforeEach(() => {
const mockUrl = 'http://mock-host:8080/v1a/';
(getFullnodeHttpUrl as jest.Mock).mockReturnValue(mockUrl);

Expand Down Expand Up @@ -166,6 +166,46 @@ describe('fetchInitialState', () => {
expect(mockDb.destroy).toHaveBeenCalled();
});

it('should not fail if reward spend min blocks is 0', async () => {
// Mock the return values of the dependencies
const mockDb = { destroy: jest.fn() };

// @ts-ignore
axios.get.mockResolvedValue({
status: 200,
data: {
version: '0.58.0-rc.1',
network: 'mainnet',
min_weight: 14,
min_tx_weight: 14,
min_tx_weight_coefficient: 1.6,
min_tx_weight_k: 100,
token_deposit_percentage: 0.01,
reward_spend_min_blocks: 0,
max_number_inputs: 255,
max_number_outputs: 255
}
});

// @ts-ignore
getDbConnection.mockReturnValue(mockDb);
// @ts-ignore
getLastSyncedEvent.mockResolvedValue({
id: 0,
last_event_id: 123,
updated_at: Date.now(),
});

const result = await fetchInitialState();

expect(result).toEqual({
lastEventId: expect.any(Number),
rewardMinBlocks: 0,
});

expect(mockDb.destroy).toHaveBeenCalled();
});

it('should return undefined if no last event is found', async () => {
const mockDb = { destroy: jest.fn() };
// @ts-ignore
Expand Down
2 changes: 1 addition & 1 deletion packages/daemon/src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ export const fetchMinRewardBlocks = async () => {

const rewardSpendMinBlocks = get(response, 'data.reward_spend_min_blocks');

if (!rewardSpendMinBlocks) {
if (rewardSpendMinBlocks == null) {
throw new Error('Failed to fetch reward spend min blocks');
}

Expand Down

0 comments on commit 4a8c1a1

Please sign in to comment.