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: Distr-PR-5 Implement Distribution #2236

Merged
merged 121 commits into from
Oct 16, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
121 commits
Select commit Hold shift + click to select a range
b2eab0d
Merge remote-tracking branch 'origin/develop' into rigel/fee-distribu…
rigelrozanski Aug 31, 2018
469334d
Merge branch 'rigel/piggy-bank-fee-spec' into rigel/fee-distribution
rigelrozanski Sep 3, 2018
d7794b4
Merge remote-tracking branch 'origin/develop' into rigel/fee-distribu…
rigelrozanski Sep 3, 2018
9a2aee8
working
rigelrozanski Sep 4, 2018
48aa9a6
working, moved WIP distribution spec to attic
rigelrozanski Sep 4, 2018
4ddf6ca
undo spec move for PR clarity
rigelrozanski Sep 4, 2018
43ab066
move WIP-lamborghini-distribution
rigelrozanski Sep 4, 2018
38c6090
Merge branch 'rigel/move-spec' into rigel/fee-distribution
rigelrozanski Sep 5, 2018
53253aa
working
rigelrozanski Sep 5, 2018
aebd36f
...
rigelrozanski Sep 5, 2018
8ab25fa
Merge remote-tracking branch 'origin/develop' into rigel/fee-distribu…
rigelrozanski Sep 5, 2018
e2b5b9b
...
rigelrozanski Sep 5, 2018
b409099
update spec
rigelrozanski Sep 5, 2018
cbb5a50
work on allocate fees
rigelrozanski Sep 5, 2018
e498d43
dec coins, refactoring distr
rigelrozanski Sep 10, 2018
7d864fe
Merge remote-tracking branch 'origin/develop' into rigel/fee-distribu…
rigelrozanski Sep 10, 2018
922eb5e
...
rigelrozanski Sep 11, 2018
a153088
...
rigelrozanski Sep 11, 2018
46db96b
Merge remote-tracking branch 'origin/develop' into rigel/fee-distribu…
rigelrozanski Sep 13, 2018
dae32a3
...
rigelrozanski Sep 13, 2018
d60f620
working
rigelrozanski Sep 14, 2018
2eff69a
Merge remote-tracking branch 'origin/develop' into rigel/fee-distribu…
rigelrozanski Sep 17, 2018
431fdb8
hook, genesis, and withdraw address
rigelrozanski Sep 18, 2018
24e43b9
hookwork
rigelrozanski Sep 18, 2018
bb9e647
add in basic commission stuff
rigelrozanski Sep 18, 2018
a18b895
handler, tags, msgs, codec
rigelrozanski Sep 19, 2018
e3cf4f6
cli transactions
rigelrozanski Sep 19, 2018
73c5fdf
more cli related
rigelrozanski Sep 19, 2018
de4071f
aliases, errors
rigelrozanski Sep 19, 2018
97f7dbc
Merge remote-tracking branch 'origin/develop' into rigel/fee-distribu…
rigelrozanski Sep 19, 2018
7688299
alias work, working on compiling
rigelrozanski Sep 19, 2018
df9c8db
working through compile errors
rigelrozanski Sep 19, 2018
d9396ed
working compile errors
rigelrozanski Sep 19, 2018
7002438
compile debugging
rigelrozanski Sep 20, 2018
18fdccf
...
rigelrozanski Sep 20, 2018
5dabd1b
the errors never want to seem to end...
rigelrozanski Sep 20, 2018
5790f33
...
rigelrozanski Sep 20, 2018
251a81f
yay it compiles
rigelrozanski Sep 20, 2018
d360304
breakout use of GetValidatorByConsAddr
rigelrozanski Sep 20, 2018
0d9105c
...
rigelrozanski Sep 21, 2018
0867ce8
...
rigelrozanski Sep 21, 2018
4e4749d
pulling in stuff from fee-distr PR
rigelrozanski Sep 21, 2018
ab61695
PENDING
rigelrozanski Sep 21, 2018
0f903bd
finished pulling in, compiles
rigelrozanski Sep 21, 2018
9e2b8e4
revert some gov changes
rigelrozanski Sep 21, 2018
1243507
no more compile errors in tests
rigelrozanski Sep 21, 2018
787a2f9
fix using cons address, also remove old commented distr code
rigelrozanski Sep 21, 2018
72e3025
doc update
rigelrozanski Sep 21, 2018
b212470
revert some accidental stuff
rigelrozanski Sep 21, 2018
a2b2ef9
Merge remote-tracking branch 'origin/develop' into rigel/stake-cons-addr
rigelrozanski Sep 22, 2018
14792f2
val comments
rigelrozanski Sep 22, 2018
d428317
import decimal from fee-distr PR
rigelrozanski Sep 22, 2018
79cfee9
pending
rigelrozanski Sep 22, 2018
6012a25
...
rigelrozanski Sep 22, 2018
6f35822
import hook from fee-distr
rigelrozanski Sep 22, 2018
1b485bb
pending
rigelrozanski Sep 22, 2018
df0d582
add staking hooks to staking spec
rigelrozanski Sep 22, 2018
9dfeeb0
address bez comments
rigelrozanski Sep 24, 2018
afe179e
remove GetValidatorByConsPubKey
rigelrozanski Sep 25, 2018
16594f7
Merge remote-tracking branch 'origin/develop' into rigel/stake-cons-addr
rigelrozanski Sep 25, 2018
dad4253
merge fix
rigelrozanski Sep 25, 2018
ff8d748
Merge branch 'rigel/stake-cons-addr' into rigel/truncate-decimal
rigelrozanski Sep 25, 2018
f5116b9
Merge branch 'rigel/truncate-decimal' into rigel/new-hooks
rigelrozanski Sep 25, 2018
ca2fc77
update commission hook for new commission work
rigelrozanski Sep 25, 2018
5be5ea3
comment update
rigelrozanski Sep 25, 2018
380d3e1
pull a bunch from develop
rigelrozanski Sep 25, 2018
47e899b
Merge remote-tracking branch 'origin/develop' into rigel/fee-distribu…
rigelrozanski Sep 25, 2018
d3146bf
...
rigelrozanski Sep 25, 2018
910af20
Merge commit '5be5ea3' into rigel/new-hooks
rigelrozanski Sep 25, 2018
9e9756a
Merge remote-tracking branch 'origin/develop' into rigel/new-hooks
rigelrozanski Sep 25, 2018
4929b02
mini-fix
rigelrozanski Sep 25, 2018
3846894
Merge branch 'rigel/new-hooks' into rigel/fee-distribution
rigelrozanski Sep 26, 2018
0436f50
...
rigelrozanski Sep 27, 2018
436d2a0
Merge remote-tracking branch 'origin/develop' into rigel/fee-distribu…
rigelrozanski Sep 28, 2018
956566d
proser
rigelrozanski Sep 28, 2018
60a3541
major debugs
rigelrozanski Sep 28, 2018
7ab02ae
lcd tests to include proposer
rigelrozanski Sep 28, 2018
733b616
disable endblock for lcd
rigelrozanski Sep 28, 2018
e223ead
types msg tests
rigelrozanski Sep 28, 2018
064f8b0
dec coin tests
rigelrozanski Sep 28, 2018
16f5d69
types fee pool test
rigelrozanski Sep 28, 2018
dcf0dda
...
rigelrozanski Sep 29, 2018
1e74da1
validator_info tests
rigelrozanski Oct 4, 2018
4d4de3a
delegator_dist_info tests
rigelrozanski Oct 4, 2018
3a9102e
fix
rigelrozanski Oct 4, 2018
eb01cb4
checkout distr types PR updates
rigelrozanski Oct 4, 2018
45f0dd0
Merge remote-tracking branch 'origin/develop' into rigel/fee-distribu…
rigelrozanski Oct 4, 2018
20a51e5
fix compile errors
rigelrozanski Oct 4, 2018
17dc812
fix distribution endblocker issues
rigelrozanski Oct 5, 2018
81d0f5f
add wanted final test case functions
rigelrozanski Oct 5, 2018
83c2781
Merge remote-tracking branch 'origin/develop' into rigel/fee-distribu…
rigelrozanski Oct 5, 2018
e304b9c
debugging simulation
rigelrozanski Oct 6, 2018
142f42e
...
rigelrozanski Oct 9, 2018
6005325
Merge remote-tracking branch 'origin/develop' into rigel/fee-distribu…
rigelrozanski Oct 9, 2018
46fe52b
create test initialization for distr
rigelrozanski Oct 9, 2018
c973032
...
rigelrozanski Oct 9, 2018
6d4975e
R4R: Fix simulation proposer (#2460)
cwgoes Oct 9, 2018
fc9bafb
Merge remote-tracking branch 'origin/develop' into rigel/fee-distribu…
rigelrozanski Oct 10, 2018
151a81e
staking changes test_cover fixes
rigelrozanski Oct 10, 2018
ace19d9
Merge branch 'rigel/fee-distribution' of https://github.com/cosmos/co…
rigelrozanski Oct 10, 2018
52316d9
fix initialization of the test
rigelrozanski Oct 10, 2018
22de856
fix distr initialization
rigelrozanski Oct 10, 2018
3b5e57b
ctx with proposer, working through allocation test
rigelrozanski Oct 10, 2018
900a5f4
keeper_test
rigelrozanski Oct 11, 2018
f4f39ae
allocation tests
rigelrozanski Oct 11, 2018
8e378e2
got basic delegation tests working
rigelrozanski Oct 11, 2018
8cb6d10
TestWithdrawDelegationRewardWithCommission
rigelrozanski Oct 11, 2018
f3c0496
TestWithdrawDelegationRewardsAll
rigelrozanski Oct 12, 2018
a60b91b
bugfix form TestWithdrawDelegationRewardTwoDelegatorsUneven
rigelrozanski Oct 12, 2018
94731a5
some validator dist tests
rigelrozanski Oct 12, 2018
7b1f4a8
finished off validator tests
rigelrozanski Oct 12, 2018
dba20d1
Merge remote-tracking branch 'origin/develop' into rigel/fee-distribu…
rigelrozanski Oct 12, 2018
06fa518
address @cwgoes comments, add in precommit calculation
rigelrozanski Oct 12, 2018
2ece2ff
address @cwgoes comments
rigelrozanski Oct 15, 2018
5de0c9a
Merge remote-tracking branch 'origin/develop' into rigel/fee-distribu…
rigelrozanski Oct 15, 2018
2f4a01b
address @ValarDragon comments, param store update
rigelrozanski Oct 15, 2018
43e4396
Merge remote-tracking branch 'origin/develop' into rigel/fee-distribu…
rigelrozanski Oct 15, 2018
076053d
proposer reward params to global param
rigelrozanski Oct 15, 2018
eaecde2
test_utils >:(
rigelrozanski Oct 15, 2018
03b9260
oh
rigelrozanski Oct 15, 2018
32e0f14
rework to correctly use previous vote info
rigelrozanski Oct 16, 2018
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
Prev Previous commit
Next Next commit
dec coins, refactoring distr
  • Loading branch information
rigelrozanski committed Sep 10, 2018
commit e498d43d03569dc8dbd7cc19c0e95844c9b31988
26 changes: 26 additions & 0 deletions types/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,32 @@ func (d Dec) RoundInt() Int {

//___________________________________________________________________________________

// similar to chopPrecisionAndRound, but always rounds down
func chopPrecisionAndTruncate(d *big.Int) *big.Int {
return d.Quo(d, precisionReuse)
}

func chopPrecisionAndTruncateNonMutative(d *big.Int) *big.Int {
tmp := new(big.Int).Set(d)
return chopPrecisionAndTruncate(tmp)
}

// RoundInt64 rounds the decimal using bankers rounding
func (d Dec) TruncateInt64() int64 {
chopped := chopPrecisionAndTruncateNonMutative(d.Int)
if !chopped.IsInt64() {
panic("Int64() out of bound")
}
return chopped.Int64()
}

// RoundInt round the decimal using bankers rounding
func (d Dec) TruncateInt() Int {
return NewIntFromBigInt(chopPrecisionAndTruncateNonMutative(d.Int))
}

//___________________________________________________________________________________

// reuse nil values
var (
nilAmino string
Expand Down
26 changes: 26 additions & 0 deletions types/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,32 @@ func TestBankerRoundChop(t *testing.T) {
}
}

func TestTruncate(t *testing.T) {
tests := []struct {
d1 Dec
exp int64
}{
{mustNewDecFromStr(t, "0"), 0},
{mustNewDecFromStr(t, "0.25"), 0},
{mustNewDecFromStr(t, "0.75"), 0},
{mustNewDecFromStr(t, "1"), 1},
{mustNewDecFromStr(t, "1.5"), 1},
{mustNewDecFromStr(t, "7.5"), 7},
{mustNewDecFromStr(t, "7.6"), 7},
{mustNewDecFromStr(t, "7.4"), 7},
{mustNewDecFromStr(t, "100.1"), 100},
{mustNewDecFromStr(t, "1000.1"), 1000},
}

for tcIndex, tc := range tests {
resNeg := tc.d1.Neg().TruncateInt64()
require.Equal(t, -1*tc.exp, resNeg, "negative tc %d", tcIndex)

resPos := tc.d1.TruncateInt64()
require.Equal(t, tc.exp, resPos, "positive tc %d", tcIndex)
}
}

func TestToLeftPadded(t *testing.T) {
tests := []struct {
dec Dec
Expand Down
6 changes: 3 additions & 3 deletions x/distribution/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/stake"
distr "github.com/cosmos/cosmos-sdk/x/distribution"
"github.com/spf13/cobra"
"github.com/spf13/viper"
wire "github.com/tendermint/go-wire"
Expand Down Expand Up @@ -38,7 +38,7 @@ var (
func GetCmdWithdrawDelegationRewardsAll(cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "withdraw-rewards [delegator]",
Short: "withdraw rewards from delegation accounts",
Short: "withdraw rewards for all delegations",
RunE: func(cmd *cobra.Command, args []string) error {
txCtx := authctx.NewTxContextFromCLI().WithCodec(cdc)
cliCtx := context.NewCLIContext().
Expand All @@ -61,7 +61,7 @@ func GetCmdWithdrawDelegationRewardsAll(cdc *wire.Codec) *cobra.Command {
return err
}

msg := stake.NewMsgDelegate(delAddr, valAddr, amount)
msg := distr.NewMsgDelegate(delAddr, valAddr, amount)

// build and sign the transaction, then broadcast to Tendermint
return utils.SendTx(txCtx, cliCtx, []sdk.Msg{msg})
Expand Down
1 change: 0 additions & 1 deletion x/distribution/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
// distribution fee handler
func DistributionFeeHandler(ctx sdk.Context, _ sdk.Tx, collectedFees sdk.Coins) {

k.
}

//////////////////////////////////////////////////////////////////////////////////
Expand Down
51 changes: 25 additions & 26 deletions x/distribution/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,18 @@ func (k Keeper) SetDelegatorDistInfo(ctx sdk.Context, ddi types.DelegatorDistInf

//___________________________________________________________________________________________

// XXX TODO
func (k Keeper) WithdrawDelegationReward(ctx sdk.Context, delegatorAddr, validatorAddr, withdrawAddr sdk.AccAddress) {
height = ctx.BlockHeight()

// get all distribution scenarios
pool = stake.GetPool()
feePool = GetFeePool()
delInfo = GetDelegationDistInfo(delegatorAddr,
validatorAddr)
valInfo = GetValidatorDistInfo(validatorAddr)
validator = GetValidator(validatorAddr)

feePool, withdraw = delInfo.WithdrawRewards(feePool, valInfo, height, pool.BondedTokens,
// withdraw all the rewards for a single delegation
func (k Keeper) WithdrawDelegationReward(ctx sdk.Context, delegatorAddr,
withdrawAddr sdk.AccAddress, validatorAddr sdk.ValAddress) {

height := ctx.BlockHeight()
pool := stake.GetPool()
feePool := GetFeePool()
delInfo := GetDelegationDistInfo(delegatorAddr, validatorAddr)
valInfo := GetValidatorDistInfo(validatorAddr)
validator := GetValidator(validatorAddr)

feePool, withdraw := delInfo.WithdrawRewards(feePool, valInfo, height, pool.BondedTokens,
validator.Tokens, validator.DelegatorShares, validator.Commission)

SetFeePool(feePool)
Expand All @@ -51,30 +50,30 @@ func (k Keeper) WithdrawDelegationReward(ctx sdk.Context, delegatorAddr, validat

///////////////////////////////////////////////////////////////////////////////////////

// XXX TODO
// return all rewards for all delegations of a delegator
func (k Keeper) WithdrawDelegationRewardsAll(ctx sdk.Context, delegatorAddr, withdrawAddr sdk.AccAddress) {
height = ctx.BlockHeight()
withdraw = GetDelegatorRewardsAll(delegatorAddr, height)
height := ctx.BlockHeight()
withdraw = GetDelegatorRewardsAll(ctx, delegatorAddr, height)
k.coinsKeeper.AddCoins(withdrawAddr, withdraw.Amount.TruncateDecimal())
}

// XXX TODO
// return all rewards for all delegations of a delegator
func (k Keeper) GetDelegatorRewardsAll(ctx sdk.Context, delAddr sdk.AccAddress, height int64) DecCoins {

// collect all entitled rewards
withdraw = 0
pool = stake.GetPool()
feePool = GetFeePool()
withdraw := sdk.NewDec(0)
pool := stake.GetPool()
feePool := GetFeePool()

// iterate over all the delegations
operationAtDelegation := func(_ int64, del types.Delegation) (stop bool) {
delInfo = GetDelegationDistInfo(delAddr, del.ValidatorAddr)
valInfo = GetValidatorDistInfo(del.ValidatorAddr)
validator = GetValidator(del.ValidatorAddr)
delInfo := GetDelegationDistInfo(delAddr, del.ValidatorAddr)
valInfo := GetValidatorDistInfo(del.ValidatorAddr)
validator := GetValidator(del.ValidatorAddr)

feePool, diWithdraw = delInfo.WithdrawRewards(feePool, valInfo, height, pool.BondedTokens,
feePool, diWithdraw := delInfo.WithdrawRewards(feePool, valInfo, height, pool.BondedTokens,
validator.Tokens, validator.DelegatorShares, validator.Commission)
withdraw += diWithdraw
withdraw = withdraw.Add(diWithdraw)
SetFeePool(feePool)
return false
}
k.stakeKeeper.IterateDelegations(ctx, delAddr, operationAtDelegation)
Expand Down
21 changes: 11 additions & 10 deletions x/distribution/keeper/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,22 @@ func (k Keeper) SetValidatorDistInfo(ctx sdk.Context, vdi types.ValidatorDistInf
}

// XXX TODO
func (k Keeper) WithdrawValidatorRewardsAll(ctx sdk.Context, operatorAddr, withdrawAddr sdk.AccAddress) {
height = ctx.BlockHeight()
feePool = k.GetFeePool(ctx)
pool = k.stakeKeeper.GetPool(ctx)
ValInfo = k.GetValidatorDistInfo(delegation.ValidatorAddr)
validator = k.GetValidator(delegation.ValidatorAddr)
func (k Keeper) WithdrawValidatorRewardsAll(ctx sdk.Context,
operatorAddr sdk.ValAddress, withdrawAddr sdk.AccAddress) {

// withdraw self-delegation
withdraw = k.GetDelegatorRewardsAll(validator.OperatorAddr, height)
height := ctx.BlockHeight()
validator := k.GetValidator(operatorAddr)
withdraw := k.GetDelegatorRewardsAll(validator.OperatorAddr, height)

// withdrawal validator commission rewards
feePool, commission = valInfo.WithdrawCommission(feePool, valInfo, height, pool.BondedTokens,
pool := k.stakeKeeper.GetPool(ctx)
valInfo := k.GetValidatorDistInfo(operatorAddr)
feePool := k.GetFeePool(ctx)
feePool, commission := valInfo.WithdrawCommission(feePool, valInfo, height, pool.BondedTokens,
validator.Tokens, validator.Commission)
withdraw += commission
SetFeePool(feePool)
withdraw = withdraw.Add(commission)
k.SetFeePool(feePool)

k.coinKeeper.AddCoins(withdrawAddr, withdraw.TruncateDecimal())
}
72 changes: 67 additions & 5 deletions x/distribution/types/dec_coin.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,89 @@
package types

import sdk "github.com/cosmos/cosmos-sdk/types"
import (
"strings"

// coins with decimal
type DecCoins []DecCoin
sdk "github.com/cosmos/cosmos-sdk/types"
)

// Coins which can have additional decimal points
type DecCoin struct {
Amount sdk.Dec `json:"amount"`
Denom string `json:"denom"`
Amount sdk.Dec `json:"amount"`
}

func NewDecCoin(coin sdk.Coin) DecCoin {
return DecCoins{
Amount: sdk.NewDec(coin.Amount),
Denom: coin.Denom,
Amount: sdk.NewDec(coin.Amount),
}
}

// Adds amounts of two coins with same denom
func (coin DecCoin) Plus(coinB DecCoin) DecCoin {
if !(coin.Denom == coinB.Denom) {
return coin
}
return Coin{coin.Denom, coin.Amount.Add(coinB.Amount)}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be DecCoin{} not Coin right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yeah these types of error's are riddled throughout - I haven't gotten this to compile yet - still WIP on that front

}

// return the decimal coins with trunctated decimals
func (coin DecCoin) TruncateDecimal() sdk.Coin {
return sdk.NewCoin(coin.Denom, coin.Amount.TruncateInt())
}

//_______________________________________________________________________

// coins with decimal
type DecCoins []DecCoin

func NewDecCoins(coins sdk.Coins) DecCoins {

dcs := make(DecCoins, len(coins))
for i, coin := range coins {
dcs[i] = NewDecCoin(coin)
}
}

// return the decimal coins with trunctated decimals
func (coins DecCoins) TruncateDecimal() sdk.Coins {
out := make(DecCoins, len(coins))
for i, coin := range coins {
out[i] = coin.TruncateDecimal()
}
}

// Plus combines two sets of coins
// CONTRACT: Plus will never return Coins where one Coin has a 0 amount.
func (coins DecCoins) Plus(coinsB DecCoins) DecCoins {
sum := ([]DecCoin)(nil)
indexA, indexB := 0, 0
lenA, lenB := len(coins), len(coinsB)
for {
if indexA == lenA {
if indexB == lenB {
return sum
}
return append(sum, coinsB[indexB:]...)
} else if indexB == lenB {
return append(sum, coins[indexA:]...)
}
coinA, coinB := coins[indexA], coinsB[indexB]
switch strings.Compare(coinA.Denom, coinB.Denom) {
case -1:
sum = append(sum, coinA)
indexA++
case 0:
if coinA.Amount.Add(coinB.Amount).IsZero() {
// ignore 0 sum coin type
} else {
sum = append(sum, coinA.Plus(coinB))
}
indexA++
indexB++
case 1:
sum = append(sum, coinB)
indexB++
}
}
}