forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
92 lines (77 loc) · 1.98 KB
/
tx.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
package sdk
import (
"strings"
"github.com/tendermint/go-wire/data"
"github.com/cosmos/cosmos-sdk/errors"
)
const maxTxSize = 10240
// TxInner is the interface all concrete transactions should implement.
//
// It adds bindings for clean un/marhsaling of the various implementations
// both as json and binary, as well as some common functionality to move them.
//
// +gen wrapper:"Tx"
type TxInner interface {
Wrap() Tx
// ValidateBasic should be a stateless check and just verify that the
// tx is properly formated (required strings not blank, signatures exist, etc.)
// this can also be run on the client-side for better debugging before posting a tx
ValidateBasic() error
}
// LoadTx parses a tx from data
func LoadTx(bin []byte) (tx Tx, err error) {
if len(bin) > maxTxSize {
return tx, errors.ErrTooLarge()
}
// Decode tx
err = data.FromWire(bin, &tx)
return tx, err
}
// TODO: do we need this abstraction? TxLayer???
// please review again after implementing "middleware"
// TxLayer provides a standard way to deal with "middleware" tx,
// That add context to an embedded tx.
type TxLayer interface {
TxInner
Next() Tx
}
func (t Tx) IsLayer() bool {
_, ok := t.Unwrap().(TxLayer)
return ok
}
func (t Tx) GetLayer() TxLayer {
l, _ := t.Unwrap().(TxLayer)
return l
}
// env lets us parse an envelope and just grab the type
type env struct {
Kind string `json:"type"`
}
// TODO: put this functionality into go-data in a cleaner and more efficient way
func (t Tx) GetKind() (string, error) {
// render as json
d, err := data.ToJSON(t)
if err != nil {
return "", err
}
// parse json
text := env{}
err = data.FromJSON(d, &text)
if err != nil {
return "", err
}
// grab the type we used in json
return text.Kind, nil
}
func (t Tx) GetMod() (string, error) {
kind, err := t.GetKind()
if err != nil {
return "", err
}
parts := strings.SplitN(kind, "/", 2)
if len(parts) != 2 {
// TODO: return "base"?
return "", errors.ErrUnknownTxType(t)
}
return parts[0], nil
}