Skip to content

Commit

Permalink
use protobuf for storing slash acks on provider
Browse files Browse the repository at this point in the history
  • Loading branch information
MSalopek committed Sep 9, 2022
1 parent b61973f commit cfc5ce9
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 55 deletions.
6 changes: 6 additions & 0 deletions proto/interchain_security/ccv/provider/v1/provider.proto
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,9 @@ message HandshakeMetadata {
string provider_fee_pool_addr = 1;
string version = 2;
}

// SlashAcks contains addesses of consumer chain validators
// successfully slashed on the provider chain
message SlashAcks {
repeated string addresses = 1;
}
30 changes: 14 additions & 16 deletions x/ccv/provider/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,12 +584,15 @@ func (k Keeper) DeleteValsetUpdateBlockHeight(ctx sdk.Context, valsetUpdateId ui
// SetSlashAcks sets the slash acks under the given chain ID
func (k Keeper) SetSlashAcks(ctx sdk.Context, chainID string, acks []string) {
store := ctx.KVStore(k.storeKey)
buf := &bytes.Buffer{}
err := json.NewEncoder(buf).Encode(acks)

sa := types.SlashAcks{
Addresses: acks,
}
bz, err := sa.Marshal()
if err != nil {
panic("failed to encode json")
panic("failed to marshal SlashAcks")
}
store.Set(types.SlashAcksKey(chainID), buf.Bytes())
store.Set(types.SlashAcksKey(chainID), bz)
}

// GetSlashAcks returns the slash acks stored under the given chain ID
Expand All @@ -599,15 +602,12 @@ func (k Keeper) GetSlashAcks(ctx sdk.Context, chainID string) []string {
if bz == nil {
return nil
}
var acks []string
buf := bytes.NewBuffer(bz)

err := json.NewDecoder(buf).Decode(&acks)
if err != nil {
var acks types.SlashAcks
if err := acks.Unmarshal(bz); err != nil {
panic(fmt.Errorf("failed to decode json: %w", err))
}

return acks
return acks.GetAddresses()
}

// EmptySlashAcks empties and returns the slash acks for a given chain ID
Expand All @@ -631,15 +631,13 @@ func (k Keeper) IterateSlashAcks(ctx sdk.Context, cb func(chainID string, acks [

chainID := string(iterator.Key()[1:])

var data []string
buf := bytes.NewBuffer(iterator.Value())

err := json.NewDecoder(buf).Decode(&data)
var sa types.SlashAcks
err := sa.Unmarshal(iterator.Value())
if err != nil {
panic(fmt.Errorf("failed to decode json: %w", err))
panic(fmt.Errorf("failed to unmarshal SlashAcks: %w", err))
}

if !cb(chainID, data) {
if !cb(chainID, sa.GetAddresses()) {
return
}
}
Expand Down
255 changes: 216 additions & 39 deletions x/ccv/provider/types/provider.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cfc5ce9

Please sign in to comment.