-
Notifications
You must be signed in to change notification settings - Fork 55
/
types.go
130 lines (105 loc) · 3.8 KB
/
types.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
127
128
129
130
package cchain
import (
"crypto/ecdsa"
"github.com/omni-network/omni/lib/cast"
"github.com/omni-network/omni/lib/errors"
"github.com/omni-network/omni/lib/umath"
cmtcrypto "github.com/cometbft/cometbft/crypto"
k1 "github.com/cometbft/cometbft/crypto/secp256k1"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
cosmosk1 "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
sltypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/gogoproto/proto"
)
// PortalValidator is a consensus chain validator in a validator set emitted/submitted by/tp portals .
type PortalValidator struct {
Address common.Address
Power int64
}
// Verify returns an error if the validator is invalid.
func (v PortalValidator) Verify() error {
if v.Address == (common.Address{}) {
return errors.New("empty validator address")
}
if v.Power <= 0 {
return errors.New("invalid validator power")
}
return nil
}
// SDKSigningInfo wraps the cosmos slashing signing info type and extends it with convenience functions.
type SDKSigningInfo struct {
sltypes.ValidatorSigningInfo
// Uptime is the percentage [0,1] of blocks signed in the previous <SignedBlockWindow> (1000).
// Note this is 100% if the validator isn't bonded, since it can't technically miss blocks.
Uptime float64
}
func (i SDKSigningInfo) Jailed() bool {
return i.JailedUntil.Unix() != 0
}
func (i SDKSigningInfo) ConsensusCmtAddr() (cmtcrypto.Address, error) {
valAddr, err := sdk.ConsAddressFromBech32(i.Address)
if err != nil {
return cmtcrypto.Address{}, errors.Wrap(err, "parse validator address")
} else if len(valAddr) != cmtcrypto.AddressSize {
return nil, errors.New("invalid consensus address length")
}
return cmtcrypto.Address(valAddr), nil
}
// SDKValidator wraps the cosmos staking validator type and extends it with
// convenience functions.
type SDKValidator struct {
stypes.Validator
}
// Power returns the validators cometBFT power.
func (v SDKValidator) Power() (uint64, error) {
return umath.ToUint64(v.ConsensusPower(sdk.DefaultPowerReduction))
}
// OperatorEthAddr returns the validator operator ethereum address.
func (v SDKValidator) OperatorEthAddr() (common.Address, error) {
opAddr, err := sdk.ValAddressFromBech32(v.OperatorAddress)
if err != nil {
return common.Address{}, errors.Wrap(err, "parse operator address")
} else if len(opAddr) != common.AddressLength {
return common.Address{}, errors.New("invalid operator address length")
}
return cast.EthAddress(opAddr)
}
// ConsensusEthAddr returns the validator consensus eth address.
func (v SDKValidator) ConsensusEthAddr() (common.Address, error) {
pk, err := v.ConsensusPublicKey()
if err != nil {
return common.Address{}, err
}
return crypto.PubkeyToAddress(*pk), nil
}
// ConsensusCmtAddr returns the validator consensus cometBFT address.
func (v SDKValidator) ConsensusCmtAddr() (cmtcrypto.Address, error) {
pk := new(cosmosk1.PubKey)
err := proto.Unmarshal(v.ConsensusPubkey.Value, pk)
if err != nil {
return nil, errors.Wrap(err, "unmarshal consensus pubkey")
}
if len(pk.Bytes()) != k1.PubKeySize {
return nil, errors.New("invalid public key size")
}
return pk.Address(), nil
}
// ConsensusPublicKey returns the validator consensus public key (eth ecdsa style).
func (v SDKValidator) ConsensusPublicKey() (*ecdsa.PublicKey, error) {
pk := new(cosmosk1.PubKey)
err := proto.Unmarshal(v.ConsensusPubkey.Value, pk)
if err != nil {
return nil, errors.Wrap(err, "unmarshal consensus pubkey")
}
if len(pk.Bytes()) != k1.PubKeySize {
return nil, errors.New("invalid public key size")
}
pubkey, err := crypto.DecompressPubkey(pk.Bytes())
if err != nil {
return nil, errors.Wrap(err, "decompress pubkey")
}
return pubkey, nil
}