How can I check that the miner actually execute the right order of smart contracts function calls, if there is a necessity of orders?

  • 1
    Declare all of them private and provide a single public function which executes them in the desired order. Alternatively, you can add a state variable to your contract (enum would be ideal for this purpose) and use it in order to implement a state-machine. In each function, require that this variable is set to a designated value at the beginning of the function, and change it to the next value at the end of the function. – goodvibration Dec 10 '18 at 15:30

You cannot ensure the miners accept transactions in the intended order, but you can check and reject if they don't. You would make each new transaction dependent on something that came earlier.

Here, I just use a simple counter.

pragma solidity 0.5.1;

contract Ordered {

    uint public nonce;

    event LogNextTransaction(address sender, uint transactionNumber);

    function doSomethingOrdered(uint transactionNumber) public {
        require(transactionNumber == nonce);
        emit LogNextTransaction(msg.sender, transactionNumber);

This method is sort of loose in that any participant can just guess at the next nonce without knowledge of what came before (because it's just counting). You can make senders show they have knowledge of the previous transaction by making the requirement something they cannot (easily) guess. There is a non-obvious opportunity to create a race condition if the senders are competitive. Ask me for details if you sense that might be relevant.

pragma solidity 0.5.1;

contract Ordered {

    bytes32 public lastHash;

    event LogNextTransaction(address sender, bytes32 txnHash);

    function doSomethingOrdered(bytes32 thisHash) public {
        bytes32 testHash = hashHelper(lastHash, msg.sender);
        require(testHash == thisHash);
        emit LogNextTransaction(msg.sender, testHash);
        lastHash = testHash;

    // convenience function helps clients compute needed hashes
    function hashHelper(bytes32 hash, address sender) public pure returns(bytes32) {
        return keccak256(abi.encodePacked(hash, sender));

Hope it helps.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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