Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add pubkey to tss vote #2844

Merged
merged 11 commits into from
Sep 16, 2024
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* [2674](https://github.com/zeta-chain/node/pull/2674) - allow operators to vote on ballots associated with discarded keygen without affecting the status of the current keygen.
* [2672](https://github.com/zeta-chain/node/pull/2672) - check observer set for duplicates when adding a new observer or updating an existing one
* [2787](https://github.com/zeta-chain/node/pull/2787) - ask for 3 accounts (signer, pda, system_program) on solana gateway deposit
* [2944](https://github.com/zeta-chain/node/pull/2844) - add tsspubkey to index for tss keygen voting

## v19.0.0

Expand Down
93 changes: 81 additions & 12 deletions x/observer/keeper/msg_server_vote_tss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,19 @@ func TestMsgServer_VoteTSS(t *testing.T) {
keygen.BlockNumber = 42
k.SetNodeAccount(ctx, *nodeAcc)
k.SetKeygen(ctx, *keygen)

tss := sample.Tss()
// ACT
_, err := srv.VoteTSS(ctx, &types.MsgVoteTSS{
Creator: nodeAcc.Operator,
TssPubkey: sample.Tss().TssPubkey,
TssPubkey: tss.TssPubkey,
KeygenZetaHeight: 42,
Status: chains.ReceiveStatus_success,
})

// ASSERT
// keygen is already completed, but the vote can still be added if the operator has not voted yet
require.NoError(t, err)
ballot, found := k.GetBallot(ctx, fmt.Sprintf("%d-%s", 42, "tss-keygen"))
ballot, found := k.GetBallot(ctx, fmt.Sprintf("%d-%s-%s", 42, tss.TssPubkey, "tss-keygen"))
require.True(t, found)
require.EqualValues(t, types.BallotStatus_BallotFinalized_SuccessObservation, ballot.BallotStatus)
require.True(t, ballot.HasVoted(nodeAcc.Operator))
Expand Down Expand Up @@ -268,11 +268,12 @@ func TestMsgServer_VoteTSS(t *testing.T) {
k.SetNodeAccount(ctx, *nodeAcc)
k.SetNodeAccount(ctx, *sample.NodeAccount())
k.SetKeygen(ctx, *keygen)
tss := sample.Tss()

// add a first vote
res, err := srv.VoteTSS(ctx, &types.MsgVoteTSS{
Creator: nodeAcc.Operator,
TssPubkey: sample.Tss().TssPubkey,
TssPubkey: tss.TssPubkey,
KeygenZetaHeight: 42,
Status: chains.ReceiveStatus_success,
})
Expand All @@ -283,7 +284,7 @@ func TestMsgServer_VoteTSS(t *testing.T) {
// vote again: voting should fail
_, err = srv.VoteTSS(ctx, &types.MsgVoteTSS{
Creator: nodeAcc.Operator,
TssPubkey: sample.Tss().TssPubkey,
TssPubkey: tss.TssPubkey,
KeygenZetaHeight: 42,
Status: chains.ReceiveStatus_success,
})
Expand Down Expand Up @@ -415,11 +416,11 @@ func TestMsgServer_VoteTSS(t *testing.T) {
_, found = k.GetTSS(ctx)
require.False(t, found)

oldBallot, found := k.GetBallot(ctx, fmt.Sprintf("%d-%s", 42, "tss-keygen"))
oldBallot, found := k.GetBallot(ctx, fmt.Sprintf("%d-%s-%s", 42, tss.TssPubkey, "tss-keygen"))
require.True(t, found)
require.EqualValues(t, types.BallotStatus_BallotFinalized_SuccessObservation, oldBallot.BallotStatus)

newBallot, found := k.GetBallot(ctx, fmt.Sprintf("%d-%s", 52, "tss-keygen"))
newBallot, found := k.GetBallot(ctx, fmt.Sprintf("%d-%s-%s", 52, tss2.TssPubkey, "tss-keygen"))
require.True(t, found)
require.EqualValues(t, types.BallotStatus_BallotInProgress, newBallot.BallotStatus)
})
Expand Down Expand Up @@ -548,16 +549,16 @@ func TestMsgServer_VoteTSS(t *testing.T) {
require.EqualValues(t, finalizingHeight, newKeygen.BlockNumber)
require.EqualValues(t, types.KeygenStatus_KeyGenSuccess, newKeygen.Status)

tss, found = k.GetTSS(ctx)
tssQueried, found := k.GetTSS(ctx)
require.True(t, found)
require.Equal(t, tss.KeyGenZetaHeight, int64(52))
require.Equal(t, tss.FinalizedZetaHeight, finalizingHeight)
require.Equal(t, tssQueried.KeyGenZetaHeight, int64(52))
require.Equal(t, tssQueried.FinalizedZetaHeight, finalizingHeight)

oldBallot, found := k.GetBallot(ctx, fmt.Sprintf("%d-%s", 42, "tss-keygen"))
oldBallot, found := k.GetBallot(ctx, fmt.Sprintf("%d-%s-%s", 42, tss.TssPubkey, "tss-keygen"))
require.True(t, found)
require.EqualValues(t, types.BallotStatus_BallotInProgress, oldBallot.BallotStatus)

newBallot, found := k.GetBallot(ctx, fmt.Sprintf("%d-%s", 52, "tss-keygen"))
newBallot, found := k.GetBallot(ctx, fmt.Sprintf("%d-%s-%s", 52, tss2.TssPubkey, "tss-keygen"))
require.True(t, found)
require.EqualValues(t, types.BallotStatus_BallotFinalized_SuccessObservation, newBallot.BallotStatus)
})
Expand Down Expand Up @@ -715,4 +716,72 @@ func TestMsgServer_VoteTSS(t *testing.T) {
require.True(t, found)
require.EqualValues(t, types.KeygenStatus_KeyGenFailed, newKeygen.Status)
})

t.Run("unable to finalize tss if pubkey is different", func(t *testing.T) {
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()
keygen := sample.Keygen(t)
keygen.BlockNumber = 42
keygen.Status = types.KeygenStatus_PendingKeygen
tss := sample.Tss()
k.SetNodeAccount(ctx, *nodeAcc1)
k.SetNodeAccount(ctx, *nodeAcc2)
k.SetKeygen(ctx, *keygen)

// ACT
// Add first vote
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)

// Add second vote with different pubkey should not finalize the tss
res, err = srv.VoteTSS(ctx, &types.MsgVoteTSS{
Creator: nodeAcc2.Operator,
TssPubkey: sample.Tss().TssPubkey,
KeygenZetaHeight: 42,
Status: chains.ReceiveStatus_success,
})
require.NoError(t, err)

// ASSERT
require.True(t, res.BallotCreated) // New ballot created as pubkey is different
require.False(t, res.VoteFinalized)
require.False(t, res.KeygenSuccess)

// Add the second vote with correct pubkey should finalize the tss
finalizingHeight := int64(55)
ctx = ctx.WithBlockHeight(finalizingHeight)
res, err = srv.VoteTSS(ctx, &types.MsgVoteTSS{
Creator: nodeAcc2.Operator,
TssPubkey: tss.TssPubkey,
KeygenZetaHeight: 42,
Status: chains.ReceiveStatus_success,
})
require.NoError(t, err)

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

tssQueried, found := k.GetTSS(ctx)
require.True(t, found)

require.Equal(t, finalizingHeight, tssQueried.FinalizedZetaHeight)
require.Equal(t, tss.TssPubkey, tssQueried.TssPubkey)
})
}
2 changes: 1 addition & 1 deletion x/observer/types/message_vote_tss.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ func (msg *MsgVoteTSS) ValidateBasic() error {

func (msg *MsgVoteTSS) Digest() string {
// We support only 1 keygen at a particular height
return fmt.Sprintf("%d-%s", msg.KeygenZetaHeight, "tss-keygen")
return fmt.Sprintf("%d-%s-%s", msg.KeygenZetaHeight, msg.TssPubkey, "tss-keygen")
}
2 changes: 1 addition & 1 deletion x/observer/types/message_vote_tss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,5 @@ func TestMsgVoteTSS_GetSignBytes(t *testing.T) {

func TestMsgVoteTSS_Digest(t *testing.T) {
msg := types.NewMsgVoteTSS(sample.AccAddress(), "pubkey", 1, chains.ReceiveStatus_success)
require.Equal(t, "1-tss-keygen", msg.Digest())
require.Equal(t, "1-pubkey-tss-keygen", msg.Digest())
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved
}
Loading