So user a and user b both call a function (transaction) on a deployed contract with the code as bellow.
uint numberA;
uint numberB;
address playerA;
address playerB;
function playANumber(uint number){
if(msg.sender == playerA && numberA == 0 && number > 0){
numberA = number;
}
if(msg.sender == playerB && numberB == 0 && number > 0){
numberB = number;
}
if(numberA > 0 && numberB > 0){
//dosomething
}
}
Will the part in "do something" always get called and if not, how can I make sure that it will. Or should it be split in two, setting the number and another transaction or call to execute the code in "do something"?
I have a contract with a similar setup and 90% of the time it works but theres also that 10% where the transaction clientside does not error but when I look at the state of the contract only numberA or B is set.
EDIT:
So I'm using truffle to call a deployed contract. PlayerA and PlayerB are simultaneous calling the contract. If I just rely on a successful transaction and don't explicitly check the state only the state change of one player is actually persisted. I use two different browsers for each player. The following pseudocode is used to call the contract and then check the change. Retrying after checking if the change actually is present does get the others player number in the contract and then the dosomething part is called.
contract.playANumber(1, {from: "someAddress"})).then( (tx) => {
console.log(tx);
return contract.getTheNumbers.call({from:"someAddress" });
})
.then( numbers=>{
if( (numbers.A > 0 && iAmPlayerA()) ||
(numbers.B > 0 && !iAmPlayerA() ){
console.log('state is ok');
} else{
console.log('oops try again later');
}
})
Output of Player A:
Output of Player B: