-
Notifications
You must be signed in to change notification settings - Fork 180
/
tx_msg.go
177 lines (143 loc) · 4.31 KB
/
tx_msg.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
package types
import (
"encoding/json"
"math/big"
)
// Transactions messages must fulfill the Msg
type Msg interface {
// Return the message type.
// Must be alphanumeric or empty.
Route() string
// Returns a human-readable string for the message, intended for utilization
// within tags
Type() string
// ValidateBasic does a simple validation check that
// doesn't require access to any other information.
ValidateBasic() error
// Get the canonical byte representation of the Msg.
GetSignBytes() []byte
// Signers returns the addrs of signers that must sign.
// CONTRACT: All signatures must be present to be valid.
// CONTRACT: Returns addrs in some deterministic order.
GetSigners() []AccAddress
}
//__________________________________________________________
// Transactions objects must fulfill the Tx
type Tx interface {
// Gets the all the transaction's messages.
GetMsgs() []Msg
// ValidateBasic does a simple and lightweight validation check that doesn't
// require access to any other information.
ValidateBasic() error
// Return tx gas price
GetGasPrice() *big.Int
// Return tx call function signature
GetTxFnSignatureInfo() ([]byte, int)
GetType() TransactionType
GetSigners() []AccAddress
GetGas() uint64
GetRaw() []byte
GetFrom() string
GetSender(ctx Context) string
GetNonce() uint64
TxHash() []byte
SetRaw([]byte)
SetTxHash([]byte)
}
type HeightSensitive interface {
ValidWithHeight(h int64) error
}
type TxAdapter interface {
Tx
HeightSensitive
}
type BaseTx struct {
Raw []byte
Hash []byte
From string
Nonce uint64
}
func (tx *BaseTx) GetMsgs() []Msg { return nil }
func (tx *BaseTx) ValidateBasic() error { return nil }
func (tx *BaseTx) GetGasPrice() *big.Int { return big.NewInt(0) }
func (tx *BaseTx) GetTxFnSignatureInfo() ([]byte, int) { return nil, 0 }
func (tx *BaseTx) GetType() TransactionType { return UnknownType }
func (tx *BaseTx) GetSigners() []AccAddress { return nil }
func (tx *BaseTx) GetGas() uint64 { return 0 }
func (tx *BaseTx) GetNonce() uint64 { return tx.Nonce }
func (tx *BaseTx) GetFrom() string { return tx.From }
func (tx *BaseTx) GetRaw() []byte { return tx.Raw }
func (tx *BaseTx) TxHash() []byte { return tx.Hash }
func (tx *BaseTx) SetRaw(raw []byte) { tx.Raw = raw }
func (tx *BaseTx) SetTxHash(hash []byte) { tx.Hash = hash }
func (tx *BaseTx) GetSender(_ Context) string { return tx.From }
//__________________________________________________________
type TransactionType int
const (
UnknownType TransactionType = iota
StdTxType
EvmTxType
)
func (t TransactionType) String() (res string) {
switch t {
case StdTxType:
res = "StdTx"
case EvmTxType:
res = "EvmTx"
default:
res = "Unknown"
}
return res
}
// __________________________________________________________
// TxDecoder unmarshals transaction bytes
type TxDecoder func(txBytes []byte, height ...int64) (Tx, error)
// TxEncoder marshals transaction to bytes
type TxEncoder func(tx Tx) ([]byte, error)
//__________________________________________________________
var _ Msg = (*TestMsg)(nil)
// TestMsg is msg type for testing
type TestMsg struct {
signers []AccAddress
}
func NewTestMsg(addrs ...AccAddress) *TestMsg {
return &TestMsg{
signers: addrs,
}
}
// nolint
func (msg *TestMsg) Route() string { return "TestMsg" }
func (msg *TestMsg) Type() string { return "Test message" }
func (msg *TestMsg) GetSignBytes() []byte {
bz, err := json.Marshal(msg.signers)
if err != nil {
panic(err)
}
return MustSortJSON(bz)
}
func (msg *TestMsg) ValidateBasic() error { return nil }
func (msg *TestMsg) GetSigners() []AccAddress {
return msg.signers
}
type TestMsg2 struct {
Signers []AccAddress
}
func NewTestMsg2(addrs ...AccAddress) *TestMsg2 {
return &TestMsg2{
Signers: addrs,
}
}
// nolint
func (msg TestMsg2) Route() string { return "TestMsg" }
func (msg TestMsg2) Type() string { return "Test message" }
func (msg TestMsg2) GetSignBytes() []byte {
bz, err := json.Marshal(msg.Signers)
if err != nil {
panic(err)
}
return MustSortJSON(bz)
}
func (msg TestMsg2) ValidateBasic() error { return nil }
func (msg TestMsg2) GetSigners() []AccAddress {
return msg.Signers
}