-
Notifications
You must be signed in to change notification settings - Fork 135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat!: compute partial set #1642
Changes from 14 commits
71ac8e3
61a4052
4bc4887
f5c3af1
de1ce67
8710bc2
a3e3d41
6fa64fa
20f9625
7a2b2d9
000760a
272b87b
2bff04a
5348f83
7144504
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1185,21 +1185,26 @@ func (k Keeper) IsOptIn(ctx sdk.Context, chainID string) bool { | |
return !found || topN == 0 | ||
} | ||
|
||
// SetOptedIn sets validator `providerAddr` as opted in with the given `blockHeight` and `power` | ||
func (k Keeper) SetOptedIn( | ||
ctx sdk.Context, | ||
chainID string, | ||
providerAddr types.ProviderConsAddress, | ||
blockHeight uint64, | ||
power uint64, | ||
) { | ||
store := ctx.KVStore(k.storeKey) | ||
|
||
// validator is considered opted in | ||
blockHeightBytes := make([]byte, 8) | ||
binary.BigEndian.PutUint64(blockHeightBytes, blockHeight) | ||
|
||
store.Set(types.OptedInKey(chainID, providerAddr), blockHeightBytes) | ||
powerBytes := make([]byte, 8) | ||
binary.BigEndian.PutUint64(powerBytes, power) | ||
|
||
store.Set(types.OptedInKey(chainID, providerAddr), append(blockHeightBytes, powerBytes...)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Store There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the recommendation here to define it in proto to simplify our life later on when we implement queries that return opted-in validators? |
||
} | ||
|
||
// DeleteOptedIn deletes opted-in validator `providerAddr` | ||
func (k Keeper) DeleteOptedIn( | ||
ctx sdk.Context, | ||
chainID string, | ||
|
@@ -1209,6 +1214,25 @@ func (k Keeper) DeleteOptedIn( | |
store.Delete(types.OptedInKey(chainID, providerAddr)) | ||
} | ||
|
||
// DeleteAllOptedIn deletes all opted-in validators | ||
func (k Keeper) DeleteAllOptedIn( | ||
insumity marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ctx sdk.Context, | ||
chainID string) { | ||
store := ctx.KVStore(k.storeKey) | ||
key := types.ChainIdWithLenKey(types.OptedInBytePrefix, chainID) | ||
iterator := sdk.KVStorePrefixIterator(store, key) | ||
|
||
var keysToDel [][]byte | ||
defer iterator.Close() | ||
for ; iterator.Valid(); iterator.Next() { | ||
keysToDel = append(keysToDel, iterator.Key()) | ||
} | ||
for _, delKey := range keysToDel { | ||
store.Delete(delKey) | ||
} | ||
} | ||
|
||
// IsOptedIn returns `true` if the validator with `providerAddr` is opted in and `false` otherwise | ||
func (k Keeper) IsOptedIn( | ||
ctx sdk.Context, | ||
chainID string, | ||
|
@@ -1218,6 +1242,7 @@ func (k Keeper) IsOptedIn( | |
return store.Get(types.OptedInKey(chainID, providerAddr)) != nil | ||
} | ||
|
||
// GetOptedIn returns all the opted-in validators on chain `chainID` | ||
func (k Keeper) GetOptedIn( | ||
ctx sdk.Context, | ||
chainID string) (optedInValidators []OptedInValidator) { | ||
|
@@ -1229,13 +1254,15 @@ func (k Keeper) GetOptedIn( | |
for ; iterator.Valid(); iterator.Next() { | ||
optedInValidators = append(optedInValidators, OptedInValidator{ | ||
ProviderAddr: types.NewProviderConsAddress(iterator.Key()[len(key):]), | ||
BlockHeight: binary.BigEndian.Uint64(iterator.Value()), | ||
BlockHeight: binary.BigEndian.Uint64(iterator.Value()[0:8]), | ||
Power: binary.BigEndian.Uint64(iterator.Value()[8:]), | ||
}) | ||
} | ||
|
||
return optedInValidators | ||
} | ||
|
||
// SetToBeOptedIn sets validator `providerAddr` as to be opted in | ||
func (k Keeper) SetToBeOptedIn( | ||
ctx sdk.Context, | ||
chainID string, | ||
|
@@ -1245,6 +1272,7 @@ func (k Keeper) SetToBeOptedIn( | |
store.Set(types.ToBeOptedInKey(chainID, providerAddr), []byte{}) | ||
} | ||
|
||
// DeleteToBeOptedIn deletes to-be-opted-in validator `providerAddr` | ||
func (k Keeper) DeleteToBeOptedIn( | ||
ctx sdk.Context, | ||
chainID string, | ||
|
@@ -1254,6 +1282,25 @@ func (k Keeper) DeleteToBeOptedIn( | |
store.Delete(types.ToBeOptedInKey(chainID, providerAddr)) | ||
} | ||
|
||
// DeleteAllToBeOptedIn deletes all to-be-opted-in validators | ||
func (k Keeper) DeleteAllToBeOptedIn( | ||
ctx sdk.Context, | ||
chainID string) { | ||
store := ctx.KVStore(k.storeKey) | ||
key := types.ChainIdWithLenKey(types.ToBeOptedInBytePrefix, chainID) | ||
iterator := sdk.KVStorePrefixIterator(store, key) | ||
|
||
var keysToDel [][]byte | ||
defer iterator.Close() | ||
for ; iterator.Valid(); iterator.Next() { | ||
keysToDel = append(keysToDel, iterator.Key()) | ||
} | ||
for _, delKey := range keysToDel { | ||
store.Delete(delKey) | ||
} | ||
} | ||
|
||
// IsToBeOptedIn returns `true` if the validator with `providerAddr` is to be opted in and `false` otherwise | ||
func (k Keeper) IsToBeOptedIn( | ||
ctx sdk.Context, | ||
chainID string, | ||
|
@@ -1263,6 +1310,7 @@ func (k Keeper) IsToBeOptedIn( | |
return store.Get(types.ToBeOptedInKey(chainID, providerAddr)) != nil | ||
} | ||
|
||
// GetToBeOptedIn returns all the to-be-opted-in validators on chain `chainID` | ||
func (k Keeper) GetToBeOptedIn( | ||
ctx sdk.Context, | ||
chainID string) (addresses []types.ProviderConsAddress) { | ||
|
@@ -1280,6 +1328,7 @@ func (k Keeper) GetToBeOptedIn( | |
return addresses | ||
} | ||
|
||
// SetToBeOptedOut sets validator `providerAddr` as to be opted out | ||
func (k Keeper) SetToBeOptedOut( | ||
ctx sdk.Context, | ||
chainID string, | ||
|
@@ -1289,6 +1338,7 @@ func (k Keeper) SetToBeOptedOut( | |
store.Set(types.ToBeOptedOutKey(chainID, providerAddr), []byte{}) | ||
} | ||
|
||
// DeleteToBeOptedOut deletes to-be-opted-out validator `providerAddr` | ||
func (k Keeper) DeleteToBeOptedOut( | ||
ctx sdk.Context, | ||
chainID string, | ||
|
@@ -1298,6 +1348,25 @@ func (k Keeper) DeleteToBeOptedOut( | |
store.Delete(types.ToBeOptedOutKey(chainID, providerAddr)) | ||
} | ||
|
||
// DeleteAllToBeOptedOut deletes all to-be-opted-out validators | ||
func (k Keeper) DeleteAllToBeOptedOut( | ||
ctx sdk.Context, | ||
chainID string) { | ||
store := ctx.KVStore(k.storeKey) | ||
key := types.ChainIdWithLenKey(types.ToBeOptedOutBytePrefix, chainID) | ||
iterator := sdk.KVStorePrefixIterator(store, key) | ||
|
||
var keysToDel [][]byte | ||
defer iterator.Close() | ||
for ; iterator.Valid(); iterator.Next() { | ||
keysToDel = append(keysToDel, iterator.Key()) | ||
} | ||
for _, delKey := range keysToDel { | ||
store.Delete(delKey) | ||
} | ||
} | ||
|
||
// IsToBeOptedOut returns `true` if the validator with `providerAddr` is to be opted out and `false` otherwise | ||
func (k Keeper) IsToBeOptedOut( | ||
ctx sdk.Context, | ||
chainID string, | ||
|
@@ -1307,6 +1376,7 @@ func (k Keeper) IsToBeOptedOut( | |
return store.Get(types.ToBeOptedOutKey(chainID, providerAddr)) != nil | ||
} | ||
|
||
// GetToBeOptedOut returns all the to-be-opted-out validators on chain `chainID` | ||
func (k Keeper) GetToBeOptedOut( | ||
ctx sdk.Context, | ||
chainID string) (addresses []types.ProviderConsAddress) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -139,19 +139,30 @@ func (k msgServer) SubmitConsumerDoubleVoting(goCtx context.Context, msg *types. | |
func (k msgServer) OptIn(goCtx context.Context, msg *types.MsgOptIn) (*types.MsgOptInResponse, error) { | ||
ctx := sdk.UnwrapSDKContext(goCtx) | ||
|
||
valAddress, err := sdk.ConsAddressFromBech32(msg.ProviderAddr) | ||
providerValidatorAddr, err := sdk.ValAddressFromBech32(msg.ProviderAddr) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed this. Similarly to |
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// validator must already be registered | ||
validator, found := k.stakingKeeper.GetValidator(ctx, providerValidatorAddr) | ||
if !found { | ||
return nil, stakingtypes.ErrNoValidatorFound | ||
} | ||
|
||
consAddress, err := validator.GetConsAddr() | ||
if err != nil { | ||
return nil, err | ||
} | ||
providerAddr := types.NewProviderConsAddress(valAddress) | ||
providerConsAddr := types.NewProviderConsAddress(consAddress) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if msg.ConsumerKey != "" { | ||
err = k.Keeper.HandleOptIn(ctx, msg.ChainId, providerAddr, &msg.ConsumerKey) | ||
err = k.Keeper.HandleOptIn(ctx, msg.ChainId, providerConsAddr, &msg.ConsumerKey) | ||
} else { | ||
err = k.Keeper.HandleOptIn(ctx, msg.ChainId, providerAddr, nil) | ||
err = k.Keeper.HandleOptIn(ctx, msg.ChainId, providerConsAddr, nil) | ||
} | ||
|
||
if err != nil { | ||
|
@@ -172,16 +183,27 @@ func (k msgServer) OptIn(goCtx context.Context, msg *types.MsgOptIn) (*types.Msg | |
func (k msgServer) OptOut(goCtx context.Context, msg *types.MsgOptOut) (*types.MsgOptOutResponse, error) { | ||
ctx := sdk.UnwrapSDKContext(goCtx) | ||
|
||
valAddress, err := sdk.ConsAddressFromBech32(msg.ProviderAddr) | ||
providerValidatorAddr, err := sdk.ValAddressFromBech32(msg.ProviderAddr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// validator must already be registered | ||
validator, found := k.stakingKeeper.GetValidator(ctx, providerValidatorAddr) | ||
if !found { | ||
return nil, stakingtypes.ErrNoValidatorFound | ||
} | ||
|
||
consAddress, err := validator.GetConsAddr() | ||
if err != nil { | ||
return nil, err | ||
} | ||
providerAddr := types.NewProviderConsAddress(valAddress) | ||
providerConsAddr := types.NewProviderConsAddress(consAddress) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = k.Keeper.HandleOptOut(ctx, msg.ChainId, providerAddr) | ||
err = k.Keeper.HandleOptOut(ctx, msg.ChainId, providerConsAddr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Introduce
power
in the opted-in state so we can see if we should send aValidatorUpdate
down to CometBFT based on whether the power changed since last time it was sent. Only makes sense in an epoch-based setting because in a non-epoch-based setting, we could use the validator updates from thestaking
module.