Skip to content

Commit

Permalink
fix key and add timeInfraction
Browse files Browse the repository at this point in the history
  • Loading branch information
likesToEatFish committed Jul 3, 2024
1 parent 3cdce6e commit 61ced87
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
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])),
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) {
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
}
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)
}
return r
}

0 comments on commit 61ced87

Please sign in to comment.