-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Slashing v2 #1278
Slashing v2 #1278
Changes from 1 commit
3d0e7ff
43a0ed9
a49f9cb
f972cab
752f084
5a77d7d
26681a8
8e677e8
43429ef
482a3c5
fd1eb5f
63a0dcc
7eaa715
3fac834
d69d179
0efb0a4
2153e0d
f6b9893
e460c06
8639f91
9ee70ea
bd41980
40ad0a5
4be5e41
03c6804
98dad7d
4601bd5
fc6ee07
ffc8cc4
6a5bc2d
436f32d
37190aa
ed7b90e
af4fd6e
1d7d4d4
d427af7
a0ac5e9
b72a219
8860fa3
24395fd
258834f
9ce6759
1ae48da
bc2e0d7
ad73cd1
60158d5
b1cb149
3cd8478
a3e04ce
782afbb
ad9da64
071d47b
5dc163c
bdc437e
2f42a2b
72d36d1
182d0cb
683b64a
0d38a72
6e2c66c
f004c6c
ceb8856
9f99a17
32b75c5
85c3639
177ab4c
3f4e528
c7433c9
f5dbcfd
de8b5b1
3e18286
40b9faf
a500931
c916f21
587e9fd
82f3529
2a34342
d5ab2f3
b8571b6
2511ef1
a0c9fed
86439ee
a029cb9
9cddb06
41bcf70
54b6ae5
e56c08b
2659120
3841a7a
03273a7
c17b8e6
dd3bdcb
48d1107
d93502d
c565dab
303fbe5
7170bd9
b7ece9a
74e0fb8
5329d73
9be314e
1a945a2
ffaefec
0012a46
0663984
70605a2
6f2c29b
a6c0381
637b275
4908504
34e46ac
f499b88
bcace44
de77006
791ad9c
44378a1
a901ed6
5449296
12fc6a9
08597c9
cff38d8
1d97ed6
f178308
1e75912
a133bc5
8895552
97eed84
3d3189c
220d03f
7ab62b4
554a72b
3c25861
99fec69
e43bbef
c76a8e1
cb85459
4edb773
78ac9ae
b00a0a1
f8dd859
c1987da
9bc99f1
30d9d90
aeea789
9ff81a6
5a479e0
e46e687
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,26 +7,66 @@ import ( | |
crypto "github.com/tendermint/go-crypto" | ||
) | ||
|
||
// NOTE the current slash functionality doesn't take into consideration unbonding/rebonding records | ||
// or the time of breach. This will be updated in slashing v2 | ||
// slash a validator | ||
func (k Keeper) Slash(ctx sdk.Context, pubkey crypto.PubKey, height int64, power int64, fraction sdk.Rat) { | ||
|
||
// Amount of slashing = slash fraction * power at time of equivocation | ||
slashAmount := sdk.NewRat(power).Mul(fraction) | ||
// hmm, https://github.com/cosmos/cosmos-sdk/issues/1348 | ||
|
||
// Current timestamp | ||
now := ctx.BlockHeader().Time | ||
|
||
validator, found := k.GetValidatorByPubKey(ctx, pubkey) | ||
if !found { | ||
panic(fmt.Errorf("Attempted to slash a nonexistent validator with address %s", pubkey.Address())) | ||
} | ||
address := pubkey.Address() | ||
|
||
// Track remaining slash amount | ||
remainingSlashAmount := slashAmount | ||
|
||
// TODO Iterate through unbondings | ||
// Iterate through unbonding delegations from slashed validator | ||
unbondingDelegations := k.GetUnbondingDelegationsFromValidator(ctx, address) | ||
for _, unbondingDelegation := range unbondingDelegations { | ||
if unbondingDelegation.MinTime < now { | ||
// TODO Delete element? | ||
continue | ||
} | ||
|
||
// Calculate slash amount & deduct from total | ||
slashAmount := sdk.NewRatFromInt(unbondingDelegation.InitialBalance.Amount, sdk.OneInt()).Mul(fraction) | ||
remainingSlashAmount = remainingSlashAmount.Sub(slashAmount) | ||
|
||
// Update unbonding delegation | ||
slashAmountInt := slashAmount.EvaluateInt() | ||
if slashAmountInt.GT(unbondingDelegation.Balance.Amount) { | ||
slashAmountInt = unbondingDelegation.Balance.Amount | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as this is an optimization point what about passing a function into the keeper to run something for each delegator as it's iterating through them opposed to retrieving the list and then iterating over it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to write to the unbonding delegations / redelegations, which I don't think we can do while iterating per https://github.com/cosmos/cosmos-sdk/blob/develop/types/store.go#L114? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That contract needs to be updated - I'm fairly positive this can be accomplished in the current IAVL implementation - something funky with the caches |
||
unbondingDelegation.Balance = unbondingDelegation.Balance.Minus(sdk.Coin{unbondingDelegation.Balance.Denom, slashAmountInt}) | ||
k.SetUnbondingDelegation(ctx, unbondingDelegation) | ||
} | ||
|
||
// TODO Iterate through redelegations | ||
// Iterate through redelegations from slashed validator | ||
redelegations := k.GetRedelegationsFromValidator(ctx, address) | ||
for _, redelegation := range redelegations { | ||
if redelegation.MinTime < now { | ||
// TODO Delete element? | ||
continue | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same comment as oppose for the redelegations There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above. |
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add a TODO for overflowing into the next block if there is to much to iterate through? going to be important at some point past launch There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
// Calculate slash amount & deduct from total | ||
slashAmount := sdk.NewRatFromInt(redelegation.InitialBalance.Amount, sdk.OneInt()).Mul(fraction) | ||
remainingSlashAmount = remainingSlashAmount.Sub(slashAmount) | ||
|
||
// Update redelegation | ||
slashAmountInt := slashAmount.EvaluateInt() | ||
if slashAmountInt.GT(redelegation.Balance.Amount) { | ||
slashAmountInt = redelegation.Balance.Amount | ||
} | ||
redelegation.Balance = redelegation.Balance.Minus(sdk.Coin{redelegation.Balance.Denom, slashAmountInt}) | ||
k.SetRedelegation(ctx, redelegation) | ||
} | ||
|
||
sharesToRemove := remainingSlashAmount | ||
// Cannot decrease balance below zero | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,7 @@ type UnbondingDelegation struct { | |
ValidatorAddr sdk.Address `json:"validator_addr"` // validator unbonding from owner addr | ||
CreationHeight int64 `json:"creation_height"` // height which the unbonding took place | ||
MinTime int64 `json:"min_time"` // unix time for unbonding completion | ||
InitialBalance sdk.Coin `json:"initial_balance"` // atoms initially scheduled to receive at completion | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this just be called the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to keep the |
||
Balance sdk.Coin `json:"balance"` // atoms to receive at completion | ||
} | ||
|
||
|
@@ -101,6 +102,8 @@ type Redelegation struct { | |
ValidatorDstAddr sdk.Address `json:"validator_dst_addr"` // validator redelegation destination owner addr | ||
CreationHeight int64 `json:"creation_height"` // height which the redelegation took place | ||
MinTime int64 `json:"min_time"` // unix time for redelegation completion | ||
InitialBalance sdk.Coin `json:"initial_balance"` // initial balance when redelegation started | ||
Balance sdk.Coin `json:"balance"` // current balance | ||
SharesSrc sdk.Rat `json:"shares` // amount of source shares redelegating | ||
SharesDst sdk.Rat `json:"shares` // amount of destination shares redelegating | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
may be good to specify the input
height
a bit more such asinfractionHeight
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to
infractionHeight
.