0

I'm running the following code as part of a truffle test:

electionController = ElectionController.new(districtsNo,districtFactory.address,voteTokenFactory.address);

function createVoteToken(electionController, districtNo,x){
        for(let i = 0; i<x; i++){
            let tokenID = web3.utils.randomHex(256);

            electionController.mintVote(tokenID,districtNo,0).then((tx)=>{

                voteTokenIDs[districtNo-1].push(tokenID);

                console.log(districtNo + '\t' + i + ': ' + tokenID + '\n');
            }).catch((err)=>{console.log(err);});
        }
    }

createVoteToken(electionController,i+1,numberOfVotesEachDistrict[i]);

This loop will run about 20,000 times, sending a transaction each time but the call-back function never runs for any of them, whatsmore when examining the blockchain it seems the transactions aren't even being picked up the miner.

At first I thought my contract code must be buggy, or it wasn't sending the transactions to the rpc endpoint, but when I rewrite the test code like so using await/async rather than callbacks:

electionController = ElectionController.new(districtsNo,districtFactory.address,voteTokenFactory.address);

async function createVoteToken(electionController, districtNo,x){
        for(let i = 0; i<x; i++){
            let tokenID = web3.utils.randomHex(256);

            await electionController.mintVote(tokenID,districtNo,0);

            voteTokenIDs[districtNo-1].push(tokenID);

            console.log(districtNo + '\t' + i + ': ' + tokenID + '\n');
        }
    }

await createVoteToken(electionController,i+1,numberOfVotesEachDistrict[i]);

....it works as expected. The transaction is picked up by the node, it succeeds, and I get the expected console output.

I've tried this with both ganache-cli and a local Geth chain (over rpc) and the behaviour appears to be exactly the same.

In both cases I've set the block time to 1 second and the gas limit to 12,500,000. (each transaction appears to average around 250,000 gas, so I'm definitely not hitting the block gas limit)

Is there something I'm doing wrong here ? I obviously can't use the async/await pattern as this loop is meant to run 13 times with >20,000 iterations each time for a total of over 300,000 transactions, so I naturally cannot afford to wait 1 second for each transaction to confirm. Not to mention the sheer size of the blockchain on my hard drive since every one of these little transactions would be placed into its own block if I wrote it this way.

Your Answer

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

Browse other questions tagged or ask your own question.