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

WIP: Fix slashing period fetch #2305

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 24 additions & 0 deletions types/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,30 @@ func PrefixEndBytes(prefix []byte) []byte {
return end
}

// PrefixStartBytes returns the []byte that would start a
// range query for all []byte with a certain prefix
func PrefixStartBytes(prefix []byte) []byte {
if prefix == nil {
return nil
}

end := make([]byte, len(prefix))
copy(end, prefix)

for {
if end[len(end)-1] != byte(0) {
end[len(end)-1]--
break
} else {
end = end[:len(end)-1]
if len(end) == 0 {
panic("could not subtract")
}
}
}
return end
}

// TransientStoreKey is used for indexing transient stores in a MultiStore
type TransientStoreKey struct {
name string
Expand Down
16 changes: 14 additions & 2 deletions x/slashing/slashing_period.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package slashing

import (
"bytes"
"encoding/binary"
"fmt"

Expand All @@ -15,7 +16,7 @@ func (k Keeper) capBySlashingPeriod(ctx sdk.Context, address sdk.ConsAddress, fr

// Sanity check
if slashingPeriod.EndHeight > 0 && slashingPeriod.EndHeight < infractionHeight {
panic(fmt.Sprintf("slashing period ended before infraction: infraction height %d, slashing period ended at %d", infractionHeight, slashingPeriod.EndHeight))
panic(fmt.Sprintf("slashing period ended before infraction: validator %s, infraction height %d, slashing period ended at %d", address, infractionHeight, slashingPeriod.EndHeight))
}

// Calculate the updated total slash amount
Expand All @@ -41,9 +42,19 @@ func (k Keeper) getValidatorSlashingPeriodForHeight(ctx sdk.Context, address sdk
// Get the most recent slashing period at or before the infraction height
start := GetValidatorSlashingPeriodPrefix(address)
end := sdk.PrefixEndBytes(GetValidatorSlashingPeriodKey(address, height))
fmt.Printf("start: %X, end: %X, diff: %v\n", start, end, bytes.Compare(start, end))
// TODO
itr := sdk.KVStorePrefixIterator(store, GetValidatorSlashingPeriodPrefix(address))
for itr.Valid() {
fmt.Printf("Key: %X\n", itr.Key())
period := k.unmarshalSlashingPeriodKeyValue(itr.Key(), itr.Value())
fmt.Printf("Found %X => %v\n", address, period)
itr.Next()
}
// END TODO
iterator := store.ReverseIterator(start, end)
if !iterator.Valid() {
panic("expected to find slashing period, but none was found")
panic(fmt.Sprintf("expected to find slashing period for validator %s before height %d, but none was found", address, height))
}
slashingPeriod = k.unmarshalSlashingPeriodKeyValue(iterator.Key(), iterator.Value())
return
Expand All @@ -60,6 +71,7 @@ func (k Keeper) addOrUpdateValidatorSlashingPeriod(ctx sdk.Context, slashingPeri
}
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinary(slashingPeriodValue)
fmt.Printf("Set slashing period for validator: %X => %s\n", GetValidatorSlashingPeriodKey(slashingPeriod.ValidatorAddr, slashingPeriod.StartHeight), slashingPeriod.ValidatorAddr)
store.Set(GetValidatorSlashingPeriodKey(slashingPeriod.ValidatorAddr, slashingPeriod.StartHeight), bz)
}

Expand Down