Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add gno.land/pkg/gnoclient (Gno.land Go client) #1047

Merged
merged 28 commits into from
Jan 18, 2024
Merged
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
0d235f0
feat: add gno.land/pkg/gnoclient (Gno.land Go client)
moul Aug 11, 2023
f4d7b73
chore: wip
moul Sep 11, 2023
588dfba
chore: fixup
moul Sep 11, 2023
e11919b
chore: fixup
moul Sep 11, 2023
f28ff5d
chore: fixup
moul Sep 13, 2023
2f6721e
chore: fixup
moul Sep 14, 2023
1cd0b1d
chore: fixup
moul Sep 14, 2023
3deec7c
chore: fixup
moul Sep 14, 2023
96c9076
chore: fixup
moul Sep 14, 2023
3f63f40
chore: fixup
moul Sep 14, 2023
89e9b48
chore: fixup
moul Sep 14, 2023
5cd1252
chore: fixup
moul Sep 14, 2023
e9901a6
chore: fixup
moul Sep 14, 2023
795f017
chore: fixup
moul Sep 14, 2023
208c61f
chore: wip
moul Sep 14, 2023
60e05e8
Update example_test.go
moul Sep 15, 2023
35e86bd
chore: In gnoclient.Client, add methods Render and QEval.
jefft0 Nov 23, 2023
dcb7d43
chore: In gnoclient.Signer.Validate, sign a blank transaction to veri…
jefft0 Nov 23, 2023
8abebbe
chore: In gnoclient.Client.QueryAccount, param addr should be crypto.…
jefft0 Nov 23, 2023
29af0b9
Merge pull request #18 from jefft0/chore/gnoclient-updates-for-PR-1047
moul Nov 23, 2023
8d451c0
Merge branch 'master' into dev/moul/gnoclient
gfanton Nov 23, 2023
e2537eb
feat: add basic integration test to gnoclient
gfanton Nov 23, 2023
89f112c
chore: add test placeholder
gfanton Nov 23, 2023
f1059c7
fix: In gnoclient Render and QEval, need to check Response.Error
jefft0 Nov 30, 2023
902c95b
Merge pull request #19 from jefft0/fix/gnoclient-Render-QEval-errors
moul Nov 30, 2023
5f0290f
fix: fix rebase
gfanton Jan 18, 2024
74bd901
Merge remote-tracking branch 'master' into dev/moul/gnoclient
gfanton Jan 18, 2024
19f90c1
fix: update simple test
gfanton Jan 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: add basic integration test to gnoclient
Signed-off-by: gfanton <[email protected]>
gfanton committed Nov 23, 2023
commit e2537eb558239428ba5e963ed89d51983c35447c
42 changes: 39 additions & 3 deletions gno.land/pkg/gnoclient/client_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,51 @@
package gnoclient

import (
"fmt"
"testing"

"github.com/gnolang/gno/gno.land/pkg/gnoland"
"github.com/gnolang/gno/gno.land/pkg/integration"
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"
)

func newInMemorySigner(t *testing.T, chainid string) *SignerFromKeybase {
t.Helper()

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) {
config, _ := integration.TestingNodeConfig(t, gnoland.MustGuessGnoRootDir())
node, remoteAddr := integration.TestingInMemoryNode(t, log.NewNopLogger(), config)
defer node.Stop()

signer := newInMemorySigner(t, config.TMConfig.ChainID())

client := Client{
// Remote: "localhost:12345",
// ChainID: "test",
Signer: signer,
RPCClient: rpcclient.NewHTTP(remoteAddr, "/websocket"),
}
_ = client

data, res, err := client.Render("gno.land/r/demo/boards", "")
require.NoError(t, err)
fmt.Println("data", data)
fmt.Println("res", res)

// TODO: xxx
}

