Skip to content

Commit

Permalink
fix: Fix tests after adding orchestrator/ethereum address to validato…
Browse files Browse the repository at this point in the history
…r struct (#133)

* first pass on tests fixes

* fixes the rest of unit tests

* remove unnecessary comments

* uses default eth address when starting sim network

* cosmetics

* comments failing test

* comments failing test

* uncomments test and fixes it from commit 434b308

* remove wrong testnet initialization
  • Loading branch information
rach-id committed Aug 4, 2022
1 parent aca9c0f commit 5a8ad44
Show file tree
Hide file tree
Showing 40 changed files with 823 additions and 746 deletions.
4 changes: 4 additions & 0 deletions client/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ const (
// Tendermint logging flags
FlagLogLevel = "log_level"
FlagLogFormat = "log_format"

// QGB related flags
FlagOrchestratorAddress = "orchestrator-address"
FlagEthereumAddress = "ethereum-address"
)

// LineBreak can be included in a command list to provide a blank line
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ require (
github.com/cosmos/go-bip39 v1.0.0
github.com/cosmos/iavl v0.19.0
github.com/cosmos/ledger-cosmos-go v0.11.1
github.com/enigmampc/btcutil v1.0.3-0.20200723161021-e2fb6adb2a25
github.com/ethereum/go-ethereum v1.9.25
github.com/gogo/gateway v1.1.0
github.com/gogo/protobuf v1.3.2
github.com/golang/mock v1.6.0
Expand Down
894 changes: 329 additions & 565 deletions go.sum

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions simapp/simd/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"bufio"
"encoding/json"
"fmt"
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
"io/ioutil"
"net"
"os"
Expand Down Expand Up @@ -288,9 +289,12 @@ func initTestnetFiles(
genBalances = append(genBalances, banktypes.Balance{Address: addr.String(), Coins: coins.Sort()})
genAccounts = append(genAccounts, authtypes.NewBaseAccount(addr, nil, 0, 0))

// TODO make these come from the test command
orchAddr, _ := sdk.AccAddressFromBech32("celes1qktu8009djs6uym9uwj84ead24exkezsaqrmn5")
ethAddr, _ := stakingtypes.NewEthAddress("0x91DEd26b5f38B065FC0204c7929Da6b2A21277Cd")
orchAddress := sdk.AccAddress(valPubKeys[i].Address())

ethAddress, err := teststaking.RandomEthAddress()
if err != nil {
return err
}

valTokens := sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction)
createValMsg, err := stakingtypes.NewMsgCreateValidator(
Expand All @@ -299,7 +303,7 @@ func initTestnetFiles(
sdk.NewCoin(sdk.DefaultBondDenom, valTokens),
stakingtypes.NewDescription(nodeDirName, "", "", "", ""),
stakingtypes.NewCommissionRates(sdk.OneDec(), sdk.OneDec(), sdk.OneDec()),
sdk.OneInt(), orchAddr, *ethAddr,
sdk.OneInt(), orchAddress, *ethAddress,
)
if err != nil {
return err
Expand Down
49 changes: 31 additions & 18 deletions testutil/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package network
import (
"bufio"
"context"
"crypto/ecdsa"
"encoding/json"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/crypto"
"io/ioutil"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -167,6 +170,11 @@ type (
api *api.Server
grpc *grpc.Server
grpcWeb *http.Server

EthPrivateKey *ecdsa.PrivateKey
EthereumAddr *stakingtypes.EthAddress

OrchestratorAddr sdk.AccAddress
}
)

Expand Down Expand Up @@ -399,19 +407,21 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) {
return nil, err
}

orchAddr, _ := sdk.AccAddressFromHex("celes1qktu8009djs6uym9uwj84ead24exkezsaqrmn5")
ethAddr, _ := stakingtypes.NewEthAddress("0x91DEd26b5f38B065FC0204c7929Da6b2A21277Cd")

orchAddr, _ := sdk.AccAddressFromHex("celes1qktu8009djs6uym9uwj84ead24exkezsaqrmn5")
ethAddr, _ := stakingtypes.NewEthAddress("0x91DEd26b5f38B065FC0204c7929Da6b2A21277Cd")
ethPrivateKey, err := crypto.GenerateKey()
require.NoError(t, err)
orchEthPublicKey := ethPrivateKey.Public().(*ecdsa.PublicKey)
ethAddr, err := stakingtypes.NewEthAddress(crypto.PubkeyToAddress(*orchEthPublicKey).Hex())
require.NoError(t, err)

createValMsg, err := stakingtypes.NewMsgCreateValidator(
sdk.ValAddress(addr),
valPubKeys[i],
sdk.NewCoin(cfg.BondDenom, cfg.BondedTokens),
stakingtypes.NewDescription(nodeDirName, "", "", "", ""),
stakingtypes.NewCommissionRates(commission, sdk.OneDec(), sdk.OneDec()),
sdk.OneInt(), orchAddr, *ethAddr,
sdk.OneInt(),
addr,
*ethAddr,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -468,18 +478,21 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) {
WithAccountRetriever(cfg.AccountRetriever)

network.Validators[i] = &Validator{
AppConfig: appCfg,
ClientCtx: clientCtx,
Ctx: ctx,
Dir: filepath.Join(network.BaseDir, nodeDirName),
NodeID: nodeID,
PubKey: pubKey,
Moniker: nodeDirName,
RPCAddress: tmCfg.RPC.ListenAddress,
P2PAddress: tmCfg.P2P.ListenAddress,
APIAddress: apiAddr,
Address: addr,
ValAddress: sdk.ValAddress(addr),
AppConfig: appCfg,
ClientCtx: clientCtx,
Ctx: ctx,
Dir: filepath.Join(network.BaseDir, nodeDirName),
NodeID: nodeID,
PubKey: pubKey,
Moniker: nodeDirName,
RPCAddress: tmCfg.RPC.ListenAddress,
P2PAddress: tmCfg.P2P.ListenAddress,
APIAddress: apiAddr,
Address: addr,
ValAddress: sdk.ValAddress(addr),
EthPrivateKey: ethPrivateKey,
EthereumAddr: ethAddr,
OrchestratorAddr: addr,
}
}

Expand Down
11 changes: 9 additions & 2 deletions x/auth/client/testutil/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,15 +644,18 @@ func (s *IntegrationTestSuite) TestCLIQueryTxsCmdByEvents() {
name string
args []string
expectEmpty bool
expectError string
}{
{
{ // Multiple shares failing test
"fee event happy case",
[]string{
fmt.Sprintf("--events=tx.fee=%s",
sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
false,
"transaction spanned more than two shares, this is not yet supported",
// TODO: change this to not expect an error when functionality is added to celestia-core
},
{
"no matching fee event",
Expand All @@ -662,6 +665,7 @@ func (s *IntegrationTestSuite) TestCLIQueryTxsCmdByEvents() {
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
true,
"",
},
}

Expand All @@ -672,7 +676,10 @@ func (s *IntegrationTestSuite) TestCLIQueryTxsCmdByEvents() {
clientCtx := val.ClientCtx

out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
s.Require().NoError(err)
if tc.expectError != "" {
s.Require().Equal(tc.expectError, err.Error())
return
}

var result sdk.SearchTxsResult
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &result))
Expand Down
6 changes: 5 additions & 1 deletion x/auth/migrations/v043/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v043_test

import (
"fmt"
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
"testing"
"time"

Expand Down Expand Up @@ -652,6 +653,9 @@ func createValidator(t *testing.T, ctx sdk.Context, app *simapp.SimApp, powers i
pks := simapp.CreateTestPubKeys(1)
cdc := simapp.MakeTestEncodingConfig().Codec

ethAddr, err := teststaking.RandomEthAddress()
require.NoError(t, err)

app.StakingKeeper = stakingkeeper.NewKeeper(
cdc,
app.GetKey(stakingtypes.StoreKey),
Expand All @@ -660,7 +664,7 @@ func createValidator(t *testing.T, ctx sdk.Context, app *simapp.SimApp, powers i
app.GetSubspace(stakingtypes.ModuleName),
)

val1, err := stakingtypes.NewValidator(valAddrs[0], pks[0], stakingtypes.Description{})
val1, err := stakingtypes.NewValidator(valAddrs[0], pks[0], stakingtypes.Description{}, sdk.AccAddress(pks[0].Address()), *ethAddr)
require.NoError(t, err)

app.StakingKeeper.SetValidator(ctx, val1)
Expand Down
7 changes: 5 additions & 2 deletions x/distribution/simulation/operations_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package simulation_test

import (
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
"math/rand"
"testing"

Expand Down Expand Up @@ -255,8 +256,10 @@ func (suite *SimTestSuite) getTestingValidator(accounts []simtypes.Account, comm
account := accounts[n]
valPubKey := account.PubKey
valAddr := sdk.ValAddress(account.PubKey.Address().Bytes())
validator, err := stakingtypes.NewValidator(valAddr, valPubKey, stakingtypes.
Description{})

randomEthAddress, err := teststaking.RandomEthAddress()
require.NoError(err)
validator, err := stakingtypes.NewValidator(valAddr, valPubKey, stakingtypes.Description{}, sdk.AccAddress(valPubKey.Address()), *randomEthAddress)
require.NoError(err)
validator, err = validator.SetInitialCommission(commission)
require.NoError(err)
Expand Down
18 changes: 15 additions & 3 deletions x/genutil/client/cli/gentx.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ $ %s gentx my-key-name 1000000stake --home=/path/to/home/dir --keyring-backend=o
--commission-rate=0.07 \
--details="..." \
--security-contact="..." \
--website="..."
--website="..." \
--orchestrator-address="..." \
--ethereum-address="..."
`, defaultsDesc, version.AppName,
),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -109,12 +111,22 @@ $ %s gentx my-key-name 1000000stake --home=/path/to/home/dir --keyring-backend=o
moniker = m
}

orchestratorAddress, err := cmd.Flags().GetString(flags.FlagOrchestratorAddress)
if err != nil {
return errors.Wrapf(err, "failed to get the orchestrator address")
}

ethereumAddress, err := cmd.Flags().GetString(flags.FlagEthereumAddress)
if err != nil {
return errors.Wrapf(err, "failed to get the ethereum address")
}

// set flags for creating a gentx
createValCfg, err := cli.PrepareConfigForTxCreateValidator(
cmd.Flags(), moniker,
nodeID, genDoc.ChainID,
valPubKey, cli.FlagOrchestratorAddress,
cli.FlagEthereumAddress,
valPubKey, orchestratorAddress,
ethereumAddress,
)
if err != nil {
return errors.Wrap(err, "error creating configuration to create validator msg")
Expand Down
2 changes: 2 additions & 0 deletions x/genutil/client/testutil/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ func (s *IntegrationTestSuite) TestGenTxCmd() {
fmt.Sprintf("--%s=%s", flags.FlagChainID, s.network.Config.ChainID),
val.Moniker,
amount.String(),
fmt.Sprintf("--%s=%s", flags.FlagEthereumAddress, val.EthereumAddr.GetAddress()),
fmt.Sprintf("--%s=%s", flags.FlagOrchestratorAddress, val.OrchestratorAddr.String()),
},
expError: false,
},
Expand Down
19 changes: 15 additions & 4 deletions x/genutil/gentx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package genutil_test
import (
"encoding/json"
"fmt"
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
"math/rand"
"testing"
"time"
Expand All @@ -29,8 +30,12 @@ var (
pk2 = priv2.PubKey()
addr1 = sdk.AccAddress(pk1.Address())
addr2 = sdk.AccAddress(pk2.Address())
desc = stakingtypes.NewDescription("testname", "", "", "", "")
comm = stakingtypes.CommissionRates{}

ethAddr1, _ = teststaking.RandomEthAddress()
ethAddr2, _ = teststaking.RandomEthAddress()

desc = stakingtypes.NewDescription("testname", "", "", "", "")
comm = stakingtypes.CommissionRates{}
)

// GenTxTestSuite is a test suite to be used with gentx tests.
Expand All @@ -55,10 +60,16 @@ func (suite *GenTxTestSuite) SetupTest() {
amount := sdk.NewInt64Coin(sdk.DefaultBondDenom, 50)
one := sdk.OneInt()
suite.msg1, err = stakingtypes.NewMsgCreateValidator(
sdk.ValAddress(pk1.Address()), pk1, amount, desc, comm, one)
sdk.ValAddress(pk1.Address()), pk1, amount, desc, comm, one,
sdk.AccAddress(pk1.Address()),
*ethAddr1,
)
suite.NoError(err)
suite.msg2, err = stakingtypes.NewMsgCreateValidator(
sdk.ValAddress(pk2.Address()), pk1, amount, desc, comm, one)
sdk.ValAddress(pk2.Address()), pk1, amount, desc, comm, one,
sdk.AccAddress(pk2.Address()),
*ethAddr2,
)
suite.NoError(err)
}

Expand Down
20 changes: 15 additions & 5 deletions x/genutil/types/genesis_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types_test

import (
"encoding/json"
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -17,8 +18,11 @@ import (
)

var (
pk1 = ed25519.GenPrivKey().PubKey()
pk2 = ed25519.GenPrivKey().PubKey()
pk1 = ed25519.GenPrivKey().PubKey()
ethAddr1, _ = teststaking.RandomEthAddress()

pk2 = ed25519.GenPrivKey().PubKey()
ethAddr2, _ = teststaking.RandomEthAddress()
)

func TestNetGenesisState(t *testing.T) {
Expand All @@ -38,11 +42,17 @@ func TestValidateGenesisMultipleMessages(t *testing.T) {
comm := stakingtypes.CommissionRates{}

msg1, err := stakingtypes.NewMsgCreateValidator(sdk.ValAddress(pk1.Address()), pk1,
sdk.NewInt64Coin(sdk.DefaultBondDenom, 50), desc, comm, sdk.OneInt())
sdk.NewInt64Coin(sdk.DefaultBondDenom, 50), desc, comm, sdk.OneInt(),
sdk.AccAddress(pk1.Address()),
*ethAddr1,
)
require.NoError(t, err)

msg2, err := stakingtypes.NewMsgCreateValidator(sdk.ValAddress(pk2.Address()), pk2,
sdk.NewInt64Coin(sdk.DefaultBondDenom, 50), desc, comm, sdk.OneInt())
sdk.NewInt64Coin(sdk.DefaultBondDenom, 50), desc, comm, sdk.OneInt(),
sdk.AccAddress(pk2.Address()),
*ethAddr2,
)
require.NoError(t, err)

txGen := simapp.MakeTestEncodingConfig().TxConfig
Expand All @@ -59,7 +69,7 @@ func TestValidateGenesisMultipleMessages(t *testing.T) {
func TestValidateGenesisBadMessage(t *testing.T) {
desc := stakingtypes.NewDescription("testname", "", "", "", "")

msg1 := stakingtypes.NewMsgEditValidator(sdk.ValAddress(pk1.Address()), desc, nil, nil)
msg1 := stakingtypes.NewMsgEditValidator(sdk.ValAddress(pk1.Address()), desc, nil, nil, nil, nil)

txGen := simapp.MakeTestEncodingConfig().TxConfig
txBuilder := txGen.NewTxBuilder()
Expand Down
18 changes: 18 additions & 0 deletions x/gov/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gov_test

import (
"bytes"
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
"log"
"sort"
"testing"
Expand Down Expand Up @@ -85,3 +86,20 @@ var pubkeys = []cryptotypes.PubKey{
ed25519.GenPrivKey().PubKey(),
ed25519.GenPrivKey().PubKey(),
}

func createValidators(t *testing.T, stakingHandler sdk.Handler, ctx sdk.Context, addrs []sdk.ValAddress, powerAmt []int64) {
require.True(t, len(addrs) <= len(pubkeys), "Not enough pubkeys specified at top of file.")

for i := 0; i < len(addrs); i++ {
valTokens := sdk.TokensFromConsensusPower(powerAmt[i], sdk.DefaultPowerReduction)
randomEthAddress, err := teststaking.RandomEthAddress()
require.NoError(t, err)
valCreateMsg, err := stakingtypes.NewMsgCreateValidator(
addrs[i], pubkeys[i], sdk.NewCoin(sdk.DefaultBondDenom, valTokens),
TestDescription, TestCommissionRates, sdk.OneInt(),
sdk.AccAddress(pubkeys[i].Address()), *randomEthAddress,
)
require.NoError(t, err)
handleAndCheck(t, stakingHandler, ctx, valCreateMsg)
}
}
Loading

0 comments on commit 5a8ad44

Please sign in to comment.