⚒️Mitigation Steps
Preventing Front Running Preventing or mitigating front running in blockchain applications is an ongoing research challenge, but several strategies have been developed and are currently being used. Here are a few notable techniques:
Commit-Reveal Schemes: This is a two-phase approach that obscures the crucial information of a transaction until it's too late for anyone else to act on it. Commit Phase: Users submit a hashed version of their transaction, effectively committing to their operation without revealing the details. The hash function ensures that it's practically impossible for anyone to discern the specifics of the transaction from the hash alone. Reveal Phase: After all commitments have been made, users submit their original (unhashed) transactions in a second "reveal" transaction. The smart contract then verifies that the revealed transaction matches the previously submitted hash and executes the operation.
Remember our vulnerable code by Solidity by example in our example section?
Here would be a solution:
A commitment scheme is a cryptographic algorithm used to allow someone to commit to a value while keeping it hidden from others with the ability to reveal it later. The values in a commitment scheme are binding, meaning that no one can change them once committed. The scheme has two phases: a commit phase in which a value is chosen and specified, and a reveal phase in which the value is revealed and checked.
Adam deploys SecuredFindThisHash with 10 Ether.
Bob finds the correct string that will hash to the target hash. ("Ethereum").
Bob then finds the keccak256(Address in lowercase + Solution + Secret). Address is his wallet address in lowercase, solution is "Ethereum", Secret is like an password ("mysecret") that only Bob knows whic Bob uses to commit and reveal the solution. keccak2566("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266Ethereummysecret") = '0xf95b1dd61edc3bd962cdea3987c6f55bcb714a02a2c3eb73bd960d6b4387fc36'
Bob then calls commitSolution("0xf95b1dd61edc3bd962cdea3987c6f55bcb714a02a2c3eb73bd960d6b4387fc36"), where he commits the calculated solution hash with gas price set to 15 gwei.
Eric is watching the transaction pool for the answer to be submitted.
Eric sees Bob's answer and he also calls commitSolution("0xf95b1dd61edc3bd962cdea3987c6f55bcb714a02a2c3eb73bd960d6b4387fc36") with a higher gas price than Bob (100 gwei).
Eric transaction was mined before Bob's transaction, but Eric has not got the reward yet. He needs to call revealSolution() with exact secret and solution, so lets say he is watching the transaction pool to front run Bob as he did previously
Then Bob calls the revealSolution("Ethereum", "mysecret") with gas price set to 15 gwei;
Let's consider that Eric who's watching the transaction pool, find's Bob's reveal solution transaction and he also calls revealSolution("Ethereum", "mysecret") with higher gas price than Bob (100 gwei)
Let's consider that this time also Eric reveal transaction was mined before Bob's transaction, but Eve will be reverted with "Hash doesn't match" error. Since the revealSolution() function checks the hash using keccak256(msg.sender + solution + secret). So this time eve fails to win the reward. 10.But Bob's revealSolution("Ethereum", "mysecret") passes the hash check and gets the reward of 10 ether.
Last updated