Skip to content

Gas estimation#2623

Merged
konradkonrad merged 7 commits into
raiden-network:masterfrom
hackaugusto:gas_estimation
Nov 7, 2018
Merged

Gas estimation#2623
konradkonrad merged 7 commits into
raiden-network:masterfrom
hackaugusto:gas_estimation

Conversation

@hackaugusto
Copy link
Copy Markdown
Contributor

@hackaugusto hackaugusto commented Sep 27, 2018

Integration tests are flaky because the current code base is using a
percentage of the block gas limit as the transaction startgas. This is a
problem when there are too few transactions and the gas limit decreases
by more than 20%, making the transaction sent invalid and consectuvily
the ethereum node rejects it with a 'exceeds block gas limit' message.

For the version v0.4.0 of the smart contract these functions have a
predictable gas profile and can use hard coded constats:

  • EndpointRegistry:registerEndpoint
  • TokenNetworkRegistry:createERC20TokenNetwork
  • TokenNetwork:closeChannel
  • TokenNetwork:updateNonClosingBalanceProof

For these functions we call external smart contracts that can have a
variable gas usage, therefore must be estimated.

  • TokenNetwork:openChannel -> uses token.balanceOf
  • TokenNetwork:setTotalDeposit -> uses token.balanceOf and
    token.transferFrom
  • TokenNetwork:setTotalWithdraw -> uses token.transfer
  • TokenNetwork:settleChannel -> uses token.transfer
  • TokenNetwork:unlock -> uses token.transfer
  • TokenNetwork:cooperativeSettle -> uses token.transfer

The proxies were updated to use a gas estimation when it makes sense,
similar to what was introduced by 454671a.

The estimate_and_transact function was broken by the byzantium release
and fixed with the max gas limit by 1f18640,
which was removed from the code base by f1d055c
and implicitely changed the code to use the block gas limit.

Fixes #2921

@hackaugusto hackaugusto force-pushed the gas_estimation branch 5 times, most recently from 2c6562f to 89267fa Compare September 27, 2018 15:26
@hackaugusto hackaugusto force-pushed the gas_estimation branch 3 times, most recently from d0e3e45 to bbebc20 Compare September 27, 2018 22:22
Copy link
Copy Markdown
Contributor

@palango palango left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments and questions

Comment thread raiden/constants.py
Comment thread raiden/network/proxies/token.py Outdated
Comment thread raiden/network/proxies/token_network.py Outdated
Comment thread raiden/network/rpc/client.py Outdated
Comment thread tools/transfer_eth.py Outdated
Comment thread raiden/network/proxies/token.py Outdated
Comment thread raiden/network/proxies/token_network.py Outdated
Comment thread tools/init_blockchain.py Outdated
Comment thread tools/scenario-player/scenario_player/runner.py Outdated
@andrevmatos andrevmatos mentioned this pull request Oct 1, 2018
Comment thread raiden/network/proxies/token.py Outdated
Comment thread raiden/network/proxies/token_network.py Outdated
Comment thread raiden/network/proxies/token_network.py Outdated
@palango
Copy link
Copy Markdown
Contributor

palango commented Nov 5, 2018

I'll rebase this now

Integration tests are flaky because the current code base is using a
percentage of the block gas limit as the transaction startgas (cached
for 600 seconds). This is a problem when there are too few transactions
and the gas limit decreases by more than 20%, making the transaction
sent invalid and consectuvily the ethereum node rejects it with a
'exceeds block gas limit' message.

For the version v0.4.0 of the smart contract these functions have a
predictable gas profile and can use hard coded constats:

- EndpointRegistry:registerEndpoint
- TokenNetworkRegistry:createERC20TokenNetwork
- TokenNetwork:closeChannel
- TokenNetwork:updateNonClosingBalanceProof
- SecretRegistry:registerSecret
- SecretRegistry:registerSecretBatch

For these functions we call external smart contracts that can have a
variable gas usage, therefore must be estimated.

- TokenNetwork:openChannel -> uses token.balanceOf
- TokenNetwork:setTotalDeposit -> uses token.balanceOf and
token.transferFrom
- TokenNetwork:setTotalWithdraw -> uses token.transfer
- TokenNetwork:settleChannel -> uses token.transfer
- TokenNetwork:unlock -> uses token.transfer
- TokenNetwork:cooperativeSettle -> uses token.transfer

The proxies were updated to use a gas estimation when it makes sense,
similar to what was introduced by 454671a.

