mirror of
https://github.com/Permissionless-Software-Foundation/psf-bch-api-base.git
synced 2026-09-21 16:52:00 -07:00
feat(mining): Ported mining endpoints from bch-api
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
Unit tests for MiningRESTController.
|
||||
*/
|
||||
|
||||
import { assert } from 'chai'
|
||||
import sinon from 'sinon'
|
||||
|
||||
import MiningRESTController from '../../../src/controllers/rest-api/full-node/mining/controller.js'
|
||||
import { createMockRequest, createMockResponse } from '../mocks/controller-mocks.js'
|
||||
|
||||
describe('#mining-controller.js', () => {
|
||||
let sandbox
|
||||
let mockAdapters
|
||||
let mockUseCases
|
||||
let uut
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.createSandbox()
|
||||
mockAdapters = {}
|
||||
mockUseCases = {
|
||||
mining: {
|
||||
getMiningInfo: sandbox.stub().resolves({ blocks: 100, difficulty: 1.5 }),
|
||||
getNetworkHashPS: sandbox.stub().resolves(1234567890)
|
||||
}
|
||||
}
|
||||
|
||||
uut = new MiningRESTController({
|
||||
adapters: mockAdapters,
|
||||
useCases: mockUseCases
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
sandbox.restore()
|
||||
})
|
||||
|
||||
describe('#constructor()', () => {
|
||||
it('should require adapters', () => {
|
||||
assert.throws(() => {
|
||||
// eslint-disable-next-line no-new
|
||||
new MiningRESTController({ useCases: mockUseCases })
|
||||
}, /Adapters library required/)
|
||||
})
|
||||
|
||||
it('should require mining use cases', () => {
|
||||
assert.throws(() => {
|
||||
// eslint-disable-next-line no-new
|
||||
new MiningRESTController({ adapters: mockAdapters, useCases: {} })
|
||||
}, /Mining use cases required/)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#root()', () => {
|
||||
it('should return mining status', async () => {
|
||||
const req = createMockRequest()
|
||||
const res = createMockResponse()
|
||||
|
||||
await uut.root(req, res)
|
||||
|
||||
assert.equal(res.statusValue, 200)
|
||||
assert.deepEqual(res.jsonData, { status: 'mining' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('#getMiningInfo()', () => {
|
||||
it('should return mining info on success', async () => {
|
||||
const req = createMockRequest()
|
||||
const res = createMockResponse()
|
||||
|
||||
await uut.getMiningInfo(req, res)
|
||||
|
||||
assert.equal(res.statusValue, 200)
|
||||
assert.deepEqual(res.jsonData, { blocks: 100, difficulty: 1.5 })
|
||||
assert.isTrue(mockUseCases.mining.getMiningInfo.calledOnce)
|
||||
})
|
||||
|
||||
it('should handle errors via handleError', async () => {
|
||||
const error = new Error('failure')
|
||||
error.status = 503
|
||||
mockUseCases.mining.getMiningInfo.rejects(error)
|
||||
const req = createMockRequest()
|
||||
const res = createMockResponse()
|
||||
|
||||
await uut.getMiningInfo(req, res)
|
||||
|
||||
assert.equal(res.statusValue, 503)
|
||||
assert.deepEqual(res.jsonData, { error: 'failure' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('#getNetworkHashPS()', () => {
|
||||
it('should return network hash PS with default params', async () => {
|
||||
const req = createMockRequest()
|
||||
const res = createMockResponse()
|
||||
|
||||
await uut.getNetworkHashPS(req, res)
|
||||
|
||||
assert.equal(res.statusValue, 200)
|
||||
assert.equal(res.jsonData, 1234567890)
|
||||
assert.isTrue(mockUseCases.mining.getNetworkHashPS.calledOnce)
|
||||
assert.deepEqual(mockUseCases.mining.getNetworkHashPS.firstCall.args[0], {
|
||||
nblocks: 120,
|
||||
height: -1
|
||||
})
|
||||
})
|
||||
|
||||
it('should parse query params for nblocks and height', async () => {
|
||||
const req = createMockRequest({
|
||||
query: {
|
||||
nblocks: '240',
|
||||
height: '1000'
|
||||
}
|
||||
})
|
||||
const res = createMockResponse()
|
||||
|
||||
await uut.getNetworkHashPS(req, res)
|
||||
|
||||
assert.equal(res.statusValue, 200)
|
||||
assert.isTrue(mockUseCases.mining.getNetworkHashPS.calledOnce)
|
||||
assert.deepEqual(mockUseCases.mining.getNetworkHashPS.firstCall.args[0], {
|
||||
nblocks: 240,
|
||||
height: 1000
|
||||
})
|
||||
})
|
||||
|
||||
it('should handle errors via handleError', async () => {
|
||||
const error = new Error('RPC error')
|
||||
error.status = 500
|
||||
mockUseCases.mining.getNetworkHashPS.rejects(error)
|
||||
const req = createMockRequest()
|
||||
const res = createMockResponse()
|
||||
|
||||
await uut.getNetworkHashPS(req, res)
|
||||
|
||||
assert.equal(res.statusValue, 500)
|
||||
assert.deepEqual(res.jsonData, { error: 'RPC error' })
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -9,6 +9,7 @@ import RESTControllers from '../../../src/controllers/rest-api/index.js'
|
||||
import BlockchainRouter from '../../../src/controllers/rest-api/full-node/blockchain/index.js'
|
||||
import ControlRouter from '../../../src/controllers/rest-api/full-node/control/index.js'
|
||||
import DSProofRouter from '../../../src/controllers/rest-api/full-node/dsproof/index.js'
|
||||
import MiningRouter from '../../../src/controllers/rest-api/full-node/mining/index.js'
|
||||
|
||||
describe('#controllers/rest-api/index.js', () => {
|
||||
let sandbox
|
||||
@@ -51,6 +52,10 @@ describe('#controllers/rest-api/index.js', () => {
|
||||
},
|
||||
dsproof: {
|
||||
getDSProof: () => {}
|
||||
},
|
||||
mining: {
|
||||
getMiningInfo: () => {},
|
||||
getNetworkHashPS: () => {}
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -80,6 +85,7 @@ describe('#controllers/rest-api/index.js', () => {
|
||||
const blockchainAttachStub = sandbox.stub(BlockchainRouter.prototype, 'attach')
|
||||
const controlAttachStub = sandbox.stub(ControlRouter.prototype, 'attach')
|
||||
const dsproofAttachStub = sandbox.stub(DSProofRouter.prototype, 'attach')
|
||||
const miningAttachStub = sandbox.stub(MiningRouter.prototype, 'attach')
|
||||
const restControllers = new RESTControllers({
|
||||
adapters: mockAdapters,
|
||||
useCases: mockUseCases
|
||||
@@ -94,6 +100,8 @@ describe('#controllers/rest-api/index.js', () => {
|
||||
assert.equal(controlAttachStub.getCall(0).args[0], app)
|
||||
assert.isTrue(dsproofAttachStub.calledOnce)
|
||||
assert.equal(dsproofAttachStub.getCall(0).args[0], app)
|
||||
assert.isTrue(miningAttachStub.calledOnce)
|
||||
assert.equal(miningAttachStub.getCall(0).args[0], app)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user