Skip to content
Merged
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
13 changes: 3 additions & 10 deletions x/btcbridge/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,18 +222,11 @@ func (m msgServer) InitiateDKG(goCtx context.Context, msg *types.MsgInitiateDKG)

ctx := sdk.UnwrapSDKContext(goCtx)

req := &types.DKGRequest{
Id: m.Keeper.GetNextDKGRequestID(ctx),
Participants: msg.Participants,
Threshold: msg.Threshold,
VaultTypes: msg.VaultTypes,
Expiration: m.Keeper.GetDKGRequestExpirationTime(ctx),
Status: types.DKGRequestStatus_DKG_REQUEST_STATUS_PENDING,
req, err := m.Keeper.InitiateDKG(ctx, msg.Participants, msg.Threshold, msg.VaultTypes)
if err != nil {
return nil, err
}

m.Keeper.SetDKGRequest(ctx, req)
m.Keeper.SetDKGRequestID(ctx, req.Id)

// Emit events
m.EmitEvent(ctx, msg.Authority,
sdk.NewAttribute("id", fmt.Sprintf("%d", req.Id)),
Expand Down
39 changes: 37 additions & 2 deletions x/btcbridge/keeper/tss.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/sideprotocol/side/x/btcbridge/types"
)
Expand Down Expand Up @@ -160,8 +161,38 @@ func (k Keeper) IterateDKGCompletionRequests(ctx sdk.Context, id uint64, cb func
}
}

// CompleteDKG attempts to complete the DKG request
// The DKG request is completed when all participants submit the valid completion request before timeout
// InitiateDKG initiates the DKG request by the specified params
func (k Keeper) InitiateDKG(ctx sdk.Context, participants []*types.DKGParticipant, threshold uint32, vaultTypes []types.AssetType) (*types.DKGRequest, error) {
for _, p := range participants {
consAddress, _ := sdk.ConsAddressFromHex(p.ConsensusAddress)

validator, found := k.stakingKeeper.GetValidatorByConsAddr(ctx, consAddress)
if !found {
return nil, sdkerrors.Wrap(types.ErrInvalidDKGParams, "non validator")
}

if validator.Status != stakingtypes.Bonded {
return nil, sdkerrors.Wrap(types.ErrInvalidDKGParams, "validator not bonded")
}
}

req := &types.DKGRequest{
Id: k.GetNextDKGRequestID(ctx),
Participants: participants,
Threshold: threshold,
VaultTypes: vaultTypes,
Expiration: k.GetDKGRequestExpirationTime(ctx),
Status: types.DKGRequestStatus_DKG_REQUEST_STATUS_PENDING,
}

k.SetDKGRequest(ctx, req)
k.SetDKGRequestID(ctx, req.Id)

return req, nil
}

// CompleteDKG completes the DKG request by the DKG participant
// The DKG request will be completed when all participants submit the valid completion request before timeout
func (k Keeper) CompleteDKG(ctx sdk.Context, req *types.DKGCompletionRequest) error {
dkgReq := k.GetDKGRequest(ctx, req.Id)
if dkgReq == nil {
Expand Down Expand Up @@ -194,6 +225,10 @@ func (k Keeper) CompleteDKG(ctx sdk.Context, req *types.DKGCompletionRequest) er
return sdkerrors.Wrap(types.ErrInvalidDKGCompletionRequest, "non validator")
}

if validator.Status != stakingtypes.Bonded {
return sdkerrors.Wrap(types.ErrInvalidDKGCompletionRequest, "validator not bonded")
}

pubKey, err := validator.ConsPubKey()
if err != nil {
return err
Expand Down
10 changes: 9 additions & 1 deletion x/btcbridge/types/message_initiate_dkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,18 @@ func (m *MsgInitiateDKG) ValidateBasic() error {
return sdkerrors.Wrap(ErrInvalidDKGParams, "vault types can not be empty")
}

vaultTypes := make(map[AssetType]bool)

for _, t := range m.VaultTypes {
if t == AssetType_ASSET_TYPE_UNSPECIFIED {
return sdkerrors.Wrap(ErrInvalidDKGParams, "invalid vault types")
return sdkerrors.Wrap(ErrInvalidDKGParams, "invalid vault type")
}

if vaultTypes[t] {
return sdkerrors.Wrap(ErrInvalidDKGParams, "duplicate vault type")
}

vaultTypes[t] = true
}

return nil
Expand Down