Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
insumity committed Jan 31, 2024
1 parent cca3e98 commit ca4d558
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 107 deletions.
5 changes: 5 additions & 0 deletions proto/interchain_security/ccv/provider/v1/provider.proto
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ message ConsumerAdditionProposal {
// chain. it is most relevant for chains performing a sovereign to consumer
// changeover in order to maintain the existing ibc transfer channel
string distribution_transmission_channel = 14;
// Corresponds to the percentage of validators that join under the Top N case.
// For example, 53 corresponds to a Top 53% chain, meaning that the top 53% provider validators
// have to validate the proposed consumer chain. top_N can be 0 or include any value in [50, 100].
// A chain can join with top_N == 0 as an Opt In, or with top_N ∈ [50, 100] as a Top N chain.
uint32 top_N = 15;
}

// ConsumerRemovalProposal is a governance proposal on the provider chain to
Expand Down
38 changes: 38 additions & 0 deletions x/ccv/provider/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -1136,3 +1136,41 @@ func (k Keeper) GetAllRegisteredAndProposedChainIDs(ctx sdk.Context) []string {

return allConsumerChains
}

// SetTopN stores the N value associated to chain with `chainID`
func (k Keeper) SetTopN(
ctx sdk.Context,
chainID string,
N uint32,
) {
store := ctx.KVStore(k.storeKey)

buf := make([]byte, 4)
binary.BigEndian.PutUint32(buf, N)

store.Set(types.TopNKey(chainID), buf)
}

// GetTopN returns the N associated to chain with `chainID`
func (k Keeper) GetTopN(
ctx sdk.Context,
chainID string,
) (uint32, bool) {
store := ctx.KVStore(k.storeKey)
buf := store.Get(types.TopNKey(chainID))
if buf == nil {
return 0, false
} else {
}
return binary.BigEndian.Uint32(buf), true
}

// IsTopN returns true if chain with `chainID` is a Top N chain (i.e., enforces at least one validator to validate chain `chainID`)
func (k Keeper) IsTopN(ctx sdk.Context, chainID string) bool {
return k.GetTopN(ctx, chainID) > 0
}

// IsOptIn returns true if chain with `chainID` is an Opt In chain (i.e., no validator is forced to validate chain `chainID`)
func (k Keeper) IsOptIn(ctx sdk.Context, chainID string) bool {
return k.GetTopN(ctx, chainID) == 0
}
7 changes: 7 additions & 0 deletions x/ccv/provider/keeper/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ func (k Keeper) HandleConsumerAdditionProposal(ctx sdk.Context, p *types.Consume
return err
}

// you should not be able
if _, found := k.GetTopN(ctx, p.ChainId); found {
return errorsmod.Wrap(types.ErrDuplicateConsumerChain,
fmt.Sprint("cannot add a chain "))
}
k.SetTopN(ctx, p.ChainId, p.Top_N)

k.SetPendingConsumerAdditionProp(ctx, p)

k.Logger(ctx).Info("consumer addition proposal enqueued",
Expand Down
8 changes: 8 additions & 0 deletions x/ccv/provider/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ const (
// ProposedConsumerChainByteKey is the byte prefix storing the consumer chainId in consumerAddition gov proposal submitted before voting finishes
ProposedConsumerChainByteKey

// TopNBytePrefix is the byte prefix storing the mapping from a consumer chain to the N value of this chain,
// that corresponds to the N% of the top validators that have to validate this consumer chain
TopNBytePrefix
// NOTE: DO NOT ADD NEW BYTE PREFIXES HERE WITHOUT ADDING THEM TO getAllKeyPrefixes() IN keys_test.go
)

Expand Down Expand Up @@ -517,6 +520,11 @@ func ParseProposedConsumerChainKey(prefix byte, bz []byte) (uint64, error) {
return proposalID, nil
}

// TopNKey returns the key of consumer chain `chainID`
func TopNKey(chainID string) []byte {
return ChainIdWithLenKey(TopNBytePrefix, chainID)
}

//
// End of generic helpers section
//
1 change: 1 addition & 0 deletions x/ccv/provider/types/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func getAllKeyPrefixes() []byte {
providertypes.VSCMaturedHandledThisBlockBytePrefix,
providertypes.EquivocationEvidenceMinHeightBytePrefix,
providertypes.ProposedConsumerChainByteKey,
providertypes.TopNBytePrefix,
}
}

Expand Down
6 changes: 6 additions & 0 deletions x/ccv/provider/types/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ func (cccp *ConsumerAdditionProposal) ValidateBasic() error {
return errorsmod.Wrap(ErrInvalidConsumerAdditionProposal, "unbonding period cannot be zero")
}

// Top N corresponds to the top N% of validators that have to validate the consumer chain and can only be 0 (for an
// Opt In chain) or in the range [50, 100] (for a Top N chain).
if cccp.Top_N != 0 && cccp.Top_N < 50 || cccp.Top_N > 100 {
return errorsmod.Wrap(ErrInvalidConsumerAdditionProposal, "Top N can either be 0 or in the range [50, 100]")
}

return nil
}

Expand Down
Loading

0 comments on commit ca4d558

Please sign in to comment.