Skip to content

Commit

Permalink
chore: further cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mbreithecker committed Dec 23, 2024
1 parent 61f8090 commit d5fb6ca
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 15 deletions.
23 changes: 18 additions & 5 deletions app/upgrades/v2_0/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ func CreateUpgradeHandler(
// Run KYVE migrations
migrateProtocolStakers(sdkCtx, delegationKeeper, stakersKeeper, stakingKeeper, bankKeeper)

// TODO: migrate slash params, commission change queues

return migratedVersionMap, err
}
}
Expand All @@ -59,9 +57,13 @@ func migrateProtocolStakers(ctx sdk.Context, delegationKeeper delegationkeeper.K
// Process current unbonding queue
delegationKeeper.FullyProcessDelegatorUnbondingQueue(ctx)

validatorMapping := ValidatorMappingsMainnet
if ctx.ChainID() == "kaon-1" {
validatorMapping = ValidatorMappingsKaon
}

totalMigratedStake := uint64(0)
for _, mapping := range ValidatorMappings {
// TODO check if staker exists
for _, mapping := range validatorMapping {
delegators := delegationKeeper.GetDelegatorsByStaker(ctx, mapping.ProtocolAddress)

for _, delegator := range delegators {
Expand Down Expand Up @@ -101,5 +103,16 @@ func migrateProtocolStakers(ctx sdk.Context, delegationKeeper delegationkeeper.K
})
}

// TODO delete staker object
// Delete all legacy stakers objects
stakersKeeper.Migration_ResetOldState(ctx)

// Migrate Params
delegationParams := delegationKeeper.GetParams(ctx)
stakersParams := stakersKeeper.GetParams(ctx)

stakersParams.TimeoutSlash = delegationParams.TimeoutSlash
stakersParams.UploadSlash = delegationParams.UploadSlash
stakersParams.VoteSlash = delegationParams.VoteSlash

stakersKeeper.SetParams(ctx, stakersParams)
}
50 changes: 47 additions & 3 deletions app/upgrades/v2_0/validator_mapping.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,59 @@
package v2_0

/*
Protocol-Consensus Validator Linking:
1. Fill out the entry below following the example.
2. Send 1$KYVE from the protocol-address to the consensus-validator-operator address using the memo "Shared-Staking"
and put the tx-hash in Proof1.
3. Send 1$KYVE from the consensus-validator-operator address to the protocol address using the memo "Shared-Staking"
and put the tx-hash in Proof2.
4. Submit a Pull-Request to https://github.com/KYVENetwork/chain
*/

type ValidatorMapping struct {
Name string
ConsensusAddress string
ProtocolAddress string
Proof1 string
Proof2 string
}

var ValidatorMappingsMainnet = []ValidatorMapping{
{
// human-readable name, only used for logging
Name: "",
// kyvevaloper... address of the chain node
ConsensusAddress: "",
// kyve... address of the protocol node
ProtocolAddress: "",
// Proof TX-Hash 1, transferring 1 $KYVE from the protocol-address to the operator address
// using "Shared Staking" as memo.
Proof1: "",
// Proof TX-Hash 2, transferring 1 $KYVE from the operator address to the protocol-address
// using "Shared Staking" as memo.
Proof2: "",
},
}

var ValidatorMappings = []ValidatorMapping{
var ValidatorMappingsKaon = []ValidatorMapping{
{
Name: "",
// human-readable name, only used for logging
Name: "",
// kyvevaloper... address of the chain node
ConsensusAddress: "",
ProtocolAddress: "",
// kyve... address of the protocol node
ProtocolAddress: "",
// Proof TX-Hash 1, transferring 1 $KYVE from the protocol-address to the operator address
// using "Shared Staking" as memo.
Proof1: "",
// Proof TX-Hash 2, transferring 1 $KYVE from the operator address to the protocol-address
// using "Shared Staking" as memo.
Proof2: "",
},
}
6 changes: 3 additions & 3 deletions x/bundles/keeper/keeper_suite_zero_delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ var _ = Describe("zero delegation", Ordered, func() {
Expect(bundleProposal.NextUploader).To(Equal(i.STAKER_0))

// calculate voter slashes
fraction := s.App().DelegationKeeper.GetVoteSlash(s.Ctx())
fraction := s.App().StakersKeeper.GetVoteSlash(s.Ctx())
slashAmountVoter := uint64(math.LegacyNewDec(int64(0 * i.KYVE)).Mul(fraction).TruncateInt64())
Expect(s.App().StakersKeeper.GetDelegationAmountOfDelegator(s.Ctx(), i.STAKER_2, i.STAKER_2)).To(Equal(0*i.KYVE - slashAmountVoter))

Expand Down Expand Up @@ -615,7 +615,7 @@ var _ = Describe("zero delegation", Ordered, func() {
Expect(s.App().StakersKeeper.GetOutstandingRewards(s.Ctx(), i.STAKER_0, i.STAKER_0)).To(BeEmpty())

// calculate uploader slashes
fraction := s.App().DelegationKeeper.GetUploadSlash(s.Ctx())
fraction := s.App().StakersKeeper.GetUploadSlash(s.Ctx())
slashAmount := uint64(math.LegacyNewDec(int64(0 * i.KYVE)).Mul(fraction).TruncateInt64())

Expect(s.App().StakersKeeper.GetDelegationAmountOfDelegator(s.Ctx(), i.STAKER_0, i.STAKER_0)).To(Equal(0*i.KYVE - slashAmount))
Expand Down Expand Up @@ -753,7 +753,7 @@ var _ = Describe("zero delegation", Ordered, func() {
Expect(valaccountActive).To(BeFalse())

// check if voter got slashed
slashAmountRatio := s.App().DelegationKeeper.GetTimeoutSlash(s.Ctx())
slashAmountRatio := s.App().StakersKeeper.GetTimeoutSlash(s.Ctx())
expectedBalance := 0*i.KYVE - uint64(math.LegacyNewDec(int64(0*i.KYVE)).Mul(slashAmountRatio).TruncateInt64())

Expect(expectedBalance).To(Equal(s.App().StakersKeeper.GetDelegationAmountOfDelegator(s.Ctx(), i.STAKER_1, i.STAKER_1)))
Expand Down
1 change: 0 additions & 1 deletion x/delegation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
// InitGenesis initializes the capability module's state from a provided genesis
// state.
func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) {

}

// ExportGenesis returns the capability module's exported genesis.
Expand Down
56 changes: 56 additions & 0 deletions x/stakers/keeper/migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package keeper

import (
"cosmossdk.io/store/prefix"
storeTypes "cosmossdk.io/store/types"
"github.com/KYVENetwork/chain/x/stakers/types"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func (k Keeper) migration_RemoveBranch(ctx sdk.Context, keyPrefix []byte) {
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, keyPrefix)
iterator := storeTypes.KVStorePrefixIterator(store, []byte{})

defer iterator.Close()

keys := make([][]byte, 0)
for ; iterator.Valid(); iterator.Next() {
keys = append(keys, iterator.Key())
}

for _, key := range keys {
store.Delete(key)
}
}

func (k Keeper) Migration_ResetOldState(ctx sdk.Context) {
k.migration_RemoveBranch(ctx, types.StakerKeyPrefix)

k.migration_RemoveBranch(ctx, types.ValaccountPrefix)
k.migration_RemoveBranch(ctx, types.ValaccountPrefixIndex2)

k.migration_RemoveBranch(ctx, types.CommissionChangeEntryKeyPrefix)
k.migration_RemoveBranch(ctx, types.CommissionChangeEntryKeyPrefixIndex2)

k.migration_RemoveBranch(ctx, types.LeavePoolEntryKeyPrefix)
k.migration_RemoveBranch(ctx, types.LeavePoolEntryKeyPrefixIndex2)

k.migration_RemoveBranch(ctx, types.ActiveStakerIndex)

k.SetQueueState(ctx, types.QUEUE_IDENTIFIER_COMMISSION, types.QueueState{
LowIndex: 0,
HighIndex: 0,
})

k.SetQueueState(ctx, types.QUEUE_IDENTIFIER_LEAVE, types.QueueState{
LowIndex: 0,
HighIndex: 0,
})

k.SetQueueState(ctx, types.QUEUE_IDENTIFIER_STAKE_FRACTION, types.QueueState{
LowIndex: 0,
HighIndex: 0,
})
}
3 changes: 0 additions & 3 deletions x/stakers/types/keys.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package types

import (
"cosmossdk.io/math"
"github.com/KYVENetwork/chain/util"
)

Expand Down Expand Up @@ -68,8 +67,6 @@ var (

const MaxStakers = 50

var DefaultCommission = math.LegacyMustNewDecFromStr("0.1")

// StakerKey returns the store Key to retrieve a Staker from the index fields
func StakerKey(staker string) []byte {
return util.GetByteKey(staker)
Expand Down

0 comments on commit d5fb6ca

Please sign in to comment.