I'm trying to a smart-contract where user can register himself (in an sorted array) and when a amount of people is registered (here 2) a Battle is created and firsts registered users battle each other.
The code isn't an issue, but my main concern is what will appends if multiple requests come in the same bloc and miners will sort requests on a queue (who have to be the same for everyone) ? How can I avoid conflict ?
Example: I have 3 users (A, B, and C). A is already registered on the contract, B and C try to register in the same bloc. What is a miner take the transaction and do [A, B, C] so A will battle B and C will stay in the registered array. But in the same way another miner take [A, C, B] create a Battle between A and C and keep B. Both A+B and A+C battles happens in the same time and remaining array are not the same. What'll happens on this case ?
I tried to search online (and found nothing good), or code a simple example and test it using Ganache and multiple browser on Remix but it's really painful to test it..
This is my test code (who work fine..) But I don't know how to be sure about it ? Any idea, link, suggestion, ...
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
contract RegisteredQueue {
struct Registered {
address addr;
uint value;
}
struct Battle {
Registered reg1;
Registered reg2;
}
event NewRegistered (Registered indexed registered);
event NewBattle (Battle indexed battle);
Registered[] public registeredValues;
Battle[] public battlesDone;
function register(uint _value) public {
for (uint i=0; i<registeredValues.length; i++) {
require(registeredValues[i].addr != msg.sender, "You're already registred");
}
registeredValues.push(Registered(msg.sender, _value));
uint l = registeredValues.length;
emit NewRegistered(registeredValues[l-1]);
if (l >= 2) {
// Create a Battle between both
battlesDone.push(Battle(registeredValues[0], registeredValues[1]));
emit NewBattle(battlesDone[battlesDone.length - 1]);
// Remove 2 firsts elements in the array without creating gap
for (uint idx=2; idx<l; idx++) {
registeredValues[idx-2] = registeredValues[idx];
}
// Remove the 2 lasts (if exist)
registeredValues.pop();
registeredValues.pop();
}
}
function countRegistered() public view returns(uint) {
return registeredValues.length;
}
function countBattle() public view returns(uint) {
return battlesDone.length;
}
}