Skip to content

Commit 9b41fa8

Browse files
committed
Modified DevFunds to “factory pattern”
Added a “factory pattern” to DevFunds to allow for async calls to be made during object construction. This allows for `web3.eth.getAccounts` to be called and the `accounts` property assigned on instantiation. This modification allows each unit test case to be run independently of the others.
1 parent 7cb9cd5 commit 9b41fa8

File tree

4 files changed

+61
-54
lines changed

4 files changed

+61
-54
lines changed

lib/cmds/blockchain/blockchain.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,10 @@ Blockchain.prototype.run = function() {
188188
};
189189

190190
Blockchain.prototype.createFundAndUnlockAccounts = function(cb) {
191-
let devFunds = new DevFunds({blockchainConfig: this.config});
192-
devFunds.createFundAndUnlockAccounts((err) => {
193-
cb(err);
191+
DevFunds.new({blockchainConfig: this.config}).then(devFunds => {
192+
devFunds.createFundAndUnlockAccounts((err) => {
193+
cb(err);
194+
});
194195
});
195196
};
196197

lib/cmds/blockchain/dev_funds.js

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ class DevFunds {
1818
}
1919
this.logger = options.logger || console;
2020
}
21+
22+
static async new(options){
23+
const df = new DevFunds(options);
24+
await df._init();
25+
return df;
26+
}
27+
28+
async _init () {
29+
const accounts = await this.web3.eth.getAccounts();
30+
this.web3.eth.defaultAccount = accounts[0];
31+
if (accounts.length > 1) {
32+
this.accounts = accounts.slice(1);
33+
}
34+
}
2135

2236
_sendTx() {
2337
if (this.networkId !== 1337) {
@@ -27,7 +41,7 @@ class DevFunds {
2741
}
2842

2943
// trigger regular txs due to a bug in geth and stuck transactions in --dev mode
30-
regularTxs(cb) {
44+
_regularTxs(cb) {
3145
const self = this;
3246
self.web3.eth.net.getId().then((networkId) => {
3347
self.networkId = networkId;
@@ -42,21 +56,11 @@ class DevFunds {
4256
});
4357
}
4458

45-
regularUnlocks() {
59+
_regularUnlocks() {
4660
const self = this;
4761
setInterval(function () { self.unlockAccounts(self.password, () => { }); }, 20000);
4862
}
4963

50-
getCurrentAccounts(cb) {
51-
this.web3.eth.getAccounts().then((accounts) => {
52-
this.web3.eth.defaultAccount = accounts[0];
53-
if (accounts.length > 1) {
54-
this.accounts = accounts.slice(1);
55-
}
56-
cb();
57-
});
58-
}
59-
6064
createAccounts(numAccounts, password, cb) {
6165
const numAccountsToCreate = numAccounts - (this.accounts.length + 1);
6266
if (numAccountsToCreate === 0) return cb();
@@ -95,18 +99,15 @@ class DevFunds {
9599
return cb();
96100
}
97101
async.waterfall([
98-
(next) => {
99-
this.getCurrentAccounts(next);
100-
},
101102
(next) => {
102103
this.createAccounts(this.numAccounts, this.password, next);
103104
},
104105
(next) => {
105106
this.unlockAccounts(this.password, next);
106107
},
107108
(next) => {
108-
this.regularTxs();
109-
this.regularUnlocks();
109+
this._regularTxs();
110+
this._regularUnlocks();
110111
this.fundAccounts(this.balance, next);
111112
}
112113
], cb);

test/devFunds.js

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*global describe, it*/
1+
/*global describe, it, before*/
22
const assert = require('assert');
33
let TestLogger = require('../lib/tests/test_logger.js');
44
const Web3 = require('web3');
@@ -53,36 +53,43 @@ describe('embark.DevFunds', function () {
5353

5454
describe('#create, fund, and unlock accounts', function () {
5555
let provider = new FakeIpcProvider();
56-
let devFunds = new DevFunds({blockchainConfig: config, provider: provider, logger: new TestLogger({})});
5756
const web3 = new Web3(provider);
57+
let devFunds;
5858

59-
it('should create correct number of accounts', function (done) {
60-
provider.injectResult(['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855']); // getAccounts - return --dev account
61-
devFunds.getCurrentAccounts(() => {
59+
before(async () => {
60+
provider.injectResult(['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855']); // getAccounts: return --dev account
61+
devFunds = await DevFunds.new({blockchainConfig: config, provider: provider, logger: new TestLogger({})});
62+
});
6263

63-
provider.injectResult('0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae'); // createAccount #1
64-
provider.injectResult('0x22f4d0a3c12e86b4b5f39b213f7e19d048276dab'); // createAccount #2
64+
it('should create correct number of accounts', function (done) {
65+
provider.injectResult('0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae'); // createAccount #1
66+
provider.injectResult('0x22f4d0a3c12e86b4b5f39b213f7e19d048276dab'); // createAccount #2
6567

66-
devFunds.createAccounts(config.account.numAccounts, 'test_password', (err) => {
67-
assert.equal(err, null);
68+
devFunds.createAccounts(config.account.numAccounts, 'test_password', (err) => {
69+
assert.equal(err, null);
6870

69-
// TODO: make FakeIpcProvider smart enough to keep track of created accounts
70-
provider.injectResult(['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', '0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae', '0x22f4d0a3c12e86b4b5f39b213f7e19d048276dab']);
71+
// TODO: make FakeIpcProvider smart enough to keep track of created accounts
72+
provider.injectResult(['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', '0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae', '0x22f4d0a3c12e86b4b5f39b213f7e19d048276dab']);
7173

72-
web3.eth.getAccounts().then((accts) => {
73-
assert.equal(accts.length, config.account.numAccounts);
74-
assert.strictEqual(accts[0], '0x47D33b27Bb249a2DBab4C0612BF9CaF4C1950855');
75-
assert.strictEqual(accts[1], '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe');
76-
assert.strictEqual(accts[2], '0x22F4d0A3C12E86b4b5F39B213f7e19D048276DAb');
77-
done();
78-
});
74+
web3.eth.getAccounts().then((accts) => {
75+
assert.equal(accts.length, config.account.numAccounts);
76+
assert.strictEqual(accts[0], '0x47D33b27Bb249a2DBab4C0612BF9CaF4C1950855'); // --dev acct
77+
assert.strictEqual(accts[1], '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe'); // created acct #1
78+
assert.strictEqual(accts[2], '0x22F4d0A3C12E86b4b5F39B213f7e19D048276DAb'); // created acct #2
79+
done();
7980
});
8081
});
8182
});
8283

8384
it('should unlock accounts', function (done) {
84-
provider.injectResult(true); // account #1 unlock result
85-
provider.injectResult(true); // account #2 unlock result
85+
if (devFunds.accounts.length === 0) {
86+
assert.equal(true, true, "no accounts to unlock");
87+
return done();
88+
}
89+
90+
devFunds.accounts.forEach(_acct => {
91+
provider.injectResult(true); // account unlock result
92+
});
8693

8794
devFunds.unlockAccounts(devFunds.password, (errUnlock) => {
8895
assert.equal(errUnlock, null);
@@ -92,13 +99,17 @@ describe('embark.DevFunds', function () {
9299

93100
it('should fund accounts', function (done) {
94101

95-
provider.injectResult('1234567890'); // account #1 balance
96-
provider.injectResult('1234567890'); // account #2 balance
97-
// provider.injectResult('0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe'); // send tx #1
98-
// provider.injectResult('0x22F4d0A3C12E86b4b5F39B213f7e19D048276DAb'); // send tx #2
102+
if (devFunds.accounts.length === 0) {
103+
assert.equal(true, true, "no accounts to fund");
104+
return done();
105+
}
106+
devFunds.accounts.forEach(_acct => {
107+
provider.injectResult('1234567890'); // account balance
108+
// provider.injectResult('0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe'); // send tx response
109+
});
99110

100111
devFunds.fundAccounts(devFunds.balance, (errFundAccounts) => {
101-
112+
102113
assert.equal(errFundAccounts, null);
103114

104115
// inject response for web3.eth.getAccounts

test/helpers/fakeIpcProvider.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
var assert = require('assert');
2-
var _ = require('lodash');
1+
const assert = require('assert');
2+
const _ = require('lodash');
33

4-
5-
6-
7-
var FakeIpcProvider = function IpcProvider() {
4+
const FakeIpcProvider = function IpcProvider() {
85
var _this = this;
96
this.countId = 1;
107
this.notificationCount = 1;
@@ -32,7 +29,6 @@ var FakeIpcProvider = function IpcProvider() {
3229
this.notificationCallbacks = [];
3330
};
3431

35-
3632
FakeIpcProvider.prototype.send = function (payload, callback) {
3733
var _this = this;
3834

@@ -106,8 +102,6 @@ FakeIpcProvider.prototype.injectNotification = function (notification) {
106102
// this.response = response;
107103
// };
108104

109-
110-
111105
FakeIpcProvider.prototype.injectBatchResults = function (results, error) {
112106
var _this = this;
113107
this.response.push(results.map(function (r) {

0 commit comments

Comments
 (0)