From d9396ed7324378fc7555137cf1ecf010e669c702 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Wed, 19 Sep 2018 19:33:12 -0400 Subject: [PATCH] working compile errors --- types/stake.go | 2 +- x/distribution/types/dec_coin.go | 2 +- x/distribution/types/delegator_info.go | 8 ++++---- x/distribution/types/fee_pool.go | 4 ++-- x/distribution/types/validator_info.go | 17 +++++++++-------- x/stake/genesis.go | 2 +- x/stake/handler.go | 16 ++++++++-------- x/stake/keeper/delegation.go | 18 ------------------ x/stake/keeper/sdk_types.go | 18 ++++++++++++++++++ x/stake/keeper/slash.go | 4 ++-- x/stake/keeper/validator.go | 11 +++++------ x/stake/stake.go | 4 ++-- 12 files changed, 53 insertions(+), 53 deletions(-) diff --git a/types/stake.go b/types/stake.go index 68257689d2..6012e4643a 100644 --- a/types/stake.go +++ b/types/stake.go @@ -68,7 +68,7 @@ type ValidatorSet interface { func(index int64, validator Validator) (stop bool)) Validator(Context, ValAddress) Validator // get a particular validator by operator - ValidatorByPubKey(Context, crypto.PubKey) Validator // get a particular validator by signing PubKey + ValidatorByConsAddr(Context, ConsAddress) Validator // get a particular validator by consensus address TotalPower(Context) Dec // total power of the validator set // slash the validator and delegators of the validator, specifying offence height, offence power, and slash fraction diff --git a/x/distribution/types/dec_coin.go b/x/distribution/types/dec_coin.go index f014e3615d..eef4f78a63 100644 --- a/x/distribution/types/dec_coin.go +++ b/x/distribution/types/dec_coin.go @@ -93,7 +93,7 @@ func (coins DecCoins) Plus(coinsB DecCoins) DecCoins { func (coins DecCoins) Mul(multiple sdk.Dec) DecCoins { products := make([]DecCoin, len(coins)) for i, coin := range coins { - product := DecCoins{ + product := DecCoin{ Denom: coin.Denom, Amount: coin.Amount.Mul(multiple), } diff --git a/x/distribution/types/delegator_info.go b/x/distribution/types/delegator_info.go index fc3dca729a..eeb5af11d4 100644 --- a/x/distribution/types/delegator_info.go +++ b/x/distribution/types/delegator_info.go @@ -15,15 +15,15 @@ func (di DelegatorDistInfo) WithdrawRewards(fp FeePool, vi ValidatorDistInfo, commissionRate sdk.Dec) (DelegatorDistInfo, FeePool, DecCoins) { vi.UpdateTotalDelAccum(height, totalDelShares) - fp = vi.TakeFeePoolRewards(fp, height, totalBonded, vdTokens, commissionRate) + vi, fp = vi.TakeFeePoolRewards(fp, height, totalBonded, vdTokens, commissionRate) - blocks = height - di.WithdrawalHeight + blocks := height - di.WithdrawalHeight di.WithdrawalHeight = height accum := delegatorShares.Mul(sdk.NewDec(blocks)) - withdrawalTokens := vi.Pool.Mul(accum.Quo(vi.TotalDelAccum)) + withdrawalTokens := vi.Pool.Mul(accum.Quo(vi.DelAccum.Accum)) vi.Pool = vi.Pool.Sub(withdrawalTokens) - vi.TotalDelAccum = vi.TotalDelAccum.sub(accum) + vi.DelAccum.Accum = vi.DelAccum.Accum.Sub(accum) return di, fp, withdrawalTokens } diff --git a/x/distribution/types/fee_pool.go b/x/distribution/types/fee_pool.go index 7c215fce2d..03725d7c1d 100644 --- a/x/distribution/types/fee_pool.go +++ b/x/distribution/types/fee_pool.go @@ -18,7 +18,7 @@ func NewTotalAccum(height int64) TotalAccum { // update total validator accumulation factor func (ta TotalAccum) Update(height int64, accumCreatedPerBlock sdk.Dec) TotalAccum { blocks := height - ta.UpdateHeight - f.Accum = f.Accum.Add(accumCreatedPerBlock.Mul(sdk.NewDec(blocks))) + ta.Accum = ta.Accum.Add(accumCreatedPerBlock.Mul(sdk.NewDec(blocks))) ta.UpdateHeight = height return ta } @@ -41,7 +41,7 @@ func (f FeePool) UpdateTotalValAccum(height int64, totalBondedTokens sdk.Dec) Fe // zero fee pool func InitialFeePool() FeePool { return FeePool{ - ValAccum: NewwTotalAccum(0), + ValAccum: NewTotalAccum(0), Pool: DecCoins{}, CommunityPool: DecCoins{}, } diff --git a/x/distribution/types/validator_info.go b/x/distribution/types/validator_info.go index 5f8fa686be..b2f2592ea3 100644 --- a/x/distribution/types/validator_info.go +++ b/x/distribution/types/validator_info.go @@ -24,19 +24,20 @@ func (vi ValidatorDistInfo) UpdateTotalDelAccum(height int64, totalDelShares sdk func (vi ValidatorDistInfo) TakeFeePoolRewards(fp FeePool, height int64, totalBonded, vdTokens, commissionRate sdk.Dec) (ValidatorDistInfo, FeePool) { - fp.UpdateTotalValAccum(height, totalBondedShares) + fp.UpdateTotalValAccum(height, totalBonded) // update the validators pool - blocks = height - vi.FeePoolWithdrawalHeight + blocks := height - vi.FeePoolWithdrawalHeight vi.FeePoolWithdrawalHeight = height - accum = sdk.NewDec(blocks).Mul(vdTokens) - withdrawalTokens := fp.Pool.Mul(accum).Quo(fp.TotalValAccum) + accum := sdk.NewDec(blocks).Mul(vdTokens) + withdrawalTokens := fp.Pool.Mul(accum.Quo(fp.ValAccum.Accum)) commission := withdrawalTokens.Mul(commissionRate) + afterCommission := withdrawalTokens.Sub(commission) - fp.TotalValAccum = fp.TotalValAccum.Sub(accum) + fp.ValAccum.Accum = fp.ValAccum.Accum.Sub(accum) fp.Pool = fp.Pool.Sub(withdrawalTokens) vi.PoolCommission = vi.PoolCommission.Add(commission) - vi.PoolCommissionFree = vi.PoolCommissionFree.Add(withdrawalTokens.Sub(commission)) + vi.Pool = vi.Pool.Add(afterCommission) return vi, fp } @@ -45,10 +46,10 @@ func (vi ValidatorDistInfo) TakeFeePoolRewards(fp FeePool, height int64, totalBo func (vi ValidatorDistInfo) WithdrawCommission(fp FeePool, height int64, totalBonded, vdTokens, commissionRate sdk.Dec) (vio ValidatorDistInfo, fpo FeePool, withdrawn DecCoins) { - fp = vi.TakeFeePoolRewards(fp, height, totalBonded, vdTokens, commissionRate) + vi, fp = vi.TakeFeePoolRewards(fp, height, totalBonded, vdTokens, commissionRate) withdrawalTokens := vi.PoolCommission - vi.PoolCommission = 0 + vi.PoolCommission = DecCoins{} // zero return vi, fp, withdrawalTokens } diff --git a/x/stake/genesis.go b/x/stake/genesis.go index 7a004bccd2..a7f8493081 100644 --- a/x/stake/genesis.go +++ b/x/stake/genesis.go @@ -32,7 +32,7 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) (res [ } // Manually set indexes for the first time - keeper.SetValidatorByPubKeyIndex(ctx, validator) + keeper.SetValidatorByConsAddr(ctx, validator) keeper.SetValidatorByPowerIndex(ctx, validator, data.Pool) if validator.Status == sdk.Bonded { diff --git a/x/stake/handler.go b/x/stake/handler.go index e1fb3edc02..2f563fdc0f 100644 --- a/x/stake/handler.go +++ b/x/stake/handler.go @@ -68,7 +68,7 @@ func handleMsgCreateValidator(ctx sdk.Context, msg types.MsgCreateValidator, k k if found { return ErrValidatorOwnerExists(k.Codespace()).Result() } - _, found = k.GetValidatorByPubKey(ctx, msg.PubKey) + _, found = k.GetValidatorByConsAddr(ctx, sdk.ConsAddress(msg.PubKey.Address())) if found { return ErrValidatorPubKeyExists(k.Codespace()).Result() } @@ -78,7 +78,7 @@ func handleMsgCreateValidator(ctx sdk.Context, msg types.MsgCreateValidator, k k validator := NewValidator(msg.ValidatorAddr, msg.PubKey, msg.Description) k.SetValidator(ctx, validator) - k.SetValidatorByPubKeyIndex(ctx, validator) + k.SetValidatorByConsAddr(ctx, validator) // move coins from the msg.Address account to a (self-delegation) delegator account // the validator account and global shares are updated within here @@ -88,10 +88,10 @@ func handleMsgCreateValidator(ctx sdk.Context, msg types.MsgCreateValidator, k k } // call the hook if present - if k.validatorHooks != nil { - k.validatorHooks.OnValidatorCreated(ctx, validator.OperatorAddr) - accAddr := sdk.AccAddress{validator.OperatorAddr} - k.validatorHooks.OnDelegationCreated(ctx, accAddr, validator.OperatorAddr) + if k.hooks != nil { + k.hooks.OnValidatorCreated(ctx, validator.OperatorAddr) + accAddr := sdk.AccAddress(validator.OperatorAddr) + k.hooks.OnDelegationCreated(ctx, accAddr, validator.OperatorAddr) } tags := sdk.NewTags( @@ -154,8 +154,8 @@ func handleMsgDelegate(ctx sdk.Context, msg types.MsgDelegate, k keeper.Keeper) } // call the hook if present - if k.validatorHooks != nil { - k.validatorHooks.OnDelegationCreated(ctx, msg.DelegatorAddr, validator.OperatorAddr) + if k.hooks != nil { + k.hooks.OnDelegationCreated(ctx, msg.DelegatorAddr, validator.OperatorAddr) } tags := sdk.NewTags( diff --git a/x/stake/keeper/delegation.go b/x/stake/keeper/delegation.go index 78e16de828..6ac51ac196 100644 --- a/x/stake/keeper/delegation.go +++ b/x/stake/keeper/delegation.go @@ -57,24 +57,6 @@ func (k Keeper) GetDelegatorDelegations(ctx sdk.Context, delegator sdk.AccAddres return delegations[:i] // trim if the array length < maxRetrieve } -// iterate through all of the delegations from a delegator -func (k Keeper) IterateDelegations(ctx sdk.Context, delAddr sdk.AccAddress, - fn func(index int64, del types.Delegation) (stop bool)) { - - store := ctx.KVStore(k.storeKey) - delegatorPrefixKey := GetDelegationsKey(delAddr) - iterator := sdk.KVStorePrefixIterator(store, delegatorPrefixKey) //smallest to largest - for i := int64(0); iterator.Valid(); iterator.Next() { - del := types.MustUnmarshalDelegation(k.cdc, iterator.Key(), iterator.Value()) - stop := fn(i, del) - if stop { - break - } - i++ - } - iterator.Close() -} - // set the delegation func (k Keeper) SetDelegation(ctx sdk.Context, delegation types.Delegation) { store := ctx.KVStore(k.storeKey) diff --git a/x/stake/keeper/sdk_types.go b/x/stake/keeper/sdk_types.go index 6297919392..d702e845da 100644 --- a/x/stake/keeper/sdk_types.go +++ b/x/stake/keeper/sdk_types.go @@ -92,3 +92,21 @@ func (k Keeper) Delegation(ctx sdk.Context, addrDel sdk.AccAddress, addrVal sdk. return bond } + +// iterate through all of the delegations from a delegator +func (k Keeper) IterateDelegations(ctx sdk.Context, delAddr sdk.AccAddress, + fn func(index int64, del sdk.Delegation) (stop bool)) { + + store := ctx.KVStore(k.storeKey) + delegatorPrefixKey := GetDelegationsKey(delAddr) + iterator := sdk.KVStorePrefixIterator(store, delegatorPrefixKey) //smallest to largest + for i := int64(0); iterator.Valid(); iterator.Next() { + del := types.MustUnmarshalDelegation(k.cdc, iterator.Key(), iterator.Value()) + stop := fn(i, del) + if stop { + break + } + i++ + } + iterator.Close() +} diff --git a/x/stake/keeper/slash.go b/x/stake/keeper/slash.go index 52dbd21c7e..e45c458f77 100644 --- a/x/stake/keeper/slash.go +++ b/x/stake/keeper/slash.go @@ -36,7 +36,7 @@ func (k Keeper) Slash(ctx sdk.Context, pubkey crypto.PubKey, infractionHeight in // ref https://github.com/cosmos/cosmos-sdk/issues/1348 // ref https://github.com/cosmos/cosmos-sdk/issues/1471 - validator, found := k.GetValidatorByPubKey(ctx, pubkey) + validator, found := k.GetValidatorByConsAddr(ctx, sdk.ConsAddress(pubkey.Address())) if !found { // If not found, the validator must have been overslashed and removed - so we don't need to do anything // NOTE: Correctness dependent on invariant that unbonding delegations / redelegations must also have been completely @@ -152,7 +152,7 @@ func (k Keeper) Unjail(ctx sdk.Context, pubkey crypto.PubKey) { // set the jailed flag on a validator func (k Keeper) setJailed(ctx sdk.Context, pubkey crypto.PubKey, isJailed bool) { - validator, found := k.GetValidatorByPubKey(ctx, pubkey) + validator, found := k.GetValidatorByConsAddr(ctx, sdk.ConsAddress(pubkey.Address())) if !found { panic(fmt.Errorf("validator with pubkey %s not found, cannot set jailed to %v", pubkey, isJailed)) } diff --git a/x/stake/keeper/validator.go b/x/stake/keeper/validator.go index d0dbcc9e45..766f3fc8f1 100644 --- a/x/stake/keeper/validator.go +++ b/x/stake/keeper/validator.go @@ -77,7 +77,7 @@ func (k Keeper) SetValidator(ctx sdk.Context, validator types.Validator) { // validator index func (k Keeper) SetValidatorByConsAddr(ctx sdk.Context, validator types.Validator) { store := ctx.KVStore(k.storeKey) - consAddr := sdk.ConsAddress{validator.OperatorAddr.Bytes()} + consAddr := sdk.ConsAddress(validator.OperatorAddr.Bytes()) store.Set(GetValidatorByConsAddrKey(consAddr), validator.OperatorAddr) } @@ -683,7 +683,7 @@ func (k Keeper) RemoveValidator(ctx sdk.Context, address sdk.ValAddress) { store := ctx.KVStore(k.storeKey) pool := k.GetPool(ctx) store.Delete(GetValidatorKey(address)) - store.Delete(GetValidatorByConsAddrKey(sdk.ConsAddr{validator.ConsPubKey.Address()})) + store.Delete(GetValidatorByConsAddrKey(sdk.ConsAddress(validator.ConsPubKey.Address()))) store.Delete(GetValidatorsByPowerIndexKey(validator, pool)) // delete from the current and power weighted validator groups if the validator @@ -738,14 +738,13 @@ func ensureValidatorFound(found bool, ownerAddr []byte) { // XXX remove this code - this is should be superceded by commission work that bez is doing // get a single validator func (k Keeper) UpdateValidatorCommission(ctx sdk.Context, addr sdk.ValAddress, newCommission sdk.Dec) sdk.Error { - store := ctx.KVStore(k.storeKey) // call the hook if present if k.hooks != nil { - k.hooks.OnValidatorCommissionChange(ctx, validator.OperatorAddr) + k.hooks.OnValidatorCommissionChange(ctx, addr) } - validator, found := k.GetValidator(addr) + validator, found := k.GetValidator(ctx, addr) // check for errors switch { @@ -763,6 +762,6 @@ func (k Keeper) UpdateValidatorCommission(ctx sdk.Context, addr sdk.ValAddress, validator.Commission = newCommission - k.SetValidator(addr, validator) + k.SetValidator(ctx, validator) return nil } diff --git a/x/stake/stake.go b/x/stake/stake.go index 18b99fd4a0..6b4036d281 100644 --- a/x/stake/stake.go +++ b/x/stake/stake.go @@ -35,7 +35,7 @@ var ( NewKeeper = keeper.NewKeeper GetValidatorKey = keeper.GetValidatorKey - GetValidatorByPubKeyIndexKey = keeper.GetValidatorByPubKeyIndexKey + GetValidatorByConsAddrKey = keeper.GetValidatorByConsAddrKey GetValidatorsBondedIndexKey = keeper.GetValidatorsBondedIndexKey GetValidatorsByPowerIndexKey = keeper.GetValidatorsByPowerIndexKey GetTendermintUpdatesTKey = keeper.GetTendermintUpdatesTKey @@ -44,7 +44,7 @@ var ( ParamKey = keeper.ParamKey PoolKey = keeper.PoolKey ValidatorsKey = keeper.ValidatorsKey - ValidatorsByPubKeyIndexKey = keeper.ValidatorsByPubKeyIndexKey + ValidatorsByConsAddrKey = keeper.ValidatorsByConsAddrKey ValidatorsBondedIndexKey = keeper.ValidatorsBondedIndexKey ValidatorsByPowerIndexKey = keeper.ValidatorsByPowerIndexKey ValidatorCliffIndexKey = keeper.ValidatorCliffIndexKey