JMLX42 / cocorico

Cocorico is an open online voting platform powered by the blockchain.
https://cocorico.cc
MIT License
89 stars 21 forks source link

Block number race condition in the ballot consumer #102

Closed JMLX42 closed 7 years ago

JMLX42 commented 7 years ago

handleRegisteringBallot and handleCastingBallot will read the bock number using web3.eth.getBlockNumber() assuming the transaction they are waiting for as not been mined already.

But if there are lots of ballot messages, the corresponding transaction could have been mined before we get a chance to watch its events. Thus, the step will never end because the block number will be too high already.

Instead of calling web3.eth.getBlockNumber() in handleRegisteringBallot and handleCastingBallot, it should be read in the previous step, saved in the ballot message object and fetched back when required.

Exemple:

async function handleRegisteredBallot(ballot) {
  ballot.registeredBlockNumber = await promise((cb)=>web3.eth.getBlockNumber(cb))();
  // ...
}

async function handleCastingBallot(ballot) {
  const blockNumber = ballot.registeredBlockNumber;
  // ...
}