I developed a smart contract that you:
1) Send 1 ether to an address, the contract saves your address
2) After 5 people have sent one ether, the contract chooses one address at random and sends the 5 ethers to that address
I read here: What is the order and concurrency behavior of multiple calls to a contract in a single transaction?
that the miner decides the order of the transactions.
If more than 5 transactions are sent to the contract within the same block, can the miner game the system to enter his own transaction and execute the smart contract on the order he decides to win every time?
The winning address is chosen using
random = uint(block.blockhash(block.number-1))%5 + 1;
I know a miner gets 5 ethers per each mined block + fees, so that's why I limited the inputs to 5 ethers total. The random function isn't that random, but the low payout (I thought) should make miners not care about gaming it. Here's the code of the function:
function () payable {
require(msg.value == 1 ether);
my_length +=1;
gamblers[my_length] = msg.sender;
if (my_length == 5) {
// pick a random number between 1 and 5
random = uint(block.blockhash(block.number-1))%5 + 1;
gamblers[random].transfer(5 ether);
my_length = 0;
}
Thanks