Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions client/proto/story/dkg/v1/types/event.proto
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,12 @@ message EventDKGFinalized {
bytes code_commitment = 1;
uint32 round = 2;
}

message EventDKGCommitteeRewarded {
bytes code_commitment = 1;
uint32 round = 2;
uint32 member_count = 3;
string total_reward = 4;
string per_member_reward = 5;
string remaining_ubi = 6;
}
9 changes: 9 additions & 0 deletions client/proto/story/dkg/v1/types/params.proto
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
syntax = "proto3";
package story.dkg.v1.types;

import "cosmos_proto/cosmos.proto";
import "gogoproto/gogo.proto";

option go_package = "github.com/piplabs/story/client/x/dkg/types";
Expand Down Expand Up @@ -36,4 +37,12 @@ message Params {
bytes code_commitment = 8 [
(gogoproto.moretags) = "yaml:\"code_commitment\""
];

// portion of UBI rewards allocated to the DKG committee (e.g. "0.10" = 10%)
string dkg_committee_reward_portion = 9 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"dkg_committee_reward_portion\""
];
}
26 changes: 26 additions & 0 deletions client/x/dkg/keeper/dkg_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"encoding/hex"

"cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/piplabs/story/client/x/dkg/types"
Expand Down Expand Up @@ -112,3 +114,27 @@ func (*Keeper) emitDKGFinalized(ctx context.Context, dkgNetwork *types.DKGNetwor

return nil
}

func (*Keeper) emitDKGCommitteeRewarded(ctx context.Context, dkgNetwork *types.DKGNetwork, memberCount uint32, totalReward, perMemberReward, remainingUbi math.Int) error {
sdkCtx := sdk.UnwrapSDKContext(ctx)

err := sdkCtx.EventManager().EmitTypedEvent(&types.EventDKGCommitteeRewarded{
CodeCommitment: dkgNetwork.CodeCommitment,
Round: dkgNetwork.Round,
MemberCount: memberCount,
TotalReward: totalReward.String(),
PerMemberReward: perMemberReward.String(),
RemainingUbi: remainingUbi.String(),
})
if err != nil {
return errors.Wrap(err, "failed to emit dkg_committee_rewarded event")
}

log.Info(ctx, "Emitted DKGCommitteeRewarded event",
"round", dkgNetwork.Round,
"member_count", memberCount,
"total_reward", totalReward.String(),
)

return nil
}
6 changes: 6 additions & 0 deletions client/x/dkg/keeper/dkg_finalization.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ func (k *Keeper) FinalizeDKGRound(ctx context.Context, latestRound *types.DKGNet
return errors.Wrap(err, "failed to emit DKG finalized event")
}

// Distribute DKG committee rewards before updating the active round,
// so that getLatestActiveDKGNetwork still returns the previous active round.
if err := k.settleRewardsForPreviousCommittee(ctx); err != nil {
return errors.Wrap(err, "failed to distribute DKG committee rewards")
}

if err := k.setLatestActiveRound(ctx, latestRound); err != nil {
return errors.Wrap(err, "failed to set the latest active round of DKG")
}
Expand Down
15 changes: 14 additions & 1 deletion client/x/dkg/keeper/dkg_handler_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,15 @@ func TestKeeper_InvalidDeal(t *testing.T) {
func setupDKGKeeper(t *testing.T) (*Keeper, context.Context) {
t.Helper()

k, _, _, testCtx := setupDKGKeeperWithMocks(t)

return k, testCtx
}

// setupDKGKeeperWithMocks creates a test DKG keeper and returns the mock keepers for fine-grained control.
func setupDKGKeeperWithMocks(t *testing.T) (*Keeper, *dkgtestutil.MockBankKeeper, *dkgtestutil.MockDistributionKeeper, context.Context) {
t.Helper()

encCfg := moduletestutil.MakeTestEncodingConfig()

key := storetypes.NewKVStoreKey(types.StoreKey)
Expand All @@ -538,6 +547,8 @@ func setupDKGKeeper(t *testing.T) (*Keeper, context.Context) {

ak := dkgtestutil.NewMockAccountKeeper(ctrl)
sk := dkgtestutil.NewMockStakingKeeper(ctrl)
bk := dkgtestutil.NewMockBankKeeper(ctrl)
dk := dkgtestutil.NewMockDistributionKeeper(ctrl)

ak.EXPECT().AddressCodec().Return(authcodec.NewBech32Codec("story")).AnyTimes()
ak.EXPECT().GetModuleAddress(types.ModuleName).Return(sdk.AccAddress{}).AnyTimes()
Expand All @@ -550,6 +561,8 @@ func setupDKGKeeper(t *testing.T) (*Keeper, context.Context) {
encCfg.Codec,
storeService,
ak,
bk,
dk,
sk,
valStore,
mockTEEClient,
Expand All @@ -559,5 +572,5 @@ func setupDKGKeeper(t *testing.T) (*Keeper, context.Context) {

require.NoError(t, k.SetParams(testCtx.Ctx, types.DefaultParams()))

return k, testCtx.Ctx
return k, bk, dk, testCtx.Ctx
}
Loading
Loading