/ test / devFunds.js
devFunds.js
  1  /*global describe, it, before*/
  2  const assert = require('assert');
  3  let TestLogger = require('../lib/utils/test_logger');
  4  const Web3 = require('web3');
  5  const i18n = require('../lib/core/i18n/i18n.js');
  6  const constants = require('../lib/constants.json');
  7  const DevFunds = require('../lib/modules/blockchain_process/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        // this conversion is normally done when constructing a Config object
 35        balance: utils.getWeiBalanceFromString("5 ether", Web3)
 36      },
 37      bootnodes: "",
 38      wsApi: ["eth", "web3", "net", "shh", "debug"],
 39      wsHost: "localhost",
 40      wsOrigins: false,
 41      wsPort: 8546,
 42      wsRPC: true,
 43      targetGasLimit: false,
 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      // TOCHECK: DevFunds does not provide this function anymore, please consider to remove this test
 65      it('should create correct number of accounts', function(done) {
 66        provider.injectResult('0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae'); // createAccount #1
 67        provider.injectResult('0x22f4d0a3c12e86b4b5f39b213f7e19d048276dab'); // createAccount #2
 68  
 69        devFunds.createAccounts(config.account.numAccounts, 'test_password', (err) => {
 70          assert.equal(err, null);
 71  
 72          // TODO: make FakeIpcProvider smart enough to keep track of created accounts
 73          provider.injectResult(['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', '0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae', '0x22f4d0a3c12e86b4b5f39b213f7e19d048276dab']);
 74  
 75          web3.eth.getAccounts().then((accts) => {
 76            assert.equal(accts.length, config.account.numAccounts);
 77            assert.strictEqual(accts[0], '0x47D33b27Bb249a2DBab4C0612BF9CaF4C1950855'); // --dev acct
 78            assert.strictEqual(accts[1], '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe'); // created acct #1
 79            assert.strictEqual(accts[2], '0x22F4d0A3C12E86b4b5F39B213f7e19D048276DAb'); // created acct #2
 80            done();
 81          });
 82        });
 83      });
 84  
 85      // TOCHECK: DevFunds does not provide this function anymore, please consider to remove this test
 86      it('should unlock accounts', function(done) {
 87        if (devFunds.accounts.length === 0) {
 88          assert.equal(true, true, "no accounts to unlock");
 89          return done();
 90        }
 91  
 92        devFunds.accounts.forEach(_acct => {
 93          provider.injectResult(true); // account unlock result
 94        });
 95  
 96        devFunds.unlockAccounts(devFunds.password, (errUnlock) => {
 97          assert.equal(errUnlock, null);
 98          done();
 99        });
100      });
101  
102      it('should fund accounts', function(done) {
103  
104        if (devFunds.accounts.length === 0) {
105          assert.equal(true, true, "no accounts to fund");
106          return done();
107        }
108        devFunds.accounts.forEach(_acct => {
109          provider.injectResult('1234567890'); // account balance
110          // provider.injectResult('0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe'); // send tx response
111        });
112  
113        devFunds.fundAccounts(devFunds.balance, (errFundAccounts) => {
114          assert.equal(errFundAccounts, null);
115  
116          // inject response for web3.eth.getAccounts
117          // TODO: make FakeIpcProvider smart enough to keep track of created accounts
118          provider.injectResult(['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', '0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae', '0x22f4d0a3c12e86b4b5f39b213f7e19d048276dab']);
119  
120          web3.eth.getAccounts().then((accts) => {
121  
122            const weiFromConfig = config.account.balance;
123  
124            async.each(accts, (acct, cb) => {
125  
126              // inject response for web3.eth.getBalance.
127              // essentially, this will always return the amount we specified
128              // in the config.
129              // this is dodgy. really, we should be letting the FakeIpcProvider
130              // at this point tell us how many wei we have per account (as it would
131              // in a real node), but the FakeIpcProvider is not smart enough... yet.
132              // TODO: make FakeIpcProvider smart enough to keep track of balances
133              provider.injectResult(web3.utils.numberToHex(weiFromConfig));
134  
135              web3.eth.getBalance(acct).then((wei) => {
136                assert.equal(wei, weiFromConfig);
137                cb();
138              }).catch(cb);
139  
140            }, function(errAcctsBalance) {
141              if (errAcctsBalance) throw errAcctsBalance;
142              done();
143            });
144          }).catch((errGetAccts) => {
145            if (errGetAccts) throw errGetAccts;
146            done();
147          });
148        });
149      });
150    });
151  });