-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathvote_inbound.go
80 lines (70 loc) · 2.33 KB
/
vote_inbound.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
package keeper
import (
sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/zeta-chain/zetacore/pkg/coin"
"github.com/zeta-chain/zetacore/x/observer/types"
)
// VoteOnInboundBallot casts a vote on an inbound transaction observed on a connected chain. If this
// is the first vote, a new ballot is created. When a threshold of votes is
// reached, the ballot is finalized.
func (k Keeper) VoteOnInboundBallot(
ctx sdk.Context,
senderChainID int64,
receiverChainID int64,
coinType coin.CoinType,
voter string,
ballotIndex string,
inboundHash string,
) (isFinalized bool, isNew bool, err error) {
if !k.IsInboundEnabled(ctx) {
return false, false, types.ErrInboundDisabled
}
// makes sure we are getting only supported chains
// if a chain support has been turned on using gov proposal
// this function returns nil
senderChain, found := k.GetSupportedChainFromChainID(ctx, senderChainID)
if !found {
return false, false, sdkerrors.Wrapf(types.ErrSupportedChains,
"ChainID %d, Observation %s",
senderChainID,
types.ObservationType_InboundTx.String())
}
// checks the voter is authorized to vote on the observation chain
if ok := k.IsNonTombstonedObserver(ctx, voter); !ok {
return false, false, types.ErrNotObserver
}
// makes sure we are getting only supported chains
receiverChain, found := k.GetSupportedChainFromChainID(ctx, receiverChainID)
if !found {
return false, false, sdkerrors.Wrapf(types.ErrSupportedChains,
"ChainID %d, Observation %s",
receiverChainID,
types.ObservationType_InboundTx.String())
}
// check if we want to send ZETA to external chain, but there is no ZETA token.
if receiverChain.IsExternalChain() {
coreParams, found := k.GetChainParamsByChainID(ctx, receiverChain.ChainId)
if !found {
return false, false, types.ErrChainParamsNotFound
}
if coreParams.ZetaTokenContractAddress == "" && coinType == coin.CoinType_Zeta {
return false, false, types.ErrInvalidZetaCoinTypes
}
}
ballot, isFinalized, isNew, err := k.VoteOnBallot(
ctx,
senderChain,
ballotIndex,
types.ObservationType_InboundTx,
voter,
types.VoteType_SuccessObservation,
)
if err != nil {
return false, false, sdkerrors.Wrap(err, errVoteOnBallot)
}
if isNew {
EmitEventBallotCreated(ctx, ballot, inboundHash, senderChain.String())
}
return isFinalized, isNew, nil
}