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

refactor(zetaclient)!: TON observer-signer #3418

Merged
merged 7 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* [3379](https://github.com/zeta-chain/node/pull/3379) - add Avalanche, Arbitrum and World Chain in chain info
* [3390](https://github.com/zeta-chain/node/pull/3390) - orchestrator V2: EVM observer-signer
* [3326](https://github.com/zeta-chain/node/pull/3326) - improve error messages for cctx status object
* [3418](https://github.com/zeta-chain/node/pull/3418) - orchestrator V2: TON observer-signer


### Fixes
Expand Down
4 changes: 2 additions & 2 deletions cmd/zetae2e/config/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/zeta-chain/node/pkg/retry"
zetacore_rpc "github.com/zeta-chain/node/pkg/rpc"
btcclient "github.com/zeta-chain/node/zetaclient/chains/bitcoin/client"
tonconfig "github.com/zeta-chain/node/zetaclient/chains/ton"
tonconfig "github.com/zeta-chain/node/zetaclient/chains/ton/config"
zetaclientconfig "github.com/zeta-chain/node/zetaclient/config"
)

Expand Down Expand Up @@ -135,7 +135,7 @@ func getTONClient(ctx context.Context, sidecarURL string) (*tonrunner.Client, er
// It might take some time to bootstrap the sidecar
cfg, err := retry.DoTypedWithRetry(
func() (*tonconfig.GlobalConfigurationFile, error) {
return tonconfig.ConfigFromURL(ctx, sidecar.LiteServerURL())
return tonconfig.FromURL(ctx, sidecar.LiteServerURL())
},
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ton
package config

import (
"context"
Expand All @@ -15,19 +15,19 @@

type GlobalConfigurationFile = config.GlobalConfigurationFile

// ConfigGetter represents LiteAPI config params getter.
// Getter represents LiteAPI config params getter.
// Don't be confused because config param in this case represent on-chain params,
// not lite-client's ADNL json config to connect to the network.
//
// Read more at https://docs.ton.org/develop/howto/blockchain-configs
type ConfigGetter interface {
type Getter interface {
GetConfigParams(ctx context.Context, mode liteapi.ConfigMode, params []uint32) (tlb.ConfigParams, error)
}

// ConfigFromURL downloads & parses lite server config.
// FromURL downloads & parses lite server config.
//
//nolint:gosec
func ConfigFromURL(ctx context.Context, url string) (*GlobalConfigurationFile, error) {
func FromURL(ctx context.Context, url string) (*GlobalConfigurationFile, error) {

Check warning on line 30 in zetaclient/chains/ton/config/config.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/ton/config/config.go#L30

Added line #L30 was not covered by tests
const timeout = 3 * time.Second

ctx, cancel := context.WithTimeout(ctx, timeout)
Expand All @@ -52,21 +52,22 @@
return config.ParseConfig(res.Body)
}

func ConfigFromPath(path string) (*GlobalConfigurationFile, error) {
// FromPath parses config file from path.
func FromPath(path string) (*GlobalConfigurationFile, error) {

Check warning on line 56 in zetaclient/chains/ton/config/config.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/ton/config/config.go#L56

Added line #L56 was not covered by tests
return config.ParseConfigFile(path)
}

// ConfigFromSource returns a parsed configuration file from a URL or a file path.
func ConfigFromSource(ctx context.Context, urlOrPath string) (*GlobalConfigurationFile, error) {
// FromSource returns a parsed configuration file from a URL or a file path.
func FromSource(ctx context.Context, urlOrPath string) (*GlobalConfigurationFile, error) {

Check warning on line 61 in zetaclient/chains/ton/config/config.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/ton/config/config.go#L61

Added line #L61 was not covered by tests
if u, err := url.Parse(urlOrPath); err == nil {
return ConfigFromURL(ctx, u.String())
return FromURL(ctx, u.String())

Check warning on line 63 in zetaclient/chains/ton/config/config.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/ton/config/config.go#L63

Added line #L63 was not covered by tests
}

return ConfigFromPath(urlOrPath)
return FromPath(urlOrPath)

Check warning on line 66 in zetaclient/chains/ton/config/config.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/ton/config/config.go#L66

Added line #L66 was not covered by tests
}

// FetchGasConfig fetches gas price from the config.
func FetchGasConfig(ctx context.Context, getter ConfigGetter) (tlb.GasLimitsPrices, error) {
func FetchGasConfig(ctx context.Context, getter Getter) (tlb.GasLimitsPrices, error) {

Check warning on line 70 in zetaclient/chains/ton/config/config.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/ton/config/config.go#L70

Added line #L70 was not covered by tests
// https://docs.ton.org/develop/howto/blockchain-configs
// https://tonviewer.com/config#21
const configKeyGas = 21
Expand Down
4 changes: 2 additions & 2 deletions zetaclient/chains/ton/liteapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"github.com/tonkeeper/tongo/tlb"
"github.com/tonkeeper/tongo/ton"

zetaton "github.com/zeta-chain/node/zetaclient/chains/ton"
"github.com/zeta-chain/node/zetaclient/chains/ton/config"
)

// Client extends liteapi.Client with some high-level tools
Expand All @@ -39,7 +39,7 @@

// NewFromSource creates a new client from a URL or a file path.
func NewFromSource(ctx context.Context, urlOrPath string) (*Client, error) {
cfg, err := zetaton.ConfigFromSource(ctx, urlOrPath)
cfg, err := config.FromSource(ctx, urlOrPath)

Check warning on line 42 in zetaclient/chains/ton/liteapi/client.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/ton/liteapi/client.go#L42

Added line #L42 was not covered by tests
if err != nil {
return nil, errors.Wrap(err, "unable to get config")
}
Expand Down
6 changes: 3 additions & 3 deletions zetaclient/chains/ton/liteapi/client_live_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/tonkeeper/tongo/tlb"
"github.com/tonkeeper/tongo/ton"
toncontracts "github.com/zeta-chain/node/pkg/contracts/ton"
zetaton "github.com/zeta-chain/node/zetaclient/chains/ton"
zetatonconfig "github.com/zeta-chain/node/zetaclient/chains/ton/config"
"github.com/zeta-chain/node/zetaclient/common"
)

Expand Down Expand Up @@ -170,14 +170,14 @@ func TestClient(t *testing.T) {

t.Run("GetGasConfig", func(t *testing.T) {
// ACT #1
gas, err := zetaton.FetchGasConfig(ctx, client)
gas, err := zetatonconfig.FetchGasConfig(ctx, client)

// ASSERT #1
require.NoError(t, err)
require.NotEmpty(t, gas)

// ACT #2
gasPrice, err := zetaton.ParseGasPrice(gas)
gasPrice, err := zetatonconfig.ParseGasPrice(gas)

// ASSERT #2
require.NoError(t, err)
Expand Down
61 changes: 8 additions & 53 deletions zetaclient/chains/ton/observer/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@ import (

"cosmossdk.io/math"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/tonkeeper/tongo/ton"

"github.com/zeta-chain/node/pkg/coin"
toncontracts "github.com/zeta-chain/node/pkg/contracts/ton"
"github.com/zeta-chain/node/pkg/ticker"
"github.com/zeta-chain/node/x/crosschain/types"
"github.com/zeta-chain/node/zetaclient/chains/ton/liteapi"
zctx "github.com/zeta-chain/node/zetaclient/context"
"github.com/zeta-chain/node/zetaclient/zetacore"
)

Expand All @@ -24,58 +21,16 @@ const (
// TODO: move to config
// https://github.com/zeta-chain/node/issues/3086
maxTransactionsPerTick = 100
// zero log sample rate for sampled logger (to avoid spamming logs)
logSampleRate = 10
)

// watchInbound watches for new txs to Gateway's account.
func (ob *Observer) watchInbound(ctx context.Context) error {
return ob.inboundTicker(ctx, "WatchInbound", ob.observeGateway)
}

func (ob *Observer) watchInboundTracker(ctx context.Context) error {
return ob.inboundTicker(ctx, "WatchInboundTracker", ob.processInboundTrackers)
}

func (ob *Observer) inboundTicker(ctx context.Context, taskName string, taskFunc func(context.Context) error) error {
app, err := zctx.FromContext(ctx)
if err != nil {
return err
}

initialInterval := ticker.DurationFromUint64Seconds(ob.ChainParams().InboundTicker)
sampledLogger := ob.Logger().Inbound.Sample(&zerolog.BasicSampler{N: logSampleRate})

task := func(ctx context.Context, t *ticker.Ticker) error {
if !app.IsInboundObservationEnabled() {
sampledLogger.Info().Msgf("%s: inbound observation is disabled", taskName)
return nil
}

if err := taskFunc(ctx); err != nil {
ob.Logger().Inbound.Err(err).Msgf("%s failed", taskName)
}

newInterval := ticker.DurationFromUint64Seconds(ob.ChainParams().InboundTicker)
t.SetInterval(newInterval)

return nil
}

return ticker.Run(
ctx,
initialInterval,
task,
ticker.WithStopChan(ob.StopChannel()),
ticker.WithLogger(ob.Logger().Inbound, taskName),
)
}

// observeGateway observes Gateway's account for new transactions.
// Due to TON architecture we have to scan for all net-new transactions.
// ObserveInbound observes Gateway's account for new transactions [INBOUND AND OUTBOUND]
//
// Due to TON's architecture we have to scan for all net-new transactions.
// The main purpose is to observe inbounds from TON.
// Note that we might also have *outbounds* here (if a signer broadcasts a tx, it will be observed here).
func (ob *Observer) observeGateway(ctx context.Context) error {
//
// The name `ObserveInbound` is used for consistency with other chains.
func (ob *Observer) ObserveInbound(ctx context.Context) error {
if err := ob.ensureLastScannedTX(ctx); err != nil {
return errors.Wrap(err, "unable to ensure last scanned tx")
}
Expand Down Expand Up @@ -165,8 +120,8 @@ func (ob *Observer) observeGateway(ctx context.Context) error {
return nil
}

// processInboundTrackers handles adhoc trackers that were somehow missed by
func (ob *Observer) processInboundTrackers(ctx context.Context) error {
// ObserveInboundTrackers handles adhoc trackers that were somehow missed by
func (ob *Observer) ObserveInboundTrackers(ctx context.Context) error {
trackers, err := ob.ZetacoreClient().GetInboundTrackersForChain(ctx, ob.Chain().ChainId)
if err != nil {
return errors.Wrap(err, "unable to get inbound trackers")
Expand Down
16 changes: 8 additions & 8 deletions zetaclient/chains/ton/observer/inbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestInbound(t *testing.T) {

// ACT
// Observe inbounds once
err = ob.observeGateway(ts.ctx)
err = ob.ObserveInbound(ts.ctx)

// ASSERT
assert.ErrorContains(t, err, "unable to ensure last scanned tx")
Expand All @@ -64,7 +64,7 @@ func TestInbound(t *testing.T) {

// ACT
// Observe inbounds once
err = ob.observeGateway(ts.ctx)
err = ob.ObserveInbound(ts.ctx)

// ASSERT
assert.NoError(t, err)
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestInbound(t *testing.T) {

// ACT
// Observe inbounds once
err = ob.observeGateway(ts.ctx)
err = ob.ObserveInbound(ts.ctx)

// ASSERT
assert.NoError(t, err)
Expand Down Expand Up @@ -145,7 +145,7 @@ func TestInbound(t *testing.T) {

// ACT
// Observe inbounds once
err = ob.observeGateway(ts.ctx)
err = ob.ObserveInbound(ts.ctx)

// ASSERT
assert.NoError(t, err)
Expand Down Expand Up @@ -207,7 +207,7 @@ func TestInbound(t *testing.T) {

// ACT
// Observe inbounds once
err = ob.observeGateway(ts.ctx)
err = ob.ObserveInbound(ts.ctx)

// ASSERT
assert.NoError(t, err)
Expand Down Expand Up @@ -275,7 +275,7 @@ func TestInbound(t *testing.T) {
Once()

// ACT
err = ob.observeGateway(ts.ctx)
err = ob.ObserveInbound(ts.ctx)

// ASSERT
assert.NoError(t, err)
Expand Down Expand Up @@ -353,7 +353,7 @@ func TestInbound(t *testing.T) {

// ACT
// Observe inbounds once
err = ob.observeGateway(ts.ctx)
err = ob.ObserveInbound(ts.ctx)

// ASSERT
assert.NoError(t, err)
Expand Down Expand Up @@ -427,7 +427,7 @@ func TestInboundTracker(t *testing.T) {
ts.OnGetInboundTrackersForChain(trackers).Once()

// ACT
err = ob.processInboundTrackers(ts.ctx)
err = ob.ObserveInboundTrackers(ts.ctx)

// ARRANGE
require.NoError(t, err)
Expand Down
Loading
Loading