From eb01cb4a35bb557bc7308e11ccaf99709d7463d0 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Thu, 4 Oct 2018 18:29:03 -0400 Subject: [PATCH] checkout distr types PR updates --- x/distribution/types/codec.go | 2 +- x/distribution/types/dec_coin.go | 20 +++++++++++----- x/distribution/types/dec_coin_test.go | 26 ++++++++------------- x/distribution/types/delegator_info.go | 12 ++-------- x/distribution/types/delegator_info_test.go | 2 +- x/distribution/types/fee_pool.go | 12 ++++++---- x/distribution/types/fee_pool_test.go | 6 ++--- x/distribution/types/genesis.go | 9 +++++++ x/distribution/types/msg.go | 2 +- x/distribution/types/validator_info.go | 4 ++-- x/distribution/types/validator_info_test.go | 4 ++-- 11 files changed, 53 insertions(+), 46 deletions(-) diff --git a/x/distribution/types/codec.go b/x/distribution/types/codec.go index 55d7c6cda..10b2dc2fb 100644 --- a/x/distribution/types/codec.go +++ b/x/distribution/types/codec.go @@ -12,7 +12,7 @@ func RegisterCodec(cdc *codec.Codec) { cdc.RegisterConcrete(MsgSetWithdrawAddress{}, "cosmos-sdk/MsgModifyWithdrawAddress", nil) } -// generic sealed codec to be used throught module +// generic sealed codec to be used throughout module var MsgCdc *codec.Codec func init() { diff --git a/x/distribution/types/dec_coin.go b/x/distribution/types/dec_coin.go index df94d864d..59373976d 100644 --- a/x/distribution/types/dec_coin.go +++ b/x/distribution/types/dec_coin.go @@ -1,6 +1,7 @@ package types import ( + "fmt" "strings" sdk "github.com/cosmos/cosmos-sdk/types" @@ -12,7 +13,14 @@ type DecCoin struct { Amount sdk.Dec `json:"amount"` } -func NewDecCoin(coin sdk.Coin) DecCoin { +func NewDecCoin(denom string, amount int64) DecCoin { + return DecCoin{ + Denom: denom, + Amount: sdk.NewDec(amount), + } +} + +func NewDecCoinFromCoin(coin sdk.Coin) DecCoin { return DecCoin{ Denom: coin.Denom, Amount: sdk.NewDecFromInt(coin.Amount), @@ -21,16 +29,16 @@ func NewDecCoin(coin sdk.Coin) DecCoin { // Adds amounts of two coins with same denom func (coin DecCoin) Plus(coinB DecCoin) DecCoin { - if !(coin.Denom == coinB.Denom) { - return coin + if coin.Denom != coinB.Denom { + panic(fmt.Sprintf("coin denom different: %v %v\n", coin.Denom, coinB.Denom)) } return DecCoin{coin.Denom, coin.Amount.Add(coinB.Amount)} } // Subtracts amounts of two coins with same denom func (coin DecCoin) Minus(coinB DecCoin) DecCoin { - if !(coin.Denom == coinB.Denom) { - return coin + if coin.Denom != coinB.Denom { + panic(fmt.Sprintf("coin denom different: %v %v\n", coin.Denom, coinB.Denom)) } return DecCoin{coin.Denom, coin.Amount.Sub(coinB.Amount)} } @@ -48,7 +56,7 @@ type DecCoins []DecCoin func NewDecCoins(coins sdk.Coins) DecCoins { dcs := make(DecCoins, len(coins)) for i, coin := range coins { - dcs[i] = NewDecCoin(coin) + dcs[i] = NewDecCoinFromCoin(coin) } return dcs } diff --git a/x/distribution/types/dec_coin_test.go b/x/distribution/types/dec_coin_test.go index 61de361ca..5a326fa17 100644 --- a/x/distribution/types/dec_coin_test.go +++ b/x/distribution/types/dec_coin_test.go @@ -4,6 +4,7 @@ import ( "testing" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -11,23 +12,16 @@ func TestPlusDecCoin(t *testing.T) { decCoinA1 := DecCoin{"A", sdk.NewDecWithPrec(11, 1)} decCoinA2 := DecCoin{"A", sdk.NewDecWithPrec(22, 1)} decCoinB1 := DecCoin{"B", sdk.NewDecWithPrec(11, 1)} - decCoinC1 := DecCoin{"C", sdk.NewDecWithPrec(11, 1)} - decCoinCn4 := DecCoin{"C", sdk.NewDecWithPrec(-44, 1)} - decCoinC5 := DecCoin{"C", sdk.NewDecWithPrec(55, 1)} - cases := []struct { - inputOne DecCoin - inputTwo DecCoin - expected DecCoin - }{ - {decCoinA1, decCoinA1, decCoinA2}, - {decCoinA1, decCoinB1, decCoinA1}, - {decCoinCn4, decCoinC5, decCoinC1}, - } - for tcIndex, tc := range cases { - res := tc.inputOne.Plus(tc.inputTwo) - require.Equal(t, tc.expected, res, "sum of coins is incorrect, tc #%d", tcIndex) - } + // regular add + res := decCoinA1.Plus(decCoinA1) + require.Equal(t, decCoinA2, res, "sum of coins is incorrect") + + // bad denom add + assert.Panics(t, func() { + decCoinA1.Plus(decCoinB1) + }, "expected panic on sum of different denoms") + } func TestPlusCoins(t *testing.T) { diff --git a/x/distribution/types/delegator_info.go b/x/distribution/types/delegator_info.go index 1a1bd4d9b..4de296890 100644 --- a/x/distribution/types/delegator_info.go +++ b/x/distribution/types/delegator_info.go @@ -4,7 +4,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -// distribution info for a delegation +// distribution info for a delegation - used to determine entitled rewards type DelegatorDistInfo struct { DelegatorAddr sdk.AccAddress `json:"delegator_addr"` ValOperatorAddr sdk.ValAddress `json:"val_operator_addr"` @@ -36,7 +36,7 @@ func (di DelegatorDistInfo) WithdrawRewards(fp FeePool, vi ValidatorDistInfo, blocks := height - di.WithdrawalHeight di.WithdrawalHeight = height - accum := delegatorShares.Mul(sdk.NewDec(blocks)) + accum := delegatorShares.MulInt(sdk.NewInt(blocks)) withdrawalTokens := vi.Pool.MulDec(accum).QuoDec(vi.DelAccum.Accum) remainingTokens := vi.Pool.Minus(withdrawalTokens) @@ -45,11 +45,3 @@ func (di DelegatorDistInfo) WithdrawRewards(fp FeePool, vi ValidatorDistInfo, return di, vi, fp, withdrawalTokens } - -//_____________________________________________________________________ - -// withdraw address for the delegation rewards -type DelegatorWithdrawInfo struct { - DelegatorAddr sdk.AccAddress `json:"delegator_addr"` - WithdrawAddr sdk.AccAddress `json:"withdraw_addr"` -} diff --git a/x/distribution/types/delegator_info_test.go b/x/distribution/types/delegator_info_test.go index 03803f8cd..2d9e9bc0f 100644 --- a/x/distribution/types/delegator_info_test.go +++ b/x/distribution/types/delegator_info_test.go @@ -26,7 +26,7 @@ func TestWithdrawRewards(t *testing.T) { // simulate adding some stake for inflation height = 10 - fp.Pool = DecCoins{DecCoin{"stake", sdk.NewDec(1000)}} + fp.Pool = DecCoins{NewDecCoin("stake", 1000)} // withdraw rewards di1, vi, fp, rewardRecv1 := di1.WithdrawRewards(fp, vi, height, totalBondedTokens, diff --git a/x/distribution/types/fee_pool.go b/x/distribution/types/fee_pool.go index d373e0485..66731cb19 100644 --- a/x/distribution/types/fee_pool.go +++ b/x/distribution/types/fee_pool.go @@ -17,10 +17,14 @@ func NewTotalAccum(height int64) TotalAccum { } } -// update total validator accumulation factor -func (ta TotalAccum) Update(height int64, accumCreatedPerBlock sdk.Dec) TotalAccum { +// update total validator accumulation factor for the new height +// CONTRACT: height should be greater than the old height +func (ta TotalAccum) UpdateForNewHeight(height int64, accumCreatedPerBlock sdk.Dec) TotalAccum { blocks := height - ta.UpdateHeight - ta.Accum = ta.Accum.Add(accumCreatedPerBlock.Mul(sdk.NewDec(blocks))) + if blocks < 0 { + panic("reverse updated for new height") + } + ta.Accum = ta.Accum.Add(accumCreatedPerBlock.MulInt(sdk.NewInt(blocks))) ta.UpdateHeight = height return ta } @@ -36,7 +40,7 @@ type FeePool struct { // update total validator accumulation factor func (f FeePool) UpdateTotalValAccum(height int64, totalBondedTokens sdk.Dec) FeePool { - f.ValAccum = f.ValAccum.Update(height, totalBondedTokens) + f.ValAccum = f.ValAccum.UpdateForNewHeight(height, totalBondedTokens) return f } diff --git a/x/distribution/types/fee_pool_test.go b/x/distribution/types/fee_pool_test.go index 1c2a804b4..e39fb09c9 100644 --- a/x/distribution/types/fee_pool_test.go +++ b/x/distribution/types/fee_pool_test.go @@ -7,14 +7,14 @@ import ( "github.com/stretchr/testify/require" ) -func TestTotalAccumUpdate(t *testing.T) { +func TestTotalAccumUpdateForNewHeight(t *testing.T) { ta := NewTotalAccum(0) - ta = ta.Update(5, sdk.NewDec(3)) + ta = ta.UpdateForNewHeight(5, sdk.NewDec(3)) require.True(sdk.DecEq(t, sdk.NewDec(15), ta.Accum)) - ta = ta.Update(8, sdk.NewDec(2)) + ta = ta.UpdateForNewHeight(8, sdk.NewDec(2)) require.True(sdk.DecEq(t, sdk.NewDec(21), ta.Accum)) } diff --git a/x/distribution/types/genesis.go b/x/distribution/types/genesis.go index a42f70594..f01d1b781 100644 --- a/x/distribution/types/genesis.go +++ b/x/distribution/types/genesis.go @@ -1,5 +1,14 @@ package types +import sdk "github.com/cosmos/cosmos-sdk/types" + +// the address for where distributions rewards are withdrawn to by default +// this struct is only used at genesis to feed in default withdraw addresses +type DelegatorWithdrawInfo struct { + DelegatorAddr sdk.AccAddress `json:"delegator_addr"` + WithdrawAddr sdk.AccAddress `json:"withdraw_addr"` +} + // GenesisState - all distribution state that must be provided at genesis type GenesisState struct { FeePool FeePool `json:"fee_pool"` diff --git a/x/distribution/types/msg.go b/x/distribution/types/msg.go index a98892ee9..f00dceae1 100644 --- a/x/distribution/types/msg.go +++ b/x/distribution/types/msg.go @@ -28,7 +28,7 @@ func NewMsgSetWithdrawAddress(delAddr, withdrawAddr sdk.AccAddress) MsgSetWithdr } func (msg MsgSetWithdrawAddress) Type() string { return MsgType } -func (msg MsgSetWithdrawAddress) Name() string { return "withdraw_delegation_rewards_all" } +func (msg MsgSetWithdrawAddress) Name() string { return "set_withdraw_address" } // Return address that must sign over msg.GetSignBytes() func (msg MsgSetWithdrawAddress) GetSigners() []sdk.AccAddress { diff --git a/x/distribution/types/validator_info.go b/x/distribution/types/validator_info.go index 3092d1019..18aef8bde 100644 --- a/x/distribution/types/validator_info.go +++ b/x/distribution/types/validator_info.go @@ -27,7 +27,7 @@ func NewValidatorDistInfo(operatorAddr sdk.ValAddress, currentHeight int64) Vali // update total delegator accumululation func (vi ValidatorDistInfo) UpdateTotalDelAccum(height int64, totalDelShares sdk.Dec) ValidatorDistInfo { - vi.DelAccum = vi.DelAccum.Update(height, totalDelShares) + vi.DelAccum = vi.DelAccum.UpdateForNewHeight(height, totalDelShares) return vi } @@ -44,7 +44,7 @@ func (vi ValidatorDistInfo) TakeFeePoolRewards(fp FeePool, height int64, totalBo // update the validators pool blocks := height - vi.FeePoolWithdrawalHeight vi.FeePoolWithdrawalHeight = height - accum := sdk.NewDec(blocks).Mul(vdTokens) + accum := vdTokens.MulInt(sdk.NewInt(blocks)) withdrawalTokens := fp.Pool.MulDec(accum).QuoDec(fp.ValAccum.Accum) remainingTokens := fp.Pool.Minus(withdrawalTokens) diff --git a/x/distribution/types/validator_info_test.go b/x/distribution/types/validator_info_test.go index da3b2c10d..afa6d8c76 100644 --- a/x/distribution/types/validator_info_test.go +++ b/x/distribution/types/validator_info_test.go @@ -26,7 +26,7 @@ func TestTakeFeePoolRewards(t *testing.T) { // simulate adding some stake for inflation height = 10 - fp.Pool = DecCoins{DecCoin{"stake", sdk.NewDec(1000)}} + fp.Pool = DecCoins{NewDecCoin("stake", 1000)} vi1, fp = vi1.TakeFeePoolRewards(fp, height, totalBondedTokens, validatorTokens1, commissionRate1) require.True(sdk.DecEq(t, sdk.NewDec(900), fp.ValAccum.Accum)) @@ -63,7 +63,7 @@ func TestWithdrawCommission(t *testing.T) { // simulate adding some stake for inflation height = 10 - fp.Pool = DecCoins{DecCoin{"stake", sdk.NewDec(1000)}} + fp.Pool = DecCoins{NewDecCoin("stake", 1000)} // for a more fun staring condition, have an non-withdraw update vi, fp = vi.TakeFeePoolRewards(fp, height, totalBondedTokens, validatorTokens, commissionRate)