Skip to content

refactor(zetaclient): wait for exact PDA nonce before signing Solana outbound#3940

Merged
ws4charlie merged 11 commits intodevelopfrom
solana-pda-nonce-preflight
Jun 6, 2025
Merged

refactor(zetaclient): wait for exact PDA nonce before signing Solana outbound#3940
ws4charlie merged 11 commits intodevelopfrom
solana-pda-nonce-preflight

Conversation

@ws4charlie
Copy link
Contributor

@ws4charlie ws4charlie commented May 30, 2025

Description

Replaces the original PRs:
#3633
#3708 simplified with same performance and no extra operational overhead.

The key points:

  1. zetaclient will NOT pre-sign Solana outbound and avoid starting the two minute recentBlockhash timer too early (ref).
  2. zetaclient will use consistent commitment level CommitmentConfirmed to handle outbound broadcasting to avoid state mismatches.
  3. The outbound broadcasting go-routines will be strictly ordered by pda_nonce to eliminate nonce mismatches.

Performance Test Comparison

We see 13% reduction in total runtime and 25% reductions in average latency.

Before:
image

After:
image

With more performance tuning:
image

How Has This Been Tested?

  • Tested CCTX in localnet
  • Tested in development environment
  • Go unit tests
  • Go integration tests
  • Tested via GitHub Actions

Summary by CodeRabbit

  • Refactor
    • Outbound Solana transactions now wait for the exact PDA nonce before processing, improving synchronization and reliability.
    • Commitment levels for transaction broadcasting and blockhash fetching have been updated for better consistency.
  • Documentation
    • Changelog updated to correct a previous entry and add a new refactor note.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented May 30, 2025

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

📝 Walkthrough

Walkthrough

The changes introduce a mechanism to wait for the exact PDA nonce before processing Solana outbound transactions, update commitment levels for transaction handling, and refine retry logic. The SolanaRPCClient interface and its mock gain a new method to fetch account info with options. The changelog is updated for accuracy and completeness.

Changes

