Skip to content

Commit 220e71c

Browse files
author
Francisco de Borja Aranda Castillejo
committed
refactor: add FindBallotAndFinalize function
Signed-off-by: Francisco de Borja Aranda Castillejo <[email protected]>
1 parent ce8afec commit 220e71c

File tree

4 files changed

+51
-29
lines changed

4 files changed

+51
-29
lines changed

x/observer/keeper/msg_server_vote_blame.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@ import (
1313

1414
func (k msgServer) VoteBlame(
1515
goCtx context.Context,
16-
vote *types.MsgVoteBlame,
16+
msg *types.MsgVoteBlame,
1717
) (*types.MsgVoteBlameResponse, error) {
1818
ctx := sdk.UnwrapSDKContext(goCtx)
1919
observationType := types.ObservationType_TSSKeySign
2020

2121
// GetChainFromChainID makes sure we are getting only supported chains , if a chain support has been turned on using gov proposal, this function returns nil
22-
observationChain, found := k.GetSupportedChainFromChainID(ctx, vote.ChainId)
22+
observationChain, found := k.GetSupportedChainFromChainID(ctx, msg.ChainId)
2323
if !found {
2424
return nil, cosmoserrors.Wrap(
2525
crosschainTypes.ErrUnsupportedChain,
26-
fmt.Sprintf("ChainID %d, Blame vote", vote.ChainId),
26+
fmt.Sprintf("ChainID %d, Blame vote", msg.ChainId),
2727
)
2828
}
2929

30-
if ok := k.IsNonTombstonedObserver(ctx, vote.Creator); !ok {
30+
if ok := k.IsNonTombstonedObserver(ctx, msg.Creator); !ok {
3131
return nil, types.ErrNotObserver
3232
}
3333

34-
index := vote.Digest()
34+
index := msg.Digest()
3535
// Add votes and Set Ballot
3636
// GetBallot checks against the supported chains list before querying for Ballot
3737
ballot, isNew, err := k.FindBallot(ctx, index, observationChain, observationType)
@@ -40,11 +40,11 @@ func (k msgServer) VoteBlame(
4040
}
4141

4242
if isNew {
43-
EmitEventBallotCreated(ctx, ballot, vote.BlameInfo.Index, observationChain.String())
43+
EmitEventBallotCreated(ctx, ballot, msg.BlameInfo.Index, observationChain.String())
4444
}
4545

4646
// AddVoteToBallot adds a vote and sets the ballot
47-
ballot, err = k.AddVoteToBallot(ctx, ballot, vote.Creator, types.VoteType_SuccessObservation)
47+
ballot, err = k.AddVoteToBallot(ctx, ballot, msg.Creator, types.VoteType_SuccessObservation)
4848
if err != nil {
4949
return nil, err
5050
}
@@ -59,6 +59,6 @@ func (k msgServer) VoteBlame(
5959
// below only happens when ballot is finalized: exactly when threshold vote is in
6060
// ******************************************************************************
6161

62-
k.SetBlame(ctx, vote.BlameInfo)
62+
k.SetBlame(ctx, msg.BlameInfo)
6363
return &types.MsgVoteBlameResponse{}, nil
6464
}

x/observer/keeper/vote_common.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package keeper
2+
3+
import (
4+
sdk "github.com/cosmos/cosmos-sdk/types"
5+
6+
"github.com/zeta-chain/zetacore/pkg/chains"
7+
"github.com/zeta-chain/zetacore/x/observer/types"
8+
)
9+
10+
// VoteOnBallot finds a ballot or creates a new one if not found,
11+
// and casts a vote on it. Then proceed to check if the vote has been finalized.
12+
// This function holds generic logic for all types of votes.
13+
func (k Keeper) VoteOnBallot(
14+
ctx sdk.Context,
15+
chain chains.Chain,
16+
ballotIndex string,
17+
observationType types.ObservationType,
18+
voter string,
19+
voteType types.VoteType,
20+
) (
21+
ballot types.Ballot,
22+
isFinalized bool,
23+
isNew bool,
24+
err error) {
25+
ballot, isNew, err = k.FindBallot(ctx, ballotIndex, chain, observationType)
26+
if err != nil {
27+
return ballot, false, false, err
28+
}
29+
30+
ballot, err = k.AddVoteToBallot(ctx, ballot, voter, voteType)
31+
if err != nil {
32+
return ballot, false, isNew, err
33+
}
34+
35+
ballot, isFinalized = k.CheckIfFinalizingVote(ctx, ballot)
36+
37+
return ballot, isFinalized, isNew, nil
38+
}

x/observer/keeper/vote_inbound.go

+3-11
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package keeper
33
import (
44
"fmt"
55

6+
sdkerrors "cosmossdk.io/errors"
67
sdk "github.com/cosmos/cosmos-sdk/types"
7-
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
88

99
"github.com/zeta-chain/zetacore/pkg/coin"
1010
"github.com/zeta-chain/zetacore/x/observer/types"
@@ -64,22 +64,14 @@ func (k Keeper) VoteOnInboundBallot(
6464
}
6565
}
6666

67-
// checks against the supported chains list before querying for Ballot
68-
ballot, isNew, err := k.FindBallot(ctx, ballotIndex, senderChain, types.ObservationType_InboundTx)
67+
ballot, isFinalized, isNew, err := k.VoteOnBallot(ctx, senderChain, ballotIndex, types.ObservationType_InboundTx, voter, types.VoteType_SuccessObservation)
6968
if err != nil {
7069
return false, false, err
7170
}
71+
7272
if isNew {
7373
EmitEventBallotCreated(ctx, ballot, inboundHash, senderChain.String())
7474
}
7575

76-
// adds a vote and sets the ballot
77-
ballot, err = k.AddVoteToBallot(ctx, ballot, voter, types.VoteType_SuccessObservation)
78-
if err != nil {
79-
return false, isNew, err
80-
}
81-
82-
// checks if the ballot is finalized
83-
_, isFinalized := k.CheckIfFinalizingVote(ctx, ballot)
8476
return isFinalized, isNew, nil
8577
}

x/observer/keeper/vote_outbound.go

+2-10
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,10 @@ func (k Keeper) VoteOnOutboundBallot(
3636
return false, false, ballot, "", observertypes.ErrNotObserver
3737
}
3838

39-
// fetch or create ballot
40-
ballot, isNew, err = k.FindBallot(ctx, ballotIndex, observationChain, observertypes.ObservationType_OutboundTx)
39+
ballot, isFinalized, isNew, err = k.VoteOnBallot(ctx, observationChain, ballotIndex, observertypes.ObservationType_OutboundTx, voter, observertypes.ConvertReceiveStatusToVoteType(receiveStatus))
4140
if err != nil {
4241
return false, false, ballot, "", err
4342
}
4443

45-
// add vote to ballot
46-
ballot, err = k.AddVoteToBallot(ctx, ballot, voter, observertypes.ConvertReceiveStatusToVoteType(receiveStatus))
47-
if err != nil {
48-
return false, false, ballot, "", err
49-
}
50-
51-
ballot, isFinalizedInThisBlock := k.CheckIfFinalizingVote(ctx, ballot)
52-
return isFinalizedInThisBlock, isNew, ballot, observationChain.String(), nil
44+
return isFinalized, isNew, ballot, observationChain.String(), nil
5345
}

0 commit comments

Comments
 (0)