Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R4R: use address instead of bond height / intratxcounter for deduplication #864

Merged
merged 1 commit into from
Dec 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion app/v0/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ func (p *ProtocolVersion0) prepForZeroHeightGenesis(ctx sdk.Context) {
panic("expected validator, not found")
}
validator.BondHeight = 0
validator.BondIntraTxCounter = counter
validator.UnbondingHeight = 0
p.StakeKeeper.SetValidator(ctx, validator)
counter++
Expand Down
23 changes: 11 additions & 12 deletions client/stake/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,17 @@ func ConvertValidatorToValidatorOutput(cliCtx context.CLIContext, v stake.Valida
UpdateTime: v.Commission.UpdateTime,
}
return ValidatorOutput{
OperatorAddr: v.OperatorAddr,
ConsPubKey: bechValPubkey,
Jailed: v.Jailed,
Status: v.Status,
Tokens: utils.ConvertDecToRat(v.Tokens).Mul(exRate).FloatString(),
DelegatorShares: utils.ConvertDecToRat(v.DelegatorShares).Mul(exRate).FloatString(),
Description: v.Description,
BondHeight: v.UnbondingHeight,
BondIntraTxCounter: v.BondIntraTxCounter,
UnbondingHeight: v.UnbondingHeight,
UnbondingMinTime: v.UnbondingMinTime,
Commission: commission,
OperatorAddr: v.OperatorAddr,
ConsPubKey: bechValPubkey,
Jailed: v.Jailed,
Status: v.Status,
Tokens: utils.ConvertDecToRat(v.Tokens).Mul(exRate).FloatString(),
DelegatorShares: utils.ConvertDecToRat(v.DelegatorShares).Mul(exRate).FloatString(),
Description: v.Description,
BondHeight: v.UnbondingHeight,
UnbondingHeight: v.UnbondingHeight,
UnbondingMinTime: v.UnbondingMinTime,
Commission: commission,
}
}

Expand Down
4 changes: 2 additions & 2 deletions modules/slashing/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,13 +416,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.NewIntWithDecimal(2, 18)))
got = sh(ctx, newTestMsgDelegate(sdk.AccAddress(addrs[2]), addrs[0], sdk.NewIntWithDecimal(3, 18)))
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
19 changes: 5 additions & 14 deletions modules/stake/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ import (
"github.com/irisnet/irishub/modules/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.
// Returns final validator set after applying all declaration and delegations
// 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.
func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) (res []abci.ValidatorUpdate, err error) {

// We need to pretend to be "n blocks before genesis", where "n" is the validator update delay,
Expand All @@ -26,14 +24,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 +86,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 @@ -117,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 modules/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
4 changes: 0 additions & 4 deletions modules/stake/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +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.EmptyTags()

// 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 modules/stake/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,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 modules/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.NewIntWithDecimal(10, 18))
require.Equal(t, sdk.NewDecFromInt(sdk.NewIntWithDecimal(10, 18)), issuedShares)
keeper.SetPool(ctx, pool)
validator = TestingUpdateValidator(keeper, ctx, validator)
validator = TestingUpdateValidator(keeper, ctx, validator, true)

pool = keeper.GetPool(ctx)
require.Equal(t, sdk.NewDecFromInt(sdk.NewIntWithDecimal(10, 18)), pool.BondedTokens)
Expand Down Expand Up @@ -230,7 +230,7 @@ func TestUndelegateSelfDelegation(t *testing.T) {
validator, pool, issuedShares := validator.AddTokensFromDel(pool, sdk.NewIntWithDecimal(10, 18))
require.Equal(t, sdk.NewDecFromInt(sdk.NewIntWithDecimal(10, 18)), issuedShares)
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.NewIntWithDecimal(10, 18))
require.Equal(t, sdk.NewDecFromInt(sdk.NewIntWithDecimal(10, 18)), issuedShares)
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.NewIntWithDecimal(10, 18))
require.Equal(t, sdk.NewDecFromInt(sdk.NewIntWithDecimal(10, 18)), issuedShares)
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.NewIntWithDecimal(10, 18))
require.Equal(t, sdk.NewDecFromInt(sdk.NewIntWithDecimal(10, 18)), issuedShares)
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.NewIntWithDecimal(10, 18))
require.Equal(t, sdk.NewDecFromInt(sdk.NewIntWithDecimal(10, 18)), issuedShares)
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.NewIntWithDecimal(10, 18))
require.Equal(t, sdk.NewDecFromInt(sdk.NewIntWithDecimal(10, 18)), issuedShares)
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.NewIntWithDecimal(10, 18))
require.Equal(t, sdk.NewDecFromInt(sdk.NewIntWithDecimal(10, 18)), issuedShares)
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.NewIntWithDecimal(10, 18))
require.Equal(t, sdk.NewDecFromInt(sdk.NewIntWithDecimal(10, 18)), issuedShares)
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.NewIntWithDecimal(10, 18))
require.Equal(t, sdk.NewDecFromInt(sdk.NewIntWithDecimal(10, 18)), issuedShares)
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, sdk.NewDecFromInt(sdk.NewIntWithDecimal(10, 18)), issuedShares)
pool.BondedTokens = pool.BondedTokens.Add(sdk.NewDecFromInt(sdk.NewIntWithDecimal(10, 18)))
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.NewIntWithDecimal(10, 18))
require.Equal(t, sdk.NewDecFromInt(sdk.NewIntWithDecimal(10, 18)), issuedShares)
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.NewIntWithDecimal(10, 18))
require.Equal(t, sdk.NewDecFromInt(sdk.NewIntWithDecimal(10, 18)), issuedShares)
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.NewIntWithDecimal(10, 18))
require.Equal(t, sdk.NewDecFromInt(sdk.NewIntWithDecimal(10, 18)), issuedShares)
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.NewIntWithDecimal(10, 18))
require.Equal(t, sdk.NewDecFromInt(sdk.NewIntWithDecimal(10, 18)), issuedShares)
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.NewIntWithDecimal(10, 18))
require.Equal(t, sdk.NewDecFromInt(sdk.NewIntWithDecimal(10, 18)), issuedShares)
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, pool)
validator, pool, issuedShares = validator.AddTokensFromDel(pool, sdk.NewIntWithDecimal(10, 18))
validator.BondIntraTxCounter = 1
require.Equal(t, sdk.NewDecFromInt(sdk.NewIntWithDecimal(10, 18)), issuedShares)
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.NewIntWithDecimal(10, 18))
require.Equal(t, sdk.NewDecFromInt(sdk.NewIntWithDecimal(10, 18)), issuedShares)
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 modules/stake/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,24 +143,3 @@ func (k Keeper) BurnAmount(ctx sdk.Context, amount sdk.Dec) {
func (k Keeper) GetStakeDenom(ctx sdk.Context) string {
return types.StakeDenom
}

//__________________________________________________________________________

// 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