Skip to content
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

feat: add gas estimation to the signer #3017

Merged
merged 17 commits into from
Mar 1, 2024
Merged

Conversation

cmwaters
Copy link
Contributor

The eventual goal is to have the signer be able to automatically estimate gas

Copy link
Contributor

coderabbitai bot commented Jan 16, 2024

Walkthrough

Walkthrough

The update enhances the transaction process by introducing a new gas estimation method and refining the transaction signing process with a new signature method. The Signer struct now includes EstimateGas for gas estimation, getSignatureV2 for improved signature handling, and an enhanced signTransaction method that accepts a sequence parameter. Testing now covers the new gas estimation functionality to ensure thorough validation of the changes.

Changes

Files Changes
pkg/user/signer.go - Added EstimateGas method
- Refactored signTransaction method to use getSignatureV2
- Introduced getSignatureV2 method
pkg/user/signer_test.go - Added TestGasEstimation()
- No changes to TestGasConsumption

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

Note: Auto-reply has been disabled for this repository by the repository owner. The CodeRabbit bot will not respond to your comments unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

rootulp
rootulp previously approved these changes Jan 16, 2024
@celestia-bot celestia-bot requested a review from a team January 17, 2024 14:11
evan-forbes
evan-forbes previously approved these changes Jan 17, 2024
@evan-forbes evan-forbes added the backport:v1.x PR will be backported automatically to the v1.x branch upon merging label Jan 17, 2024
@cmwaters
Copy link
Contributor Author

Blocked on celestiaorg/cosmos-sdk#371

