Skip to content

Commit

Permalink
Use address instead of bond height / intratxcounter for deduplication (
Browse files Browse the repository at this point in the history
  • Loading branch information
jaekwon authored Dec 10, 2018
1 parent 40a30b7 commit 133134c
Show file tree
Hide file tree
Showing 19 changed files with 227 additions and 243 deletions.
1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ BREAKING CHANGES

* SDK
- [auth] \#2952 Signatures are no longer serialized on chain with the account number and sequence number
- [stake] \#3055 Use address instead of bond height / intratxcounter for deduplication

* Tendermint

Expand Down
1 change: 0 additions & 1 deletion cmd/gaia/app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ func (app *GaiaApp) prepForZeroHeightGenesis(ctx sdk.Context) {
panic("expected validator, not found")
}
validator.BondHeight = 0
validator.BondIntraTxCounter = counter
validator.UnbondingHeight = 0
app.stakeKeeper.SetValidator(ctx, validator)
counter++
Expand Down
4 changes: 2 additions & 2 deletions x/slashing/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,13 +419,13 @@ func TestValidatorDippingInAndOut(t *testing.T) {
ctx = ctx.WithBlockHeight(height)

// validator added back in
got = sh(ctx, newTestMsgDelegate(sdk.AccAddress(addrs[2]), addrs[0], sdk.NewInt(2)))
got = sh(ctx, newTestMsgDelegate(sdk.AccAddress(addrs[2]), addrs[0], sdk.NewInt(3)))
require.True(t, got.IsOK())
validatorUpdates, _ = stake.EndBlocker(ctx, sk)
require.Equal(t, 2, len(validatorUpdates))
validator, _ = sk.GetValidator(ctx, addr)
require.Equal(t, sdk.Bonded, validator.Status)
newAmt = int64(102)
newAmt = int64(103)

// validator misses a block
keeper.handleValidatorSignature(ctx, val.Address(), newAmt, false)
Expand Down
18 changes: 5 additions & 13 deletions x/stake/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import (
"github.com/cosmos/cosmos-sdk/x/stake/types"
)

// InitGenesis sets the pool and parameters for the provided keeper and
// initializes the IntraTxCounter. For each validator in data, it sets that
// validator in the keeper along with manually setting the indexes. In
// addition, it also sets any delegations found in data. Finally, it updates
// the bonded validators.
// InitGenesis sets the pool and parameters for the provided keeper. For each
// validator in data, it sets that validator in the keeper along with manually
// setting the indexes. In addition, it also sets any delegations found in
// data. Finally, it updates the bonded validators.
// Returns final validator set after applying all declaration and delegations
func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) (res []abci.ValidatorUpdate, err error) {

Expand All @@ -26,14 +25,9 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) (res [

keeper.SetPool(ctx, data.Pool)
keeper.SetParams(ctx, data.Params)
keeper.SetIntraTxCounter(ctx, data.IntraTxCounter)
keeper.SetLastTotalPower(ctx, data.LastTotalPower)

for i, validator := range data.Validators {
// set the intra-tx counter to the order the validators are presented, if necessary
if !data.Exported {
validator.BondIntraTxCounter = int16(i)
}
for _, validator := range data.Validators {
keeper.SetValidator(ctx, validator)

// Manually set indices for the first time
Expand Down Expand Up @@ -93,7 +87,6 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) (res [
func ExportGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState {
pool := keeper.GetPool(ctx)
params := keeper.GetParams(ctx)
intraTxCounter := keeper.GetIntraTxCounter(ctx)
lastTotalPower := keeper.GetLastTotalPower(ctx)
validators := keeper.GetAllValidators(ctx)
bonds := keeper.GetAllDelegations(ctx)
Expand All @@ -116,7 +109,6 @@ func ExportGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState {
return types.GenesisState{
Pool: pool,
Params: params,
IntraTxCounter: intraTxCounter,
LastTotalPower: lastTotalPower,
LastValidatorPowers: lastValidatorPowers,
Validators: validators,
Expand Down
2 changes: 0 additions & 2 deletions x/stake/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,10 @@ func TestInitGenesis(t *testing.T) {
resVal, found := keeper.GetValidator(ctx, sdk.ValAddress(keep.Addrs[0]))
require.True(t, found)
require.Equal(t, sdk.Bonded, resVal.Status)
require.Equal(t, int16(0), resVal.BondIntraTxCounter)

resVal, found = keeper.GetValidator(ctx, sdk.ValAddress(keep.Addrs[1]))
require.True(t, found)
require.Equal(t, sdk.Bonded, resVal.Status)
require.Equal(t, int16(1), resVal.BondIntraTxCounter)

abcivals := make([]abci.ValidatorUpdate, len(vals))
for i, val := range validators {
Expand Down
3 changes: 0 additions & 3 deletions x/stake/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ func NewHandler(k keeper.Keeper) sdk.Handler {

// Called every block, update validator set
func EndBlocker(ctx sdk.Context, k keeper.Keeper) (validatorUpdates []abci.ValidatorUpdate, endBlockerTags sdk.Tags) {
// Reset the intra-transaction counter.
k.SetIntraTxCounter(ctx, 0)

// Calculate validator set changes.
//
// NOTE: ApplyAndReturnValidatorSetUpdates has to come before
Expand Down
2 changes: 1 addition & 1 deletion x/stake/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ func (k Keeper) CompleteUnbonding(ctx sdk.Context, delAddr sdk.AccAddress, valAd
return nil
}

// complete unbonding an unbonding record
// begin unbonding / redelegation; create a redelegation record
func (k Keeper) BeginRedelegation(ctx sdk.Context, delAddr sdk.AccAddress,
valSrcAddr, valDstAddr sdk.ValAddress, sharesAmount sdk.Dec) (types.Redelegation, sdk.Error) {

Expand Down
48 changes: 22 additions & 26 deletions x/stake/keeper/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ func TestDelegation(t *testing.T) {
}

keeper.SetPool(ctx, pool)
validators[0] = TestingUpdateValidator(keeper, ctx, validators[0])
validators[1] = TestingUpdateValidator(keeper, ctx, validators[1])
validators[2] = TestingUpdateValidator(keeper, ctx, validators[2])
validators[0] = TestingUpdateValidator(keeper, ctx, validators[0], true)
validators[1] = TestingUpdateValidator(keeper, ctx, validators[1], true)
validators[2] = TestingUpdateValidator(keeper, ctx, validators[2], true)

// first add a validators[0] to delegate too

Expand Down Expand Up @@ -188,7 +188,7 @@ func TestUnbondDelegation(t *testing.T) {
validator, pool, issuedShares := validator.AddTokensFromDel(pool, sdk.NewInt(10))
require.Equal(t, int64(10), issuedShares.RoundInt64())
keeper.SetPool(ctx, pool)
validator = TestingUpdateValidator(keeper, ctx, validator)
validator = TestingUpdateValidator(keeper, ctx, validator, true)

pool = keeper.GetPool(ctx)
require.Equal(t, int64(10), pool.BondedTokens.RoundInt64())
Expand Down Expand Up @@ -230,7 +230,7 @@ func TestUndelegateSelfDelegation(t *testing.T) {
validator, pool, issuedShares := validator.AddTokensFromDel(pool, sdk.NewInt(10))
require.Equal(t, int64(10), issuedShares.RoundInt64())
keeper.SetPool(ctx, pool)
validator = TestingUpdateValidator(keeper, ctx, validator)
validator = TestingUpdateValidator(keeper, ctx, validator, true)
pool = keeper.GetPool(ctx)
selfDelegation := types.Delegation{
DelegatorAddr: sdk.AccAddress(addrVals[0].Bytes()),
Expand All @@ -244,7 +244,7 @@ func TestUndelegateSelfDelegation(t *testing.T) {
validator, pool, issuedShares = validator.AddTokensFromDel(pool, sdk.NewInt(10))
require.Equal(t, int64(10), issuedShares.RoundInt64())
keeper.SetPool(ctx, pool)
validator = TestingUpdateValidator(keeper, ctx, validator)
validator = TestingUpdateValidator(keeper, ctx, validator, true)
pool = keeper.GetPool(ctx)
delegation := types.Delegation{
DelegatorAddr: addrDels[0],
Expand Down Expand Up @@ -278,7 +278,7 @@ func TestUndelegateFromUnbondingValidator(t *testing.T) {
validator, pool, issuedShares := validator.AddTokensFromDel(pool, sdk.NewInt(10))
require.Equal(t, int64(10), issuedShares.RoundInt64())
keeper.SetPool(ctx, pool)
validator = TestingUpdateValidator(keeper, ctx, validator)
validator = TestingUpdateValidator(keeper, ctx, validator, true)
pool = keeper.GetPool(ctx)
selfDelegation := types.Delegation{
DelegatorAddr: sdk.AccAddress(addrVals[0].Bytes()),
Expand All @@ -292,7 +292,7 @@ func TestUndelegateFromUnbondingValidator(t *testing.T) {
validator, pool, issuedShares = validator.AddTokensFromDel(pool, sdk.NewInt(10))
require.Equal(t, int64(10), issuedShares.RoundInt64())
keeper.SetPool(ctx, pool)
validator = TestingUpdateValidator(keeper, ctx, validator)
validator = TestingUpdateValidator(keeper, ctx, validator, true)
pool = keeper.GetPool(ctx)
delegation := types.Delegation{
DelegatorAddr: addrDels[0],
Expand Down Expand Up @@ -354,7 +354,7 @@ func TestUndelegateFromUnbondedValidator(t *testing.T) {
validator, pool, issuedShares := validator.AddTokensFromDel(pool, sdk.NewInt(10))
require.Equal(t, int64(10), issuedShares.RoundInt64())
keeper.SetPool(ctx, pool)
validator = TestingUpdateValidator(keeper, ctx, validator)
validator = TestingUpdateValidator(keeper, ctx, validator, true)
pool = keeper.GetPool(ctx)
val0AccAddr := sdk.AccAddress(addrVals[0].Bytes())
selfDelegation := types.Delegation{
Expand All @@ -369,7 +369,7 @@ func TestUndelegateFromUnbondedValidator(t *testing.T) {
validator, pool, issuedShares = validator.AddTokensFromDel(pool, sdk.NewInt(10))
require.Equal(t, int64(10), issuedShares.RoundInt64())
keeper.SetPool(ctx, pool)
validator = TestingUpdateValidator(keeper, ctx, validator)
validator = TestingUpdateValidator(keeper, ctx, validator, true)
pool = keeper.GetPool(ctx)
delegation := types.Delegation{
DelegatorAddr: addrDels[0],
Expand Down Expand Up @@ -433,7 +433,7 @@ func TestUnbondingAllDelegationFromValidator(t *testing.T) {
validator, pool, issuedShares := validator.AddTokensFromDel(pool, sdk.NewInt(10))
require.Equal(t, int64(10), issuedShares.RoundInt64())
keeper.SetPool(ctx, pool)
validator = TestingUpdateValidator(keeper, ctx, validator)
validator = TestingUpdateValidator(keeper, ctx, validator, true)
pool = keeper.GetPool(ctx)
val0AccAddr := sdk.AccAddress(addrVals[0].Bytes())
selfDelegation := types.Delegation{
Expand All @@ -448,7 +448,7 @@ func TestUnbondingAllDelegationFromValidator(t *testing.T) {
validator, pool, issuedShares = validator.AddTokensFromDel(pool, sdk.NewInt(10))
require.Equal(t, int64(10), issuedShares.RoundInt64())
keeper.SetPool(ctx, pool)
validator = TestingUpdateValidator(keeper, ctx, validator)
validator = TestingUpdateValidator(keeper, ctx, validator, true)
pool = keeper.GetPool(ctx)
delegation := types.Delegation{
DelegatorAddr: addrDels[0],
Expand Down Expand Up @@ -595,7 +595,7 @@ func TestRedelegateToSameValidator(t *testing.T) {
validator, pool, issuedShares := validator.AddTokensFromDel(pool, sdk.NewInt(10))
require.Equal(t, int64(10), issuedShares.RoundInt64())
keeper.SetPool(ctx, pool)
validator = TestingUpdateValidator(keeper, ctx, validator)
validator = TestingUpdateValidator(keeper, ctx, validator, true)
pool = keeper.GetPool(ctx)
val0AccAddr := sdk.AccAddress(addrVals[0].Bytes())
selfDelegation := types.Delegation{
Expand All @@ -621,7 +621,7 @@ func TestRedelegateSelfDelegation(t *testing.T) {
validator, pool, issuedShares := validator.AddTokensFromDel(pool, sdk.NewInt(10))
require.Equal(t, int64(10), issuedShares.RoundInt64())
keeper.SetPool(ctx, pool)
validator = TestingUpdateValidator(keeper, ctx, validator)
validator = TestingUpdateValidator(keeper, ctx, validator, true)
pool = keeper.GetPool(ctx)
val0AccAddr := sdk.AccAddress(addrVals[0].Bytes())
selfDelegation := types.Delegation{
Expand All @@ -637,14 +637,14 @@ func TestRedelegateSelfDelegation(t *testing.T) {
require.Equal(t, int64(10), issuedShares.RoundInt64())
pool.BondedTokens = pool.BondedTokens.Add(sdk.NewDec(10))
keeper.SetPool(ctx, pool)
validator2 = TestingUpdateValidator(keeper, ctx, validator2)
validator2 = TestingUpdateValidator(keeper, ctx, validator2, true)
require.Equal(t, sdk.Bonded, validator2.Status)

// create a second delegation to this validator
validator, pool, issuedShares = validator.AddTokensFromDel(pool, sdk.NewInt(10))
require.Equal(t, int64(10), issuedShares.RoundInt64())
keeper.SetPool(ctx, pool)
validator = TestingUpdateValidator(keeper, ctx, validator)
validator = TestingUpdateValidator(keeper, ctx, validator, true)
pool = keeper.GetPool(ctx)
delegation := types.Delegation{
DelegatorAddr: addrDels[0],
Expand Down Expand Up @@ -673,12 +673,11 @@ func TestRedelegateFromUnbondingValidator(t *testing.T) {

//create a validator with a self-delegation
validator := types.NewValidator(addrVals[0], PKs[0], types.Description{})
validator.BondIntraTxCounter = 1

validator, pool, issuedShares := validator.AddTokensFromDel(pool, sdk.NewInt(10))
require.Equal(t, int64(10), issuedShares.RoundInt64())
keeper.SetPool(ctx, pool)
validator = TestingUpdateValidator(keeper, ctx, validator)
validator = TestingUpdateValidator(keeper, ctx, validator, true)
pool = keeper.GetPool(ctx)
val0AccAddr := sdk.AccAddress(addrVals[0].Bytes())
selfDelegation := types.Delegation{
Expand All @@ -693,7 +692,7 @@ func TestRedelegateFromUnbondingValidator(t *testing.T) {
validator, pool, issuedShares = validator.AddTokensFromDel(pool, sdk.NewInt(10))
require.Equal(t, int64(10), issuedShares.RoundInt64())
keeper.SetPool(ctx, pool)
validator = TestingUpdateValidator(keeper, ctx, validator)
validator = TestingUpdateValidator(keeper, ctx, validator, true)
pool = keeper.GetPool(ctx)
delegation := types.Delegation{
DelegatorAddr: addrDels[0],
Expand All @@ -704,11 +703,10 @@ func TestRedelegateFromUnbondingValidator(t *testing.T) {

// create a second validator
validator2 := types.NewValidator(addrVals[1], PKs[1], types.Description{})
validator2.BondIntraTxCounter = 2
validator2, pool, issuedShares = validator2.AddTokensFromDel(pool, sdk.NewInt(10))
require.Equal(t, int64(10), issuedShares.RoundInt64())
keeper.SetPool(ctx, pool)
validator2 = TestingUpdateValidator(keeper, ctx, validator2)
validator2 = TestingUpdateValidator(keeper, ctx, validator2, true)

header := ctx.BlockHeader()
blockHeight := int64(10)
Expand Down Expand Up @@ -762,7 +760,7 @@ func TestRedelegateFromUnbondedValidator(t *testing.T) {
validator, pool, issuedShares := validator.AddTokensFromDel(pool, sdk.NewInt(10))
require.Equal(t, int64(10), issuedShares.RoundInt64())
keeper.SetPool(ctx, pool)
validator = TestingUpdateValidator(keeper, ctx, validator)
validator = TestingUpdateValidator(keeper, ctx, validator, true)
pool = keeper.GetPool(ctx)
val0AccAddr := sdk.AccAddress(addrVals[0].Bytes())
selfDelegation := types.Delegation{
Expand All @@ -775,10 +773,9 @@ func TestRedelegateFromUnbondedValidator(t *testing.T) {
// create a second delegation to this validator
keeper.DeleteValidatorByPowerIndex(ctx, validator)
validator, pool, issuedShares = validator.AddTokensFromDel(pool, sdk.NewInt(10))
validator.BondIntraTxCounter = 1
require.Equal(t, int64(10), issuedShares.RoundInt64())
keeper.SetPool(ctx, pool)
validator = TestingUpdateValidator(keeper, ctx, validator)
validator = TestingUpdateValidator(keeper, ctx, validator, true)
pool = keeper.GetPool(ctx)
delegation := types.Delegation{
DelegatorAddr: addrDels[0],
Expand All @@ -789,11 +786,10 @@ func TestRedelegateFromUnbondedValidator(t *testing.T) {

// create a second validator
validator2 := types.NewValidator(addrVals[1], PKs[1], types.Description{})
validator2.BondIntraTxCounter = 2
validator2, pool, issuedShares = validator2.AddTokensFromDel(pool, sdk.NewInt(10))
require.Equal(t, int64(10), issuedShares.RoundInt64())
keeper.SetPool(ctx, pool)
validator2 = TestingUpdateValidator(keeper, ctx, validator2)
validator2 = TestingUpdateValidator(keeper, ctx, validator2, true)
require.Equal(t, sdk.Bonded, validator2.Status)

ctx = ctx.WithBlockHeight(10)
Expand Down
21 changes: 0 additions & 21 deletions x/stake/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,24 +132,3 @@ func (k Keeper) DeleteLastValidatorPower(ctx sdk.Context, operator sdk.ValAddres
store := ctx.KVStore(k.storeKey)
store.Delete(GetLastValidatorPowerKey(operator))
}

//__________________________________________________________________________

// get the current in-block validator operation counter
func (k Keeper) GetIntraTxCounter(ctx sdk.Context) int16 {
store := ctx.KVStore(k.storeKey)
b := store.Get(IntraTxCounterKey)
if b == nil {
return 0
}
var counter int16
k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &counter)
return counter
}

// set the current in-block validator operation counter
func (k Keeper) SetIntraTxCounter(ctx sdk.Context, counter int16) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinaryLengthPrefixed(counter)
store.Set(IntraTxCounterKey, bz)
}
Loading

0 comments on commit 133134c

Please sign in to comment.