From 3910ec25f636717713b3a11334f4738e64712ff6 Mon Sep 17 00:00:00 2001 From: Jonathan Gimeno Date: Mon, 24 Feb 2020 16:14:16 +0100 Subject: [PATCH] refactor and remove unused code --- x/staking/keeper/delegation_test.go | 48 ++++++++++---------- x/staking/keeper/historical_info_test.go | 20 +++++---- x/staking/keeper/test_common_test.go | 56 +----------------------- 3 files changed, 37 insertions(+), 87 deletions(-) diff --git a/x/staking/keeper/delegation_test.go b/x/staking/keeper/delegation_test.go index 004314a92..a1b0c6841 100644 --- a/x/staking/keeper/delegation_test.go +++ b/x/staking/keeper/delegation_test.go @@ -17,8 +17,8 @@ import ( func TestDelegation(t *testing.T) { _, app, ctx := getBaseSimappWithCustomKeeper() - addrs := simapp.AddTestAddrsIncremental(app, ctx, 3, sdk.NewInt(10000)) - valAddrs := simapp.ConvertAddrsToValAddrs(addrs) + addrDels := simapp.AddTestAddrsIncremental(app, ctx, 3, sdk.NewInt(10000)) + valAddrs := simapp.ConvertAddrsToValAddrs(addrDels) //construct the validators amts := []sdk.Int{sdk.NewInt(9), sdk.NewInt(8), sdk.NewInt(7)} @@ -33,31 +33,31 @@ func TestDelegation(t *testing.T) { validators[2] = keeper.TestingUpdateValidator(app.StakingKeeper, ctx, validators[2], true) // first add a validators[0] to delegate too - bond1to1 := types.NewDelegation(addrs[0], valAddrs[0], sdk.NewDec(9)) + bond1to1 := types.NewDelegation(addrDels[0], valAddrs[0], sdk.NewDec(9)) // check the empty keeper first - _, found := app.StakingKeeper.GetDelegation(ctx, addrs[0], valAddrs[0]) + _, found := app.StakingKeeper.GetDelegation(ctx, addrDels[0], valAddrs[0]) require.False(t, found) // set and retrieve a record app.StakingKeeper.SetDelegation(ctx, bond1to1) - resBond, found := app.StakingKeeper.GetDelegation(ctx, addrs[0], valAddrs[0]) + resBond, found := app.StakingKeeper.GetDelegation(ctx, addrDels[0], valAddrs[0]) require.True(t, found) require.True(t, bond1to1.Equal(resBond)) // modify a records, save, and retrieve bond1to1.Shares = sdk.NewDec(99) app.StakingKeeper.SetDelegation(ctx, bond1to1) - resBond, found = app.StakingKeeper.GetDelegation(ctx, addrs[0], valAddrs[0]) + resBond, found = app.StakingKeeper.GetDelegation(ctx, addrDels[0], valAddrs[0]) require.True(t, found) require.True(t, bond1to1.Equal(resBond)) // add some more records - bond1to2 := types.NewDelegation(addrs[0], valAddrs[1], sdk.NewDec(9)) - bond1to3 := types.NewDelegation(addrs[0], valAddrs[2], sdk.NewDec(9)) - bond2to1 := types.NewDelegation(addrs[1], valAddrs[0], sdk.NewDec(9)) - bond2to2 := types.NewDelegation(addrs[1], valAddrs[1], sdk.NewDec(9)) - bond2to3 := types.NewDelegation(addrs[1], valAddrs[2], sdk.NewDec(9)) + bond1to2 := types.NewDelegation(addrDels[0], valAddrs[1], sdk.NewDec(9)) + bond1to3 := types.NewDelegation(addrDels[0], valAddrs[2], sdk.NewDec(9)) + bond2to1 := types.NewDelegation(addrDels[1], valAddrs[0], sdk.NewDec(9)) + bond2to2 := types.NewDelegation(addrDels[1], valAddrs[1], sdk.NewDec(9)) + bond2to3 := types.NewDelegation(addrDels[1], valAddrs[2], sdk.NewDec(9)) app.StakingKeeper.SetDelegation(ctx, bond1to2) app.StakingKeeper.SetDelegation(ctx, bond1to3) app.StakingKeeper.SetDelegation(ctx, bond2to1) @@ -65,16 +65,16 @@ func TestDelegation(t *testing.T) { app.StakingKeeper.SetDelegation(ctx, bond2to3) // test all bond retrieve capabilities - resBonds := app.StakingKeeper.GetDelegatorDelegations(ctx, addrs[0], 5) + resBonds := app.StakingKeeper.GetDelegatorDelegations(ctx, addrDels[0], 5) require.Equal(t, 3, len(resBonds)) require.True(t, bond1to1.Equal(resBonds[0])) require.True(t, bond1to2.Equal(resBonds[1])) require.True(t, bond1to3.Equal(resBonds[2])) - resBonds = app.StakingKeeper.GetAllDelegatorDelegations(ctx, addrs[0]) + resBonds = app.StakingKeeper.GetAllDelegatorDelegations(ctx, addrDels[0]) require.Equal(t, 3, len(resBonds)) - resBonds = app.StakingKeeper.GetDelegatorDelegations(ctx, addrs[0], 2) + resBonds = app.StakingKeeper.GetDelegatorDelegations(ctx, addrDels[0], 2) require.Equal(t, 2, len(resBonds)) - resBonds = app.StakingKeeper.GetDelegatorDelegations(ctx, addrs[1], 5) + resBonds = app.StakingKeeper.GetDelegatorDelegations(ctx, addrDels[1], 5) require.Equal(t, 3, len(resBonds)) require.True(t, bond2to1.Equal(resBonds[0])) require.True(t, bond2to2.Equal(resBonds[1])) @@ -88,17 +88,17 @@ func TestDelegation(t *testing.T) { require.True(t, bond2to2.Equal(allBonds[4])) require.True(t, bond2to3.Equal(allBonds[5])) - resVals := app.StakingKeeper.GetDelegatorValidators(ctx, addrs[0], 3) + resVals := app.StakingKeeper.GetDelegatorValidators(ctx, addrDels[0], 3) require.Equal(t, 3, len(resVals)) - resVals = app.StakingKeeper.GetDelegatorValidators(ctx, addrs[1], 4) + resVals = app.StakingKeeper.GetDelegatorValidators(ctx, addrDels[1], 4) require.Equal(t, 3, len(resVals)) for i := 0; i < 3; i++ { - resVal, err := app.StakingKeeper.GetDelegatorValidator(ctx, addrs[0], valAddrs[i]) + resVal, err := app.StakingKeeper.GetDelegatorValidator(ctx, addrDels[0], valAddrs[i]) require.Nil(t, err) require.Equal(t, valAddrs[i], resVal.GetOperator()) - resVal, err = app.StakingKeeper.GetDelegatorValidator(ctx, addrs[1], valAddrs[i]) + resVal, err = app.StakingKeeper.GetDelegatorValidator(ctx, addrDels[1], valAddrs[i]) require.Nil(t, err) require.Equal(t, valAddrs[i], resVal.GetOperator()) @@ -108,22 +108,22 @@ func TestDelegation(t *testing.T) { // delete a record app.StakingKeeper.RemoveDelegation(ctx, bond2to3) - _, found = app.StakingKeeper.GetDelegation(ctx, addrs[1], valAddrs[2]) + _, found = app.StakingKeeper.GetDelegation(ctx, addrDels[1], valAddrs[2]) require.False(t, found) - resBonds = app.StakingKeeper.GetDelegatorDelegations(ctx, addrs[1], 5) + resBonds = app.StakingKeeper.GetDelegatorDelegations(ctx, addrDels[1], 5) require.Equal(t, 2, len(resBonds)) require.True(t, bond2to1.Equal(resBonds[0])) require.True(t, bond2to2.Equal(resBonds[1])) - resBonds = app.StakingKeeper.GetAllDelegatorDelegations(ctx, addrs[1]) + resBonds = app.StakingKeeper.GetAllDelegatorDelegations(ctx, addrDels[1]) require.Equal(t, 2, len(resBonds)) // delete all the records from delegator 2 app.StakingKeeper.RemoveDelegation(ctx, bond2to1) app.StakingKeeper.RemoveDelegation(ctx, bond2to2) - _, found = app.StakingKeeper.GetDelegation(ctx, addrs[1], valAddrs[0]) + _, found = app.StakingKeeper.GetDelegation(ctx, addrDels[1], valAddrs[0]) require.False(t, found) - _, found = app.StakingKeeper.GetDelegation(ctx, addrs[1], valAddrs[1]) + _, found = app.StakingKeeper.GetDelegation(ctx, addrDels[1], valAddrs[1]) require.False(t, found) resBonds = app.StakingKeeper.GetDelegatorDelegations(ctx, addrDels[1], 5) require.Equal(t, 0, len(resBonds)) diff --git a/x/staking/keeper/historical_info_test.go b/x/staking/keeper/historical_info_test.go index 73cc64de0..d4ed69310 100644 --- a/x/staking/keeper/historical_info_test.go +++ b/x/staking/keeper/historical_info_test.go @@ -15,8 +15,10 @@ import ( ) func TestHistoricalInfo(t *testing.T) { - app := simapp.Setup(false) - ctx := app.BaseApp.NewContext(false, abci.Header{}) + _, app, ctx := getBaseSimappWithCustomKeeper() + + addrDels := simapp.AddTestAddrsIncremental(app, ctx, 50, sdk.NewInt(0)) + addrVals := simapp.ConvertAddrsToValAddrs(addrDels) validators := make([]types.Validator, len(addrVals)) @@ -41,8 +43,10 @@ func TestHistoricalInfo(t *testing.T) { } func TestTrackHistoricalInfo(t *testing.T) { - app := simapp.Setup(false) - ctx := app.BaseApp.NewContext(false, abci.Header{}) + _, app, ctx := getBaseSimappWithCustomKeeper() + + addrDels := simapp.AddTestAddrsIncremental(app, ctx, 50, sdk.NewInt(0)) + addrVals := simapp.ConvertAddrsToValAddrs(addrDels) // set historical entries in params to 5 params := types.DefaultParams() @@ -60,8 +64,8 @@ func TestTrackHistoricalInfo(t *testing.T) { Height: 5, } valSet := []types.Validator{ - types.NewValidator(sdk.ValAddress(Addrs[0]), PKs[0], types.Description{}), - types.NewValidator(sdk.ValAddress(Addrs[1]), PKs[1], types.Description{}), + types.NewValidator(addrVals[0], PKs[0], types.Description{}), + types.NewValidator(addrVals[1], PKs[1], types.Description{}), } hi4 := types.NewHistoricalInfo(h4, valSet) hi5 := types.NewHistoricalInfo(h5, valSet) @@ -75,10 +79,10 @@ func TestTrackHistoricalInfo(t *testing.T) { require.Equal(t, hi5, recv) // Set last validators in keeper - val1 := types.NewValidator(sdk.ValAddress(Addrs[2]), PKs[2], types.Description{}) + val1 := types.NewValidator(addrVals[2], PKs[2], types.Description{}) app.StakingKeeper.SetValidator(ctx, val1) app.StakingKeeper.SetLastValidatorPower(ctx, val1.OperatorAddress, 10) - val2 := types.NewValidator(sdk.ValAddress(Addrs[3]), PKs[3], types.Description{}) + val2 := types.NewValidator(addrVals[3], PKs[3], types.Description{}) vals := []types.Validator{val1, val2} sort.Sort(types.Validators(vals)) app.StakingKeeper.SetValidator(ctx, val2) diff --git a/x/staking/keeper/test_common_test.go b/x/staking/keeper/test_common_test.go index 7c6cf8f0c..2275aff10 100644 --- a/x/staking/keeper/test_common_test.go +++ b/x/staking/keeper/test_common_test.go @@ -17,40 +17,9 @@ import ( ) var ( - Addrs = createTestAddrs(500) - PKs = createTestPubKeys(500) - - addrDels = []sdk.AccAddress{ - Addrs[0], - Addrs[1], - } - addrVals = []sdk.ValAddress{ - sdk.ValAddress(Addrs[2]), - sdk.ValAddress(Addrs[3]), - sdk.ValAddress(Addrs[4]), - sdk.ValAddress(Addrs[5]), - sdk.ValAddress(Addrs[6]), - } + PKs = createTestPubKeys(500) ) -func createTestAddrs(numAddrs int) []sdk.AccAddress { - var addresses []sdk.AccAddress - var buffer bytes.Buffer - - // start at 100 so we can make up to 999 test addresses with valid test addresses - for i := 100; i < (numAddrs + 100); i++ { - numString := strconv.Itoa(i) - buffer.WriteString("A58856F0FD53BF058B4909A21AEC019107BA6") //base address string - - buffer.WriteString(numString) //adding on final two digits to make addresses unique - res, _ := sdk.AccAddressFromHex(buffer.String()) - bech := res.String() - addresses = append(addresses, CreateTestAddr(buffer.String(), bech)) - buffer.Reset() - } - return addresses -} - // nolint: unparam func createTestPubKeys(numPubKeys int) []crypto.PubKey { var publicKeys []crypto.PubKey @@ -79,29 +48,6 @@ func NewPubKey(pk string) (res crypto.PubKey) { return pkEd } -// for incode address generation -func CreateTestAddr(addr string, bech string) sdk.AccAddress { - res, err := sdk.AccAddressFromHex(addr) - if err != nil { - panic(err) - } - - bechexpected := res.String() - if bech != bechexpected { - panic("Bech encoding doesn't match reference") - } - - bechres, err := sdk.AccAddressFromBech32(bech) - if err != nil { - panic(err) - } - if !bytes.Equal(bechres, res) { - panic("Bech decode and hex decode don't match") - } - - return res -} - // getBaseSimappWithCustomKeeper Returns a simapp with custom StakingKeeper // to avoid messing with the hooks. func getBaseSimappWithCustomKeeper() (*codec.Codec, *simapp.SimApp, sdk.Context) {