Skip to content

Commit

Permalink
parameterizing average block time (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
ap0calypse644 authored Oct 30, 2024
1 parent 5f24e5c commit 6d98dcf
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 56 deletions.
93 changes: 74 additions & 19 deletions api/fairyring/keyshare/params.pulsar.go

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

2 changes: 1 addition & 1 deletion proto/fairyring/keyshare/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ message Params {
repeated string trusted_addresses = 4 [(gogoproto.moretags) = "yaml:\"trusted_addresses\""];
bytes slash_fraction_no_keyshare = 5 [(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec", (gogoproto.nullable) = false, (gogoproto.moretags) = "yaml:\"slash_fraction_no_keyshare\""];
bytes slash_fraction_wrong_keyshare = 6 [(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec", (gogoproto.nullable) = false, (gogoproto.moretags) = "yaml:\"slash_fraction_wrong_keyshare\""];

float avg_block_time = 7 [(gogoproto.moretags) = "yaml:\"avg_block_time\""];
}
3 changes: 3 additions & 0 deletions scripts/tests/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ sed -i -e 's/"trusted_counter_parties": \[\]/"trusted_counter_parties": \['"$TRU
sed -i -e 's/"key_expiry": "100"/"key_expiry": "10000"/g' $CHAIN_DIR/$CHAINID_1/config/genesis.json
sed -i -e 's/"key_expiry": "100"/"key_expiry": "10000"/g' $CHAIN_DIR/$CHAINID_2/config/genesis.json

sed -i -e 's/"avg_block_time": 5.6/"avg_block_time": 1.12/g' $CHAIN_DIR/$CHAINID_1/config/genesis.json
sed -i -e 's/"avg_block_time": 5.6/"avg_block_time": 1.12/g' $CHAIN_DIR/$CHAINID_2/config/genesis.json

sed -i -e 's/"is_source_chain": false/"is_source_chain": true/g' $CHAIN_DIR/$CHAINID_1/config/genesis.json

jsonData2=$(cat "$CHAIN_DIR/$CHAINID_2/config/genesis.json")
Expand Down
5 changes: 5 additions & 0 deletions x/keyshare/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,8 @@ func (k Keeper) SlashFractionWrongKeyshare(ctx sdk.Context) (res math.LegacyDec)
func (k Keeper) MaxIdledBlock(ctx sdk.Context) (res uint64) {
return k.GetParams(ctx).MaxIdledBlock
}

// AvgBlockTime returns the MaxIdledBlock param
func (k Keeper) AvgBlockTime(ctx sdk.Context) (res float32) {
return k.GetParams(ctx).AvgBlockTime
}
6 changes: 4 additions & 2 deletions x/keyshare/keeper/process_queues.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func (k Keeper) ProcessPepRequestQueue(ctx sdk.Context) error {
continue
}
delay := req.EstimatedDelay
blockDelay := uint64(math.Ceil(delay.Seconds() / types.AvgBlockTime))
avgBlockTime := k.AvgBlockTime(ctx)
blockDelay := uint64(math.Ceil(delay.Seconds() / float64(avgBlockTime)))
currentHeight := uint64(ctx.BlockHeight())
executionHeight := currentHeight + blockDelay
if executionHeight > activePubkey.Expiry {
Expand Down Expand Up @@ -189,7 +190,8 @@ func (k Keeper) ProcessGovRequestQueue(ctx sdk.Context) error {
reqs := k.govKeeper.GetAllReqQueueEntry(ctx)
for _, req := range reqs {
delay := req.EstimatedDelay
blockDelay := uint64(math.Ceil(delay.Seconds() / types.AvgBlockTime))
avgBlockTime := k.AvgBlockTime(ctx)
blockDelay := uint64(math.Ceil(delay.Seconds() / float64(avgBlockTime)))

currentHeight := uint64(ctx.BlockHeight())
executionHeight := currentHeight + blockDelay
Expand Down
3 changes: 2 additions & 1 deletion x/keyshare/keeper/request_decryption_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ func (k Keeper) OnRecvRequestDecryptionKeyPacket(
}

delay := data.EstimatedDelay
blockDelay := uint64(math.Ceil(delay.Seconds() / types.AvgBlockTime))
avgBlockTime := k.AvgBlockTime(ctx)
blockDelay := uint64(math.Ceil(delay.Seconds() / float64(avgBlockTime)))
currentHeight := uint64(ctx.BlockHeight())
executionHeight := currentHeight + blockDelay
if executionHeight > activePubkey.Expiry {
Expand Down
4 changes: 3 additions & 1 deletion x/keyshare/migrations/v2/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ func MigrateStore(ctx sdk.Context, storeService store.KVStoreService, cdc codec.
types.DefaultMinimumBonded,
types.DefaultSlashFractionNoKeyshare,
types.DefaultSlashFractionWrongKeyshare,
types.DefaultMaxIdledBlock)
types.DefaultMaxIdledBlock,
types.DefaultAvgBlockTime,
)

bz, err := cdc.Marshal(&currParams)
if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions x/keyshare/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,3 @@ const (
func KeyPrefix(p string) []byte {
return []byte(p)
}

const AvgBlockTime = 5.6
19 changes: 19 additions & 0 deletions x/keyshare/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ var (
DefaultMaxIdledBlock uint64 = 10
)

var (
KeyAvgBlockTime = []byte("KeyAvgBlockTime")
DefaultAvgBlockTime float32 = 5.6
)

// ParamKeyTable the param key table for launch module
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
Expand All @@ -54,6 +59,7 @@ func NewParams(
noKeyshareFraction math.LegacyDec,
wrongKeyshareFraction math.LegacyDec,
maxIdledBlock uint64,
avgBlockTime float32,
) Params {
return Params{
KeyExpiry: keyExp,
Expand All @@ -62,6 +68,7 @@ func NewParams(
SlashFractionWrongKeyshare: wrongKeyshareFraction,
MaxIdledBlock: maxIdledBlock,
MinimumBonded: minimumBonded,
AvgBlockTime: avgBlockTime,
}
}

Expand All @@ -74,6 +81,7 @@ func DefaultParams() Params {
DefaultSlashFractionNoKeyshare,
DefaultSlashFractionWrongKeyshare,
DefaultMaxIdledBlock,
DefaultAvgBlockTime,
)
}

Expand All @@ -86,6 +94,7 @@ func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
paramtypes.NewParamSetPair(KeySlashFractionNoKeyshare, &p.SlashFractionNoKeyshare, validateSlashFractionNoKeyshare),
paramtypes.NewParamSetPair(KeySlashFractionWrongKeyshare, &p.SlashFractionWrongKeyshare, validateSlashFractionWrongKeyshare),
paramtypes.NewParamSetPair(KeyMaxIdledBlock, &p.MaxIdledBlock, validateMaxIdledBlock),
paramtypes.NewParamSetPair(KeyAvgBlockTime, &p.AvgBlockTime, validateAvgBlockTime),
}
}

Expand Down Expand Up @@ -177,3 +186,13 @@ func validateMaxIdledBlock(v interface{}) error {

return nil
}

// validateAvgBlockTime validates the AvgBlockTime param
func validateAvgBlockTime(v interface{}) error {
_, ok := v.(float32)
if !ok {
return fmt.Errorf("invalid parameter type: %T", v)
}

return nil
}
Loading

0 comments on commit 6d98dcf

Please sign in to comment.