checkout distr types PR updates
This commit is contained in:
parent
3a9102e2af
commit
eb01cb4a35
|
@ -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() {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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"`
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
||||
|
|
|
@ -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"`
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue