-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathmsg_server_vote_block_header.go
62 lines (52 loc) · 1.64 KB
/
msg_server_vote_block_header.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
package keeper
import (
"context"
sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
lightclienttypes "github.com/zeta-chain/zetacore/x/lightclient/types"
"github.com/zeta-chain/zetacore/x/observer/types"
)
// VoteBlockHeader vote for a new block header to the storers
func (k msgServer) VoteBlockHeader(
goCtx context.Context,
msg *types.MsgVoteBlockHeader,
) (*types.MsgVoteBlockHeaderResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// check if the chain is enabled
chain, found := k.GetSupportedChainFromChainID(ctx, msg.ChainId)
if !found {
return nil, sdkerrors.Wrapf(types.ErrSupportedChains, "chain id: %d", msg.ChainId)
}
// check if observer
if ok := k.IsNonTombstonedObserver(ctx, msg.Creator); !ok {
return nil, types.ErrNotObserver
}
// check the new block header is valid
parentHash, err := k.lightclientKeeper.CheckNewBlockHeader(ctx, msg.ChainId, msg.BlockHash, msg.Height, msg.Header)
if err != nil {
return nil, sdkerrors.Wrap(lightclienttypes.ErrInvalidBlockHeader, err.Error())
}
_, isFinalized, isNew, err := k.VoteOnBallot(
ctx,
chain,
msg.Digest(),
types.ObservationType_InboundTx,
msg.Creator,
types.VoteType_SuccessObservation,
)
if err != nil {
return nil, sdkerrors.Wrap(err, errVoteOnBallot)
}
if !isFinalized {
return &types.MsgVoteBlockHeaderResponse{
BallotCreated: isNew,
VoteFinalized: false,
}, nil
}
// Add the new block header to the store.
k.lightclientKeeper.AddBlockHeader(ctx, msg.ChainId, msg.Height, msg.BlockHash, msg.Header, parentHash)
return &types.MsgVoteBlockHeaderResponse{
BallotCreated: isNew,
VoteFinalized: true,
}, nil
}