Skip to content

Commit 1347826

Browse files
committed
add events on replacement
1 parent 03dd3b5 commit 1347826

File tree

3 files changed

+60
-23
lines changed

3 files changed

+60
-23
lines changed

x/bandtss/keeper/group.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"time"
66

7+
tmbytes "github.com/cometbft/cometbft/libs/bytes"
78
sdk "github.com/cosmos/cosmos-sdk/types"
89

910
"github.com/bandprotocol/chain/v2/pkg/tss"
@@ -57,23 +58,24 @@ func (k Keeper) CreateGroupReplacement(
5758
return 0, err
5859
}
5960

60-
k.SetReplacement(ctx, types.Replacement{
61+
replacement = types.Replacement{
6162
SigningID: signing.ID,
6263
CurrentGroupID: currentGroupID,
6364
CurrentPubKey: currentGroup.PubKey,
6465
NewGroupID: newGroupID,
6566
NewPubKey: newGroup.PubKey,
6667
Status: types.REPLACEMENT_STATUS_WAITING_SIGN,
6768
ExecTime: execTime,
68-
})
69+
}
70+
k.SetReplacement(ctx, replacement)
6971

7072
ctx.EventManager().EmitEvent(
7173
sdk.NewEvent(
7274
types.EventTypeReplacement,
73-
sdk.NewAttribute(tsstypes.AttributeKeySigningID, fmt.Sprintf("%d", signing.ID)),
74-
sdk.NewAttribute(types.AttributeKeyCurrentGroupID, fmt.Sprintf("%d", currentGroupID)),
75-
sdk.NewAttribute(types.AttributeKeyReplacingGroupID, fmt.Sprintf("%d", newGroupID)),
76-
sdk.NewAttribute(types.AttributeKeyReplacementStatus, types.REPLACEMENT_STATUS_WAITING_SIGN.String()),
75+
sdk.NewAttribute(tsstypes.AttributeKeySigningID, fmt.Sprintf("%d", replacement.SigningID)),
76+
sdk.NewAttribute(types.AttributeKeyCurrentGroupID, fmt.Sprintf("%d", replacement.CurrentGroupID)),
77+
sdk.NewAttribute(types.AttributeKeyReplacingGroupID, fmt.Sprintf("%d", replacement.NewGroupID)),
78+
sdk.NewAttribute(types.AttributeKeyReplacementStatus, replacement.Status.String()),
7779
sdk.NewAttribute(types.AttributeKeyExecTime, replacement.ExecTime.Format(time.RFC3339)),
7880
),
7981
)
@@ -102,14 +104,29 @@ func (k Keeper) HandleReplaceGroup(ctx sdk.Context, endBlockTime time.Time) erro
102104
if signing.Status == tsstypes.SIGNING_STATUS_SUCCESS {
103105
replacement.Status = types.REPLACEMENT_STATUS_WAITING_REPLACE
104106
k.SetReplacement(ctx, replacement)
107+
108+
newGroup, err := k.tssKeeper.GetGroup(ctx, replacement.NewGroupID)
109+
if err != nil {
110+
return err
111+
}
112+
113+
rAddress, err := signing.Signature.R().Address()
114+
if err != nil {
115+
return err
116+
}
117+
sig := tmbytes.HexBytes(signing.Signature.S())
118+
105119
ctx.EventManager().EmitEvent(
106120
sdk.NewEvent(
107121
types.EventTypeReplacement,
108122
sdk.NewAttribute(tsstypes.AttributeKeySigningID, fmt.Sprintf("%d", replacement.SigningID)),
109123
sdk.NewAttribute(types.AttributeKeyCurrentGroupID, fmt.Sprintf("%d", replacement.CurrentGroupID)),
110124
sdk.NewAttribute(types.AttributeKeyReplacingGroupID, fmt.Sprintf("%d", replacement.NewGroupID)),
111-
sdk.NewAttribute(types.AttributeKeyReplacementStatus, types.REPLACEMENT_STATUS_WAITING_REPLACE.String()),
125+
sdk.NewAttribute(types.AttributeKeyReplacementStatus, replacement.Status.String()),
112126
sdk.NewAttribute(types.AttributeKeyExecTime, replacement.ExecTime.Format(time.RFC3339)),
127+
sdk.NewAttribute(types.AttributeKeyNewGroupPubKey, newGroup.PubKey.String()),
128+
sdk.NewAttribute(types.AttributeKeyRAddress, tmbytes.HexBytes(rAddress).String()),
129+
sdk.NewAttribute(types.AttributeKeySignature, sig.String()),
113130
),
114131
)
115132
}
@@ -133,7 +150,7 @@ func (k Keeper) HandleFailReplacementSigning(ctx sdk.Context, replacement types.
133150
sdk.NewAttribute(tsstypes.AttributeKeySigningID, fmt.Sprintf("%d", replacement.SigningID)),
134151
sdk.NewAttribute(types.AttributeKeyCurrentGroupID, fmt.Sprintf("%d", replacement.CurrentGroupID)),
135152
sdk.NewAttribute(types.AttributeKeyReplacingGroupID, fmt.Sprintf("%d", replacement.NewGroupID)),
136-
sdk.NewAttribute(types.AttributeKeyReplacementStatus, types.REPLACEMENT_STATUS_FALLEN.String()),
153+
sdk.NewAttribute(types.AttributeKeyReplacementStatus, replacement.Status.String()),
137154
sdk.NewAttribute(types.AttributeKeyExecTime, replacement.ExecTime.Format(time.RFC3339)),
138155
),
139156
)
@@ -170,7 +187,7 @@ func (k Keeper) ReplaceGroup(ctx sdk.Context, replacement types.Replacement) err
170187
sdk.NewEvent(
171188
types.EventTypeNewGroupActivate,
172189
sdk.NewAttribute(types.AttributeKeyGroupID, fmt.Sprintf("%d", newGroup.ID)),
173-
sdk.NewAttribute(types.AttributeKeyGroupPubKey, fmt.Sprintf("%v", newGroup.PubKey.String())),
190+
sdk.NewAttribute(types.AttributeKeyGroupPubKey, newGroup.PubKey.String()),
174191
),
175192
)
176193

