Skip to content
This repository was archived by the owner on Nov 10, 2023. It is now read-only.

(Feature) #1515 Gas estimation for NON-GETH nodes#1523

Merged
dasanra merged 12 commits intodevelopmentfrom
feature/1515-safe-tx-estimation
Oct 28, 2020
Merged

(Feature) #1515 Gas estimation for NON-GETH nodes#1523
dasanra merged 12 commits intodevelopmentfrom
feature/1515-safe-tx-estimation

Conversation

@Agupane
Copy link
Contributor

@Agupane Agupane commented Oct 24, 2020

Closes #1515, by:

Notes:

  • The main point for understanding the implementation is to know that GETH/Nethermind nodes will return the response of the call as a response and OpenEthereum nodes will always return the response as an error, to know if the error contains a valid value we need to check if the error data contains the string Reverted <resultHash>, in that case, we known that the transaction was successful and we can extract the hash result, otherwise the transaction has failed.
  • Please also check the comments on the code
  • To test this is better to use xDai because the rpc we use is OpenEthereum https://rpc.xdaichain.com/ / https://dai.poa.network/:
    Post request:
{
    "jsonrpc": "2.0",
    "method": "web3_clientVersion",
    "id": 1
}

Result:

{
    "jsonrpc": "2.0",
    "result": "OpenEthereum//v3.0.1-stable-8ca8089-20200601/x86_64-alpine-linux-musl/rustc1.43.1",
    "id": 1
}

@github-actions
Copy link

CLA Assistant Lite All Contributors have signed the CLA.

@github-actions
Copy link

github-actions bot commented Oct 24, 2020

ESLint Summary View Full Report

Annotations are provided inline on the Files Changed tab. You can also see all annotations that were generated on the annotations page.

Type Occurrences Fixable
Errors 0 0
Warnings 0 0
Ignored 1 N/A
  • Result: ✅ success
  • Annotations: 0 total

Report generated by eslint-plus-action

@ghost
Copy link

ghost commented Oct 24, 2020

Travis automatic deployment:
https://pr1523--safereact.review.gnosisdev.com/app

