Skip to content

Commit

Permalink
Merge branch 'master' into expose-genesis-response
Browse files Browse the repository at this point in the history
  • Loading branch information
n0izn0iz authored Jul 22, 2024
2 parents ecdf0f2 + fec2d18 commit 973a73f
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 41 deletions.
1 change: 1 addition & 0 deletions gno.land/cmd/gnoland/testdata/addpkg.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ stdout 'GAS WANTED: 2000000'
stdout 'GAS USED: \d+'
stdout 'HEIGHT: \d+'
stdout 'EVENTS: \[\]'
stdout 'TX HASH: '

-- hello.gno --
package hello
Expand Down
2 changes: 1 addition & 1 deletion gno.land/cmd/gnoland/testdata/addpkg_invalid.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ gnoland start
! gnokey maketx addpkg -pkgdir $WORK -pkgpath gno.land/r/foobar/bar -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1

# check error message
! stdout .+
stdout 'TX HASH: '
stderr 'as string value in return statement'
stderr '"std" imported and not used'

Expand Down
2 changes: 1 addition & 1 deletion gno.land/cmd/gnoland/testdata/event_callback.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ stdout 'GAS WANTED: 2000000'
stdout 'GAS USED: [0-9]+'
stdout 'HEIGHT: [0-9]+'
stdout 'EVENTS: \[{\"type\":\"foo\",\"attrs\":\[{\"key\":\"k1\",\"value\":\"v1\"},{\"key\":\"k2\",\"value\":\"v2\"}],\"pkg_path\":\"gno.land\/r\/demo\/cbee\",\"func\":\"subFoo\"},{\"type\":\"bar\",\"attrs\":\[{\"key\":\"bar\",\"value\":\"baz\"}],\"pkg_path\":\"gno.land\/r\/demo\/cbee\",\"func\":\"subBar\"}]'

stdout 'TX HASH: '

-- cbee.gno --
package cbee
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ stdout 'GAS WANTED: 2000000'
stdout 'GAS USED: [0-9]+'
stdout 'HEIGHT: [0-9]+'
stdout 'EVENTS: \[{\"type\":\"ForLoopEvent\",\"attrs\":\[{\"key\":\"iteration\",\"value\":\"0\"},{\"key\":\"key\",\"value\":\"value\"}\],\"pkg_path\":\"gno.land\/r\/demo\/edcl\",\"func\":\"\"},{\"type\":\"ForLoopEvent\",\"attrs\":\[{\"key\":\"iteration\",\"value\":\"1\"},{\"key\":\"key\",\"value\":\"value\"}\],\"pkg_path\":\"gno.land\/r\/demo\/edcl\",\"func\":\"\"},{\"type\":\"ForLoopEvent\",\"attrs\":\[{\"key\":\"iteration\",\"value\":\"2\"},{\"key\":\"key\",\"value\":\"value\"}\],\"pkg_path\":\"gno.land\/r\/demo\/edcl\",\"func\":\"\"},{\"type\":\"ForLoopCompletionEvent\",\"attrs\":\[{\"key\":\"count\",\"value\":\"3\"}\],\"pkg_path\":\"gno.land\/r\/demo\/edcl\",\"func\":\"forLoopEmitExample\"},{\"type\":\"CallbackEvent\",\"attrs\":\[{\"key\":\"key1\",\"value\":\"value1\"},{\"key\":\"key2\",\"value\":\"value2\"}\],\"pkg_path\":\"gno.land\/r\/demo\/edcl\",\"func\":\"\"},{\"type\":\"CallbackCompletionEvent\",\"attrs\":\[{\"key\":\"key\",\"value\":\"value\"}\],\"pkg_path\":\"gno.land\/r\/demo\/edcl\",\"func\":\"callbackEmitExample\"},{\"type\":\"DeferEvent\",\"attrs\":\[{\"key\":\"key1\",\"value\":\"value1\"},{\"key\":\"key2\",\"value\":\"value2\"}\],\"pkg_path\":\"gno.land\/r\/demo\/edcl\",\"func\":\"deferEmitExample\"}\]'
stdout 'TX HASH: '

-- edcl.gno --

