Skip to content

Commit 7cb9cd5

Browse files
committed
Basic unit tests complete for dev funds
Finished added unit tests for dev_funds. These are weak tests as the FakeIpcProvider does not provide enough support for keeping track of accounts and balances and instead we are injecting the provider responses in the tests, which kind of defeats the purposes of the tests. Instead, the FakeIpcProvider should be a little smarter and do more of what a real node would do.
1 parent 12304ea commit 7cb9cd5

File tree

3 files changed

+87
-85
lines changed

3 files changed

+87
-85
lines changed

lib/cmds/blockchain/blockchain.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,8 @@ Blockchain.prototype.run = function() {
170170
if (self.onReadyCallback && !self.readyCalled && data.indexOf('WebSocket endpoint opened') > -1) {
171171
if (self.isDev) {
172172
self.createFundAndUnlockAccounts((err) => {
173+
// TODO: this is never called!
173174
if(err) console.error('Error creating, unlocking, and funding accounts', err);
174-
//self.readyCalled = true;
175-
//self.onReadyCallback();
176175
});
177176
}
178177
self.readyCalled = true;
@@ -189,7 +188,7 @@ Blockchain.prototype.run = function() {
189188
};
190189

191190
Blockchain.prototype.createFundAndUnlockAccounts = function(cb) {
192-
let devFunds = new DevFunds(this.config);
191+
let devFunds = new DevFunds({blockchainConfig: this.config});
193192
devFunds.createFundAndUnlockAccounts((err) => {
194193
cb(err);
195194
});

lib/cmds/blockchain/dev_funds.js

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
const async = require('async');
22
const Web3 = require('web3');
3-
const { getWeiBalanceFromString, buildUrl } = require('../../utils/utils.js');
4-
const { readFileSync, dappPath } = require('../../core/fs');
3+
const {getWeiBalanceFromString, buildUrl} = require('../../utils/utils.js');
4+
const {readFileSync, dappPath} = require('../../core/fs');
55

66
class DevFunds {
7-
constructor(blockchainConfig, provider, logger) {
8-
this.blockchainConfig = blockchainConfig;
7+
constructor(options) {
8+
this.blockchainConfig = options.blockchainConfig;
99
this.accounts = [];
1010
this.numAccounts = this.blockchainConfig.account.numAccounts || 0;
11-
this.password = blockchainConfig.account.password ? readFileSync(dappPath(blockchainConfig.account.password), 'utf8').replace('\n', '') : 'dev_password';
11+
this.password = this.blockchainConfig.account.password ? readFileSync(dappPath(this.blockchainConfig.account.password), 'utf8').replace('\n', '') : 'dev_password';
1212
this.networkId = null;
1313
this.balance = Web3.utils.toWei("1", "ether");
14-
this.provider = provider || new Web3.providers.WebsocketProvider(buildUrl('ws', this.blockchainConfig.wsHost, this.blockchainConfig.wsPort), { headers: { Origin: "http://localhost:8000" } });
15-
this.web3 = new Web3(provider);
14+
this.provider = options.provider || new Web3.providers.WebsocketProvider(buildUrl('ws', this.blockchainConfig.wsHost, this.blockchainConfig.wsPort), {headers: {Origin: "http://localhost:8000"}});
15+
this.web3 = new Web3(this.provider);
1616
if (this.blockchainConfig.account.balance) {
1717
this.balance = getWeiBalanceFromString(this.blockchainConfig.account.balance, this.web3);
1818
}
19-
this.logger = logger || console;
19+
this.logger = options.logger || console;
2020
}
2121

2222
_sendTx() {
2323
if (this.networkId !== 1337) {
2424
return;
2525
}
26-
this.web3.eth.sendTransaction({ value: "1000000000000000", to: "0xA2817254cb8E7b6269D1689c3E0eBadbB78889d1", from: this.web3.eth.defaultAccount });
26+
this.web3.eth.sendTransaction({value: "1000000000000000", to: "0xA2817254cb8E7b6269D1689c3E0eBadbB78889d1", from: this.web3.eth.defaultAccount});
2727
}
2828

2929
// trigger regular txs due to a bug in geth and stuck transactions in --dev mode
@@ -48,7 +48,6 @@ class DevFunds {
4848
}
4949

5050
getCurrentAccounts(cb) {
51-
5251
this.web3.eth.getAccounts().then((accounts) => {
5352
this.web3.eth.defaultAccount = accounts[0];
5453
if (accounts.length > 1) {
@@ -80,25 +79,15 @@ class DevFunds {
8079
}
8180

8281
fundAccounts(balance, cb) {
83-
this.logger.info('[dev_funds]: funding accounts');
8482
async.each(this.accounts, (account, next) => {
85-
this.logger.info('[dev_funds]: funding acct ' + account);
8683
this.web3.eth.getBalance(account).then(currBalance => {
87-
this.logger.info('[dev_funds]: acct ' + account + ' current balance ' + currBalance);
8884
const remainingBalance = balance - currBalance;
8985
if (remainingBalance <= 0) return next();
9086

91-
this.logger.info('[dev_funds]: funding acct ' + account + ' with ' + remainingBalance);
92-
93-
this.web3.eth.sendTransaction({ to: account, value: remainingBalance }).then((_result) => {
94-
this.logger.info('[dev_funds]: funding result ' + JSON.stringify(_result));
95-
next();
96-
}).catch(next);
97-
}, (err) => {
98-
this.logger.info('[dev_funds]: done funding');
99-
cb(err);
100-
});
101-
});
87+
this.web3.eth.sendTransaction({to: account, value: remainingBalance}).catch(next);
88+
next(); // don't wait for the tx receipt as it never comes!
89+
}).catch(cb);
90+
}, cb);
10291
}
10392

10493
createFundAndUnlockAccounts(cb) {

test/devFunds.js

Lines changed: 72 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ let TestLogger = require('../lib/tests/test_logger.js');
44
const Web3 = require('web3');
55
const i18n = require('../lib/i18n/i18n.js');
66
const constants = require('../lib/constants.json');
7-
const Test = require('../lib/tests/test');
87
const DevFunds = require('../lib/cmds/blockchain/dev_funds');
98
const async = require('async');
109
const FakeIpcProvider = require('./helpers/fakeIpcProvider');
@@ -52,73 +51,88 @@ describe('embark.DevFunds', function () {
5251
config.rpcPort += constants.blockchain.servicePortOnProxy;
5352
}
5453

55-
// TODO put default config
56-
const test = new Test({ loglevel: 'trace' });
54+
describe('#create, fund, and unlock accounts', function () {
55+
let provider = new FakeIpcProvider();
56+
let devFunds = new DevFunds({blockchainConfig: config, provider: provider, logger: new TestLogger({})});
57+
const web3 = new Web3(provider);
5758

59+
it('should create correct number of accounts', function (done) {
60+
provider.injectResult(['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855']); // getAccounts - return --dev account
61+
devFunds.getCurrentAccounts(() => {
5862

59-
test.initWeb3Provider((err) => {
60-
if (err) throw err;
61-
describe('#create, fund, and unlock accounts', function () {
62-
let provider = new FakeIpcProvider();
63-
let devFunds = new DevFunds(config, provider, new TestLogger({}));
64-
const web3 = new Web3(provider);
63+
provider.injectResult('0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae'); // createAccount #1
64+
provider.injectResult('0x22f4d0a3c12e86b4b5f39b213f7e19d048276dab'); // createAccount #2
6565

66-
it('should create correct number of accounts', function (done) {
67-
provider.injectResult(['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855']); // getAccounts - return --dev account
68-
devFunds.getCurrentAccounts(() => {
66+
devFunds.createAccounts(config.account.numAccounts, 'test_password', (err) => {
67+
assert.equal(err, null);
6968

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

73-
74-
devFunds.createAccounts(config.account.numAccounts, 'test_password', (err) => {
75-
assert.equal(err, null);
76-
77-
provider.injectResult(['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', '0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae', '0x22f4d0a3c12e86b4b5f39b213f7e19d048276dab']);
78-
web3.eth.getAccounts().then((accts) => {
79-
console.log('got accts: ' + JSON.stringify(accts));
80-
assert.equal(accts.length, config.account.numAccounts);
81-
assert.strictEqual(accts[0], '0x47D33b27Bb249a2DBab4C0612BF9CaF4C1950855');
82-
assert.strictEqual(accts[1], '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe');
83-
assert.strictEqual(accts[2], '0x22F4d0A3C12E86b4b5F39B213f7e19D048276DAb');
84-
done();
85-
});
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();
8678
});
8779
});
8880
});
81+
});
82+
83+
it('should unlock accounts', function (done) {
84+
provider.injectResult(true); // account #1 unlock result
85+
provider.injectResult(true); // account #2 unlock result
86+
87+
devFunds.unlockAccounts(devFunds.password, (errUnlock) => {
88+
assert.equal(errUnlock, null);
89+
done();
90+
});
91+
});
92+
93+
it('should fund accounts', function (done) {
94+
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
8999

90-
it('should fund accounts', function (done) {
91-
console.dir('funding accounts...');
92-
93-
provider.injectResult('1234567890'); // account #1 balance
94-
provider.injectResult('1234567890'); // account #2 balance
95-
provider.injectResult('0xfff12345678976543213456786543212345675432'); // send tx #1
96-
provider.injectResult('0xfff12345678976543213456786543212345675433'); // send tx #2
97-
98-
try {
99-
devFunds.fundAccounts(devFunds.balance, (err) => {
100-
console.dir('accounts funded...');
101-
assert.equal(err, null);
102-
103-
provider.injectResult(['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', '0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae', '0x22f4d0a3c12e86b4b5f39b213f7e19d048276dab']);
104-
web3.eth.getAccounts().then((accts) => {
105-
console.log('got accts: ' + JSON.stringify(accts));
106-
107-
const weiFromConfig = utils.getWeiBalanceFromString(config.account.balance);
108-
async.each(accts, (acct, cb) => {
109-
provider.injectResult(web3.utils.numberToHex(weiFromConfig));
110-
devFunds.web3.eth.getBalance(acct).then((wei) => {
111-
assert.equal(wei, weiFromConfig);
112-
cb();
113-
}).catch(cb);
114-
}, function(err) { done(); });
115-
}).catch(() => {
116-
done();
117-
});
100+
devFunds.fundAccounts(devFunds.balance, (errFundAccounts) => {
101+
102+
assert.equal(errFundAccounts, null);
103+
104+
// inject response for web3.eth.getAccounts
105+
// TODO: make FakeIpcProvider smart enough to keep track of created accounts
106+
provider.injectResult(['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', '0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae', '0x22f4d0a3c12e86b4b5f39b213f7e19d048276dab']);
107+
108+
web3.eth.getAccounts().then((accts) => {
109+
110+
const weiFromConfig = utils.getWeiBalanceFromString(config.account.balance, web3);
111+
112+
async.each(accts, (acct, cb) => {
113+
114+
// inject response for web3.eth.getBalance.
115+
// essentially, this will always return the amount we specified
116+
// in the config.
117+
// this is dodgy. really, we should be letting the FakeIpcProvider
118+
// at this point tell us how many wei we have per account (as it would
119+
// in a real node), but the FakeIpcProvider is not smart enough... yet.
120+
// TODO: make FakeIpcProvider smart enough to keep track of balances
121+
provider.injectResult(web3.utils.numberToHex(weiFromConfig));
122+
123+
web3.eth.getBalance(acct).then((wei) => {
124+
assert.equal(wei, weiFromConfig);
125+
cb();
126+
}).catch(cb);
127+
128+
}, function (errAcctsBalance) {
129+
if (errAcctsBalance) throw errAcctsBalance;
130+
done();
118131
});
119-
} catch (errFundAccts) {
120-
throw errFundAccts;
121-
}
132+
}).catch((errGetAccts) => {
133+
if (errGetAccts) throw errGetAccts;
134+
done();
135+
});
122136
});
123137
});
124138
});

0 commit comments

Comments
 (0)