Skip to content

Commit 807950d

Browse files
authored
Merge pull request #667 from embark-framework/patch/dev_funds-tests
Patch: Basic unit tests complete for dev_funds
2 parents c577ad6 + 89651dd commit 807950d

File tree

5 files changed

+329
-49
lines changed

5 files changed

+329
-49
lines changed

lib/cmds/blockchain/blockchain.js

Lines changed: 5 additions & 5 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,9 +188,10 @@ Blockchain.prototype.run = function() {
189188
};
190189

191190
Blockchain.prototype.createFundAndUnlockAccounts = function(cb) {
192-
let devFunds = new DevFunds(this.config);
193-
devFunds.createFundAndUnlockAccounts((err) => {
194-
cb(err);
191+
DevFunds.new({blockchainConfig: this.config}).then(devFunds => {
192+
devFunds.createFundAndUnlockAccounts((err) => {
193+
cb(err);
194+
});
195195
});
196196
};
197197

lib/cmds/blockchain/dev_funds.js

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,32 @@ const {getWeiBalanceFromString, buildUrl} = require('../../utils/utils.js');
44
const {readFileSync, dappPath} = require('../../core/fs');
55

66
class DevFunds {
7-
constructor(blockchainConfig) {
8-
// TODO: temporary to avoid nasty suprises, should be removed
9-
try {
10-
this.web3 = null;
11-
this.blockchainConfig = blockchainConfig;
12-
this.accounts = [];
13-
this.numAccounts = this.blockchainConfig.account.numAccounts || 0;
14-
this.password = readFileSync(dappPath(blockchainConfig.account.password), 'utf8').replace('\n', '');
15-
this.web3 = new Web3();
16-
this.networkId = null;
17-
this.balance = Web3.utils.toWei("1", "ether");
18-
if (this.blockchainConfig.account.balance) {
19-
this.balance = getWeiBalanceFromString(this.blockchainConfig.account.balance, this.web3);
20-
}
21-
} catch(_err) {
22-
// empty for now
7+
constructor(options) {
8+
this.blockchainConfig = options.blockchainConfig;
9+
this.accounts = [];
10+
this.numAccounts = this.blockchainConfig.account.numAccounts || 0;
11+
this.password = this.blockchainConfig.account.password ? readFileSync(dappPath(this.blockchainConfig.account.password), 'utf8').replace('\n', '') : 'dev_password';
12+
this.networkId = null;
13+
this.balance = Web3.utils.toWei("1", "ether");
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);
16+
if (this.blockchainConfig.account.balance) {
17+
this.balance = getWeiBalanceFromString(this.blockchainConfig.account.balance, this.web3);
18+
}
19+
this.logger = options.logger || console;
20+
}
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);
2333
}
2434
}
2535

@@ -31,37 +41,24 @@ class DevFunds {
3141
}
3242

3343
// trigger regular txs due to a bug in geth and stuck transactions in --dev mode
34-
regularTxs(cb) {
44+
_regularTxs(cb) {
3545
const self = this;
3646
self.web3.eth.net.getId().then((networkId) => {
3747
self.networkId = networkId;
3848
if (self.networkId !== 1337) {
3949
return;
4050
}
4151

42-
setInterval(function() { self._sendTx(); }, 1500);
52+
setInterval(function () { self._sendTx(); }, 1500);
4353
if (cb) {
4454
cb();
4555
}
4656
});
4757
}
4858

49-
regularUnlocks() {
59+
_regularUnlocks() {
5060
const self = this;
51-
setInterval(function() { self.unlockAccounts(self.password, () => {}); }, 20000);
52-
}
53-
54-
connectToNode(cb) {
55-
56-
this.web3.setProvider(new Web3.providers.WebsocketProvider(buildUrl('ws', this.blockchainConfig.wsHost, this.blockchainConfig.wsPort), {headers: {Origin: "http://localhost:8000"}}));
57-
58-
this.web3.eth.getAccounts().then((accounts) => {
59-
this.web3.eth.defaultAccount = accounts[0];
60-
if (accounts.length > 1) {
61-
this.accounts = accounts.slice(1);
62-
}
63-
cb();
64-
});
61+
setInterval(function () { self.unlockAccounts(self.password, () => { }); }, 20000);
6562
}
6663

6764
createAccounts(numAccounts, password, cb) {
@@ -86,36 +83,31 @@ class DevFunds {
8683
}
8784

8885
fundAccounts(balance, cb) {
89-
9086
async.each(this.accounts, (account, next) => {
9187
this.web3.eth.getBalance(account).then(currBalance => {
9288
const remainingBalance = balance - currBalance;
9389
if (remainingBalance <= 0) return next();
9490

95-
this.web3.eth.sendTransaction({to: account, value: remainingBalance}).then((_result) => {
96-
next();
97-
}).catch(next);
98-
}, cb);
99-
});
91+
this.web3.eth.sendTransaction({to: account, value: remainingBalance}).catch(next);
92+
next(); // don't wait for the tx receipt as it never comes!
93+
}).catch(cb);
94+
}, cb);
10095
}
10196

