Skip to content

feat(ton): Implement HTTP-RPC based on toncenter v2#3929

Merged
swift1337 merged 8 commits intodevelopfrom
feat/ton-rpc
Jun 3, 2025
Merged

feat(ton): Implement HTTP-RPC based on toncenter v2#3929
swift1337 merged 8 commits intodevelopfrom
feat/ton-rpc

Conversation

@swift1337
Copy link
Contributor

@swift1337 swift1337 commented May 28, 2025

  • GetMasterchainInfo
  • GetBlockHeader
  • HealthCheck
  • GetAccountState
  • GetConfigParam
  • GetTransactions
  • GetTransaction
  • GetTransactionsSince
  • GetFirstTransaction
  • SendMessage
  • Add prometheus metrics

Closes #3927

Note: This PR implements only the required subset of RPC methods necessary for zetaclient's needs.

Integration of this client will be done in #3928

Summary by CodeRabbit

  • New Features

    • Added support for TON HTTP-RPC, enabling integration with the TON blockchain via the toncenter API.
    • Introduced environment variable to enable RPC functionality in local TON network deployments.
  • Tests

    • Added live integration tests for the TON RPC client, covering connectivity, account state, configuration parameters, and transaction retrieval.
  • Documentation

    • Updated changelog with a new entry for TON HTTP-RPC support.

@swift1337 swift1337 self-assigned this May 28, 2025
@swift1337 swift1337 requested a review from a team as a code owner May 28, 2025 17:13
@swift1337 swift1337 added the TON_TESTS Runs TON E2E tests label May 28, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented May 28, 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

A new HTTP-RPC client for the TON blockchain was introduced, including comprehensive types, client logic, and live integration tests. Environment variable support and docker-compose configuration were updated to facilitate the new functionality. Documentation was amended to reflect the addition.

Changes

File(s) Change Summary
zetaclient/chains/ton/rpc/client.go, zetaclient/chains/ton/rpc/types.go Implemented a full-featured TON HTTP-RPC client, associated types, error handling, and utility methods.
zetaclient/chains/ton/rpc/client_live_test.go Added live integration tests for the TON RPC client, covering connectivity, account, config, and txns.
zetaclient/common/env.go Added EnvTONRPC constant for environment variable configuration.
contrib/localnet/docker-compose.yml Added ENABLE_RPC: true to the ton service environment variables.
changelog.md Documented the new TON http-rpc feature under unreleased changes.

Sequence Diagram(s)

sequenceDiagram
    participant ClientApp
    participant TONRPCClient
    participant TONCenterAPI

    ClientApp->>TONRPCClient: New(endpoint, opts)
    ClientApp->>TONRPCClient: GetMasterchainInfo()
    TONRPCClient->>TONCenterAPI: POST /jsonRPC getMasterchainInfo
    TONCenterAPI-->>TONRPCClient: JSON response
    TONRPCClient-->>ClientApp: MasterchainInfo

    ClientApp->>TONRPCClient: GetAccountState(account)
    TONRPCClient->>TONCenterAPI: POST /jsonRPC getAccountState
    TONCenterAPI-->>TONRPCClient: JSON response
    TONRPCClient-->>ClientApp: Account

    ClientApp->>TONRPCClient: SendMessage(payload)
    TONRPCClient->>TONCenterAPI: POST /jsonRPC sendMessage
    TONCenterAPI-->>TONRPCClient: JSON response
    TONRPCClient-->>ClientApp: Result
Loading

Assessment against linked issues

