Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
insumity committed Jan 30, 2024
1 parent cc81c45 commit dc00d7c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 42 deletions.
4 changes: 2 additions & 2 deletions x/ccv/provider/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -1168,15 +1168,15 @@ func (k Keeper) GetTopN(ctx sdk.Context, chainID string) (uint8, bool) {
return bz[1], true
}

func (k Keeper) AddOptedIn(ctx sdk.Context, chainID string, providerAddr types.ProviderConsAddress) {
func (k Keeper) AddToBeOptedIn(ctx sdk.Context, chainID string, providerAddr types.ProviderConsAddress) {
store := ctx.KVStore(k.storeKey)

heightBytes := make([]byte, 8)
binary.BigEndian.PutUint64(heightBytes, uint64(ctx.BlockHeight()))
store.Set(types.TopNOptedIn(chainID, providerAddr), heightBytes)
}

func (k Keeper) RemoveOptedIn(ctx sdk.Context, chainID string, providerAddr types.ProviderConsAddress) {
func (k Keeper) RemoveToBeOptedIn(ctx sdk.Context, chainID string, providerAddr types.ProviderConsAddress) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.TopNOptedIn(chainID, providerAddr))
}
Expand Down
8 changes: 4 additions & 4 deletions x/ccv/provider/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,21 +639,21 @@ func TestSomething(t *testing.T) {
sth = append(sth, byte(5))
sth = append(sth, byte(21))

providerKeeper.AddOptedIn(ctx, "chainId", types.NewProviderConsAddress(sth))
providerKeeper.AddToBeOptedIn(ctx, "chainId", types.NewProviderConsAddress(sth))

sth2 := make([]byte, 0)
sth2 = append(sth2, byte(9))
sth2 = append(sth2, byte(3))
sth2 = append(sth2, byte(6))
sth2 = append(sth2, byte(17))
providerKeeper.AddOptedIn(ctx, "chainId", types.NewProviderConsAddress(sth2))
providerKeeper.AddToBeOptedIn(ctx, "chainId", types.NewProviderConsAddress(sth2))

sth3 := []byte{234, 12, 3, 9, 123, 23, 4, 23, 14, 234}
providerKeeper.AddOptedIn(ctx, "chainId", types.NewProviderConsAddress(sth3))
providerKeeper.AddToBeOptedIn(ctx, "chainId", types.NewProviderConsAddress(sth3))

fmt.Println(providerKeeper.GetOptedIn(ctx, "chainId"))

providerKeeper.RemoveOptedIn(ctx, "chainId", types.NewProviderConsAddress(sth3))
providerKeeper.RemoveToBeOptedIn(ctx, "chainId", types.NewProviderConsAddress(sth3))
fmt.Println(providerKeeper.GetOptedIn(ctx, "chainId"))

//require.True(t, found)
Expand Down
4 changes: 2 additions & 2 deletions x/ccv/provider/keeper/partial_set_security.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (k Keeper) HandleOptIn(ctx sdk.Context, msg types.MsgOptIn) error {
return stakingtypes.ErrNoValidatorFound
}

k.AddOptedIn(ctx, chainId, types.ProviderConsAddress{providerValidatorAddr.Bytes()})
k.AddToBeOptedIn(ctx, chainId, types.ProviderConsAddress{providerValidatorAddr.Bytes()})

// TODO: (think) validator might not be active ... but that's oky ...

Expand Down Expand Up @@ -64,7 +64,7 @@ func (k Keeper) HandleOptOut(ctx sdk.Context, msg types.MsgOptOut) error {
return stakingtypes.ErrNoValidatorFound
}

k.RemoveOptedIn(ctx, chainId, types.ProviderConsAddress{providerValidatorAddr.Bytes()})
k.RemoveToBeOptedIn(ctx, chainId, types.ProviderConsAddress{providerValidatorAddr.Bytes()})

return nil
}
61 changes: 27 additions & 34 deletions x/ccv/provider/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"fmt"
"github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/proto/tendermint/crypto"
"strconv"

clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
Expand Down Expand Up @@ -243,44 +244,32 @@ func (k Keeper) SendVSCPacketsToChain(ctx sdk.Context, chainID, channelID string
}

// this would give you the new set of validator updates ...
func (k Keeper) GetPartialValidatorSet(ctx sdk.Context, chainId string, prevOptedInValidatorSet []types.ValidatorUpdate, valUpdates []types.ValidatorUpdate) (newUpdates []types.ValidatorUpdate) {
newOptedInValidatorSet := make([]types.ValidatorUpdate, len(prevOptedInValidatorSet))
elementCopied := copy(newOptedInValidatorSet, prevOptedInValidatorSet)
// assert elementCopied


newValUpdates := make([]types.ValidatorUpdate, 0)

for _, valUpdate := range valUpdates {
if valUpdate in newOptedInValidatorSet {
// change it's power
}
func (k Keeper) GetPartialValidatorSet(ctx sdk.Context, chainId string, prevOptedInValidatorSet map[crypto.PublicKey]bool,
optedInValidators []types.ValidatorUpdate, optedOutValidators []types.ValidatorUpdate, stakingValUpdates []types.ValidatorUpdate) (newUpdates []types.ValidatorUpdate) {

for _, val := range optedInValidators {
newUpdates = append(newUpdates, types.ValidatorUpdate{
PubKey: val.PubKey,
Power: val.Power,
})
}

// remove opted out from the upcoming set
for _, val := range k.GetOptedOutValidators(ctx, chainId) {
remove val from newOptedInValidatorSet
// add val with power 0
newValUpdates := { }
for _, val := range optedOutValidators {
newUpdates = append(newUpdates, types.ValidatorUpdate{
PubKey: val.PubKey,
Power: 0,
})
}

// introduce the opted in validators
for _, val := range k.GetOptedInValidators(ctx, chainId) {
add val to newVAlUpdates
// WITH WHAT POWER?
// power either in the val updates or not ...
// if in valUpdates you're good
// otherwise, from k.stakingKeeper.GetValidator() you get the right validator and get the power
for _, val := range stakingValUpdates {
if found := prevOptedInValidatorSet[val.PubKey]; found {
newUpdates = append(newUpdates, types.ValidatorUpdate{
PubKey: val.PubKey,
Power: val.Power,
})
}
}

//// clean all the opted Out
//for _, val := range valUpdates {
// newUpdates = append(newUpdates, types.ValidatorUpdate{
// PubKey: val.PubKey,
// Power: val.Power,
// })
//}

return newUpdates
}

Expand All @@ -296,8 +285,12 @@ func (k Keeper) QueueVSCPackets(ctx sdk.Context) {

for _, chain := range k.GetAllConsumerChains(ctx) {
prevSentOptedInValUpdated := k.GetPrevSentOptedInValUpdated(ctx, chain.ChainId)
// returns valUpdates with the provider public keys ...
valUpdates = k.GetPartialValidatorSet(ctx, chain.ChainId, prevSentOptedInValUpdated, valUpdates)
// transform to a prevOptedInValidatorSet map[crypto.PublicKey]bool map

// find the ones opted In ... should be toOptIn .. somewhere
// find the ones to be opted Out should be toOptOut somewhere

valUpdates = k.GetPartialValidatorSet(ctx, chain.ChainId, prevSentOptedInValUpdated, optedInValUpdates, optedOutValUpdats, valUpdates)
k.SetPrevSentOptedInValUpdated(ctx, chain.ChainId, prevSentOptedInValUpdated)

// Apply the key assignment to the validator updates.
Expand Down

0 comments on commit dc00d7c

Please sign in to comment.