From ee7ccc37040862ef897a8aa32405537670cab686 Mon Sep 17 00:00:00 2001 From: Jonathan Gimeno Date: Fri, 21 Feb 2020 19:34:07 +0100 Subject: [PATCH] make TestDelegation pass with generated accounts --- simapp/test_helpers.go | 64 +++++++- x/staking/keeper/delegation_test.go | 228 ++++++++++++++-------------- 2 files changed, 175 insertions(+), 117 deletions(-) diff --git a/simapp/test_helpers.go b/simapp/test_helpers.go index 31b4db3b80..36237bc5ec 100644 --- a/simapp/test_helpers.go +++ b/simapp/test_helpers.go @@ -80,15 +80,52 @@ func SetupWithGenesisAccounts(genAccs []authexported.GenesisAccount, balances .. return app } -// AddTestAddrs constructs and returns accNum amount of accounts with an -// initial balance of accAmt -func AddTestAddrs(app *SimApp, ctx sdk.Context, accNum int, accAmt sdk.Int) []sdk.AccAddress { +type GenerateAccountStrategy func(int) []sdk.AccAddress + +func Random(accNum int) []sdk.AccAddress { testAddrs := make([]sdk.AccAddress, accNum) for i := 0; i < accNum; i++ { pk := ed25519.GenPrivKey().PubKey() testAddrs[i] = sdk.AccAddress(pk.Address()) } + return testAddrs +} + +func Incremental(accNum 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 < (accNum + 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, TestAddr(buffer.String(), bech)) + buffer.Reset() + } + + return addresses +} + +// AddTestAddrs constructs and returns accNum amount of accounts with an +// initial balance of accAmt in random order +func AddTestAddrs(app *SimApp, ctx sdk.Context, accNum int, accAmt sdk.Int) []sdk.AccAddress { + return addTestAddrs(app, ctx, accNum, accAmt, Random) +} + +// AddTestAddrs constructs and returns accNum amount of accounts with an +// initial balance of accAmt in random order +func AddTestAddrsIncremental(app *SimApp, ctx sdk.Context, accNum int, accAmt sdk.Int) []sdk.AccAddress { + return addTestAddrs(app, ctx, accNum, accAmt, Incremental) +} + +func addTestAddrs(app *SimApp, ctx sdk.Context, accNum int, accAmt sdk.Int, strategy GenerateAccountStrategy) []sdk.AccAddress { + testAddrs := strategy(accNum) + initCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), accAmt)) totalSupply := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), accAmt.MulRaw(int64(len(testAddrs))))) prevSupply := app.SupplyKeeper.GetSupply(ctx) @@ -107,6 +144,27 @@ func AddTestAddrs(app *SimApp, ctx sdk.Context, accNum int, accAmt sdk.Int) []sd return testAddrs } +func TestAddr(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 +} + // CheckBalance checks the balance of an account. func CheckBalance(t *testing.T, app *SimApp, addr sdk.AccAddress, balances sdk.Coins) { ctxCheck := app.BaseApp.NewContext(true, abci.Header{}) diff --git a/x/staking/keeper/delegation_test.go b/x/staking/keeper/delegation_test.go index 45879644be..7c0bac9c4b 100644 --- a/x/staking/keeper/delegation_test.go +++ b/x/staking/keeper/delegation_test.go @@ -15,120 +15,120 @@ import ( ) // tests GetDelegation, GetDelegatorDelegations, SetDelegation, RemoveDelegation, GetDelegatorDelegations -//func TestDelegation(t *testing.T) { -// _, app, ctx := getBaseSimappWithCustomKeeper() -// -// addrs := simapp.AddTestAddrs(app, ctx, 3, sdk.NewInt(10000)) -// -// //construct the validators -// amts := []sdk.Int{sdk.NewInt(9), sdk.NewInt(8), sdk.NewInt(7)} -// var validators [3]types.Validator -// for i, amt := range amts { -// validators[i] = types.NewValidator(sdk.ValAddress(addrs[i]), PKs[i], types.Description{}) -// validators[i], _ = validators[i].AddTokensFromDel(amt) -// } -// -// validators[0] = keeper.TestingUpdateValidator(app.StakingKeeper, ctx, validators[0], true) -// validators[1] = keeper.TestingUpdateValidator(app.StakingKeeper, ctx, validators[1], true) -// validators[2] = keeper.TestingUpdateValidator(app.StakingKeeper, ctx, validators[2], true) -// -// // first add a validators[0] to delegate too -// bond1to1 := types.NewDelegation(addrs[0], sdk.ValAddress(addrs[0]), sdk.NewDec(9)) -// -// // check the empty keeper first -// _, found := app.StakingKeeper.GetDelegation(ctx, addrs[0], sdk.ValAddress(addrs[0])) -// require.False(t, found) -// -// // set and retrieve a record -// app.StakingKeeper.SetDelegation(ctx, bond1to1) -// resBond, found := app.StakingKeeper.GetDelegation(ctx, addrs[0], sdk.ValAddress(addrs[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], sdk.ValAddress(addrs[0])) -// require.True(t, found) -// require.True(t, bond1to1.Equal(resBond)) -// -// // add some more records -// bond1to2 := types.NewDelegation(addrs[0], sdk.ValAddress(addrs[1]), sdk.NewDec(9)) -// bond1to3 := types.NewDelegation(addrs[0], sdk.ValAddress(addrs[2]), sdk.NewDec(9)) -// bond2to1 := types.NewDelegation(addrs[1], sdk.ValAddress(addrs[0]), sdk.NewDec(9)) -// bond2to2 := types.NewDelegation(addrs[1], sdk.ValAddress(addrs[1]), sdk.NewDec(9)) -// bond2to3 := types.NewDelegation(addrs[1], sdk.ValAddress(addrs[2]), sdk.NewDec(9)) -// app.StakingKeeper.SetDelegation(ctx, bond1to2) -// app.StakingKeeper.SetDelegation(ctx, bond1to3) -// app.StakingKeeper.SetDelegation(ctx, bond2to1) -// app.StakingKeeper.SetDelegation(ctx, bond2to2) -// app.StakingKeeper.SetDelegation(ctx, bond2to3) -// -// // test all bond retrieve capabilities -// resBonds := app.StakingKeeper.GetDelegatorDelegations(ctx, addrs[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]) -// require.Equal(t, 3, len(resBonds)) -// resBonds = app.StakingKeeper.GetDelegatorDelegations(ctx, addrs[0], 2) -// require.Equal(t, 2, len(resBonds)) -// resBonds = app.StakingKeeper.GetDelegatorDelegations(ctx, addrs[1], 5) -// require.Equal(t, 3, len(resBonds)) -// require.True(t, bond2to1.Equal(resBonds[0])) -// require.True(t, bond2to2.Equal(resBonds[1])) -// require.True(t, bond2to3.Equal(resBonds[2])) -// allBonds := app.StakingKeeper.GetAllDelegations(ctx) -// require.Equal(t, 6, len(allBonds)) -// require.True(t, bond1to1.Equal(allBonds[0])) -// require.True(t, bond1to2.Equal(allBonds[1])) -// require.True(t, bond1to3.Equal(allBonds[2])) -// require.True(t, bond2to1.Equal(allBonds[3])) -// require.True(t, bond2to2.Equal(allBonds[4])) -// require.True(t, bond2to3.Equal(allBonds[5])) -// -// resVals := app.StakingKeeper.GetDelegatorValidators(ctx, addrs[0], 3) -// require.Equal(t, 3, len(resVals)) -// resVals = app.StakingKeeper.GetDelegatorValidators(ctx, addrs[1], 4) -// require.Equal(t, 3, len(resVals)) -// -// for i := 0; i < 3; i++ { -// resVal, err := app.StakingKeeper.GetDelegatorValidator(ctx, addrs[0], sdk.ValAddress(addrs[i])) -// require.Nil(t, err) -// require.Equal(t, addrs[i], resVal.GetOperator()) -// -// resVal, err = app.StakingKeeper.GetDelegatorValidator(ctx, addrs[1], sdk.ValAddress(addrs[i])) -// require.Nil(t, err) -// require.Equal(t, addrs[i], resVal.GetOperator()) -// -// resDels := app.StakingKeeper.GetValidatorDelegations(ctx, sdk.ValAddress(addrs[i])) -// require.Len(t, resDels, 2) -// } -// -// // delete a record -// app.StakingKeeper.RemoveDelegation(ctx, bond2to3) -// _, found = app.StakingKeeper.GetDelegation(ctx, addrs[1], sdk.ValAddress(addrs[2])) -// require.False(t, found) -// resBonds = app.StakingKeeper.GetDelegatorDelegations(ctx, addrs[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]) -// 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], sdk.ValAddress(addrs[0])) -// require.False(t, found) -// _, found = app.StakingKeeper.GetDelegation(ctx, addrs[1], sdk.ValAddress(addrs[1])) -// require.False(t, found) -// resBonds = app.StakingKeeper.GetDelegatorDelegations(ctx, addrDels[1], 5) -// require.Equal(t, 0, len(resBonds)) -//} -// +func TestDelegation(t *testing.T) { + _, app, ctx := getBaseSimappWithCustomKeeper() + + addrs := simapp.AddTestAddrsIncremental(app, ctx, 3, sdk.NewInt(10000)) + + //construct the validators + amts := []sdk.Int{sdk.NewInt(9), sdk.NewInt(8), sdk.NewInt(7)} + var validators [3]types.Validator + for i, amt := range amts { + validators[i] = types.NewValidator(sdk.ValAddress(addrs[i]), PKs[i], types.Description{}) + validators[i], _ = validators[i].AddTokensFromDel(amt) + } + + validators[0] = keeper.TestingUpdateValidator(app.StakingKeeper, ctx, validators[0], true) + validators[1] = keeper.TestingUpdateValidator(app.StakingKeeper, ctx, validators[1], true) + validators[2] = keeper.TestingUpdateValidator(app.StakingKeeper, ctx, validators[2], true) + + // first add a validators[0] to delegate too + bond1to1 := types.NewDelegation(addrs[0], sdk.ValAddress(addrs[0]), sdk.NewDec(9)) + + // check the empty keeper first + _, found := app.StakingKeeper.GetDelegation(ctx, addrs[0], sdk.ValAddress(addrs[0])) + require.False(t, found) + + // set and retrieve a record + app.StakingKeeper.SetDelegation(ctx, bond1to1) + resBond, found := app.StakingKeeper.GetDelegation(ctx, addrs[0], sdk.ValAddress(addrs[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], sdk.ValAddress(addrs[0])) + require.True(t, found) + require.True(t, bond1to1.Equal(resBond)) + + // add some more records + bond1to2 := types.NewDelegation(addrs[0], sdk.ValAddress(addrs[1]), sdk.NewDec(9)) + bond1to3 := types.NewDelegation(addrs[0], sdk.ValAddress(addrs[2]), sdk.NewDec(9)) + bond2to1 := types.NewDelegation(addrs[1], sdk.ValAddress(addrs[0]), sdk.NewDec(9)) + bond2to2 := types.NewDelegation(addrs[1], sdk.ValAddress(addrs[1]), sdk.NewDec(9)) + bond2to3 := types.NewDelegation(addrs[1], sdk.ValAddress(addrs[2]), sdk.NewDec(9)) + app.StakingKeeper.SetDelegation(ctx, bond1to2) + app.StakingKeeper.SetDelegation(ctx, bond1to3) + app.StakingKeeper.SetDelegation(ctx, bond2to1) + app.StakingKeeper.SetDelegation(ctx, bond2to2) + app.StakingKeeper.SetDelegation(ctx, bond2to3) + + // test all bond retrieve capabilities + resBonds := app.StakingKeeper.GetDelegatorDelegations(ctx, addrs[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]) + require.Equal(t, 3, len(resBonds)) + resBonds = app.StakingKeeper.GetDelegatorDelegations(ctx, addrs[0], 2) + require.Equal(t, 2, len(resBonds)) + resBonds = app.StakingKeeper.GetDelegatorDelegations(ctx, addrs[1], 5) + require.Equal(t, 3, len(resBonds)) + require.True(t, bond2to1.Equal(resBonds[0])) + require.True(t, bond2to2.Equal(resBonds[1])) + require.True(t, bond2to3.Equal(resBonds[2])) + allBonds := app.StakingKeeper.GetAllDelegations(ctx) + require.Equal(t, 6, len(allBonds)) + require.True(t, bond1to1.Equal(allBonds[0])) + require.True(t, bond1to2.Equal(allBonds[1])) + require.True(t, bond1to3.Equal(allBonds[2])) + require.True(t, bond2to1.Equal(allBonds[3])) + require.True(t, bond2to2.Equal(allBonds[4])) + require.True(t, bond2to3.Equal(allBonds[5])) + + resVals := app.StakingKeeper.GetDelegatorValidators(ctx, addrs[0], 3) + require.Equal(t, 3, len(resVals)) + resVals = app.StakingKeeper.GetDelegatorValidators(ctx, addrs[1], 4) + require.Equal(t, 3, len(resVals)) + + for i := 0; i < 3; i++ { + resVal, err := app.StakingKeeper.GetDelegatorValidator(ctx, addrs[0], sdk.ValAddress(addrs[i])) + require.Nil(t, err) + require.Equal(t, sdk.ValAddress(addrs[i]), resVal.GetOperator()) + + resVal, err = app.StakingKeeper.GetDelegatorValidator(ctx, addrs[1], sdk.ValAddress(addrs[i])) + require.Nil(t, err) + require.Equal(t, sdk.ValAddress(addrs[i]), resVal.GetOperator()) + + resDels := app.StakingKeeper.GetValidatorDelegations(ctx, sdk.ValAddress(addrs[i])) + require.Len(t, resDels, 2) + } + + // delete a record + app.StakingKeeper.RemoveDelegation(ctx, bond2to3) + _, found = app.StakingKeeper.GetDelegation(ctx, addrs[1], sdk.ValAddress(addrs[2])) + require.False(t, found) + resBonds = app.StakingKeeper.GetDelegatorDelegations(ctx, addrs[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]) + 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], sdk.ValAddress(addrs[0])) + require.False(t, found) + _, found = app.StakingKeeper.GetDelegation(ctx, addrs[1], sdk.ValAddress(addrs[1])) + require.False(t, found) + resBonds = app.StakingKeeper.GetDelegatorDelegations(ctx, addrDels[1], 5) + require.Equal(t, 0, len(resBonds)) +} + // tests Get/Set/Remove UnbondingDelegation func TestUnbondingDelegation(t *testing.T) { _, app, ctx := getBaseSimappWithCustomKeeper()