/
encoder.go
249 lines (218 loc) · 6.08 KB
/
encoder.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package ibc_tx
import (
"fmt"
"github.com/FiboChain/fbc/libs/cosmos-sdk/codec"
codectypes "github.com/FiboChain/fbc/libs/cosmos-sdk/codec/types"
sdk "github.com/FiboChain/fbc/libs/cosmos-sdk/types"
sdkerrors "github.com/FiboChain/fbc/libs/cosmos-sdk/types/errors"
ibctx "github.com/FiboChain/fbc/libs/cosmos-sdk/types/ibc-adapter"
"github.com/FiboChain/fbc/libs/cosmos-sdk/x/auth/types"
"github.com/gogo/protobuf/proto"
)
const MaxGasWanted = uint64((1 << 63) - 1)
func DefaultTxEncoder() ibctx.TxEncoder {
return func(tx ibctx.Tx) ([]byte, error) {
txWrapper, ok := tx.(*wrapper)
if !ok {
return nil, fmt.Errorf("expected %T, got %T", &wrapper{}, tx)
}
raw := &types.TxRaw{
BodyBytes: txWrapper.getBodyBytes(),
AuthInfoBytes: txWrapper.getAuthInfoBytes(),
Signatures: txWrapper.tx.Signatures,
}
return proto.Marshal(raw)
}
}
// DefaultJSONTxEncoder returns a default protobuf JSON TxEncoder using the provided Marshaler.
func DefaultJSONTxEncoder(cdc codec.ProtoCodecMarshaler) ibctx.IBCTxEncoder {
return func(tx ibctx.Tx) ([]byte, error) {
txWrapper, ok := tx.(*wrapper)
if ok {
return cdc.MarshalJSON(txWrapper.tx)
}
protoTx, ok := tx.(*TxAdapter)
if ok {
return cdc.MarshalJSON(protoTx)
}
return nil, fmt.Errorf("expected %T, got %T", &wrapper{}, tx)
}
}
type TxAdapter struct {
*types.Tx
}
func (t *TxAdapter) GetMsgs() []ibctx.Msg {
if t == nil || t.Body == nil {
return nil
}
anys := t.Body.Messages
res := make([]ibctx.Msg, len(anys))
for i, any := range anys {
cached := any.GetCachedValue()
if cached == nil {
panic("Any cached value is nil. Transaction messages must be correctly packed Any values.")
}
res[i] = cached.(ibctx.Msg)
}
return res
}
// ValidateBasic implements the ValidateBasic method on sdk.Tx.
func (t *TxAdapter) 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 sdkerrors.Wrapf(
sdkerrors.ErrInvalidRequest,
"invalid gas supplied; %d > %d", fee.GasLimit, MaxGasWanted,
)
}
if fee.Amount.IsAnyNil() {
return sdkerrors.Wrapf(
sdkerrors.ErrInsufficientFee,
"invalid fee provided: null",
)
}
if fee.Amount.IsAnyNegative() {
return sdkerrors.Wrapf(
sdkerrors.ErrInsufficientFee,
"invalid fee provided: %s", fee.Amount,
)
}
if fee.Payer != "" {
_, err := sdk.AccAddressFromBech32(fee.Payer)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "Invalid fee payer address (%s)", err)
}
}
sigs := t.Signatures
if len(sigs) == 0 {
return sdkerrors.ErrNoSignatures
}
if len(sigs) != len(t.GetSigners()) {
return sdkerrors.Wrapf(
sdkerrors.ErrUnauthorized,
"wrong number of signers; expected %d, got %d", len(t.GetSigners()), len(sigs),
)
}
return nil
}
// GetSigners retrieves all the signers of a tx.
// This includes all unique signers of the messages (in order),
// as well as the FeePayer (if specified and not already included).
func (t *TxAdapter) 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
}
}
}
// ensure any specified fee payer is included in the required signers (at the end)
feePayer := t.AuthInfo.Fee.Payer
if feePayer != "" && !seen[feePayer] {
payerAddr, err := sdk.AccAddressFromBech32(feePayer)
if err != nil {
panic(err)
}
signers = append(signers, payerAddr)
seen[feePayer] = true
}
return signers
}
//
//func (t *TxRawAdapter) GetGas() uint64 {
// return t.AuthInfo.Fee.GasLimit
//}
//func (t *TxRawAdapter) GetFee() sdk.CoinAdapters {
// return t.AuthInfo.Fee.Amount
//}
//func (t *TxRawAdapter) FeePayer() sdk.AccAddress {
// feePayer := t.AuthInfo.Fee.Payer
// if feePayer != "" {
// payerAddr, err := sdk.AccAddressFromBech32(feePayer)
// if err != nil {
// panic(err)
// }
// return payerAddr
// }
// // use first signer as default if no payer specified
// return t.GetSigners()[0]
//}
//
//func (t *TxRawAdapter) FeeGranter() sdk.AccAddress {
// feePayer := t.AuthInfo.Fee.Granter
// if feePayer != "" {
// granterAddr, err := sdk.AccAddressFromBech32(feePayer)
// if err != nil {
// panic(err)
// }
// return granterAddr
// }
// return nil
//}
//
//// UnpackInterfaces implements the UnpackInterfaceMessages.UnpackInterfaces method
//func (t *TxRawAdapter) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
// if t.Body != nil {
// if err := t.Body.UnpackInterfaces(unpacker); err != nil {
// return err
// }
// }
//
// if t.AuthInfo != nil {
// return t.AuthInfo.UnpackInterfaces(unpacker)
// }
//
// return nil
//}
//
//// UnpackInterfaces implements the UnpackInterfaceMessages.UnpackInterfaces method
//func (m *TxRawAdapter) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
// for _, any := range m.Messages {
// var msg sdk.Msg
// err := unpacker.UnpackAny(any, &msg)
// if err != nil {
// return err
// }
// }
//
// return nil
//}
//
//// UnpackInterfaces implements the UnpackInterfaceMessages.UnpackInterfaces method
//func (m *TxRawAdapter) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
// for _, signerInfo := range m.SignerInfos {
// err := signerInfo.UnpackInterfaces(unpacker)
// if err != nil {
// return err
// }
// }
// return nil
//}
//
//// UnpackInterfaces implements the UnpackInterfaceMessages.UnpackInterfaces method
//func (m *TxRawAdapter) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
// return unpacker.UnpackAny(m.PublicKey, new(cryptotypes.PubKey))
//}
// TODO add call RegisterInterfaces
// RegisterInterfaces registers the sdk.Tx interface.
func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
registry.RegisterInterface("cosmos.tx.v1beta1.Tx", (*sdk.Tx)(nil))
registry.RegisterImplementations((*sdk.Tx)(nil), &types.Tx{})
}