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)
|
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
|
var MsgCdc *codec.Codec
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package types
|
package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
@ -12,7 +13,14 @@ type DecCoin struct {
|
||||||
Amount sdk.Dec `json:"amount"`
|
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{
|
return DecCoin{
|
||||||
Denom: coin.Denom,
|
Denom: coin.Denom,
|
||||||
Amount: sdk.NewDecFromInt(coin.Amount),
|
Amount: sdk.NewDecFromInt(coin.Amount),
|
||||||
|
@ -21,16 +29,16 @@ func NewDecCoin(coin sdk.Coin) DecCoin {
|
||||||
|
|
||||||
// Adds amounts of two coins with same denom
|
// Adds amounts of two coins with same denom
|
||||||
func (coin DecCoin) Plus(coinB DecCoin) DecCoin {
|
func (coin DecCoin) Plus(coinB DecCoin) DecCoin {
|
||||||
if !(coin.Denom == coinB.Denom) {
|
if coin.Denom != coinB.Denom {
|
||||||
return coin
|
panic(fmt.Sprintf("coin denom different: %v %v\n", coin.Denom, coinB.Denom))
|
||||||
}
|
}
|
||||||
return DecCoin{coin.Denom, coin.Amount.Add(coinB.Amount)}
|
return DecCoin{coin.Denom, coin.Amount.Add(coinB.Amount)}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subtracts amounts of two coins with same denom
|
// Subtracts amounts of two coins with same denom
|
||||||
func (coin DecCoin) Minus(coinB DecCoin) DecCoin {
|
func (coin DecCoin) Minus(coinB DecCoin) DecCoin {
|
||||||
if !(coin.Denom == coinB.Denom) {
|
if coin.Denom != coinB.Denom {
|
||||||
return coin
|
panic(fmt.Sprintf("coin denom different: %v %v\n", coin.Denom, coinB.Denom))
|
||||||
}
|
}
|
||||||
return DecCoin{coin.Denom, coin.Amount.Sub(coinB.Amount)}
|
return DecCoin{coin.Denom, coin.Amount.Sub(coinB.Amount)}
|
||||||
}
|
}
|
||||||
|
@ -48,7 +56,7 @@ type DecCoins []DecCoin
|
||||||
func NewDecCoins(coins sdk.Coins) DecCoins {
|
func NewDecCoins(coins sdk.Coins) DecCoins {
|
||||||
dcs := make(DecCoins, len(coins))
|
dcs := make(DecCoins, len(coins))
|
||||||
for i, coin := range coins {
|
for i, coin := range coins {
|
||||||
dcs[i] = NewDecCoin(coin)
|
dcs[i] = NewDecCoinFromCoin(coin)
|
||||||
}
|
}
|
||||||
return dcs
|
return dcs
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -11,23 +12,16 @@ func TestPlusDecCoin(t *testing.T) {
|
||||||
decCoinA1 := DecCoin{"A", sdk.NewDecWithPrec(11, 1)}
|
decCoinA1 := DecCoin{"A", sdk.NewDecWithPrec(11, 1)}
|
||||||
decCoinA2 := DecCoin{"A", sdk.NewDecWithPrec(22, 1)}
|
decCoinA2 := DecCoin{"A", sdk.NewDecWithPrec(22, 1)}
|
||||||
decCoinB1 := DecCoin{"B", sdk.NewDecWithPrec(11, 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 {
|
// regular add
|
||||||
inputOne DecCoin
|
res := decCoinA1.Plus(decCoinA1)
|
||||||
inputTwo DecCoin
|
require.Equal(t, decCoinA2, res, "sum of coins is incorrect")
|
||||||
expected DecCoin
|
|
||||||
}{
|
// bad denom add
|
||||||
{decCoinA1, decCoinA1, decCoinA2},
|
assert.Panics(t, func() {
|
||||||
{decCoinA1, decCoinB1, decCoinA1},
|
decCoinA1.Plus(decCoinB1)
|
||||||
{decCoinCn4, decCoinC5, decCoinC1},
|
}, "expected panic on sum of different denoms")
|
||||||
}
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPlusCoins(t *testing.T) {
|
func TestPlusCoins(t *testing.T) {
|
||||||
|
|
|
@ -4,7 +4,7 @@ import (
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
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 {
|
type DelegatorDistInfo struct {
|
||||||
DelegatorAddr sdk.AccAddress `json:"delegator_addr"`
|
DelegatorAddr sdk.AccAddress `json:"delegator_addr"`
|
||||||
ValOperatorAddr sdk.ValAddress `json:"val_operator_addr"`
|
ValOperatorAddr sdk.ValAddress `json:"val_operator_addr"`
|
||||||
|
@ -36,7 +36,7 @@ func (di DelegatorDistInfo) WithdrawRewards(fp FeePool, vi ValidatorDistInfo,
|
||||||
|
|
||||||
blocks := height - di.WithdrawalHeight
|
blocks := height - di.WithdrawalHeight
|
||||||
di.WithdrawalHeight = height
|
di.WithdrawalHeight = height
|
||||||
accum := delegatorShares.Mul(sdk.NewDec(blocks))
|
accum := delegatorShares.MulInt(sdk.NewInt(blocks))
|
||||||
withdrawalTokens := vi.Pool.MulDec(accum).QuoDec(vi.DelAccum.Accum)
|
withdrawalTokens := vi.Pool.MulDec(accum).QuoDec(vi.DelAccum.Accum)
|
||||||
remainingTokens := vi.Pool.Minus(withdrawalTokens)
|
remainingTokens := vi.Pool.Minus(withdrawalTokens)
|
||||||
|
|
||||||
|
@ -45,11 +45,3 @@ func (di DelegatorDistInfo) WithdrawRewards(fp FeePool, vi ValidatorDistInfo,
|
||||||
|
|
||||||
return di, vi, fp, withdrawalTokens
|
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
|
// simulate adding some stake for inflation
|
||||||
height = 10
|
height = 10
|
||||||
fp.Pool = DecCoins{DecCoin{"stake", sdk.NewDec(1000)}}
|
fp.Pool = DecCoins{NewDecCoin("stake", 1000)}
|
||||||
|
|
||||||
// withdraw rewards
|
// withdraw rewards
|
||||||
di1, vi, fp, rewardRecv1 := di1.WithdrawRewards(fp, vi, height, totalBondedTokens,
|
di1, vi, fp, rewardRecv1 := di1.WithdrawRewards(fp, vi, height, totalBondedTokens,
|
||||||
|
|
|
@ -17,10 +17,14 @@ func NewTotalAccum(height int64) TotalAccum {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// update total validator accumulation factor
|
// update total validator accumulation factor for the new height
|
||||||
func (ta TotalAccum) Update(height int64, accumCreatedPerBlock sdk.Dec) TotalAccum {
|
// CONTRACT: height should be greater than the old height
|
||||||
|
func (ta TotalAccum) UpdateForNewHeight(height int64, accumCreatedPerBlock sdk.Dec) TotalAccum {
|
||||||
blocks := height - ta.UpdateHeight
|
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
|
ta.UpdateHeight = height
|
||||||
return ta
|
return ta
|
||||||
}
|
}
|
||||||
|
@ -36,7 +40,7 @@ type FeePool struct {
|
||||||
|
|
||||||
// update total validator accumulation factor
|
// update total validator accumulation factor
|
||||||
func (f FeePool) UpdateTotalValAccum(height int64, totalBondedTokens sdk.Dec) FeePool {
|
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
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,14 +7,14 @@ import (
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTotalAccumUpdate(t *testing.T) {
|
func TestTotalAccumUpdateForNewHeight(t *testing.T) {
|
||||||
|
|
||||||
ta := NewTotalAccum(0)
|
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))
|
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))
|
require.True(sdk.DecEq(t, sdk.NewDec(21), ta.Accum))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,14 @@
|
||||||
package types
|
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
|
// GenesisState - all distribution state that must be provided at genesis
|
||||||
type GenesisState struct {
|
type GenesisState struct {
|
||||||
FeePool FeePool `json:"fee_pool"`
|
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) 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()
|
// Return address that must sign over msg.GetSignBytes()
|
||||||
func (msg MsgSetWithdrawAddress) GetSigners() []sdk.AccAddress {
|
func (msg MsgSetWithdrawAddress) GetSigners() []sdk.AccAddress {
|
||||||
|
|
|
@ -27,7 +27,7 @@ func NewValidatorDistInfo(operatorAddr sdk.ValAddress, currentHeight int64) Vali
|
||||||
|
|
||||||
// update total delegator accumululation
|
// update total delegator accumululation
|
||||||
func (vi ValidatorDistInfo) UpdateTotalDelAccum(height int64, totalDelShares sdk.Dec) ValidatorDistInfo {
|
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
|
return vi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ func (vi ValidatorDistInfo) TakeFeePoolRewards(fp FeePool, height int64, totalBo
|
||||||
// update the validators pool
|
// update the validators pool
|
||||||
blocks := height - vi.FeePoolWithdrawalHeight
|
blocks := height - vi.FeePoolWithdrawalHeight
|
||||||
vi.FeePoolWithdrawalHeight = height
|
vi.FeePoolWithdrawalHeight = height
|
||||||
accum := sdk.NewDec(blocks).Mul(vdTokens)
|
accum := vdTokens.MulInt(sdk.NewInt(blocks))
|
||||||
withdrawalTokens := fp.Pool.MulDec(accum).QuoDec(fp.ValAccum.Accum)
|
withdrawalTokens := fp.Pool.MulDec(accum).QuoDec(fp.ValAccum.Accum)
|
||||||
remainingTokens := fp.Pool.Minus(withdrawalTokens)
|
remainingTokens := fp.Pool.Minus(withdrawalTokens)
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,7 @@ func TestTakeFeePoolRewards(t *testing.T) {
|
||||||
|
|
||||||
// simulate adding some stake for inflation
|
// simulate adding some stake for inflation
|
||||||
height = 10
|
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)
|
vi1, fp = vi1.TakeFeePoolRewards(fp, height, totalBondedTokens, validatorTokens1, commissionRate1)
|
||||||
require.True(sdk.DecEq(t, sdk.NewDec(900), fp.ValAccum.Accum))
|
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
|
// simulate adding some stake for inflation
|
||||||
height = 10
|
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
|
// for a more fun staring condition, have an non-withdraw update
|
||||||
vi, fp = vi.TakeFeePoolRewards(fp, height, totalBondedTokens, validatorTokens, commissionRate)
|
vi, fp = vi.TakeFeePoolRewards(fp, height, totalBondedTokens, validatorTokens, commissionRate)
|
||||||
|
|
Loading…
Reference in New Issue