Skip to content

Commit

Permalink
Merge pull request #482 from bandprotocol/tss-update-error
Browse files Browse the repository at this point in the history
[TSS] update error
  • Loading branch information
RogerKSI authored Nov 22, 2024
2 parents 7ec7893 + bf1dded commit b960aa0
Show file tree
Hide file tree
Showing 30 changed files with 299 additions and 263 deletions.
13 changes: 8 additions & 5 deletions x/bandtss/keeper/keeper_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ func (k Keeper) ActivateMember(ctx sdk.Context, address sdk.AccAddress, groupID
}

if member.IsActive {
return types.ErrMemberAlreadyActive
return types.ErrMemberAlreadyActive.Wrapf("address %s is already active", address.String())
}

if member.Since.Add(k.GetParams(ctx).InactivePenaltyDuration).After(ctx.BlockTime()) {
return types.ErrTooSoonToActivate
penaltyEndTime := member.Since.Add(k.GetParams(ctx).InactivePenaltyDuration)
if penaltyEndTime.After(ctx.BlockTime()) {
return types.ErrPenaltyDurationNotElapsed.Wrapf("penalty end time: %s", penaltyEndTime)
}

member.IsActive = true
Expand Down Expand Up @@ -99,7 +100,7 @@ func (k Keeper) AddMembers(ctx sdk.Context, groupID tss.GroupID) error {
// AddMember adds a new member to the group and return error if already exists
func (k Keeper) AddMember(ctx sdk.Context, address sdk.AccAddress, groupID tss.GroupID) error {
if k.HasMember(ctx, address, groupID) {
return types.ErrMemberAlreadyExists.Wrapf("address : %v", address)
return types.ErrMemberAlreadyExists.Wrapf("address %s already exists", address.String())
}

member := types.NewMember(address, groupID, true, ctx.BlockTime())
Expand Down Expand Up @@ -142,14 +143,16 @@ func (k Keeper) GetMember(ctx sdk.Context, address sdk.AccAddress, groupID tss.G

// GetMembers retrieves all statuses of the store.
func (k Keeper) GetMembers(ctx sdk.Context) []types.Member {
var members []types.Member
iterator := k.GetMembersIterator(ctx)
defer iterator.Close()

var members []types.Member
for ; iterator.Valid(); iterator.Next() {
var status types.Member
k.cdc.MustUnmarshal(iterator.Value(), &status)
members = append(members, status)
}

return members
}

Expand Down
2 changes: 1 addition & 1 deletion x/bandtss/keeper/keeper_member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (s *AppTestSuite) TestActivateMember() {
s.Require().NoError(err)

err = k.ActivateMember(ctx, address, groupCtx.GroupID)
s.Require().ErrorIs(err, types.ErrTooSoonToActivate)
s.Require().ErrorIs(err, types.ErrPenaltyDurationNotElapsed)

// Failed case - no member
err = k.ActivateMember(ctx, address, groupCtx.GroupID)
Expand Down
10 changes: 3 additions & 7 deletions x/bandtss/keeper/keeper_signing.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"cosmossdk.io/math"

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

"github.com/bandprotocol/chain/v3/pkg/ctxcache"
"github.com/bandprotocol/chain/v3/pkg/tss"
Expand Down Expand Up @@ -61,14 +60,15 @@ func (k Keeper) createSigningRequest(

// charged fee if necessary; If found any coins that exceed limit then return error
feePerSigner := sdk.NewCoins()
totalFee := sdk.NewCoins()
if sender.String() != k.authority && currentGroupID != 0 {
currentGroup, err := k.tssKeeper.GetGroup(ctx, currentGroupID)
if err != nil {
return 0, err
}

feePerSigner = k.GetParams(ctx).FeePerSigner
totalFee := feePerSigner.MulInt(math.NewInt(int64(currentGroup.Threshold)))
totalFee = feePerSigner.MulInt(math.NewInt(int64(currentGroup.Threshold)))
for _, fc := range totalFee {
limitAmt := feeLimit.AmountOf(fc.Denom)
if fc.Amount.GT(limitAmt) {
Expand Down Expand Up @@ -129,17 +129,13 @@ func (k Keeper) createSigningRequest(
sdk.NewAttribute(types.AttributeKeyCurrentGroupSigningID, fmt.Sprintf("%d", currentGroupSigningID)),
sdk.NewAttribute(types.AttributeKeyIncomingGroupID, fmt.Sprintf("%d", incomingGroupID)),
sdk.NewAttribute(types.AttributeKeyIncomingGroupSigningID, fmt.Sprintf("%d", incomingGroupSigningID)),
sdk.NewAttribute(types.AttributeKeyTotalFee, totalFee.String()),
),
)

return bandtssSigningID, nil
}

func decodeSigningMappingKeyToSigningID(key []byte) tss.SigningID {
kv.AssertKeyLength(key, 9)
return tss.SigningID(sdk.BigEndianToUint64(key[1:]))
}

// =====================================
// Signing store
// =====================================
Expand Down
2 changes: 1 addition & 1 deletion x/bandtss/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (k msgServer) ForceTransitionGroup(
// validate incoming group
currentGroupID := k.GetCurrentGroup(ctx).GroupID
if currentGroupID == req.IncomingGroupID {
return nil, types.ErrInvalidIncomingGroup.Wrap("incoming group is the same as the current group")
return nil, types.ErrInvalidGroupID.Wrap("incoming group is the same as the current group")
}

incomingGroup, err := k.tssKeeper.GetGroup(ctx, req.IncomingGroupID)
Expand Down
8 changes: 4 additions & 4 deletions x/bandtss/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (s *AppTestSuite) TestFailTransitionGroup() {
err := s.app.TSSKeeper.SetParams(ctx, tssParams)
s.Require().NoError(err)
},
expectErr: tsstypes.ErrGroupSizeTooLarge,
expectErr: tsstypes.ErrGroupCreationFailed,
},
{
name: "duplicate members",
Expand Down Expand Up @@ -144,7 +144,7 @@ func (s *AppTestSuite) TestFailTransitionGroup() {
},
preProcess: func() {},
postProcess: func() {},
expectErr: fmt.Errorf("threshold must be less than or equal to the members but more than zero"),
expectErr: types.ErrInvalidThreshold,
},
}

Expand Down Expand Up @@ -209,7 +209,7 @@ func (s *AppTestSuite) TestFailForceTransitionGroupInvalidGroupID() {
ExecTime: ctx.BlockTime().Add(10 * time.Minute),
Authority: s.authority.String(),
})
s.Require().ErrorIs(err, types.ErrInvalidIncomingGroup)
s.Require().ErrorIs(err, types.ErrInvalidGroupID)
}

func (s *AppTestSuite) TestFailForceTransitionGroupFromWaitingExecutionStatus() {
Expand Down Expand Up @@ -595,7 +595,7 @@ func (s *AppTestSuite) TestFailActivateNotPassDuration() {
Sender: acc.Address.String(),
GroupID: groupCtx.GroupID,
})
s.Require().ErrorIs(err, types.ErrTooSoonToActivate)
s.Require().ErrorIs(err, types.ErrPenaltyDurationNotElapsed)
}
}

Expand Down
5 changes: 2 additions & 3 deletions x/bandtss/types/constructors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"time"

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

"github.com/bandprotocol/chain/v3/pkg/tss"
)
Expand Down Expand Up @@ -58,11 +57,11 @@ func NewMember(
// Validate performs basic validation of member information.
func (m Member) Validate() error {
if _, err := sdk.AccAddressFromBech32(m.Address); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid member address: %s", err)
return ErrInvalidMember.Wrapf("invalid member address: %s", err)
}

if m.GroupID == 0 {
return ErrInvalidGroupID.Wrap("group id is 0")
return ErrInvalidMember.Wrap("group id cannot be 0")
}

return nil
Expand Down
34 changes: 17 additions & 17 deletions x/bandtss/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ import errorsmod "cosmossdk.io/errors"

// x/bandtss module sentinel errors
var (
ErrInvalidStatus = errorsmod.Register(ModuleName, 2, "invalid status")
ErrTooSoonToActivate = errorsmod.Register(ModuleName, 3, "too soon to activate")
ErrFeeExceedsLimit = errorsmod.Register(ModuleName, 4, "fee exceeds limit")
ErrNoCurrentGroup = errorsmod.Register(ModuleName, 5, "no current group")
ErrTransitionInProgress = errorsmod.Register(ModuleName, 6, "group transition is in progress")
ErrInvalidExecTime = errorsmod.Register(ModuleName, 7, "invalid exec time")
ErrSigningNotFound = errorsmod.Register(ModuleName, 8, "signing not found")
ErrMemberNotFound = errorsmod.Register(ModuleName, 9, "member not found")
ErrMemberAlreadyExists = errorsmod.Register(ModuleName, 10, "member already exists")
ErrMemberAlreadyActive = errorsmod.Register(ModuleName, 11, "member already active")
ErrMemberDuplicate = errorsmod.Register(ModuleName, 12, "duplicated member found within the list")
ErrInvalidSigningThreshold = errorsmod.Register(ModuleName, 13, "invalid signing threshold number")
ErrContentNotAllowed = errorsmod.Register(ModuleName, 14, "content not allowed")
ErrInvalidIncomingGroup = errorsmod.Register(ModuleName, 15, "invalid incoming group")
ErrNoActiveGroup = errorsmod.Register(ModuleName, 16, "no active group supported")
ErrNoIncomingGroup = errorsmod.Register(ModuleName, 17, "no incoming group")
ErrInvalidGroupID = errorsmod.Register(ModuleName, 18, "invalid group ID")
ErrPenaltyDurationNotElapsed = errorsmod.Register(ModuleName, 2, "not allowed to activate due to penalty duration")
ErrFeeExceedsLimit = errorsmod.Register(ModuleName, 3, "fee exceeds limit")
ErrNoCurrentGroup = errorsmod.Register(ModuleName, 4, "no current group")
ErrTransitionInProgress = errorsmod.Register(ModuleName, 5, "group transition is in progress")
ErrInvalidExecTime = errorsmod.Register(ModuleName, 6, "invalid exec time")
ErrSigningNotFound = errorsmod.Register(ModuleName, 7, "signing not found")
ErrMemberNotFound = errorsmod.Register(ModuleName, 8, "member not found")
ErrMemberAlreadyExists = errorsmod.Register(ModuleName, 9, "member already exists")
ErrMemberAlreadyActive = errorsmod.Register(ModuleName, 10, "member already active")
ErrMemberDuplicated = errorsmod.Register(ModuleName, 11, "duplicated member found within the list")
ErrInvalidThreshold = errorsmod.Register(ModuleName, 12, "invalid threshold number")
ErrContentNotAllowed = errorsmod.Register(ModuleName, 13, "content not allowed")
ErrInvalidIncomingGroup = errorsmod.Register(ModuleName, 14, "invalid incoming group")
ErrNoActiveGroup = errorsmod.Register(ModuleName, 15, "no active group supported")
ErrNoIncomingGroup = errorsmod.Register(ModuleName, 16, "no incoming group")
ErrInvalidGroupID = errorsmod.Register(ModuleName, 17, "invalid group ID")
ErrInvalidMember = errorsmod.Register(ModuleName, 18, "invalid member")
)
8 changes: 4 additions & 4 deletions x/bandtss/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const (
AttributeKeyCurrentGroupPubKey = "current_group_pub_key"
AttributeKeyRandomAddress = "random_address"
AttributeKeySignature = "signature"

AttributeKeySigningErrReason = "signing_error_reason"
AttributeKeySigningErrCode = "signing_error_code"
AttributeKeySigningErrCodespace = "signing_error_codespace"
AttributeKeyTotalFee = "total_fee"
AttributeKeySigningErrReason = "signing_error_reason"
AttributeKeySigningErrCode = "signing_error_code"
AttributeKeySigningErrCodespace = "signing_error_codespace"
)
15 changes: 10 additions & 5 deletions x/bandtss/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (m MsgTransitionGroup) ValidateBasic() error {
existed := make(map[string]bool)
for _, member := range m.Members {
if existed[member] {
return ErrMemberDuplicate
return ErrMemberDuplicated.Wrapf("member %s is duplicated", member)
}
existed[member] = true
}
Expand All @@ -72,10 +72,15 @@ func (m MsgTransitionGroup) ValidateBasic() error {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid authority address: %s", err)
}

// Validate threshold must be less than or equal to members but more than zero
if m.Threshold > uint64(len(m.Members)) || m.Threshold <= 0 {
return ErrInvalidSigningThreshold.Wrapf(
"threshold must be less than or equal to the members but more than zero",
// Validate threshold must be less than or equal to members but not zero
if m.Threshold == 0 {
return ErrInvalidThreshold.Wrap("threshold must be greater than zero")
}
if m.Threshold > uint64(len(m.Members)) {
return ErrInvalidThreshold.Wrapf(
"threshold (%d) must be less than or equal to the number of members (%d)",
m.Threshold,
len(m.Members),
)
}

Expand Down
2 changes: 1 addition & 1 deletion x/tss/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (q queryServer) DE(goCtx context.Context, req *types.QueryDERequest) (*type
return nil
})
if err != nil {
return nil, types.ErrInvalidArgument.Wrapf("paginate: %v", err)
return nil, status.Error(codes.Internal, err.Error())
}

return &types.QueryDEResponse{
Expand Down
10 changes: 6 additions & 4 deletions x/tss/keeper/keeper_de.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import (
// them into the queue. It returns an error if the DE size exceeds the maximum limit.
func (k Keeper) EnqueueDEs(ctx sdk.Context, address sdk.AccAddress, des []types.DE) error {
deQueue := k.GetDEQueue(ctx, address)
cnt := deQueue.Tail - deQueue.Head
if cnt+uint64(len(des)) > k.GetParams(ctx).MaxDESize {
return types.ErrDEReachMaxLimit.Wrapf("DE size exceeds %d", k.GetParams(ctx).MaxDESize)
total := deQueue.Tail - deQueue.Head + uint64(len(des))

maxDESize := k.GetParams(ctx).MaxDESize
if total > maxDESize {
return types.ErrDELimitExceeded.Wrapf("DE size exceeds %d; total %d", maxDESize, total)
}

for i, de := range des {
Expand All @@ -34,7 +36,7 @@ func (k Keeper) EnqueueDEs(ctx sdk.Context, address sdk.AccAddress, des []types.
func (k Keeper) DequeueDE(ctx sdk.Context, address sdk.AccAddress) (types.DE, error) {
deQueue := k.GetDEQueue(ctx, address)
if deQueue.Head >= deQueue.Tail {
return types.DE{}, types.ErrDENotFound.Wrapf("DE not found for address %s", address)
return types.DE{}, types.ErrDENotFound.Wrapf("no existing DE for address %s", address)
}

de, err := k.GetDE(ctx, address, deQueue.Head)
Expand Down
13 changes: 10 additions & 3 deletions x/tss/keeper/keeper_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,15 @@ func (k Keeper) CreateGroup(
) (tss.GroupID, error) {
// Validate group size
groupSize := uint64(len(members))
if groupSize == 0 {
return 0, types.ErrGroupCreationFailed.Wrapf("group size must be greater than 0")
}

maxGroupSize := k.GetParams(ctx).MaxGroupSize
if groupSize > maxGroupSize {
return 0, types.ErrGroupSizeTooLarge.Wrap(fmt.Sprintf("group size exceeds %d", maxGroupSize))
return 0, types.ErrGroupCreationFailed.Wrapf(
"the given group size (%d) exceeds the limit (%d)", groupSize, maxGroupSize,
)
}

// Validate duplicate members
Expand Down Expand Up @@ -142,7 +148,7 @@ func (k Keeper) GetGroupResponse(
func (k Keeper) GetGroup(ctx sdk.Context, groupID tss.GroupID) (types.Group, error) {
bz := ctx.KVStore(k.storeKey).Get(types.GroupStoreKey(groupID))
if bz == nil {
return types.Group{}, types.ErrGroupNotFound.Wrapf("failed to get group with groupID: %d", groupID)
return types.Group{}, types.ErrGroupNotFound.Wrapf("failed to get groupID: %d", groupID)
}

var group types.Group
Expand Down Expand Up @@ -171,9 +177,10 @@ func (k Keeper) GetGroupsIterator(ctx sdk.Context) dbm.Iterator {

// GetGroups retrieves all group of the store.
func (k Keeper) GetGroups(ctx sdk.Context) []types.Group {
var groups []types.Group
iterator := k.GetGroupsIterator(ctx)
defer iterator.Close()

var groups []types.Group
for ; iterator.Valid(); iterator.Next() {
var group types.Group
k.cdc.MustUnmarshal(iterator.Value(), &group)
Expand Down
Loading

0 comments on commit b960aa0

Please sign in to comment.