|
| 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