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

fix key and add time Infraction #172

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
5 changes: 4 additions & 1 deletion x/meshsecurity/keeper/adapter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

import (
"time"

"cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -120,7 +122,8 @@ func (s StakingDecorator) Slash(ctx sdk.Context, consAddr sdk.ConsAddress, power
if val == nil {
ModuleLogger(ctx).
Error("can not propagate slash: validator not found", "validator", consAddr.String())
} else if err := s.k.ScheduleSlashed(ctx, val.GetOperator(), power, height, totalSlashAmount, slashRatio); err != nil {
// todo: add TimeInfraction
} else if err := s.k.ScheduleSlashed(ctx, val.GetOperator(), power, height, totalSlashAmount, slashRatio, time.Unix(0, 0)); err != nil {
DongLieu marked this conversation as resolved.
Show resolved Hide resolved
ModuleLogger(ctx).
Error("can not propagate slash: schedule event",
"cause", err,
Expand Down
15 changes: 9 additions & 6 deletions x/meshsecurity/keeper/valset_updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"cosmossdk.io/math"
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
"time"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -25,12 +26,13 @@ func (k Keeper) ScheduleUnbonded(ctx sdk.Context, addr sdk.ValAddress) error {
}

// ScheduleSlashed store a validator slash event / data for the valset update report
func (k Keeper) ScheduleSlashed(ctx sdk.Context, addr sdk.ValAddress, power int64, height int64, totalSlashAmount math.Int, slashRatio sdk.Dec) error {
func (k Keeper) ScheduleSlashed(ctx sdk.Context, addr sdk.ValAddress, power int64, height int64, totalSlashAmount math.Int, slashRatio sdk.Dec, timeInfraction time.Time) error {
var slashInfo = &types.SlashInfo{
Power: power,
InfractionHeight: height,
TotalSlashAmount: totalSlashAmount.String(),
SlashFraction: slashRatio.String(),
TimeInfraction: timeInfraction,
}
return k.sendAsync(ctx, types.ValidatorSlashed, addr, slashInfo)
}
Expand Down Expand Up @@ -127,8 +129,7 @@ func (k Keeper) ValsetUpdateReport(ctx sdk.Context) (contract.ValsetUpdate, erro
case types.ValidatorModified:
return appendValidator(&r.Updated, valAddr)
case types.ValidatorSlashed:
// TODO: Add / send the infraction time
return slashValidator(&r.Slashed, valAddr, slashInfo.Power, slashInfo.InfractionHeight, 0,
return slashValidator(&r.Slashed, valAddr, slashInfo.Power, slashInfo.InfractionHeight, slashInfo.TimeInfraction.Unix(),
slashInfo.TotalSlashAmount, slashInfo.SlashFraction)
default:
innerErr = types.ErrInvalid.Wrapf("undefined operation type %X", op)
Expand Down Expand Up @@ -170,11 +171,13 @@ func (k Keeper) iteratePipedValsetOperations(ctx sdk.Context, cb func(valAddress
return types.ErrInvalid.Wrapf("invalid slash key length %d", len(key))
}
totalSlashAmountLen := key[addrLen+2+8+8]
slashFractionLen := key[addrLen+2+8+8+1+totalSlashAmountLen]
slashInfo = &types.SlashInfo{
Power: int64(sdk.BigEndianToUint64(key[addrLen+2 : addrLen+2+8])),
InfractionHeight: int64(sdk.BigEndianToUint64(key[addrLen+2+8 : addrLen+2+8+8])),
InfractionHeight: int64(sdk.BigEndianToUint64(key[addrLen+2 : addrLen+2+8])),
Power: int64(sdk.BigEndianToUint64(key[addrLen+2+8 : addrLen+2+8+8])),
DongLieu marked this conversation as resolved.
Show resolved Hide resolved
TotalSlashAmount: string(key[addrLen+2+8+8+1 : addrLen+2+8+8+1+totalSlashAmountLen]),
SlashFraction: string(key[addrLen+2+8+8+1+totalSlashAmountLen:]),
SlashFraction: string(key[addrLen+2+8+8+1+totalSlashAmountLen+1 : addrLen+2+8+8+1+totalSlashAmountLen+1+slashFractionLen]),
TimeInfraction: time.Unix(int64(sdk.BigEndianToUint64(key[addrLen+2+8+8+1+totalSlashAmountLen+1+slashFractionLen:addrLen+2+8+8+1+totalSlashAmountLen+1+slashFractionLen+8])), 0),
}
}
if cb(addr, types.PipedValsetOperation(op), slashInfo) {
Expand Down
32 changes: 32 additions & 0 deletions x/meshsecurity/keeper/valset_updates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
stdrand "math/rand"
"testing"
"time"

"github.com/cometbft/cometbft/libs/rand"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -232,3 +233,34 @@ func TestClearPipedValsetOperations(t *testing.T) {
// then
assert.Empty(t, FetchAllStoredOperations(t, ctx, k))
}

func TestSetAndGetScheduleSlashed(t *testing.T) {
DongLieu marked this conversation as resolved.
Show resolved Hide resolved
ctx, keepers := CreateDefaultTestInput(t)
k := keepers.MeshKeeper

slashInfo := &types.SlashInfo{
Power: 12,
InfractionHeight: 123,
TotalSlashAmount: sdk.NewInt(1000).String(),
SlashFraction: sdk.NewDec(1).String(),
TimeInfraction: time.Now(),
}
// set
err := k.sendAsync(ctx, types.ValidatorSlashed, rand.Bytes(address.Len), slashInfo)
require.NoError(t, err)
// get
var getSlash types.SlashInfo
err = k.iteratePipedValsetOperations(ctx, func(valAddress sdk.ValAddress, op types.PipedValsetOperation, slashInfo *types.SlashInfo) bool {
if op == types.ValidatorSlashed {
getSlash = *slashInfo
return true
}
return false
})
require.NoError(t, err)
// check
require.Equal(t, slashInfo.TimeInfraction.Unix(), getSlash.TimeInfraction.Unix())
require.Equal(t, slashInfo.Power, getSlash.Power)
require.Equal(t, slashInfo.InfractionHeight, getSlash.InfractionHeight)
require.Equal(t, slashInfo.TotalSlashAmount, getSlash.TotalSlashAmount)
}
14 changes: 12 additions & 2 deletions x/meshsecurity/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/binary"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
"time"
)

const (
Expand Down Expand Up @@ -51,6 +52,7 @@ type SlashInfo struct {
Power int64
TotalSlashAmount string
SlashFraction string
TimeInfraction time.Time
}

// BuildMaxCapLimitKey build max cap limit store key
Expand Down Expand Up @@ -100,7 +102,7 @@ func BuildPipedValsetOpKey(op PipedValsetOperation, val sdk.ValAddress, slashInf
if slashInfo == nil {
panic("slash info is nil")
}
sn = 8 + 8 + 1 + len(slashInfo.TotalSlashAmount) + len(slashInfo.SlashFraction) // 8 for height, 8 for power, +1 for total amount length
sn = 8 + 8 + 1 + len(slashInfo.TotalSlashAmount) + 1 + len(slashInfo.SlashFraction) + 8 // 8 for height, 8 for power, +1 for total amount length ,+1 for slash length, +8 for time
DongLieu marked this conversation as resolved.
Show resolved Hide resolved
}
r := make([]byte, pn+an+sn+1+1) // +1 for address prefix, +1 for op
copy(r, PipedValsetPrefix)
Expand All @@ -112,10 +114,18 @@ func BuildPipedValsetOpKey(op PipedValsetOperation, val sdk.ValAddress, slashInf
copy(r[pn+an+1+1:], b)
binary.BigEndian.PutUint64(b, uint64(slashInfo.Power))
copy(r[pn+an+1+1+8:], b)

tn := len(slashInfo.TotalSlashAmount)
r[pn+an+1+1+8+8] = byte(tn)
copy(r[pn+an+1+1+8+8+1:], slashInfo.TotalSlashAmount)
copy(r[pn+an+1+1+8+8+1+tn:], slashInfo.SlashFraction)

sn := len(slashInfo.SlashFraction)
r[pn+an+1+1+8+8+1+tn] = byte(sn)
copy(r[pn+an+1+1+8+8+1+tn+1:], slashInfo.SlashFraction)

timeUnix := slashInfo.TimeInfraction.Unix()
binary.BigEndian.PutUint64(b, uint64(timeUnix))
copy(r[pn+an+1+1+8+8+1+tn+1+sn:], b)
likesToEatFish marked this conversation as resolved.
Show resolved Hide resolved
}
return r
}
Loading