Skip to content

Commit

Permalink
Merge pull request #3 from rarimo/chains/mainnet-v1.0.7
Browse files Browse the repository at this point in the history
Upgrade v1.0.7
  • Loading branch information
olegfomenko authored Oct 14, 2023
2 parents 3684665 + c1bc9f7 commit daca5e6
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 7 deletions.
7 changes: 7 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,13 @@ func New(
},
)

app.UpgradeKeeper.SetUpgradeHandler(
"v1.0.7",
func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
return app.mm.RunMigrations(ctx, app.configurator, fromVM)
},
)

if loadLatest {
if err := app.LoadLatestVersion(); err != nil {
tmos.Exit(err.Error())
Expand Down
4 changes: 2 additions & 2 deletions x/rarimocore/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (k Keeper) EndBlocker(ctx sdk.Context) {

party.Status = types.PartyStatus_Slashed
party.FreezeEndBlock = 0

k.SetParams(ctx, param)
}

k.SetParams(ctx, param)
}
9 changes: 9 additions & 0 deletions x/rarimocore/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
v3 "github.com/rarimo/rarimo-core/x/rarimocore/migrations/v3"
v4 "github.com/rarimo/rarimo-core/x/rarimocore/migrations/v4"
)

type Migrator struct {
Expand All @@ -22,3 +23,11 @@ func (m Migrator) Migrate2to3(ctx sdk.Context) error {

return nil
}

func (m Migrator) Migrate3to4(ctx sdk.Context) error {
if err := v4.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc); err != nil {
return err
}

return nil
}
35 changes: 31 additions & 4 deletions x/rarimocore/keeper/msg_server_report_violation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"
"strconv"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -58,14 +59,14 @@ func (t msgServer) ReportViolation(goCtx context.Context, msg *types.MsgCreateVi
alreadyIncremented = alreadyIncremented || (sessionReported == msg.SessionId)
}

// If party was not incremented then increment.
// If violations reaches threshold then party becomes frozen.
// Trying to apply new violation if was not applied yet.
if !alreadyIncremented {
// Check that already have enough reports to confirm violation
if uint64(len(reports)) >= params.Threshold {
party.ViolationsCount++
party.ReportedSessions = append(party.ReportedSessions, msg.SessionId)
confirmViolation(party, msg.SessionId)
}

// If violations reaches threshold then party becomes frozen.
if party.ViolationsCount == params.MaxViolationsCount {
party.Status = types.PartyStatus_Frozen
party.FreezeEndBlock = uint64(ctx.BlockHeight()) + params.FreezeBlocksPeriod
Expand All @@ -89,3 +90,29 @@ func (t msgServer) ReportViolation(goCtx context.Context, msg *types.MsgCreateVi

return &types.MsgCreateViolationReportResponse{}, nil
}

// confirmViolation updates party entry with:
// 1. increments violations counter
// 2. adds new sessionId to the reported sessions
// 3. removes old reports with corresponding decreasing of counter
func confirmViolation(party *types.Party, sessionId string) {
party.ReportedSessions = append(party.ReportedSessions, sessionId)

// we are assuming that session ids is an integer values encoded into string
sessionIdInt, _ := strconv.Atoi(sessionId)

// dropReportSessionDelta is an amount of sessions when report is considered to old and can be removed
const dropReportSessionDelta = 100

actualReportedSessions := make([]string, 0, len(party.ReportedSessions))

for _, ssi := range party.ReportedSessions {
ssiInt, _ := strconv.Atoi(ssi)
if ssiInt+dropReportSessionDelta > sessionIdInt {
actualReportedSessions = append(actualReportedSessions, ssi)
}
}

party.ReportedSessions = actualReportedSessions
party.ViolationsCount = uint64(len(party.ReportedSessions))
}
33 changes: 33 additions & 0 deletions x/rarimocore/migrations/v4/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package v4

import (
"fmt"

"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/rarimo/rarimo-core/x/rarimocore/types"
)

func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) error {
ctx.Logger().Info(fmt.Sprintf("Performing v0.0.7 %s module migrations", types.ModuleName))

params := types.Params{}
b := ctx.KVStore(storeKey).Get(types.KeyPrefix(types.ParamsKey))
cdc.MustUnmarshal(b, &params)

// Fixing the issue with unexpected slashing 1 of 2 active partis due to
// saving all violations without expiration.
for _, party := range params.Parties {
party.ViolationsCount = 0
party.ReportedSessions = []string{}
party.Status = types.PartyStatus_Active
party.FreezeEndBlock = 0
}

params.IsUpdateRequired = false

b = cdc.MustMarshal(&params)
ctx.KVStore(storeKey).Set(types.KeyPrefix(types.ParamsKey), b)
return nil
}
6 changes: 5 additions & 1 deletion x/rarimocore/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
if err := cfg.RegisterMigration(types.ModuleName, 2, am.migrator.Migrate2to3); err != nil {
panic(err)
}

if err := cfg.RegisterMigration(types.ModuleName, 3, am.migrator.Migrate3to4); err != nil {
panic(err)
}
}

// RegisterInvariants registers the capability module's invariants.
Expand All @@ -169,7 +173,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion implements ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 3 }
func (AppModule) ConsensusVersion() uint64 { return 4 }

// BeginBlock executes all ABCI BeginBlock logic respective to the capability module.
func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}
Expand Down

0 comments on commit daca5e6

Please sign in to comment.