-
-
Notifications
You must be signed in to change notification settings - Fork 799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reentrancy Bug in Market Maker Example #2014
Comments
Yep, this appears to be true. You suggested solution would definitely work to mitigate this, or it might also be a good spot to show off our If you want to fix it, let me know, otherwise I'll look into making this change when I get a chance. |
You're right, the Since I only have an already built version of vyper and don't know everything that I should consider before making a commit / pull request, I would gratefully accept your offer to make this change. |
On further inspection, this is not true, due to the 2300 gas limit on vyper/tests/parser/functions/test_send.py Lines 31 to 72 in 1ebce95
It may be time to re-evaluate this mechanic, and explore using a "default reentrancy guard" on all outgoing cc @iamdefinitelyahuman thoughts? |
Relying on the 2300 gas sent seems bad, yeah. And limiting the gas to 2300 is also a bit outdated and we should rethink that IMO: https://diligence.consensys.net/blog/2019/09/stop-using-soliditys-transfer-now/ I'm not sure how I feel about a default re-entrancy guard either but definitely into having a conversation about it. |
Lol, already had this discussion: #1756 |
What's your issue about?
The example examples/market_maker/on_chain_market_maker.vy seems to have a reentrancy bug. ethToTokens and tokensToEth first make an external function call before they update local state variables.
The effect of this reentrancy bug is demonstrated by the following example:
initiate gets called with a
token_quantity
of 20 andmsg.value
of 100.Contract a calls tokensToEth with a
sell_quantity
of 40.sell_quantity
of 40.totalEthQty
to 50 andtotalTokenQty
to 60.totalEthQty
to 50 andtotalTokenQty
to 40.The contract has now 60 Tokens and 0 ether. Therefore, the invariant does not hold anymore and
totalEthQty
as well astotalTokenQty
do no longer model the current state.I assume that these examples just demonstrate what Vyper contracts can do, but I would rather suggest to fix such a reentrancy bug not to show case anti-patterns.
How can it be fixed?
Move the call to transfer at the end of ethToTokens and also move send at the end of tokensToEth.
The text was updated successfully, but these errors were encountered: