Skip to content

Commit b68bdfa

Browse files
committed
optimize dkg threshold validation
1 parent b304a90 commit b68bdfa

File tree

5 files changed

+53
-22
lines changed

5 files changed

+53
-22
lines changed

x/dlc/types/msg_create_dcm.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
errorsmod "cosmossdk.io/errors"
77
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
88
sdk "github.com/cosmos/cosmos-sdk/types"
9+
10+
tsstypes "github.com/bitwaylabs/bitway/x/tss/types"
911
)
1012

1113
var _ sdk.Msg = &MsgCreateDCM{}
@@ -16,12 +18,12 @@ func (m *MsgCreateDCM) ValidateBasic() error {
1618
return errorsmod.Wrap(err, "invalid authority address")
1719
}
1820

19-
if m.Threshold == 0 {
20-
return errorsmod.Wrap(ErrInvalidThreshold, "threshold must be greater than 0")
21+
if len(m.Participants) < tsstypes.MinDKGParticipantNum {
22+
return errorsmod.Wrapf(ErrInvalidParticipants, "participant number cannot be less than min dkg participant number %d", tsstypes.MinDKGParticipantNum)
2123
}
2224

23-
if len(m.Participants) == 0 || len(m.Participants) < int(m.Threshold) {
24-
return errorsmod.Wrap(ErrInvalidParticipants, "incorrect participant length")
25+
if err := tsstypes.CheckDKGThreshold(len(m.Participants), int(m.Threshold)); err != nil {
26+
return err
2527
}
2628

2729
participants := make(map[string]bool)

x/dlc/types/params.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66

77
errorsmod "cosmossdk.io/errors"
88
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
9+
10+
tsstypes "github.com/bitwaylabs/bitway/x/tss/types"
911
)
1012

1113
var (
@@ -21,9 +23,6 @@ var (
2123
// default nonce generation timeout duration
2224
DefaultNonceGenerationTimeoutDuration = 24 * time.Hour // 24 hours
2325

24-
// minimum oracle participant number
25-
MinOracleParticipantNum = uint32(3)
26-
2726
// default oracle participant number
2827
DefaultOracleParticipantNum = uint32(3)
2928

@@ -71,19 +70,11 @@ func (p Params) Validate() error {
7170
return err
7271
}
7372

74-
if len(p.AllowedOracleParticipants) > 0 && p.OracleParticipantNum > uint32(len(p.AllowedOracleParticipants)) {
75-
return errorsmod.Wrapf(ErrInvalidParams, "oracle participant number cannot be greater than allowed oracle participant number %d", len(p.AllowedOracleParticipants))
76-
}
77-
78-
if p.OracleParticipantNum < MinOracleParticipantNum {
79-
return errorsmod.Wrapf(ErrInvalidParams, "oracle participant number cannot be less than min oracle participant number %d", MinOracleParticipantNum)
80-
}
81-
82-
if p.OracleParticipantThreshold == 0 || p.OracleParticipantThreshold > p.OracleParticipantNum {
83-
return errorsmod.Wrapf(ErrInvalidParams, "invalid oracle participant threshold")
73+
if err := validateOracleParticipantNum(p); err != nil {
74+
return err
8475
}
8576

86-
return nil
77+
return tsstypes.CheckDKGThreshold(int(p.OracleParticipantNum), int(p.OracleParticipantThreshold))
8778
}
8879

8980
// validateOracleParticipants validates the given oracle participants
@@ -102,3 +93,16 @@ func validateOracleParticipants(participants []string) error {
10293

10394
return nil
10495
}
96+
97+
// validateOracleParticipantNum validates the given oracle participant num
98+
func validateOracleParticipantNum(p Params) error {
99+
if p.OracleParticipantNum < uint32(tsstypes.MinDKGParticipantNum) {
100+
return errorsmod.Wrapf(ErrInvalidParams, "oracle participant number cannot be less than min dkg participant number %d", tsstypes.MinDKGParticipantNum)
101+
}
102+
103+
if len(p.AllowedOracleParticipants) > 0 && p.OracleParticipantNum > uint32(len(p.AllowedOracleParticipants)) {
104+
return errorsmod.Wrapf(ErrInvalidParams, "oracle participant number cannot be greater than allowed oracle participant number %d", len(p.AllowedOracleParticipants))
105+
}
106+
107+
return nil
108+
}

x/tss/types/errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var (
1818
ErrUnauthorizedParticipant = errorsmod.Register(ModuleName, 2003, "unauthorized participant")
1919
ErrDKGCompletionAlreadyExists = errorsmod.Register(ModuleName, 2004, "dkg completion already exists")
2020
ErrInvalidDKGCompletion = errorsmod.Register(ModuleName, 2005, "invalid dkg completion")
21+
ErrInvalidThreshold = errorsmod.Register(ModuleName, 2006, "invalid threshold")
2122

2223
ErrSigningRequestDoesNotExist = errorsmod.Register(ModuleName, 3000, "signing request does not exist")
2324
ErrInvalidSigningStatus = errorsmod.Register(ModuleName, 3001, "invalid signing status")

x/tss/types/params.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ var (
1313
// maximum moniker size
1414
MaxMonikerLength = stakingtypes.MaxMonikerLength
1515

16-
// minimum DKG participant number
17-
MinDKGParticipantNum = 3
18-
1916
// default DKG timeout duration
2017
DefaultDKGTimeoutDuration = time.Duration(86400) * time.Second // 1 day
2118
)

x/tss/types/tss.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,20 @@ import (
1212
"github.com/btcsuite/btcd/btcec/v2/schnorr"
1313
"github.com/btcsuite/btcd/txscript"
1414

15+
errorsmod "cosmossdk.io/errors"
16+
sdkmath "cosmossdk.io/math"
1517
sdk "github.com/cosmos/cosmos-sdk/types"
1618

1719
"github.com/sideprotocol/side/bitcoin/crypto/hash"
1820
)
1921

20-
const (
22+
var (
23+
// minimum dkg participant number
24+
MinDKGParticipantNum = 3
25+
26+
// minimum dkg threshold ratio
27+
MinDKGThresholdRatio = sdkmath.LegacyMustNewDecFromStr("0.5")
28+
2129
// schnorr signature size
2230
SchnorrSignatureSize = 64
2331

@@ -131,3 +139,22 @@ func GetExpirationTime(currentTime time.Time, timeoutDuration time.Duration) tim
131139

132140
return currentTime.Add(timeoutDuration)
133141
}
142+
143+
// CheckDKGThreshold checks if the given threshold is valid
144+
func CheckDKGThreshold(participantNum int, threshold int) error {
145+
if threshold == 0 {
146+
return errorsmod.Wrap(ErrInvalidThreshold, "dkg threshold must be greater than 0")
147+
}
148+
149+
if threshold > participantNum {
150+
return errorsmod.Wrapf(ErrInvalidThreshold, "dkg threshold cannot be greater than participant number %d", participantNum)
151+
}
152+
153+
if MinDKGThresholdRatio.IsPositive() {
154+
if int64(threshold) < MinDKGThresholdRatio.MulInt64(int64(participantNum)).TruncateInt64() {
155+
return errorsmod.Wrapf(ErrInvalidThreshold, "dkg threshold ratio cannot be less than %s", MinDKGThresholdRatio)
156+
}
157+
}
158+
159+
return nil
160+
}

0 commit comments

Comments
 (0)