forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtx.go
84 lines (68 loc) · 1.62 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
//nolint
package mock
import (
"bytes"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// An sdk.Tx which is its own sdk.Msg.
type kvstoreTx struct {
key []byte
value []byte
bytes []byte
}
// dummy implementation of proto.Message
func (msg kvstoreTx) Reset() {}
func (msg kvstoreTx) String() string { return "TODO" }
func (msg kvstoreTx) ProtoMessage() {}
var (
_ sdk.Tx = kvstoreTx{}
_ sdk.Msg = kvstoreTx{}
)
func NewTx(key, value string) kvstoreTx {
bytes := fmt.Sprintf("%s=%s", key, value)
return kvstoreTx{
key: []byte(key),
value: []byte(value),
bytes: []byte(bytes),
}
}
func (tx kvstoreTx) Route() string {
return "kvstore"
}
func (tx kvstoreTx) Type() string {
return "kvstore_tx"
}
func (tx kvstoreTx) GetMsgs() []sdk.Msg {
return []sdk.Msg{tx}
}
func (tx kvstoreTx) GetMemo() string {
return ""
}
func (tx kvstoreTx) GetSignBytes() []byte {
return tx.bytes
}
// Should the app be calling this? Or only handlers?
func (tx kvstoreTx) ValidateBasic() error {
return nil
}
func (tx kvstoreTx) GetSigners() []sdk.AccAddress {
return nil
}
// takes raw transaction bytes and decodes them into an sdk.Tx. An sdk.Tx has
// all the signatures and can be used to authenticate.
func decodeTx(txBytes []byte) (sdk.Tx, error) {
var tx sdk.Tx
split := bytes.Split(txBytes, []byte("="))
if len(split) == 1 {
k := split[0]
tx = kvstoreTx{k, k, txBytes}
} else if len(split) == 2 {
k, v := split[0], split[1]
tx = kvstoreTx{k, v, txBytes}
} else {
return nil, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "too many '='")
}
return tx, nil
}