Skip to content

Commit

Permalink
[TSS] Fix transition state and msgServer (#204)
Browse files Browse the repository at this point in the history
* fix transition state and msgServer

* fix readme

* fix comments
  • Loading branch information
nkitlabs authored Sep 2, 2024
1 parent 397ed00 commit f4e856c
Show file tree
Hide file tree
Showing 18 changed files with 348 additions and 265 deletions.
9 changes: 6 additions & 3 deletions proto/bandtss/v1beta1/bandtss.proto
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ message Signing {

// GroupTransition defines the group transition information of the current group and incoming group.
message GroupTransition {
// signing_id is a tss signing ID of the replacing group signing request.
// signing_id is a tss signing ID of group transition signing request.
uint64 signing_id = 1 [
(gogoproto.customname) = "SigningID",
(gogoproto.casttype) = "github.com/bandprotocol/chain/v2/pkg/tss.SigningID"
Expand All @@ -61,7 +61,7 @@ message GroupTransition {
(gogoproto.customname) = "CurrentGroupID",
(gogoproto.casttype) = "github.com/bandprotocol/chain/v2/pkg/tss.GroupID"
];
// current_group_pub_key is the public key pair that used for sign & verify replace group msg.
// current_group_pub_key is the public key pair that used for sign & verify transition group msg.
bytes current_group_pub_key = 3 [(gogoproto.casttype) = "github.com/bandprotocol/chain/v2/pkg/tss.Point"];
// new_group_id is the ID of the new group that be a new key candidate.
uint64 incoming_group_id = 4 [
Expand All @@ -74,6 +74,9 @@ message GroupTransition {
TransitionStatus status = 6;
// exec_time is the time when the transition will be executed.
google.protobuf.Timestamp exec_time = 7 [(gogoproto.stdtime) = true, (gogoproto.nullable) = false];
// is_force_transition is a flag to indicate whether the current group signs the transition message
// before the transition is executed or not.
bool is_force_transition = 8;
}

// TransitionStatus is an enumeration of the possible statuses of a group transition process.
Expand All @@ -89,7 +92,7 @@ enum TransitionStatus {
// a current group to sign the transition message.
TRANSITION_STATUS_WAITING_SIGN = 2;
// TRANSITION_STATUS_WAITING_EXECUTION is the status of a group transition that
// a transition process is completed, either from a forceReplace or having a current-group
// a transition process is completed, either from a forceTransition or having a current-group
// signature on a transition message, but waits for the execution time.
TRANSITION_STATUS_WAITING_EXECUTION = 3;
}
Expand Down
16 changes: 8 additions & 8 deletions proto/bandtss/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ service Msg {
// TransitionGroup creates a request for creating a new group and replacing current group.
rpc TransitionGroup(MsgTransitionGroup) returns (MsgTransitionGroupResponse);

// ForceReplaceGroup sets the given group to the incoming group without the signature of a transition
// ForceTransitionGroup sets the given group to the incoming group without the signature of a transition
// message from a current group.
rpc ForceReplaceGroup(MsgForceReplaceGroup) returns (MsgForceReplaceGroupResponse);
rpc ForceTransitionGroup(MsgForceTransitionGroup) returns (MsgForceTransitionGroupResponse);
}

// MsgRequestSignature is a request message used for initiating the signing process.
Expand Down Expand Up @@ -117,12 +117,12 @@ message MsgTransitionGroup {
// MsgTransitionGroupResponse is the Msg/TransitionGroup response type.
message MsgTransitionGroupResponse {}

// MsgForceReplaceGroup is the Msg/ForceReplaceGroup request type.
message MsgForceReplaceGroup {
// MsgForceTransitionGroup is the Msg/ForceTransitionGroup request type.
message MsgForceTransitionGroup {
option (cosmos.msg.v1.signer) = "authority";
option (amino.name) = "bandtss/ForceReplaceGroup";
option (amino.name) = "bandtss/ForceTransitionGroup";

// incoming_group_id is the ID of the group that want to replace.
// incoming_group_id is the ID of the group that the module want to transition to.
uint64 incoming_group_id = 1 [
(gogoproto.customname) = "IncomingGroupID",
(gogoproto.casttype) = "github.com/bandprotocol/chain/v2/pkg/tss.GroupID"
Expand All @@ -133,5 +133,5 @@ message MsgForceReplaceGroup {
string authority = 3 [(cosmos_proto.scalar) = "cosmos.AddressString"];
}

// MsgForceReplaceGroupResponse is the Msg/ForceReplaceGroup response type.
message MsgForceReplaceGroupResponse {}
// MsgForceTransitionGroupResponse is the Msg/ForceTransitionGroup response type.
message MsgForceTransitionGroupResponse {}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export WALLET_NAME=validator
BASEDIR=$(dirname "$0")

# Submit force_replace_group proposal
bandd tx gov submit-proposal $BASEDIR/proposal_force_replace_group.json \
bandd tx gov submit-proposal $BASEDIR/proposal_force_transition_group.json \
--from $WALLET_NAME \
-y --keyring-backend test --gas-prices 0.0025uband -b sync

Expand Down
14 changes: 0 additions & 14 deletions scripts/bandtss/proposal_force_replace_group.json

This file was deleted.

14 changes: 14 additions & 0 deletions scripts/bandtss/proposal_force_transition_group.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"messages": [
{
"@type": "/bandtss.v1beta1.MsgForceTransitionGroup",
"incoming_group_id": "1",
"exec_time": "2024-09-02T07:40:00+00:00",
"authority": "band10d07y265gmmuvt4z0w9aw880jnsr700jrdn8wm"
}
],
"metadata": "ipfs://",
"deposit": "1000000000uband",
"title": "Force transition group",
"summary": "This proposal is to force transition a group"
}
6 changes: 3 additions & 3 deletions scripts/bandtss/proposal_transition_group.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
"band1nx0xkpnytk35wvflsg7gszf9smw3vaeauk248q"
],
"threshold": "2",
"exec_time": "2024-07-15T10:10:00+00:00",
"exec_time": "2024-09-02T07:30:00+00:00",
"authority": "band10d07y265gmmuvt4z0w9aw880jnsr700jrdn8wm"
}
],
"metadata": "ipfs://",
"deposit": "1000000000uband",
"title": "Create group",
"summary": "This proposal is to create a group"
"title": "Transition group",
"summary": "This proposal is to transition a group"
}
11 changes: 6 additions & 5 deletions x/bandtss/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ The module is configured to charge a fee for each signing request, a cost that i
- [Params](#params)
- [Msg Service](#msg-service)
- [Msg/TransitionGroup](#msgtransitiongroup)
- [Msg/ForceReplaceGroup](#msgforcereplacegroup)
- [Msg/ForceTransitionGroup](#msgforcetransitiongroup)
- [Msg/RequestSignature](#msgrequestsignature)
- [Msg/Activate](#msgactivate)
- [Msg/Heartbeat](#msgheartbeat)
Expand Down Expand Up @@ -121,6 +121,7 @@ type GroupTransition struct {
IncomingGroupPubKey github_com_bandprotocol_chain_v2_pkg_tss.Point
Status TransitionStatus
ExecTime time.Time
IsForceTransition bool
}
```

Expand Down Expand Up @@ -179,7 +180,7 @@ message MsgTransitionGroup {
}
```

### Msg/ForceReplaceGroup
### Msg/ForceTransitionGroup

A current group can be replaced by an incoming group without needing a signing request (transition message) from the current group.

Expand All @@ -190,11 +191,11 @@ It's expected to fail if:
- The execution time is before the current time or beyond the maximum transition duration.

```protobuf
message MsgForceReplaceGroup {
message MsgForceTransitionGroup {
option (cosmos.msg.v1.signer) = "authority";
option (amino.name) = "bandtss/ForceReplaceGroup";
option (amino.name) = "bandtss/ForceTransitionGroup";
// incoming_group_id is the ID of the group that want to replace.
// incoming_group_id is the ID of the group that the module want to transition to.
uint64 incoming_group_id = 1 [
(gogoproto.customname) = "IncomingGroupID",
(gogoproto.casttype) = "github.com/bandprotocol/chain/v2/pkg/tss.GroupID"
Expand Down
10 changes: 5 additions & 5 deletions x/bandtss/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ func (k msgServer) TransitionGroup(
return &types.MsgTransitionGroupResponse{}, nil
}

// ForceReplaceGroup handles the group transition without waiting a current group to
// ForceTransitionGroup handles the group transition without requesting a current group to
// sign a transition message.
func (k msgServer) ForceReplaceGroup(
func (k msgServer) ForceTransitionGroup(
goCtx context.Context,
req *types.MsgForceReplaceGroup,
) (*types.MsgForceReplaceGroupResponse, error) {
req *types.MsgForceTransitionGroup,
) (*types.MsgForceTransitionGroupResponse, error) {
if k.authority != req.Authority {
return nil, govtypes.ErrInvalidSigner.Wrapf("expected %s got %s", k.authority, req.Authority)
}
Expand Down Expand Up @@ -126,7 +126,7 @@ func (k msgServer) ForceReplaceGroup(
attrs := k.ExtractEventAttributesFromTransition(transition)
ctx.EventManager().EmitEvent(sdk.NewEvent(types.EventTypeGroupTransition, attrs...))

return &types.MsgForceReplaceGroupResponse{}, nil
return &types.MsgForceTransitionGroupResponse{}, nil
}

// RequestSignature initiates the signing process by requesting signatures from assigned members.
Expand Down
21 changes: 11 additions & 10 deletions x/bandtss/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (s *AppTestSuite) TestFailTransitionGroup() {
}
}

func (s *AppTestSuite) TestFailForceReplaceGroupInvalidExecTime() {
func (s *AppTestSuite) TestFailForceTransitionGroupInvalidExecTime() {
ctx, msgSrvr, _ := s.ctx, s.msgSrvr, s.app.TSSKeeper

_ = s.SetupNewGroup(5, 3)
Expand All @@ -173,15 +173,15 @@ func (s *AppTestSuite) TestFailForceReplaceGroupInvalidExecTime() {
maxTransitionDuration := s.app.BandtssKeeper.GetParams(ctx).MaxTransitionDuration
execTime := ctx.BlockTime().Add(10 * time.Minute).Add(maxTransitionDuration)

_, err = msgSrvr.ForceReplaceGroup(ctx, &types.MsgForceReplaceGroup{
_, err = msgSrvr.ForceTransitionGroup(ctx, &types.MsgForceTransitionGroup{
IncomingGroupID: group2Ctx.GroupID,
ExecTime: execTime,
Authority: s.authority.String(),
})
s.Require().ErrorIs(err, types.ErrInvalidExecTime)
}

func (s *AppTestSuite) TestFailForceReplaceGroupInvalidGroupStatus() {
func (s *AppTestSuite) TestFailForceTransitionGroupInvalidGroupStatus() {
ctx, msgSrvr, _ := s.ctx, s.msgSrvr, s.app.TSSKeeper

_ = s.SetupNewGroup(5, 3)
Expand All @@ -194,27 +194,27 @@ func (s *AppTestSuite) TestFailForceReplaceGroupInvalidGroupStatus() {

s.app.BandtssKeeper.DeleteGroupTransition(ctx)

_, err = msgSrvr.ForceReplaceGroup(ctx, &types.MsgForceReplaceGroup{
_, err = msgSrvr.ForceTransitionGroup(ctx, &types.MsgForceTransitionGroup{
IncomingGroupID: group2Ctx.GroupID,
ExecTime: ctx.BlockTime().Add(10 * time.Minute),
Authority: s.authority.String(),
})
s.Require().ErrorIs(err, types.ErrInvalidIncomingGroup)
}

func (s *AppTestSuite) TestFailForceReplaceGroupInvalidGroupID() {
func (s *AppTestSuite) TestFailForceTransitionGroupInvalidGroupID() {
ctx, msgSrvr, _ := s.ctx, s.msgSrvr, s.app.TSSKeeper

group1Ctx := s.SetupNewGroup(5, 3)
_, err := msgSrvr.ForceReplaceGroup(ctx, &types.MsgForceReplaceGroup{
_, err := msgSrvr.ForceTransitionGroup(ctx, &types.MsgForceTransitionGroup{
IncomingGroupID: group1Ctx.GroupID,
ExecTime: ctx.BlockTime().Add(10 * time.Minute),
Authority: s.authority.String(),
})
s.Require().ErrorIs(err, types.ErrInvalidIncomingGroup)
}

func (s *AppTestSuite) TestFailForceReplaceGroupFromApprovedWaitingReplaceStatus() {
func (s *AppTestSuite) TestFailForceTransitionGroupFromWaitingExecutionStatus() {
ctx, msgSrvr, _ := s.ctx, s.msgSrvr, s.app.TSSKeeper

group1Ctx := s.SetupNewGroup(5, 3)
Expand All @@ -232,7 +232,7 @@ func (s *AppTestSuite) TestFailForceReplaceGroupFromApprovedWaitingReplaceStatus
s.Require().True(found)
s.Require().Equal(types.TRANSITION_STATUS_WAITING_EXECUTION, transition.Status)

_, err = msgSrvr.ForceReplaceGroup(ctx, &types.MsgForceReplaceGroup{
_, err = msgSrvr.ForceTransitionGroup(ctx, &types.MsgForceTransitionGroup{
IncomingGroupID: group2Ctx.GroupID,
ExecTime: ctx.BlockTime().Add(10 * time.Minute),
Authority: s.authority.String(),
Expand Down Expand Up @@ -272,7 +272,7 @@ func (s *AppTestSuite) TestFailForceReplaceGroupFromApprovedWaitingReplaceStatus
}
}

func (s *AppTestSuite) TestSuccessForceReplaceGroupFromFallenStatus() {
func (s *AppTestSuite) TestSuccessForceTransitionGroupFromFallenStatus() {
ctx, msgSrvr, _ := s.ctx, s.msgSrvr, s.app.TSSKeeper

group1Ctx := s.SetupNewGroup(5, 3)
Expand All @@ -286,7 +286,7 @@ func (s *AppTestSuite) TestSuccessForceReplaceGroupFromFallenStatus() {

s.app.BandtssKeeper.DeleteGroupTransition(ctx)

_, err = msgSrvr.ForceReplaceGroup(ctx, &types.MsgForceReplaceGroup{
_, err = msgSrvr.ForceTransitionGroup(ctx, &types.MsgForceTransitionGroup{
IncomingGroupID: group2Ctx.GroupID,
ExecTime: ctx.BlockTime().Add(10 * time.Minute),
Authority: s.authority.String(),
Expand All @@ -305,6 +305,7 @@ func (s *AppTestSuite) TestSuccessForceReplaceGroupFromFallenStatus() {
IncomingGroupPubKey: g2.PubKey,
ExecTime: ctx.BlockTime().Add(10 * time.Minute),
SigningID: tss.SigningID(0),
IsForceTransition: true,
}
s.Require().True(found)
s.Require().Equal(expectedTransition, transition)
Expand Down
26 changes: 14 additions & 12 deletions x/bandtss/keeper/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ func (k Keeper) SetNewGroupTransition(
ctx sdk.Context,
incomingGroupID tss.GroupID,
execTime time.Time,
isForceReplace bool,
isForceTransition bool,
) (types.GroupTransition, error) {
status := types.TRANSITION_STATUS_CREATING_GROUP
if isForceReplace {
if isForceTransition {
status = types.TRANSITION_STATUS_WAITING_EXECUTION
}

Expand All @@ -57,22 +57,24 @@ func (k Keeper) SetNewGroupTransition(

// get incoming group and its public key.
var incomingGroupPubKey tss.Point
if isForceReplace {
if isForceTransition {
incomingGroup, err := k.tssKeeper.GetGroup(ctx, incomingGroupID)
if err != nil {
return types.GroupTransition{}, err
}
incomingGroupPubKey = incomingGroup.PubKey
}

transition := types.GroupTransition{
CurrentGroupID: currentGroupID,
CurrentGroupPubKey: currentGroupPubKey,
IncomingGroupID: incomingGroupID,
IncomingGroupPubKey: incomingGroupPubKey,
Status: status,
ExecTime: execTime,
}
transition := types.NewGroupTransition(
tss.SigningID(0),
currentGroupID,
incomingGroupID,
currentGroupPubKey,
incomingGroupPubKey,
status,
execTime,
isForceTransition,
)
k.SetGroupTransition(ctx, transition)

return transition, nil
Expand Down Expand Up @@ -145,7 +147,7 @@ func (k Keeper) ValidateTransitionInProgress(ctx sdk.Context) error {
}

// GetIncomingGroupID returns the incoming group ID from transition state. If the status is not
// either ApprovedWaitingReplace or ForcedWaitingReplace, it returns 0.
// WaitingExecution, it returns 0.
func (k Keeper) GetIncomingGroupID(ctx sdk.Context) tss.GroupID {
transition, found := k.GetGroupTransition(ctx)
if !found || transition.Status != types.TRANSITION_STATUS_WAITING_EXECUTION {
Expand Down
6 changes: 3 additions & 3 deletions x/bandtss/keeper/transition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestHandleGroupTransition(t *testing.T) {
},
},
{
name: "transition with status ForcedWaitingReplace but no current group",
name: "transition with status WaitingExecution but no current group",
preProcess: func(s *KeeperTestSuite) {
s.Keeper.SetGroupTransition(s.Ctx, types.GroupTransition{
SigningID: tss.SigningID(1),
Expand All @@ -50,7 +50,7 @@ func TestHandleGroupTransition(t *testing.T) {
},
},
{
name: "transition with status ForcedWaitingReplace; has a current group",
name: "transition with status WaitingExecution; has a current group",
preProcess: func(s *KeeperTestSuite) {
s.Keeper.SetGroupTransition(s.Ctx, types.GroupTransition{
SigningID: tss.SigningID(0),
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestHandleGroupTransition(t *testing.T) {
},
},
{
name: "transition with status ApprovedWaitingReplace; has a current group",
name: "transition with status WaitingExecution; has a current group",
preProcess: func(s *KeeperTestSuite) {
s.Keeper.SetGroupTransition(s.Ctx, types.GroupTransition{
SigningID: tss.SigningID(1),
Expand Down
Loading

0 comments on commit f4e856c

Please sign in to comment.