migrate: add dxns module

1. add bond,auction, nameserivce module
2. update to v0.12.2 ethermint version
3. fix the test cases
4. add gql server
This commit is contained in:
Sai Kumar
2022-04-05 12:39:27 +05:30
parent 4ddc8afd18
commit e90b21bc8e
137 changed files with 56381 additions and 26 deletions
+16
View File
@@ -0,0 +1,16 @@
package testutil
import (
"testing"
"github.com/stretchr/testify/suite"
"github.com/tharsis/ethermint/testutil/network"
)
func TestIntegrationTestSuite(t *testing.T) {
cfg := network.DefaultConfig()
cfg.NumValidators = 1
suite.Run(t, NewIntegrationTestSuite(cfg))
}
+80
View File
@@ -0,0 +1,80 @@
package testutil
import (
"fmt"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
banktestutil "github.com/cosmos/cosmos-sdk/x/bank/client/testutil"
"github.com/stretchr/testify/suite"
tmcli "github.com/tendermint/tendermint/libs/cli"
"github.com/tharsis/ethermint/crypto/hd"
"github.com/tharsis/ethermint/testutil/network"
)
type IntegrationTestSuite struct {
suite.Suite
cfg network.Config
network *network.Network
defaultAuctionID string
}
var (
ownerAccount = "owner"
bidderAccount = "bidder"
ownerAddress string
bidderAddress string
)
func NewIntegrationTestSuite(cfg network.Config) *IntegrationTestSuite {
return &IntegrationTestSuite{cfg: cfg}
}
func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")
var err error
s.network, err = network.New(s.T(), s.T().TempDir(), s.cfg)
s.Require().NoError(err)
_, err = s.network.WaitForHeight(1)
s.Require().NoError(err)
// setting up random owner and bidder accounts
s.createAccountWithBalance(ownerAccount, &ownerAddress)
s.createAccountWithBalance(bidderAccount, &bidderAddress)
s.defaultAuctionID = s.createAuctionAndBid(true, false)
}
func (s *IntegrationTestSuite) TearDownSuite() {
s.T().Log("tearing down integration test suite")
s.network.Cleanup()
}
func (s *IntegrationTestSuite) createAccountWithBalance(accountName string, accountAddress *string) {
val := s.network.Validators[0]
sr := s.Require()
info, _, err := val.ClientCtx.Keyring.NewMnemonic(accountName, keyring.English, sdk.FullFundraiserPath, keyring.DefaultBIP39Passphrase, hd.EthSecp256k1)
sr.NoError(err)
val.ClientCtx.Keyring.SavePubKey(accountName, info.GetPubKey(), hd.EthSecp256k1Type)
newAddr := sdk.AccAddress(info.GetPubKey().Address())
_, err = banktestutil.MsgSendExec(
val.ClientCtx,
val.Address,
newAddr,
sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(200000))),
fmt.Sprintf("--%s=%s", flags.FlagFrom, accountName),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
)
sr.NoError(err)
*accountAddress = newAddr.String()
}
+255
View File
@@ -0,0 +1,255 @@
package testutil
import (
"fmt"
"github.com/cosmos/cosmos-sdk/types/rest"
auctiontypes "github.com/tharsis/ethermint/x/auction/types"
)
const (
randomAuctionID = "randomAuctionID"
randomBidderAddress = "randomBidderAddress"
randomOwnerAddress = "randomOwnerAddress"
)
func (suite *IntegrationTestSuite) TestGetAllAuctionsGrpc() {
val := suite.network.Validators[0]
sr := suite.Require()
reqUrl := fmt.Sprintf("%s/vulcanize/auction/v1beta1/auctions", val.APIAddress)
testCases := []struct {
msg string
url string
errorMsg string
isErrorExpected bool
}{
{
"invalid request to get all auctions",
reqUrl + randomAuctionID,
"",
true,
},
{
"valid request to get all auctions",
reqUrl,
"",
false,
},
}
for _, tc := range testCases {
suite.Run(tc.msg, func() {
resp, err := rest.GetRequest(tc.url)
if tc.isErrorExpected {
sr.Contains(string(resp), tc.errorMsg)
} else {
sr.NoError(err)
var auctions auctiontypes.AuctionsResponse
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &auctions)
sr.NotZero(len(auctions.Auctions.Auctions))
}
})
}
}
func (suite *IntegrationTestSuite) TestQueryParamsGrpc() {
val := suite.network.Validators[0]
sr := suite.Require()
reqUrl := fmt.Sprintf("%s/vulcanize/auction/v1beta1/params", val.APIAddress)
suite.Run("valid request to get auction params", func() {
resp, err := rest.GetRequest(reqUrl)
suite.Require().NoError(err)
var params auctiontypes.QueryParamsResponse
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &params)
sr.NoError(err)
sr.Equal(*params.GetParams(), auctiontypes.DefaultParams())
})
}
func (suite *IntegrationTestSuite) TestGetAuctionGrpc() {
val := suite.network.Validators[0]
sr := suite.Require()
reqUrl := fmt.Sprintf("%s/vulcanize/auction/v1beta1/auctions/", val.APIAddress)
testCases := []struct {
msg string
url string
errorMsg string
isErrorExpected bool
preRun func() string
}{
{
"invalid request to get an auction",
reqUrl + randomAuctionID,
"",
true,
func() string { return "" },
},
{
"valid request to get an auction",
reqUrl,
"",
false,
func() string { return suite.defaultAuctionID },
},
}
for _, tc := range testCases {
suite.Run(tc.msg, func() {
auctionID := tc.preRun()
resp, err := rest.GetRequest(tc.url + auctionID)
if tc.isErrorExpected {
sr.Contains(string(resp), tc.errorMsg)
} else {
sr.NoError(err)
var auction auctiontypes.AuctionResponse
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &auction)
sr.NoError(err)
sr.Equal(auctionID, auction.Auction.Id)
}
})
}
}
func (suite *IntegrationTestSuite) TestGetBidsGrpc() {
val := suite.network.Validators[0]
sr := suite.Require()
reqUrl := fmt.Sprintf("%s/vulcanize/auction/v1beta1/bids/", val.APIAddress)
testCases := []struct {
msg string
url string
errorMsg string
isErrorExpected bool
preRun func() string
}{
{
"invalid request to get all bids",
reqUrl,
"",
true,
func() string { return "" },
},
{
"valid request to get all bids",
reqUrl,
"",
false,
func() string { return suite.createAuctionAndBid(false, true) },
},
}
for _, tc := range testCases {
suite.Run(tc.msg, func() {
auctionID := tc.preRun()
tc.url += auctionID
resp, err := rest.GetRequest(tc.url)
if tc.isErrorExpected {
sr.Contains(string(resp), tc.errorMsg)
} else {
sr.NoError(err)
var bids auctiontypes.BidsResponse
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &bids)
sr.NoError(err)
sr.Equal(auctionID, bids.Bids[0].AuctionId)
}
})
}
}
func (suite *IntegrationTestSuite) TestGetBidGrpc() {
val := suite.network.Validators[0]
sr := suite.Require()
reqUrl := fmt.Sprintf("%s/vulcanize/auction/v1beta1/bids/", val.APIAddress)
testCases := []struct {
msg string
url string
errorMsg string
isErrorExpected bool
}{
{
"invalid request to get bid",
fmt.Sprintf("%s/%s/", reqUrl, randomAuctionID),
"",
true,
},
{
"valid request to get bid",
fmt.Sprintf("%s/%s/%s", reqUrl, randomAuctionID, randomBidderAddress),
"",
false,
},
}
for _, tc := range testCases {
suite.Run(tc.msg, func() {
resp, err := rest.GetRequest(tc.url)
if tc.isErrorExpected {
sr.Contains(string(resp), tc.errorMsg)
} else {
sr.NoError(err)
var bid auctiontypes.BidResponse
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &bid)
sr.NoError(err)
}
})
}
}
func (suite *IntegrationTestSuite) TestGetAuctionsByOwnerGrpc() {
val := suite.network.Validators[0]
sr := suite.Require()
reqUrl := fmt.Sprintf("%s/vulcanize/auction/v1beta1/by-owner/", val.APIAddress)
testCases := []struct {
msg string
url string
errorMsg string
isErrorExpected bool
}{
{
"invalid request to get auctions by owner",
reqUrl,
"",
true,
},
{
"valid request to get auctions by owner",
fmt.Sprintf("%s/%s", reqUrl, randomOwnerAddress),
"",
false,
},
}
for _, tc := range testCases {
suite.Run(tc.msg, func() {
resp, err := rest.GetRequest(tc.url)
if tc.isErrorExpected {
sr.Contains(string(resp), tc.errorMsg)
} else {
sr.NoError(err)
var auctions auctiontypes.AuctionsResponse
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &auctions)
sr.NoError(err)
}
})
}
}
func (suite *IntegrationTestSuite) TestQueryBalanceGrpc() {
val := suite.network.Validators[0]
sr := suite.Require()
reqUrl := fmt.Sprintf("%s/vulcanize/auction/v1beta1/balance", val.APIAddress)
msg := "valid request to get the auction module balance"
suite.createAuctionAndBid(false, true)
suite.Run(msg, func() {
resp, err := rest.GetRequest(reqUrl)
sr.NoError(err)
var response auctiontypes.BalanceResponse
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &response)
sr.NoError(err)
sr.NotZero(len(response.GetBalance()))
})
}
+311
View File
@@ -0,0 +1,311 @@
package testutil
import (
"fmt"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
tmcli "github.com/tendermint/tendermint/libs/cli"
"github.com/tharsis/ethermint/x/auction/client/cli"
"github.com/tharsis/ethermint/x/auction/types"
)
var queryJSONFlag = []string{fmt.Sprintf("--%s=json", tmcli.OutputFlag)}
func (suite *IntegrationTestSuite) TestGetCmdQueryParams() {
val := suite.network.Validators[0]
sr := suite.Require()
suite.Run(fmt.Sprintf("Case %s", "fetch query params"), func() {
out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cli.GetCmdQueryParams(), queryJSONFlag)
sr.NoError(err)
var params types.QueryParamsResponse
err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &params)
sr.NoError(err)
sr.Equal(types.DefaultParams(), *params.Params)
})
}
func (suite *IntegrationTestSuite) TestGetCmdBalance() {
val := suite.network.Validators[0]
sr := suite.Require()
testCases := []struct {
msg string
createAuctionAndBid bool
}{
{
"fetch module balance without creating auction and bid",
false,
},
{
"fetch module balance with valid auction and bid",
true,
},
}
for _, test := range testCases {
suite.Run(fmt.Sprintf("Case %s", test.msg), func() {
if test.createAuctionAndBid {
suite.createAuctionAndBid(false, true)
}
out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cli.GetCmdBalance(), queryJSONFlag)
sr.NoError(err)
var balance types.BalanceResponse
err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &balance)
sr.NoError(err)
if test.createAuctionAndBid {
sr.NotZero(len(balance.Balance))
}
})
}
}
func (suite *IntegrationTestSuite) TestGetCmdList() {
val := suite.network.Validators[0]
sr := suite.Require()
testCases := []struct {
msg string
createAuction bool
}{
{
"list auctions when no auctions exist",
false,
},
{
"list auctions after creating an auction",
true,
},
}
for _, test := range testCases {
suite.Run(fmt.Sprintf("Case %s", test.msg), func() {
out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cli.GetCmdList(), queryJSONFlag)
sr.NoError(err)
var auctions types.AuctionsResponse
err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &auctions)
sr.NoError(err)
if test.createAuction {
sr.NotZero(len(auctions.Auctions.Auctions))
}
})
}
}
func (suite *IntegrationTestSuite) TestGetCmdGetBid() {
val := suite.network.Validators[0]
sr := suite.Require()
testCases := []struct {
msg string
args []string
createAuctionAndBid bool
}{
{
"get bid without creating auction",
[]string{},
false,
},
{
"get bid after creating auction and bid",
[]string{},
true,
},
}
for _, test := range testCases {
suite.Run(fmt.Sprintf("Case %s", test.msg), func() {
if test.createAuctionAndBid {
auctionID := suite.createAuctionAndBid(false, true)
test.args = append(test.args, auctionID)
getBidsArgs := []string{auctionID, queryJSONFlag[0]}
out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cli.GetCmdGetBids(), getBidsArgs)
sr.NoError(err)
var bids types.BidsResponse
err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &bids)
sr.NoError(err)
test.args = append(test.args, bids.GetBids()[0].BidderAddress)
}
test.args = append(test.args, queryJSONFlag...)
out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cli.GetCmdGetBid(), test.args)
if test.createAuctionAndBid {
sr.NoError(err)
var bid types.BidResponse
err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &bid)
sr.NoError(err)
sr.NotNil(bid.GetBid())
} else {
sr.Error(err)
}
})
}
}
func (suite *IntegrationTestSuite) TestGetCmdGetBids() {
val := suite.network.Validators[0]
sr := suite.Require()
testCases := []struct {
msg string
args []string
createAuctionAndBid bool
}{
{
"get bids without creating auction",
[]string{},
false,
},
{
"get bids after creating auction and bid",
[]string{},
true,
},
}
for _, test := range testCases {
suite.Run(fmt.Sprintf("Case %s", test.msg), func() {
if test.createAuctionAndBid {
auctionID := suite.createAuctionAndBid(false, true)
test.args = append(test.args, auctionID)
}
test.args = append(test.args, queryJSONFlag...)
out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cli.GetCmdGetBids(), test.args)
if test.createAuctionAndBid {
sr.NoError(err)
var bids types.BidsResponse
err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &bids)
sr.NoError(err)
sr.NotZero(len(bids.Bids))
} else {
sr.Error(err)
}
})
}
}
func (suite *IntegrationTestSuite) TestGetCmdGetAuction() {
val := suite.network.Validators[0]
sr := suite.Require()
testCases := []struct {
msg string
auctionID string
createAuction bool
}{
{
"get auction with empty auction ID",
"",
false,
},
{
"get auction with valid auction ID",
"",
true,
},
}
for _, test := range testCases {
suite.Run(fmt.Sprintf("Case %s", test.msg), func() {
if test.createAuction {
test.auctionID = suite.defaultAuctionID
}
args := []string{test.auctionID, queryJSONFlag[0]}
out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cli.GetCmdGetAuction(), args)
if test.createAuction {
sr.NoError(err)
var auction types.AuctionResponse
err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &auction)
sr.NoError(err)
sr.NotNil(auction.GetAuction())
sr.Equal(test.auctionID, auction.GetAuction().Id)
} else {
sr.Error(err)
}
})
}
}
func (suite *IntegrationTestSuite) TestGetCmdAuctionsByBidder() {
val := suite.network.Validators[0]
sr := suite.Require()
testCases := []struct {
msg string
createAuctionAndBid bool
bidderAddress string
}{
{
"get auctions by bidder without creating auctions",
false,
"",
},
{
"get auctions by bidder for valid bidder address",
true,
"",
},
}
for _, test := range testCases {
suite.Run(fmt.Sprintf("Case %s", test.msg), func() {
if test.createAuctionAndBid {
auctionID := suite.createAuctionAndBid(false, true)
args := []string{auctionID, queryJSONFlag[0]}
out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cli.GetCmdGetBids(), args)
sr.NoError(err)
var bids types.BidsResponse
err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &bids)
sr.NoError(err)
test.bidderAddress = bids.Bids[0].BidderAddress
}
getByBidderArgs := []string{test.bidderAddress, queryJSONFlag[0]}
_, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cli.GetCmdAuctionsByBidder(), getByBidderArgs)
if test.createAuctionAndBid {
sr.NoError(err)
} else {
sr.Error(err)
}
})
}
}
func (suite IntegrationTestSuite) createAuctionAndBid(createAuction, createBid bool) string {
val := suite.network.Validators[0]
sr := suite.Require()
auctionID := ""
if createAuction {
auctionArgs := []string{
sampleCommitTime, sampleRevealTime,
fmt.Sprintf("10%s", suite.cfg.BondDenom),
fmt.Sprintf("10%s", suite.cfg.BondDenom),
fmt.Sprintf("100%s", suite.cfg.BondDenom),
}
resp, err := suite.executeTx(cli.GetCmdCreateAuction(), auctionArgs, ownerAccount)
sr.NoError(err)
sr.Zero(resp.Code)
out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cli.GetCmdList(), queryJSONFlag)
sr.NoError(err)
var queryResponse types.AuctionsResponse
err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &queryResponse)
sr.NoError(err)
auctionID = queryResponse.Auctions.Auctions[0].Id
} else {
auctionID = suite.defaultAuctionID
}
if createBid {
bidArgs := []string{auctionID, fmt.Sprintf("200%s", suite.cfg.BondDenom)}
resp, err := suite.executeTx(cli.GetCmdCommitBid(), bidArgs, bidderAccount)
sr.NoError(err)
sr.Zero(resp.Code)
}
return auctionID
}
+141
View File
@@ -0,0 +1,141 @@
package testutil
import (
"fmt"
"github.com/cosmos/cosmos-sdk/client/flags"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cobra"
tmcli "github.com/tendermint/tendermint/libs/cli"
"github.com/tharsis/ethermint/x/auction/client/cli"
"github.com/tharsis/ethermint/x/auction/types"
)
const (
sampleCommitTime = "90s"
sampleRevealTime = "5s"
placeholderAuctionID = "placeholder_auction_id"
)
func (suite *IntegrationTestSuite) TestTxCreateAuction() {
sr := suite.Require()
testCases := []struct {
msg string
args []string
expectErr bool
}{
{
"create auction with missing arguments",
[]string{sampleCommitTime, sampleRevealTime},
true,
},
{
"create auction with proper arguments",
[]string{
sampleCommitTime, sampleRevealTime,
fmt.Sprintf("10%s", suite.cfg.BondDenom),
fmt.Sprintf("10%s", suite.cfg.BondDenom),
fmt.Sprintf("100%s", suite.cfg.BondDenom),
},
false,
},
}
for _, test := range testCases {
suite.Run(fmt.Sprintf("Case %s", test.msg), func() {
resp, err := suite.executeTx(cli.GetCmdCreateAuction(), test.args, ownerAccount)
if test.expectErr {
sr.Error(err)
} else {
sr.NoError(err)
sr.Zero(resp.Code)
}
})
}
}
func (suite *IntegrationTestSuite) TestTxCommitBid() {
val := suite.network.Validators[0]
sr := suite.Require()
testCases := []struct {
msg string
args []string
createAuction bool
}{
{
"commit bid with missing args",
[]string{fmt.Sprintf("200%s", suite.cfg.BondDenom)},
false,
},
{
"commit bid with valid args",
[]string{
placeholderAuctionID,
fmt.Sprintf("200%s", suite.cfg.BondDenom),
},
true,
},
}
for _, test := range testCases {
suite.Run(fmt.Sprintf("Case %s", test.msg), func() {
if test.createAuction {
auctionArgs := []string{
sampleCommitTime, sampleRevealTime,
fmt.Sprintf("10%s", suite.cfg.BondDenom),
fmt.Sprintf("10%s", suite.cfg.BondDenom),
fmt.Sprintf("100%s", suite.cfg.BondDenom),
}
_, err := suite.executeTx(cli.GetCmdCreateAuction(), auctionArgs, ownerAccount)
sr.NoError(err)
out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cli.GetCmdList(),
[]string{fmt.Sprintf("--%s=json", tmcli.OutputFlag)})
sr.NoError(err)
var queryResponse types.AuctionsResponse
err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &queryResponse)
sr.NoError(err)
sr.NotNil(queryResponse.GetAuctions())
test.args[0] = queryResponse.GetAuctions().Auctions[0].Id
}
resp, err := suite.executeTx(cli.GetCmdCommitBid(), test.args, bidderAccount)
if test.createAuction {
sr.NoError(err)
sr.Zero(resp.Code)
} else {
sr.Error(err)
}
})
}
}
func (suite *IntegrationTestSuite) executeTx(cmd *cobra.Command, args []string, caller string) (sdk.TxResponse, error) {
val := suite.network.Validators[0]
additionalArgs := []string{
fmt.Sprintf("--%s=%s", flags.FlagFrom, caller),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, fmt.Sprintf("3%s", suite.cfg.BondDenom)),
}
args = append(args, additionalArgs...)
out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, args)
if err != nil {
return sdk.TxResponse{}, err
}
var resp sdk.TxResponse
err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &resp)
if err != nil {
return sdk.TxResponse{}, err
}
err = suite.network.WaitForNextBlock()
if err != nil {
return sdk.TxResponse{}, err
}
return resp, nil
}