Skip to content

feat: Verify transfer addresses in Observer#7943

Merged
czarcas7ic merged 2 commits intomainfrom
kormick/verify-transfer-addresses
Apr 5, 2024
Merged

feat: Verify transfer addresses in Observer#7943
czarcas7ic merged 2 commits intomainfrom
kormick/verify-transfer-addresses

Conversation

@Kormick
Copy link
Copy Markdown
Collaborator

@Kormick Kormick commented Apr 3, 2024

Epic: #7643

What is the purpose of the change

  • Add destination address verification for x/bridge observer

Testing and Verifying

  • Unit tests

Documentation and Release Note

  • Does this pull request introduce a new feature or user-facing behavior changes?
  • Changelog entry added to Unreleased section of CHANGELOG.md?

Where is the change documented?

  • Specification (x/{module}/README.md)
  • Osmosis documentation site
  • Code comments?
  • N/A

Summary by CodeRabbit

  • New Features

    • Enhanced Bitcoin observer functionality for improved error messaging and transaction relevance logic.
    • Introduced new features to Osmosis observer including mode settings and better error handling.
  • Bug Fixes

    • Fixed Bitcoin address validation in memo retrieval.
    • Improved test scenarios for both Bitcoin and Osmosis observers to cover various transaction types and scenarios.
  • Refactor

    • Streamlined Bitcoin and Osmosis observers' code for better maintainability and performance.

@Kormick Kormick added V:state/compatible/no_backport State machine compatible PR, depends on prior breaks A:no-changelog A:backport/v24.x backport patches to v24.x branch labels Apr 3, 2024
@Kormick Kormick self-assigned this Apr 3, 2024
@Kormick Kormick marked this pull request as ready for review April 3, 2024 11:31
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 3, 2024

Walkthrough

This update focuses on enhancing the functionality and reliability of the Bitcoin and Osmosis observers within a blockchain bridge system. For Bitcoin, it involves refining transaction relevance checks and memo retrieval, including address validation. For Osmosis, enhancements include the introduction of operating modes, improved error handling, and outbound address verification. Both sets of changes aim to improve the system's robustness and its ability to handle transactions more effectively.

Changes

Files Change Summary
x/bridge/observer/bitcoin/... - Removed slices import
- Updated error messages and logic
- Added address validation
x/bridge/observer/osmosis/... - Added imports and Mode type
- Enhanced error handling
- Introduced address verification

🐇✨
In the realm of code, where bridges span wide,
Observers watch, with keen eyes on the tide.
Bitcoin and Osmosis, under the moon's glow,
Enhanced by a rabbit, with magic in tow.
Through digital forests, over streams of data,
Our bridge stands stronger, now and hereafter.
🌉🚀

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:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • 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 testing code 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 and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @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.
  • Please see the configuration documentation for more information.
  • 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

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Comment on lines +253 to +266
func verifyOutboundDestAddress(mode Mode, chainId observer.ChainId, addr string) error {
switch chainId {
case observer.ChainIdBitcoin:
switch mode {
case ModeMainnet:
_, err := btcutil.DecodeAddress(addr, &btcdchaincfg.MainNetParams)
return err
case ModeTestnet:
_, err := btcutil.DecodeAddress(addr, &btcdchaincfg.TestNet3Params)
return err
}
}
return fmt.Errorf("Unsupported outbound destination chain: %s", chainId)
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Add a default case in the switch statement for chainId to handle unsupported chains more gracefully in the verifyOutboundDestAddress function.

 switch chainId {
 case observer.ChainIdBitcoin:
   // existing code
+default:
+  return fmt.Errorf("verifyOutboundDestAddress: unsupported chain ID %s", chainId)
 }

This change improves error handling by providing a clear error message for unsupported chains.


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 verifyOutboundDestAddress(mode Mode, chainId observer.ChainId, addr string) error {
switch chainId {
case observer.ChainIdBitcoin:
switch mode {
case ModeMainnet:
_, err := btcutil.DecodeAddress(addr, &btcdchaincfg.MainNetParams)
return err
case ModeTestnet:
_, err := btcutil.DecodeAddress(addr, &btcdchaincfg.TestNet3Params)
return err
}
}
return fmt.Errorf("Unsupported outbound destination chain: %s", chainId)
}
func verifyOutboundDestAddress(mode Mode, chainId observer.ChainId, addr string) error {
switch chainId {
case observer.ChainIdBitcoin:
switch mode {
case ModeMainnet:
_, err := btcutil.DecodeAddress(addr, &btcdchaincfg.MainNetParams)
return err
case ModeTestnet:
_, err := btcutil.DecodeAddress(addr, &btcdchaincfg.TestNet3Params)
return err
}
default:
return fmt.Errorf("verifyOutboundDestAddress: unsupported chain ID %s", chainId)
}
return fmt.Errorf("Unsupported outbound destination chain: %s", chainId)
}

memo, contains := getMemo(tx)
if !contains {
return observer.Transfer{}, false, fmt.Errorf("failed to get Tx memo")
return observer.Transfer{}, false, fmt.Errorf("malformed Tx memo")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ensure proper logging for the error case in the getMemo function when a transaction memo is malformed.
Adding a log statement here can aid in debugging and operational monitoring.

 if !contains {
+  b.logger.Error("Malformed transaction memo", "txid", tx.Txid)
   return observer.Transfer{}, false, fmt.Errorf("malformed Tx memo")
 }

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
return observer.Transfer{}, false, fmt.Errorf("malformed Tx memo")
b.logger.Error("Malformed transaction memo", "txid", tx.Txid)
return observer.Transfer{}, false, fmt.Errorf("malformed Tx memo")

Copy link
Copy Markdown
Member

@czarcas7ic czarcas7ic left a comment

Choose a reason for hiding this comment

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

Nice!

Comment on lines +108 to +117
// We expect Observer to observe 1 block with 8 Txs:
// - tx to our vault but without memo
// - tx to our vault with memo with invalid address
// - tx to our vault but with zero tokens
// - tx to our vault with invalid script type for output Vout
// - tx to our vault with invalid script type for memo Vout
// - tx to our vault with invalid tokens amount
// - unrelated tx
// + valid tx to our vault
// So, we should receive only 1 Transfer
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice

@czarcas7ic czarcas7ic merged commit 1b3449b into main Apr 5, 2024
@czarcas7ic czarcas7ic deleted the kormick/verify-transfer-addresses branch April 5, 2024 04:54
mergify bot pushed a commit that referenced this pull request Apr 5, 2024
* feat: Verify transfer addresses

* chore: typo

(cherry picked from commit 1b3449b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A:backport/v24.x backport patches to v24.x branch A:no-changelog V:state/compatible/no_backport State machine compatible PR, depends on prior breaks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants