Skip to content

Commit

Permalink
add response status to MsgVoteTSSResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Aug 20, 2024
1 parent 3bd9105 commit a6e4b63
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 5 deletions.
17 changes: 13 additions & 4 deletions x/observer/keeper/msg_server_vote_tss.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,23 @@ func (k msgServer) VoteTSS(goCtx context.Context, msg *types.MsgVoteTSS) (*types
// 1. The keygen is still pending
// 2. The keygen block number matches the ballot block number ,which makes sure this the correct ballot for the current keygen

// Return without error so the vote is added to the ballot
// Return without an error so the vote is added to the ballot
if keygen.Status != types.KeygenStatus_PendingKeygen {
return &types.MsgVoteTSSResponse{}, nil
// The response is used for testing only.Setting false for keygen success as the keygen has already been finalized and it doesnt matter what the final status is.We are just asserting that the keygen was previously finalized and is not in pending status.
return &types.MsgVoteTSSResponse{
VoteFinalized: isFinalized,
BallotCreated: ballotCreated,
KeygenSuccess: false,
}, nil
}

// For cases when an observer tries to vote for an older pending ballot , associated with a keygen that was discarded , we would return at this check while still adding the vote to the ballot
// For cases when an observer tries to vote for an older pending ballot, associated with a keygen that was discarded , we would return at this check while still adding the vote to the ballot
if msg.KeygenZetaHeight != keygen.BlockNumber {
return &types.MsgVoteTSSResponse{}, nil
return &types.MsgVoteTSSResponse{
VoteFinalized: isFinalized,
BallotCreated: ballotCreated,
KeygenSuccess: false,
}, nil
}

// Set TSS only on success, set keygen either way.
Expand Down
163 changes: 162 additions & 1 deletion x/observer/keeper/msg_server_vote_tss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,10 @@ func TestMsgServer_VoteTSS(t *testing.T) {
require.NoError(t, err)

// ASSERT
// Check response
require.False(t, res.BallotCreated)
require.True(t, res.VoteFinalized)
require.False(t, res.KeygenSuccess)
// Older ballot should be finalized which still keep keygen in pending state.
newKeygen, found = k.GetKeygen(ctx)
require.True(t, found)
Expand Down Expand Up @@ -522,7 +526,7 @@ func TestMsgServer_VoteTSS(t *testing.T) {
// 1. Ballot for keygen 42 Voted : (acc1, acc2)
// 2. Ballot for keygen 52 Voted : (acc1, acc3)

// 3rd vote on ballot 1: finalize the older ballot
// 3rd vote on ballot 2: finalize the newer ballot

finalizingHeight := int64(55)
ctx = ctx.WithBlockHeight(finalizingHeight)
Expand All @@ -535,6 +539,9 @@ func TestMsgServer_VoteTSS(t *testing.T) {
require.NoError(t, err)

// ASSERT
require.True(t, res.BallotCreated)
require.False(t, res.VoteFinalized)
require.True(t, res.KeygenSuccess)
// Newer ballot should be finalized which make keygen success
newKeygen, found = k.GetKeygen(ctx)
require.True(t, found)
Expand All @@ -554,4 +561,158 @@ func TestMsgServer_VoteTSS(t *testing.T) {
require.True(t, found)
require.EqualValues(t, types.BallotStatus_BallotFinalized_SuccessObservation, newBallot.BallotStatus)
})

t.Run("add vote to a successful keygen", func(t *testing.T) {
// ARRANGE
k, ctx, _, _ := keepertest.ObserverKeeper(t)
ctx = ctx.WithBlockHeight(42)
srv := keeper.NewMsgServerImpl(*k)

// setup state with 3 node accounts
nodeAcc1 := sample.NodeAccount()
nodeAcc2 := sample.NodeAccount()
nodeAcc3 := sample.NodeAccount()
keygen := sample.Keygen(t)
keygen.Status = types.KeygenStatus_KeyGenSuccess
tss := sample.Tss()
k.SetNodeAccount(ctx, *nodeAcc1)
k.SetNodeAccount(ctx, *nodeAcc2)
k.SetNodeAccount(ctx, *nodeAcc3)
k.SetKeygen(ctx, *keygen)

// ACT
// 1st vote: created ballot, but not finalized
res, err := srv.VoteTSS(ctx, &types.MsgVoteTSS{
Creator: nodeAcc1.Operator,
TssPubkey: tss.TssPubkey,
KeygenZetaHeight: 42,
Status: chains.ReceiveStatus_success,
})
require.NoError(t, err)

// check response
require.True(t, res.BallotCreated)
require.False(t, res.VoteFinalized)
require.False(t, res.KeygenSuccess)

// check keygen not updated
newKeygen, found := k.GetKeygen(ctx)
require.True(t, found)
require.EqualValues(t, types.KeygenStatus_KeyGenSuccess, newKeygen.Status)

// 2nd vote: already created ballot, and not finalized
res, err = srv.VoteTSS(ctx, &types.MsgVoteTSS{
Creator: nodeAcc2.Operator,
TssPubkey: tss.TssPubkey,
KeygenZetaHeight: 42,
Status: chains.ReceiveStatus_success,
})
require.NoError(t, err)

// check response
require.False(t, res.BallotCreated)
require.False(t, res.VoteFinalized)
require.False(t, res.KeygenSuccess)

// check keygen not updated
newKeygen, found = k.GetKeygen(ctx)
require.True(t, found)
require.EqualValues(t, types.KeygenStatus_KeyGenSuccess, newKeygen.Status)

// 3nd vote: already created ballot, and not finalized (acc3)
res, err = srv.VoteTSS(ctx, &types.MsgVoteTSS{
Creator: nodeAcc3.Operator,
TssPubkey: tss.TssPubkey,
KeygenZetaHeight: 42,
Status: chains.ReceiveStatus_success,
})
require.NoError(t, err)

// check response
require.False(t, res.BallotCreated)
require.True(t, res.VoteFinalized)
require.False(t, res.KeygenSuccess)

// check keygen not updated
newKeygen, found = k.GetKeygen(ctx)
require.True(t, found)
require.EqualValues(t, types.KeygenStatus_KeyGenSuccess, newKeygen.Status)
})

t.Run("add vote to a failed keygen ", func(t *testing.T) {
// ARRANGE
k, ctx, _, _ := keepertest.ObserverKeeper(t)
ctx = ctx.WithBlockHeight(42)
srv := keeper.NewMsgServerImpl(*k)

// setup state with 3 node accounts
nodeAcc1 := sample.NodeAccount()
nodeAcc2 := sample.NodeAccount()
nodeAcc3 := sample.NodeAccount()
keygen := sample.Keygen(t)
keygen.Status = types.KeygenStatus_KeyGenFailed
tss := sample.Tss()
k.SetNodeAccount(ctx, *nodeAcc1)
k.SetNodeAccount(ctx, *nodeAcc2)
k.SetNodeAccount(ctx, *nodeAcc3)
k.SetKeygen(ctx, *keygen)

// ACT
// 1st vote: created ballot, but not finalized
res, err := srv.VoteTSS(ctx, &types.MsgVoteTSS{
Creator: nodeAcc1.Operator,
TssPubkey: tss.TssPubkey,
KeygenZetaHeight: 42,
Status: chains.ReceiveStatus_failed,
})
require.NoError(t, err)

// check response
require.True(t, res.BallotCreated)
require.False(t, res.VoteFinalized)
require.False(t, res.KeygenSuccess)

// check keygen not updated
newKeygen, found := k.GetKeygen(ctx)
require.True(t, found)
require.EqualValues(t, types.KeygenStatus_KeyGenFailed, newKeygen.Status)

// 2nd vote: already created ballot, and not finalized
res, err = srv.VoteTSS(ctx, &types.MsgVoteTSS{
Creator: nodeAcc2.Operator,
TssPubkey: tss.TssPubkey,
KeygenZetaHeight: 42,
Status: chains.ReceiveStatus_failed,
})
require.NoError(t, err)

// check response
require.False(t, res.BallotCreated)
require.False(t, res.VoteFinalized)
require.False(t, res.KeygenSuccess)

// check keygen not updated
newKeygen, found = k.GetKeygen(ctx)
require.True(t, found)
require.EqualValues(t, types.KeygenStatus_KeyGenFailed, newKeygen.Status)

// 3nd vote: already created ballot, and not finalized (acc3)
res, err = srv.VoteTSS(ctx, &types.MsgVoteTSS{
Creator: nodeAcc3.Operator,
TssPubkey: tss.TssPubkey,
KeygenZetaHeight: 42,
Status: chains.ReceiveStatus_failed,
})
require.NoError(t, err)

// check response
require.False(t, res.BallotCreated)
require.True(t, res.VoteFinalized)
require.False(t, res.KeygenSuccess)

// check keygen not updated
newKeygen, found = k.GetKeygen(ctx)
require.True(t, found)
require.EqualValues(t, types.KeygenStatus_KeyGenFailed, newKeygen.Status)
})
}

0 comments on commit a6e4b63

Please sign in to comment.