Unchanged files with check annotations Beta

}
// validateSigner checks that the signer is correctly configured.
func (c Client) validateSigner() error {
if c.Signer == nil {
return errors.New("missing Signer")
}
return nil

Check warning on line 19 in gno.land/pkg/gnoclient/client.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/client.go#L15-L19

Added lines #L15 - L19 were not covered by tests
}
// validateRPCClient checks that the RPCClient is correctly configured.
func (c Client) validateRPCClient() error {
if c.RPCClient == nil {
return errors.New("missing RPCClient")
}

Check warning on line 26 in gno.land/pkg/gnoclient/client.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/client.go#L25-L26

Added lines #L25 - L26 were not covered by tests
return nil
}
}
// Query performs a generic query on the blockchain.
func (c Client) Query(cfg QueryCfg) (*ctypes.ResultABCIQuery, error) {
if err := c.validateRPCClient(); err != nil {
return nil, err
}
qres, err := c.RPCClient.ABCIQueryWithOptions(cfg.Path, cfg.Data, cfg.ABCIQueryOptions)
if err != nil {
return nil, errors.Wrap(err, "query error")
}

Check warning on line 29 in gno.land/pkg/gnoclient/client_queries.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_queries.go#L22-L29

Added lines #L22 - L29 were not covered by tests
if qres.Response.Error != nil {
return qres, errors.Wrap(qres.Response.Error, "deliver transaction failed: log:%s", qres.Response.Log)
}

Check warning on line 33 in gno.land/pkg/gnoclient/client_queries.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_queries.go#L31-L33

Added lines #L31 - L33 were not covered by tests
return qres, nil

Check warning on line 35 in gno.land/pkg/gnoclient/client_queries.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_queries.go#L35

Added line #L35 was not covered by tests
}
// QueryAccount retrieves account information for a given address.
func (c Client) QueryAccount(addr crypto.Address) (*std.BaseAccount, *ctypes.ResultABCIQuery, error) {
if err := c.validateRPCClient(); err != nil {
return nil, nil, err
}

Check warning on line 42 in gno.land/pkg/gnoclient/client_queries.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_queries.go#L39-L42

Added lines #L39 - L42 were not covered by tests
path := fmt.Sprintf("auth/accounts/%s", crypto.AddressToBech32(addr))
data := []byte{}
qres, err := c.RPCClient.ABCIQuery(path, data)
if err != nil {
return nil, nil, errors.Wrap(err, "query account")
}
if qres.Response.Data == nil || len(qres.Response.Data) == 0 || string(qres.Response.Data) == "null" {
return nil, nil, std.ErrUnknownAddress("unknown address: " + crypto.AddressToBech32(addr))
}

Check warning on line 53 in gno.land/pkg/gnoclient/client_queries.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_queries.go#L44-L53

Added lines #L44 - L53 were not covered by tests
var qret struct{ BaseAccount std.BaseAccount }
err = amino.UnmarshalJSON(qres.Response.Data, &qret)
if err != nil {
return nil, nil, err
}

Check warning on line 59 in gno.land/pkg/gnoclient/client_queries.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_queries.go#L55-L59

Added lines #L55 - L59 were not covered by tests
return &qret.BaseAccount, qres, nil

Check warning on line 61 in gno.land/pkg/gnoclient/client_queries.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_queries.go#L61

Added line #L61 was not covered by tests
}
func (c Client) QueryAppVersion() (string, *ctypes.ResultABCIQuery, error) {
if err := c.validateRPCClient(); err != nil {
return "", nil, err
}

Check warning on line 67 in gno.land/pkg/gnoclient/client_queries.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_queries.go#L64-L67

Added lines #L64 - L67 were not covered by tests
path := ".app/version"
data := []byte{}
qres, err := c.RPCClient.ABCIQuery(path, data)
if err != nil {
return "", nil, errors.Wrap(err, "query app version")
}

Check warning on line 75 in gno.land/pkg/gnoclient/client_queries.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_queries.go#L69-L75

Added lines #L69 - L75 were not covered by tests
version := string(qres.Response.Value)
return version, qres, nil

Check warning on line 78 in gno.land/pkg/gnoclient/client_queries.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_queries.go#L77-L78

Added lines #L77 - L78 were not covered by tests
}
// Render calls the Render function for pkgPath with optional args. The pkgPath should
// <testnet>/<pkgPath>:<args> where <pkgPath> doesn't have the prefix like "gno.land/".
func (c Client) Render(pkgPath string, args string) (string, *ctypes.ResultABCIQuery, error) {
if err := c.validateRPCClient(); err != nil {
return "", nil, err
}

Check warning on line 87 in gno.land/pkg/gnoclient/client_queries.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_queries.go#L86-L87

Added lines #L86 - L87 were not covered by tests
path := "vm/qrender"
data := []byte(fmt.Sprintf("%s\n%s", pkgPath, args))
qres, err := c.RPCClient.ABCIQuery(path, data)
if err != nil {
return "", nil, errors.Wrap(err, "query render")
}

Check warning on line 95 in gno.land/pkg/gnoclient/client_queries.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_queries.go#L94-L95

Added lines #L94 - L95 were not covered by tests
return string(qres.Response.Data), qres, nil
}
// include the prefix like "gno.land/". The expression is usually a function call like
// "GetBoardIDFromName(\"testboard\")". The return value is a typed expression like
// "(1 gno.land/r/demo/boards.BoardID)\n(true bool)".
func (c Client) QEval(pkgPath string, expression string) (string, *ctypes.ResultABCIQuery, error) {
if err := c.validateRPCClient(); err != nil {
return "", nil, err
}

Check warning on line 107 in gno.land/pkg/gnoclient/client_queries.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_queries.go#L104-L107

Added lines #L104 - L107 were not covered by tests
path := "vm/qeval"
data := []byte(fmt.Sprintf("%s\n%s", pkgPath, expression))
qres, err := c.RPCClient.ABCIQuery(path, data)
if err != nil {
return "", nil, errors.Wrap(err, "query qeval")
}

Check warning on line 115 in gno.land/pkg/gnoclient/client_queries.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_queries.go#L109-L115

Added lines #L109 - L115 were not covered by tests
return string(qres.Response.Data), qres, nil

Check warning on line 117 in gno.land/pkg/gnoclient/client_queries.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_queries.go#L117

Added line #L117 was not covered by tests
}
}
// Call executes a contract call on the blockchain.
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")
}
if err := c.validateRPCClient(); err != nil {
return nil, errors.Wrap(err, "validate RPC client")
}