Objective Addressed Explanation
Implement HTTP-RPC client for TON blockchain (#3927)
Provide methods for blockchain data and transaction actions (#3927)
Include error handling and metrics instrumentation (#3927)
Supply integration tests for the client (#3927)

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/ton/rpc/client.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 28, 2025
@codecov
Copy link

codecov bot commented May 28, 2025

Codecov Report

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

Project coverage is 64.21%. Comparing base (a7b7086) to head (52f2059).
Report is 1 commits behind head on develop.

Files with missing lines Patch % Lines
zetaclient/chains/ton/rpc/client.go 0.00% 282 Missing ⚠️
zetaclient/chains/ton/rpc/types.go 0.00% 72 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #3929      +/-   ##
===========================================
- Coverage    64.87%   64.21%   -0.66%     
===========================================
  Files          472      474       +2     
  Lines        34442    34796     +354     
===========================================
  Hits         22343    22343              
- Misses       11071    11425     +354     
  Partials      1028     1028              
Files with missing lines Coverage Δ
zetaclient/common/env.go 88.88% <ø> (ø)
zetaclient/chains/ton/rpc/types.go 0.00% <0.00%> (ø)
zetaclient/chains/ton/rpc/client.go 0.00% <0.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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 (4)
zetaclient/chains/ton/rpc/types.go (1)

116-127: Consider using a more flexible request ID generation strategy.

The hardcoded ID "1" in newRPCRequest could cause issues with concurrent requests or request tracking.

Consider generating unique request IDs:

+import (
+	"strconv"
+	"sync/atomic"
+)
+
+var requestCounter int64
+
 func newRPCRequest(method string, params map[string]any) rpcRequest {
 	if params == nil {
 		params = make(map[string]any)
 	}

+	id := atomic.AddInt64(&requestCounter, 1)
 	return rpcRequest{
 		Jsonrpc: "2.0",
-		ID:      "1",
+		ID:      strconv.FormatInt(id, 10),
 		Method:  method,
 		Params:  params,
 	}
 }
zetaclient/chains/ton/rpc/client.go (3)

78-92: Consider implementing caching for block headers.

The TODO comment correctly identifies an optimization opportunity. Block headers are immutable once finalized, making them ideal candidates for caching.

Would you like me to implement a simple LRU cache for block headers to improve performance?

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 78-91: zetaclient/chains/ton/rpc/client.go#L78-L91
Added lines #L78 - L91 were not covered by tests


322-322: Fix typo in comment.

-		// Not we take the latest item in the list (oldest tx in the page)
+		// Note we take the latest item in the list (oldest tx in the page)

346-349: Verify response code parsing in SendMessage.

The TODO indicates uncertainty about response parsing. The current implementation may not handle all response scenarios correctly.

Please verify the response handling during e2e testing and ensure all status codes are properly parsed and returned.

Would you like me to research the toncenter API documentation to determine the correct response parsing approach?

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c796d35 and be43af3.

📒 Files selected for processing (6)
  • changelog.md (1 hunks)
  • contrib/localnet/docker-compose.yml (1 hunks)
  • zetaclient/chains/ton/rpc/client.go (1 hunks)
  • zetaclient/chains/ton/rpc/client_live_test.go (1 hunks)
  • zetaclient/chains/ton/rpc/types.go (1 hunks)
  • zetaclient/common/env.go (1 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/common/env.go
  • zetaclient/chains/ton/rpc/client_live_test.go
  • zetaclient/chains/ton/rpc/types.go
  • zetaclient/chains/ton/rpc/client.go
🪛 GitHub Check: codecov/patch
zetaclient/chains/ton/rpc/client.go

[warning] 37-38: zetaclient/chains/ton/rpc/client.go#L37-L38
Added lines #L37 - L38 were not covered by tests


[warning] 45-65: zetaclient/chains/ton/rpc/client.go#L45-L65
Added lines #L45 - L65 were not covered by tests


[warning] 67-67: zetaclient/chains/ton/rpc/client.go#L67
Added line #L67 was not covered by tests


[warning] 70-75: zetaclient/chains/ton/rpc/client.go#L70-L75
Added lines #L70 - L75 were not covered by tests


[warning] 78-91: zetaclient/chains/ton/rpc/client.go#L78-L91
Added lines #L78 - L91 were not covered by tests


[warning] 94-98: zetaclient/chains/ton/rpc/client.go#L94-L98
Added lines #L94 - L98 were not covered by tests


[warning] 100-103: zetaclient/chains/ton/rpc/client.go#L100-L103
Added lines #L100 - L103 were not covered by tests


[warning] 105-107: zetaclient/chains/ton/rpc/client.go#L105-L107
Added lines #L105 - L107 were not covered by tests


[warning] 110-119: zetaclient/chains/ton/rpc/client.go#L110-L119
Added lines #L110 - L119 were not covered by tests


[warning] 123-127: zetaclient/chains/ton/rpc/client.go#L123-L127
Added lines #L123 - L127 were not covered by tests


[warning] 129-131: zetaclient/chains/ton/rpc/client.go#L129-L131
Added lines #L129 - L131 were not covered by tests


[warning] 133-133: zetaclient/chains/ton/rpc/client.go#L133
Added line #L133 was not covered by tests


[warning] 136-144: zetaclient/chains/ton/rpc/client.go#L136-L144
Added lines #L136 - L144 were not covered by tests


[warning] 146-149: zetaclient/chains/ton/rpc/client.go#L146-L149
Added lines #L146 - L149 were not covered by tests


[warning] 151-159: zetaclient/chains/ton/rpc/client.go#L151-L159
Added lines #L151 - L159 were not covered by tests


[warning] 169-177: zetaclient/chains/ton/rpc/client.go#L169-L177
Added lines #L169 - L177 were not covered by tests


[warning] 179-181: zetaclient/chains/ton/rpc/client.go#L179-L181
Added lines #L179 - L181 were not covered by tests


[warning] 187-190: zetaclient/chains/ton/rpc/client.go#L187-L190
Added lines #L187 - L190 were not covered by tests


[warning] 193-196: zetaclient/chains/ton/rpc/client.go#L193-L196
Added lines #L193 - L196 were not covered by tests


[warning] 198-204: zetaclient/chains/ton/rpc/client.go#L198-L204
Added lines #L198 - L204 were not covered by tests


[warning] 206-206: zetaclient/chains/ton/rpc/client.go#L206
Added line #L206 was not covered by tests


[warning] 209-209: zetaclient/chains/ton/rpc/client.go#L209
Added line #L209 was not covered by tests


[warning] 217-221: zetaclient/chains/ton/rpc/client.go#L217-L221
Added lines #L217 - L221 were not covered by tests


[warning] 223-225: zetaclient/chains/ton/rpc/client.go#L223-L225
Added lines #L223 - L225 were not covered by tests


[warning] 227-227: zetaclient/chains/ton/rpc/client.go#L227
Added line #L227 was not covered by tests


[warning] 237-241: zetaclient/chains/ton/rpc/client.go#L237-L241
Added lines #L237 - L241 were not covered by tests


[warning] 243-249: zetaclient/chains/ton/rpc/client.go#L243-L249
Added lines #L243 - L249 were not covered by tests


[warning] 252-260: zetaclient/chains/ton/rpc/client.go#L252-L260
Added lines #L252 - L260 were not covered by tests


[warning] 262-263: zetaclient/chains/ton/rpc/client.go#L262-L263
Added lines #L262 - L263 were not covered by tests


[warning] 266-269: zetaclient/chains/ton/rpc/client.go#L266-L269
Added lines #L266 - L269 were not covered by tests

🔇 Additional comments (6)
zetaclient/common/env.go (1)

18-18: LGTM: Clean constant addition following established patterns.

The new EnvTONRPC constant follows the existing naming convention and maintains consistency with other environment variable definitions in the file.

contrib/localnet/docker-compose.yml (1)

283-283: LGTM: Appropriate configuration for enabling TON RPC.

The addition of ENABLE_RPC: true properly enables RPC functionality for the TON service in the local testing environment, supporting the new HTTP-RPC client implementation.

zetaclient/chains/ton/rpc/types.go (1)

97-107: LGTM: Well-structured base64 BOC unmarshaling with proper error handling.

The unmarshalFromBase64 function demonstrates good error handling practices and follows Go conventions for utility functions.

changelog.md (1)

32-32: LGTM: Proper documentation of the new TON HTTP-RPC feature.

The changelog entry appropriately documents the new feature with the correct PR reference, maintaining consistency with the existing documentation format.

zetaclient/chains/ton/rpc/client_live_test.go (1)

18-144: Well-structured integration tests with comprehensive coverage.

The test suite effectively validates the HTTP-RPC client functionality against a live endpoint. The tests cover all major client methods and include proper assertions for both successful and edge cases.

zetaclient/chains/ton/rpc/client.go (1)

43-68: Clean constructor implementation with sensible defaults.

The functional options pattern and automatic endpoint normalization are well-implemented. The comment clearly explains the URL handling logic.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 45-65: zetaclient/chains/ton/rpc/client.go#L45-L65
Added lines #L45 - L65 were not covered by tests


[warning] 67-67: zetaclient/chains/ton/rpc/client.go#L67
Added line #L67 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: 1

🧹 Nitpick comments (7)
zetaclient/chains/ton/rpc/client_live_test.go (2)

28-28: Consider documenting the hardcoded gateway account.

The hardcoded testnet gateway account should be documented to explain its purpose and ensure it remains valid for testing.

+// TON testnet gateway account used for live testing
 gatewayTestnet := ton.MustParseAccountID("EQB6TUFJZyaq2yJ89NMTyVkS8f5sx0LBjr3jBv9ZiB2IFjrk")

96-96: Consider adding error case testing for invalid parameters.

While the current tests cover successful scenarios, consider adding test cases for error conditions such as invalid account IDs or network failures to ensure robust error handling.

t.Run("GetTransactions_InvalidAccount", func(t *testing.T) {
    // Test with malformed account ID
    invalidID := ton.MustParseAccountID("0:0000000000000000000000000000000000000000000000000000000000000000")
    
    txs, err := client.GetTransactions(ctx, 10, invalidID, 0, ton.Bits256{})
    // Validate error handling behavior
    require.NoError(t, err) // or require.Error depending on expected behavior
})
zetaclient/chains/ton/rpc/types.go (1)

73-83: Consider more robust state parsing.

The current approach using strings.Contains for state determination could be fragile and match unintended values. Consider using exact string matching or a more structured approach.

Apply this more robust implementation:

-	switch {
-	case ltRaw == "0" && strings.Contains(stateRaw, "uninit"):
-		acc.Status = tlb.AccountNone
-		return nil
-	case strings.Contains(stateRaw, "uninit"):
-		acc.Status = tlb.AccountUninit
-	case stateRaw == "raw.accountState":
-		acc.Status = tlb.AccountActive
-	case frozenHashRaw != "":
-		acc.Status = tlb.AccountFrozen
-	}
+	switch stateRaw {
+	case "wallet.accountState.walletStateUninit", "wallet.accountState.walletV3StateUninit":
+		if ltRaw == "0" {
+			acc.Status = tlb.AccountNone
+			return nil
+		}
+		acc.Status = tlb.AccountUninit
+	case "raw.accountState":
+		acc.Status = tlb.AccountActive
+	case "raw.frozenAccountState":
+		acc.Status = tlb.AccountFrozen
+	default:
+		// Handle any unexpected states
+		if strings.Contains(stateRaw, "uninit") && ltRaw == "0" {
+			acc.Status = tlb.AccountNone
+			return nil
+		}
+		return errors.Errorf("unexpected account state: %s", stateRaw)
+	}
zetaclient/chains/ton/rpc/client.go (4)

48-53: Document the automatic URL path modification.

The constructor automatically appends /jsonRPC to the endpoint URL. While this is helpful, it should be clearly documented in the function comment to avoid confusion.

Add documentation above the New function:

// New Client constructor
// The endpoint URL will be automatically modified to append "/jsonRPC" if not present.
// To enable generic client metrics, use WithHTTPClient() + metrics.GetInstrumentedHTTPClient()

78-79: Address the caching TODO for immutable data.

Block headers are immutable once created and would benefit from caching to reduce RPC calls.

Would you like me to implement a simple LRU cache for block headers or create an issue to track this optimization?

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 78-91: zetaclient/chains/ton/rpc/client.go#L78-L91
Added lines #L78 - L91 were not covered by tests


146-160: Consider reusing the unmarshalFromBase64 helper.

The cell deserialization logic here duplicates similar code. Consider using the existing unmarshalFromBase64 function for consistency.

-	cells, err := boc.DeserializeBocBase64(rawBase64)
-
-	switch {
-	case err != nil:
-		return nil, errors.Wrapf(err, "unable to deserialize boc from %q", rawBase64)
-	case len(cells) == 0:
-		return nil, errors.Errorf("expected at least one cell, got 0")
-	default:
-		return cells[0], nil
-	}
+	var cell *boc.Cell
+	// We can't use unmarshalFromBase64 directly as it expects a TLB-unmarshalable type
+	cells, err := boc.DeserializeBocBase64(rawBase64)
+	if err != nil {
+		return nil, errors.Wrapf(err, "unable to deserialize boc from %q", rawBase64)
+	}
+	if len(cells) == 0 {
+		return nil, errors.Errorf("expected at least one cell, got 0")
+	}
+	return cells[0], nil
🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 146-149: zetaclient/chains/ton/rpc/client.go#L146-L149
Added lines #L146 - L149 were not covered by tests


[warning] 151-159: zetaclient/chains/ton/rpc/client.go#L151-L159
Added lines #L151 - L159 were not covered by tests


320-322: Fix typo in comment.

-		// Not we take the latest item in the list (oldest tx in the page)
+		// Now we take the latest item in the list (oldest tx in the page)
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c796d35 and be43af3.

📒 Files selected for processing (6)
  • changelog.md (1 hunks)
  • contrib/localnet/docker-compose.yml (1 hunks)
  • zetaclient/chains/ton/rpc/client.go (1 hunks)
  • zetaclient/chains/ton/rpc/client_live_test.go (1 hunks)
  • zetaclient/chains/ton/rpc/types.go (1 hunks)
  • zetaclient/common/env.go (1 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/common/env.go
  • zetaclient/chains/ton/rpc/client_live_test.go
  • zetaclient/chains/ton/rpc/types.go
  • zetaclient/chains/ton/rpc/client.go
🪛 GitHub Check: codecov/patch
zetaclient/chains/ton/rpc/client.go

[warning] 37-38: zetaclient/chains/ton/rpc/client.go#L37-L38
Added lines #L37 - L38 were not covered by tests


[warning] 45-65: zetaclient/chains/ton/rpc/client.go#L45-L65
Added lines #L45 - L65 were not covered by tests


[warning] 67-67: zetaclient/chains/ton/rpc/client.go#L67
Added line #L67 was not covered by tests


[warning] 70-75: zetaclient/chains/ton/rpc/client.go#L70-L75
Added lines #L70 - L75 were not covered by tests


[warning] 78-91: zetaclient/chains/ton/rpc/client.go#L78-L91
Added lines #L78 - L91 were not covered by tests


[warning] 94-98: zetaclient/chains/ton/rpc/client.go#L94-L98
Added lines #L94 - L98 were not covered by tests


[warning] 100-103: zetaclient/chains/ton/rpc/client.go#L100-L103
Added lines #L100 - L103 were not covered by tests


[warning] 105-107: zetaclient/chains/ton/rpc/client.go#L105-L107
Added lines #L105 - L107 were not covered by tests


[warning] 110-119: zetaclient/chains/ton/rpc/client.go#L110-L119
Added lines #L110 - L119 were not covered by tests


[warning] 123-127: zetaclient/chains/ton/rpc/client.go#L123-L127
Added lines #L123 - L127 were not covered by tests


[warning] 129-131: zetaclient/chains/ton/rpc/client.go#L129-L131
Added lines #L129 - L131 were not covered by tests


[warning] 133-133: zetaclient/chains/ton/rpc/client.go#L133
Added line #L133 was not covered by tests


[warning] 136-144: zetaclient/chains/ton/rpc/client.go#L136-L144
Added lines #L136 - L144 were not covered by tests


[warning] 146-149: zetaclient/chains/ton/rpc/client.go#L146-L149
Added lines #L146 - L149 were not covered by tests


[warning] 151-159: zetaclient/chains/ton/rpc/client.go#L151-L159
Added lines #L151 - L159 were not covered by tests


[warning] 169-177: zetaclient/chains/ton/rpc/client.go#L169-L177
Added lines #L169 - L177 were not covered by tests


[warning] 179-181: zetaclient/chains/ton/rpc/client.go#L179-L181
Added lines #L179 - L181 were not covered by tests


[warning] 187-190: zetaclient/chains/ton/rpc/client.go#L187-L190
Added lines #L187 - L190 were not covered by tests


[warning] 193-196: zetaclient/chains/ton/rpc/client.go#L193-L196
Added lines #L193 - L196 were not covered by tests


[warning] 198-204: zetaclient/chains/ton/rpc/client.go#L198-L204
Added lines #L198 - L204 were not covered by tests


[warning] 206-206: zetaclient/chains/ton/rpc/client.go#L206
Added line #L206 was not covered by tests


[warning] 209-209: zetaclient/chains/ton/rpc/client.go#L209
Added line #L209 was not covered by tests


[warning] 217-221: zetaclient/chains/ton/rpc/client.go#L217-L221
Added lines #L217 - L221 were not covered by tests


[warning] 223-225: zetaclient/chains/ton/rpc/client.go#L223-L225
Added lines #L223 - L225 were not covered by tests


[warning] 227-227: zetaclient/chains/ton/rpc/client.go#L227
Added line #L227 was not covered by tests


[warning] 237-241: zetaclient/chains/ton/rpc/client.go#L237-L241
Added lines #L237 - L241 were not covered by tests


[warning] 243-249: zetaclient/chains/ton/rpc/client.go#L243-L249
Added lines #L243 - L249 were not covered by tests


[warning] 252-260: zetaclient/chains/ton/rpc/client.go#L252-L260
Added lines #L252 - L260 were not covered by tests


[warning] 262-263: zetaclient/chains/ton/rpc/client.go#L262-L263
Added lines #L262 - L263 were not covered by tests


[warning] 266-269: zetaclient/chains/ton/rpc/client.go#L266-L269
Added lines #L266 - L269 were not covered by tests

🔇 Additional comments (15)
zetaclient/common/env.go (1)

18-18: LGTM: Clean environment variable constant addition.

The EnvTONRPC constant follows the established naming convention and pattern used by other environment variables in this package.

changelog.md (1)

32-32: LGTM: Proper changelog documentation.

The changelog entry correctly documents the new TON HTTP-RPC feature and follows the established format.

contrib/localnet/docker-compose.yml (1)

283-283: LGTM: Appropriate Docker configuration for TON RPC enablement.

The ENABLE_RPC: true environment variable correctly enables RPC functionality for the TON service in the localnet setup, supporting the new HTTP-RPC client implementation.

zetaclient/chains/ton/rpc/client_live_test.go (4)

18-32: Well-structured test setup with proper conditional execution.

The test setup correctly uses environment variables for conditional execution and endpoint configuration. The fallback to testnet endpoint is appropriate for live testing.


44-74: Comprehensive account state testing with both positive and negative cases.

The test properly validates both existing and non-existing accounts, ensuring correct behavior for different account states. The assertions are thorough and appropriate.


105-142: Well-designed pagination test with proper validation.

The GetTransactionsSince test demonstrates good test design with clear arrange/act/assert structure and validates both the ordering logic and pagination behavior. The assertions ensure data consistency between different query methods.


146-161: Clean helper functions for test output formatting.

The printTx and simplifyTx helper functions provide clean abstractions for test logging and debugging, making the test output more readable and maintainable.

zetaclient/chains/ton/rpc/types.go (4)

1-15: Imports and package declaration look good.

The package structure and imports are well-organized and appropriate for the TON RPC client implementation.


17-40: Type definitions are well-structured.

The struct definitions properly represent TON blockchain entities with appropriate field types and JSON tags.


69-71: Clarify the special balance value "-1".

The check for balanceRaw != "-1" appears to handle a special case. Please add a comment explaining when the API returns "-1" as a balance value and what it signifies.


96-143: Helper functions and RPC types are well-implemented.

The unmarshalFromBase64 function has proper error handling, and the RPC request/response types correctly implement JSON-RPC 2.0 specification.

zetaclient/chains/ton/rpc/client.go (4)

1-42: Package structure and client definition are well-organized.

Good documentation with reference links to the toncenter API specifications. The client struct is simple and focused.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 37-38: zetaclient/chains/ton/rpc/client.go#L37-L38
Added lines #L37 - L38 were not covered by tests


232-288: Efficient implementation of transaction pagination.

The use of defer to reverse the results and the clear pagination logic make this method easy to understand and maintain. Good handling of the edge case when the target transaction is found.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 237-241: zetaclient/chains/ton/rpc/client.go#L237-L241
Added lines #L237 - L241 were not covered by tests


[warning] 243-249: zetaclient/chains/ton/rpc/client.go#L243-L249
Added lines #L243 - L249 were not covered by tests


[warning] 252-260: zetaclient/chains/ton/rpc/client.go#L252-L260
Added lines #L252 - L260 were not covered by tests


[warning] 262-263: zetaclient/chains/ton/rpc/client.go#L262-L263
Added lines #L262 - L263 were not covered by tests


[warning] 266-269: zetaclient/chains/ton/rpc/client.go#L266-L269
Added lines #L266 - L269 were not covered by tests


332-390: Clean abstraction for RPC calls.

The SendMessage, callAndUnmarshal, and call methods provide a well-structured abstraction layer for RPC communication with consistent error handling.


392-441: Robust HTTP handling and metrics implementation.

The HTTP request handling follows best practices with proper resource cleanup and comprehensive error handling. The metrics collection appropriately tracks both success/failure status and request duration.

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 (7)
zetaclient/chains/ton/rpc/types.go (1)

116-127: Consider making RPC request parameters more configurable.

The hardcoded JSON-RPC version and ID values may limit flexibility for different use cases.

Consider parameterizing these values:

-func newRPCRequest(method string, params map[string]any) rpcRequest {
+func newRPCRequest(method string, params map[string]any) rpcRequest {
+	return newRPCRequestWithID(method, params, "1")
+}
+
+func newRPCRequestWithID(method string, params map[string]any, id string) rpcRequest {
 	if params == nil {
 		params = make(map[string]any)
 	}

 	return rpcRequest{
 		Jsonrpc: "2.0",
-		ID:      "1",
+		ID:      id,
 		Method:  method,
 		Params:  params,
 	}
 }

This would allow for request tracking and potential concurrent request handling.

zetaclient/chains/ton/rpc/client_live_test.go (1)

28-28: Consider making the test account configurable.

The hard-coded test account could become problematic if its state changes on the testnet. Consider making it configurable via an environment variable with this address as the default.

-gatewayTestnet := ton.MustParseAccountID("EQB6TUFJZyaq2yJ89NMTyVkS8f5sx0LBjr3jBv9ZiB2IFjrk")
+testAccount := os.Getenv("TON_TEST_ACCOUNT")
+if testAccount == "" {
+    testAccount = "EQB6TUFJZyaq2yJ89NMTyVkS8f5sx0LBjr3jBv9ZiB2IFjrk"
+}
+gatewayTestnet := ton.MustParseAccountID(testAccount)
zetaclient/chains/ton/rpc/client.go (5)

43-68: Document the automatic endpoint modification.

The constructor automatically appends /jsonRPC to the endpoint URL. While convenient, this behavior should be clearly documented in the function comment to avoid confusion.

 // New Client constructor
 // To enable generic client metrics, use WithHTTPClient() + metrics.GetInstrumentedHTTPClient()
+// Note: The endpoint URL will automatically have "/jsonRPC" appended to it.
 func New(endpoint string, chainID int64, opts ...Opt) *Client {
🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 45-65: zetaclient/chains/ton/rpc/client.go#L45-L65
Added lines #L45 - L65 were not covered by tests


[warning] 67-67: zetaclient/chains/ton/rpc/client.go#L67
Added line #L67 was not covered by tests


79-79: Address the caching TODO.

Block headers are immutable once finalized, making them excellent candidates for caching to reduce API calls.

Would you like me to implement a caching layer for block headers or create an issue to track this optimization?


183-185: Consider implementing archival node support.

The TODO comment indicates missing support for archival nodes, which is essential for accessing historical transactions beyond the lite server's retention period.

Would you like me to implement archival node support by adding an archival parameter to the RPC request?


230-288: Elegant handling of transaction ordering.

The implementation correctly transforms the API's reverse chronological order into chronological order. Consider adding a comment explaining the pagination logic for future maintainers.

 func (c *Client) GetTransactionsSince(
 	ctx context.Context,
 	acc ton.AccountID,
 	oldestLT uint64,
 	oldestHash ton.Bits256,
 ) (txs []ton.Transaction, err error) {
+	// The TON API returns transactions in reverse chronological order (newest first).
+	// This method reverses that to return transactions in chronological order (oldest first).
 	lt, hash, err := c.getLastTransactionHash(ctx, acc)
🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 237-241: zetaclient/chains/ton/rpc/client.go#L237-L241
Added lines #L237 - L241 were not covered by tests


[warning] 243-249: zetaclient/chains/ton/rpc/client.go#L243-L249
Added lines #L243 - L249 were not covered by tests


[warning] 252-260: zetaclient/chains/ton/rpc/client.go#L252-L260
Added lines #L252 - L260 were not covered by tests


[warning] 262-263: zetaclient/chains/ton/rpc/client.go#L262-L263
Added lines #L262 - L263 were not covered by tests


[warning] 266-269: zetaclient/chains/ton/rpc/client.go#L266-L269
Added lines #L266 - L269 were not covered by tests


346-348: Complete the response parsing implementation.

The TODO comments indicate that response parsing needs to be explored during e2e testing. This should be addressed to ensure proper error handling.

Would you like me to research the toncenter API response format and implement proper response code parsing?

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c796d35 and be43af3.

📒 Files selected for processing (6)
  • changelog.md (1 hunks)
  • contrib/localnet/docker-compose.yml (1 hunks)
  • zetaclient/chains/ton/rpc/client.go (1 hunks)
  • zetaclient/chains/ton/rpc/client_live_test.go (1 hunks)
  • zetaclient/chains/ton/rpc/types.go (1 hunks)
  • zetaclient/common/env.go (1 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/common/env.go
  • zetaclient/chains/ton/rpc/types.go
  • zetaclient/chains/ton/rpc/client_live_test.go
  • zetaclient/chains/ton/rpc/client.go
🪛 GitHub Check: codecov/patch
zetaclient/chains/ton/rpc/client.go

[warning] 37-38: zetaclient/chains/ton/rpc/client.go#L37-L38
Added lines #L37 - L38 were not covered by tests


[warning] 45-65: zetaclient/chains/ton/rpc/client.go#L45-L65
Added lines #L45 - L65 were not covered by tests


[warning] 67-67: zetaclient/chains/ton/rpc/client.go#L67
Added line #L67 was not covered by tests


[warning] 70-75: zetaclient/chains/ton/rpc/client.go#L70-L75
Added lines #L70 - L75 were not covered by tests


[warning] 78-91: zetaclient/chains/ton/rpc/client.go#L78-L91
Added lines #L78 - L91 were not covered by tests


[warning] 94-98: zetaclient/chains/ton/rpc/client.go#L94-L98
Added lines #L94 - L98 were not covered by tests


[warning] 100-103: zetaclient/chains/ton/rpc/client.go#L100-L103
Added lines #L100 - L103 were not covered by tests


[warning] 105-107: zetaclient/chains/ton/rpc/client.go#L105-L107
Added lines #L105 - L107 were not covered by tests


[warning] 110-119: zetaclient/chains/ton/rpc/client.go#L110-L119
Added lines #L110 - L119 were not covered by tests


[warning] 123-127: zetaclient/chains/ton/rpc/client.go#L123-L127
Added lines #L123 - L127 were not covered by tests


[warning] 129-131: zetaclient/chains/ton/rpc/client.go#L129-L131
Added lines #L129 - L131 were not covered by tests


[warning] 133-133: zetaclient/chains/ton/rpc/client.go#L133
Added line #L133 was not covered by tests


[warning] 136-144: zetaclient/chains/ton/rpc/client.go#L136-L144
Added lines #L136 - L144 were not covered by tests


[warning] 146-149: zetaclient/chains/ton/rpc/client.go#L146-L149
Added lines #L146 - L149 were not covered by tests


[warning] 151-159: zetaclient/chains/ton/rpc/client.go#L151-L159
Added lines #L151 - L159 were not covered by tests


[warning] 169-177: zetaclient/chains/ton/rpc/client.go#L169-L177
Added lines #L169 - L177 were not covered by tests


[warning] 179-181: zetaclient/chains/ton/rpc/client.go#L179-L181
Added lines #L179 - L181 were not covered by tests


[warning] 187-190: zetaclient/chains/ton/rpc/client.go#L187-L190
Added lines #L187 - L190 were not covered by tests


[warning] 193-196: zetaclient/chains/ton/rpc/client.go#L193-L196
Added lines #L193 - L196 were not covered by tests


[warning] 198-204: zetaclient/chains/ton/rpc/client.go#L198-L204
Added lines #L198 - L204 were not covered by tests


[warning] 206-206: zetaclient/chains/ton/rpc/client.go#L206
Added line #L206 was not covered by tests


[warning] 209-209: zetaclient/chains/ton/rpc/client.go#L209
Added line #L209 was not covered by tests


[warning] 217-221: zetaclient/chains/ton/rpc/client.go#L217-L221
Added lines #L217 - L221 were not covered by tests


[warning] 223-225: zetaclient/chains/ton/rpc/client.go#L223-L225
Added lines #L223 - L225 were not covered by tests


[warning] 227-227: zetaclient/chains/ton/rpc/client.go#L227
Added line #L227 was not covered by tests


[warning] 237-241: zetaclient/chains/ton/rpc/client.go#L237-L241
Added lines #L237 - L241 were not covered by tests


[warning] 243-249: zetaclient/chains/ton/rpc/client.go#L243-L249
Added lines #L243 - L249 were not covered by tests


[warning] 252-260: zetaclient/chains/ton/rpc/client.go#L252-L260
Added lines #L252 - L260 were not covered by tests


[warning] 262-263: zetaclient/chains/ton/rpc/client.go#L262-L263
Added lines #L262 - L263 were not covered by tests


[warning] 266-269: zetaclient/chains/ton/rpc/client.go#L266-L269
Added lines #L266 - L269 were not covered by tests

🔇 Additional comments (13)
zetaclient/common/env.go (1)

18-18: LGTM! Environment variable constant follows established patterns.

The constant naming and placement are consistent with existing code standards.

changelog.md (1)

32-32: LGTM! Changelog entry properly documents the new feature.

The entry follows the established format and includes the appropriate PR reference.

contrib/localnet/docker-compose.yml (1)

283-283: LGTM! Environment variable enables the new TON RPC functionality.

The configuration change correctly enables RPC support for local development testing.

zetaclient/chains/ton/rpc/types.go (3)

1-15: LGTM! Package structure and imports are well-organized.

The imports are appropriate for TON blockchain interaction with proper use of external libraries for JSON parsing and TON-specific operations.


17-40: LGTM! Struct definitions are clean and well-structured.

The type definitions appropriately represent TON blockchain entities with proper JSON tags and type safety.


129-143: LGTM! Request serialization and response structure are well-implemented.

The asBody method properly handles JSON marshaling with appropriate error wrapping, and the response structure correctly maps the expected JSON-RPC response format.

zetaclient/chains/ton/rpc/client_live_test.go (4)

34-42: Well-structured health check test.

The test appropriately validates the blockchain's liveness by ensuring the latest block time is recent.


44-74: Comprehensive account state testing.

Excellent coverage of both active and non-existent account scenarios with appropriate assertions for each case.


105-142: Thorough pagination and ordering validation.

The test effectively validates the pagination logic and ensures correct ordering transformation between GetTransactions (DESC) and GetTransactionsSince (ASC).


146-161: Clean helper functions for transaction debugging.

The helper functions provide a clean way to log transaction details during test execution.

zetaclient/chains/ton/rpc/client.go (3)

122-134: Proper validation in transaction hash retrieval.

Good defensive programming by checking account status before accessing transaction fields.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 123-127: zetaclient/chains/ton/rpc/client.go#L123-L127
Added lines #L123 - L127 were not covered by tests


[warning] 129-131: zetaclient/chains/ton/rpc/client.go#L129-L131
Added lines #L129 - L131 were not covered by tests


[warning] 133-133: zetaclient/chains/ton/rpc/client.go#L133
Added line #L133 was not covered by tests


136-161: Well-implemented configuration parameter retrieval.

The method properly handles BOC deserialization with comprehensive error handling and informative error messages.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 136-144: zetaclient/chains/ton/rpc/client.go#L136-L144
Added lines #L136 - L144 were not covered by tests


[warning] 146-149: zetaclient/chains/ton/rpc/client.go#L146-L149
Added lines #L146 - L149 were not covered by tests


[warning] 151-159: zetaclient/chains/ton/rpc/client.go#L151-L159
Added lines #L151 - L159 were not covered by tests


391-429: Well-structured RPC request handling.

The method properly constructs HTTP requests, handles responses, and ensures resource cleanup with deferred body closure.

@swift1337 swift1337 enabled auto-merge May 29, 2025 11:47
@swift1337 swift1337 requested review from kingpinXD and ws4charlie May 30, 2025 11:34
Copy link
Contributor

@ws4charlie ws4charlie left a comment

Choose a reason for hiding this comment

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

looks nice

@swift1337 swift1337 added this pull request to the merge queue Jun 3, 2025
Merged via the queue into develop with commit 927e060 Jun 3, 2025
46 of 47 checks passed
@swift1337 swift1337 deleted the feat/ton-rpc branch June 3, 2025 08:04
swift1337 added a commit that referenced this pull request Jun 11, 2025
* Add TON HTTP-RPC client skeleton

* RPC WIP [1]

* RPC WIP [2]

* RPC WIP [3]

* Metrics note

* Record metrics

* Update changelog
swift1337 added a commit that referenced this pull request Jun 11, 2025
* feat: migrate to golangci-lint v2 (#3960)

* Migrate linter to V2

* Apply lint

* Improve makefile

* feat(ton): Implement HTTP-RPC based on toncenter v2 (#3929)

* Add TON HTTP-RPC client skeleton

* RPC WIP [1]

* RPC WIP [2]

* RPC WIP [3]

* Metrics note

* Record metrics

* Update changelog

* feat(zetaclient): integrate TON HTTP-RPC (#3958)

* Refactor observer; refactor mocks

* Fix signer & unit tests

* Drop liteapi

* Restore tx hash conversion aliases

* Fix E2E, adapt Tongo x RPC

* Update wait-for-ton

* Move tlb to rpc package

* fix e2e tests; simplify ensureLastScannedTx; fix client

* rename .rpc to .Endpoint in config

* Simplify config

* Update changelog

* lint

* Address PR comments

* PR fixes

* fix merge conflict

* bump linter timeout

* use ton localnet v2

* bump ton localnet

* bump ton localnet

* Bump changelog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

nosec TON_TESTS Runs TON E2E tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement http-rpc client

3 participants