Expand Down
3 changes: 2 additions & 1 deletion gno.land/cmd/gnoland/testdata/event_for_statement.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ stdout 'GAS WANTED: 2000000'
stdout 'GAS USED: [0-9]+'
stdout 'HEIGHT: [0-9]+'
stdout 'EVENTS: \[{\"type\":\"testing\",\"attrs\":\[{\"key\":\"foo\",\"value\":\"bar\"}\],\"pkg_path\":\"gno.land\/r\/demo\/foree\",\"func\":\"Foo\"},{\"type\":\"testing\",\"attrs\":\[{\"key\":\"foo\",\"value\":\"bar\"}\],\"pkg_path\":\"gno.land\/r\/demo\/foree\",\"func\":\"Foo\"},{\"type\":\"testing\",\"attrs\":\[{\"key\":\"foo\",\"value\":\"bar\"}\],\"pkg_path\":\"gno.land\/r\/demo\/foree\",\"func\":\"Foo\"},{\"type\":\"testing\",\"attrs\":\[{\"key\":\"foo\",\"value\":\"bar\"}\],\"pkg_path\":\"gno.land\/r\/demo\/foree\",\"func\":\"Foo\"},{\"type\":\"testing\",\"attrs\":\[{\"key\":\"foo\",\"value\":\"bar\"}\],\"pkg_path\":\"gno.land\/r\/demo\/foree\",\"func\":\"Foo\"},{\"type\":\"testing\",\"attrs\":\[{\"key\":\"foo\",\"value\":\"bar\"}\],\"pkg_path\":\"gno.land\/r\/demo\/foree\",\"func\":\"Foo\"},{\"type\":\"testing\",\"attrs\":\[{\"key\":\"foo\",\"value\":\"bar\"}\],\"pkg_path\":\"gno.land\/r\/demo\/foree\",\"func\":\"Foo\"},{\"type\":\"testing\",\"attrs\":\[{\"key\":\"foo\",\"value\":\"bar\"}\],\"pkg_path\":\"gno.land\/r\/demo\/foree\",\"func\":\"Foo\"},{\"type\":\"testing\",\"attrs\":\[{\"key\":\"foo\",\"value\":\"bar\"}\],\"pkg_path\":\"gno.land\/r\/demo\/foree\",\"func\":\"Foo\"},{\"type\":\"testing\",\"attrs\":\[{\"key\":\"foo\",\"value\":\"bar\"}\],\"pkg_path\":\"gno.land\/r\/demo\/foree\",\"func\":\"Foo\"}\]'
stdout 'TX HASH: '

gnokey maketx call -pkgpath gno.land/r/demo/foree -func Bar -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
stdout OK!
stdout 'GAS WANTED: 2000000'
stdout 'GAS USED: [0-9]+'
stdout 'HEIGHT: [0-9]+'
stdout 'EVENTS: \[{\"type\":\"Foo\",\"attrs\":\[{\"key\":\"k1\",\"value\":\"v1\"},{\"key\":\"k2\",\"value\":\"v2\"}\],\"pkg_path\":\"gno.land\/r\/demo\/foree\",\"func\":\"subFoo\"},{\"type\":\"Bar\",\"attrs\":\[{\"key\":\"bar\",\"value\":\"baz\"}\],\"pkg_path\":\"gno.land\/r\/demo\/foree\",\"func\":\"subBar\"},{\"type\":\"Foo\",\"attrs\":\[{\"key\":\"k1\",\"value\":\"v1\"},{\"key\":\"k2\",\"value\":\"v2\"}\],\"pkg_path\":\"gno.land\/r\/demo\/foree\",\"func\":\"subFoo\"},{\"type\":\"Bar\",\"attrs\":\[{\"key\":\"bar\",\"value\":\"baz\"}\],\"pkg_path\":\"gno.land\/r\/demo\/foree\",\"func\":\"subBar\"}\]'

stdout 'TX HASH: '

-- foree.gno --

Expand Down
2 changes: 2 additions & 0 deletions gno.land/cmd/gnoland/testdata/event_normal.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ stdout 'GAS WANTED: 2000000'
stdout 'GAS USED: \d+'
stdout 'HEIGHT: \d+'
stdout 'EVENTS: \[{\"type\":\"foo\",\"attrs\":\[{\"key\":\"key1\",\"value\":\"value1\"},{\"key\":\"key2\",\"value\":\"value2\"},{\"key\":\"key3\",\"value\":\"value3\"}\],\"pkg_path\":\"gno.land\/r\/demo\/ee\",\"func\":\"SubFoo\"},{\"type\":\"bar\",\"attrs\":\[{\"key\":\"bar\",\"value\":\"baz\"}\],\"pkg_path\":\"gno.land\/r\/demo\/ee\",\"func\":\"SubBar\"}\]'
stdout 'TX HASH: '