The estimate_and_transact function was broken by the byzantium release
and fixed with the max gas limit by 1f18640,
which was removed from the code base by f1d055c
and implicitely changed the code to use the block gas limit.
@palango palango force-pushed the gas_estimation branch 2 times, most recently from cf465e7 to 7f20a9b Compare November 6, 2018 13:41
Comment thread raiden/constants.py Outdated
Comment thread raiden/network/proxies/token_network.py Outdated
Comment thread raiden/network/proxies/token_network.py
Comment thread raiden/network/proxies/token_network.py Outdated
Comment thread raiden/network/proxies/token_network.py Outdated
Comment thread raiden/tests/integration/rpc/test_assumptions.py Outdated
Comment thread raiden/tests/integration/test_endpointregistry.py Outdated
Comment thread raiden/tests/integration/rpc/test_assumptions.py Outdated
Copy link
Copy Markdown
Contributor

@konradkonrad konradkonrad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, that looks good!

@konradkonrad konradkonrad merged commit f3f9fac into raiden-network:master Nov 7, 2018
LefterisJP added a commit to LefterisJP/raiden that referenced this pull request Nov 8, 2018
After the changes introduced by
raiden-network#2623 we again rely on gas
estimation for the gas limit to provide to an onchain call which is great since
it reduces our overall ETH balance requirements.

But we don't take into account the fact that if estimateGas returns None it's
because the transaction will fail, either due to our account not having enough
funds or because the call will just revert somewhere down the line due to the
contract logic (e.g. calling settle before closing).

With that in mind we can now detect those bad cases a bit earlier and handle
them without sending an onchain transaction. This is what this PR is trying to achieve.
LefterisJP added a commit to LefterisJP/raiden that referenced this pull request Nov 9, 2018
After the changes introduced by
raiden-network#2623 we again rely on gas
estimation for the gas limit to provide to an onchain call which is great since
it reduces our overall ETH balance requirements.

But we don't take into account the fact that if estimateGas returns None it's
because the transaction will fail, either due to our account not having enough
funds or because the call will just revert somewhere down the line due to the
contract logic (e.g. calling settle before closing).

With that in mind we can now detect those bad cases a bit earlier and handle
them without sending an onchain transaction. This is what this PR is trying to achieve.
LefterisJP added a commit to LefterisJP/raiden that referenced this pull request Nov 9, 2018
After the changes introduced by
raiden-network#2623 we again rely on gas
estimation for the gas limit to provide to an onchain call which is great since
it reduces our overall ETH balance requirements.

But we don't take into account the fact that if estimateGas returns None it's
because the transaction will fail, either due to our account not having enough
funds or because the call will just revert somewhere down the line due to the
contract logic (e.g. calling settle before closing).

