From a6e4b63a201ac7baaa0e6c74395da5a8adcc524b Mon Sep 17 00:00:00 2001 From: Tanmay Date: Tue, 20 Aug 2024 11:09:58 -0400 Subject: [PATCH] add response status to MsgVoteTSSResponse --- x/observer/keeper/msg_server_vote_tss.go | 17 +- x/observer/keeper/msg_server_vote_tss_test.go | 163 +++++++++++++++++- 2 files changed, 175 insertions(+), 5 deletions(-) diff --git a/x/observer/keeper/msg_server_vote_tss.go b/x/observer/keeper/msg_server_vote_tss.go index 24cd34a998..9455dfa25b 100644 --- a/x/observer/keeper/msg_server_vote_tss.go +++ b/x/observer/keeper/msg_server_vote_tss.go @@ -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. diff --git a/x/observer/keeper/msg_server_vote_tss_test.go b/x/observer/keeper/msg_server_vote_tss_test.go index a0bc757e46..d12bdb9b8b 100644 --- a/x/observer/keeper/msg_server_vote_tss_test.go +++ b/x/observer/keeper/msg_server_vote_tss_test.go @@ -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) @@ -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) @@ -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) @@ -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) + }) }