-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathmsg_server_vote_tss.go
126 lines (113 loc) · 4.19 KB
/
msg_server_vote_tss.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package keeper
import (
"context"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ethereum/go-ethereum/common/math"
"github.com/zeta-chain/zetacore/pkg/chains"
"github.com/zeta-chain/zetacore/x/observer/types"
)
// VoteTSS votes on creating a TSS key and recording the information about it (public
// key, participant and operator addresses, finalized and keygen heights).
//
// If the vote passes, the information about the TSS key is recorded on chain
// and the status of the keygen is set to "success".
//
// Fails if the keygen does not exist, the keygen has been already
// completed, or the keygen has failed.
//
// Only node accounts are authorized to broadcast this message.
func (k msgServer) VoteTSS(goCtx context.Context, msg *types.MsgVoteTSS) (*types.MsgVoteTSSResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// checks whether a signer is authorized to sign , by checking their address against the observer mapper which contains the observer list for the chain and type
_, found := k.GetNodeAccount(ctx, msg.Creator)
if !found {
return nil, errorsmod.Wrapf(
sdkerrors.ErrorInvalidSigner,
"signer %s does not have a node account set", msg.Creator)
}
// no need to create a ballot if keygen does not exist
keygen, found := k.GetKeygen(ctx)
if !found {
return &types.MsgVoteTSSResponse{}, types.ErrKeygenNotFound
}
// use a separate transaction to update KEYGEN status to pending when trying to change the TSS address
if keygen.Status == types.KeygenStatus_KeyGenSuccess {
return &types.MsgVoteTSSResponse{}, types.ErrKeygenCompleted
}
// GetBallot checks against the supported chains list before querying for Ballot
ballotCreated := false
index := msg.Digest()
ballot, found := k.GetBallot(ctx, index)
if !found {
// if ballot does not exist, create a new ballot
var voterList []string
for _, nodeAccount := range k.GetAllNodeAccount(ctx) {
voterList = append(voterList, nodeAccount.Operator)
}
ballot = types.Ballot{
Index: "",
BallotIdentifier: index,
VoterList: voterList,
Votes: types.CreateVotes(len(voterList)),
ObservationType: types.ObservationType_TSSKeyGen,
BallotThreshold: sdk.MustNewDecFromStr("1.00"),
BallotStatus: types.BallotStatus_BallotInProgress,
BallotCreationHeight: ctx.BlockHeight(),
}
k.AddBallotToList(ctx, ballot)
EmitEventBallotCreated(ctx, ballot, msg.TssPubkey, "Common-TSS-For-All-Chain")
ballotCreated = true
}
// vote the ballot
var err error
vote := types.VoteType_SuccessObservation
if msg.Status == chains.ReceiveStatus_failed {
vote = types.VoteType_FailureObservation
}
ballot, err = k.AddVoteToBallot(ctx, ballot, msg.Creator, vote)
if err != nil {
return &types.MsgVoteTSSResponse{}, err
}
// returns here if the ballot is not finalized
ballot, isFinalized := k.CheckIfFinalizingVote(ctx, ballot)
if !isFinalized {
return &types.MsgVoteTSSResponse{
VoteFinalized: false,
BallotCreated: ballotCreated,
KeygenSuccess: false,
}, nil
}
// set TSS only on success, set Keygen either way.
// keygen block can be updated using a policy transaction if keygen fails
keygenSuccess := false
if ballot.BallotStatus == types.BallotStatus_BallotFinalized_FailureObservation {
keygen.Status = types.KeygenStatus_KeyGenFailed
keygen.BlockNumber = math.MaxInt64
} else {
tss := types.TSS{
TssPubkey: msg.TssPubkey,
TssParticipantList: keygen.GetGranteePubkeys(),
OperatorAddressList: ballot.VoterList,
FinalizedZetaHeight: ctx.BlockHeight(),
KeyGenZetaHeight: msg.KeygenZetaHeight,
}
// set TSS history only, current TSS is updated via admin transaction
// in Case this is the first TSS address update both current and history
tssList := k.GetAllTSS(ctx)
if len(tssList) == 0 {
k.SetTssAndUpdateNonce(ctx, tss)
}
k.SetTSSHistory(ctx, tss)
keygen.Status = types.KeygenStatus_KeyGenSuccess
keygen.BlockNumber = ctx.BlockHeight()
keygenSuccess = true
}
k.SetKeygen(ctx, keygen)
return &types.MsgVoteTSSResponse{
VoteFinalized: true,
BallotCreated: ballotCreated,
KeygenSuccess: keygenSuccess,
}, nil
}