Check warning on line 32 in gno.land/pkg/gnoclient/client_txs.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_txs.go#L25-L32

Added lines #L25 - L32 were not covered by tests
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")
}

Check warning on line 50 in gno.land/pkg/gnoclient/client_txs.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_txs.go#L34-L50

Added lines #L34 - L50 were not covered by tests
// Parse send amount.
sendCoins, err := std.ParseCoins(send)
if err != nil {
return nil, errors.Wrap(err, "parsing send coins")
}

Check warning on line 56 in gno.land/pkg/gnoclient/client_txs.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_txs.go#L53-L56

Added lines #L53 - L56 were not covered by tests
// Parse gas wanted & fee.
gasFeeCoins, err := std.ParseCoin(gasFee)
if err != nil {
return nil, errors.Wrap(err, "parsing gas fee coin")
}

Check warning on line 62 in gno.land/pkg/gnoclient/client_txs.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_txs.go#L59-L62

Added lines #L59 - L62 were not covered by tests
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)

Check warning on line 81 in gno.land/pkg/gnoclient/client_txs.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_txs.go#L64-L81

Added lines #L64 - L81 were not covered by tests
}
// 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()
if sequenceNumber == 0 || accountNumber == 0 {
account, _, err := c.QueryAccount(caller)
if err != nil {
return nil, errors.Wrap(err, "query account")
}
accountNumber = account.AccountNumber
sequenceNumber = account.Sequence

Check warning on line 94 in gno.land/pkg/gnoclient/client_txs.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_txs.go#L85-L94

Added lines #L85 - L94 were not covered by tests
}
signCfg := SignCfg{
UnsignedTX: tx,
SequenceNumber: sequenceNumber,
AccountNumber: accountNumber,
}
signedTx, err := c.Signer.Sign(signCfg)
if err != nil {
return nil, errors.Wrap(err, "sign")
}

Check warning on line 105 in gno.land/pkg/gnoclient/client_txs.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_txs.go#L97-L105

Added lines #L97 - L105 were not covered by tests
bz, err := amino.Marshal(signedTx)
if err != nil {
return nil, errors.Wrap(err, "marshaling tx binary bytes")
}

Check warning on line 110 in gno.land/pkg/gnoclient/client_txs.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_txs.go#L107-L110

Added lines #L107 - L110 were not covered by tests
bres, err := c.RPCClient.BroadcastTxCommit(bz)
if err != nil {
return nil, errors.Wrap(err, "broadcasting bytes")
}

Check warning on line 115 in gno.land/pkg/gnoclient/client_txs.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_txs.go#L112-L115

Added lines #L112 - L115 were not covered by tests
if bres.CheckTx.IsErr() {
return bres, errors.Wrap(bres.CheckTx.Error, "check transaction failed: log:%s", bres.CheckTx.Log)
}
if bres.DeliverTx.IsErr() {
return bres, errors.Wrap(bres.DeliverTx.Error, "deliver transaction failed: log:%s", bres.DeliverTx.Log)
}

Check warning on line 122 in gno.land/pkg/gnoclient/client_txs.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_txs.go#L117-L122

Added lines #L117 - L122 were not covered by tests
return bres, nil

