I have a smart contract which generates a random number and compares it with the user's input to check if both are equals, and based on that it updates a mapping. But anyhow the same transaction is getting mined twice. Not able to understand what is going wrong.
//function to store the guess in the mapping when a guess is made.
function guessIt(uint _guess) public returns (bool) {
//Need to check if the guess number is less than or equal tp range
require(_guess <= range);
uint random = generateRandomNumber(range);
// uint random = 10;
if (random == _guess) {
Guess storage correctGuess = guesses[msg.sender];
correctGuess.lastGuess = _guess;
correctGuess.lastCorrectGuess = _guess;
correctGuess.correctGuessCount = correctGuess.correctGuessCount + 1;
correctGuess.lastGuessAt = now;
totalNoOfGuesses = totalNoOfGuesses + 1;
Guessed(msg.sender,random, _guess);
return true;
} else {
Guess storage wrongGuess = guesses[msg.sender];
wrongGuess.lastGuess = _guess;
wrongGuess.wrongGuessCount = wrongGuess.wrongGuessCount + 1;
wrongGuess.lastGuessAt = now;
totalNoOfGuesses = totalNoOfGuesses + 1;
Guessed(msg.sender,random, _guess);
}
return false;
}
This is how I am calling the function in my aap.js file.
checkMyGuess: function() {
var guessInstance;
var guessedNumber = parseInt($('#myGuess').val());
console.log($('#myGuess').val());
console.log($('#myGuess').val());
var account = web3.eth.coinbase;
App.contracts.GuessNumber.deployed().then(function(instance){
guessInstance = instance;
return guessInstance.guessIt(guessedNumber, {from: account});
}).then(function(receipt){
var guessResult;
if (receipt.logs.length == 1) {
guessResult = receipt.logs[0].args._randomNumber.toNumber() === receipt.logs[0].args._guess.toNumber() ? "Congrats!! You Guessed it correct"
: "Sorry!! I thought it was " + receipt.logs[0].args._randomNumber.toNumber() + ".<br/> Better luck next time.";
}
$("#guessResult").html(guessResult);
});
}
My Random number function looks like.
function generateRandomNumber(uint _range) public returns (uint) { return uint(sha3(block.timestamp)) % _range; }Is this not the way I am supposed to do it? I am not facing the problem always, its sporadic. @Ismael – Arindam Jun 17 '18 at 04:40