From f681dfe1b52d014437f9faf3db2b0e8fab1fdd9c Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Mon, 25 Nov 2024 23:34:27 +0100 Subject: [PATCH] refactore: move ika client to ika package --- .env.example | 11 +++--- cmd/native-ika/cli.go | 20 +++++++---- ika/client.go | 80 +++++++++++++++++++++++++++++++++++++++++++ ika/sui.go | 27 +++++++++++++++ native/client.go | 72 -------------------------------------- native/handlers.go | 2 +- native/indexer.go | 16 +++------ native/tx.go | 56 ------------------------------ 8 files changed, 134 insertions(+), 150 deletions(-) create mode 100644 ika/client.go create mode 100644 ika/sui.go delete mode 100644 native/client.go delete mode 100644 native/tx.go diff --git a/.env.example b/.env.example index 88d75a1..6a16bc5 100644 --- a/.env.example +++ b/.env.example @@ -1,14 +1,17 @@ -NATIVE_RPC=http://localhost:26657 -NATIVE_GRPC=127.0.0.1:9090 LOG_FILE_PATH="" # If empty, logs to STDOUT LOG_LEVEL="debug" + +NATIVE_RPC=http://localhost:26657 +NATIVE_GRPC=127.0.0.1:9090 + IKA_RPC=http://127.0.0.1:9000 IKA_SIGNER_MNEMONIC="" -GAS_ADDRESS="" +IKA_GAS_ACC="" +IKA_GAS_BUDGET="" IKA_NATIVE_LC_PACKAGE="" IKA_NATIVE_LC_MODULE="" IKA_NATIVE_LC_FUNCTION="" -GAS_BUDGET="" + BTC_RPC_USER="user" BTC_RPC_PASS="pass" BTC_RPC="localhost:18334" diff --git a/cmd/native-ika/cli.go b/cmd/native-ika/cli.go index 18c2c1e..1112252 100644 --- a/cmd/native-ika/cli.go +++ b/cmd/native-ika/cli.go @@ -6,6 +6,7 @@ import ( "github.com/block-vision/sui-go-sdk/signer" "github.com/block-vision/sui-go-sdk/sui" + "github.com/gonative-cc/relayer/ika" "github.com/gonative-cc/relayer/native" "github.com/gonative-cc/relayer/native/blockchain" "github.com/rs/zerolog/log" @@ -23,8 +24,8 @@ const ( IkaNativeLcPackage = "IKA_NATIVE_LC_PACKAGE" IkaNativeLcModule = "IKA_NATIVE_LC_MODULE" IkaNativeLcFunction = "IKA_NATIVE_LC_FUNCTION" - GasAddress = "GAS_ADDRESS" - GasBudget = "GAS_BUDGET" + IkaGasAcc = "IKA_GAS_ACC" + IkaGasBudget = "IKA_GAS_BUDGET" ) var ( @@ -72,14 +73,21 @@ func CmdStart() *cobra.Command { logger := log.With().Str("module", "native").Logger() ctx := cmd.Context() - pc, err := native.NewIkaClient(c, signer, - os.Getenv(IkaNativeLcPackage), os.Getenv(IkaNativeLcModule), - os.Getenv(IkaNativeLcFunction), os.Getenv(GasAddress), os.Getenv(GasBudget)) + lcContract := ika.SuiCtrCall{ + Package: os.Getenv(IkaNativeLcPackage), + Module: os.Getenv(IkaNativeLcModule), + Function: os.Getenv(IkaNativeLcFunction), + } + if err := lcContract.Validate(); err != nil { + return err + } + ikaClient, err := ika.NewClient(c, signer, lcContract, + os.Getenv(IkaGasAcc), os.Getenv(IkaGasBudget)) if err != nil { return err } - idx, err := native.NewIndexer(ctx, b, logger, minimumBlockHeight, pc) + idx, err := native.NewIndexer(ctx, b, logger, minimumBlockHeight, ikaClient) if err != nil { return err } diff --git a/ika/client.go b/ika/client.go new file mode 100644 index 0000000..45cfb2f --- /dev/null +++ b/ika/client.go @@ -0,0 +1,80 @@ +package ika + +import ( + "context" + + "github.com/block-vision/sui-go-sdk/models" + "github.com/block-vision/sui-go-sdk/signer" + "github.com/block-vision/sui-go-sdk/sui" + tmtypes "github.com/cometbft/cometbft/types" + "github.com/rs/zerolog" +) + +// Client is a wrapper around the Sui client that provides functionality +// for interacting with Ika +type Client struct { + c *sui.Client + Signer *signer.Signer + LcPackage string + Module string + Function string + GasAddr string + GasBudget string +} + +// NewClient creates a new Client instance +func NewClient( + c *sui.Client, + signer *signer.Signer, + ctr SuiCtrCall, + gasAddr, gasBudget string, +) (*Client, error) { + i := &Client{ + c: c, + Signer: signer, + LcPackage: ctr.Package, + Module: ctr.Module, + Function: ctr.Function, + GasAddr: gasAddr, + GasBudget: gasBudget, + } + return i, nil +} + +// UpdateLC sends light blocks to the Native Light Client module in the Ika blockchain. +// It returns the transaction response and an error if any occurred. +func (p *Client) UpdateLC( + ctx context.Context, + lb *tmtypes.LightBlock, + logger zerolog.Logger, +) (models.SuiTransactionBlockResponse, error) { + req := models.MoveCallRequest{ + Signer: p.Signer.Address, + PackageObjectId: p.LcPackage, + Module: p.Module, + Function: p.Function, + TypeArguments: []interface{}{}, + Arguments: []interface{}{ + lb, + }, + Gas: p.GasAddr, + GasBudget: p.GasBudget, + } + resp, err := p.c.MoveCall(ctx, req) + if err != nil { + logger.Err(err).Msg("Error calling move function:") + return models.SuiTransactionBlockResponse{}, err // Return zero value for the response + } + + // TODO: verify if we need to call this + return p.c.SignAndExecuteTransactionBlock(ctx, models.SignAndExecuteTransactionBlockRequest{ + TxnMetaData: resp, + PriKey: p.Signer.PriKey, + Options: models.SuiTransactionBlockOptions{ + ShowInput: true, + ShowRawInput: true, + ShowEffects: true, + }, + RequestType: "WaitForLocalExecution", + }) +} diff --git a/ika/sui.go b/ika/sui.go new file mode 100644 index 0000000..ef9c7eb --- /dev/null +++ b/ika/sui.go @@ -0,0 +1,27 @@ +package ika + +import ( + "errors" + "strings" +) + +// SuiCtrCall defines a contract path to call in Sui +type SuiCtrCall struct { + Package, Module, Function string +} + +// Validate the fields of the struct +func (ctr SuiCtrCall) Validate() error { + return errors.Join( + checkNotEmpty("package", ctr.Package), + checkNotEmpty("module", ctr.Module), + checkNotEmpty("function", ctr.Function), + ) +} + +func checkNotEmpty(name, s string) error { + if strings.TrimSpace(s) == "" { + return errors.New(name + " must be defined") + } + return nil +} diff --git a/native/client.go b/native/client.go deleted file mode 100644 index 2db6db0..0000000 --- a/native/client.go +++ /dev/null @@ -1,72 +0,0 @@ -package native - -import ( - "context" - - "github.com/block-vision/sui-go-sdk/models" - "github.com/block-vision/sui-go-sdk/signer" - "github.com/block-vision/sui-go-sdk/sui" - tmtypes "github.com/cometbft/cometbft/types" - "github.com/rs/zerolog" -) - -// IkaClient is a wrapper around the Sui client that provides functionality -// for interacting with Ika -type IkaClient struct { - c *sui.Client - Signer *signer.Signer - LcPackage string - Module string - Function string - GasAddr string - GasBudget string -} - -// NewIkaClient creates a new IkaClient instance -func NewIkaClient( - c *sui.Client, - signer *signer.Signer, - lcpackage, module, function, gasAddr, gasBudget string, -) (*IkaClient, error) { - i := &IkaClient{ - c: c, - Signer: signer, - LcPackage: lcpackage, - Module: module, - Function: function, - GasAddr: gasAddr, - GasBudget: gasBudget, - } - return i, nil -} - -// updateLC sends light blocks to the Native Light Client module in the Ika blockchain. -// It returns the transaction response and an error if any occurred. -func (p *IkaClient) updateLC( - ctx context.Context, - lb *tmtypes.LightBlock, - logger zerolog.Logger, -) (models.SuiTransactionBlockResponse, error) { - rsp, err := callMoveFunction( - ctx, - p.c, - p.LcPackage, - p.Module, - p.Function, - p.GasBudget, - p.Signer.Address, - p.GasAddr, - lb, - ) - if err != nil { - logger.Err(err).Msg("Error calling move function:") - return models.SuiTransactionBlockResponse{}, err // Return zero value for the response - } - - rsp2, err := executeTransaction(ctx, p.c, rsp, p.Signer.PriKey) - if err != nil { - logger.Err(err).Msg("Error executing transaction:") - return models.SuiTransactionBlockResponse{}, err // Return zero value for the response - } - return rsp2, nil -} diff --git a/native/handlers.go b/native/handlers.go index 2285875..216c12b 100644 --- a/native/handlers.go +++ b/native/handlers.go @@ -24,7 +24,7 @@ func (i *Indexer) HandleBlock(ctx context.Context, blk *tmtypes.Block) error { } i.logger.Info().Int64("light block", lb.SignedHeader.Header.Height).Msg("Light Block ") - txrsp, err := i.pc.updateLC(ctx, lb, i.logger) + txrsp, err := i.ika.UpdateLC(ctx, lb, i.logger) if err != nil { return err } diff --git a/native/indexer.go b/native/indexer.go index 9433a30..6fede5b 100644 --- a/native/indexer.go +++ b/native/indexer.go @@ -6,8 +6,8 @@ import ( "time" tmtypes "github.com/cometbft/cometbft/types" + "github.com/gonative-cc/relayer/ika" "github.com/rs/zerolog" - "golang.org/x/sync/errgroup" ) const ( @@ -23,17 +23,17 @@ type Indexer struct { lowestBlock int logger zerolog.Logger - pc *IkaClient + ika *ika.Client } // NewIndexer returns a new indexer struct with open connections. func NewIndexer(ctx context.Context, b Blockchain, logger zerolog.Logger, - startBlockHeight int, pc *IkaClient) (*Indexer, error) { + startBlockHeight int, ika *ika.Client) (*Indexer, error) { i := &Indexer{ b: b, logger: logger.With().Str("package", "indexer").Logger(), lowestBlock: startBlockHeight, - pc: pc, + ika: ika, } return i, i.onStart(ctx) } @@ -164,11 +164,5 @@ func (i *Indexer) loadChainHeader(_ context.Context) error { // Close closes all the open connections. func (i *Indexer) Close(ctx context.Context) error { - g, ctx := errgroup.WithContext(ctx) - - g.Go(func() error { - return i.b.Close(ctx) - }) - - return g.Wait() + return i.b.Close(ctx) } diff --git a/native/tx.go b/native/tx.go deleted file mode 100644 index 06c63f2..0000000 --- a/native/tx.go +++ /dev/null @@ -1,56 +0,0 @@ -package native - -import ( - "context" - "crypto/ed25519" - - "github.com/block-vision/sui-go-sdk/models" - "github.com/block-vision/sui-go-sdk/sui" - tmtypes "github.com/cometbft/cometbft/types" -) - -// callMoveFunction prepares a Move call transaction to be executed on the Ika/Sui network. -// It takes the necessary parameters for the Move call, including the package, module, -// function, arguments, gas information, and signer address. -// It returns the transaction metadata and an error if any occurred during preparation. -func callMoveFunction( - ctx context.Context, - c *sui.Client, - lcpackage, module, function, gasbudget, - signerAddress, gasAddr string, - lb *tmtypes.LightBlock, -) (models.TxnMetaData, error) { - return c.MoveCall(ctx, models.MoveCallRequest{ - Signer: signerAddress, - PackageObjectId: lcpackage, - Module: module, - Function: function, - TypeArguments: []interface{}{}, - Arguments: []interface{}{ - lb, - }, - Gas: gasAddr, - GasBudget: gasbudget, - }) -} - -// executeTransaction signs and executes a prepared Move call transaction on the Ika/Sui network. -// It takes the transaction metadata, the signer's private key, and execution options. -// It returns the transaction response and an error if any occurred during execution. -func executeTransaction( - ctx context.Context, - cli *sui.Client, - txnMetaData models.TxnMetaData, - priKey ed25519.PrivateKey, -) (models.SuiTransactionBlockResponse, error) { - return cli.SignAndExecuteTransactionBlock(ctx, models.SignAndExecuteTransactionBlockRequest{ - TxnMetaData: txnMetaData, - PriKey: priKey, - Options: models.SuiTransactionBlockOptions{ - ShowInput: true, - ShowRawInput: true, - ShowEffects: true, - }, - RequestType: "WaitForLocalExecution", - }) -}