-
Notifications
You must be signed in to change notification settings - Fork 361
Admin api #2415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Admin api #2415
Changes from 9 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e8518a9
draft
jiqiang90 9231a51
rewind
jiqiang90 d3279ff
poi
jiqiang90 54f6ad0
add tests
jiqiang90 ab12ac3
add test for stop poi stop sync
jiqiang90 99d4afa
Update packages/node-core/src/indexer/blockDispatcher/base-block-disp…
jiqiang90 032d7f6
address comment
jiqiang90 06f5787
Merge remote-tracking branch 'origin/admin-api' into admin-api
jiqiang90 8874427
monitor service add exit
jiqiang90 a4984e9
Update packages/node-core/src/process.ts
jiqiang90 8a6b72e
address comments
jiqiang90 6da3831
Merge remote-tracking branch 'origin/admin-api' into admin-api
jiqiang90 eaef4df
change write
jiqiang90 3bab87c
update
jiqiang90 91aa8ca
improve exit error
jiqiang90 1e6ecde
fix rewind height issue
jiqiang90 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,259 @@ | ||
// Copyright 2020-2024 SubQuery Pte Ltd authors & contributors | ||
// SPDX-License-Identifier: GPL-3.0 | ||
|
||
import {HttpException} from '@nestjs/common'; | ||
import {EventEmitter2} from '@nestjs/event-emitter'; | ||
import {Test, TestingModule} from '@nestjs/testing'; | ||
import {TargetBlockPayload, RewindPayload, AdminEvent} from '../events'; | ||
import {MonitorService, PoiService, ProofOfIndex} from '../indexer'; | ||
import {AdminController, AdminListener} from './admin.controller'; | ||
import {BlockRangeDto} from './blockRange'; | ||
|
||
describe('AdminController', () => { | ||
let adminController: AdminController; | ||
let monitorService: MonitorService; | ||
let poiService: PoiService; | ||
let eventEmitter: EventEmitter2; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [AdminController], | ||
providers: [ | ||
{ | ||
provide: MonitorService, | ||
useValue: { | ||
getBlockIndexHistory: jest.fn(), | ||
getForkedRecords: jest.fn(), | ||
getBlockIndexRecords: jest.fn(), | ||
}, | ||
}, | ||
{ | ||
provide: PoiService, | ||
useValue: { | ||
plainPoiRepo: { | ||
getStartAndEndBlock: jest.fn(), | ||
getPoiBlocksByRange: jest.fn(), | ||
}, | ||
PoiToHuman: jest.fn(), | ||
}, | ||
}, | ||
{ | ||
provide: EventEmitter2, | ||
useValue: { | ||
emitAsync: jest.fn(), | ||
once: jest.fn(), | ||
emit: jest.fn(), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
adminController = module.get<AdminController>(AdminController); | ||
monitorService = module.get<MonitorService>(MonitorService); | ||
poiService = module.get<PoiService>(PoiService); | ||
eventEmitter = module.get<EventEmitter2>(EventEmitter2); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(adminController).toBeDefined(); | ||
}); | ||
|
||
describe('getIndexBlocks', () => { | ||
it('should return block index history', () => { | ||
const result = ['block1', 'block2']; | ||
jest.spyOn(monitorService, 'getBlockIndexHistory').mockImplementation(() => result); | ||
|
||
expect(adminController.getIndexBlocks()).toEqual(result); | ||
}); | ||
|
||
it('should handle errors', () => { | ||
jest.spyOn(monitorService, 'getBlockIndexHistory').mockImplementation(() => { | ||
throw new Error('Test error'); | ||
}); | ||
|
||
expect(() => adminController.getIndexBlocks()).toThrow(HttpException); | ||
}); | ||
}); | ||
|
||
describe('getIndexForks', () => { | ||
it('should return forked records', async () => { | ||
const result = ['fork1', 'fork2']; | ||
jest.spyOn(monitorService, 'getForkedRecords').mockImplementation(() => Promise.resolve(result)); | ||
|
||
await expect(adminController.getIndexForks()).resolves.toEqual(result); | ||
}); | ||
|
||
it('should handle errors', async () => { | ||
jest.spyOn(monitorService, 'getForkedRecords').mockImplementation(() => { | ||
throw new Error('Test error'); | ||
}); | ||
|
||
await expect(adminController.getIndexForks()).rejects.toThrow(HttpException); | ||
}); | ||
}); | ||
|
||
describe('getIndexBlockRecord', () => { | ||
it('should return block index records', async () => { | ||
const result = ['record1', 'record2']; | ||
jest.spyOn(monitorService, 'getBlockIndexRecords').mockImplementation(() => Promise.resolve(result)); | ||
|
||
await expect(adminController.getIndexBlockRecord('1')).resolves.toEqual(result); | ||
}); | ||
|
||
it('should handle errors', async () => { | ||
jest.spyOn(monitorService, 'getBlockIndexRecords').mockImplementation(() => { | ||
throw new Error('Test error'); | ||
}); | ||
|
||
await expect(adminController.getIndexBlockRecord('1')).rejects.toThrow(HttpException); | ||
}); | ||
}); | ||
|
||
describe('getPoiRange', () => { | ||
it('should return POI range', async () => { | ||
const result = {startBlock: 1, endBlock: 10}; | ||
jest.spyOn(poiService.plainPoiRepo, 'getStartAndEndBlock').mockImplementation(() => Promise.resolve(result)); | ||
|
||
await expect(adminController.getPoiRange()).resolves.toEqual(result); | ||
}); | ||
|
||
it('should handle errors', async () => { | ||
jest.spyOn(poiService.plainPoiRepo, 'getStartAndEndBlock').mockImplementation(() => { | ||
throw new Error('Test error'); | ||
}); | ||
|
||
await expect(adminController.getPoiRange()).rejects.toThrow(HttpException); | ||
}); | ||
}); | ||
|
||
describe('getPoisByRange', () => { | ||
it('should return POIs by range', async () => { | ||
const pois: ProofOfIndex[] = [ | ||
{ | ||
id: 1, | ||
chainBlockHash: new Uint8Array(), | ||
hash: new Uint8Array(), | ||
parentHash: new Uint8Array(), | ||
operationHashRoot: new Uint8Array(), | ||
}, | ||
]; | ||
const blockRange = new BlockRangeDto(1, 10); | ||
jest.spyOn(poiService.plainPoiRepo, 'getPoiBlocksByRange').mockImplementation(() => Promise.resolve(pois)); | ||
await expect(adminController.getPoisByRange(blockRange)).resolves.toEqual([ | ||
{ | ||
chainBlockHash: '0x', | ||
hash: '0x', | ||
id: 1, | ||
operationHashRoot: '0x', | ||
parentHash: '0x', | ||
}, | ||
]); | ||
}); | ||
|
||
it('should handle errors', async () => { | ||
const blockRange = new BlockRangeDto(1, 10); | ||
jest.spyOn(poiService.plainPoiRepo, 'getPoiBlocksByRange').mockImplementation(() => { | ||
throw new Error('Test error'); | ||
}); | ||
|
||
await expect(() => adminController.getPoisByRange(blockRange)).rejects.toThrow(HttpException); | ||
}); | ||
|
||
it('should throw an error if startBlock is greater than endBlock', async () => { | ||
const blockRange = new BlockRangeDto(10, 1); | ||
|
||
await expect(adminController.getPoisByRange(blockRange)).rejects.toThrow(HttpException); | ||
}); | ||
}); | ||
|
||
describe('rewindTarget', () => { | ||
it('should return successful rewind payload', async () => { | ||
const rewindData = {height: 1} as TargetBlockPayload; | ||
const result = {success: true, height: 1} as RewindPayload; | ||
jest.spyOn(eventEmitter, 'emitAsync').mockImplementation(() => Promise.resolve([])); | ||
jest.spyOn(eventEmitter, 'once').mockImplementation((event, callback) => { | ||
callback(result); | ||
return eventEmitter; // Ensure that it returns the eventEmitter instance | ||
}); | ||
await expect(adminController.rewindTarget(rewindData)).resolves.toEqual(result); | ||
}); | ||
|
||
it('should return failure if rewind times out', async () => { | ||
const rewindData = {height: 1} as TargetBlockPayload; | ||
|
||
jest.spyOn(eventEmitter, 'emitAsync').mockImplementation(() => Promise.resolve([])); | ||
jest.spyOn(eventEmitter, 'once').mockImplementation(() => { | ||
throw new Error('timeout'); | ||
}); | ||
|
||
await expect(adminController.rewindTarget(rewindData)).resolves.toEqual({ | ||
success: false, | ||
height: rewindData.height, | ||
message: expect.stringContaining('Rewind failed:'), | ||
}); | ||
}); | ||
|
||
it('should handle errors', async () => { | ||
const rewindData = {height: 1} as TargetBlockPayload; | ||
|
||
jest.spyOn(eventEmitter, 'emitAsync').mockImplementation(() => { | ||
throw new Error('Test error'); | ||
}); | ||
|
||
await expect(adminController.rewindTarget(rewindData)).resolves.toEqual({ | ||
success: false, | ||
height: rewindData.height, | ||
message: expect.stringContaining('Rewind failed:'), | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('AdminListener', () => { | ||
let adminListener: AdminListener; | ||
let eventEmitter: EventEmitter2; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
AdminListener, | ||
{ | ||
provide: EventEmitter2, | ||
useValue: { | ||
emit: jest.fn(), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
adminListener = module.get<AdminListener>(AdminListener); | ||
eventEmitter = module.get<EventEmitter2>(EventEmitter2); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(adminListener).toBeDefined(); | ||
}); | ||
|
||
describe('handleRewindSuccess', () => { | ||
it('should emit RewindTargetResponse event on rewind success', () => { | ||
const payload = {height: 1, success: true} as RewindPayload; | ||
|
||
adminListener.handleRewindSuccess(payload); | ||
|
||
expect(eventEmitter.emit).toHaveBeenCalledWith(AdminEvent.RewindTargetResponse, { | ||
...payload, | ||
message: `Rewind to block ${payload.height} successful`, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('handleRewindFailure', () => { | ||
it('should emit RewindTargetResponse event on rewind failure', () => { | ||
const payload = {height: 1, success: false} as RewindPayload; | ||
|
||
adminListener.handleRewindFailure(payload); | ||
|
||
expect(eventEmitter.emit).toHaveBeenCalledWith(AdminEvent.RewindTargetResponse, payload); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.