/
types.go
121 lines (98 loc) · 2.55 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
package tx
import (
"errors"
"fmt"
"github.com/irisnet/core-sdk-go/common/codec/types"
sdk "github.com/irisnet/core-sdk-go/types"
)
// MaxGasWanted defines the max gas allowed.
const MaxGasWanted = uint64((1 << 63) - 1)
var _, _ types.UnpackInterfacesMessage = &Tx{}, &TxBody{}
var _ sdk.Tx = &Tx{}
// GetMsgs implements the GetMsgs method on sdk.Tx.
func (t *Tx) GetMsgs() []sdk.Msg {
if t == nil || t.Body == nil {
return nil
}
anys := t.Body.Messages
res := make([]sdk.Msg, len(anys))
for i, any := range anys {
msg := any.GetCachedValue().(sdk.Msg)
res[i] = msg
}
return res
}
// ValidateBasic implements the ValidateBasic method on sdk.Tx.
func (t *Tx) ValidateBasic() error {
if t == nil {
return fmt.Errorf("bad Tx")
}
body := t.Body
if body == nil {
return fmt.Errorf("missing TxBody")
}
authInfo := t.AuthInfo
if authInfo == nil {
return fmt.Errorf("missing AuthInfo")
}
fee := authInfo.Fee
if fee == nil {
return fmt.Errorf("missing fee")
}
if fee.GasLimit > MaxGasWanted {
return fmt.Errorf(
"invalid gas supplied; %d > %d", fee.GasLimit, MaxGasWanted,
)
}
if fee.Amount.IsAnyNegative() {
return fmt.Errorf(
"invalid fee provided: %s", fee.Amount,
)
}
sigs := t.Signatures
if len(sigs) == 0 {
return errors.New("no signatures supplied")
}
if len(sigs) != len(t.GetSigners()) {
return fmt.Errorf(
"wrong number of signers; expected %d, got %d", t.GetSigners(), len(sigs),
)
}
return nil
}
// GetSigners retrieves all the signers of a tx.
func (t *Tx) GetSigners() []sdk.AccAddress {
var signers []sdk.AccAddress
seen := map[string]bool{}
for _, msg := range t.GetMsgs() {
for _, addr := range msg.GetSigners() {
if !seen[addr.String()] {
signers = append(signers, addr)
seen[addr.String()] = true
}
}
}
return signers
}
// UnpackInterfaces implements the UnpackInterfaceMessages.UnpackInterfaces method
func (t *Tx) UnpackInterfaces(unpacker types.AnyUnpacker) error {
if t.Body != nil {
return t.Body.UnpackInterfaces(unpacker)
}
return nil
}
// UnpackInterfaces implements the UnpackInterfaceMessages.UnpackInterfaces method
func (m *TxBody) UnpackInterfaces(unpacker types.AnyUnpacker) error {
for _, any := range m.Messages {
var msg sdk.Msg
if err := unpacker.UnpackAny(any, &msg); err != nil {
return err
}
}
return nil
}
// RegisterInterfaces registers the sdk.Tx interface.
func RegisterInterfaces(registry types.InterfaceRegistry) {
registry.RegisterInterface("cosmos.tx.v1beta1.Tx", (*sdk.Tx)(nil))
registry.RegisterImplementations((*sdk.Tx)(nil), &Tx{})
}