Check warning on line 124 in gno.land/pkg/gnoclient/client_txs.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_txs.go#L124

Added line #L124 was not covered by tests
}
// TODO: Add more functionality, examples, and unit tests.
ChainID string // Chain ID for transaction signing
}
func (s SignerFromKeybase) Validate() error {
if s.ChainID == "" {
return errors.New("missing ChainID")
}

Check warning on line 30 in gno.land/pkg/gnoclient/signer.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/signer.go#L27-L30

Added lines #L27 - L30 were not covered by tests
_, err := s.Keybase.GetByNameOrAddress(s.Account)
if err != nil {
return err
}

Check warning on line 35 in gno.land/pkg/gnoclient/signer.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/signer.go#L32-L35

Added lines #L32 - L35 were not covered by tests
// To verify if the password unlocks the account, sign a blank transaction.
msg := vm.MsgCall{
Caller: s.Info().GetAddress(),
}
signCfg := SignCfg{
UnsignedTX: std.Tx{
Msgs: []std.Msg{msg},
Fee: std.NewFee(0, std.NewCoin("ugnot", 1000000)),
},
}
if _, err = s.Sign(signCfg); err != nil {
return err
}

Check warning on line 49 in gno.land/pkg/gnoclient/signer.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/signer.go#L38-L49

Added lines #L38 - L49 were not covered by tests
return nil

Check warning on line 51 in gno.land/pkg/gnoclient/signer.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/signer.go#L51

Added line #L51 was not covered by tests
}
func (s SignerFromKeybase) Info() keys.Info {
info, err := s.Keybase.GetByNameOrAddress(s.Account)
if err != nil {
panic("should not happen")

Check warning on line 57 in gno.land/pkg/gnoclient/signer.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/signer.go#L54-L57

Added lines #L54 - L57 were not covered by tests
}
return info

Check warning on line 59 in gno.land/pkg/gnoclient/signer.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/signer.go#L59

Added line #L59 was not covered by tests
}
// Sign implements the Signer interface for SignerFromKeybase.
AccountNumber uint64
}
func (s SignerFromKeybase) Sign(cfg SignCfg) (*std.Tx, error) {
tx := cfg.UnsignedTX
chainID := s.ChainID
accountNumber := cfg.AccountNumber
sequenceNumber := cfg.SequenceNumber
account := s.Account
password := s.Password
// Initialize tx signatures.
signers := tx.GetSigners()
if tx.Signatures == nil {
for range signers {
tx.Signatures = append(tx.Signatures, std.Signature{
PubKey: nil, // Zero signature
Signature: nil, // Zero signature
})
}

Check warning on line 85 in gno.land/pkg/gnoclient/signer.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/signer.go#L69-L85

Added lines #L69 - L85 were not covered by tests
}
// Validate the transaction to sign.
err := tx.ValidateBasic()
if err != nil {
return nil, err
}

Check warning on line 92 in gno.land/pkg/gnoclient/signer.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/signer.go#L89-L92

Added lines #L89 - L92 were not covered by tests
// Derive sign doc bytes.
signbz := tx.GetSignBytes(chainID, accountNumber, sequenceNumber)
sig, pub, err := s.Keybase.Sign(account, password, signbz)
if err != nil {
return nil, err
}
addr := pub.Address()
found := false
for i := range tx.Signatures {
if signers[i] == addr {
found = true
tx.Signatures[i] = std.Signature{
PubKey: pub,
Signature: sig,
}
}

Check warning on line 110 in gno.land/pkg/gnoclient/signer.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/signer.go#L95-L110

Added lines #L95 - L110 were not covered by tests
}
if !found {
return nil, fmt.Errorf("address %v (%s) not in signer set", addr, account)
}

Check warning on line 115 in gno.land/pkg/gnoclient/signer.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/signer.go#L113-L115

Added lines #L113 - L115 were not covered by tests
return &tx, nil

Check warning on line 117 in gno.land/pkg/gnoclient/signer.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/signer.go#L117

Added line #L117 was not covered by tests
}
// Ensure SignerFromKeybase implements the Signer interface.
_, err := kb.CreateAccount(name, mnemonic, passphrase, password, account, index)
if err != nil {
return nil, err
}

Check warning on line 135 in gno.land/pkg/gnoclient/signer.go

Codecov / codecov/patch

gno.land/pkg/gnoclient/signer.go#L134-L135

Added lines #L134 - L135 were not covered by tests
signer := SignerFromKeybase{
Keybase: kb,