With that in mind we can now detect those bad cases a bit earlier and handle
them without sending an onchain transaction. This is what this PR is trying to achieve.
Comment thread raiden/utils/__init__.py
def safe_gas_limit(*estimates) -> int:
""" Calculates a safe gas limit for a number of estimates.

Even though it's not documented, it does happen that estimate_gas returns `None`.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't make sense anymore once #2988 is merged, perhaps we should remove it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if only returns None os failure scenarios.

@hackaugusto hackaugusto deleted the gas_estimation branch November 9, 2018 18:49
LefterisJP added a commit to LefterisJP/raiden that referenced this pull request Jan 8, 2019
After the changes introduced by
raiden-network#2623 we again rely on gas
estimation for the gas limit to provide to an onchain call which is great since
it reduces our overall ETH balance requirements.

But we don't take into account the fact that if estimateGas returns None it's
because the transaction will fail, either due to our account not having enough
funds or because the call will just revert somewhere down the line due to the
contract logic (e.g. calling settle before closing).

With that in mind we can now detect those bad cases a bit earlier and handle
them without sending an onchain transaction. This is what this PR is trying to achieve.
LefterisJP added a commit to LefterisJP/raiden that referenced this pull request Jan 14, 2019
After the changes introduced by
raiden-network#2623 we again rely on gas
estimation for the gas limit to provide to an onchain call which is great since
it reduces our overall ETH balance requirements.

But we don't take into account the fact that if estimateGas returns None it's
because the transaction will fail, either due to our account not having enough
funds or because the call will just revert somewhere down the line due to the
contract logic (e.g. calling settle before closing).

With that in mind we can now detect those bad cases a bit earlier and handle
them without sending an onchain transaction. This is what this PR is trying to achieve.
LefterisJP added a commit to LefterisJP/raiden that referenced this pull request Jan 16, 2019
After the changes introduced by
raiden-network#2623 we again rely on gas
estimation for the gas limit to provide to an onchain call which is great since
it reduces our overall ETH balance requirements.

But we don't take into account the fact that if estimateGas returns None it's
because the transaction will fail, either due to our account not having enough
funds or because the call will just revert somewhere down the line due to the
contract logic (e.g. calling settle before closing).

With that in mind we can now detect those bad cases a bit earlier and handle
them without sending an onchain transaction. This is what this PR is trying to achieve.
LefterisJP added a commit to LefterisJP/raiden that referenced this pull request Jan 16, 2019
After the changes introduced by
raiden-network#2623 we again rely on gas
estimation for the gas limit to provide to an onchain call which is great since
it reduces our overall ETH balance requirements.

But we don't take into account the fact that if estimateGas returns None it's
because the transaction will fail, either due to our account not having enough
funds or because the call will just revert somewhere down the line due to the
contract logic (e.g. calling settle before closing).

With that in mind we can now detect those bad cases a bit earlier and handle
them without sending an onchain transaction. This is what this PR is trying to achieve.
LefterisJP added a commit to LefterisJP/raiden that referenced this pull request Jan 22, 2019
After the changes introduced by
raiden-network#2623 we again rely on gas
estimation for the gas limit to provide to an onchain call which is great since
it reduces our overall ETH balance requirements.

But we don't take into account the fact that if estimateGas returns None it's
because the transaction will fail, either due to our account not having enough
funds or because the call will just revert somewhere down the line due to the
contract logic (e.g. calling settle before closing).

With that in mind we can now detect those bad cases a bit earlier and handle
them without sending an onchain transaction. This is what this PR is trying to achieve.
LefterisJP added a commit to LefterisJP/raiden that referenced this pull request Jan 23, 2019
After the changes introduced by
raiden-network#2623 we again rely on gas
estimation for the gas limit to provide to an onchain call which is great since
it reduces our overall ETH balance requirements.

But we don't take into account the fact that if estimateGas returns None it's
because the transaction will fail, either due to our account not having enough
funds or because the call will just revert somewhere down the line due to the
contract logic (e.g. calling settle before closing).

With that in mind we can now detect those bad cases a bit earlier and handle
them without sending an onchain transaction. This is what this PR is trying to achieve.
hackaugusto pushed a commit that referenced this pull request Jan 23, 2019
After the changes introduced by
#2623 we again rely on gas
estimation for the gas limit to provide to an onchain call which is great since
it reduces our overall ETH balance requirements.

But we don't take into account the fact that if estimateGas returns None it's
because the transaction will fail, either due to our account not having enough
funds or because the call will just revert somewhere down the line due to the
contract logic (e.g. calling settle before closing).

With that in mind we can now detect those bad cases a bit earlier and handle
them without sending an onchain transaction. This is what this PR is trying to achieve.
hackaugusto pushed a commit to hackaugusto/raiden that referenced this pull request Jan 25, 2019
After the changes introduced by
raiden-network#2623 we again rely on gas
estimation for the gas limit to provide to an onchain call which is great since
it reduces our overall ETH balance requirements.

But we don't take into account the fact that if estimateGas returns None it's
because the transaction will fail, either due to our account not having enough
funds or because the call will just revert somewhere down the line due to the
contract logic (e.g. calling settle before closing).

With that in mind we can now detect those bad cases a bit earlier and handle
them without sending an onchain transaction. This is what this PR is trying to achieve.
hackaugusto pushed a commit to hackaugusto/raiden that referenced this pull request Jan 25, 2019
After the changes introduced by
raiden-network#2623 we again rely on gas
estimation for the gas limit to provide to an onchain call which is great since
it reduces our overall ETH balance requirements.

But we don't take into account the fact that if estimateGas returns None it's
because the transaction will fail, either due to our account not having enough
funds or because the call will just revert somewhere down the line due to the
contract logic (e.g. calling settle before closing).

With that in mind we can now detect those bad cases a bit earlier and handle
them without sending an onchain transaction. This is what this PR is trying to achieve.
hackaugusto pushed a commit to hackaugusto/raiden that referenced this pull request Jan 25, 2019
After the changes introduced by
raiden-network#2623 we again rely on gas
estimation for the gas limit to provide to an onchain call which is great since
it reduces our overall ETH balance requirements.

But we don't take into account the fact that if estimateGas returns None it's
because the transaction will fail, either due to our account not having enough
funds or because the call will just revert somewhere down the line due to the
contract logic (e.g. calling settle before closing).

With that in mind we can now detect those bad cases a bit earlier and handle
them without sending an onchain transaction. This is what this PR is trying to achieve.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Estimate transaction gas limit properly

5 participants