gnokey maketx call -pkgpath gno.land/r/demo/ee -func Bar -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
stdout OK!
stdout 'GAS WANTED: 2000000'
stdout 'GAS USED: \d+'
stdout 'HEIGHT: \d+'
stdout 'EVENTS: \[{\"type\":\"bar\",\"attrs\":\[{\"key\":\"foo\",\"value\":\"bar\"}\],\"pkg_path\":\"gno.land/r/demo/ee\",\"func\":\"Bar\"}\]'
stdout 'TX HASH: '

-- ee.gno --
package ee
Expand Down
1 change: 1 addition & 0 deletions gno.land/cmd/gnoland/testdata/run.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ stdout 'main: --- hello from foo ---'
stdout 'OK!'
stdout 'GAS WANTED: 200000'
stdout 'GAS USED: '
stdout 'TX HASH: '

-- bar/bar.gno --
package bar
Expand Down
1 change: 1 addition & 0 deletions gno.land/pkg/integration/testdata/adduser.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ stdout 'main: --- hello from foo ---'
stdout 'OK!'
stdout 'GAS WANTED: 200000'
stdout 'GAS USED: '
stdout 'TX HASH: '

# should fail if user is added after node is started
! adduser test5
Expand Down
1 change: 1 addition & 0 deletions gno.land/pkg/integration/testdata/loadpkg_work.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ stdout 'main: --- hello from foo ---'
stdout 'OK!'
stdout 'GAS WANTED: 200000'
stdout 'GAS USED: '
stdout 'TX HASH: '

-- bar/bar.gno --
package bar
Expand Down
9 changes: 6 additions & 3 deletions tm2/pkg/bft/rpc/lib/client/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const (
protoHTTP = "http"
protoHTTPS = "https"
protoTCP = "tcp"

portHTTP = "80"
portHTTPS = "443"
)

var (
Expand Down Expand Up @@ -57,7 +60,7 @@ func (c *Client) SendRequest(ctx context.Context, request types.RPCRequest) (*ty
}

// Make sure the ID matches
if response.ID != response.ID {
if request.ID != response.ID {
return nil, ErrRequestResponseIDMismatch
}

Expand Down Expand Up @@ -230,9 +233,9 @@ func parseRemoteAddr(remoteAddr string) (string, string) {
if !strings.Contains(address, ":") {
switch protocol {
case protoHTTPS:
address += ":443"
address += ":" + portHTTPS
case protoHTTP, protoTCP:
address += ":80"
address += ":" + portHTTP
default: // noop
}
}
Expand Down
118 changes: 83 additions & 35 deletions tm2/pkg/bft/rpc/lib/client/http/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,53 +108,101 @@ func createTestServer(
func TestClient_SendRequest(t *testing.T) {
t.Parallel()

var (
request = types.RPCRequest{
JSONRPC: "2.0",
ID: types.JSONRPCStringID("id"),
}
t.Run("valid request, response", func(t *testing.T) {
t.Parallel()

handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, http.MethodPost, r.Method)
require.Equal(t, "application/json", r.Header.Get("content-type"))
var (
request = types.RPCRequest{
JSONRPC: "2.0",
ID: types.JSONRPCStringID("id"),
}

// Parse the message
var req types.RPCRequest
require.NoError(t, json.NewDecoder(r.Body).Decode(&req))
require.Equal(t, request.ID.String(), req.ID.String())
handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, http.MethodPost, r.Method)
require.Equal(t, "application/json", r.Header.Get("content-type"))

// Send an empty response back
response := types.RPCResponse{
// Parse the message
var req types.RPCRequest
require.NoError(t, json.NewDecoder(r.Body).Decode(&req))
require.Equal(t, request.ID.String(), req.ID.String())

// Send an empty response back
response := types.RPCResponse{
JSONRPC: "2.0",
ID: req.ID,
}

// Marshal the response
marshalledResponse, err := json.Marshal(response)
require.NoError(t, err)

_, err = w.Write(marshalledResponse)
require.NoError(t, err)
})

server = createTestServer(t, handler)
)

// Create the client
c, err := NewClient(server.URL)
require.NoError(t, err)

ctx, cancelFn := context.WithTimeout(context.Background(), time.Second*5)
defer cancelFn()

// Send the request
resp, err := c.SendRequest(ctx, request)
require.NoError(t, err)

assert.Equal(t, request.ID, resp.ID)
assert.Equal(t, request.JSONRPC, resp.JSONRPC)
assert.Nil(t, resp.Result)
assert.Nil(t, resp.Error)
})

t.Run("response ID mismatch", func(t *testing.T) {
t.Parallel()

var (
request = types.RPCRequest{
JSONRPC: "2.0",
ID: req.ID,
ID: types.JSONRPCStringID("id"),
}

// Marshal the response
marshalledResponse, err := json.Marshal(response)
require.NoError(t, err)
handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, http.MethodPost, r.Method)
require.Equal(t, "application/json", r.Header.Get("content-type"))

_, err = w.Write(marshalledResponse)
require.NoError(t, err)
})
// Send an empty response back,
// with an invalid ID
response := types.RPCResponse{
JSONRPC: "2.0",
ID: types.JSONRPCStringID("totally random ID"),
}

server = createTestServer(t, handler)
)
// Marshal the response
marshalledResponse, err := json.Marshal(response)
require.NoError(t, err)

// Create the client
c, err := NewClient(server.URL)
require.NoError(t, err)
_, err = w.Write(marshalledResponse)
require.NoError(t, err)
})

