Skip to content

Commit

Permalink
Merge pull request #6 from navcoindev/#5-address-generator
Browse files Browse the repository at this point in the history
#5 address generator
  • Loading branch information
proletesseract authored Mar 21, 2017
2 parents 4ba9742 + 551080f commit ebaa59b
Show file tree
Hide file tree
Showing 3 changed files with 306 additions and 14 deletions.
31 changes: 18 additions & 13 deletions src/lib/AddressGenerator.js
Original file line number Diff line number Diff line change
@@ -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
}
})
Expand All @@ -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
})
}
Expand All @@ -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
}
})
Expand All @@ -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
}
})
Expand Down
2 changes: 1 addition & 1 deletion src/settings/private.settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@
"minConfs": 1,
"blockThreshold": {
"checking": 5,
"processing": 1
"processing": 3
}
}
287 changes: 287 additions & 0 deletions test/AddressGenerator.spec.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
})

0 comments on commit ebaa59b

Please sign in to comment.