From 61ced87bbb3238fe01b3c4df5d0b5fb877d43fd2 Mon Sep 17 00:00:00 2001 From: Likes To Eat Fish Date: Wed, 3 Jul 2024 12:16:56 +0700 Subject: [PATCH] fix key and add timeInfraction --- x/meshsecurity/keeper/valset_updates.go | 15 +++++---- x/meshsecurity/keeper/valset_updates_test.go | 32 ++++++++++++++++++++ x/meshsecurity/types/keys.go | 14 +++++++-- 3 files changed, 53 insertions(+), 8 deletions(-) diff --git a/x/meshsecurity/keeper/valset_updates.go b/x/meshsecurity/keeper/valset_updates.go index 50101d1a..80c7f509 100644 --- a/x/meshsecurity/keeper/valset_updates.go +++ b/x/meshsecurity/keeper/valset_updates.go @@ -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" @@ -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) } @@ -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) @@ -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) { diff --git a/x/meshsecurity/keeper/valset_updates_test.go b/x/meshsecurity/keeper/valset_updates_test.go index 5d794593..01352810 100644 --- a/x/meshsecurity/keeper/valset_updates_test.go +++ b/x/meshsecurity/keeper/valset_updates_test.go @@ -4,6 +4,7 @@ import ( "bytes" stdrand "math/rand" "testing" + "time" "github.com/cometbft/cometbft/libs/rand" "github.com/stretchr/testify/assert" @@ -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) +} diff --git a/x/meshsecurity/types/keys.go b/x/meshsecurity/types/keys.go index fa21f06c..73e7b54a 100644 --- a/x/meshsecurity/types/keys.go +++ b/x/meshsecurity/types/keys.go @@ -4,6 +4,7 @@ import ( "encoding/binary" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/address" + "time" ) const ( @@ -51,6 +52,7 @@ type SlashInfo struct { Power int64 TotalSlashAmount string SlashFraction string + TimeInfraction time.Time } // BuildMaxCapLimitKey build max cap limit store key @@ -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) @@ -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 }