From bf6a79f1c79203d09b69fc2cf450331721517caa Mon Sep 17 00:00:00 2001 From: leohhhn Date: Sat, 20 Jan 2024 18:53:24 +0100 Subject: [PATCH 01/38] wip save --- gno.land/pkg/gnoclient/client_txs.go | 95 ++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/gno.land/pkg/gnoclient/client_txs.go b/gno.land/pkg/gnoclient/client_txs.go index ab475154fad..2f5c38e2e0f 100644 --- a/gno.land/pkg/gnoclient/client_txs.go +++ b/gno.land/pkg/gnoclient/client_txs.go @@ -21,6 +21,30 @@ type CallCfg struct { Memo string // Memo } +// type MsgCall struct { +// Caller crypto.Address `json:"caller" yaml:"caller"` +// Send std.Coins `json:"send" yaml:"send"` +// PkgPath string `json:"pkg_path" yaml:"pkg_path"` +// Func string `json:"func" yaml:"func"` +// Args []string `json:"args" yaml:"args"` +//} + +// MultiCallCfg contains configuration options for executing a contract call. +type MultiCallCfg struct { + // Per MsgCall + PkgPath string // Package path + FuncName string // Function name + Args []string // Function arguments + Send string // Send amount + + // Per TX + GasFee string // Gas fee + GasWanted int64 // Gas wanted + AccountNumber uint64 // Account number + SequenceNumber uint64 // Sequence number + Memo string // Memo +} + // Call executes a contract call on the blockchain. func (c *Client) Call(cfg CallCfg) (*ctypes.ResultBroadcastTxCommit, error) { // Validate required client fields. @@ -81,6 +105,77 @@ func (c *Client) Call(cfg CallCfg) (*ctypes.ResultBroadcastTxCommit, error) { return c.signAndBroadcastTxCommit(tx, accountNumber, sequenceNumber) } +// MultiCall executes a contract call on the blockchain. +func (c *Client) MultiCall(cfgs []CallCfg) (*ctypes.ResultBroadcastTxCommit, error) { + // Validate required client fields. + if err := c.validateSigner(); err != nil { + return nil, errors.Wrap(err, "validate signer") + } + if err := c.validateRPCClient(); err != nil { + return nil, errors.Wrap(err, "validate RPC client") + } + + sequenceNumber := cfg.SequenceNumber + accountNumber := cfg.AccountNumber + + var gasWanted int64 + var gasFee string + gasFeeCoins, err := std.ParseCoin(gasFee) + + var msgs []vm.MsgCall + + for _, cfg := range cfgs { + pkgPath := cfg.PkgPath + funcName := cfg.FuncName + args := cfg.Args + send := cfg.Send + + // Validate config. + if pkgPath == "" { + return nil, errors.New("missing PkgPath") + } + if funcName == "" { + return nil, errors.New("missing FuncName") + } + + // Parse send amount. + sendCoins, err := std.ParseCoins(send) + if err != nil { + return nil, errors.Wrap(err, "parsing send coins") + } + + if err != nil { + return nil, errors.Wrap(err, "parsing gas fee coin") + } + + caller := c.Signer.Info().GetAddress() + + // Construct message & transaction and marshal. + msgs = append(msgs, vm.MsgCall{ + Caller: caller, + Send: sendCoins, + PkgPath: pkgPath, + Func: funcName, + Args: args, + }) + + } + + stdMsgs := make([]std.Msg, len(msgs)) + for i, msg := range msgs { + stdMsgs[i] = msg + } + + tx := std.Tx{ + Msgs: stdMsgs, + Fee: std.NewFee(gasWanted, gasFeeCoins), + Signatures: nil, + Memo: "", + } + + return c.signAndBroadcastTxCommit(tx, accountNumber, sequenceNumber) +} + // signAndBroadcastTxCommit signs a transaction and broadcasts it, returning the result. func (c Client) signAndBroadcastTxCommit(tx std.Tx, accountNumber, sequenceNumber uint64) (*ctypes.ResultBroadcastTxCommit, error) { caller := c.Signer.Info().GetAddress() From 738f70ca1572bda42b19f30f6655b407252b0406 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Mon, 22 Jan 2024 00:19:28 +0100 Subject: [PATCH 02/38] add msgcall wrapper & add batchcall func --- gno.land/pkg/gnoclient/client_txs.go | 64 +++++++++++++--------------- 1 file changed, 29 insertions(+), 35 deletions(-) diff --git a/gno.land/pkg/gnoclient/client_txs.go b/gno.land/pkg/gnoclient/client_txs.go index 2f5c38e2e0f..ac6d191dfc6 100644 --- a/gno.land/pkg/gnoclient/client_txs.go +++ b/gno.land/pkg/gnoclient/client_txs.go @@ -8,36 +8,30 @@ import ( "github.com/gnolang/gno/tm2/pkg/std" ) +// MsgCall - syntax sugar for vm.MsgCall +type MsgCall struct { + PkgPath string // Package path + FuncName string // Function name + Args []string // Function arguments + Send string // Send amount +} + // CallCfg contains configuration options for executing a contract call. type CallCfg struct { - PkgPath string // Package path - FuncName string // Function name - Args []string // Function arguments - GasFee string // Gas fee - GasWanted int64 // Gas wanted - Send string // Send amount - AccountNumber uint64 // Account number - SequenceNumber uint64 // Sequence number - Memo string // Memo + MsgCall + GasFee string // Gas fee + GasWanted int64 // Gas wanted + AccountNumber uint64 // Account number + SequenceNumber uint64 // Sequence number + Memo string // Memo } -// type MsgCall struct { -// Caller crypto.Address `json:"caller" yaml:"caller"` -// Send std.Coins `json:"send" yaml:"send"` -// PkgPath string `json:"pkg_path" yaml:"pkg_path"` -// Func string `json:"func" yaml:"func"` -// Args []string `json:"args" yaml:"args"` -//} - // MultiCallCfg contains configuration options for executing a contract call. type MultiCallCfg struct { // Per MsgCall - PkgPath string // Package path - FuncName string // Function name - Args []string // Function arguments - Send string // Send amount + Msgs []MsgCall - // Per TX + // Per Tx GasFee string // Gas fee GasWanted int64 // Gas wanted AccountNumber uint64 // Account number @@ -106,7 +100,7 @@ func (c *Client) Call(cfg CallCfg) (*ctypes.ResultBroadcastTxCommit, error) { } // MultiCall executes a contract call on the blockchain. -func (c *Client) MultiCall(cfgs []CallCfg) (*ctypes.ResultBroadcastTxCommit, error) { +func (c *Client) MultiCall(cfg MultiCallCfg) (*ctypes.ResultBroadcastTxCommit, error) { // Validate required client fields. if err := c.validateSigner(); err != nil { return nil, errors.Wrap(err, "validate signer") @@ -118,17 +112,12 @@ func (c *Client) MultiCall(cfgs []CallCfg) (*ctypes.ResultBroadcastTxCommit, err sequenceNumber := cfg.SequenceNumber accountNumber := cfg.AccountNumber - var gasWanted int64 - var gasFee string - gasFeeCoins, err := std.ParseCoin(gasFee) - var msgs []vm.MsgCall - - for _, cfg := range cfgs { - pkgPath := cfg.PkgPath - funcName := cfg.FuncName - args := cfg.Args - send := cfg.Send + for _, msg := range cfg.Msgs { + pkgPath := msg.PkgPath + funcName := msg.FuncName + args := msg.Args + send := msg.Send // Validate config. if pkgPath == "" { @@ -150,7 +139,6 @@ func (c *Client) MultiCall(cfgs []CallCfg) (*ctypes.ResultBroadcastTxCommit, err caller := c.Signer.Info().GetAddress() - // Construct message & transaction and marshal. msgs = append(msgs, vm.MsgCall{ Caller: caller, Send: sendCoins, @@ -166,9 +154,15 @@ func (c *Client) MultiCall(cfgs []CallCfg) (*ctypes.ResultBroadcastTxCommit, err stdMsgs[i] = msg } + // Parse gas wanted & fee. + gasFeeCoins, err := std.ParseCoin(cfg.GasFee) + if err != nil { + return nil, errors.Wrap(err, "parsing gas fee coin") + } + tx := std.Tx{ Msgs: stdMsgs, - Fee: std.NewFee(gasWanted, gasFeeCoins), + Fee: std.NewFee(cfg.GasWanted, gasFeeCoins), Signatures: nil, Memo: "", } From 2de37123d6bf9c91f6a042352061baf2a7d86502 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Mon, 22 Jan 2024 00:19:48 +0100 Subject: [PATCH 03/38] remove comment --- gno.land/pkg/gnoclient/client_txs.go | 1 - 1 file changed, 1 deletion(-) diff --git a/gno.land/pkg/gnoclient/client_txs.go b/gno.land/pkg/gnoclient/client_txs.go index ac6d191dfc6..ff190fad614 100644 --- a/gno.land/pkg/gnoclient/client_txs.go +++ b/gno.land/pkg/gnoclient/client_txs.go @@ -28,7 +28,6 @@ type CallCfg struct { // MultiCallCfg contains configuration options for executing a contract call. type MultiCallCfg struct { - // Per MsgCall Msgs []MsgCall // Per Tx From de83a5ba1bd3936762ce9660dcdffb6b214ef93c Mon Sep 17 00:00:00 2001 From: leohhhn Date: Mon, 22 Jan 2024 00:20:54 +0100 Subject: [PATCH 04/38] add comment --- gno.land/pkg/gnoclient/client_txs.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gno.land/pkg/gnoclient/client_txs.go b/gno.land/pkg/gnoclient/client_txs.go index ff190fad614..58c0fb4129e 100644 --- a/gno.land/pkg/gnoclient/client_txs.go +++ b/gno.land/pkg/gnoclient/client_txs.go @@ -148,6 +148,7 @@ func (c *Client) MultiCall(cfg MultiCallCfg) (*ctypes.ResultBroadcastTxCommit, e } + // Cast vm.MsgCall back into std.Msg stdMsgs := make([]std.Msg, len(msgs)) for i, msg := range msgs { stdMsgs[i] = msg @@ -159,6 +160,7 @@ func (c *Client) MultiCall(cfg MultiCallCfg) (*ctypes.ResultBroadcastTxCommit, e return nil, errors.Wrap(err, "parsing gas fee coin") } + // Pack transaction tx := std.Tx{ Msgs: stdMsgs, Fee: std.NewFee(cfg.GasWanted, gasFeeCoins), From 05aedff1f7ff0797cb24bc3cbbc817c0ffad7da1 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Mon, 22 Jan 2024 00:24:56 +0100 Subject: [PATCH 05/38] fix lint --- gno.land/pkg/gnoclient/client_txs.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/gno.land/pkg/gnoclient/client_txs.go b/gno.land/pkg/gnoclient/client_txs.go index 58c0fb4129e..79c5181c9ed 100644 --- a/gno.land/pkg/gnoclient/client_txs.go +++ b/gno.land/pkg/gnoclient/client_txs.go @@ -111,7 +111,7 @@ func (c *Client) MultiCall(cfg MultiCallCfg) (*ctypes.ResultBroadcastTxCommit, e sequenceNumber := cfg.SequenceNumber accountNumber := cfg.AccountNumber - var msgs []vm.MsgCall + msgs := make([]vm.MsgCall, 0, len(cfg.Msgs)) for _, msg := range cfg.Msgs { pkgPath := msg.PkgPath funcName := msg.FuncName @@ -145,7 +145,6 @@ func (c *Client) MultiCall(cfg MultiCallCfg) (*ctypes.ResultBroadcastTxCommit, e Func: funcName, Args: args, }) - } // Cast vm.MsgCall back into std.Msg From ebaf516a36fc8c93705746c7feeea5126e0c439a Mon Sep 17 00:00:00 2001 From: leohhhn Date: Mon, 22 Jan 2024 14:56:27 +0100 Subject: [PATCH 06/38] remove multicall func, update struct, remove aliasing --- gno.land/pkg/gnoclient/client_txs.go | 91 +++------------------------- 1 file changed, 7 insertions(+), 84 deletions(-) diff --git a/gno.land/pkg/gnoclient/client_txs.go b/gno.land/pkg/gnoclient/client_txs.go index 79c5181c9ed..a2697008b9f 100644 --- a/gno.land/pkg/gnoclient/client_txs.go +++ b/gno.land/pkg/gnoclient/client_txs.go @@ -18,19 +18,7 @@ type MsgCall struct { // CallCfg contains configuration options for executing a contract call. type CallCfg struct { - MsgCall - GasFee string // Gas fee - GasWanted int64 // Gas wanted - AccountNumber uint64 // Account number - SequenceNumber uint64 // Sequence number - Memo string // Memo -} - -// MultiCallCfg contains configuration options for executing a contract call. -type MultiCallCfg struct { - Msgs []MsgCall - - // Per Tx + Msgs []MsgCall GasFee string // Gas fee GasWanted int64 // Gas wanted AccountNumber uint64 // Account number @@ -48,86 +36,21 @@ func (c *Client) Call(cfg CallCfg) (*ctypes.ResultBroadcastTxCommit, error) { return nil, errors.Wrap(err, "validate RPC client") } - pkgPath := cfg.PkgPath - funcName := cfg.FuncName - args := cfg.Args - gasWanted := cfg.GasWanted - gasFee := cfg.GasFee - send := cfg.Send - sequenceNumber := cfg.SequenceNumber - accountNumber := cfg.AccountNumber - memo := cfg.Memo - - // Validate config. - if pkgPath == "" { - return nil, errors.New("missing PkgPath") - } - if funcName == "" { - return nil, errors.New("missing FuncName") - } - - // Parse send amount. - sendCoins, err := std.ParseCoins(send) - if err != nil { - return nil, errors.Wrap(err, "parsing send coins") - } - - // Parse gas wanted & fee. - gasFeeCoins, err := std.ParseCoin(gasFee) - if err != nil { - return nil, errors.Wrap(err, "parsing gas fee coin") - } - - caller := c.Signer.Info().GetAddress() - - // Construct message & transaction and marshal. - msg := vm.MsgCall{ - Caller: caller, - Send: sendCoins, - PkgPath: pkgPath, - Func: funcName, - Args: args, - } - tx := std.Tx{ - Msgs: []std.Msg{msg}, - Fee: std.NewFee(gasWanted, gasFeeCoins), - Signatures: nil, - Memo: memo, - } - - return c.signAndBroadcastTxCommit(tx, accountNumber, sequenceNumber) -} - -// MultiCall executes a contract call on the blockchain. -func (c *Client) MultiCall(cfg MultiCallCfg) (*ctypes.ResultBroadcastTxCommit, error) { - // Validate required client fields. - if err := c.validateSigner(); err != nil { - return nil, errors.Wrap(err, "validate signer") - } - if err := c.validateRPCClient(); err != nil { - return nil, errors.Wrap(err, "validate RPC client") - } - sequenceNumber := cfg.SequenceNumber accountNumber := cfg.AccountNumber msgs := make([]vm.MsgCall, 0, len(cfg.Msgs)) for _, msg := range cfg.Msgs { - pkgPath := msg.PkgPath - funcName := msg.FuncName - args := msg.Args - send := msg.Send - // Validate config. - if pkgPath == "" { + if msg.PkgPath == "" { return nil, errors.New("missing PkgPath") } - if funcName == "" { + if msg.FuncName == "" { return nil, errors.New("missing FuncName") } // Parse send amount. - sendCoins, err := std.ParseCoins(send) + sendCoins, err := std.ParseCoins(msg.Send) if err != nil { return nil, errors.Wrap(err, "parsing send coins") } @@ -141,9 +64,9 @@ func (c *Client) MultiCall(cfg MultiCallCfg) (*ctypes.ResultBroadcastTxCommit, e msgs = append(msgs, vm.MsgCall{ Caller: caller, Send: sendCoins, - PkgPath: pkgPath, - Func: funcName, - Args: args, + PkgPath: msg.PkgPath, + Func: msg.FuncName, + Args: msg.Args, }) } From 9a5fd71c6251e16887da7dbcc7ef4d6c04e98993 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Mon, 22 Jan 2024 14:58:02 +0100 Subject: [PATCH 07/38] extract errors to pkg level --- gno.land/pkg/gnoclient/client_txs.go | 4 ++-- gno.land/pkg/gnoclient/errors.go | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 gno.land/pkg/gnoclient/errors.go diff --git a/gno.land/pkg/gnoclient/client_txs.go b/gno.land/pkg/gnoclient/client_txs.go index a2697008b9f..7dcc19d4c5a 100644 --- a/gno.land/pkg/gnoclient/client_txs.go +++ b/gno.land/pkg/gnoclient/client_txs.go @@ -43,10 +43,10 @@ func (c *Client) Call(cfg CallCfg) (*ctypes.ResultBroadcastTxCommit, error) { for _, msg := range cfg.Msgs { // Validate config. if msg.PkgPath == "" { - return nil, errors.New("missing PkgPath") + return nil, errInvalidPkgPath } if msg.FuncName == "" { - return nil, errors.New("missing FuncName") + return nil, errInvalidFuncName } // Parse send amount. diff --git a/gno.land/pkg/gnoclient/errors.go b/gno.land/pkg/gnoclient/errors.go new file mode 100644 index 00000000000..3b225118010 --- /dev/null +++ b/gno.land/pkg/gnoclient/errors.go @@ -0,0 +1,8 @@ +package gnoclient + +import "errors" + +var ( + errInvalidPkgPath = errors.New("invalid pkgpath") + errInvalidFuncName = errors.New("invalid function name") +) From bdb9f60560cf33fa562a07657dd85b91074b8272 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Mon, 22 Jan 2024 15:16:48 +0100 Subject: [PATCH 08/38] remove aliasing --- gno.land/pkg/gnoclient/client_txs.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/gno.land/pkg/gnoclient/client_txs.go b/gno.land/pkg/gnoclient/client_txs.go index 7dcc19d4c5a..b1f27f2ff45 100644 --- a/gno.land/pkg/gnoclient/client_txs.go +++ b/gno.land/pkg/gnoclient/client_txs.go @@ -36,9 +36,6 @@ func (c *Client) Call(cfg CallCfg) (*ctypes.ResultBroadcastTxCommit, error) { return nil, errors.Wrap(err, "validate RPC client") } - sequenceNumber := cfg.SequenceNumber - accountNumber := cfg.AccountNumber - msgs := make([]vm.MsgCall, 0, len(cfg.Msgs)) for _, msg := range cfg.Msgs { // Validate config. @@ -90,7 +87,7 @@ func (c *Client) Call(cfg CallCfg) (*ctypes.ResultBroadcastTxCommit, error) { Memo: "", } - return c.signAndBroadcastTxCommit(tx, accountNumber, sequenceNumber) + return c.signAndBroadcastTxCommit(tx, cfg.AccountNumber, cfg.SequenceNumber) } // signAndBroadcastTxCommit signs a transaction and broadcasts it, returning the result. From fb2416d49e5358a9f8cb7890d903eb524e4e6cc1 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Tue, 23 Jan 2024 00:36:58 +0100 Subject: [PATCH 09/38] add preliminary tests, remove some error wrapping --- gno.land/pkg/gnoclient/client.go | 5 +- gno.land/pkg/gnoclient/client_test.go | 104 ++++++++++++++++++++++++++ gno.land/pkg/gnoclient/client_txs.go | 4 +- gno.land/pkg/gnoclient/errors.go | 6 +- 4 files changed, 112 insertions(+), 7 deletions(-) diff --git a/gno.land/pkg/gnoclient/client.go b/gno.land/pkg/gnoclient/client.go index 2c43a5fa01d..a674a522523 100644 --- a/gno.land/pkg/gnoclient/client.go +++ b/gno.land/pkg/gnoclient/client.go @@ -2,7 +2,6 @@ package gnoclient import ( rpcclient "github.com/gnolang/gno/tm2/pkg/bft/rpc/client" - "github.com/gnolang/gno/tm2/pkg/errors" ) // Client provides an interface for interacting with the blockchain. @@ -14,7 +13,7 @@ type Client struct { // validateSigner checks that the signer is correctly configured. func (c Client) validateSigner() error { if c.Signer == nil { - return errors.New("missing Signer") + return errMissingSigner } return nil } @@ -22,7 +21,7 @@ func (c Client) validateSigner() error { // validateRPCClient checks that the RPCClient is correctly configured. func (c Client) validateRPCClient() error { if c.RPCClient == nil { - return errors.New("missing RPCClient") + return errMissingRPCClient } return nil } diff --git a/gno.land/pkg/gnoclient/client_test.go b/gno.land/pkg/gnoclient/client_test.go index 418a95aa997..05fdc7a3078 100644 --- a/gno.land/pkg/gnoclient/client_test.go +++ b/gno.land/pkg/gnoclient/client_test.go @@ -50,3 +50,107 @@ func TestClient_Request(t *testing.T) { // XXX: need more test } + +func TestClient_Call(t *testing.T) { + t.Parallel() + + config, _ := integration.TestingNodeConfig(t, gnoenv.RootDir()) + node, remoteAddr := integration.TestingInMemoryNode(t, log.NewNopLogger(), config) + defer node.Stop() + + signer := newInMemorySigner(t, config.TMConfig.ChainID()) + rpcClient := rpcclient.NewHTTP(remoteAddr, "/websocket") + + client := Client{ + Signer: signer, + RPCClient: rpcClient, + } + + cfg := CallCfg{ + Msgs: []MsgCall{ + { + PkgPath: "gno.land/r/demo/deep/very/deep", + FuncName: "Render", + Args: []string{""}, + Send: "", + }, + }, + GasFee: "1000000ugnot", + GasWanted: 8000000, + AccountNumber: 0, + SequenceNumber: 0, + } + + _, err := client.Call(cfg) + require.NoError(t, err) +} + +func TestClient_Call_Errors(t *testing.T) { + t.Parallel() + + config, _ := integration.TestingNodeConfig(t, gnoenv.RootDir()) + node, remoteAddr := integration.TestingInMemoryNode(t, log.NewNopLogger(), config) + defer node.Stop() + + signer := newInMemorySigner(t, config.TMConfig.ChainID()) + rpcClient := rpcclient.NewHTTP(remoteAddr, "/websocket") + + testCases := []struct { + name string + client Client + cfg CallCfg + expectedError error + }{ + { + name: "Invalid Signer", + client: Client{ + nil, + rpcClient, + }, + cfg: CallCfg{}, + expectedError: errMissingSigner, + }, + { + name: "Invalid RPCClient", + client: Client{ + signer, + nil, + }, + cfg: CallCfg{}, + expectedError: errMissingRPCClient, + }, + { + name: "Invalid PkgPath", + client: Client{ + signer, + rpcClient, + }, + cfg: CallCfg{ + Msgs: []MsgCall{ + {PkgPath: ""}, + }, + }, + expectedError: errInvalidPkgPath, + }, + { + name: "Invalid FuncName", + client: Client{ + signer, + rpcClient, + }, + cfg: CallCfg{ + Msgs: []MsgCall{ + {PkgPath: "random/path", FuncName: ""}, + }, + }, + expectedError: errInvalidFuncName, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + _, err := tc.client.Call(tc.cfg) + require.Equal(t, err, tc.expectedError) + }) + } +} diff --git a/gno.land/pkg/gnoclient/client_txs.go b/gno.land/pkg/gnoclient/client_txs.go index b1f27f2ff45..f0a41cb2081 100644 --- a/gno.land/pkg/gnoclient/client_txs.go +++ b/gno.land/pkg/gnoclient/client_txs.go @@ -30,10 +30,10 @@ type CallCfg struct { func (c *Client) Call(cfg CallCfg) (*ctypes.ResultBroadcastTxCommit, error) { // Validate required client fields. if err := c.validateSigner(); err != nil { - return nil, errors.Wrap(err, "validate signer") + return nil, err } if err := c.validateRPCClient(); err != nil { - return nil, errors.Wrap(err, "validate RPC client") + return nil, err } msgs := make([]vm.MsgCall, 0, len(cfg.Msgs)) diff --git a/gno.land/pkg/gnoclient/errors.go b/gno.land/pkg/gnoclient/errors.go index 3b225118010..08ad8c93be7 100644 --- a/gno.land/pkg/gnoclient/errors.go +++ b/gno.land/pkg/gnoclient/errors.go @@ -3,6 +3,8 @@ package gnoclient import "errors" var ( - errInvalidPkgPath = errors.New("invalid pkgpath") - errInvalidFuncName = errors.New("invalid function name") + errInvalidPkgPath = errors.New("invalid pkgpath") + errInvalidFuncName = errors.New("invalid function name") + errMissingSigner = errors.New("missing Signer") + errMissingRPCClient = errors.New("missing RPCClient") ) From ced737b4d07ebf1521ae5929c0f3b337d258a841 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Thu, 25 Jan 2024 13:48:21 +0100 Subject: [PATCH 10/38] inline caller --- gno.land/pkg/gnoclient/client_txs.go | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/gno.land/pkg/gnoclient/client_txs.go b/gno.land/pkg/gnoclient/client_txs.go index f0a41cb2081..47ba71225c0 100644 --- a/gno.land/pkg/gnoclient/client_txs.go +++ b/gno.land/pkg/gnoclient/client_txs.go @@ -52,14 +52,9 @@ func (c *Client) Call(cfg CallCfg) (*ctypes.ResultBroadcastTxCommit, error) { return nil, errors.Wrap(err, "parsing send coins") } - if err != nil { - return nil, errors.Wrap(err, "parsing gas fee coin") - } - - caller := c.Signer.Info().GetAddress() - + // Pack message msgs = append(msgs, vm.MsgCall{ - Caller: caller, + Caller: c.Signer.Info().GetAddress(), Send: sendCoins, PkgPath: msg.PkgPath, Func: msg.FuncName, From 9c912407b989c8bc3092163230b77a3590d2df61 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Thu, 25 Jan 2024 18:54:01 +0100 Subject: [PATCH 11/38] add baseTxCfg, modify Call flow --- gno.land/pkg/gnoclient/client_txs.go | 73 +++++++++++++++++----------- gno.land/pkg/gnoclient/errors.go | 10 ---- 2 files changed, 45 insertions(+), 38 deletions(-) delete mode 100644 gno.land/pkg/gnoclient/errors.go diff --git a/gno.land/pkg/gnoclient/client_txs.go b/gno.land/pkg/gnoclient/client_txs.go index 47ba71225c0..ae3fcbea9a9 100644 --- a/gno.land/pkg/gnoclient/client_txs.go +++ b/gno.land/pkg/gnoclient/client_txs.go @@ -1,6 +1,7 @@ package gnoclient import ( + "fmt" "github.com/gnolang/gno/gno.land/pkg/sdk/vm" "github.com/gnolang/gno/tm2/pkg/amino" ctypes "github.com/gnolang/gno/tm2/pkg/bft/rpc/core/types" @@ -8,6 +9,21 @@ import ( "github.com/gnolang/gno/tm2/pkg/std" ) +var ( + errInvalidPkgPath = errors.New("invalid pkgpath") + errInvalidFuncName = errors.New("invalid function name") + errMissingSigner = errors.New("missing Signer") + errMissingRPCClient = errors.New("missing RPCClient") +) + +type BaseTxCfg struct { + GasFee string // Gas fee + GasWanted int64 // Gas wanted + AccountNumber uint64 // Account number + SequenceNumber uint64 // Sequence number + Memo string // Memo +} + // MsgCall - syntax sugar for vm.MsgCall type MsgCall struct { PkgPath string // Package path @@ -16,18 +32,23 @@ type MsgCall struct { Send string // Send amount } -// CallCfg contains configuration options for executing a contract call. -type CallCfg struct { - Msgs []MsgCall - GasFee string // Gas fee - GasWanted int64 // Gas wanted - AccountNumber uint64 // Account number - SequenceNumber uint64 // Sequence number - Memo string // Memo +func (cfg BaseTxCfg) validateBaseTxConfig() error { + // todo implement + return nil +} + +func validateMsgCall(msg MsgCall) error { + if msg.PkgPath == "" { + return errInvalidPkgPath + } + if msg.FuncName == "" { + return errInvalidFuncName + } + return nil } // Call executes a contract call on the blockchain. -func (c *Client) Call(cfg CallCfg) (*ctypes.ResultBroadcastTxCommit, error) { +func (c *Client) Call(cfg BaseTxCfg, msgs ...MsgCall) (*ctypes.ResultBroadcastTxCommit, error) { // Validate required client fields. if err := c.validateSigner(); err != nil { return nil, err @@ -35,43 +56,39 @@ func (c *Client) Call(cfg CallCfg) (*ctypes.ResultBroadcastTxCommit, error) { if err := c.validateRPCClient(); err != nil { return nil, err } + if err := cfg.validateBaseTxConfig(); err != nil { + return nil, err + } - msgs := make([]vm.MsgCall, 0, len(cfg.Msgs)) - for _, msg := range cfg.Msgs { - // Validate config. - if msg.PkgPath == "" { - return nil, errInvalidPkgPath - } - if msg.FuncName == "" { - return nil, errInvalidFuncName + vmMsgs := make([]vm.MsgCall, len(msgs)) + for _, msg := range msgs { + if err := validateMsgCall(msg); err != nil { + return nil, err } - // Parse send amount. - sendCoins, err := std.ParseCoins(msg.Send) + send, err := std.ParseCoins(msg.Send) if err != nil { - return nil, errors.Wrap(err, "parsing send coins") + return nil, fmt.Errorf("%w", err) } - // Pack message - msgs = append(msgs, vm.MsgCall{ + vmMsgs = append(vmMsgs, vm.MsgCall{ Caller: c.Signer.Info().GetAddress(), - Send: sendCoins, PkgPath: msg.PkgPath, Func: msg.FuncName, - Args: msg.Args, + Send: send, }) } // Cast vm.MsgCall back into std.Msg - stdMsgs := make([]std.Msg, len(msgs)) - for i, msg := range msgs { + stdMsgs := make([]std.Msg, len(vmMsgs)) + for i, msg := range vmMsgs { stdMsgs[i] = msg } // Parse gas wanted & fee. gasFeeCoins, err := std.ParseCoin(cfg.GasFee) if err != nil { - return nil, errors.Wrap(err, "parsing gas fee coin") + return nil, fmt.Errorf("%w", err) } // Pack transaction @@ -79,7 +96,7 @@ func (c *Client) Call(cfg CallCfg) (*ctypes.ResultBroadcastTxCommit, error) { Msgs: stdMsgs, Fee: std.NewFee(cfg.GasWanted, gasFeeCoins), Signatures: nil, - Memo: "", + Memo: cfg.Memo, } return c.signAndBroadcastTxCommit(tx, cfg.AccountNumber, cfg.SequenceNumber) diff --git a/gno.land/pkg/gnoclient/errors.go b/gno.land/pkg/gnoclient/errors.go deleted file mode 100644 index 08ad8c93be7..00000000000 --- a/gno.land/pkg/gnoclient/errors.go +++ /dev/null @@ -1,10 +0,0 @@ -package gnoclient - -import "errors" - -var ( - errInvalidPkgPath = errors.New("invalid pkgpath") - errInvalidFuncName = errors.New("invalid function name") - errMissingSigner = errors.New("missing Signer") - errMissingRPCClient = errors.New("missing RPCClient") -) From c9e85ea131c1ca87727d71974e214ebaf6a582cd Mon Sep 17 00:00:00 2001 From: leohhhn Date: Thu, 25 Jan 2024 19:07:03 +0100 Subject: [PATCH 12/38] add basecfg validation, comments, errors --- gno.land/pkg/gnoclient/client_txs.go | 29 ++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/gno.land/pkg/gnoclient/client_txs.go b/gno.land/pkg/gnoclient/client_txs.go index ae3fcbea9a9..836e842ef4a 100644 --- a/gno.land/pkg/gnoclient/client_txs.go +++ b/gno.land/pkg/gnoclient/client_txs.go @@ -10,10 +10,12 @@ import ( ) var ( - errInvalidPkgPath = errors.New("invalid pkgpath") - errInvalidFuncName = errors.New("invalid function name") - errMissingSigner = errors.New("missing Signer") - errMissingRPCClient = errors.New("missing RPCClient") + errInvalidPkgPath = errors.New("invalid pkgpath") + errInvalidFuncName = errors.New("invalid function name") + errInvalidGasWanted = errors.New("invalid gas-wanted") + errInvalidAccOrSeqNum = errors.New("invalid account or sequence number") + errMissingSigner = errors.New("missing Signer") + errMissingRPCClient = errors.New("missing RPCClient") ) type BaseTxCfg struct { @@ -33,11 +35,16 @@ type MsgCall struct { } func (cfg BaseTxCfg) validateBaseTxConfig() error { - // todo implement + if cfg.GasWanted < 0 { + return errInvalidGasWanted + } + if cfg.AccountNumber < 0 || cfg.SequenceNumber < 0 { + return errInvalidAccOrSeqNum + } return nil } -func validateMsgCall(msg MsgCall) error { +func (msg MsgCall) validateMsgCall() error { if msg.PkgPath == "" { return errInvalidPkgPath } @@ -56,21 +63,27 @@ func (c *Client) Call(cfg BaseTxCfg, msgs ...MsgCall) (*ctypes.ResultBroadcastTx if err := c.validateRPCClient(); err != nil { return nil, err } + + // Validate base transaction config if err := cfg.validateBaseTxConfig(); err != nil { return nil, err } + // Parse MsgCall slice vmMsgs := make([]vm.MsgCall, len(msgs)) for _, msg := range msgs { - if err := validateMsgCall(msg); err != nil { + // Validate MsgCall fields + if err := msg.validateMsgCall(); err != nil { return nil, err } + // Parse send coins send, err := std.ParseCoins(msg.Send) if err != nil { return nil, fmt.Errorf("%w", err) } + // Create vmMsgs = append(vmMsgs, vm.MsgCall{ Caller: c.Signer.Info().GetAddress(), PkgPath: msg.PkgPath, @@ -85,7 +98,7 @@ func (c *Client) Call(cfg BaseTxCfg, msgs ...MsgCall) (*ctypes.ResultBroadcastTx stdMsgs[i] = msg } - // Parse gas wanted & fee. + // Parse gas fee gasFeeCoins, err := std.ParseCoin(cfg.GasFee) if err != nil { return nil, fmt.Errorf("%w", err) From bb828dff2d402d598ad2ae16e0fbbad4dfaffb5e Mon Sep 17 00:00:00 2001 From: leohhhn Date: Thu, 25 Jan 2024 20:38:42 +0100 Subject: [PATCH 13/38] add more unit tests, comments --- gno.land/pkg/gnoclient/client_test.go | 64 ++++++++++++++++++++++----- gno.land/pkg/gnoclient/client_txs.go | 19 ++++---- 2 files changed, 63 insertions(+), 20 deletions(-) diff --git a/gno.land/pkg/gnoclient/client_test.go b/gno.land/pkg/gnoclient/client_test.go index 05fdc7a3078..1b9f2bc171d 100644 --- a/gno.land/pkg/gnoclient/client_test.go +++ b/gno.land/pkg/gnoclient/client_test.go @@ -1,6 +1,7 @@ package gnoclient import ( + "github.com/jaekwon/testify/assert" "testing" "github.com/gnolang/gno/gno.land/pkg/integration" @@ -88,6 +89,7 @@ func TestClient_Call(t *testing.T) { func TestClient_Call_Errors(t *testing.T) { t.Parallel() + // todo Replace with mock client config, _ := integration.TestingNodeConfig(t, gnoenv.RootDir()) node, remoteAddr := integration.TestingInMemoryNode(t, log.NewNopLogger(), config) defer node.Stop() @@ -98,7 +100,8 @@ func TestClient_Call_Errors(t *testing.T) { testCases := []struct { name string client Client - cfg CallCfg + cfg BaseTxCfg + msgs []MsgCall expectedError error }{ { @@ -107,7 +110,7 @@ func TestClient_Call_Errors(t *testing.T) { nil, rpcClient, }, - cfg: CallCfg{}, + cfg: BaseTxCfg{}, expectedError: errMissingSigner, }, { @@ -116,18 +119,52 @@ func TestClient_Call_Errors(t *testing.T) { signer, nil, }, - cfg: CallCfg{}, + cfg: BaseTxCfg{}, expectedError: errMissingRPCClient, }, + { + name: "Invalid Gas Fee", + client: Client{ + signer, + rpcClient, + }, + cfg: BaseTxCfg{ + GasFee: "", + }, + expectedError: errInvalidGasFee, + }, + { + name: "Negative Gas Wanted", + client: Client{ + signer, + rpcClient, + }, + cfg: BaseTxCfg{ + GasWanted: -20, + }, + expectedError: errInvalidGasWanted, + }, + { + name: "0 Gas Wanted", + client: Client{ + signer, + rpcClient, + }, + cfg: BaseTxCfg{ + GasWanted: 0, + }, + expectedError: errInvalidGasWanted, + }, { name: "Invalid PkgPath", client: Client{ signer, rpcClient, }, - cfg: CallCfg{ - Msgs: []MsgCall{ - {PkgPath: ""}, + cfg: BaseTxCfg{}, + msgs: []MsgCall{ + { + PkgPath: "", }, }, expectedError: errInvalidPkgPath, @@ -138,9 +175,11 @@ func TestClient_Call_Errors(t *testing.T) { signer, rpcClient, }, - cfg: CallCfg{ - Msgs: []MsgCall{ - {PkgPath: "random/path", FuncName: ""}, + cfg: BaseTxCfg{}, + msgs: []MsgCall{ + { + PkgPath: "random/path", + FuncName: "", }, }, expectedError: errInvalidFuncName, @@ -149,8 +188,11 @@ func TestClient_Call_Errors(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - _, err := tc.client.Call(tc.cfg) - require.Equal(t, err, tc.expectedError) + t.Parallel() + + res, err := tc.client.Call(tc.cfg) + assert.Equal(t, err, tc.expectedError) + assert.NotNil(t, res) }) } } diff --git a/gno.land/pkg/gnoclient/client_txs.go b/gno.land/pkg/gnoclient/client_txs.go index 836e842ef4a..25930635ab8 100644 --- a/gno.land/pkg/gnoclient/client_txs.go +++ b/gno.land/pkg/gnoclient/client_txs.go @@ -10,12 +10,12 @@ import ( ) var ( - errInvalidPkgPath = errors.New("invalid pkgpath") - errInvalidFuncName = errors.New("invalid function name") - errInvalidGasWanted = errors.New("invalid gas-wanted") - errInvalidAccOrSeqNum = errors.New("invalid account or sequence number") - errMissingSigner = errors.New("missing Signer") - errMissingRPCClient = errors.New("missing RPCClient") + errInvalidPkgPath = errors.New("invalid pkgpath") + errInvalidFuncName = errors.New("invalid function name") + errInvalidGasWanted = errors.New("invalid gas wanted") + errInvalidGasFee = errors.New("invalid gas fee") + errMissingSigner = errors.New("missing Signer") + errMissingRPCClient = errors.New("missing RPCClient") ) type BaseTxCfg struct { @@ -38,9 +38,10 @@ func (cfg BaseTxCfg) validateBaseTxConfig() error { if cfg.GasWanted < 0 { return errInvalidGasWanted } - if cfg.AccountNumber < 0 || cfg.SequenceNumber < 0 { - return errInvalidAccOrSeqNum + if cfg.GasFee < "" { + return errInvalidGasFee } + return nil } @@ -83,7 +84,7 @@ func (c *Client) Call(cfg BaseTxCfg, msgs ...MsgCall) (*ctypes.ResultBroadcastTx return nil, fmt.Errorf("%w", err) } - // Create + // Unwrap syntax sugar to vm.MsgCall slice vmMsgs = append(vmMsgs, vm.MsgCall{ Caller: c.Signer.Info().GetAddress(), PkgPath: msg.PkgPath, From 175bd5ed79d47f4beeda644a8fb0cf550ad704e3 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Thu, 25 Jan 2024 21:06:00 +0100 Subject: [PATCH 14/38] made unit tests better --- gno.land/pkg/gnoclient/client_test.go | 130 +++++++++++++++++++++----- 1 file changed, 105 insertions(+), 25 deletions(-) diff --git a/gno.land/pkg/gnoclient/client_test.go b/gno.land/pkg/gnoclient/client_test.go index 1b9f2bc171d..7788f31a002 100644 --- a/gno.land/pkg/gnoclient/client_test.go +++ b/gno.land/pkg/gnoclient/client_test.go @@ -67,23 +67,24 @@ func TestClient_Call(t *testing.T) { RPCClient: rpcClient, } - cfg := CallCfg{ - Msgs: []MsgCall{ - { - PkgPath: "gno.land/r/demo/deep/very/deep", - FuncName: "Render", - Args: []string{""}, - Send: "", - }, - }, - GasFee: "1000000ugnot", - GasWanted: 8000000, - AccountNumber: 0, - SequenceNumber: 0, + cfg := BaseTxCfg{ + GasWanted: 100000, + GasFee: "10000ugnot", + AccountNumber: 1, + SequenceNumber: 1, + Memo: "Test memo", } - _, err := client.Call(cfg) - require.NoError(t, err) + msg := MsgCall{ + PkgPath: "gno.land/r/demo/deep/very/deep", + FuncName: "Render", + Args: []string{""}, + Send: "100ugnot", + } + + res, err := client.Call(cfg, msg) + assert.NoError(t, err) + assert.NotNil(t, res) } func TestClient_Call_Errors(t *testing.T) { @@ -110,7 +111,21 @@ func TestClient_Call_Errors(t *testing.T) { nil, rpcClient, }, - cfg: BaseTxCfg{}, + cfg: BaseTxCfg{ + GasWanted: 100000, + GasFee: "10000ugnot", + AccountNumber: 1, + SequenceNumber: 1, + Memo: "Test memo", + }, + msgs: []MsgCall{ + { + PkgPath: "random/path", + FuncName: "RandomName", + Send: "", + Args: []string{}, + }, + }, expectedError: errMissingSigner, }, { @@ -119,7 +134,21 @@ func TestClient_Call_Errors(t *testing.T) { signer, nil, }, - cfg: BaseTxCfg{}, + cfg: BaseTxCfg{ + GasWanted: 100000, + GasFee: "10000ugnot", + AccountNumber: 1, + SequenceNumber: 1, + Memo: "Test memo", + }, + msgs: []MsgCall{ + { + PkgPath: "random/path", + FuncName: "RandomName", + Send: "", + Args: []string{}, + }, + }, expectedError: errMissingRPCClient, }, { @@ -129,7 +158,17 @@ func TestClient_Call_Errors(t *testing.T) { rpcClient, }, cfg: BaseTxCfg{ - GasFee: "", + GasWanted: 100000, + GasFee: "", + AccountNumber: 1, + SequenceNumber: 1, + Memo: "Test memo", + }, + msgs: []MsgCall{ + { + PkgPath: "random/path", + FuncName: "RandomName", + }, }, expectedError: errInvalidGasFee, }, @@ -140,7 +179,19 @@ func TestClient_Call_Errors(t *testing.T) { rpcClient, }, cfg: BaseTxCfg{ - GasWanted: -20, + GasWanted: -1, + GasFee: "10000ugnot", + AccountNumber: 1, + SequenceNumber: 1, + Memo: "Test memo", + }, + msgs: []MsgCall{ + { + PkgPath: "random/path", + FuncName: "RandomName", + Send: "", + Args: []string{}, + }, }, expectedError: errInvalidGasWanted, }, @@ -151,7 +202,19 @@ func TestClient_Call_Errors(t *testing.T) { rpcClient, }, cfg: BaseTxCfg{ - GasWanted: 0, + GasWanted: 0, + GasFee: "10000ugnot", + AccountNumber: 1, + SequenceNumber: 1, + Memo: "Test memo", + }, + msgs: []MsgCall{ + { + PkgPath: "random/path", + FuncName: "RandomName", + Send: "", + Args: []string{}, + }, }, expectedError: errInvalidGasWanted, }, @@ -161,10 +224,19 @@ func TestClient_Call_Errors(t *testing.T) { signer, rpcClient, }, - cfg: BaseTxCfg{}, + cfg: BaseTxCfg{ + GasWanted: 100000, + GasFee: "10000ugnot", + AccountNumber: 1, + SequenceNumber: 1, + Memo: "Test memo", + }, msgs: []MsgCall{ { - PkgPath: "", + PkgPath: "", + FuncName: "RandomName", + Send: "", + Args: []string{}, }, }, expectedError: errInvalidPkgPath, @@ -175,11 +247,19 @@ func TestClient_Call_Errors(t *testing.T) { signer, rpcClient, }, - cfg: BaseTxCfg{}, + cfg: BaseTxCfg{ + GasWanted: 100000, + GasFee: "10000ugnot", + AccountNumber: 1, + SequenceNumber: 1, + Memo: "Test memo", + }, msgs: []MsgCall{ { PkgPath: "random/path", FuncName: "", + Send: "", + Args: []string{}, }, }, expectedError: errInvalidFuncName, @@ -190,9 +270,9 @@ func TestClient_Call_Errors(t *testing.T) { t.Run(tc.name, func(t *testing.T) { t.Parallel() - res, err := tc.client.Call(tc.cfg) + res, err := tc.client.Call(tc.cfg, tc.msgs...) assert.Equal(t, err, tc.expectedError) - assert.NotNil(t, res) + assert.Nil(t, res) }) } } From f0958d836f3e425ca1ae17ab51dec56198d17690 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Thu, 25 Jan 2024 23:20:42 +0100 Subject: [PATCH 15/38] add testcase --- gno.land/pkg/gnoclient/client_test.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/gno.land/pkg/gnoclient/client_test.go b/gno.land/pkg/gnoclient/client_test.go index 7788f31a002..a229ab2fad1 100644 --- a/gno.land/pkg/gnoclient/client_test.go +++ b/gno.land/pkg/gnoclient/client_test.go @@ -75,14 +75,16 @@ func TestClient_Call(t *testing.T) { Memo: "Test memo", } - msg := MsgCall{ - PkgPath: "gno.land/r/demo/deep/very/deep", - FuncName: "Render", - Args: []string{""}, - Send: "100ugnot", + msg := []MsgCall{ + { + PkgPath: "gno.land/r/demo/deep/very/deep", + FuncName: "Render", + Args: []string{""}, + Send: "100ugnot", + }, } - res, err := client.Call(cfg, msg) + res, err := client.Call(cfg, msg...) assert.NoError(t, err) assert.NotNil(t, res) } From a6fb52dc76007cce201cddfa306d701473d1eadb Mon Sep 17 00:00:00 2001 From: leohhhn Date: Mon, 29 Jan 2024 11:23:47 +0100 Subject: [PATCH 16/38] make errors public --- gno.land/pkg/gnoclient/client.go | 4 ++-- gno.land/pkg/gnoclient/client_test.go | 14 +++++++------- gno.land/pkg/gnoclient/client_txs.go | 20 ++++++++++---------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/gno.land/pkg/gnoclient/client.go b/gno.land/pkg/gnoclient/client.go index a674a522523..0a6918999a6 100644 --- a/gno.land/pkg/gnoclient/client.go +++ b/gno.land/pkg/gnoclient/client.go @@ -13,7 +13,7 @@ type Client struct { // validateSigner checks that the signer is correctly configured. func (c Client) validateSigner() error { if c.Signer == nil { - return errMissingSigner + return ErrMissingSigner } return nil } @@ -21,7 +21,7 @@ func (c Client) validateSigner() error { // validateRPCClient checks that the RPCClient is correctly configured. func (c Client) validateRPCClient() error { if c.RPCClient == nil { - return errMissingRPCClient + return ErrMissingRPCClient } return nil } diff --git a/gno.land/pkg/gnoclient/client_test.go b/gno.land/pkg/gnoclient/client_test.go index a229ab2fad1..7a32c0bfba4 100644 --- a/gno.land/pkg/gnoclient/client_test.go +++ b/gno.land/pkg/gnoclient/client_test.go @@ -128,7 +128,7 @@ func TestClient_Call_Errors(t *testing.T) { Args: []string{}, }, }, - expectedError: errMissingSigner, + expectedError: ErrMissingSigner, }, { name: "Invalid RPCClient", @@ -151,7 +151,7 @@ func TestClient_Call_Errors(t *testing.T) { Args: []string{}, }, }, - expectedError: errMissingRPCClient, + expectedError: ErrMissingRPCClient, }, { name: "Invalid Gas Fee", @@ -172,7 +172,7 @@ func TestClient_Call_Errors(t *testing.T) { FuncName: "RandomName", }, }, - expectedError: errInvalidGasFee, + expectedError: ErrInvalidGasFee, }, { name: "Negative Gas Wanted", @@ -195,7 +195,7 @@ func TestClient_Call_Errors(t *testing.T) { Args: []string{}, }, }, - expectedError: errInvalidGasWanted, + expectedError: ErrInvalidGasWanted, }, { name: "0 Gas Wanted", @@ -218,7 +218,7 @@ func TestClient_Call_Errors(t *testing.T) { Args: []string{}, }, }, - expectedError: errInvalidGasWanted, + expectedError: ErrInvalidGasWanted, }, { name: "Invalid PkgPath", @@ -241,7 +241,7 @@ func TestClient_Call_Errors(t *testing.T) { Args: []string{}, }, }, - expectedError: errInvalidPkgPath, + expectedError: ErrInvalidPkgPath, }, { name: "Invalid FuncName", @@ -264,7 +264,7 @@ func TestClient_Call_Errors(t *testing.T) { Args: []string{}, }, }, - expectedError: errInvalidFuncName, + expectedError: ErrInvalidFuncName, }, } diff --git a/gno.land/pkg/gnoclient/client_txs.go b/gno.land/pkg/gnoclient/client_txs.go index 4310a977fb0..f4ead13b1e2 100644 --- a/gno.land/pkg/gnoclient/client_txs.go +++ b/gno.land/pkg/gnoclient/client_txs.go @@ -11,12 +11,12 @@ import ( ) var ( - errInvalidPkgPath = errors.New("invalid pkgpath") - errInvalidFuncName = errors.New("invalid function name") - errInvalidGasWanted = errors.New("invalid gas wanted") - errInvalidGasFee = errors.New("invalid gas fee") - errMissingSigner = errors.New("missing Signer") - errMissingRPCClient = errors.New("missing RPCClient") + ErrInvalidPkgPath = errors.New("invalid pkgpath") + ErrInvalidFuncName = errors.New("invalid function name") + ErrInvalidGasWanted = errors.New("invalid gas wanted") + ErrInvalidGasFee = errors.New("invalid gas fee") + ErrMissingSigner = errors.New("missing Signer") + ErrMissingRPCClient = errors.New("missing RPCClient") ) type BaseTxCfg struct { @@ -37,10 +37,10 @@ type MsgCall struct { func (cfg BaseTxCfg) validateBaseTxConfig() error { if cfg.GasWanted < 0 { - return errInvalidGasWanted + return ErrInvalidGasWanted } if cfg.GasFee < "" { - return errInvalidGasFee + return ErrInvalidGasFee } return nil @@ -48,10 +48,10 @@ func (cfg BaseTxCfg) validateBaseTxConfig() error { func (msg MsgCall) validateMsgCall() error { if msg.PkgPath == "" { - return errInvalidPkgPath + return ErrInvalidPkgPath } if msg.FuncName == "" { - return errInvalidFuncName + return ErrInvalidFuncName } return nil } From 8be378eb932dec25b3f949d330446dbad7fb9def Mon Sep 17 00:00:00 2001 From: leohhhn Date: Mon, 29 Jan 2024 11:35:19 +0100 Subject: [PATCH 17/38] remove fmt.errorf --- gno.land/pkg/gnoclient/client_txs.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/gno.land/pkg/gnoclient/client_txs.go b/gno.land/pkg/gnoclient/client_txs.go index f4ead13b1e2..ce613be2665 100644 --- a/gno.land/pkg/gnoclient/client_txs.go +++ b/gno.land/pkg/gnoclient/client_txs.go @@ -1,7 +1,6 @@ package gnoclient import ( - "fmt" "github.com/gnolang/gno/gno.land/pkg/sdk/vm" gno "github.com/gnolang/gno/gnovm/pkg/gnolang" "github.com/gnolang/gno/tm2/pkg/amino" @@ -92,7 +91,7 @@ func (c *Client) Call(cfg BaseTxCfg, msgs ...MsgCall) (*ctypes.ResultBroadcastTx // Parse send coins send, err := std.ParseCoins(msg.Send) if err != nil { - return nil, fmt.Errorf("%w", err) + return nil, err } // Unwrap syntax sugar to vm.MsgCall slice @@ -113,7 +112,7 @@ func (c *Client) Call(cfg BaseTxCfg, msgs ...MsgCall) (*ctypes.ResultBroadcastTx // Parse gas fee gasFeeCoins, err := std.ParseCoin(cfg.GasFee) if err != nil { - return nil, fmt.Errorf("%w", err) + return nil, err } // Pack transaction From def2e0b206ed84cb9ed1aafb9bacf533fcccc393 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Mon, 29 Jan 2024 11:42:49 +0100 Subject: [PATCH 18/38] make error names clearer --- gno.land/pkg/gnoclient/client_test.go | 4 ++-- gno.land/pkg/gnoclient/client_txs.go | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/gno.land/pkg/gnoclient/client_test.go b/gno.land/pkg/gnoclient/client_test.go index 7a32c0bfba4..339641a87a7 100644 --- a/gno.land/pkg/gnoclient/client_test.go +++ b/gno.land/pkg/gnoclient/client_test.go @@ -241,7 +241,7 @@ func TestClient_Call_Errors(t *testing.T) { Args: []string{}, }, }, - expectedError: ErrInvalidPkgPath, + expectedError: ErrEmptyPkgPath, }, { name: "Invalid FuncName", @@ -264,7 +264,7 @@ func TestClient_Call_Errors(t *testing.T) { Args: []string{}, }, }, - expectedError: ErrInvalidFuncName, + expectedError: ErrEmptyFuncName, }, } diff --git a/gno.land/pkg/gnoclient/client_txs.go b/gno.land/pkg/gnoclient/client_txs.go index ce613be2665..16dde0a8dd2 100644 --- a/gno.land/pkg/gnoclient/client_txs.go +++ b/gno.land/pkg/gnoclient/client_txs.go @@ -10,8 +10,8 @@ import ( ) var ( - ErrInvalidPkgPath = errors.New("invalid pkgpath") - ErrInvalidFuncName = errors.New("invalid function name") + ErrEmptyPkgPath = errors.New("empty pkgpath") + ErrEmptyFuncName = errors.New("empty function name") ErrInvalidGasWanted = errors.New("invalid gas wanted") ErrInvalidGasFee = errors.New("invalid gas fee") ErrMissingSigner = errors.New("missing Signer") @@ -47,10 +47,10 @@ func (cfg BaseTxCfg) validateBaseTxConfig() error { func (msg MsgCall) validateMsgCall() error { if msg.PkgPath == "" { - return ErrInvalidPkgPath + return ErrEmptyPkgPath } if msg.FuncName == "" { - return ErrInvalidFuncName + return ErrEmptyFuncName } return nil } From 4f9febfd7f08f47eaebf23ea0b5eed9f659e44a3 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Mon, 29 Jan 2024 14:34:33 +0100 Subject: [PATCH 19/38] mocking wip --- gno.land/pkg/gnoclient/client_test.go | 28 +++++------ gno.land/pkg/gnoclient/mock.go | 68 +++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 16 deletions(-) create mode 100644 gno.land/pkg/gnoclient/mock.go diff --git a/gno.land/pkg/gnoclient/client_test.go b/gno.land/pkg/gnoclient/client_test.go index 339641a87a7..5f1515b3714 100644 --- a/gno.land/pkg/gnoclient/client_test.go +++ b/gno.land/pkg/gnoclient/client_test.go @@ -1,6 +1,7 @@ package gnoclient import ( + "github.com/gnolang/gno/tm2/pkg/std" "github.com/jaekwon/testify/assert" "testing" @@ -55,15 +56,17 @@ func TestClient_Request(t *testing.T) { func TestClient_Call(t *testing.T) { t.Parallel() - config, _ := integration.TestingNodeConfig(t, gnoenv.RootDir()) - node, remoteAddr := integration.TestingInMemoryNode(t, log.NewNopLogger(), config) - defer node.Stop() - - signer := newInMemorySigner(t, config.TMConfig.ChainID()) rpcClient := rpcclient.NewHTTP(remoteAddr, "/websocket") client := Client{ - Signer: signer, + Signer: &mockSigner{ + sign: func(cfg SignCfg) (*std.Tx, error) { + return &cfg.UnsignedTX, nil + }, + info: func() keys.Info { + return mockKeysInfo{} + }, + }, RPCClient: rpcClient, } @@ -89,17 +92,10 @@ func TestClient_Call(t *testing.T) { assert.NotNil(t, res) } + func TestClient_Call_Errors(t *testing.T) { t.Parallel() - // todo Replace with mock client - config, _ := integration.TestingNodeConfig(t, gnoenv.RootDir()) - node, remoteAddr := integration.TestingInMemoryNode(t, log.NewNopLogger(), config) - defer node.Stop() - - signer := newInMemorySigner(t, config.TMConfig.ChainID()) - rpcClient := rpcclient.NewHTTP(remoteAddr, "/websocket") - testCases := []struct { name string client Client @@ -111,7 +107,7 @@ func TestClient_Call_Errors(t *testing.T) { name: "Invalid Signer", client: Client{ nil, - rpcClient, + , }, cfg: BaseTxCfg{ GasWanted: 100000, @@ -133,7 +129,7 @@ func TestClient_Call_Errors(t *testing.T) { { name: "Invalid RPCClient", client: Client{ - signer, + &mockSigner{}, nil, }, cfg: BaseTxCfg{ diff --git a/gno.land/pkg/gnoclient/mock.go b/gno.land/pkg/gnoclient/mock.go new file mode 100644 index 00000000000..08a75e45b00 --- /dev/null +++ b/gno.land/pkg/gnoclient/mock.go @@ -0,0 +1,68 @@ +package gnoclient + +import ( + "github.com/gnolang/gno/tm2/pkg/crypto" + "github.com/gnolang/gno/tm2/pkg/crypto/hd" + "github.com/gnolang/gno/tm2/pkg/crypto/keys" + "github.com/gnolang/gno/tm2/pkg/std" +) + +type ( + mockSign func(cfg SignCfg) (*std.Tx, error) + mockInfo func() keys.Info + mockValidate func() error +) + +type mockSigner struct { + sign mockSign + info mockInfo + validate mockValidate +} + +func (m *mockSigner) Sign(cfg SignCfg) (*std.Tx, error) { + if m.sign != nil { + return m.sign(cfg) + } + + return nil, nil +} + +func (m *mockSigner) Info() keys.Info { + if m.info != nil { + return m.info() + } + + return nil +} + +func (m *mockSigner) Validate() error { + if m.validate != nil { + return m.validate() + } + + return nil +} + +type mockKeysInfo struct{} + +func (m mockKeysInfo) GetAddress() crypto.Address { + adr, _ := crypto.AddressFromBech32("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + return adr +} + +func (m mockKeysInfo) GetType() keys.KeyType { + return 0 +} + +func (m mockKeysInfo) GetName() string { + return "mockKeyInfoName" +} + +func (m mockKeysInfo) GetPubKey() crypto.PubKey { + pubkey, _ := crypto.PubKeyFromBech32("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + return pubkey +} + +func (m mockKeysInfo) GetPath() (*hd.BIP44Params, error) { + return nil, nil +} From cb7496f52f1b73ac5fa5ac4f70b296a520f9b94b Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 31 Jan 2024 15:11:18 +0100 Subject: [PATCH 20/38] add rpcclient mock, wip tests --- gno.land/pkg/gnoclient/client_test.go | 69 +++++++++-------- gno.land/pkg/gnoclient/client_txs.go | 23 +----- gno.land/pkg/gnoclient/mock.go | 104 ++++++++++++++++++++++++++ gno.land/pkg/gnoclient/util.go | 22 ++++++ 4 files changed, 164 insertions(+), 54 deletions(-) create mode 100644 gno.land/pkg/gnoclient/util.go diff --git a/gno.land/pkg/gnoclient/client_test.go b/gno.land/pkg/gnoclient/client_test.go index 5f1515b3714..394c69e2994 100644 --- a/gno.land/pkg/gnoclient/client_test.go +++ b/gno.land/pkg/gnoclient/client_test.go @@ -1,15 +1,14 @@ package gnoclient import ( + ctypes "github.com/gnolang/gno/tm2/pkg/bft/rpc/core/types" + "github.com/gnolang/gno/tm2/pkg/bft/types" "github.com/gnolang/gno/tm2/pkg/std" "github.com/jaekwon/testify/assert" "testing" "github.com/gnolang/gno/gno.land/pkg/integration" - "github.com/gnolang/gno/gnovm/pkg/gnoenv" - rpcclient "github.com/gnolang/gno/tm2/pkg/bft/rpc/client" "github.com/gnolang/gno/tm2/pkg/crypto/keys" - "github.com/gnolang/gno/tm2/pkg/log" "github.com/jaekwon/testify/require" ) @@ -32,15 +31,20 @@ func newInMemorySigner(t *testing.T, chainid string) *SignerFromKeybase { } func TestClient_Request(t *testing.T) { - config, _ := integration.TestingNodeConfig(t, gnoenv.RootDir()) - node, remoteAddr := integration.TestingInMemoryNode(t, log.NewNopLogger(), config) - defer node.Stop() - - signer := newInMemorySigner(t, config.TMConfig.ChainID()) - client := Client{ - Signer: signer, - RPCClient: rpcclient.NewHTTP(remoteAddr, "/websocket"), + Signer: &mockSigner{ + sign: func(cfg SignCfg) (*std.Tx, error) { + return &std.Tx{}, nil + }, + info: func() keys.Info { + return mockKeysInfo{} + }, + }, + RPCClient: mockRPCClient{ + broadcastTxCommit: func(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) { + return &ctypes.ResultBroadcastTxCommit{}, nil + }, + }, } data, res, err := client.Render("gno.land/r/demo/boards", "") @@ -56,18 +60,20 @@ func TestClient_Request(t *testing.T) { func TestClient_Call(t *testing.T) { t.Parallel() - rpcClient := rpcclient.NewHTTP(remoteAddr, "/websocket") - client := Client{ Signer: &mockSigner{ - sign: func(cfg SignCfg) (*std.Tx, error) { - return &cfg.UnsignedTX, nil - }, - info: func() keys.Info { - return mockKeysInfo{} + sign: func(cfg SignCfg) (*std.Tx, error) { + return &std.Tx{}, nil + }, + info: func() keys.Info { + return mockKeysInfo{} + }, }, + RPCClient: mockRPCClient{ + broadcastTxCommit: func(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) { + return &ctypes.ResultBroadcastTxCommit{}, nil + }, }, - RPCClient: rpcClient, } cfg := BaseTxCfg{ @@ -92,7 +98,6 @@ func TestClient_Call(t *testing.T) { assert.NotNil(t, res) } - func TestClient_Call_Errors(t *testing.T) { t.Parallel() @@ -106,8 +111,8 @@ func TestClient_Call_Errors(t *testing.T) { { name: "Invalid Signer", client: Client{ - nil, - , + Signer: nil, + RPCClient: &mockRPCClient{}, }, cfg: BaseTxCfg{ GasWanted: 100000, @@ -152,8 +157,8 @@ func TestClient_Call_Errors(t *testing.T) { { name: "Invalid Gas Fee", client: Client{ - signer, - rpcClient, + Signer: &mockSigner{}, + RPCClient: &mockRPCClient{}, }, cfg: BaseTxCfg{ GasWanted: 100000, @@ -173,8 +178,8 @@ func TestClient_Call_Errors(t *testing.T) { { name: "Negative Gas Wanted", client: Client{ - signer, - rpcClient, + Signer: &mockSigner{}, + RPCClient: &mockRPCClient{}, }, cfg: BaseTxCfg{ GasWanted: -1, @@ -196,8 +201,8 @@ func TestClient_Call_Errors(t *testing.T) { { name: "0 Gas Wanted", client: Client{ - signer, - rpcClient, + Signer: &mockSigner{}, + RPCClient: &mockRPCClient{}, }, cfg: BaseTxCfg{ GasWanted: 0, @@ -219,8 +224,8 @@ func TestClient_Call_Errors(t *testing.T) { { name: "Invalid PkgPath", client: Client{ - signer, - rpcClient, + Signer: &mockSigner{}, + RPCClient: &mockRPCClient{}, }, cfg: BaseTxCfg{ GasWanted: 100000, @@ -242,8 +247,8 @@ func TestClient_Call_Errors(t *testing.T) { { name: "Invalid FuncName", client: Client{ - signer, - rpcClient, + Signer: &mockSigner{}, + RPCClient: &mockRPCClient{}, }, cfg: BaseTxCfg{ GasWanted: 100000, diff --git a/gno.land/pkg/gnoclient/client_txs.go b/gno.land/pkg/gnoclient/client_txs.go index 16dde0a8dd2..73dfee472c2 100644 --- a/gno.land/pkg/gnoclient/client_txs.go +++ b/gno.land/pkg/gnoclient/client_txs.go @@ -10,7 +10,7 @@ import ( ) var ( - ErrEmptyPkgPath = errors.New("empty pkgpath") + ErrEmptyPkgPath = errors.New("empty pkg path") ErrEmptyFuncName = errors.New("empty function name") ErrInvalidGasWanted = errors.New("invalid gas wanted") ErrInvalidGasFee = errors.New("invalid gas fee") @@ -34,27 +34,6 @@ type MsgCall struct { Send string // Send amount } -func (cfg BaseTxCfg) validateBaseTxConfig() error { - if cfg.GasWanted < 0 { - return ErrInvalidGasWanted - } - if cfg.GasFee < "" { - return ErrInvalidGasFee - } - - return nil -} - -func (msg MsgCall) validateMsgCall() error { - if msg.PkgPath == "" { - return ErrEmptyPkgPath - } - if msg.FuncName == "" { - return ErrEmptyFuncName - } - return nil -} - // RunCfg contains configuration options for running a temporary package on the blockchain. type RunCfg struct { Package *std.MemPackage diff --git a/gno.land/pkg/gnoclient/mock.go b/gno.land/pkg/gnoclient/mock.go index 08a75e45b00..ae408b8a9d4 100644 --- a/gno.land/pkg/gnoclient/mock.go +++ b/gno.land/pkg/gnoclient/mock.go @@ -1,12 +1,16 @@ package gnoclient import ( + "github.com/gnolang/gno/tm2/pkg/bft/rpc/client" + ctypes "github.com/gnolang/gno/tm2/pkg/bft/rpc/core/types" + "github.com/gnolang/gno/tm2/pkg/bft/types" "github.com/gnolang/gno/tm2/pkg/crypto" "github.com/gnolang/gno/tm2/pkg/crypto/hd" "github.com/gnolang/gno/tm2/pkg/crypto/keys" "github.com/gnolang/gno/tm2/pkg/std" ) +// Signer type ( mockSign func(cfg SignCfg) (*std.Tx, error) mockInfo func() keys.Info @@ -66,3 +70,103 @@ func (m mockKeysInfo) GetPubKey() crypto.PubKey { func (m mockKeysInfo) GetPath() (*hd.BIP44Params, error) { return nil, nil } + +// RPC Client +type ( + mockBroadcastTxCommit func(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) + mockABCIQuery func(path string, data []byte) (*ctypes.ResultABCIQuery, error) +) + +type mockRPCClient struct { + broadcastTxCommit mockBroadcastTxCommit + abciQuery mockABCIQuery +} + +func (m mockRPCClient) BroadcastTxCommit(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) { + if m.broadcastTxCommit != nil { + return m.broadcastTxCommit(tx) + } + + return nil, nil +} + +func (m mockRPCClient) ABCIQuery(path string, data []byte) (*ctypes.ResultABCIQuery, error) { + if m.abciQuery != nil { + return m.abciQuery(path, data) + } + return nil, nil +} + +// Unused RPC Client functions + +func (m mockRPCClient) ABCIInfo() (*ctypes.ResultABCIInfo, error) { + panic("implement me") +} + +func (m mockRPCClient) ABCIQueryWithOptions(path string, data []byte, opts client.ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) { + panic("implement me") +} + +func (m mockRPCClient) BroadcastTxAsync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) { + panic("implement me") +} + +func (m mockRPCClient) BroadcastTxSync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) { + panic("implement me") +} + +func (m mockRPCClient) Genesis() (*ctypes.ResultGenesis, error) { + panic("implement me") +} + +func (m mockRPCClient) BlockchainInfo(minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, error) { + panic("implement me") +} + +func (m mockRPCClient) NetInfo() (*ctypes.ResultNetInfo, error) { + panic("implement me") +} + +func (m mockRPCClient) DumpConsensusState() (*ctypes.ResultDumpConsensusState, error) { + panic("implement me") +} + +func (m mockRPCClient) ConsensusState() (*ctypes.ResultConsensusState, error) { + panic("implement me") +} + +func (m mockRPCClient) ConsensusParams(height *int64) (*ctypes.ResultConsensusParams, error) { + panic("implement me") +} + +func (m mockRPCClient) Health() (*ctypes.ResultHealth, error) { + panic("implement me") +} + +func (m mockRPCClient) Block(height *int64) (*ctypes.ResultBlock, error) { + panic("implement me") +} + +func (m mockRPCClient) BlockResults(height *int64) (*ctypes.ResultBlockResults, error) { + panic("implement me") +} + +func (m mockRPCClient) Commit(height *int64) (*ctypes.ResultCommit, error) { + panic("implement me") +} + +func (m mockRPCClient) Validators(height *int64) (*ctypes.ResultValidators, error) { + panic("implement me") +} + +func (m mockRPCClient) Status() (*ctypes.ResultStatus, error) { + panic("implement me") +} + +func (m mockRPCClient) UnconfirmedTxs(limit int) (*ctypes.ResultUnconfirmedTxs, error) { + panic("implement me") +} + +func (m mockRPCClient) NumUnconfirmedTxs() (*ctypes.ResultUnconfirmedTxs, error) { + panic("implement me") +} diff --git a/gno.land/pkg/gnoclient/util.go b/gno.land/pkg/gnoclient/util.go new file mode 100644 index 00000000000..d9836fe04bb --- /dev/null +++ b/gno.land/pkg/gnoclient/util.go @@ -0,0 +1,22 @@ +package gnoclient + +func (cfg BaseTxCfg) validateBaseTxConfig() error { + if cfg.GasWanted < 0 { + return ErrInvalidGasWanted + } + if cfg.GasFee < "" { + return ErrInvalidGasFee + } + + return nil +} + +func (msg MsgCall) validateMsgCall() error { + if msg.PkgPath == "" { + return ErrEmptyPkgPath + } + if msg.FuncName == "" { + return ErrEmptyFuncName + } + return nil +} From 2cd4af379b5bcd031c01b53320b50b0d92f91caf Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 31 Jan 2024 15:17:22 +0100 Subject: [PATCH 21/38] expand txbroadcast res --- gno.land/pkg/gnoclient/client_test.go | 28 ++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/gno.land/pkg/gnoclient/client_test.go b/gno.land/pkg/gnoclient/client_test.go index 394c69e2994..948b55fe652 100644 --- a/gno.land/pkg/gnoclient/client_test.go +++ b/gno.land/pkg/gnoclient/client_test.go @@ -1,6 +1,7 @@ package gnoclient import ( + abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types" ctypes "github.com/gnolang/gno/tm2/pkg/bft/rpc/core/types" "github.com/gnolang/gno/tm2/pkg/bft/types" "github.com/gnolang/gno/tm2/pkg/std" @@ -71,7 +72,32 @@ func TestClient_Call(t *testing.T) { }, RPCClient: mockRPCClient{ broadcastTxCommit: func(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) { - return &ctypes.ResultBroadcastTxCommit{}, nil + res := &ctypes.ResultBroadcastTxCommit{ + CheckTx: abci.ResponseCheckTx{ + ResponseBase: abci.ResponseBase{ + Error: nil, + Data: nil, + Events: nil, + Log: "", + Info: "", + }, + }, + DeliverTx: abci.ResponseDeliverTx{ + ResponseBase: abci.ResponseBase{ + Error: nil, + Data: nil, + Events: nil, + Log: "", + Info: "", + }, + GasWanted: 0, + GasUsed: 0, + }, + Hash: nil, + Height: 0, + } + + return res, nil }, }, } From 9f6cd765aa0f23d0938734ed8f7af6df08722174 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 31 Jan 2024 16:17:05 +0100 Subject: [PATCH 22/38] wip more tests --- gno.land/pkg/gnoclient/client_test.go | 128 ++++++++++++++++++++++++-- 1 file changed, 122 insertions(+), 6 deletions(-) diff --git a/gno.land/pkg/gnoclient/client_test.go b/gno.land/pkg/gnoclient/client_test.go index 948b55fe652..d7c9db94480 100644 --- a/gno.land/pkg/gnoclient/client_test.go +++ b/gno.land/pkg/gnoclient/client_test.go @@ -43,22 +43,126 @@ func TestClient_Request(t *testing.T) { }, RPCClient: mockRPCClient{ broadcastTxCommit: func(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) { - return &ctypes.ResultBroadcastTxCommit{}, nil + res := &ctypes.ResultBroadcastTxCommit{ + CheckTx: abci.ResponseCheckTx{ + ResponseBase: abci.ResponseBase{ + Error: nil, + Data: nil, + Events: nil, + Log: "", + Info: "", + }, + }, + DeliverTx: abci.ResponseDeliverTx{ + ResponseBase: abci.ResponseBase{ + Error: nil, + Data: nil, + Events: nil, + Log: "", + Info: "", + }, + GasWanted: 0, + GasUsed: 0, + }, + Hash: nil, + Height: 0, + } + + return res, nil }, }, } - data, res, err := client.Render("gno.land/r/demo/boards", "") + res, data, err := client.Render("gno.land/r/demo/boards", "") require.NoError(t, err) require.NotEmpty(t, data) + require.NotEmpty(t, res) - require.NotNil(t, res) - require.NotEmpty(t, res.Response.Data) +} + +func TestClient_CallSingle(t *testing.T) { + t.Parallel() + + client := Client{ + Signer: &mockSigner{ + sign: func(cfg SignCfg) (*std.Tx, error) { + return &std.Tx{}, nil + }, + info: func() keys.Info { + return mockKeysInfo{} + }, + }, + RPCClient: mockRPCClient{ + broadcastTxCommit: func(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) { + res := &ctypes.ResultBroadcastTxCommit{ + CheckTx: abci.ResponseCheckTx{ + ResponseBase: abci.ResponseBase{ + Error: nil, + Data: nil, + Events: nil, + Log: "", + Info: "", + }, + }, + DeliverTx: abci.ResponseDeliverTx{ + ResponseBase: abci.ResponseBase{ + Error: nil, + Data: []byte("it works!"), + Events: nil, + Log: "", + Info: "", + }, + GasWanted: 0, + GasUsed: 0, + }, + Hash: nil, + Height: 0, + } + + return res, nil + }, + abciQuery: func(path string, data []byte) (*ctypes.ResultABCIQuery, error) { + res := &ctypes.ResultABCIQuery{ + Response: abci.ResponseQuery{ + ResponseBase: abci.ResponseBase{}, + Key: nil, + Value: nil, + Proof: nil, + Height: 0, + }, + } + + return res, nil + + }, + }, + } + + cfg := BaseTxCfg{ + GasWanted: 100000, + GasFee: "10000ugnot", + AccountNumber: 1, + SequenceNumber: 1, + Memo: "Test memo", + } + + msg := []MsgCall{ + { + PkgPath: "gno.land/r/demo/deep/very/deep", + FuncName: "Render", + Args: []string{""}, + Send: "100ugnot", + }, + } + + res, err := client.Call(cfg, msg...) + assert.NoError(t, err) + assert.NotNil(t, res) + assert.Equal(t, string(res.DeliverTx.Data), "it works!") - // XXX: need more test } -func TestClient_Call(t *testing.T) { +func TestClient_CallMultiple(t *testing.T) { t.Parallel() client := Client{ @@ -117,6 +221,18 @@ func TestClient_Call(t *testing.T) { Args: []string{""}, Send: "100ugnot", }, + { + PkgPath: "gno.land/r/demo/wugnot", + FuncName: "Deposit", + Args: []string{""}, + Send: "1000ugnot", + }, + { + PkgPath: "gno.land/r/demo/tamagotchi", + FuncName: "Feed", + Args: []string{}, + Send: "", + }, } res, err := client.Call(cfg, msg...) From 23fb5b4685239a83a9f2f0aed8eab818ee2a3d19 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 31 Jan 2024 17:09:09 +0100 Subject: [PATCH 23/38] add full rpcclient mock infra --- gno.land/pkg/gnoclient/mock.go | 139 +++++++++++++++++++++++++++------ 1 file changed, 113 insertions(+), 26 deletions(-) diff --git a/gno.land/pkg/gnoclient/mock.go b/gno.land/pkg/gnoclient/mock.go index ae408b8a9d4..787c2cd4a30 100644 --- a/gno.land/pkg/gnoclient/mock.go +++ b/gno.land/pkg/gnoclient/mock.go @@ -71,22 +71,57 @@ func (m mockKeysInfo) GetPath() (*hd.BIP44Params, error) { return nil, nil } -// RPC Client +// RPC Client mock type ( - mockBroadcastTxCommit func(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) - mockABCIQuery func(path string, data []byte) (*ctypes.ResultABCIQuery, error) + mockBroadcastTxCommit func(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) + mockABCIQuery func(path string, data []byte) (*ctypes.ResultABCIQuery, error) + mockABCIInfo func() (*ctypes.ResultABCIInfo, error) + mockABCIQueryWithOptions func(path string, data []byte, opts client.ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) + mockBroadcastTxAsync func(tx types.Tx) (*ctypes.ResultBroadcastTx, error) + mockBroadcastTxSync func(tx types.Tx) (*ctypes.ResultBroadcastTx, error) + mockGenesis func() (*ctypes.ResultGenesis, error) + mockBlockchainInfo func(minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, error) + mockNetInfo func() (*ctypes.ResultNetInfo, error) + mockDumpConsensusState func() (*ctypes.ResultDumpConsensusState, error) + mockConsensusState func() (*ctypes.ResultConsensusState, error) + mockConsensusParams func(height *int64) (*ctypes.ResultConsensusParams, error) + mockHealth func() (*ctypes.ResultHealth, error) + mockBlock func(height *int64) (*ctypes.ResultBlock, error) + mockBlockResults func(height *int64) (*ctypes.ResultBlockResults, error) + mockCommit func(height *int64) (*ctypes.ResultCommit, error) + mockValidators func(height *int64) (*ctypes.ResultValidators, error) + mockStatus func() (*ctypes.ResultStatus, error) + mockUnconfirmedTxs func(limit int) (*ctypes.ResultUnconfirmedTxs, error) + mockNumUnconfirmedTxs func() (*ctypes.ResultUnconfirmedTxs, error) ) type mockRPCClient struct { - broadcastTxCommit mockBroadcastTxCommit - abciQuery mockABCIQuery + broadcastTxCommit mockBroadcastTxCommit + abciQuery mockABCIQuery + abciInfo mockABCIInfo + abciQueryWithOptions mockABCIQueryWithOptions + broadcastTxAsync mockBroadcastTxAsync + broadcastTxSync mockBroadcastTxSync + genesis mockGenesis + blockchainInfo mockBlockchainInfo + netInfo mockNetInfo + dumpConsensusState mockDumpConsensusState + consensusState mockConsensusState + consensusParams mockConsensusParams + health mockHealth + block mockBlock + blockResults mockBlockResults + commit mockCommit + validators mockValidators + status mockStatus + unconfirmedTxs mockUnconfirmedTxs + numUnconfirmedTxs mockNumUnconfirmedTxs } func (m mockRPCClient) BroadcastTxCommit(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) { if m.broadcastTxCommit != nil { return m.broadcastTxCommit(tx) } - return nil, nil } @@ -97,76 +132,128 @@ func (m mockRPCClient) ABCIQuery(path string, data []byte) (*ctypes.ResultABCIQu return nil, nil } -// Unused RPC Client functions - func (m mockRPCClient) ABCIInfo() (*ctypes.ResultABCIInfo, error) { - panic("implement me") + if m.abciInfo != nil { + return m.ABCIInfo() + } + return nil, nil } func (m mockRPCClient) ABCIQueryWithOptions(path string, data []byte, opts client.ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) { - panic("implement me") + if m.abciQueryWithOptions != nil { + return m.abciQueryWithOptions(path, data, opts) + } + return nil, nil } func (m mockRPCClient) BroadcastTxAsync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) { - panic("implement me") + if m.broadcastTxAsync != nil { + return m.broadcastTxAsync(tx) + } + return nil, nil } func (m mockRPCClient) BroadcastTxSync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) { - panic("implement me") + if m.broadcastTxSync != nil { + return m.broadcastTxSync(tx) + } + return nil, nil } func (m mockRPCClient) Genesis() (*ctypes.ResultGenesis, error) { - panic("implement me") + if m.genesis != nil { + return m.genesis() + } + return nil, nil } func (m mockRPCClient) BlockchainInfo(minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, error) { - panic("implement me") + if m.blockchainInfo != nil { + return m.blockchainInfo(minHeight, maxHeight) + } + return nil, nil } func (m mockRPCClient) NetInfo() (*ctypes.ResultNetInfo, error) { - panic("implement me") + if m.netInfo != nil { + return m.netInfo() + } + return nil, nil } func (m mockRPCClient) DumpConsensusState() (*ctypes.ResultDumpConsensusState, error) { - panic("implement me") + if m.dumpConsensusState != nil { + return m.dumpConsensusState() + } + return nil, nil } func (m mockRPCClient) ConsensusState() (*ctypes.ResultConsensusState, error) { - panic("implement me") + if m.consensusState != nil { + return m.consensusState() + } + return nil, nil } func (m mockRPCClient) ConsensusParams(height *int64) (*ctypes.ResultConsensusParams, error) { - panic("implement me") + if m.consensusParams != nil { + return m.consensusParams(height) + } + return nil, nil } func (m mockRPCClient) Health() (*ctypes.ResultHealth, error) { - panic("implement me") + if m.health != nil { + return m.health() + } + return nil, nil } func (m mockRPCClient) Block(height *int64) (*ctypes.ResultBlock, error) { - panic("implement me") + if m.block != nil { + return m.block(height) + } + return nil, nil } func (m mockRPCClient) BlockResults(height *int64) (*ctypes.ResultBlockResults, error) { - panic("implement me") + if m.blockResults != nil { + return m.blockResults(height) + } + return nil, nil } func (m mockRPCClient) Commit(height *int64) (*ctypes.ResultCommit, error) { - panic("implement me") + if m.commit != nil { + return m.commit(height) + } + return nil, nil } func (m mockRPCClient) Validators(height *int64) (*ctypes.ResultValidators, error) { - panic("implement me") + if m.validators != nil { + return m.validators(height) + } + return nil, nil } func (m mockRPCClient) Status() (*ctypes.ResultStatus, error) { - panic("implement me") + if m.status != nil { + return m.status() + } + return nil, nil } func (m mockRPCClient) UnconfirmedTxs(limit int) (*ctypes.ResultUnconfirmedTxs, error) { - panic("implement me") + if m.unconfirmedTxs != nil { + return m.unconfirmedTxs(limit) + } + return nil, nil } func (m mockRPCClient) NumUnconfirmedTxs() (*ctypes.ResultUnconfirmedTxs, error) { - panic("implement me") + if m.numUnconfirmedTxs != nil { + return m.numUnconfirmedTxs() + } + return nil, nil } From 3ecf8f0836c5443f3c54d3ee4ffe5b387398f754 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 31 Jan 2024 17:47:02 +0100 Subject: [PATCH 24/38] simplify tests --- gno.land/pkg/gnoclient/client_test.go | 152 +++++++++----------------- gno.land/pkg/gnoclient/mock.go | 95 ++++++++++------ 2 files changed, 114 insertions(+), 133 deletions(-) diff --git a/gno.land/pkg/gnoclient/client_test.go b/gno.land/pkg/gnoclient/client_test.go index d7c9db94480..1ff81a2495c 100644 --- a/gno.land/pkg/gnoclient/client_test.go +++ b/gno.land/pkg/gnoclient/client_test.go @@ -1,83 +1,55 @@ package gnoclient import ( + "github.com/gnolang/gno/gno.land/pkg/integration" abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types" ctypes "github.com/gnolang/gno/tm2/pkg/bft/rpc/core/types" "github.com/gnolang/gno/tm2/pkg/bft/types" + "github.com/gnolang/gno/tm2/pkg/crypto" "github.com/gnolang/gno/tm2/pkg/std" "github.com/jaekwon/testify/assert" + "github.com/jaekwon/testify/require" "testing" - "github.com/gnolang/gno/gno.land/pkg/integration" "github.com/gnolang/gno/tm2/pkg/crypto/keys" - "github.com/jaekwon/testify/require" ) -func newInMemorySigner(t *testing.T, chainid string) *SignerFromKeybase { - t.Helper() +func TestClient_Render(t *testing.T) { + testRealmPath := "gno.land/r/demo/deep/very/deep" + expectedRender := []byte("it works!") - mmeonic := integration.DefaultAccount_Seed - name := integration.DefaultAccount_Name - - kb := keys.NewInMemory() - _, err := kb.CreateAccount(name, mmeonic, "", "", uint32(0), uint32(0)) - require.NoError(t, err) - - return &SignerFromKeybase{ - Keybase: kb, // Stores keys in memory or on disk - Account: name, // Account name or bech32 format - Password: "", // Password for encryption - ChainID: chainid, // Chain ID for transaction signing - } -} - -func TestClient_Request(t *testing.T) { client := Client{ Signer: &mockSigner{ sign: func(cfg SignCfg) (*std.Tx, error) { return &std.Tx{}, nil }, info: func() keys.Info { - return mockKeysInfo{} + return mockKeysInfo{ + getAddress: func() crypto.Address { + adr, _ := crypto.AddressFromBech32("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + return adr + }, + } }, }, RPCClient: mockRPCClient{ - broadcastTxCommit: func(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) { - res := &ctypes.ResultBroadcastTxCommit{ - CheckTx: abci.ResponseCheckTx{ - ResponseBase: abci.ResponseBase{ - Error: nil, - Data: nil, - Events: nil, - Log: "", - Info: "", - }, - }, - DeliverTx: abci.ResponseDeliverTx{ + abciQuery: func(path string, data []byte) (*ctypes.ResultABCIQuery, error) { + res := &ctypes.ResultABCIQuery{ + Response: abci.ResponseQuery{ ResponseBase: abci.ResponseBase{ - Error: nil, - Data: nil, - Events: nil, - Log: "", - Info: "", + Data: expectedRender, }, - GasWanted: 0, - GasUsed: 0, - }, - Hash: nil, - Height: 0, - } - + }} return res, nil }, }, } - res, data, err := client.Render("gno.land/r/demo/boards", "") - require.NoError(t, err) - require.NotEmpty(t, data) - require.NotEmpty(t, res) - + res, data, err := client.Render(testRealmPath, "") + assert.NoError(t, err) + assert.NotEmpty(t, data.Response.Data) + assert.NotEmpty(t, res) + assert.Equal(t, data.Response.Data, expectedRender) } func TestClient_CallSingle(t *testing.T) { @@ -89,52 +61,25 @@ func TestClient_CallSingle(t *testing.T) { return &std.Tx{}, nil }, info: func() keys.Info { - return mockKeysInfo{} + return mockKeysInfo{ + getAddress: func() crypto.Address { + adr, _ := crypto.AddressFromBech32("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + return adr + }, + } }, }, RPCClient: mockRPCClient{ broadcastTxCommit: func(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) { res := &ctypes.ResultBroadcastTxCommit{ - CheckTx: abci.ResponseCheckTx{ - ResponseBase: abci.ResponseBase{ - Error: nil, - Data: nil, - Events: nil, - Log: "", - Info: "", - }, - }, DeliverTx: abci.ResponseDeliverTx{ ResponseBase: abci.ResponseBase{ - Error: nil, - Data: []byte("it works!"), - Events: nil, - Log: "", - Info: "", + Data: []byte("it works!"), }, - GasWanted: 0, - GasUsed: 0, }, - Hash: nil, - Height: 0, } - return res, nil }, - abciQuery: func(path string, data []byte) (*ctypes.ResultABCIQuery, error) { - res := &ctypes.ResultABCIQuery{ - Response: abci.ResponseQuery{ - ResponseBase: abci.ResponseBase{}, - Key: nil, - Value: nil, - Proof: nil, - Height: 0, - }, - } - - return res, nil - - }, }, } @@ -159,7 +104,6 @@ func TestClient_CallSingle(t *testing.T) { assert.NoError(t, err) assert.NotNil(t, res) assert.Equal(t, string(res.DeliverTx.Data), "it works!") - } func TestClient_CallMultiple(t *testing.T) { @@ -171,7 +115,12 @@ func TestClient_CallMultiple(t *testing.T) { return &std.Tx{}, nil }, info: func() keys.Info { - return mockKeysInfo{} + return mockKeysInfo{ + getAddress: func() crypto.Address { + adr, _ := crypto.AddressFromBech32("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + return adr + }, + } }, }, RPCClient: mockRPCClient{ @@ -186,19 +135,6 @@ func TestClient_CallMultiple(t *testing.T) { Info: "", }, }, - DeliverTx: abci.ResponseDeliverTx{ - ResponseBase: abci.ResponseBase{ - Error: nil, - Data: nil, - Events: nil, - Log: "", - Info: "", - }, - GasWanted: 0, - GasUsed: 0, - }, - Hash: nil, - Height: 0, } return res, nil @@ -421,3 +357,21 @@ func TestClient_Call_Errors(t *testing.T) { }) } } + +func newInMemorySigner(t *testing.T, chainid string) *SignerFromKeybase { + t.Helper() + + mmemonic := integration.DefaultAccount_Seed + name := integration.DefaultAccount_Name + + kb := keys.NewInMemory() + _, err := kb.CreateAccount(name, mmemonic, "", "", uint32(0), uint32(0)) + require.NoError(t, err) + + return &SignerFromKeybase{ + Keybase: kb, // Stores keys in memory or on disk + Account: name, // Account name or bech32 format + Password: "", // Password for encryption + ChainID: chainid, // Chain ID for transaction signing + } +} diff --git a/gno.land/pkg/gnoclient/mock.go b/gno.land/pkg/gnoclient/mock.go index 787c2cd4a30..00c48daba88 100644 --- a/gno.land/pkg/gnoclient/mock.go +++ b/gno.land/pkg/gnoclient/mock.go @@ -27,48 +27,75 @@ func (m *mockSigner) Sign(cfg SignCfg) (*std.Tx, error) { if m.sign != nil { return m.sign(cfg) } - - return nil, nil + panic("no implementation passed in") } func (m *mockSigner) Info() keys.Info { if m.info != nil { return m.info() } - - return nil + panic("no implementation passed in") } func (m *mockSigner) Validate() error { if m.validate != nil { return m.validate() } - - return nil + panic("no implementation passed in") } -type mockKeysInfo struct{} +// Keys Info + +type ( + mockGetAddress func() crypto.Address + mockGetType func() keys.KeyType + mockGetName func() string + mockGetPubKey func() crypto.PubKey + mockGetPath func() (*hd.BIP44Params, error) +) + +type mockKeysInfo struct { + getAddress mockGetAddress + getType mockGetType + getName mockGetName + getPubKey mockGetPubKey + getPath mockGetPath +} func (m mockKeysInfo) GetAddress() crypto.Address { - adr, _ := crypto.AddressFromBech32("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") - return adr + if m.getAddress != nil { + return m.getAddress() + } + + panic("no implementation passed in") } func (m mockKeysInfo) GetType() keys.KeyType { - return 0 + if m.getType != nil { + return m.getType() + } + panic("no implementation passed in") } func (m mockKeysInfo) GetName() string { - return "mockKeyInfoName" + if m.getName != nil { + return m.getName() + } + panic("no implementation passed in") } func (m mockKeysInfo) GetPubKey() crypto.PubKey { - pubkey, _ := crypto.PubKeyFromBech32("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") - return pubkey + if m.getPubKey != nil { + return m.getPubKey() + } + panic("no implementation passed in") } func (m mockKeysInfo) GetPath() (*hd.BIP44Params, error) { - return nil, nil + if m.getPath != nil { + return m.getPath() + } + panic("no implementation passed in") } // RPC Client mock @@ -122,138 +149,138 @@ func (m mockRPCClient) BroadcastTxCommit(tx types.Tx) (*ctypes.ResultBroadcastTx if m.broadcastTxCommit != nil { return m.broadcastTxCommit(tx) } - return nil, nil + panic("no implementation passed in") } func (m mockRPCClient) ABCIQuery(path string, data []byte) (*ctypes.ResultABCIQuery, error) { if m.abciQuery != nil { return m.abciQuery(path, data) } - return nil, nil + panic("no implementation passed in") } func (m mockRPCClient) ABCIInfo() (*ctypes.ResultABCIInfo, error) { if m.abciInfo != nil { return m.ABCIInfo() } - return nil, nil + panic("no implementation passed in") } func (m mockRPCClient) ABCIQueryWithOptions(path string, data []byte, opts client.ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) { if m.abciQueryWithOptions != nil { return m.abciQueryWithOptions(path, data, opts) } - return nil, nil + panic("no implementation passed in") } func (m mockRPCClient) BroadcastTxAsync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) { if m.broadcastTxAsync != nil { return m.broadcastTxAsync(tx) } - return nil, nil + panic("no implementation passed in") } func (m mockRPCClient) BroadcastTxSync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) { if m.broadcastTxSync != nil { return m.broadcastTxSync(tx) } - return nil, nil + panic("no implementation passed in") } func (m mockRPCClient) Genesis() (*ctypes.ResultGenesis, error) { if m.genesis != nil { return m.genesis() } - return nil, nil + panic("no implementation passed in") } func (m mockRPCClient) BlockchainInfo(minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, error) { if m.blockchainInfo != nil { return m.blockchainInfo(minHeight, maxHeight) } - return nil, nil + panic("no implementation passed in") } func (m mockRPCClient) NetInfo() (*ctypes.ResultNetInfo, error) { if m.netInfo != nil { return m.netInfo() } - return nil, nil + panic("no implementation passed in") } func (m mockRPCClient) DumpConsensusState() (*ctypes.ResultDumpConsensusState, error) { if m.dumpConsensusState != nil { return m.dumpConsensusState() } - return nil, nil + panic("no implementation passed in") } func (m mockRPCClient) ConsensusState() (*ctypes.ResultConsensusState, error) { if m.consensusState != nil { return m.consensusState() } - return nil, nil + panic("no implementation passed in") } func (m mockRPCClient) ConsensusParams(height *int64) (*ctypes.ResultConsensusParams, error) { if m.consensusParams != nil { return m.consensusParams(height) } - return nil, nil + panic("no implementation passed in") } func (m mockRPCClient) Health() (*ctypes.ResultHealth, error) { if m.health != nil { return m.health() } - return nil, nil + panic("no implementation passed in") } func (m mockRPCClient) Block(height *int64) (*ctypes.ResultBlock, error) { if m.block != nil { return m.block(height) } - return nil, nil + panic("no implementation passed in") } func (m mockRPCClient) BlockResults(height *int64) (*ctypes.ResultBlockResults, error) { if m.blockResults != nil { return m.blockResults(height) } - return nil, nil + panic("no implementation passed in") } func (m mockRPCClient) Commit(height *int64) (*ctypes.ResultCommit, error) { if m.commit != nil { return m.commit(height) } - return nil, nil + panic("no implementation passed in") } func (m mockRPCClient) Validators(height *int64) (*ctypes.ResultValidators, error) { if m.validators != nil { return m.validators(height) } - return nil, nil + panic("no implementation passed in") } func (m mockRPCClient) Status() (*ctypes.ResultStatus, error) { if m.status != nil { return m.status() } - return nil, nil + panic("no implementation passed in") } func (m mockRPCClient) UnconfirmedTxs(limit int) (*ctypes.ResultUnconfirmedTxs, error) { if m.unconfirmedTxs != nil { return m.unconfirmedTxs(limit) } - return nil, nil + panic("no implementation passed in") } func (m mockRPCClient) NumUnconfirmedTxs() (*ctypes.ResultUnconfirmedTxs, error) { if m.numUnconfirmedTxs != nil { return m.numUnconfirmedTxs() } - return nil, nil + panic("no implementation passed in") } From a190219446b6c1f85b36de003521d2ec4fa9c4bd Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 31 Jan 2024 17:54:48 +0100 Subject: [PATCH 25/38] typo fix --- gno.land/pkg/gnoclient/client_test.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/gno.land/pkg/gnoclient/client_test.go b/gno.land/pkg/gnoclient/client_test.go index 1ff81a2495c..e384e805127 100644 --- a/gno.land/pkg/gnoclient/client_test.go +++ b/gno.land/pkg/gnoclient/client_test.go @@ -174,6 +174,8 @@ func TestClient_CallMultiple(t *testing.T) { res, err := client.Call(cfg, msg...) assert.NoError(t, err) assert.NotNil(t, res) + + // todo check for res data? } func TestClient_Call_Errors(t *testing.T) { @@ -361,11 +363,11 @@ func TestClient_Call_Errors(t *testing.T) { func newInMemorySigner(t *testing.T, chainid string) *SignerFromKeybase { t.Helper() - mmemonic := integration.DefaultAccount_Seed + mnemonic := integration.DefaultAccount_Seed name := integration.DefaultAccount_Name kb := keys.NewInMemory() - _, err := kb.CreateAccount(name, mmemonic, "", "", uint32(0), uint32(0)) + _, err := kb.CreateAccount(name, mnemonic, "", "", uint32(0), uint32(0)) require.NoError(t, err) return &SignerFromKeybase{ From 7caf6925fb7b955c7ed59f1d026a064cc184e069 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 31 Jan 2024 18:09:32 +0100 Subject: [PATCH 26/38] remove comment --- gno.land/pkg/gnoclient/client_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/gno.land/pkg/gnoclient/client_test.go b/gno.land/pkg/gnoclient/client_test.go index e384e805127..ea7c3ff808d 100644 --- a/gno.land/pkg/gnoclient/client_test.go +++ b/gno.land/pkg/gnoclient/client_test.go @@ -174,8 +174,6 @@ func TestClient_CallMultiple(t *testing.T) { res, err := client.Call(cfg, msg...) assert.NoError(t, err) assert.NotNil(t, res) - - // todo check for res data? } func TestClient_Call_Errors(t *testing.T) { From 085ce3f3b719147284b17180d9048864d7e9f0c6 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 31 Jan 2024 18:48:59 +0100 Subject: [PATCH 27/38] fix lint --- gno.land/pkg/gnoclient/client_test.go | 4 ++-- gno.land/pkg/gnoclient/client_txs.go | 4 ++-- gno.land/pkg/gnoclient/mock.go | 5 ++--- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/gno.land/pkg/gnoclient/client_test.go b/gno.land/pkg/gnoclient/client_test.go index ea7c3ff808d..bf9367822f9 100644 --- a/gno.land/pkg/gnoclient/client_test.go +++ b/gno.land/pkg/gnoclient/client_test.go @@ -6,12 +6,12 @@ import ( ctypes "github.com/gnolang/gno/tm2/pkg/bft/rpc/core/types" "github.com/gnolang/gno/tm2/pkg/bft/types" "github.com/gnolang/gno/tm2/pkg/crypto" + "github.com/gnolang/gno/tm2/pkg/crypto/keys" "github.com/gnolang/gno/tm2/pkg/std" "github.com/jaekwon/testify/assert" "github.com/jaekwon/testify/require" - "testing" - "github.com/gnolang/gno/tm2/pkg/crypto/keys" + "testing" ) func TestClient_Render(t *testing.T) { diff --git a/gno.land/pkg/gnoclient/client_txs.go b/gno.land/pkg/gnoclient/client_txs.go index 73dfee472c2..67e54be733c 100644 --- a/gno.land/pkg/gnoclient/client_txs.go +++ b/gno.land/pkg/gnoclient/client_txs.go @@ -60,7 +60,7 @@ func (c *Client) Call(cfg BaseTxCfg, msgs ...MsgCall) (*ctypes.ResultBroadcastTx } // Parse MsgCall slice - vmMsgs := make([]vm.MsgCall, len(msgs)) + vmMsgs := make([]vm.MsgCall, 0, len(msgs)) for _, msg := range msgs { // Validate MsgCall fields if err := msg.validateMsgCall(); err != nil { @@ -83,7 +83,7 @@ func (c *Client) Call(cfg BaseTxCfg, msgs ...MsgCall) (*ctypes.ResultBroadcastTx } // Cast vm.MsgCall back into std.Msg - stdMsgs := make([]std.Msg, len(vmMsgs)) + stdMsgs := make([]std.Msg, 0, len(vmMsgs)) for i, msg := range vmMsgs { stdMsgs[i] = msg } diff --git a/gno.land/pkg/gnoclient/mock.go b/gno.land/pkg/gnoclient/mock.go index 00c48daba88..8b4312360c6 100644 --- a/gno.land/pkg/gnoclient/mock.go +++ b/gno.land/pkg/gnoclient/mock.go @@ -10,7 +10,7 @@ import ( "github.com/gnolang/gno/tm2/pkg/std" ) -// Signer +// Signer mock type ( mockSign func(cfg SignCfg) (*std.Tx, error) mockInfo func() keys.Info @@ -44,8 +44,7 @@ func (m *mockSigner) Validate() error { panic("no implementation passed in") } -// Keys Info - +// Keys Info mock type ( mockGetAddress func() crypto.Address mockGetType func() keys.KeyType From 849ede25fcb3009f85fee5802c4c83adce4842a1 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 31 Jan 2024 18:49:38 +0100 Subject: [PATCH 28/38] fix test err --- gno.land/pkg/gnoclient/client_txs.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gno.land/pkg/gnoclient/client_txs.go b/gno.land/pkg/gnoclient/client_txs.go index 67e54be733c..9f06217599f 100644 --- a/gno.land/pkg/gnoclient/client_txs.go +++ b/gno.land/pkg/gnoclient/client_txs.go @@ -83,7 +83,7 @@ func (c *Client) Call(cfg BaseTxCfg, msgs ...MsgCall) (*ctypes.ResultBroadcastTx } // Cast vm.MsgCall back into std.Msg - stdMsgs := make([]std.Msg, 0, len(vmMsgs)) + stdMsgs := make([]std.Msg, len(vmMsgs)) for i, msg := range vmMsgs { stdMsgs[i] = msg } From 800d18802726a818701bd3ec8d14d42cc145e763 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Thu, 1 Feb 2024 11:27:57 +0100 Subject: [PATCH 29/38] parallel test --- gno.land/pkg/gnoclient/client_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/gno.land/pkg/gnoclient/client_test.go b/gno.land/pkg/gnoclient/client_test.go index bf9367822f9..f97efd56353 100644 --- a/gno.land/pkg/gnoclient/client_test.go +++ b/gno.land/pkg/gnoclient/client_test.go @@ -15,6 +15,7 @@ import ( ) func TestClient_Render(t *testing.T) { + t.Parallel() testRealmPath := "gno.land/r/demo/deep/very/deep" expectedRender := []byte("it works!") From 9e8c7e24ab4501bd4b9131ac66445802c1f65e8c Mon Sep 17 00:00:00 2001 From: leohhhn Date: Thu, 1 Feb 2024 11:28:43 +0100 Subject: [PATCH 30/38] replace assert with require --- gno.land/pkg/gnoclient/client_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gno.land/pkg/gnoclient/client_test.go b/gno.land/pkg/gnoclient/client_test.go index f97efd56353..fea272c7b4f 100644 --- a/gno.land/pkg/gnoclient/client_test.go +++ b/gno.land/pkg/gnoclient/client_test.go @@ -103,7 +103,7 @@ func TestClient_CallSingle(t *testing.T) { res, err := client.Call(cfg, msg...) assert.NoError(t, err) - assert.NotNil(t, res) + require.NotNil(t, res) assert.Equal(t, string(res.DeliverTx.Data), "it works!") } From a4bf5cbe41ee55a51d8e268d81a70f1e3f42e173 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Thu, 1 Feb 2024 11:29:16 +0100 Subject: [PATCH 31/38] add errors.Is --- gno.land/pkg/gnoclient/client_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gno.land/pkg/gnoclient/client_test.go b/gno.land/pkg/gnoclient/client_test.go index fea272c7b4f..aa14c468bd8 100644 --- a/gno.land/pkg/gnoclient/client_test.go +++ b/gno.land/pkg/gnoclient/client_test.go @@ -1,6 +1,7 @@ package gnoclient import ( + "errors" "github.com/gnolang/gno/gno.land/pkg/integration" abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types" ctypes "github.com/gnolang/gno/tm2/pkg/bft/rpc/core/types" @@ -353,7 +354,7 @@ func TestClient_Call_Errors(t *testing.T) { t.Parallel() res, err := tc.client.Call(tc.cfg, tc.msgs...) - assert.Equal(t, err, tc.expectedError) + errors.Is(err, tc.expectedError) assert.Nil(t, res) }) } From 44a0657e4cfa7af44b458cc9867ab8196a17f1fa Mon Sep 17 00:00:00 2001 From: leohhhn Date: Thu, 1 Feb 2024 11:31:49 +0100 Subject: [PATCH 32/38] remove panics --- gno.land/pkg/gnoclient/mock.go | 57 +++++++++++++++++----------------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/gno.land/pkg/gnoclient/mock.go b/gno.land/pkg/gnoclient/mock.go index 8b4312360c6..2cc45c1f382 100644 --- a/gno.land/pkg/gnoclient/mock.go +++ b/gno.land/pkg/gnoclient/mock.go @@ -27,21 +27,21 @@ func (m *mockSigner) Sign(cfg SignCfg) (*std.Tx, error) { if m.sign != nil { return m.sign(cfg) } - panic("no implementation passed in") + return nil, nil } func (m *mockSigner) Info() keys.Info { if m.info != nil { return m.info() } - panic("no implementation passed in") + return nil } func (m *mockSigner) Validate() error { if m.validate != nil { return m.validate() } - panic("no implementation passed in") + return nil } // Keys Info mock @@ -65,36 +65,35 @@ func (m mockKeysInfo) GetAddress() crypto.Address { if m.getAddress != nil { return m.getAddress() } - - panic("no implementation passed in") + return crypto.Address{} } func (m mockKeysInfo) GetType() keys.KeyType { if m.getType != nil { return m.getType() } - panic("no implementation passed in") + return 0 } func (m mockKeysInfo) GetName() string { if m.getName != nil { return m.getName() } - panic("no implementation passed in") + return "" } func (m mockKeysInfo) GetPubKey() crypto.PubKey { if m.getPubKey != nil { return m.getPubKey() } - panic("no implementation passed in") + return nil } func (m mockKeysInfo) GetPath() (*hd.BIP44Params, error) { if m.getPath != nil { return m.getPath() } - panic("no implementation passed in") + return nil, nil } // RPC Client mock @@ -148,138 +147,138 @@ func (m mockRPCClient) BroadcastTxCommit(tx types.Tx) (*ctypes.ResultBroadcastTx if m.broadcastTxCommit != nil { return m.broadcastTxCommit(tx) } - panic("no implementation passed in") + return nil, nil } func (m mockRPCClient) ABCIQuery(path string, data []byte) (*ctypes.ResultABCIQuery, error) { if m.abciQuery != nil { return m.abciQuery(path, data) } - panic("no implementation passed in") + return nil, nil } func (m mockRPCClient) ABCIInfo() (*ctypes.ResultABCIInfo, error) { if m.abciInfo != nil { return m.ABCIInfo() } - panic("no implementation passed in") + return nil, nil } func (m mockRPCClient) ABCIQueryWithOptions(path string, data []byte, opts client.ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) { if m.abciQueryWithOptions != nil { return m.abciQueryWithOptions(path, data, opts) } - panic("no implementation passed in") + return nil, nil } func (m mockRPCClient) BroadcastTxAsync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) { if m.broadcastTxAsync != nil { return m.broadcastTxAsync(tx) } - panic("no implementation passed in") + return nil, nil } func (m mockRPCClient) BroadcastTxSync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) { if m.broadcastTxSync != nil { return m.broadcastTxSync(tx) } - panic("no implementation passed in") + return nil, nil } func (m mockRPCClient) Genesis() (*ctypes.ResultGenesis, error) { if m.genesis != nil { return m.genesis() } - panic("no implementation passed in") + return nil, nil } func (m mockRPCClient) BlockchainInfo(minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, error) { if m.blockchainInfo != nil { return m.blockchainInfo(minHeight, maxHeight) } - panic("no implementation passed in") + return nil, nil } func (m mockRPCClient) NetInfo() (*ctypes.ResultNetInfo, error) { if m.netInfo != nil { return m.netInfo() } - panic("no implementation passed in") + return nil, nil } func (m mockRPCClient) DumpConsensusState() (*ctypes.ResultDumpConsensusState, error) { if m.dumpConsensusState != nil { return m.dumpConsensusState() } - panic("no implementation passed in") + return nil, nil } func (m mockRPCClient) ConsensusState() (*ctypes.ResultConsensusState, error) { if m.consensusState != nil { return m.consensusState() } - panic("no implementation passed in") + return nil, nil } func (m mockRPCClient) ConsensusParams(height *int64) (*ctypes.ResultConsensusParams, error) { if m.consensusParams != nil { return m.consensusParams(height) } - panic("no implementation passed in") + return nil, nil } func (m mockRPCClient) Health() (*ctypes.ResultHealth, error) { if m.health != nil { return m.health() } - panic("no implementation passed in") + return nil, nil } func (m mockRPCClient) Block(height *int64) (*ctypes.ResultBlock, error) { if m.block != nil { return m.block(height) } - panic("no implementation passed in") + return nil, nil } func (m mockRPCClient) BlockResults(height *int64) (*ctypes.ResultBlockResults, error) { if m.blockResults != nil { return m.blockResults(height) } - panic("no implementation passed in") + return nil, nil } func (m mockRPCClient) Commit(height *int64) (*ctypes.ResultCommit, error) { if m.commit != nil { return m.commit(height) } - panic("no implementation passed in") + return nil, nil } func (m mockRPCClient) Validators(height *int64) (*ctypes.ResultValidators, error) { if m.validators != nil { return m.validators(height) } - panic("no implementation passed in") + return nil, nil } func (m mockRPCClient) Status() (*ctypes.ResultStatus, error) { if m.status != nil { return m.status() } - panic("no implementation passed in") + return nil, nil } func (m mockRPCClient) UnconfirmedTxs(limit int) (*ctypes.ResultUnconfirmedTxs, error) { if m.unconfirmedTxs != nil { return m.unconfirmedTxs(limit) } - panic("no implementation passed in") + return nil, nil } func (m mockRPCClient) NumUnconfirmedTxs() (*ctypes.ResultUnconfirmedTxs, error) { if m.numUnconfirmedTxs != nil { return m.numUnconfirmedTxs() } - panic("no implementation passed in") + return nil, nil } From 1bad3dee18d09217655ebe40ba082fe0075ac519 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Thu, 1 Feb 2024 11:38:07 +0100 Subject: [PATCH 33/38] add pointer receivers --- gno.land/pkg/gnoclient/client_test.go | 12 +++---- gno.land/pkg/gnoclient/mock.go | 50 +++++++++++++-------------- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/gno.land/pkg/gnoclient/client_test.go b/gno.land/pkg/gnoclient/client_test.go index aa14c468bd8..0255bd7ea24 100644 --- a/gno.land/pkg/gnoclient/client_test.go +++ b/gno.land/pkg/gnoclient/client_test.go @@ -26,7 +26,7 @@ func TestClient_Render(t *testing.T) { return &std.Tx{}, nil }, info: func() keys.Info { - return mockKeysInfo{ + return &mockKeysInfo{ getAddress: func() crypto.Address { adr, _ := crypto.AddressFromBech32("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") return adr @@ -34,7 +34,7 @@ func TestClient_Render(t *testing.T) { } }, }, - RPCClient: mockRPCClient{ + RPCClient: &mockRPCClient{ abciQuery: func(path string, data []byte) (*ctypes.ResultABCIQuery, error) { res := &ctypes.ResultABCIQuery{ Response: abci.ResponseQuery{ @@ -63,7 +63,7 @@ func TestClient_CallSingle(t *testing.T) { return &std.Tx{}, nil }, info: func() keys.Info { - return mockKeysInfo{ + return &mockKeysInfo{ getAddress: func() crypto.Address { adr, _ := crypto.AddressFromBech32("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") return adr @@ -71,7 +71,7 @@ func TestClient_CallSingle(t *testing.T) { } }, }, - RPCClient: mockRPCClient{ + RPCClient: &mockRPCClient{ broadcastTxCommit: func(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) { res := &ctypes.ResultBroadcastTxCommit{ DeliverTx: abci.ResponseDeliverTx{ @@ -117,7 +117,7 @@ func TestClient_CallMultiple(t *testing.T) { return &std.Tx{}, nil }, info: func() keys.Info { - return mockKeysInfo{ + return &mockKeysInfo{ getAddress: func() crypto.Address { adr, _ := crypto.AddressFromBech32("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") return adr @@ -125,7 +125,7 @@ func TestClient_CallMultiple(t *testing.T) { } }, }, - RPCClient: mockRPCClient{ + RPCClient: &mockRPCClient{ broadcastTxCommit: func(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) { res := &ctypes.ResultBroadcastTxCommit{ CheckTx: abci.ResponseCheckTx{ diff --git a/gno.land/pkg/gnoclient/mock.go b/gno.land/pkg/gnoclient/mock.go index 2cc45c1f382..4a12dfd2d88 100644 --- a/gno.land/pkg/gnoclient/mock.go +++ b/gno.land/pkg/gnoclient/mock.go @@ -61,35 +61,35 @@ type mockKeysInfo struct { getPath mockGetPath } -func (m mockKeysInfo) GetAddress() crypto.Address { +func (m *mockKeysInfo) GetAddress() crypto.Address { if m.getAddress != nil { return m.getAddress() } return crypto.Address{} } -func (m mockKeysInfo) GetType() keys.KeyType { +func (m *mockKeysInfo) GetType() keys.KeyType { if m.getType != nil { return m.getType() } return 0 } -func (m mockKeysInfo) GetName() string { +func (m *mockKeysInfo) GetName() string { if m.getName != nil { return m.getName() } return "" } -func (m mockKeysInfo) GetPubKey() crypto.PubKey { +func (m *mockKeysInfo) GetPubKey() crypto.PubKey { if m.getPubKey != nil { return m.getPubKey() } return nil } -func (m mockKeysInfo) GetPath() (*hd.BIP44Params, error) { +func (m *mockKeysInfo) GetPath() (*hd.BIP44Params, error) { if m.getPath != nil { return m.getPath() } @@ -143,140 +143,140 @@ type mockRPCClient struct { numUnconfirmedTxs mockNumUnconfirmedTxs } -func (m mockRPCClient) BroadcastTxCommit(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) { +func (m *mockRPCClient) BroadcastTxCommit(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) { if m.broadcastTxCommit != nil { return m.broadcastTxCommit(tx) } return nil, nil } -func (m mockRPCClient) ABCIQuery(path string, data []byte) (*ctypes.ResultABCIQuery, error) { +func (m *mockRPCClient) ABCIQuery(path string, data []byte) (*ctypes.ResultABCIQuery, error) { if m.abciQuery != nil { return m.abciQuery(path, data) } return nil, nil } -func (m mockRPCClient) ABCIInfo() (*ctypes.ResultABCIInfo, error) { +func (m *mockRPCClient) ABCIInfo() (*ctypes.ResultABCIInfo, error) { if m.abciInfo != nil { return m.ABCIInfo() } return nil, nil } -func (m mockRPCClient) ABCIQueryWithOptions(path string, data []byte, opts client.ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) { +func (m *mockRPCClient) ABCIQueryWithOptions(path string, data []byte, opts client.ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) { if m.abciQueryWithOptions != nil { return m.abciQueryWithOptions(path, data, opts) } return nil, nil } -func (m mockRPCClient) BroadcastTxAsync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) { +func (m *mockRPCClient) BroadcastTxAsync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) { if m.broadcastTxAsync != nil { return m.broadcastTxAsync(tx) } return nil, nil } -func (m mockRPCClient) BroadcastTxSync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) { +func (m *mockRPCClient) BroadcastTxSync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) { if m.broadcastTxSync != nil { return m.broadcastTxSync(tx) } return nil, nil } -func (m mockRPCClient) Genesis() (*ctypes.ResultGenesis, error) { +func (m *mockRPCClient) Genesis() (*ctypes.ResultGenesis, error) { if m.genesis != nil { return m.genesis() } return nil, nil } -func (m mockRPCClient) BlockchainInfo(minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, error) { +func (m *mockRPCClient) BlockchainInfo(minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, error) { if m.blockchainInfo != nil { return m.blockchainInfo(minHeight, maxHeight) } return nil, nil } -func (m mockRPCClient) NetInfo() (*ctypes.ResultNetInfo, error) { +func (m *mockRPCClient) NetInfo() (*ctypes.ResultNetInfo, error) { if m.netInfo != nil { return m.netInfo() } return nil, nil } -func (m mockRPCClient) DumpConsensusState() (*ctypes.ResultDumpConsensusState, error) { +func (m *mockRPCClient) DumpConsensusState() (*ctypes.ResultDumpConsensusState, error) { if m.dumpConsensusState != nil { return m.dumpConsensusState() } return nil, nil } -func (m mockRPCClient) ConsensusState() (*ctypes.ResultConsensusState, error) { +func (m *mockRPCClient) ConsensusState() (*ctypes.ResultConsensusState, error) { if m.consensusState != nil { return m.consensusState() } return nil, nil } -func (m mockRPCClient) ConsensusParams(height *int64) (*ctypes.ResultConsensusParams, error) { +func (m *mockRPCClient) ConsensusParams(height *int64) (*ctypes.ResultConsensusParams, error) { if m.consensusParams != nil { return m.consensusParams(height) } return nil, nil } -func (m mockRPCClient) Health() (*ctypes.ResultHealth, error) { +func (m *mockRPCClient) Health() (*ctypes.ResultHealth, error) { if m.health != nil { return m.health() } return nil, nil } -func (m mockRPCClient) Block(height *int64) (*ctypes.ResultBlock, error) { +func (m *mockRPCClient) Block(height *int64) (*ctypes.ResultBlock, error) { if m.block != nil { return m.block(height) } return nil, nil } -func (m mockRPCClient) BlockResults(height *int64) (*ctypes.ResultBlockResults, error) { +func (m *mockRPCClient) BlockResults(height *int64) (*ctypes.ResultBlockResults, error) { if m.blockResults != nil { return m.blockResults(height) } return nil, nil } -func (m mockRPCClient) Commit(height *int64) (*ctypes.ResultCommit, error) { +func (m *mockRPCClient) Commit(height *int64) (*ctypes.ResultCommit, error) { if m.commit != nil { return m.commit(height) } return nil, nil } -func (m mockRPCClient) Validators(height *int64) (*ctypes.ResultValidators, error) { +func (m *mockRPCClient) Validators(height *int64) (*ctypes.ResultValidators, error) { if m.validators != nil { return m.validators(height) } return nil, nil } -func (m mockRPCClient) Status() (*ctypes.ResultStatus, error) { +func (m *mockRPCClient) Status() (*ctypes.ResultStatus, error) { if m.status != nil { return m.status() } return nil, nil } -func (m mockRPCClient) UnconfirmedTxs(limit int) (*ctypes.ResultUnconfirmedTxs, error) { +func (m *mockRPCClient) UnconfirmedTxs(limit int) (*ctypes.ResultUnconfirmedTxs, error) { if m.unconfirmedTxs != nil { return m.unconfirmedTxs(limit) } return nil, nil } -func (m mockRPCClient) NumUnconfirmedTxs() (*ctypes.ResultUnconfirmedTxs, error) { +func (m *mockRPCClient) NumUnconfirmedTxs() (*ctypes.ResultUnconfirmedTxs, error) { if m.numUnconfirmedTxs != nil { return m.numUnconfirmedTxs() } From a663e8e01a07c26db06754b744c1c29cc595ce0c Mon Sep 17 00:00:00 2001 From: leohhhn Date: Thu, 1 Feb 2024 11:40:12 +0100 Subject: [PATCH 34/38] make fmt --- gno.land/pkg/gnoclient/client_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/gno.land/pkg/gnoclient/client_test.go b/gno.land/pkg/gnoclient/client_test.go index 0255bd7ea24..85af2a04ae5 100644 --- a/gno.land/pkg/gnoclient/client_test.go +++ b/gno.land/pkg/gnoclient/client_test.go @@ -2,6 +2,8 @@ package gnoclient import ( "errors" + "testing" + "github.com/gnolang/gno/gno.land/pkg/integration" abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types" ctypes "github.com/gnolang/gno/tm2/pkg/bft/rpc/core/types" @@ -11,8 +13,6 @@ import ( "github.com/gnolang/gno/tm2/pkg/std" "github.com/jaekwon/testify/assert" "github.com/jaekwon/testify/require" - - "testing" ) func TestClient_Render(t *testing.T) { @@ -41,7 +41,8 @@ func TestClient_Render(t *testing.T) { ResponseBase: abci.ResponseBase{ Data: expectedRender, }, - }} + }, + } return res, nil }, }, From f6e78d2e07aac815e9bb71d0a6a9069086f25e7f Mon Sep 17 00:00:00 2001 From: leohhhn Date: Thu, 1 Feb 2024 12:02:37 +0100 Subject: [PATCH 35/38] update imports, fix assert.ErrorIs --- gno.land/pkg/gnoclient/client_test.go | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/gno.land/pkg/gnoclient/client_test.go b/gno.land/pkg/gnoclient/client_test.go index 85af2a04ae5..8962fa117a6 100644 --- a/gno.land/pkg/gnoclient/client_test.go +++ b/gno.land/pkg/gnoclient/client_test.go @@ -1,18 +1,16 @@ package gnoclient import ( - "errors" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "testing" - "github.com/gnolang/gno/gno.land/pkg/integration" abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types" ctypes "github.com/gnolang/gno/tm2/pkg/bft/rpc/core/types" "github.com/gnolang/gno/tm2/pkg/bft/types" "github.com/gnolang/gno/tm2/pkg/crypto" "github.com/gnolang/gno/tm2/pkg/crypto/keys" "github.com/gnolang/gno/tm2/pkg/std" - "github.com/jaekwon/testify/assert" - "github.com/jaekwon/testify/require" ) func TestClient_Render(t *testing.T) { @@ -355,26 +353,9 @@ func TestClient_Call_Errors(t *testing.T) { t.Parallel() res, err := tc.client.Call(tc.cfg, tc.msgs...) - errors.Is(err, tc.expectedError) + assert.Error(t, err, tc.expectedError) assert.Nil(t, res) + assert.ErrorIs(t, err, tc.expectedError) }) } } - -func newInMemorySigner(t *testing.T, chainid string) *SignerFromKeybase { - t.Helper() - - mnemonic := integration.DefaultAccount_Seed - name := integration.DefaultAccount_Name - - kb := keys.NewInMemory() - _, err := kb.CreateAccount(name, mnemonic, "", "", uint32(0), uint32(0)) - require.NoError(t, err) - - return &SignerFromKeybase{ - Keybase: kb, // Stores keys in memory or on disk - Account: name, // Account name or bech32 format - Password: "", // Password for encryption - ChainID: chainid, // Chain ID for transaction signing - } -} From bd9ac4b393cb94ee0e4d6a7d762c17064cd1b312 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Thu, 1 Feb 2024 12:05:03 +0100 Subject: [PATCH 36/38] make fmt --- gno.land/pkg/gnoclient/client_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gno.land/pkg/gnoclient/client_test.go b/gno.land/pkg/gnoclient/client_test.go index 8962fa117a6..76b1a2ee368 100644 --- a/gno.land/pkg/gnoclient/client_test.go +++ b/gno.land/pkg/gnoclient/client_test.go @@ -1,9 +1,10 @@ package gnoclient import ( + "testing" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "testing" abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types" ctypes "github.com/gnolang/gno/tm2/pkg/bft/rpc/core/types" From 0e6300ed3237da7e2992749f3b9c6667a11958fa Mon Sep 17 00:00:00 2001 From: leohhhn Date: Thu, 1 Feb 2024 12:07:51 +0100 Subject: [PATCH 37/38] remove double assert --- gno.land/pkg/gnoclient/client_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/gno.land/pkg/gnoclient/client_test.go b/gno.land/pkg/gnoclient/client_test.go index 76b1a2ee368..c7e6b3c6c5b 100644 --- a/gno.land/pkg/gnoclient/client_test.go +++ b/gno.land/pkg/gnoclient/client_test.go @@ -354,7 +354,6 @@ func TestClient_Call_Errors(t *testing.T) { t.Parallel() res, err := tc.client.Call(tc.cfg, tc.msgs...) - assert.Error(t, err, tc.expectedError) assert.Nil(t, res) assert.ErrorIs(t, err, tc.expectedError) }) From 60b5963c8b6e91cc4ffa3f61af9dfe6814175687 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Thu, 1 Feb 2024 18:24:58 +0100 Subject: [PATCH 38/38] rename mock to mock_test --- gno.land/pkg/gnoclient/{mock.go => mock_test.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename gno.land/pkg/gnoclient/{mock.go => mock_test.go} (100%) diff --git a/gno.land/pkg/gnoclient/mock.go b/gno.land/pkg/gnoclient/mock_test.go similarity index 100% rename from gno.land/pkg/gnoclient/mock.go rename to gno.land/pkg/gnoclient/mock_test.go