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?

1 Answer 1

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
    }
}
2
  • "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? Commented May 26, 2017 at 12:44
  • @TripleSpeeder added an example.
    – jeff
    Commented May 26, 2017 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.