Skip to content

Commit

Permalink
add: payloads systemtest
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianToledano committed Jan 22, 2025
1 parent 38b308f commit 3314ee9
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 18 deletions.
2 changes: 1 addition & 1 deletion tests/systemtests/accounts_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:build system_test

package systemtests
package rossetaSystemTests

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion tests/systemtests/block_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:build system_test

package systemtests
package rossetaSystemTests

import (
"testing"
Expand Down
36 changes: 32 additions & 4 deletions tests/systemtests/construction_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//go:build system_test

package systemtests
package rossetaSystemTests

import (
"encoding/base64"
"encoding/hex"
"fmt"
"strings"
"testing"

Expand All @@ -25,12 +26,13 @@ func TestDerive(t *testing.T) {
rosettaRest := newRestClient(rosetta)

pubKey := secp256k1.GenPrivKey().PubKey()
addr, err := address.NewBech32Codec("cosmos").BytesToString(pubKey.Address().Bytes())
assert.NoError(t, err)

hexPk := strings.Split(pubKey.String(), "{")[1]

res, err := rosettaRest.constructionDerive(hexPk[:len(hexPk)-1])
assert.NoError(t, err)

addr, err := address.NewBech32Codec("cosmos").BytesToString(pubKey.Address().Bytes())
assert.NoError(t, err)
assert.Equal(t, addr, gjson.GetBytes(res, "address").String())
}

Expand Down Expand Up @@ -82,3 +84,29 @@ func TestMetadata(t *testing.T) {
assert.Equal(t, gjson.GetBytes(res, "metadata.gas_price").String(), "123uatom")
assert.Greater(t, gjson.GetBytes(res, "suggested_fee.0.value").Int(), int64(0))
}

func TestPayloads(t *testing.T) {
sut.ResetChain(t)
sut.StartChain(t)

rosetta.restart(t)
rosettaRest := newRestClient(rosetta)

cli := systemtests.NewCLIWrapper(t, sut, verbose)
addr := cli.GetKeyAddr("node0")
bz, err := base64.StdEncoding.DecodeString(cli.GetPubKeyByCustomField(addr, "address"))
assert.NoError(t, err)

pk := secp256k1.PubKey{Key: bz}
hexPk := strings.Split(pk.String(), "{")[1]
hexPk = hexPk[:len(hexPk)-1]

op := operation{
msgType: "/cosmos.bank.v1beta1.MsgSend",
metadata: fmt.Sprintf(`{"from_address": "%s", "to_address": "%s", "amount":[{"amount":"123", "denom":"stake"}]}`, addr, "cosmos1qcym25uch54xmja4eys35zcyr8npwlm80glwuk"),
}
res, err := rosettaRest.constructionPayloads(`"signer_data":[{"account_number":1, "sequence": 0}]`, hexPk, op)
assert.NoError(t, err)
assert.NotEmpty(t, gjson.GetBytes(res, "unsigned_transaction"))
assert.Equal(t, gjson.GetBytes(res, "payloads.0.address").String(), addr)
}
2 changes: 1 addition & 1 deletion tests/systemtests/main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:build system_test

package systemtests
package rossetaSystemTests

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion tests/systemtests/mempool_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:build system_test

package systemtests
package rossetaSystemTests

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion tests/systemtests/network_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:build system_test

package systemtests
package rossetaSystemTests

import (
"testing"
Expand Down
34 changes: 33 additions & 1 deletion tests/systemtests/rest_client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package systemtests
package rossetaSystemTests

import (
"fmt"
Expand Down Expand Up @@ -156,3 +156,35 @@ func (c *restClient) constructionMetadata(hexPk string, options map[string]inter

return res.Body(), nil
}

func (c *restClient) constructionPayloads(metadata, hexPk string, opts ...operation) ([]byte, error) {
body := fmt.Sprintf(
`{%s, "operations":%s, "metadata":{%s}, "public_keys":[{"hex_bytes":"%s", "curve_type":"secp256k1"}]}`,
c.networkIdentifier, payloadsOperations(opts...), metadata, hexPk)
res, err := c.client.R().SetBody(
body,
).Post("/construction/payloads")
if err != nil {
return nil, err
}

return res.Body(), nil
}

type operation struct {
msgType string
metadata string
}

func (o *operation) String() string {
return fmt.Sprintf(`"type":"%s", "metadata": %s`, o.msgType, o.metadata)
}

func payloadsOperations(ops ...operation) []string {
r := make([]string, len(ops))
for i, op := range ops {
r = append(r, fmt.Sprintf(`{"operation_identifier":{"index": %d}, %s}`, i, op.String()))
}

return r
}
7 changes: 2 additions & 5 deletions tests/systemtests/rosetta.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package systemtests
package rossetaSystemTests

import (
"bufio"
Expand Down Expand Up @@ -35,7 +35,6 @@ type rosettaRunner struct {
Network string // the network name (default "network")
Plugin string // plugin folder name
Tendermint string // CometBFT rpc endpoint
Offline bool // run rosetta only with construction API
verbose bool
out io.Writer
outBuff *ring.Ring
Expand All @@ -46,7 +45,7 @@ type rosettaRunner struct {
outputDir string
}

func newRosettaRunner(binary, denom, grpcTypesServer, plugin string, offline, verbose bool) rosettaRunner {
func newRosettaRunner(binary, denom, grpcTypesServer, plugin string, verbose bool) rosettaRunner {
execBinary := filepath.Join(systemtests.WorkDir, "binaries", binary)
return rosettaRunner{
execBinary: execBinary,
Expand All @@ -58,7 +57,6 @@ func newRosettaRunner(binary, denom, grpcTypesServer, plugin string, offline, ve
Network: "cosmos",
Plugin: plugin,
Tendermint: "tcp://localhost:26657",
Offline: offline,
out: os.Stdout,
outBuff: ring.New(100),
errBuff: ring.New(100),
Expand All @@ -77,7 +75,6 @@ func (r *rosettaRunner) start(t *testing.T) {
"--addr", r.Addr,
"--grpc", r.GRPC,
"--grpc-types-server", r.GRPCTypesServer,
"--plugin", r.Plugin,
}

r.log("Start Rosetta\n")
Expand Down
5 changes: 2 additions & 3 deletions tests/systemtests/test_runner.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package systemtests
package rossetaSystemTests

import (
"flag"
Expand Down Expand Up @@ -33,7 +33,6 @@ func RunTests(m *testing.M) {
rosettaDenom := flag.String("rosetta-denom", "ustake", "rosetta denom to suggest")
rosettaGRPCTypesServer := flag.String("rosetta-grpc-types-server", "localhost:9090", "rosetta gRPC Server endpoint for proto messages types and reflection")
rosettaPlugin := flag.String("rosetta-plugin", "", "rosetta plugin folder name")
rosettaOffline := flag.Bool("rosetta-offline", false, "rosetta run only with construction API")
flag.Parse()

requireEnoughFileHandlers(*nodesCount + 1) // +1 as tests may start another node
Expand All @@ -56,7 +55,7 @@ func RunTests(m *testing.M) {
sut = systemtests.NewSystemUnderTest(*execBinary, verbose, *nodesCount, *blockTime)
sut.SetupChain() // setup chain and keyring

rosetta = newRosettaRunner(*rosettaBinary, *rosettaDenom, *rosettaGRPCTypesServer, *rosettaPlugin, *rosettaOffline, verbose)
rosetta = newRosettaRunner(*rosettaBinary, *rosettaDenom, *rosettaGRPCTypesServer, *rosettaPlugin, verbose)

// run tests
exitCode := m.Run()
Expand Down

0 comments on commit 3314ee9

Please sign in to comment.