Skip to content

Commit 9b56e7a

Browse files
test: add system-tests (#189)
* add:system tests * add: go.mod * update: rename send_test * update: own go mod * block tests * add: accounts, mempool and network tests * some construction endpoints * add: system-test workflow * fix: lint * update: workflows * fix: lint * fix: system tests * fix: makefile rosetta-cli * update: echo sdk version in test-system * rabbit suggestions * update: nosec annotation
1 parent c370232 commit 9b56e7a

20 files changed

+2016
-11
lines changed

.github/workflows/rosetta-cli-test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ jobs:
2121
go-version: '^1.23.4'
2222
- name: Run make test-rosetta-ci
2323
run:
24-
COSMOS_SDK_VERSION=v0.50.3
24+
COSMOS_SDK_VERSION=v0.52.0-rc.1
2525
make test-rosetta-ci
2626
shell: bash

.github/workflows/test.yml

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,31 @@ jobs:
3737
- name: tests
3838
if: env.GIT_DIFF
3939
run: |
40-
make plugin && make test
40+
make plugin && make test
41+
42+
test-system:
43+
runs-on: ubuntu-latest
44+
timeout-minutes: 30
45+
steps:
46+
- uses: actions/checkout@v4
47+
- uses: actions/setup-go@v5
48+
with:
49+
go-version: "1.23"
50+
check-latest: true
51+
cache: true
52+
cache-dependency-path: tests/systemtests/go.sum
53+
- uses: technote-space/[email protected]
54+
id: git_diff
55+
with:
56+
PATTERNS: |
57+
**/*.go
58+
go.mod
59+
go.sum
60+
**/go.mod
61+
**/go.sum
62+
**/Makefile
63+
Makefile
64+
- name: system tests
65+
if: env.GIT_DIFF
66+
run: |
67+
make test-system

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ sim_log_file
3939
x/genutil/config
4040
x/genutil/data
4141
*.fail
42+
tests/systemtests/binaries
43+
tests/systemtests/testnet
4244

4345
# Vagrant
4446
.vagrant/

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
3636

3737
## [Unreleased]
3838

39+
### Features
40+
41+
* [#189](https://github.com/cosmos/rosetta/pull/189) Add system tests.
42+
3943
### Improvements
4044

4145
* [180](https://github.com/cosmos/rosetta/pull/180) Update to cosmos-sdk v0.52.0-rc.1.

Makefile

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
all: build plugin
44

55
build:
6-
go build -mod=readonly ./cmd/rosetta
6+
mkdir -p ./build
7+
go build -mod=readonly -o ./build/rosetta ./cmd/rosetta
78

89
plugin:
910
cd plugins/cosmos-hub && make plugin
@@ -17,10 +18,23 @@ docker:
1718
test:
1819
go test -mod=readonly -timeout 30m -coverprofile=coverage.out -covermode=atomic ./...
1920

21+
.PHONY: test-system
22+
test-system: build
23+
mkdir -p ./tests/systemtests/binaries/
24+
git clone https://github.com/cosmos/cosmos-sdk.git ./build/tmp/cosmos-sdk
25+
$(eval SDK_VERSION := $(shell grep -m 2 'github.com/cosmos/cosmos-sdk' go.mod | awk 'NR==2 {print $$4; exit} END{if(NR==1) print $$2}'))
26+
@echo "Checking out cosmos-sdk version: $(SDK_VERSION)"
27+
cd ./build/tmp/cosmos-sdk && git checkout $(SDK_VERSION)
28+
$(MAKE) -C ./build/tmp/cosmos-sdk build
29+
cp ./build/tmp/cosmos-sdk/build/simd$(if $(findstring v2,$(COSMOS_BUILD_OPTIONS)),v2) ./tests/systemtests/binaries/
30+
cp ./build/rosetta ./tests/systemtests/binaries/
31+
$(MAKE) -C tests/systemtests test
32+
rm -rf ./build/tmp
33+
2034
test-rosetta-ci:
2135
sh ./scripts/simapp-start-node.sh
2236
make build && make plugin
23-
./rosetta --blockchain "cosmos" --network "cosmos" --tendermint "tcp://localhost:26657" --addr "localhost:8080" --grpc "localhost:9090" &
37+
./build/rosetta --blockchain "cosmos" --network "cosmos" --tendermint "tcp://localhost:26657" --addr "localhost:8080" --grpc "localhost:9090" &
2438
sleep 30
2539
export SIMD_BIN=./cosmos-sdk/build/simd && sh ./tests/rosetta-cli/rosetta-cli-test.sh
2640

codec.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func MakeCodec() (*codec.ProtoCodec, codectypes.InterfaceRegistry) {
3131
})
3232
cdc := codec.NewProtoCodec(ir)
3333

34+
sdk.RegisterInterfaces(ir)
3435
authcodec.RegisterInterfaces(ir)
3536
bankcodec.RegisterInterfaces(ir)
3637
cryptocodec.RegisterInterfaces(ir)

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,5 +169,4 @@ require (
169169

170170
replace (
171171
github.com/cosmos/cosmos-sdk => github.com/cosmos/cosmos-sdk v0.52.0-rc.1
172-
// Ensure we're using compatible versions
173172
)

scripts/go-lint-all.bash

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,50 @@
11
#!/usr/bin/env bash
22

3-
set -eu -o pipefail
3+
set -e
44

55
REPO_ROOT="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )/.." &> /dev/null && pwd )"
66
export REPO_ROOT
77

8+
LINT_TAGS="system_test"
89
lint_module() {
910
local root="$1"
1011
shift
11-
cd "$(dirname "$root")" &&
12-
echo "linting $(grep "^module" go.mod) [$(date -Iseconds -u)]" &&
13-
golangci-lint run ./... -c "${REPO_ROOT}/.golangci.yml" "$@"
12+
if [ -f $root ]; then
13+
cd "$(dirname "$root")"
14+
else
15+
cd "$REPO_ROOT/$root"
16+
fi
17+
echo "linting $(grep "^module" go.mod) [$(date -Iseconds -u)]"
18+
golangci-lint run ./... -c "${REPO_ROOT}/.golangci.yml" "$@" --build-tags=${LINT_TAGS}
1419
}
1520
export -f lint_module
1621

17-
find "${REPO_ROOT}" -type f -name go.mod -print0 |
18-
xargs -0 -I{} bash -c 'lint_module "$@"' _ {} "$@" # Prepend go.mod file before command-line args.
22+
if [[ -z "${LINT_DIFF:-}" ]]; then
23+
find "${REPO_ROOT}" -type f -name go.mod -print0 | xargs -0 -I{} bash -c 'lint_module "$@"' _ {} "$@" --build-tags=${LINT_TAGS}
24+
else
25+
if [[ -z $GIT_DIFF ]]; then
26+
GIT_DIFF=$(git diff --name-only) || true
27+
fi
28+
29+
if [[ -z "$GIT_DIFF" ]]; then
30+
echo "no files to lint"
31+
exit 0
32+
fi
33+
34+
GIT_DIFF=$(echo $GIT_DIFF | tr -d "'" | tr ' ' '\n' | grep '\.go$' | grep -v '\.pb\.go$' | grep -Eo '^[^/]+\/[^/]+' | uniq)
35+
36+
lint_sdk=false
37+
for dir in ${GIT_DIFF[@]}; do
38+
if [[ ! -f "$REPO_ROOT/$dir/go.mod" ]]; then
39+
lint_sdk=true
40+
else
41+
lint_module $dir "$@"
42+
fi
43+
done
44+
45+
if [[ $lint_sdk ]]; then
46+
cd "$REPO_ROOT"
47+
echo "linting github.com/cosmos/rosetta [$(date -Iseconds -u)]"
48+
golangci-lint run ./... -c "${REPO_ROOT}/.golangci.yml" "$@" --build-tags=${LINT_TAGS}
49+
fi
50+
fi

tests/systemtests/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/make -f
2+
3+
WAIT_TIME ?= 45s
4+
5+
test:
6+
go test -mod=readonly -failfast -timeout=15m -tags='system_test' ./... --wait-time=$(WAIT_TIME) --verbose

tests/systemtests/accounts_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//go:build system_test
2+
3+
package systemtests
4+
5+
import (
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/tidwall/gjson"
10+
11+
"cosmossdk.io/systemtests"
12+
)
13+
14+
func TestAccounts(t *testing.T) {
15+
sut.ResetChain(t)
16+
cli := systemtests.NewCLIWrapper(t, sut, verbose)
17+
// add genesis account with some tokens
18+
fromAddr := cli.AddKey("account1")
19+
sut.ModifyGenesisCLI(t,
20+
[]string{"genesis", "add-genesis-account", fromAddr, "10000000stake"},
21+
)
22+
toAddr := cli.AddKey("account2")
23+
sut.StartChain(t)
24+
25+
rsp := cli.RunAndWait("tx", "bank", "send", fromAddr, toAddr, "1000000stake")
26+
systemtests.RequireTxSuccess(t, rsp)
27+
28+
rosetta.restart(t)
29+
rosettaRest := newRestClient(rosetta)
30+
31+
// check balance after spent
32+
res, err := rosettaRest.accountBalance(fromAddr)
33+
assert.NoError(t, err)
34+
assert.Equal(t, int64(8999999), gjson.GetBytes(res, "balances.0.value").Int())
35+
36+
// check recipient's balance after receiving tokens
37+
res, err = rosettaRest.accountBalance(toAddr)
38+
assert.NoError(t, err)
39+
assert.Equal(t, int64(1000000), gjson.GetBytes(res, "balances.0.value").Int())
40+
41+
// check balance at genesis, before spent
42+
res, err = rosettaRest.accountBalance(fromAddr, withBlockIdentifier("1"))
43+
assert.NoError(t, err)
44+
assert.Equal(t, int64(10000000), gjson.GetBytes(res, "balances.0.value").Int())
45+
}

tests/systemtests/block_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//go:build system_test
2+
3+
package systemtests
4+
5+
import (
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/tidwall/gjson"
10+
11+
"cosmossdk.io/systemtests"
12+
)
13+
14+
func TestBlockAndBlockTransaction(t *testing.T) {
15+
sut.ResetChain(t)
16+
cli := systemtests.NewCLIWrapper(t, sut, verbose)
17+
// add genesis account with some tokens
18+
fromAddr := cli.AddKey("account1")
19+
sut.ModifyGenesisCLI(t,
20+
[]string{"genesis", "add-genesis-account", fromAddr, "10000000stake"},
21+
)
22+
toAddr := cli.AddKey("account2")
23+
sut.StartChain(t)
24+
25+
rosetta.restart(t)
26+
27+
// stake tokens
28+
rsp := cli.RunAndWait("tx", "bank", "send", fromAddr, toAddr, "10stake")
29+
systemtests.RequireTxSuccess(t, rsp)
30+
31+
rosettaRest := newRestClient(rosetta)
32+
33+
// test /block endpoint
34+
height := gjson.Get(rsp, "height").String()
35+
res, err := rosettaRest.block(height)
36+
assert.NoError(t, err)
37+
assert.Equal(t, gjson.GetBytes(res, "block.block_identifier.index").String(), height)
38+
39+
// test block/transaction endpoint
40+
blockHash := gjson.GetBytes(res, "block.block_identifier.hash").String()
41+
hash := gjson.GetBytes(res, "block.transactions.0.transaction_identifier.hash").String()
42+
res, err = rosettaRest.blockTransaction(height, blockHash, hash)
43+
assert.NoError(t, err)
44+
assert.Equal(t, gjson.GetBytes(res, "transaction.operations.0.metadata.from_address").String(), fromAddr)
45+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//go:build system_test
2+
3+
package systemtests
4+
5+
import (
6+
"encoding/base64"
7+
"encoding/hex"
8+
"strings"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
"github.com/tidwall/gjson"
13+
14+
"cosmossdk.io/systemtests"
15+
16+
"github.com/cosmos/cosmos-sdk/codec/address"
17+
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
18+
)
19+
20+
func TestDerive(t *testing.T) {
21+
sut.ResetChain(t)
22+
sut.StartChain(t)
23+
24+
rosetta.restart(t)
25+
rosettaRest := newRestClient(rosetta)
26+
27+
pubKey := secp256k1.GenPrivKey().PubKey()
28+
addr, err := address.NewBech32Codec("cosmos").BytesToString(pubKey.Address().Bytes())
29+
assert.NoError(t, err)
30+
31+
hexPk := strings.Split(pubKey.String(), "{")[1]
32+
res, err := rosettaRest.constructionDerive(hexPk[:len(hexPk)-1])
33+
assert.NoError(t, err)
34+
assert.Equal(t, addr, gjson.GetBytes(res, "address").String())
35+
}
36+
37+
func TestHash(t *testing.T) {
38+
sut.ResetChain(t)
39+
sut.StartChain(t)
40+
41+
cli := systemtests.NewCLIWrapper(t, sut, verbose)
42+
fromAddr := cli.AddKey("account1")
43+
sut.ModifyGenesisCLI(t,
44+
[]string{"genesis", "add-genesis-account", fromAddr, "10000000stake"},
45+
)
46+
toAddr := cli.AddKey("account2")
47+
48+
rosetta.restart(t)
49+
rosettaRest := newRestClient(rosetta)
50+
51+
rsp := cli.RunCommandWithArgs(cli.WithTXFlags("tx", "bank", "send", fromAddr, toAddr, "10stake", "--generate-only")...)
52+
tempFile := systemtests.StoreTempFile(t, []byte(rsp))
53+
rsp = cli.RunCommandWithArgs(cli.WithTXFlags("tx", "sign", tempFile.Name(), "--from", fromAddr)...)
54+
tempFile = systemtests.StoreTempFile(t, []byte(rsp))
55+
rsp = cli.RunCommandWithArgs("tx", "encode", tempFile.Name())
56+
57+
txBytes, err := base64.StdEncoding.DecodeString(rsp)
58+
assert.NoError(t, err)
59+
hexTx := hex.EncodeToString(txBytes)
60+
61+
res, err := rosettaRest.constructionHash(hexTx)
62+
assert.NoError(t, err)
63+
assert.NotEmpty(t, gjson.GetBytes(res, "transaction_identifier.hash"))
64+
}
65+
66+
func TestMetadata(t *testing.T) {
67+
sut.ResetChain(t)
68+
sut.StartChain(t)
69+
70+
rosetta.restart(t)
71+
rosettaRest := newRestClient(rosetta)
72+
73+
pubKey := secp256k1.GenPrivKey().PubKey()
74+
hexPk := strings.Split(pubKey.String(), "{")[1]
75+
76+
metadata := make(map[string]interface{})
77+
metadata["gas_price"] = `"123uatom"`
78+
metadata["gas_limit"] = 423
79+
80+
res, err := rosettaRest.constructionMetadata(hexPk, metadata)
81+
assert.NoError(t, err)
82+
assert.Equal(t, gjson.GetBytes(res, "metadata.gas_price").String(), "123uatom")
83+
assert.Greater(t, gjson.GetBytes(res, "suggested_fee.0.value").Int(), int64(0))
84+
}

0 commit comments

Comments
 (0)