10297
createFundAndUnlockAccounts(cb) {
10398
if (!this.web3) {
10499
return cb();
105100
}
106101
async.waterfall([
107-
(next) => {
108-
this.connectToNode(next);
109-
},
110102
(next) => {
111103
this.createAccounts(this.numAccounts, this.password, next);
112104
},
113105
(next) => {
114106
this.unlockAccounts(this.password, next);
115107
},
116108
(next) => {
117-
this.regularTxs();
118-
this.regularUnlocks();
109+
this._regularTxs();
110+
this._regularUnlocks();
119111
this.fundAccounts(this.balance, next);
120112
}
121113
], cb);

test/devFunds.js

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*global describe, it, before*/
2+
const assert = require('assert');
3+
let TestLogger = require('../lib/tests/test_logger.js');
4+
const Web3 = require('web3');
5+
const i18n = require('../lib/i18n/i18n.js');
6+
const constants = require('../lib/constants.json');
7+
const DevFunds = require('../lib/cmds/blockchain/dev_funds');
8+
const async = require('async');
9+
const FakeIpcProvider = require('./helpers/fakeIpcProvider');
10+
const utils = require('../lib/utils/utils');
11+
i18n.setOrDetectLocale('en');
12+
13+
describe('embark.DevFunds', function () {
14+
let config = {
15+
networkType: 'livenet',
16+
genesisBlock: 'foo/bar/genesis.json',
17+
geth_bin: 'geth',
18+
datadir: '/foo/datadir/',
19+
mineWhenNeeded: true,
20+
rpcHost: 'someserver',
21+
rpcPort: 12345,
22+
rpcApi: ['eth', 'web3', 'net', 'debug'],
23+
rpcCorsDomain: true,
24+
networkId: 1,
25+
port: 123456,
26+
nodiscover: true,
27+
maxpeers: 25,
28+
mine: true,
29+
vmdebug: false,
30+
whisper: false,
31+
account: {
32+
password: './test/test1/password',
33+
numAccounts: 3,
34+
balance: "5 ether"
35+
},
36+
bootnodes: "",
37+
wsApi: ["eth", "web3", "net", "shh", "debug"],
38+
wsHost: "localhost",
39+
wsOrigins: false,
40+
wsPort: 8546,
41+
wsRPC: true,
42+
targetGasLimit: false,
43+
syncMode: undefined,
44+
syncmode: undefined,
45+
verbosity: undefined,
46+
proxy: true
47+
};
48+
49+
if (config.proxy) {
50+
config.wsPort += constants.blockchain.servicePortOnProxy;
51+
config.rpcPort += constants.blockchain.servicePortOnProxy;
52+
}
53+
54+
describe('#create, fund, and unlock accounts', function () {
55+
let provider = new FakeIpcProvider();
56+
const web3 = new Web3(provider);
57+
let devFunds;
58+
59+
before(async () => {
60+
provider.injectResult(['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855']); // getAccounts: return --dev account
61+
devFunds = await DevFunds.new({blockchainConfig: config, provider: provider, logger: new TestLogger({})});
62+
});
63+
64+
it('should create correct number of accounts', function (done) {
65+
provider.injectResult('0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae'); // createAccount #1
66+
provider.injectResult('0x22f4d0a3c12e86b4b5f39b213f7e19d048276dab'); // createAccount #2
67+
68+
devFunds.createAccounts(config.account.numAccounts, 'test_password', (err) => {
69+
assert.equal(err, null);
70+
71+
// TODO: make FakeIpcProvider smart enough to keep track of created accounts
72+
provider.injectResult(['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', '0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae', '0x22f4d0a3c12e86b4b5f39b213f7e19d048276dab']);
73+
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();
80+
});
81+
});
82+
});
83+
84+
it('should unlock accounts', function (done) {
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+
});
93+
94+
devFunds.unlockAccounts(devFunds.password, (errUnlock) => {
95+
assert.equal(errUnlock, null);
96+
done();
97+
});
98+
});
99+
100+
it('should fund accounts', function (done) {
101+
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+
});
110+
111+
devFunds.fundAccounts(devFunds.balance, (errFundAccounts) => {
112+
113+
assert.equal(errFundAccounts, null);
114+
115+
// inject response for web3.eth.getAccounts
116+
// TODO: make FakeIpcProvider smart enough to keep track of created accounts
117+
provider.injectResult(['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', '0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae', '0x22f4d0a3c12e86b4b5f39b213f7e19d048276dab']);
118+
119+
web3.eth.getAccounts().then((accts) => {
120+
121+
const weiFromConfig = utils.getWeiBalanceFromString(config.account.balance, web3);
122+
123+
async.each(accts, (acct, cb) => {
124+
125+
// inject response for web3.eth.getBalance.
126+
// essentially, this will always return the amount we specified
127+
// in the config.
128+
// this is dodgy. really, we should be letting the FakeIpcProvider
129+
// at this point tell us how many wei we have per account (as it would
130+
// in a real node), but the FakeIpcProvider is not smart enough... yet.
131+
// TODO: make FakeIpcProvider smart enough to keep track of balances
132+
provider.injectResult(web3.utils.numberToHex(weiFromConfig));
133+
134+
web3.eth.getBalance(acct).then((wei) => {
135+
assert.equal(wei, weiFromConfig);
136+
cb();
137+
}).catch(cb);
138+
139+
}, function (errAcctsBalance) {
140+
if (errAcctsBalance) throw errAcctsBalance;
141+
done();
142+
});
143+
}).catch((errGetAccts) => {
144+
if (errGetAccts) throw errGetAccts;
145+
done();
146+
});
147+
});
148+
});
149+
});
150+
});

0 commit comments

Comments
 (0)