diff --git a/src/lib/AddressGenerator.js b/src/lib/AddressGenerator.js index e1ca699..603ca0e 100644 --- a/src/lib/AddressGenerator.js +++ b/src/lib/AddressGenerator.js @@ -1,23 +1,28 @@ 'use strict' +const lodash = require('lodash') +let Logger = require('./Logger.js') // eslint-disable-line + const AddressGenerator = {} AddressGenerator.generate = (options, callback) => { - if (!options.accountName || !options.client || !options.maxAddresses) { - console.log('STATUS: AddressGenerator.generate invalid params') - callback(false) + const required = ['accountName', 'client', 'maxAddresses'] + if (lodash.intersection(Object.keys(options), required).length !== required.length) { + Logger.writeLog('ADG_001', 'invalid options', { options, required }) + callback(false, { message: 'invalid options provided to AddressGenerator.generate' }) return } + options.client.getAccountAddress(options.accountName).then(() => { console.log('STATUS: "' + options.accountName + '" account created') AddressGenerator.getAccountAddressesForGeneration(options, callback) }) .catch((err) => { if (err.code === -12) { - AddressGenerator.runKeypoolRefill() + AddressGenerator.runKeypoolRefill(options, callback) } else { - console.log('ERROR: client.getAccountAddress failed', err) - callback(false) + Logger.writeLog('ADG_002', 'client.getAccountAddress failed', { err }) + callback(false, err) return } }) @@ -27,9 +32,9 @@ AddressGenerator.runKeypoolRefill = (options, callback) => { options.client.keypoolRefill().then(() => { console.log('STATUS: keypool filled') AddressGenerator.generate(options, callback) - }).catch((err2) => { - console.log('ERROR: client.keypoolRefill failed', err2) - callback(false) + }).catch((err) => { + Logger.writeLog('ADG_003', 'client.keypoolRefill failed', { err }) + callback(false, err) return }) } @@ -55,8 +60,8 @@ AddressGenerator.getAccountAddressesForGeneration = (options, callback) => { if (err.code === -12) { AddressGenerator.runKeypoolRefill(options, callback) } else { - console.log('ERROR: client.getAddressesByAccount', err) - callback(false) + Logger.writeLog('ADG_004', 'client.getAddressesByAccount', { err }) + callback(false, err) return } }) @@ -79,8 +84,8 @@ AddressGenerator.generateNewAccountAddresses = (options, callback) => { if (err.code === -12) { AddressGenerator.runKeypoolRefill(options, callback) } else { - console.log('ERROR: client.getNewAddress', err) - callback(false) + Logger.writeLog('ADG_005', 'client.getNewAddress', { err }) + callback(false, err) return } }) diff --git a/src/settings/private.settings.json b/src/settings/private.settings.json index 11bf6e0..544431f 100644 --- a/src/settings/private.settings.json +++ b/src/settings/private.settings.json @@ -35,6 +35,6 @@ "minConfs": 1, "blockThreshold": { "checking": 5, - "processing": 1 + "processing": 3 } } diff --git a/test/AddressGenerator.spec.js b/test/AddressGenerator.spec.js new file mode 100644 index 0000000..6b196bb --- /dev/null +++ b/test/AddressGenerator.spec.js @@ -0,0 +1,287 @@ +'use strict' + +const expect = require('expect') +const rewire = require('rewire') +const sinon = require('sinon') + +let AddressGenerator = rewire('../src/lib/AddressGenerator') + +describe('[AddressGenerator]', () => { + describe('(generate)', () => { + it('should fail on params', (done) => { + const callback = (success, data) => { + expect(success).toBe(false) + expect(data.message).toBeA('string') + sinon.assert.calledOnce(mockLogger.writeLog) + done() + } + const mockLogger = { + writeLog: sinon.spy(), + } + AddressGenerator.__set__('Logger', mockLogger) + AddressGenerator.generate({ + junkParam: 'sdfsdfsd', + }, callback) + }) + it('should fail client.getAccountAddress with error -12', (done) => { + const mockClient = { + getAccountAddress: () => { return Promise.reject({ code: -12 }) }, + } + + AddressGenerator.runKeypoolRefill = () => { + expect(true).toBe(true) + done() + } + + const callback = () => {} + + AddressGenerator.generate({ + accountName: 'incomingAccount', + client: mockClient, + maxAddresses: 100, + }, callback) + }) + it('should fail client.getAccountAddress catch all error', (done) => { + const mockClient = { + getAccountAddress: () => { return Promise.reject({ code: -17 }) }, + } + + const callback = (success, data) => { + expect(success).toBe(false) + expect(data.code).toBe(-17) + sinon.assert.calledOnce(mockLogger.writeLog) + done() + } + const mockLogger = { + writeLog: sinon.spy(), + } + AddressGenerator.__set__('Logger', mockLogger) + AddressGenerator.generate({ + accountName: 'incomingAccount', + client: mockClient, + maxAddresses: 100, + }, callback) + }) + it('should get the account address and run getAccountAddressesForGeneration', (done) => { + const mockClient = { + getAccountAddress: () => { return Promise.resolve() }, + } + + const callback = () => {} + + AddressGenerator.getAccountAddressesForGeneration = (options, parsedCallback) => { + expect(parsedCallback).toBe(callback) + expect(options).toBeA('object') + done() + } + + AddressGenerator.generate({ + accountName: 'incomingAccount', + client: mockClient, + maxAddresses: 100, + }, callback) + }) + }) + describe('(runKeypoolRefill)', () => { + before(() => { // reset the rewired functions + AddressGenerator = rewire('../src/lib/AddressGenerator') + }) + it('should fail to refill the keypool', (done) => { + const callback = (success, data) => { + expect(success).toBe(false) + expect(data.code).toBe(-99) + sinon.assert.calledOnce(mockLogger.writeLog) + done() + } + + const mockClient = { + keypoolRefill: () => { return Promise.reject({ code: -99 }) }, + } + + const mockLogger = { + writeLog: sinon.spy(), + } + AddressGenerator.__set__('Logger', mockLogger) + AddressGenerator.runKeypoolRefill({ + client: mockClient, + }, callback) + }) + it('should refill the keypool and run generate again', (done) => { + const callback = () => {} + + AddressGenerator.generate = (options, parsedCallback) => { + expect(parsedCallback).toBe(callback) + expect(options).toBeA('object') + done() + } + + const mockClient = { + keypoolRefill: () => { return Promise.resolve() }, + } + + const mockLogger = { + writeLog: sinon.spy(), + } + AddressGenerator.__set__('Logger', mockLogger) + AddressGenerator.runKeypoolRefill({ + client: mockClient, + }, callback) + }) + }) + describe('(getAccountAddressesForGeneration)', () => { + before(() => { // reset the rewired functions + AddressGenerator = rewire('../src/lib/AddressGenerator') + }) + it('fail client.getAddressesByAccount with error 12', (done) => { + const mockClient = { + getAddressesByAccount: () => { return Promise.reject({ code: -12 }) }, + } + + AddressGenerator.runKeypoolRefill = () => { + expect(true).toBe(true) + done() + } + + const callback = () => {} + + AddressGenerator.getAccountAddressesForGeneration({ + accountName: 'incomingAccount', + client: mockClient, + maxAddresses: 100, + }, callback) + }) + it('fail client.getAddressesByAccount catch all error', (done) => { + const mockClient = { + getAddressesByAccount: () => { return Promise.reject({ code: -17 }) }, + } + + const callback = (success, err) => { + expect(success).toBe(false) + expect(err.code).toBe(-17) + sinon.assert.calledOnce(mockLogger.writeLog) + done() + } + + const mockLogger = { + writeLog: sinon.spy(), + } + AddressGenerator.__set__('Logger', mockLogger) + + AddressGenerator.getAccountAddressesForGeneration({ + accountName: 'incomingAccount', + client: mockClient, + maxAddresses: 100, + }, callback) + }) + it('should return true because the max addresses already generated', (done) => { + const addresses = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] + + const mockClient = { + getAddressesByAccount: () => { return Promise.resolve(addresses) }, + } + + const callback = (success) => { + expect(success).toBe(true) + done() + } + + AddressGenerator.getAccountAddressesForGeneration({ + accountName: 'incomingAccount', + client: mockClient, + maxAddresses: 10, + }, callback) + }) + it('should call the generate function because more addresses are required ', (done) => { + const addresses = [1, 2, 3, 4, 5] + + const mockClient = { + getAddressesByAccount: () => { return Promise.resolve(addresses) }, + } + + const callback = () => {} + + AddressGenerator.generateNewAccountAddresses = (options, parsedCallback) => { + expect(parsedCallback).toBe(callback) + expect(options.numToGenerate).toBe(5) + expect(options.client).toBe(mockClient) + expect(options.maxAddresses).toBe(10) + expect(options.accountName).toBe('incomingAccount') + done() + } + + AddressGenerator.getAccountAddressesForGeneration({ + accountName: 'incomingAccount', + client: mockClient, + maxAddresses: 10, + }, callback) + }) + }) + describe('(generateNewAccountAddresses)', () => { + before(() => { // reset the rewired functions + AddressGenerator = rewire('../src/lib/AddressGenerator') + }) + it('should fail client.getNewAddress with error 12', (done) => { + const mockClient = { + getNewAddress: () => { return Promise.reject({ code: -12 }) }, + } + + AddressGenerator.runKeypoolRefill = () => { + expect(true).toBe(true) + done() + } + + const callback = () => {} + + AddressGenerator.generateNewAccountAddresses({ + accountName: 'incomingAccount', + client: mockClient, + maxAddresses: 100, + numToGenerate: 100, + }, callback) + }) + it('should fail client.getNewAddress catch all error', (done) => { + const mockClient = { + getNewAddress: () => { return Promise.reject({ code: -17 }) }, + } + + AddressGenerator.runKeypoolRefill = () => { + expect(true).toBe(true) + done() + } + + const callback = (success, err) => { + expect(success).toBe(false) + expect(err.code).toBe(-17) + sinon.assert.calledOnce(mockLogger.writeLog) + done() + } + const mockLogger = { + writeLog: sinon.spy(), + } + AddressGenerator.__set__('Logger', mockLogger) + AddressGenerator.generateNewAccountAddresses({ + accountName: 'incomingAccount', + client: mockClient, + maxAddresses: 100, + numToGenerate: 100, + }, callback) + }) + it('should generate the address and call recurse until numToGenerate is 0', (done) => { + const mockClient = { + getNewAddress: () => { return Promise.resolve('123412341234') }, + } + + const callback = (success) => { + expect(success).toBe(true) + done() + } + + AddressGenerator.generateNewAccountAddresses({ + accountName: 'incomingAccount', + client: mockClient, + maxAddresses: 10, + numToGenerate: 10, + }, callback) + }) + }) +})