1

I'd like to test a contract using ~100s of different accounts. To start with I'd like to encapsulate contract interactions with successive addresses in a for loop. For argument's sake, using this contract:

contract UnitCounter {
    mapping (address => uint256) public UnitsFrom;
    uint256 public TotalUnits;

    function submitUnits(uint256 Units) {
        UnitsFrom[msg.sender] = Units;
        TotalUnits += Units;
    }
}

They way I imagine doing this is with the following pseudo code (where abi is from above):

var MyContract = web3.eth.contract(abi)
var myContractInstance = MyContract.at('0xE522E4A145f345925c80C5Cf4b67a5fa00304875')
var randnumberlist = [4,5,1,3,8,7,2,8,9,9,2]
for (var x = 0; x < 10; x++){
    personal.newAccount("pword")
    rand=randnumberlist[x]
    personal.unlockAccount(eth.coinbase,"coinbasepword",300) 
    eth.sendTransaction({from: eth.coinbase, to: eth.accounts[personal.listAccounts.length-1], gasPrice: "1000"})
    personal.unlockAccount(eth.accounts[personal.listAccounts.length-2],"pword",300)
    var randnumberlist = [4,5,1,3,8,7,2,8,9,9,2]
    myContractInstance.submitUnits(rand, {from: eth.accounts[personal.listAccounts.length-1], gas:3000000})
}

Is it possible to achieve loops like the above using the geth javascript console? If not, could I use the geth command line options with a loop implemented on the OS command line?

Looking at the docs it seems this is not possible. How else might I achieve it?

EDIT:

I think I've almost managed (code amended). However I'm now getting this error:

ReferenceError: 'MyContractInstance' is not defined
    at <anonymous>:10:1

Possibly I need to wait for the transaction to be mined first.

  • I'm not sure if your example is taken from actual code, but it looks like it's got a bit of a bug. submitUnits uses = to set UnitsFrom, but TotalUnits is increased with +=. – Matthew Schmidt Jan 5 '17 at 22:58
  • @MatthewSchmidt thanks - that was intentional - a mapping and a running total. Code is actually recycled from this question. – atomh33ls Jan 6 '17 at 10:25
  • There was a typo myContractInstance vs MyContractInstance - that's what caused the error... – atomh33ls Apr 6 '17 at 9:44
2

It looks like you've got a handle on this anyway, but there's probably an easier way to go about testing with hundreds of accounts: testrpc.

With testrpc -a 1000 you can create a simulated blockchain with a thousand virtual accounts filled with ether. It will be vastly faster to use it as well, since testrpc does not actually mine, and you will not have to spend any time fooling with sending virtual ETH around. To use testrpc with geth's console, use geth attach rpc:http://localhost:8545.

As for the second question, contract.at() returns immediately--there's no mining involved, as it's used for already existing contracts. Are you sure what you're getting out of web3.eth.contract() is actually a contract object?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.