From b3d6a59c619a9779a2bb3b9d39c7ae857e710da1 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Mon, 2 Jul 2018 18:58:52 -0400 Subject: [PATCH] ... --- x/stake/keeper/delegation.go | 20 +++++++------- x/stake/keeper/delegation_test.go | 44 ++++++++++++++++++++++++------- x/stake/keeper/key.go | 4 +++ 3 files changed, 47 insertions(+), 21 deletions(-) diff --git a/x/stake/keeper/delegation.go b/x/stake/keeper/delegation.go index 8ede4b55e..0fe4406c3 100644 --- a/x/stake/keeper/delegation.go +++ b/x/stake/keeper/delegation.go @@ -154,26 +154,24 @@ func (k Keeper) GetRedelegation(ctx sdk.Context, func (k Keeper) GetRedelegationsFromValidator(ctx sdk.Context, valAddr sdk.Address) (redelegations []types.Redelegation) { store := ctx.KVStore(k.storeKey) iterator := sdk.KVStorePrefixIterator(store, GetREDsFromValSrcIndexKey(valAddr)) - i := 0 - for ; ; i++ { - fmt.Printf("debug i: %v\n", i) + for { if !iterator.Valid() { break } - redelegationKeyOrig := iterator.Value() - redelegationBytes2 := store.Get(redelegationKeyOrig) - var redelegation2 types.Redelegation - k.cdc.MustUnmarshalBinary(redelegationBytes2, &redelegation2) - fmt.Printf("debug orig redelegation2: %v\n", redelegation2) - redelegationKey := GetREDKeyFromValSrcIndexKey(iterator.Key()) - fmt.Printf("debug orig redelegationKey: %v\n", redelegationKeyOrig) + fmt.Println("called") + //redelegationKey := iterator.Value() + iKey := iterator.Key() + redelegationKey := GetREDKeyFromValSrcIndexKey(iKey) + fmt.Printf("debug iteratorValue: %v\n", iterator.Value()) + fmt.Printf("debug iteratorKey: %v\n", iKey) fmt.Printf("debug redelegationKey: %v\n", redelegationKey) - fmt.Printf("debug iteratorKey: %v\n", iterator.Key()) + redelegationBytes := store.Get(redelegationKey) var redelegation types.Redelegation k.cdc.MustUnmarshalBinary(redelegationBytes, &redelegation) redelegations = append(redelegations, redelegation) + iterator.Next() } iterator.Close() diff --git a/x/stake/keeper/delegation_test.go b/x/stake/keeper/delegation_test.go index 4b5d12e09..90cfb8f37 100644 --- a/x/stake/keeper/delegation_test.go +++ b/x/stake/keeper/delegation_test.go @@ -180,6 +180,40 @@ func TestUnbondDelegation(t *testing.T) { require.Equal(t, int64(4), pool.BondedTokens) } +// tests Get/Set/Remove/Has UnbondingDelegation +func TestGetRedelegationsFromValidator(t *testing.T) { + ctx, _, keeper := CreateTestInput(t, false, 0) + + rd := types.Redelegation{ + DelegatorAddr: addrDels[0], + ValidatorSrcAddr: addrVals[0], + ValidatorDstAddr: addrVals[1], + CreationHeight: 0, + MinTime: 0, + SharesSrc: sdk.NewRat(5), + SharesDst: sdk.NewRat(5), + } + + // set and retrieve a record + keeper.SetRedelegation(ctx, rd) + resBond, found := keeper.GetRedelegation(ctx, addrDels[0], addrVals[0], addrVals[1]) + require.True(t, found) + fmt.Printf("debug addrDels[0]: %v\n", addrDels[0]) + fmt.Printf("debug addrVals[0]: %v\n", addrVals[0]) + fmt.Printf("debug addrVals[1]: %v\n", addrVals[1]) + + fmt.Println("zoo0") + redelegations := keeper.GetRedelegationsFromValidator(ctx, addrVals[0]) + require.Equal(t, 1, len(redelegations)) + require.True(t, redelegations[0].Equal(resBond)) + + fmt.Println("zoo1") + redelegations = keeper.GetRedelegationsFromValidator(ctx, addrVals[0]) + require.Equal(t, 1, len(redelegations)) + require.True(t, redelegations[0].Equal(resBond)) + fmt.Println("zoo2") +} + // tests Get/Set/Remove/Has UnbondingDelegation func TestRedelegation(t *testing.T) { ctx, _, keeper := CreateTestInput(t, false, 0) @@ -203,21 +237,13 @@ func TestRedelegation(t *testing.T) { resBond, found := keeper.GetRedelegation(ctx, addrDels[0], addrVals[0], addrVals[1]) require.True(t, found) - fmt.Println("zoo0") redelegations := keeper.GetRedelegationsFromValidator(ctx, addrVals[0]) require.Equal(t, 1, len(redelegations)) require.True(t, redelegations[0].Equal(resBond)) - fmt.Println("zoo1") - redelegations = keeper.GetRedelegationsFromValidator(ctx, addrVals[0]) - require.Equal(t, 1, len(redelegations)) - require.True(t, redelegations[0].Equal(resBond)) - fmt.Println("zoo2") - // check if has the redelegation has = keeper.HasReceivingRedelegation(ctx, addrDels[0], addrVals[1]) require.True(t, has) - fmt.Println("hoolahoop") // modify a records, save, and retrieve rd.SharesSrc = sdk.NewRat(21) @@ -228,12 +254,10 @@ func TestRedelegation(t *testing.T) { resBond, found = keeper.GetRedelegation(ctx, addrDels[0], addrVals[0], addrVals[1]) require.True(t, found) require.True(t, rd.Equal(resBond)) - fmt.Println("hippo2") redelegations = keeper.GetRedelegationsFromValidator(ctx, addrVals[0]) require.Equal(t, 1, len(redelegations)) require.True(t, redelegations[0].Equal(resBond)) - fmt.Println("zzzzzzzzzzzzzzzzzzebra") // delete a record keeper.RemoveRedelegation(ctx, rd) diff --git a/x/stake/keeper/key.go b/x/stake/keeper/key.go index c0f8c8b29..248ee9db0 100644 --- a/x/stake/keeper/key.go +++ b/x/stake/keeper/key.go @@ -2,6 +2,7 @@ package keeper import ( "encoding/binary" + "fmt" "github.com/tendermint/tendermint/crypto" @@ -205,6 +206,9 @@ func GetREDKeyFromValSrcIndexKey(IndexKey []byte) []byte { valSrcAddr := addrs[:split] delAddr := addrs[split : 2*split] valDstAddr := addrs[2*split:] + fmt.Printf("debug delAddr: %v\n", delAddr) + fmt.Printf("debug valSrcAddr: %v\n", valSrcAddr) + fmt.Printf("debug valDstAddr: %v\n", valDstAddr) return GetREDKey(delAddr, valSrcAddr, valDstAddr) }