ctx, cancelFn := context.WithTimeout(context.Background(), time.Second*5)
defer cancelFn()
server = createTestServer(t, handler)
)

// Send the request
resp, err := c.SendRequest(ctx, request)
require.NoError(t, err)
// Create the client
c, err := NewClient(server.URL)
require.NoError(t, err)

assert.Equal(t, request.ID, resp.ID)
assert.Equal(t, request.JSONRPC, resp.JSONRPC)
assert.Nil(t, resp.Result)
assert.Nil(t, resp.Error)
ctx, cancelFn := context.WithTimeout(context.Background(), time.Second*5)
defer cancelFn()

// Send the request
resp, err := c.SendRequest(ctx, request)
assert.Nil(t, resp)
assert.ErrorIs(t, err, ErrRequestResponseIDMismatch)
})
}

func TestClient_SendBatchRequest(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions tm2/pkg/crypto/keys/client/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"context"
"encoding/base64"
"flag"
"os"

Expand Down Expand Up @@ -79,6 +80,7 @@ func execBroadcast(cfg *BroadcastCfg, args []string, io commands.IO) error {
if res.CheckTx.IsErr() {
return errors.New("transaction failed %#v\nlog %s", res, res.CheckTx.Log)
} else if res.DeliverTx.IsErr() {
io.Println("TX HASH: ", base64.StdEncoding.EncodeToString(res.Hash))
return errors.New("transaction failed %#v\nlog %s", res, res.DeliverTx.Log)
} else {
io.Println(string(res.DeliverTx.Data))
Expand All @@ -87,6 +89,7 @@ func execBroadcast(cfg *BroadcastCfg, args []string, io commands.IO) error {
io.Println("GAS USED: ", res.DeliverTx.GasUsed)
io.Println("HEIGHT: ", res.Height)
io.Println("EVENTS: ", string(res.DeliverTx.EncodeEvents()))
io.Println("TX HASH: ", base64.StdEncoding.EncodeToString(res.Hash))
}
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions tm2/pkg/crypto/keys/client/maketx.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"encoding/base64"
"flag"
"fmt"

Expand Down Expand Up @@ -210,6 +211,7 @@ func ExecSignAndBroadcast(
return errors.Wrap(bres.CheckTx.Error, "check transaction failed: log:%s", bres.CheckTx.Log)
}
if bres.DeliverTx.IsErr() {
io.Println("TX HASH: ", base64.StdEncoding.EncodeToString(bres.Hash))
return errors.Wrap(bres.DeliverTx.Error, "deliver transaction failed: log:%s", bres.DeliverTx.Log)
}

Expand All @@ -219,6 +221,7 @@ func ExecSignAndBroadcast(
io.Println("GAS USED: ", bres.DeliverTx.GasUsed)
io.Println("HEIGHT: ", bres.Height)
io.Println("EVENTS: ", string(bres.DeliverTx.EncodeEvents()))
io.Println("TX HASH: ", base64.StdEncoding.EncodeToString(bres.Hash))

return nil
}

0 comments on commit 973a73f

Please sign in to comment.