File(s) Change Summary
changelog.md Corrected PR number formatting and added a refactor entry for Solana outbound transaction nonce handling (PR #3940).
zetaclient/chains/interfaces/interfaces.go Added GetAccountInfoWithOpts method to SolanaRPCClient interface.
zetaclient/chains/solana/signer/signer.go Added nonce wait logic before signing/broadcasting; introduced constants for timeout and commitment; refined retry logic.
zetaclient/testutils/mocks/solana_rpc.go Added mock implementation for GetAccountInfoWithOpts; updated mockery version comment.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Signer
    participant SolanaRPC
    participant Zetacore

    Client->>Signer: TryProcessOutbound(ctx, cctx, zetacoreClient, height)
    Note right of Signer: Create context with pdaNonceWaitTimeout
    Signer->>SolanaRPC: waitExactGatewayNonce(ctx, nonce)
    loop Until PDA nonce == target nonce or timeout
        SolanaRPC->>SolanaRPC: GetAccountInfoWithOpts(ctx, account, opts)
        SolanaRPC-->>Signer: AccountInfoResult (with current nonce)
        alt PDA nonce < target
            Note right of Signer: Sleep and retry
        else PDA nonce == target
            Note right of Signer: Proceed
        else PDA nonce > target or error/timeout
            Note right of Signer: Abort processing
        end
    end
    alt PDA nonce == target
        Signer->>Signer: signTx(ctx, inst, limit)
        Signer->>Signer: broadcastOutbound(ctx, outbound, chainID, nonce, logger, zetacoreClient)
        Signer-->>Client: Outbound processed
    else
        Signer-->>Client: Abort processing
    end
Loading

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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>, please review it.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 using 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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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.

@github-actions
Copy link

!!!WARNING!!!
nosec detected in the following files: zetaclient/chains/solana/signer/signer.go

Be very careful about using #nosec in code. It can be a quick way to suppress security warnings and move forward with development, it should be employed with caution. Suppressing warnings with #nosec can hide potentially serious vulnerabilities. Only use #nosec when you're absolutely certain that the security issue is either a false positive or has been mitigated in another way.

Only suppress a single rule (or a specific set of rules) within a section of code, while continuing to scan for other problems. To do this, you can list the rule(s) to be suppressed within the #nosec annotation, e.g: /* #nosec G401 */ or //#nosec G201 G202 G203
Broad #nosec annotations should be avoided, as they can hide other vulnerabilities. The CI will block you from merging this PR until you remove #nosec annotations that do not target specific rules.

Pay extra attention to the way #nosec is being used in the files listed above.

@github-actions github-actions bot added the nosec label May 30, 2025
@ws4charlie ws4charlie changed the title feat: wait for exact PDA nonce before signing Solana outbound refactor (zetaclient): wait for exact PDA nonce before signing Solana outbound May 30, 2025
@ws4charlie ws4charlie changed the title refactor (zetaclient): wait for exact PDA nonce before signing Solana outbound refactor(zetaclient): wait for exact PDA nonce before signing Solana outbound May 30, 2025
@codecov
Copy link

codecov bot commented May 30, 2025

Codecov Report

Attention: Patch coverage is 0% with 75 lines in your changes missing coverage. Please review.

Project coverage is 64.08%. Comparing base (d2c403a) to head (78aa32b).
Report is 1 commits behind head on develop.

Files with missing lines Patch % Lines
zetaclient/chains/solana/signer/signer.go 0.00% 75 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #3940      +/-   ##
===========================================
- Coverage    64.18%   64.08%   -0.10%     
===========================================
  Files          474      474              
  Lines        34803    34854      +51     
===========================================
  Hits         22337    22337              
- Misses       11438    11489      +51     
  Partials      1028     1028              
Files with missing lines Coverage Δ
zetaclient/chains/solana/signer/signer.go 16.20% <0.00%> (-3.00%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ws4charlie ws4charlie added chain:solana SOLANA_TESTS Run make start-solana-test PERFORMANCE_TESTS Run make start-e2e-performance-test and removed nosec labels May 30, 2025
@ws4charlie ws4charlie marked this pull request as ready for review May 30, 2025 21:47
@ws4charlie ws4charlie requested a review from a team as a code owner May 30, 2025 21:47
@github-actions github-actions bot added the nosec label May 30, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🧹 Nitpick comments (2)
zetaclient/chains/solana/signer/signer.go (2)

321-321: Consider adding a comment for clarity.

While the range-based loop is cleaner, it might be less immediately clear that this loops exactly broadcastRetries times.

Consider adding a comment:

 // try broacasting tx with increasing backoff (1s, 2s, 4s, 8s, 16s, 32s, 64s)
 // to tolerate tx nonce mismatch with PDA nonce or unknown RPC error
 backOff := broadcastBackoff
+// retry up to broadcastRetries times
 for range broadcastRetries {
🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 321-321: zetaclient/chains/solana/signer/signer.go#L321
Added line #L321 was not covered by tests


500-505: Simplify timeout handling.

The manual deadline check is redundant since ctx.Err() already returns context.DeadlineExceeded when the deadline is exceeded.

Simplify the timeout handling:

 for {
     if ctx.Err() != nil {
+        logger.Error().Err(ctx.Err()).Msgf("context cancelled while waiting for gateway nonce")
         return false
     }

-    // check timeout to avoid infinite waiting
-    if deadline, ok := ctx.Deadline(); ok {
-        if time.Now().After(deadline) {
-            logger.Error().Msgf("timeout reached on waiting for gateway nonce")
-            return false
-        }
-    }
🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 500-504: zetaclient/chains/solana/signer/signer.go#L500-L504
Added lines #L500 - L504 were not covered by tests

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c73ff7e and 836926a.

📒 Files selected for processing (4)
  • changelog.md (1 hunks)
  • zetaclient/chains/interfaces/interfaces.go (1 hunks)
  • zetaclient/chains/solana/signer/signer.go (6 hunks)
  • zetaclient/testutils/mocks/solana_rpc.go (2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.go`: Review the Go code, point out issues relative to principles of clean code, expressiveness, and performance.

**/*.go: Review the Go code, point out issues relative to principles of clean code, expressiveness, and performance.

  • zetaclient/chains/interfaces/interfaces.go
  • zetaclient/testutils/mocks/solana_rpc.go
  • zetaclient/chains/solana/signer/signer.go
🧠 Learnings (1)
zetaclient/chains/solana/signer/signer.go (1)
Learnt from: gartnera
PR: zeta-chain/node#3632
File: zetaclient/chains/solana/signer/signer.go:304-304
Timestamp: 2025-03-04T22:39:58.395Z
Learning: The Solana signer implementation in zetaclient/chains/solana/signer/signer.go has limited test coverage, particularly for the transaction broadcasting logic with fallback scenarios. Adding this coverage has been acknowledged as a potential future improvement outside the scope of immediate fixes.
🧬 Code Graph Analysis (1)
zetaclient/testutils/mocks/solana_rpc.go (1)
zetaclient/chains/interfaces/interfaces.go (1)
  • SolanaRPCClient (141-182)
🪛 GitHub Check: codecov/patch
zetaclient/chains/solana/signer/signer.go

[warning] 232-238: zetaclient/chains/solana/signer/signer.go#L232-L238
Added lines #L232 - L238 were not covered by tests


[warning] 259-259: zetaclient/chains/solana/signer/signer.go#L259
Added line #L259 was not covered by tests


[warning] 321-321: zetaclient/chains/solana/signer/signer.go#L321
Added line #L321 was not covered by tests


[warning] 342-342: zetaclient/chains/solana/signer/signer.go#L342
Added line #L342 was not covered by tests


[warning] 487-497: zetaclient/chains/solana/signer/signer.go#L487-L497
Added lines #L487 - L497 were not covered by tests


[warning] 500-504: zetaclient/chains/solana/signer/signer.go#L500-L504
Added lines #L500 - L504 were not covered by tests


[warning] 508-515: zetaclient/chains/solana/signer/signer.go#L508-L515
Added lines #L508 - L515 were not covered by tests


[warning] 519-523: zetaclient/chains/solana/signer/signer.go#L519-L523
Added lines #L519 - L523 were not covered by tests


[warning] 525-539: zetaclient/chains/solana/signer/signer.go#L525-L539
Added lines #L525 - L539 were not covered by tests

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: start-solana-test / e2e
🔇 Additional comments (5)
zetaclient/chains/interfaces/interfaces.go (1)

147-151: LGTM!

The new GetAccountInfoWithOpts method is a clean addition to the interface that follows the established pattern and provides necessary flexibility for querying account information with specific options.

changelog.md (1)

39-40: LGTM!

The changelog updates are accurate - the typo fix and the new entry for PR #3940 properly document the changes.

zetaclient/testutils/mocks/solana_rpc.go (1)

1-1: LGTM!

The auto-generated mock implementation for GetAccountInfoWithOpts correctly follows the mockery pattern and supports the new interface method.

Also applies to: 50-78

zetaclient/chains/solana/signer/signer.go (2)

36-46: Well-defined constants with clear documentation.

The timeout and commitment level choices are well-reasoned and properly documented. The 1-minute timeout provides adequate coverage for parallel CCTX processing, and the "confirmed" commitment level strikes a good balance between safety and transaction expiration risk.


259-259: Good use of the defined constant.

Using broadcastOutboundCommitment instead of a hardcoded value improves consistency and maintainability.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 259-259: zetaclient/chains/solana/signer/signer.go#L259
Added line #L259 was not covered by tests

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
zetaclient/chains/solana/signer/signer.go (1)

525-542: Consider adding a maximum sleep duration cap.

While the proportional sleep based on nonce difference is clever, consider adding an explicit cap to prevent excessive sleep times:

 			// #nosec G115 always in range
 			sleepDuration := time.Second * time.Duration(nonce-pda.Nonce)
+			// Cap the sleep duration to prevent excessive waiting
+			const maxSleepDuration = 10 * time.Second
+			if sleepDuration > maxSleepDuration {
+				sleepDuration = maxSleepDuration
+			}
 			time.Sleep(sleepDuration)

Also, this method needs comprehensive test coverage to verify all branches and edge cases.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 525-539: zetaclient/chains/solana/signer/signer.go#L525-L539
Added lines #L525 - L539 were not covered by tests

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c73ff7e and 836926a.

📒 Files selected for processing (4)
  • changelog.md (1 hunks)
  • zetaclient/chains/interfaces/interfaces.go (1 hunks)
  • zetaclient/chains/solana/signer/signer.go (6 hunks)
  • zetaclient/testutils/mocks/solana_rpc.go (2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.go`: Review the Go code, point out issues relative to principles of clean code, expressiveness, and performance.

**/*.go: Review the Go code, point out issues relative to principles of clean code, expressiveness, and performance.

  • zetaclient/chains/interfaces/interfaces.go
  • zetaclient/testutils/mocks/solana_rpc.go
  • zetaclient/chains/solana/signer/signer.go
🧬 Code Graph Analysis (1)
zetaclient/testutils/mocks/solana_rpc.go (1)
zetaclient/chains/interfaces/interfaces.go (1)
  • SolanaRPCClient (141-182)
🪛 GitHub Check: codecov/patch
zetaclient/chains/solana/signer/signer.go

[warning] 232-238: zetaclient/chains/solana/signer/signer.go#L232-L238
Added lines #L232 - L238 were not covered by tests


[warning] 259-259: zetaclient/chains/solana/signer/signer.go#L259
Added line #L259 was not covered by tests


[warning] 321-321: zetaclient/chains/solana/signer/signer.go#L321
Added line #L321 was not covered by tests


[warning] 342-342: zetaclient/chains/solana/signer/signer.go#L342
Added line #L342 was not covered by tests


[warning] 487-497: zetaclient/chains/solana/signer/signer.go#L487-L497
Added lines #L487 - L497 were not covered by tests


[warning] 500-504: zetaclient/chains/solana/signer/signer.go#L500-L504
Added lines #L500 - L504 were not covered by tests


[warning] 508-515: zetaclient/chains/solana/signer/signer.go#L508-L515
Added lines #L508 - L515 were not covered by tests


[warning] 519-523: zetaclient/chains/solana/signer/signer.go#L519-L523
Added lines #L519 - L523 were not covered by tests


[warning] 525-539: zetaclient/chains/solana/signer/signer.go#L525-L539
Added lines #L525 - L539 were not covered by tests

🔇 Additional comments (7)
zetaclient/chains/interfaces/interfaces.go (1)

147-151: LGTM!

The addition of GetAccountInfoWithOpts to the interface is clean and follows the established pattern. This method appropriately extends the functionality to support commitment level options.

changelog.md (1)

39-40: LGTM!

The changelog updates are accurate - fixed the typo in PR #3848 and added an appropriate entry for the current PR #3940.

zetaclient/testutils/mocks/solana_rpc.go (1)

1-1: LGTM!

The mock implementation correctly reflects the interface changes. The method follows the standard mockery pattern and the version update is routine maintenance.

Also applies to: 50-78

zetaclient/chains/solana/signer/signer.go (4)

36-46: Well-documented constants with appropriate values.

The 1-minute timeout is reasonable for the expected finality time, and the commitment level choice is well-justified with clear documentation explaining the tradeoffs.


259-259: Good refactoring to use constant.

Replacing the hardcoded commitment level with the constant improves maintainability and consistency.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 259-259: zetaclient/chains/solana/signer/signer.go#L259
Added line #L259 was not covered by tests


321-321: Nice simplification using idiomatic Go.

The range loop is cleaner when the index isn't needed.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 321-321: zetaclient/chains/solana/signer/signer.go#L321
Added line #L321 was not covered by tests


342-342: Consistent commitment level usage.

Good to maintain consistency by using the same commitment level constant throughout the broadcast process.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 342-342: zetaclient/chains/solana/signer/signer.go#L342
Added line #L342 was not covered by tests

Copy link
Contributor

@lumtis lumtis left a comment

Choose a reason for hiding this comment

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

LGTM

@ws4charlie ws4charlie requested a review from skosito June 4, 2025 22:29
@ws4charlie ws4charlie enabled auto-merge June 5, 2025 19:02
@ws4charlie ws4charlie added this pull request to the merge queue Jun 5, 2025
Merged via the queue into develop with commit a6245a0 Jun 6, 2025
46 of 47 checks passed
@ws4charlie ws4charlie deleted the solana-pda-nonce-preflight branch June 6, 2025 00:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

chain:solana nosec PERFORMANCE_TESTS Run make start-e2e-performance-test SOLANA_TESTS Run make start-solana-test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants