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 go-sdk and e2e test framework #48

Merged
merged 24 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CGO_CFLAGS="-O -D__BLST_PORTABLE__"
CGO_CFLAGS_ALLOW="-O -D__BLST_PORTABLE__"
5 changes: 3 additions & 2 deletions .github/workflows/e2e-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@ jobs:
make build
- name: run local chain
run: bash ./deployment/localup/localup.sh all 1
- name: run cli test
run: bash ./e2e/cli_test.sh
- name: run e2e test
run: |
make e2e_test
20 changes: 17 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.PHONY: build build-linux build-macos build-windows
.PHONY: tools proto-gen proto-format test
.PHONY: tools proto-gen proto-format test e2e_test ci lint

VERSION=$(shell git describe --tags --always)
GIT_COMMIT=$(shell git rev-parse HEAD)
Expand All @@ -12,6 +12,8 @@ ldflags = -X $(REPO)/version.AppVersion=$(VERSION) \
-X $(REPO)/version.GitCommit=$(GIT_COMMIT) \
-X $(REPO)/version.GitCommitDate=$(GIT_COMMIT_DATE)

include .env

format:
bash scripts/format.sh

Expand All @@ -27,12 +29,24 @@ proto-swagger-gen:
proto-format:
buf format -w

proto-format-check:
buf format --diff --exit-code

build:
CGO_CFLAGS="-O -D__BLST_PORTABLE__" CGO_CFLAGS_ALLOW="-O -D__BLST_PORTABLE__" go build -o build/bin/gnfd -ldflags="$(ldflags)" ./cmd/gnfd/main.go
go build -o build/bin/gnfd -ldflags="$(ldflags)" ./cmd/gnfd/main.go

docker-image:
go mod vendor # temporary, should be removed after open source
docker build . -t ${IMAGE_NAME}

test:
go test ./...
go test $$(go list ./... | grep -v e2e | grep -v sdk)

e2e_test:
go test ./e2e/...

lint:
golangci-lint run --fix

ci: proto-format-check build test e2e_test lint
echo "ci passed"
68 changes: 68 additions & 0 deletions e2e/core/basesuite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package core

import (
"github.com/bnb-chain/greenfield/sdk/client"
"github.com/bnb-chain/greenfield/sdk/keys"
"github.com/bnb-chain/greenfield/sdk/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/stretchr/testify/suite"
)

type BaseSuite struct {
suite.Suite
config *Config
Client client.GreenfieldClient
TestAccount keys.KeyManager
}

func (s *BaseSuite) SetupSuite() {
s.config = InitConfig()
s.Client = client.NewGreenfieldClient(s.config.GrpcAddr, s.config.ChainId)
var err error
s.TestAccount, err = keys.NewMnemonicKeyManager(s.config.Mnemonic)
s.Require().NoError(err)
}

func (s *BaseSuite) SendTxBlock(msg sdk.Msg, from keys.KeyManager) (txRes *sdk.TxResponse) {
mode := tx.BroadcastMode_BROADCAST_MODE_BLOCK
txOpt := &types.TxOption{
Mode: &mode,
GasLimit: 1000000,
Memo: "",
FeeAmount: sdk.Coins{{Denom: s.config.Denom, Amount: sdk.NewInt(1)}},
}
s.Client.SetKeyManager(from)
response, err := s.Client.BroadcastTx([]sdk.Msg{msg}, txOpt)
s.Require().NoError(err)
s.T().Logf("tx_hash: %s", response.TxResponse.TxHash)
s.Require().Equal(response.TxResponse.Code, uint32(0))
return response.TxResponse
}

func (s *BaseSuite) GenAndChargeAccounts(n int, balance int64) (accounts []keys.KeyManager) {
var outputs []banktypes.Output
denom := s.config.Denom
for i := 0; i < n; i++ {
km := GenRandomKeyManager()
accounts = append(accounts, km)
outputs = append(outputs, banktypes.Output{
Address: km.GetAddr().String(),
Coins: []sdk.Coin{{Denom: denom, Amount: sdk.NewInt(balance)}},
})
}
if balance == 0 {
return
}
in := banktypes.Input{
Address: s.TestAccount.GetAddr().String(),
Coins: []sdk.Coin{{Denom: denom, Amount: sdk.NewInt(balance * int64(n))}},
}
msg := banktypes.MsgMultiSend{
Inputs: []banktypes.Input{in},
Outputs: outputs,
}
_ = s.SendTxBlock(&msg, s.TestAccount)
return accounts
}
46 changes: 46 additions & 0 deletions e2e/core/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package core

import (
"bufio"
"fmt"
"os"
)

type Config struct {
GrpcAddr string `yaml:"GrpcAddr"`
ChainId string `yaml:"ChainId"`
Mnemonic string `yaml:"Mnemonic"` // test account mnemonic with enough balance
Denom string `yaml:"Denom"`
}

func InitConfig() *Config {
// todo: support qa and testnet config
return InitE2eConfig()
}

func InitE2eConfig() *Config {
return &Config{
GrpcAddr: "localhost:9090",
ChainId: "greenfield_9000-121",
Denom: "bnb",
Mnemonic: ParseValidatorMnemonic(0),
}
}

// ParseValidatorMnemonic read a file and return the last non-empty line
func ParseValidatorMnemonic(i int) string {
file, err := os.Open(fmt.Sprintf("../../deployment/localup/.local/validator%d/info", i))
if err != nil {
panic(err)
}
defer file.Close()

scanner := bufio.NewScanner(file)
var line string
for scanner.Scan() {
if scanner.Text() != "" {
line = scanner.Text()
}
}
return line
}
31 changes: 31 additions & 0 deletions e2e/core/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package core

import (
"fmt"
"math/rand"

"github.com/bnb-chain/greenfield/sdk/keys"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/tendermint/tendermint/crypto"
)

func GenRandomAddr() sdk.AccAddress {
return sdk.AccAddress(crypto.AddressHash([]byte(fmt.Sprintf("%d", rand.Int()))))
}

func GenRandomHexString(len int) string {
b := make([]byte, len)
_, err := rand.Read(b)
if err != nil {
panic(err)
}
return fmt.Sprintf("%x", b)
}

func GenRandomKeyManager() keys.KeyManager {
keyManager, err := keys.NewPrivateKeyManager(GenRandomHexString(32))
if err != nil {
panic(err)
}
return keyManager
}
65 changes: 65 additions & 0 deletions e2e/tests/payment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package tests

// Basic imports
import (
"context"
"testing"

"github.com/bnb-chain/greenfield/e2e/core"
paymenttypes "github.com/bnb-chain/greenfield/x/payment/types"
"github.com/stretchr/testify/suite"
)

type PaymentTestSuite struct {
core.BaseSuite
}

func (s *PaymentTestSuite) SetupSuite() {
s.BaseSuite.SetupSuite()
}

func (s *PaymentTestSuite) SetupTest() {
}

func (s *PaymentTestSuite) TestPaymentAccount() {
user := s.GenAndChargeAccounts(1, 100)[0]
ctx := context.Background()
// create a new payment account
msgCreatePaymentAccount := &paymenttypes.MsgCreatePaymentAccount{
Creator: user.GetAddr().String(),
}
_ = s.SendTxBlock(msgCreatePaymentAccount, user)
// query user's payment accounts
queryGetPaymentAccountsByOwnerRequest := paymenttypes.QueryGetPaymentAccountsByOwnerRequest{
Owner: user.GetAddr().String(),
}
paymentAccounts, err := s.Client.GetPaymentAccountsByOwner(ctx, &queryGetPaymentAccountsByOwnerRequest)
s.Require().NoError(err)
s.T().Log(paymentAccounts)
s.Require().Equal(1, len(paymentAccounts.PaymentAccounts))
paymentAccountAddr := paymentAccounts.PaymentAccounts[0]
// query this payment account
queryGetPaymentAccountRequest := paymenttypes.QueryGetPaymentAccountRequest{
Addr: paymentAccountAddr,
}
paymentAccount, err := s.Client.PaymentAccount(ctx, &queryGetPaymentAccountRequest)
s.Require().NoError(err)
s.T().Log(paymentAccount)
s.Require().Equal(user.GetAddr().String(), paymentAccount.PaymentAccount.Owner)
s.Require().Equal(true, paymentAccount.PaymentAccount.Refundable)
// set this payment account to non-refundable
msgDisableRefund := &paymenttypes.MsgDisableRefund{
Owner: user.GetAddr().String(),
Addr: paymentAccountAddr,
}
_ = s.SendTxBlock(msgDisableRefund, user)
// query this payment account
paymentAccount, err = s.Client.PaymentAccount(ctx, &queryGetPaymentAccountRequest)
s.Require().NoError(err)
s.T().Log(paymentAccount)
s.Require().Equal(false, paymentAccount.PaymentAccount.Refundable)
}

