4

Let's say I have a first-come-first-serve bounty contract. Whoever calls "getBounty()" first gets all balance owned by the contract.

Now if 10 people create transactions calling the getBounty() function - How does the blockchain/the miner determine who comes first and gets the ethers?

TripleSpeeder
  • 920
  • 9
  • 16

1 Answers1

3

There are several related questions like this and this. IMHO, all the answers lead to "it's arbitrary".

Maybe you can make a central interface to your contract in order to further control the ordering of the transactions. You can also design your contract in such a way that it does not accept multiple transactions in a single block.

To implement this, you can store the "last" block in a global variable and update it like this:

contract C {
    uint lastBlock = 0;

    function myFunction(){
        if(block.number>lastBlock){
            lastBlock = block.number; // update lastBlock
        } else if (block.number == lastBlock){
            throw; // don't allow if this block already had a transaction
        }
        // do what the function is supposed to do here
    }
}
jeff
  • 2,550
  • 2
  • 18
  • 40
  • "You can also design your contract in such a way that it does not accept multiple transactions in a single block."

    Thats interesting. Could you elaborate a bit more how this could be approached?

    – TripleSpeeder May 26 '17 at 12:44
  • @TripleSpeeder added an example. – jeff May 26 '17 at 12:57