1

I would like to return partially fees to users of my dApp. Once they have reached X amount in fees, I want to make a function that will pay them ethereum automatically. Now my question: Is that secure? Or can they cheat? The code is following: In the beginning of every function, I call

uint _gasBegin = gasleft();

And in the end

uint _gasEnd = _gasBegin.sub(gasleft());
gasPaid[msg.sender] = gasPaid[msg.sender].add(_gasEnd);

Once they reached for example 0.001 ethereum in gas fees, they can call the funcion

returnFees()

that will transfer gasPaid[msg.sender] to them.

Edit: One way to cheat could be, that a miner sets a very high gas price in hope to empty the wallet. But however, he will not 100% surely mine the block and if it fails, he loses the fees to an other miner. Also I will limit this by allowing a max. price of for example 15 wei to be eligle to get back the fees by using (tx.gasprice <= 15, can be changed).

Is this...
1) A secure way, without them being able to cheat and
2) maybe improveable?

0

Depends on your implementation of returnFees. Remember to first zero the user's balance and only after that send the balance. If you do it the other way around the receiver can trigger a re-entrancy attack (call the same function again in their payable fallback function).

I don't really see how a miner could try to empty the wallet with setting a high gas price. Even if there was some way to cheat the system with such, as you said the miner most likely won't be able to mine the block himself.

As for improvements, it depends on your code. If it's pretty static code you should measure the gas utilization in advance and just use a static value for that. All calculations cost gas and the more gas you use for your gas returning functionality the better.

  • As far as I know, reentrancy only works, when I work with msg.data. In my case I don't. I hope I'm right here. A miner could cheat out all the balance by setting an extreme high gas price (let's say 100 wei). Now if the gas value is 200000 gas it would cost the miner 20000000 wei. If the miner now mines the block where that transaction is, he would get the gas back. By setting a max price of 15, this won't be able. – dkb May 18 at 15:08
  • Reentrancy attack doesn't have much to do with msg.data. It's about the order of execution. Have a look here for example: medium.com/@gus_tavo_guim/… As for your miner vulnerability, there's not much to gain if they spend a lot of gas to gain (some of) the same gas back. – Lauri Peltonen May 18 at 15:23
  • I understood. I was not vulnerable for reentrance as I anyway put the transfer function always into the last possible row in my contract. Sorry for misunderstanding. Regarding the miners, it might not be a win for them, but a big loss for the contract/the project. If they'd be able to steal all funds with using high gas prices, that definetly is a risk. – dkb May 18 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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