func TestPaymentTestSuite(t *testing.T) {
suite.Run(t, new(PaymentTestSuite))
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ require (
require golang.org/x/text v0.6.0 // indirect

require (
github.com/cosmos/go-bip39 v1.0.0
github.com/cosmos/gogoproto v1.4.4
github.com/prysmaticlabs/prysm v0.0.0-20220124113610-e26cde5e091b
github.com/rakyll/statik v0.1.7
Expand Down Expand Up @@ -69,7 +70,6 @@ require (
github.com/containerd/cgroups v1.0.3 // indirect
github.com/containerd/containerd v1.6.8 // indirect
github.com/cosmos/btcutil v1.0.4 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
github.com/cosmos/gorocksdb v1.2.0 // indirect
github.com/cosmos/iavl v0.19.4 // indirect
github.com/cosmos/ledger-cosmos-go v0.11.1 // indirect
Expand Down Expand Up @@ -228,7 +228,7 @@ require (

replace (
github.com/confio/ics23/go => github.com/cosmos/cosmos-sdk/ics23/go v0.8.0
github.com/cosmos/cosmos-sdk => github.com/bnb-chain/gnfd-cosmos-sdk v0.0.2-0.20230214041918-873d75242b68
github.com/cosmos/cosmos-sdk => github.com/bnb-chain/gnfd-cosmos-sdk v0.0.2-0.20230214050107-c8beb3e72b61
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
github.com/tendermint/tendermint => github.com/bnb-chain/gnfd-tendermint v0.0.1
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdn
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
github.com/bmizerany/pat v0.0.0-20170815010413-6226ea591a40/go.mod h1:8rLXio+WjiTceGBHIoTvn60HIbs7Hm7bcHjyrSqYB9c=
github.com/bnb-chain/gnfd-cosmos-sdk v0.0.2-0.20230214041918-873d75242b68 h1:94uyqcuXWb/zMJ9GQ/GdvlbGJp6mc6lVCb5XrOe4uyE=
github.com/bnb-chain/gnfd-cosmos-sdk v0.0.2-0.20230214041918-873d75242b68/go.mod h1:NlIOmju3uhTOJ2YAzLPidpmh7sAgJ+J9dkUlSysHmjw=
github.com/bnb-chain/gnfd-cosmos-sdk v0.0.2-0.20230214050107-c8beb3e72b61 h1:L+oI2VYN0GxQDqossulvO0InsZ/cBkPBSzRZg8iOP0Y=
github.com/bnb-chain/gnfd-cosmos-sdk v0.0.2-0.20230214050107-c8beb3e72b61/go.mod h1:NlIOmju3uhTOJ2YAzLPidpmh7sAgJ+J9dkUlSysHmjw=
github.com/bnb-chain/gnfd-tendermint v0.0.1 h1:KWFuxWv8m6t1vUi3ADyVcEG0qVnEzQt1tjA6dk/b7+Q=
github.com/bnb-chain/gnfd-tendermint v0.0.1/go.mod h1:/v9z9F6cq0+f7EGG92lYSLBcPYQDILoK91X8YM28hWo=
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
Expand Down
Loading