/
stdtx.go
549 lines (476 loc) · 13.5 KB
/
stdtx.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
package types
import (
"encoding/json"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/rlp"
"github.com/tendermint/go-amino"
yaml "gopkg.in/yaml.v2"
"github.com/FiboChain/fbc/libs/cosmos-sdk/codec"
sdk "github.com/FiboChain/fbc/libs/cosmos-sdk/types"
sdkerrors "github.com/FiboChain/fbc/libs/cosmos-sdk/types/errors"
"github.com/FiboChain/fbc/libs/cosmos-sdk/x/auth/exported"
"github.com/FiboChain/fbc/libs/tendermint/crypto"
cryptoamino "github.com/FiboChain/fbc/libs/tendermint/crypto/encoding/amino"
"github.com/FiboChain/fbc/libs/tendermint/crypto/multisig"
)
var (
_ sdk.Tx = (*StdTx)(nil)
maxGasWanted = uint64((1 << 63) - 1)
)
// StdTx is a standard way to wrap a Msg with Fee and Signatures.
// NOTE: the first signature is the fee payer (Signatures must not be nil).
type StdTx struct {
Msgs []sdk.Msg `json:"msg" yaml:"msg"`
Fee StdFee `json:"fee" yaml:"fee"`
Signatures []StdSignature `json:"signatures" yaml:"signatures"`
Memo string `json:"memo" yaml:"memo"`
sdk.BaseTx `json:"-" yaml:"-"`
}
func (tx *StdTx) VerifySequence(index int, acc exported.Account) error {
//this function no use in stdtx, never add anythin in this
//only new cosmos44 tx will call this to verify sequence
return nil
}
func (tx *StdTx) UnmarshalFromAmino(cdc *amino.Codec, data []byte) error {
var dataLen uint64 = 0
var subData []byte
for {
data = data[dataLen:]
if len(data) == 0 {
break
}
pos, pbType, err := amino.ParseProtoPosAndTypeMustOneByte(data[0])
if err != nil {
return err
}
data = data[1:]
if pbType != amino.Typ3_ByteLength {
return fmt.Errorf("invalid pbType: %v", pbType)
}
var n int
dataLen, n, err = amino.DecodeUvarint(data)
if err != nil {
return err
}
data = data[n:]
if len(data) < int(dataLen) {
return fmt.Errorf("invalid tx data")
}
subData = data[:dataLen]
switch pos {
case 1:
var msg sdk.Msg
v, err := cdc.UnmarshalBinaryBareWithRegisteredUnmarshaller(subData, &msg)
if err != nil {
err = cdc.UnmarshalBinaryBare(subData, &msg)
if err != nil {
return err
} else {
tx.Msgs = append(tx.Msgs, msg)
}
} else {
tx.Msgs = append(tx.Msgs, v.(sdk.Msg))
}
case 2:
if err := tx.Fee.UnmarshalFromAmino(cdc, subData); err != nil {
return err
}
case 3:
var sig StdSignature
if err := sig.UnmarshalFromAmino(cdc, subData); err != nil {
return err
}
tx.Signatures = append(tx.Signatures, sig)
case 4:
tx.Memo = string(subData)
default:
return fmt.Errorf("unexpect feild num %d", pos)
}
}
return nil
}
func NewStdTx(msgs []sdk.Msg, fee StdFee, sigs []StdSignature, memo string) *StdTx {
return &StdTx{
Msgs: msgs,
Fee: fee,
Signatures: sigs,
Memo: memo,
}
}
// GetMsgs returns the all the transaction's messages.
func (tx *StdTx) GetMsgs() []sdk.Msg { return tx.Msgs }
// ValidateBasic does a simple and lightweight validation check that doesn't
// require access to any other information.
func (tx *StdTx) ValidateBasic() error {
stdSigs := tx.GetSignatures()
if tx.Fee.Gas > maxGasWanted {
return sdkerrors.Wrapf(
sdkerrors.ErrInvalidRequest,
"invalid gas supplied; %d > %d", tx.Fee.Gas, maxGasWanted,
)
}
if tx.Fee.Amount.IsAnyNegative() {
return sdkerrors.Wrapf(
sdkerrors.ErrInsufficientFee,
"invalid fee provided: %s", tx.Fee.Amount,
)
}
if len(stdSigs) == 0 {
return sdkerrors.ErrNoSignatures
}
if len(stdSigs) != len(tx.GetSigners()) {
return sdkerrors.Wrapf(
sdkerrors.ErrUnauthorized,
"wrong number of signers; expected %d, got %d", tx.GetSigners(), len(stdSigs),
)
}
return nil
}
func (tx *StdTx) ValidWithHeight(h int64) error {
for _, msg := range tx.Msgs {
if v, ok := msg.(sdk.HeightSensitive); ok {
if err := v.ValidWithHeight(h); nil != err {
return err
}
}
}
return nil
}
// CountSubKeys counts the total number of keys for a multi-sig public key.
func CountSubKeys(pub crypto.PubKey) int {
v, ok := pub.(multisig.PubKeyMultisigThreshold)
if !ok {
return 1
}
numKeys := 0
for _, subkey := range v.PubKeys {
numKeys += CountSubKeys(subkey)
}
return numKeys
}
// GetSigners returns the addresses that must sign the transaction.
// Addresses are returned in a deterministic order.
// They are accumulated from the GetSigners method for each Msg
// in the order they appear in tx.GetMsgs().
// Duplicate addresses will be omitted.
func (tx *StdTx) GetSigners() []sdk.AccAddress {
seen := map[string]bool{}
var signers []sdk.AccAddress
for _, msg := range tx.GetMsgs() {
for _, addr := range msg.GetSigners() {
if !seen[addr.String()] {
signers = append(signers, addr)
seen[addr.String()] = true
}
}
}
return signers
}
func (tx *StdTx) GetType() sdk.TransactionType {
return sdk.StdTxType
}
// GetMemo returns the memo
func (tx *StdTx) GetMemo() string { return tx.Memo }
// GetSignatures returns the signature of signers who signed the Msg.
// CONTRACT: Length returned is same as length of
// pubkeys returned from MsgKeySigners, and the order
// matches.
// CONTRACT: If the signature is missing (ie the Msg is
// invalid), then the corresponding signature is
// .Empty().
func (tx *StdTx) GetSignatures() [][]byte {
sigs := make([][]byte, len(tx.Signatures))
for i, stdSig := range tx.Signatures {
sigs[i] = stdSig.Signature
}
return sigs
}
// GetPubkeys returns the pubkeys of signers if the pubkey is included in the signature
// If pubkey is not included in the signature, then nil is in the slice instead
func (tx *StdTx) GetPubKeys() []crypto.PubKey {
pks := make([]crypto.PubKey, len(tx.Signatures))
for i, stdSig := range tx.Signatures {
pks[i] = stdSig.PubKey
}
return pks
}
// GetSignBytes returns the signBytes of the tx for a given signer
func (tx *StdTx) GetSignBytes(ctx sdk.Context, index int, acc exported.Account) []byte {
genesis := ctx.BlockHeight() == 0
chainID := ctx.ChainID()
var accNum uint64
if !genesis {
accNum = acc.GetAccountNumber()
}
return StdSignBytes(
chainID, accNum, acc.GetSequence(), tx.Fee, tx.Msgs, tx.Memo,
)
}
// GetGas returns the Gas in StdFee
func (tx *StdTx) GetGas() uint64 { return tx.Fee.Gas }
// GetFee returns the FeeAmount in StdFee
func (tx *StdTx) GetFee() sdk.Coins { return tx.Fee.Amount }
// FeePayer returns the address that is responsible for paying fee
// StdTx returns the first signer as the fee payer
// If no signers for tx, return empty address
func (tx *StdTx) FeePayer(ctx sdk.Context) sdk.AccAddress {
if tx.GetSigners() != nil {
return tx.GetSigners()[0]
}
return sdk.AccAddress{}
}
// GetGasPrice return gas price
func (tx *StdTx) GetGasPrice() *big.Int {
if tx.Fee.Gas == 0 {
return big.NewInt(0)
}
gasPrices := tx.Fee.GasPrices()
if len(gasPrices) == 0 {
return big.NewInt(0)
}
return tx.Fee.GasPrices()[0].Amount.BigInt()
}
func (tx *StdTx) GetTxFnSignatureInfo() ([]byte, int) {
return nil, 0
}
func (tx *StdTx) GetFrom() string {
signers := tx.GetSigners()
if len(signers) == 0 {
return ""
}
return signers[0].String()
}
func (tx *StdTx) GetSender(_ sdk.Context) string {
return tx.GetFrom()
}
func (tx *StdTx) GetNonce() uint64 {
return 0
}
//__________________________________________________________
// StdFee includes the amount of coins paid in fees and the maximum
// gas to be used by the transaction. The ratio yields an effective "gasprice",
// which must be above some miminum to be accepted into the mempool.
type StdFee struct {
Amount sdk.Coins `json:"amount" yaml:"amount"`
Gas uint64 `json:"gas" yaml:"gas"`
}
// NewStdFee returns a new instance of StdFee
func NewStdFee(gas uint64, amount sdk.Coins) StdFee {
return StdFee{
Amount: amount,
Gas: gas,
}
}
// Bytes for signing later
func (fee *StdFee) Bytes() []byte {
// normalize. XXX
// this is a sign of something ugly
// (in the lcd_test, client side its null,
// server side its [])
if len(fee.Amount) == 0 {
fee.Amount = sdk.NewCoins()
}
bz, err := ModuleCdc.MarshalJSON(fee) // TODO
if err != nil {
panic(err)
}
return bz
}
// GasPrices returns the gas prices for a StdFee.
//
// NOTE: The gas prices returned are not the true gas prices that were
// originally part of the submitted transaction because the fee is computed
// as fee = ceil(gasWanted * gasPrices).
func (fee *StdFee) GasPrices() sdk.DecCoins {
// NOTE: here fee.Gas must be greater than 0.
return sdk.NewDecCoinsFromCoins(fee.Amount...).QuoDec(sdk.NewDec(int64(fee.Gas)))
//return fee.Amount.QuoDec(sdk.NewDec(int64(fee.Gas)))
}
func (fee *StdFee) UnmarshalFromAmino(cdc *amino.Codec, data []byte) error {
var dataLen uint64 = 0
var subData []byte
for {
data = data[dataLen:]
if len(data) == 0 {
break
}
pos, pbType, err := amino.ParseProtoPosAndTypeMustOneByte(data[0])
if err != nil {
return err
}
data = data[1:]
if pbType == amino.Typ3_ByteLength {
var n int
dataLen, n, err = amino.DecodeUvarint(data)
if err != nil {
return err
}
data = data[n:]
if len(data) < int(dataLen) {
return fmt.Errorf("invalid tx data")
}
subData = data[:dataLen]
}
switch pos {
case 1:
var coin sdk.DecCoin
err = coin.UnmarshalFromAmino(cdc, subData)
if err != nil {
return err
}
fee.Amount = append(fee.Amount, coin)
case 2:
var n int
fee.Gas, n, err = amino.DecodeUvarint(data)
if err != nil {
return err
}
dataLen = uint64(n)
default:
return fmt.Errorf("unexpect feild num %d", pos)
}
}
return nil
}
//__________________________________________________________
// StdSignDoc is replay-prevention structure.
// It includes the result of msg.GetSignBytes(),
// as well as the ChainID (prevent cross chain replay)
// and the Sequence numbers for each signature (prevent
// inchain replay and enforce tx ordering per account).
type StdSignDoc struct {
AccountNumber uint64 `json:"account_number" yaml:"account_number"`
ChainID string `json:"chain_id" yaml:"chain_id"`
Fee json.RawMessage `json:"fee" yaml:"fee"`
Memo string `json:"memo" yaml:"memo"`
Msgs []json.RawMessage `json:"msgs" yaml:"msgs"`
Sequence uint64 `json:"sequence" yaml:"sequence"`
}
// StdSignBytes returns the bytes to sign for a transaction.
func StdSignBytes(chainID string, accnum uint64, sequence uint64, fee StdFee, msgs []sdk.Msg, memo string) []byte {
msgsBytes := make([]json.RawMessage, 0, len(msgs))
for _, msg := range msgs {
msgsBytes = append(msgsBytes, json.RawMessage(msg.GetSignBytes()))
}
bz, err := ModuleCdc.MarshalJSON(StdSignDoc{
AccountNumber: accnum,
ChainID: chainID,
Fee: json.RawMessage(fee.Bytes()),
Memo: memo,
Msgs: msgsBytes,
Sequence: sequence,
})
if err != nil {
panic(err)
}
return sdk.MustSortJSON(bz)
}
// StdSignature represents a sig
type StdSignature struct {
crypto.PubKey `json:"pub_key" yaml:"pub_key"` // optional
Signature []byte `json:"signature" yaml:"signature"`
}
// DefaultTxDecoder logic for standard transaction decoding
func DefaultTxDecoder(cdc *codec.Codec) sdk.TxDecoder {
return func(txBytes []byte, heights ...int64) (sdk.Tx, error) {
if len(heights) > 0 {
return nil, fmt.Errorf("too many height parameters")
}
var tx StdTx
if len(txBytes) == 0 {
return nil, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "tx bytes are empty")
}
// StdTx.Msg is an interface. The concrete types
// are registered by MakeTxCodec
err := cdc.UnmarshalBinaryLengthPrefixed(txBytes, &tx)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrTxDecode, err.Error())
}
tx.BaseTx.Raw = txBytes
return &tx, nil
}
}
// DefaultTxEncoder logic for standard transaction encoding
func DefaultTxEncoder(cdc *codec.Codec) sdk.TxEncoder {
return func(tx sdk.Tx) ([]byte, error) {
return cdc.MarshalBinaryLengthPrefixed(tx)
}
}
func EthereumTxEncoder(_ *codec.Codec) sdk.TxEncoder {
return func(tx sdk.Tx) ([]byte, error) {
return EthereumTxEncode(tx)
}
}
func EthereumTxEncode(tx sdk.Tx) ([]byte, error) {
return rlp.EncodeToBytes(tx)
}
func EthereumTxDecode(b []byte, tx interface{}) error {
return rlp.DecodeBytes(b, tx)
}
// MarshalYAML returns the YAML representation of the signature.
func (ss StdSignature) MarshalYAML() (interface{}, error) {
var (
bz []byte
pubkey string
err error
)
if ss.PubKey != nil {
pubkey, err = sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, ss.PubKey)
if err != nil {
return nil, err
}
}
bz, err = yaml.Marshal(struct {
PubKey string
Signature string
}{
PubKey: pubkey,
Signature: fmt.Sprintf("%s", ss.Signature),
})
if err != nil {
return nil, err
}
return string(bz), err
}
func (ss *StdSignature) UnmarshalFromAmino(cdc *amino.Codec, data []byte) error {
var dataLen uint64 = 0
var subData []byte
for {
data = data[dataLen:]
if len(data) == 0 {
break
}
pos, pbType, err := amino.ParseProtoPosAndTypeMustOneByte(data[0])
if err != nil {
return err
}
data = data[1:]
if pbType != amino.Typ3_ByteLength {
return fmt.Errorf("invalid field type in StdSignature")
}
var n int
dataLen, n, err = amino.DecodeUvarint(data)
if err != nil {
return err
}
data = data[n:]
if len(data) < int(dataLen) {
return fmt.Errorf("invalid tx data")
}
subData = data[:dataLen]
switch pos {
case 1:
ss.PubKey, err = cryptoamino.UnmarshalPubKeyFromAmino(cdc, subData)
if err != nil {
return err
}
case 2:
ss.Signature = make([]byte, dataLen)
copy(ss.Signature, subData)
default:
return fmt.Errorf("unexpect feild num %d", pos)
}
}
return nil
}