const web3 = getWeb3()
for (const additionalGasIterator of additionalGasBatches) {
try {
await web3.eth.call({
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did we remove the batch request support?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because for batch requests we need to use web3.eth.call.request() instead of web3.eth.call(). The first method does not returns the error as data that we need.

Responses examples:

Batch request:
Error: Returned error: Internal JSON-RPC error.

Non-batch request:

 Error: Internal JSON-RPC error.
{
  "code": -32015,
  "message": "VM execution error.",
  "data": "Reverted 0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000006457"
}

Copy link
Contributor

Choose a reason for hiding this comment

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

🤔 hmmm maybe in this case we should adjust it for all ... while we might have to do 1 or 2 more request the code overhead might not be worth it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

what do you think? @fernandomg

Copy link
Contributor

@fernandomg fernandomg Oct 26, 2020

Choose a reason for hiding this comment

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

@rmeissner, by that you mean to get rid of the batchRequest approach?

I'm perfectly fine with that.

It's easy to follow, in the worst-case scenario we'll do 10 RPC requests (one after the other, not triggered in parallel), and the user may experiment a delay (we should think of adding something around "calculating gas..." statement somewhere, but in a different issue).

Copy link
Contributor

Choose a reason for hiding this comment

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

yes ... but maybe @dasanra or @mikheevm have a better idea ... but since batched requests truncate the response it is not really possible to always use it.

Copy link
Contributor

@nicosampler nicosampler Oct 27, 2020

Choose a reason for hiding this comment

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

we'll do 10 RPC requests (one after the other, not triggered in parallel)

that's because we are using await, right? if we make use of promise.all() it should not be the case, isn't it?
@fernandomg

Copy link
Contributor

Choose a reason for hiding this comment

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

I think not doing them in parallel might be better here, as in most cases we will probably only need 1 or 2 requests, perform all 10 for this always would be quite some additional overhead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed the batch request and simplified the logic, now it doesn't matter which node it's running the code, the function getGasEstimationTxResponse will take care of it and return a result, then I replaced calculateGasForOpenEthereumNodes / calculateGasForGethAndNethermindNodes with the function calculateMinimumGasForTransaction. I think it's more clear now, please give it a check

const errorAsJSON = JSON.parse(error.join(''))

if (errorAsJSON?.data) {
const [, dataResult] = errorAsJSON.data.split('Reverted ')
Copy link
Contributor

Choose a reason for hiding this comment

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

This only works for OpenEthereum/Parity ... but not for Nethermind

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes you are right, I check this again and replaced non-geth comments/checks with OpenEthereum because it's the one failing. On Geth and Nethermind nodes, the initial implementation it's working (nethermind returns the result the same way as get, not as an error like openEthereum).

Copy link
Contributor

@rmeissner rmeissner left a comment

Choose a reason for hiding this comment

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

I would really like to see unit tests for the parsing of the error message here. (E.g. getNonGethErrorDataResult and if possible also extract the logic for the other case into a method too and test it)

@ghost
Copy link

ghost commented Oct 26, 2020

Travis automatic deployment:
https://pr1523--safereact.review.gnosisdev.com/app

@ghost
Copy link

ghost commented Oct 26, 2020

Travis automatic deployment:
https://pr1523--safereact.review.gnosisdev.com/app

const errorAsJSON = JSON.parse(error.join(''))

if (errorAsJSON?.data) {
const [, dataResult] = errorAsJSON.data.split('Reverted ')
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe instead if split by Reverted we split by (space), this way we also handle Nethermind (which uses revert <hex data>

Copy link
Contributor Author

@Agupane Agupane Oct 26, 2020

Choose a reason for hiding this comment

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

Do you have a nethermind endpoint rpc that I can use for testing the response?

Edit: https://rpc.xdaichain.com/ uses OpenEthereum not Nethermind:
image

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmmm seems they switched back .... annoying ... so we can only test this in our tests with the data from the issue ... should be enough for now :/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I refactored the file to support nethermind and added a test case for it :)

@rmeissner rmeissner dismissed their stale review October 26, 2020 18:47

Tests have been added

}
}

const getOpenEthereumErrorDataResult = (errorMessage: string): string | undefined => {
Copy link
Contributor

Choose a reason for hiding this comment

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

does it make sense to move this function to a more generic place?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure because it will only be used here, do you have any ideas?

@Agupane Agupane requested a review from fernandomg October 27, 2020 15:23
@ghost
Copy link

ghost commented Oct 27, 2020

Travis automatic deployment:
https://pr1523--safereact.review.gnosisdev.com/app

@ghost
Copy link

ghost commented Oct 27, 2020

Travis automatic deployment:
https://pr1523--safereact.review.gnosisdev.com/app

Copy link
Contributor

@fernandomg fernandomg left a comment

Choose a reason for hiding this comment

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

LGTM

@ghost
Copy link

ghost commented Oct 28, 2020

Travis automatic deployment:
https://pr1523--safereact.review.gnosisdev.com/rinkeby/app

@ghost
Copy link

ghost commented Oct 28, 2020

Travis automatic deployment:
https://pr1523--safereact.review.gnosisdev.com/rinkeby/app

@dasanra dasanra merged commit 551db13 into development Oct 28, 2020
@dasanra dasanra deleted the feature/1515-safe-tx-estimation branch October 28, 2020 16:03
@github-actions github-actions bot locked and limited conversation to collaborators Oct 28, 2020
@dasanra dasanra restored the feature/1515-safe-tx-estimation branch October 28, 2020 16:06
Copy link
Contributor

@mmv08 mmv08 left a comment

Choose a reason for hiding this comment

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

Good job! It was a pleasure to review that after reading a nicely-written description, keep it up 💪

@dasanra dasanra deleted the feature/1515-safe-tx-estimation branch October 29, 2020 08:28
@Agupane
Copy link
Contributor Author

Agupane commented Oct 29, 2020

Good job! It was a pleasure to review that after reading a nicely-written description, keep it up 💪

Thank you! 💪

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

safeTxGas estimation fails with non-Geth nodes

6 participants