ninabarbakadze
ninabarbakadze previously approved these changes Jan 18, 2024
@celestia-bot celestia-bot requested a review from a team February 9, 2024 16:34
staheri14
staheri14 previously approved these changes Feb 9, 2024
pkg/user/signer.go Show resolved Hide resolved
pkg/user/signer.go Show resolved Hide resolved
@celestia-bot celestia-bot requested a review from a team February 13, 2024 13:22
@celestia-bot celestia-bot requested a review from a team February 27, 2024 17:41
evan-forbes
evan-forbes previously approved these changes Feb 28, 2024
@cmwaters cmwaters added the backport:v1.x PR will be backported automatically to the v1.x branch upon merging label Feb 28, 2024
rootulp
rootulp previously approved these changes Feb 28, 2024
pkg/user/signer_test.go Outdated Show resolved Hide resolved
@cmwaters cmwaters dismissed stale reviews from rootulp and evan-forbes via 5f8193c February 29, 2024 15:04
@celestia-bot celestia-bot requested a review from a team February 29, 2024 15:04
@cmwaters cmwaters requested review from rootulp and removed request for a team February 29, 2024 15:04
Comment on lines +246 to +269
func (s *Signer) EstimateGas(ctx context.Context, msgs []sdktypes.Msg, opts ...TxOption) (uint64, error) {
txBuilder := s.txBuilder(opts...)
if err := txBuilder.SetMsgs(msgs...); err != nil {
return 0, err
}

if err := s.signTransaction(txBuilder, s.Sequence()); err != nil {
return 0, err
}

txBytes, err := s.enc.TxEncoder()(txBuilder.GetTx())
if err != nil {
return 0, err
}

resp, err := tx.NewServiceClient(s.grpc).Simulate(ctx, &tx.SimulateRequest{
TxBytes: txBytes,
})
if err != nil {
return 0, err
}

return resp.GasInfo.GasUsed, nil
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The EstimateGas method has been added to estimate gas for transactions. This method is crucial for automating gas estimation, aligning with the PR's objective. However, there are a few considerations:

  • Ensure that the Simulate call accurately reflects the gas estimation for the intended transactions. This might involve verifying that all necessary transaction parameters are set before simulation.
  • Consider handling potential errors more specifically than just returning them. For instance, if certain errors are expected under normal circumstances, it might be helpful to log them or handle them differently.
  • It's good practice to document the expected behavior and limitations of the EstimateGas method, especially since gas estimation can be complex and dependent on various factors.

Comment on lines +422 to +435

func (s *Signer) getSignatureV2(sequence uint64, signature []byte) signing.SignatureV2 {
sigV2 := signing.SignatureV2{
Data: &signing.SingleSignatureData{
SignMode: signing.SignMode_SIGN_MODE_DIRECT,
Signature: signature,
},
Sequence: sequence,
}
if sequence == 0 {
sigV2.PubKey = s.pk
}
return sigV2
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The getSignatureV2 method constructs a SignatureV2 struct, considering the sequence and signature parameters. Points to note:

  • The conditional setting of PubKey based on the sequence being 0 is a specific logic that might need clarification in comments. It's mentioned in the PR comments that setting the PubKey is no longer necessary due to a bug fix in the SDK. Ensure this logic is still required and correctly implemented.
  • Adding documentation for the method, explaining the significance of the sequence parameter and the conditions under which the PubKey is set, would improve code readability and maintainability.
+ // getSignatureV2 constructs a SignatureV2 struct using the provided sequence and signature.
+ // If the sequence is 0, the signer's public key is also included in the signature.

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
func (s *Signer) getSignatureV2(sequence uint64, signature []byte) signing.SignatureV2 {
sigV2 := signing.SignatureV2{
Data: &signing.SingleSignatureData{
SignMode: signing.SignMode_SIGN_MODE_DIRECT,
Signature: signature,
},
Sequence: sequence,
}
if sequence == 0 {
sigV2.PubKey = s.pk
}
return sigV2
}
// getSignatureV2 constructs a SignatureV2 struct using the provided sequence and signature.
// If the sequence is 0, the signer's public key is also included in the signature.
func (s *Signer) getSignatureV2(sequence uint64, signature []byte) signing.SignatureV2 {
sigV2 := signing.SignatureV2{
Data: &signing.SingleSignatureData{
SignMode: signing.SignMode_SIGN_MODE_DIRECT,
Signature: signature,
},
Sequence: sequence,
}
if sequence == 0 {
sigV2.PubKey = s.pk
}
return sigV2
}

rootulp
rootulp previously approved these changes Feb 29, 2024
Copy link
Collaborator

@rootulp rootulp left a comment

Choose a reason for hiding this comment

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

LGTM

pkg/user/signer_test.go Outdated Show resolved Hide resolved
Co-authored-by: Rootul P <rootulp@gmail.com>
@celestia-bot celestia-bot requested a review from a team February 29, 2024 17:33
@cmwaters cmwaters merged commit 2b13533 into main Mar 1, 2024
33 checks passed
@cmwaters cmwaters deleted the cal/signer-gas-estimation branch March 1, 2024 11:52
mergify bot pushed a commit that referenced this pull request Mar 1, 2024
(cherry picked from commit 2b13533)

# Conflicts:
#	pkg/user/signer.go
#	pkg/user/signer_test.go
rootulp pushed a commit that referenced this pull request Mar 5, 2024
This is an automatic backport of pull request #3017 done by
[Mergify](https://mergify.com).
Cherry-pick of 2b13533 has failed:
```
On branch mergify/bp/v1.x/pr-3017
Your branch is up to date with 'origin/v1.x'.

You are currently cherry-picking commit 2b13533.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:
	modified:   pkg/proof/proof_test.go

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   pkg/user/signer.go
	both modified:   pkg/user/signer_test.go

```


To fix up this pull request, you can check it out locally. See
documentation:
https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

---


<details>
<summary>Mergify commands and options</summary>

<br />

More conditions and actions can be found in the
[documentation](https://docs.mergify.com/).

You can also trigger Mergify actions by commenting on this pull request:

- `@Mergifyio refresh` will re-evaluate the rules
- `@Mergifyio rebase` will rebase this PR on its base branch
- `@Mergifyio update` will merge the base branch into this PR
- `@Mergifyio backport <destination>` will backport this PR on
`<destination>` branch

Additionally, on Mergify [dashboard](https://dashboard.mergify.com) you
can:

- look at your merge queues
- generate the Mergify configuration with the config editor.

Finally, you can contact us on https://mergify.com
</details>

---------

Co-authored-by: Callum Waters <cmwaters19@gmail.com>
ninabarbakadze pushed a commit to ninabarbakadze/celestia-app that referenced this pull request Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:v1.x PR will be backported automatically to the v1.x branch upon merging
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants