Skip to content

Commit

Permalink
Merge PR #2429: LCD Properly Init Proposing Validator in Tests
Browse files Browse the repository at this point in the history
* Refactor InitializeTestLCD to initialize correctly

* Update InitializeTestLCD API usage in LCD unit tests

* Update pending log

* Update named return variables

* Increase power of validator proposer in LCD tests
  • Loading branch information
alexanderbez authored and rigelrozanski committed Oct 3, 2018
1 parent 324bdaf commit 89d13d1
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 47 deletions.
2 changes: 2 additions & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,7 @@ BUG FIXES
* [\#2158](https://github.com/cosmos/cosmos-sdk/issues/2158) Fix non-deterministic ordering of validator iteration when slashing in `gov EndBlocker`
* [simulation] \#1924 Make simulation stop on SIGTERM
* [\#2388](https://github.com/cosmos/cosmos-sdk/issues/2388) Remove dependency on deprecated tendermint/tmlibs repository.
* [\#2416](https://github.com/cosmos/cosmos-sdk/issues/2416) Refactored
`InitializeTestLCD` to properly include proposing validator in genesis state.

* Tendermint
72 changes: 37 additions & 35 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func init() {
func TestKeys(t *testing.T) {
name, password := "test", "1234567890"
addr, seed := CreateAddr(t, "test", password, GetKeyBase(t))
cleanup, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr})
cleanup, _, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr})
defer cleanup()

// get seed
Expand Down Expand Up @@ -125,7 +125,7 @@ func TestVersion(t *testing.T) {
t.SkipNow()
}

cleanup, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{})
cleanup, _, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{})
defer cleanup()

// node info
Expand All @@ -148,7 +148,7 @@ func TestVersion(t *testing.T) {
}

func TestNodeStatus(t *testing.T) {
cleanup, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{})
cleanup, _, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{})
defer cleanup()

// node info
Expand All @@ -170,7 +170,7 @@ func TestNodeStatus(t *testing.T) {
}

func TestBlock(t *testing.T) {
cleanup, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{})
cleanup, _, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{})
defer cleanup()

var resultBlock ctypes.ResultBlock
Expand Down Expand Up @@ -200,7 +200,7 @@ func TestBlock(t *testing.T) {
}

func TestValidators(t *testing.T) {
cleanup, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{})
cleanup, _, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{})
defer cleanup()

var resultVals rpc.ResultValidatorsOutput
Expand Down Expand Up @@ -235,7 +235,7 @@ func TestValidators(t *testing.T) {
func TestCoinSend(t *testing.T) {
name, password := "test", "1234567890"
addr, seed := CreateAddr(t, "test", password, GetKeyBase(t))
cleanup, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr})
cleanup, _, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr})
defer cleanup()

bz, err := hex.DecodeString("8FA6AB57AD6870F6B5B2E57735F38F2F30E73CB6")
Expand Down Expand Up @@ -303,7 +303,7 @@ func TestCoinSend(t *testing.T) {
func DisabledTestIBCTransfer(t *testing.T) {
name, password := "test", "1234567890"
addr, seed := CreateAddr(t, "test", password, GetKeyBase(t))
cleanup, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr})
cleanup, _, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr})
defer cleanup()

acc := getAccount(t, port, addr)
Expand Down Expand Up @@ -332,7 +332,7 @@ func DisabledTestIBCTransfer(t *testing.T) {
func TestCoinSendGenerateSignAndBroadcast(t *testing.T) {
name, password := "test", "1234567890"
addr, seed := CreateAddr(t, "test", password, GetKeyBase(t))
cleanup, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr})
cleanup, _, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr})
defer cleanup()
acc := getAccount(t, port, addr)

Expand Down Expand Up @@ -391,7 +391,7 @@ func TestCoinSendGenerateSignAndBroadcast(t *testing.T) {
func TestTxs(t *testing.T) {
name, password := "test", "1234567890"
addr, seed := CreateAddr(t, "test", password, GetKeyBase(t))
cleanup, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr})
cleanup, _, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr})
defer cleanup()

// query wrong
Expand Down Expand Up @@ -450,7 +450,7 @@ func TestTxs(t *testing.T) {
func TestPoolParamsQuery(t *testing.T) {
_, password := "test", "1234567890"
addr, _ := CreateAddr(t, "test", password, GetKeyBase(t))
cleanup, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr})
cleanup, _, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr})
defer cleanup()

defaultParams := stake.DefaultParams()
Expand Down Expand Up @@ -484,45 +484,47 @@ func TestPoolParamsQuery(t *testing.T) {
}

