osmosis-labs / osmosis

The AMM Laboratory
https://app.osmosis.zone
Apache License 2.0
875 stars 562 forks source link

feat: pool swaps per block script #8451

Closed czarcas7ic closed 1 week ago

czarcas7ic commented 1 week ago

Closes: #XXX

What is the purpose of the change

The current flow of this script is as follows:

We first have a list of pool IDs that have OSMO as one of the assets in the pool. This is to simplify the logic of determining what the non-OSMO asset is, and not having to worry about seeding wallets with with every possible asset. The idea is, each block, we swap over one more pool, until we have swapped over all pools.

Ex. If we have 2 GAMM pools, 2 CL pools, and 1 CW pool: Block 1: Swap over GAMM pool 1 Block 2: Swap over GAMM pool 1, GAMM pool 2 Block 3: Swap over GAMM pool 1, GAMM pool 2, CL pool 1 Block 4: Swap over GAMM pool 1, GAMM pool 2, CL pool 1, CL pool 2 Block 5: Swap over GAMM pool 1, GAMM pool 2, CL pool 1, CL pool 2, CW pool 1

The script creates and seeds as many wallets as their are pools. On the first run, it will seed all wallets. On subsequent runs, if the wallets exist and have sufficient balance, they will not be seeded for speed. The reason for using many wallets instead of incrementing the sequence number on a single wallet is because the single wallet must be submitted in a single threaded manner, in the order of the sequence number. With 2 second blocks, as we get more and more pools, the likelihood of going over the 2 second block time increases. With multiple wallets, we can submit transactions concurrently since there are no sequence number conflicts.

Some notes:

Its recommended to run this script on a machine with a good amount of CPU cores, as the script will submit transactions concurrently for each pool. The more cores, the faster the script will run. With a 16 vCPU machine, and 65 pool entires, only the last 3 blocks had a 2 block delay.

You should use one of the lo-test1 - lo-test10 KEYRING_NAME keys since they are seeded with OSMO from in-place-testnet.

The script works with the given five CW pools, but adding other CW pools might require some extra logic to handle their different query responses.

Lastly, to run this script, you would do the following:

  1. Download a mainnet snapshot
  2. In the osmosis repo, run make localnet-keys
  3. Run the in-place-testnet command (at the time of this writing, use the trigger upgrade flag as "v26" since this script is meant for a sdk v50 chain)
  4. Hit the upgrade height and upgrade
  5. Make SURE you make install the v26 (or higher) osmosisd binary, as that is what will be used to run this script
  6. If you want to check on the txhashes that are output from the script, make sure to change your tx indexer in config.toml to "kv"
  7. Run this script

Some TODOs:

  1. Delete all unsigned tx files and singed tx files in the event of an error
  2. Figure out some better concurrency strategy for creating and signing the txs
  3. Store all the tx hashes, and once complete, iterate over them all and ensure they are all successful and give a summary of the results

Testing and Verifying

I have run this script on several in place testnets.

coderabbitai[bot] commented 1 week ago

Walkthrough

The pool_swaps_per_block_inc.py script for Osmosis network pool swaps has been enhanced to automate swapping across different pool types (GAMM, CL, CW) and manage concurrent transactions effectively. Key additions include wallet management functions, transaction generation, and broadcasting. Tweaks to constants and settings ensure efficient operation and concurrent execution.

Changes

File(s) Change Summary
scripts/pool_swaps_per_block_inc.py Introduced new functions for creating, seeding wallets, handling transactions, and retrieving statuses. Updated various constants and settings related to pool types and transaction configuration.

Sequence Diagram(s)

Pool Swap Flow

sequenceDiagram
    participant User
    participant Script

    User ->> Script: Start script execution
    Script ->> Blockchain: Retrieve status
    alt Wallet Exists
        Script ->> Blockchain: Get Account Info
    else Wallet Missing
        Script ->> Blockchain: Create Wallet
        Script ->> Blockchain: Seed Wallet
    end
    Script ->> Blockchain: Get Pool Info
    Script ->> Blockchain: Get Non-OSMO Pool Asset
    loop Over Blocks
        Script ->> Blockchain: Generate and Sign Tx
        Script ->> Blockchain: Broadcast Tx
        Script ->> Blockchain: Wait For Next Block
    end
    Script ->> User: Complete

Wallet Creation and Seeding

sequenceDiagram
    participant Script
    participant Blockchain

    Script ->> Blockchain: Check Wallet Existence
    alt Wallet Missing
        Script ->> Blockchain: Create Wallet
        Script ->> Blockchain: Seed Wallet
    end
    Script ->> Blockchain: Initialize Wallet Sequences

These diagrams illustrate the primary interactions and control flow for pool swapping and wallet management within the enhanced script.


Recent review details **Configuration used: CodeRabbit UI** **Review profile: CHILL**
Commits Files that changed from the base of the PR and between a80a5c04b98a7d8de9f76690b41f80bac78aeea4 and a5391486b01f16bb1c38266d60bb8d4d8065b643.
Files selected for processing (1) * scripts/pool_swaps_per_block_inc.py (1 hunks)
Additional context used
Ruff
scripts/pool_swaps_per_block_inc.py
327-327: f-string without any placeholders Remove extraneous `f` prefix (F541)
Additional comments not posted (3)
scripts/pool_swaps_per_block_inc.py (3)
`83-85`: **Ensure command execution safety.** The `run_command` function uses `subprocess.run`, which is potentially vulnerable to command injection if not properly sanitized. Ensure that commands passed to this function are either hardcoded, sanitized, or securely parameterized to prevent security vulnerabilities. [SECURITY] --- `88-105`: **Good use of concurrency in wallet management.** The `create_wallets` function effectively utilizes `ThreadPoolExecutor` to handle wallet creation and seeding concurrently. This is a good practice in Python for managing multiple I/O-bound tasks simultaneously, which can significantly improve the script's performance. --- `327-327`: **Remove extraneous f-string prefix.** The f-string on line 327 is used without placeholders. This is unnecessary and can be removed for clarity. ```diff - print(f"Generating and signing transactions prior to broadcasting...") + print("Generating and signing transactions prior to broadcasting...") ```
Tools
Ruff
327-327: f-string without any placeholders Remove extraneous `f` prefix (F541)
--- 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 - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)
Tips ### Chat There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai): - Review comments: Directly reply to a review comment made by CodeRabbit. Example: - `I pushed a fix in commit .` - `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.` - `@coderabbitai help me debug CodeRabbit configuration file.` 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 an incremental review. This is useful when automatic reviews are disabled for the repository. - `@coderabbitai full review` to do a full review from scratch and review all the files again. - `@coderabbitai summary` to regenerate the summary of the PR. - `@coderabbitai resolve` resolve all the CodeRabbit review comments. - `@coderabbitai configuration` to show the current CodeRabbit configuration for the repository. - `@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](https://docs.coderabbit.ai/guides/configure-coderabbit) 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/schema.v2.json` ### Documentation and Community - Visit our [Documentation](https://coderabbit.ai/docs) for detailed information on how to use CodeRabbit. - Join our [Discord Community](https://discord.com/invite/GsXnASn26c) to get help, request features, and share feedback. - Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.
czarcas7ic commented 1 week ago

Thanks for approval, I think I am going to migrate this to go though, will respond here soon with decision.

czarcas7ic commented 1 week ago

Closing in favor of a way better golang side solution!