x/bandtss/keeper/group_test.go

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package keeper_test
22

33
import (
4+
"encoding/hex"
45
"testing"
56
"time"
67

@@ -157,6 +158,10 @@ func TestFailCreateGroupReplacement(t *testing.T) {
157158

158159
func TestHandleReplaceGroup(t *testing.T) {
159160
currentGroupID := tss.GroupID(1)
161+
b, err := hex.DecodeString("022E7B370CA78A3814CEFABFEC3EE4919648E60618BB50C0B77594F8F1ED1816658F3125B7522FA215E71AEF46C1DDAC449AC43CF9C78DE829AC8F66BC2FE09EEB")
162+
require.NoError(t, err)
163+
164+
signature := tss.Signature(b)
160165

161166
type expectOut struct {
162167
replacementStatus types.ReplacementStatus
@@ -198,13 +203,20 @@ func TestHandleReplaceGroup(t *testing.T) {
198203
name: "have replacement and signed but not meet exec time",
199204
preProcess: func(s *testutil.TestSuite) {
200205
s.Keeper.SetReplacement(s.Ctx, types.Replacement{
201-
SigningID: tss.SigningID(1),
202-
Status: types.REPLACEMENT_STATUS_WAITING_SIGN,
203-
ExecTime: s.Ctx.BlockTime().Add(10 * time.Minute),
206+
SigningID: tss.SigningID(1),
207+
CurrentGroupID: tss.GroupID(1),
208+
NewGroupID: tss.GroupID(2),
209+
Status: types.REPLACEMENT_STATUS_WAITING_SIGN,
210+
ExecTime: s.Ctx.BlockTime().Add(10 * time.Minute),
204211
})
205212
s.MockTSSKeeper.EXPECT().GetSigning(s.Ctx, tss.SigningID(1)).Return(tsstypes.Signing{
206-
ID: tss.SigningID(1),
207-
Status: tsstypes.SIGNING_STATUS_SUCCESS,
213+
ID: tss.SigningID(1),
214+
Status: tsstypes.SIGNING_STATUS_SUCCESS,
215+
Signature: signature,
216+
GroupPubKey: []byte("test-pubkey-1"),
217+
}, nil)
218+
s.MockTSSKeeper.EXPECT().GetGroup(s.Ctx, tss.GroupID(2)).Return(tsstypes.Group{
219+
PubKey: []byte("test-pubkey-2"),
208220
}, nil)
209221
},
210222
expectOut: expectOut{
@@ -216,9 +228,11 @@ func TestHandleReplaceGroup(t *testing.T) {
216228
name: "have replacement and signing failed",
217229
preProcess: func(s *testutil.TestSuite) {
218230
s.Keeper.SetReplacement(s.Ctx, types.Replacement{
219-
SigningID: tss.SigningID(1),
220-
Status: types.REPLACEMENT_STATUS_WAITING_SIGN,
221-
ExecTime: s.Ctx.BlockTime().Add(10 * time.Minute),
231+
SigningID: tss.SigningID(1),
232+
CurrentGroupID: tss.GroupID(1),
233+
NewGroupID: tss.GroupID(2),
234+
Status: types.REPLACEMENT_STATUS_WAITING_SIGN,
235+
ExecTime: s.Ctx.BlockTime().Add(10 * time.Minute),
222236
})
223237
s.MockTSSKeeper.EXPECT().GetSigning(s.Ctx, tss.SigningID(1)).Return(tsstypes.Signing{
224238
ID: tss.SigningID(1),
@@ -281,17 +295,20 @@ func TestHandleReplaceGroup(t *testing.T) {
281295
s.Ctx = s.Ctx.WithBlockTime(s.Ctx.BlockTime().Add(11 * time.Minute))
282296

283297
s.MockTSSKeeper.EXPECT().GetSigning(s.Ctx, tss.SigningID(1)).Return(tsstypes.Signing{
284-
ID: tss.SigningID(1),
285-
Status: tsstypes.SIGNING_STATUS_SUCCESS,
298+
ID: tss.SigningID(1),
299+
Status: tsstypes.SIGNING_STATUS_SUCCESS,
300+
Signature: signature,
301+
GroupPubKey: []byte("test-pubkey-1"),
286302
}, nil)
287303
s.MockTSSKeeper.EXPECT().MustGetMembers(s.Ctx, tss.GroupID(1)).Return([]tsstypes.Member{
288-
{ID: 1, Address: "band1m5lq9u533qaya4q3nfyl6ulzqkpkhge9q8tpzs"},
304+
{ID: 1, Address: "band1m5lq9u533qaya4q3nfyl6ulzqkpkhge9q8tpzs", PubKey: []byte("test-pubkey-1")},
289305
})
290306
s.MockTSSKeeper.EXPECT().MustGetMembers(s.Ctx, tss.GroupID(2)).Return([]tsstypes.Member{
291-
{ID: 1, Address: "band1p40yh3zkmhcv0ecqp3mcazy83sa57rgjp07dun"},
307+
{ID: 1, Address: "band1p40yh3zkmhcv0ecqp3mcazy83sa57rgjp07dun", PubKey: []byte("test-pubkey-2")},
292308
})
293-
s.MockTSSKeeper.EXPECT().GetGroup(s.Ctx, tss.GroupID(2)).Return(tsstypes.Group{
294-
ID: tss.GroupID(2),
309+
s.MockTSSKeeper.EXPECT().GetGroup(s.Ctx, tss.GroupID(2)).Times(2).Return(tsstypes.Group{
310+
ID: tss.GroupID(2),
311+
PubKey: []byte("test-pubkey-2"),
295312
}, nil)
296313
},
297314
expectOut: expectOut{

x/bandtss/types/events.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@ const (
1818
AttributeKeyExecTime = "exec_time"
1919
AttributeKeyGroupID = "group_id"
2020
AttributeKeyGroupPubKey = "group_pub_key"
21+
AttributeKeyNewGroupPubKey = "new_group_pub_key"
22+
AttributeKeyRAddress = "r_address"
23+
AttributeKeySignature = "signature"
2124
)

0 commit comments

Comments
 (0)