func TestValidatorsQuery(t *testing.T) {
cleanup, pks, port := InitializeTestLCD(t, 1, []sdk.AccAddress{})
cleanup, valPubKeys, operAddrs, port := InitializeTestLCD(t, 1, []sdk.AccAddress{})
defer cleanup()
require.Equal(t, 1, len(pks))

require.Equal(t, 1, len(valPubKeys))
require.Equal(t, 1, len(operAddrs))

validators := getValidators(t, port)
require.Equal(t, len(validators), 1)

// make sure all the validators were found (order unknown because sorted by operator addr)
foundVal := false

if validators[0].ConsPubKey == pks[0] {
if validators[0].ConsPubKey == valPubKeys[0] {
foundVal = true
}

require.True(t, foundVal, "pk %v, operator %v", pks[0], validators[0].OperatorAddr)
require.True(t, foundVal, "pk %v, operator %v", operAddrs[0], validators[0].OperatorAddr)
}

func TestValidatorQuery(t *testing.T) {
cleanup, pks, port := InitializeTestLCD(t, 1, []sdk.AccAddress{})
cleanup, valPubKeys, operAddrs, port := InitializeTestLCD(t, 1, []sdk.AccAddress{})
defer cleanup()
require.Equal(t, 1, len(pks))
require.Equal(t, 1, len(valPubKeys))
require.Equal(t, 1, len(operAddrs))

validator1Operator := sdk.ValAddress(pks[0].Address())
validator := getValidator(t, port, validator1Operator)
assert.Equal(t, validator.OperatorAddr, validator1Operator, "The returned validator does not hold the correct data")
validator := getValidator(t, port, operAddrs[0])
assert.Equal(t, validator.OperatorAddr, operAddrs[0], "The returned validator does not hold the correct data")
}

func TestBonding(t *testing.T) {
name, password, denom := "test", "1234567890", "steak"
addr, seed := CreateAddr(t, name, password, GetKeyBase(t))
cleanup, pks, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr})

cleanup, _, operAddrs, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr})
defer cleanup()

amt := sdk.NewDec(60)
validator1Operator := sdk.ValAddress(pks[0].Address())
validator := getValidator(t, port, validator1Operator)
validator := getValidator(t, port, operAddrs[0])

// create bond TX
resultTx := doDelegate(t, port, seed, name, password, addr, validator1Operator, 60)
resultTx := doDelegate(t, port, seed, name, password, addr, operAddrs[0], 60)
tests.WaitForHeight(resultTx.Height+1, port)

require.Equal(t, uint32(0), resultTx.CheckTx.Code)
Expand All @@ -534,7 +536,7 @@ func TestBonding(t *testing.T) {
require.Equal(t, int64(40), coins.AmountOf(denom).Int64())

// query validator
bond := getDelegation(t, port, addr, validator1Operator)
bond := getDelegation(t, port, addr, operAddrs[0])
require.Equal(t, amt, bond.Shares)

summary := getDelegationSummary(t, port, addr)
Expand All @@ -545,17 +547,17 @@ func TestBonding(t *testing.T) {

bondedValidators := getDelegatorValidators(t, port, addr)
require.Len(t, bondedValidators, 1)
require.Equal(t, validator1Operator, bondedValidators[0].OperatorAddr)
require.Equal(t, operAddrs[0], bondedValidators[0].OperatorAddr)
require.Equal(t, validator.DelegatorShares.Add(amt).String(), bondedValidators[0].DelegatorShares.String())

bondedValidator := getDelegatorValidator(t, port, addr, validator1Operator)
require.Equal(t, validator1Operator, bondedValidator.OperatorAddr)
bondedValidator := getDelegatorValidator(t, port, addr, operAddrs[0])
require.Equal(t, operAddrs[0], bondedValidator.OperatorAddr)

//////////////////////
// testing unbonding

// create unbond TX
resultTx = doBeginUnbonding(t, port, seed, name, password, addr, validator1Operator, 60)
resultTx = doBeginUnbonding(t, port, seed, name, password, addr, operAddrs[0], 60)
tests.WaitForHeight(resultTx.Height+1, port)

require.Equal(t, uint32(0), resultTx.CheckTx.Code)
Expand All @@ -566,7 +568,7 @@ func TestBonding(t *testing.T) {
coins = acc.GetCoins()
require.Equal(t, int64(40), coins.AmountOf("steak").Int64())

unbonding := getUndelegation(t, port, addr, validator1Operator)
unbonding := getUndelegation(t, port, addr, operAddrs[0])
require.Equal(t, "60", unbonding.Balance.Amount.String())

summary = getDelegationSummary(t, port, addr)
Expand Down Expand Up @@ -599,7 +601,7 @@ func TestBonding(t *testing.T) {
func TestSubmitProposal(t *testing.T) {
name, password := "test", "1234567890"
addr, seed := CreateAddr(t, "test", password, GetKeyBase(t))
cleanup, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr})
cleanup, _, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr})
defer cleanup()

// create SubmitProposal TX
Expand All @@ -621,7 +623,7 @@ func TestSubmitProposal(t *testing.T) {
func TestDeposit(t *testing.T) {
name, password := "test", "1234567890"
addr, seed := CreateAddr(t, "test", password, GetKeyBase(t))
cleanup, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr})
cleanup, _, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr})
defer cleanup()

