comment before creating new way to generate accounts

This commit is contained in:
Jonathan Gimeno 2020-02-21 19:19:57 +01:00
parent 01aaf8446d
commit 89785765e0
1 changed files with 114 additions and 113 deletions

View File

@ -15,119 +15,120 @@ import (
)
// tests GetDelegation, GetDelegatorDelegations, SetDelegation, RemoveDelegation, GetDelegatorDelegations
func TestDelegation(t *testing.T) {
_, app, ctx := getBaseSimappWithCustomKeeper()
//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(addrVals[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(addrDels[0], addrVals[0], sdk.NewDec(9))
// check the empty keeper first
_, found := app.StakingKeeper.GetDelegation(ctx, addrDels[0], addrVals[0])
require.False(t, found)
// set and retrieve a record
app.StakingKeeper.SetDelegation(ctx, bond1to1)
resBond, found := app.StakingKeeper.GetDelegation(ctx, addrDels[0], addrVals[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, addrDels[0], addrVals[0])
require.True(t, found)
require.True(t, bond1to1.Equal(resBond))
// add some more records
bond1to2 := types.NewDelegation(addrDels[0], addrVals[1], sdk.NewDec(9))
bond1to3 := types.NewDelegation(addrDels[0], addrVals[2], sdk.NewDec(9))
bond2to1 := types.NewDelegation(addrDels[1], addrVals[0], sdk.NewDec(9))
bond2to2 := types.NewDelegation(addrDels[1], addrVals[1], sdk.NewDec(9))
bond2to3 := types.NewDelegation(addrDels[1], addrVals[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, 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, addrDels[0])
require.Equal(t, 3, len(resBonds))
resBonds = app.StakingKeeper.GetDelegatorDelegations(ctx, addrDels[0], 2)
require.Equal(t, 2, len(resBonds))
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]))
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, addrDels[0], 3)
require.Equal(t, 3, len(resVals))
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, addrDels[0], addrVals[i])
require.Nil(t, err)
require.Equal(t, addrVals[i], resVal.GetOperator())
resVal, err = app.StakingKeeper.GetDelegatorValidator(ctx, addrDels[1], addrVals[i])
require.Nil(t, err)
require.Equal(t, addrVals[i], resVal.GetOperator())
resDels := app.StakingKeeper.GetValidatorDelegations(ctx, addrVals[i])
require.Len(t, resDels, 2)
}
// delete a record
app.StakingKeeper.RemoveDelegation(ctx, bond2to3)
_, found = app.StakingKeeper.GetDelegation(ctx, addrDels[1], addrVals[2])
require.False(t, found)
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, 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, addrDels[1], addrVals[0])
require.False(t, found)
_, found = app.StakingKeeper.GetDelegation(ctx, addrDels[1], addrVals[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.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))
//}
//
// tests Get/Set/Remove UnbondingDelegation
func TestUnbondingDelegation(t *testing.T) {
_, app, ctx := getBaseSimappWithCustomKeeper()