// create SubmitProposal TX
Expand Down Expand Up @@ -655,7 +657,7 @@ func TestDeposit(t *testing.T) {
func TestVote(t *testing.T) {
name, password := "test", "1234567890"
addr, seed := CreateAddr(t, "test", password, GetKeyBase(t))
cleanup, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr})
cleanup, _, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr})
defer cleanup()

// create SubmitProposal TX
Expand Down Expand Up @@ -693,12 +695,12 @@ func TestVote(t *testing.T) {
func TestUnjail(t *testing.T) {
_, password := "test", "1234567890"
addr, _ := CreateAddr(t, "test", password, GetKeyBase(t))
cleanup, pks, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr})
cleanup, valPubKeys, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr})
defer cleanup()

// XXX: any less than this and it fails
tests.WaitForHeight(3, port)
pkString, _ := sdk.Bech32ifyConsPub(pks[0])
pkString, _ := sdk.Bech32ifyConsPub(valPubKeys[0])
signingInfo := getSigningInfo(t, port, pkString)
tests.WaitForHeight(4, port)
require.Equal(t, true, signingInfo.IndexOffset > 0)
Expand All @@ -711,7 +713,7 @@ func TestProposalsQuery(t *testing.T) {
name2, password2 := "test2", "1234567890"
addr, seed := CreateAddr(t, "test", password1, GetKeyBase(t))
addr2, seed2 := CreateAddr(t, "test2", password2, GetKeyBase(t))
cleanup, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr, addr2})
cleanup, _, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr, addr2})
defer cleanup()

// Addr1 proposes (and deposits) proposals #1 and #2
Expand Down
35 changes: 23 additions & 12 deletions client/lcd/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,14 @@ func CreateAddr(t *testing.T, name, password string, kb crkeys.Keybase) (sdk.Acc
// their respective sockets where nValidators is the total number of validators
// and initAddrs are the accounts to initialize with some steak tokens. It
// returns a cleanup function, a set of validator public keys, and a port.
func InitializeTestLCD(t *testing.T, nValidators int, initAddrs []sdk.AccAddress) (func(), []crypto.PubKey, string) {
func InitializeTestLCD(
t *testing.T, nValidators int, initAddrs []sdk.AccAddress,
) (cleanup func(), valConsPubKeys []crypto.PubKey, valOperAddrs []sdk.ValAddress, port string) {

if nValidators < 1 {
panic("InitializeTestLCD must use at least one validator")
}

config := GetConfig()
config.Consensus.TimeoutCommit = 100
config.Consensus.SkipTimeoutCommit = false
Expand All @@ -136,30 +143,34 @@ func InitializeTestLCD(t *testing.T, nValidators int, initAddrs []sdk.AccAddress
genDoc, err := tmtypes.GenesisDocFromFile(genesisFile)
require.NoError(t, err)

if nValidators < 1 {
panic("InitializeTestLCD must use at least one validator")
// append initial (proposing) validator
genDoc.Validators[0] = tmtypes.GenesisValidator{
PubKey: privVal.GetPubKey(),
Power: 999999, // create enough power to enable 2/3 voting power
Name: "validator-1",
}

// append any additional (non-proposing) validators
for i := 1; i < nValidators; i++ {
genDoc.Validators = append(genDoc.Validators,
tmtypes.GenesisValidator{
PubKey: ed25519.GenPrivKey().PubKey(),
Power: 1,
Name: "val",
Name: fmt.Sprintf("validator-%d", i+1),
},
)
}

var validatorsPKs []crypto.PubKey

// NOTE: It's bad practice to reuse public key address for the operator
// address but doing in the test for simplicity.
var appGenTxs []json.RawMessage

for _, gdValidator := range genDoc.Validators {
operAddr := ed25519.GenPrivKey().PubKey().Address()
pk := gdValidator.PubKey
validatorsPKs = append(validatorsPKs, pk)

appGenTx, _, _, err := gapp.GaiaAppGenTxNF(cdc, pk, sdk.AccAddress(pk.Address()), "test_val1")
valConsPubKeys = append(valConsPubKeys, pk)
valOperAddrs = append(valOperAddrs, sdk.ValAddress(operAddr))

appGenTx, _, _, err := gapp.GaiaAppGenTxNF(cdc, pk, sdk.AccAddress(operAddr), gdValidator.Name)
require.NoError(t, err)

appGenTxs = append(appGenTxs, appGenTx)
Expand Down Expand Up @@ -203,14 +214,14 @@ func InitializeTestLCD(t *testing.T, nValidators int, initAddrs []sdk.AccAddress
tests.WaitForLCDStart(port)
tests.WaitForHeight(1, port)

cleanup := func() {
cleanup = func() {
logger.Debug("cleaning up LCD initialization")
node.Stop()
node.Wait()
lcd.Close()
}

return cleanup, validatorsPKs, port
return cleanup, valConsPubKeys, valOperAddrs, port
}

// startTM creates and starts an in-process Tendermint node with memDB and
Expand Down

0 comments on commit 89d13d1

Please sign in to comment.