-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgs.go
244 lines (205 loc) · 6.18 KB
/
msgs.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
package types
import (
"fmt"
sdk "github.com/furyaxyz/fuxchain/libs/cosmos-sdk/types"
sdkerrors "github.com/furyaxyz/fuxchain/libs/cosmos-sdk/types/errors"
"github.com/tendermint/go-amino"
)
// RouterKey is they name of the bank module
const RouterKey = ModuleName
// MsgSend - high level transaction of the coin module
type MsgSend struct {
FromAddress sdk.AccAddress `json:"from_address" yaml:"from_address"`
ToAddress sdk.AccAddress `json:"to_address" yaml:"to_address"`
Amount sdk.Coins `json:"amount" yaml:"amount"`
}
var _ sdk.Msg = MsgSend{}
// NewMsgSend - construct arbitrary multi-in, multi-out send msg.
func NewMsgSend(fromAddr, toAddr sdk.AccAddress, amount sdk.Coins) MsgSend {
return MsgSend{FromAddress: fromAddr, ToAddress: toAddr, Amount: amount}
}
// Route Implements Msg.
func (msg MsgSend) Route() string { return RouterKey }
// Type Implements Msg.
func (msg MsgSend) Type() string { return "send" }
// ValidateBasic Implements Msg.
func (msg MsgSend) ValidateBasic() error {
if msg.FromAddress.Empty() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "missing sender address")
}
if msg.ToAddress.Empty() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "missing recipient address")
}
if !msg.Amount.IsValid() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String())
}
if !msg.Amount.IsAllPositive() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String())
}
return nil
}
// GetSignBytes Implements Msg.
func (msg MsgSend) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}
// GetSigners Implements Msg.
func (msg MsgSend) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.FromAddress}
}
func (msg *MsgSend) 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:]
// pb types of all fields are byteLength(2)
if pbType != amino.Typ3_ByteLength {
return fmt.Errorf("invalid pbType: %v in MsgSend", 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:
msg.FromAddress = make([]byte, dataLen)
copy(msg.FromAddress, subData)
case 2:
msg.ToAddress = make([]byte, dataLen)
copy(msg.ToAddress, subData)
case 3:
var coin sdk.DecCoin
err = coin.UnmarshalFromAmino(cdc, subData)
if err != nil {
return err
}
msg.Amount = append(msg.Amount, coin)
default:
return fmt.Errorf("unexpect feild num %d", pos)
}
}
return nil
}
// MsgMultiSend - high level transaction of the coin module
type MsgMultiSend struct {
Inputs []Input `json:"inputs" yaml:"inputs"`
Outputs []Output `json:"outputs" yaml:"outputs"`
}
var _ sdk.Msg = MsgMultiSend{}
// NewMsgMultiSend - construct arbitrary multi-in, multi-out send msg.
func NewMsgMultiSend(in []Input, out []Output) MsgMultiSend {
return MsgMultiSend{Inputs: in, Outputs: out}
}
// Route Implements Msg
func (msg MsgMultiSend) Route() string { return RouterKey }
// Type Implements Msg
func (msg MsgMultiSend) Type() string { return "multisend" }
// ValidateBasic Implements Msg.
func (msg MsgMultiSend) ValidateBasic() error {
// this just makes sure all the inputs and outputs are properly formatted,
// not that they actually have the money inside
if len(msg.Inputs) == 0 {
return ErrNoInputs
}
if len(msg.Outputs) == 0 {
return ErrNoOutputs
}
return ValidateInputsOutputs(msg.Inputs, msg.Outputs)
}
// GetSignBytes Implements Msg.
func (msg MsgMultiSend) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}
// GetSigners Implements Msg.
func (msg MsgMultiSend) GetSigners() []sdk.AccAddress {
addrs := make([]sdk.AccAddress, len(msg.Inputs))
for i, in := range msg.Inputs {
addrs[i] = in.Address
}
return addrs
}
// Input models transaction input
type Input struct {
Address sdk.AccAddress `json:"address" yaml:"address"`
Coins sdk.Coins `json:"coins" yaml:"coins"`
}
// ValidateBasic - validate transaction input
func (in Input) ValidateBasic() error {
if len(in.Address) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "input address missing")
}
if !in.Coins.IsValid() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, in.Coins.String())
}
if !in.Coins.IsAllPositive() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, in.Coins.String())
}
return nil
}
// NewInput - create a transaction input, used with MsgMultiSend
func NewInput(addr sdk.AccAddress, coins sdk.Coins) Input {
return Input{
Address: addr,
Coins: coins,
}
}
// Output models transaction outputs
type Output struct {
Address sdk.AccAddress `json:"address" yaml:"address"`
Coins sdk.Coins `json:"coins" yaml:"coins"`
}
// ValidateBasic - validate transaction output
func (out Output) ValidateBasic() error {
if len(out.Address) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "output address missing")
}
if !out.Coins.IsValid() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, out.Coins.String())
}
if !out.Coins.IsAllPositive() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, out.Coins.String())
}
return nil
}
// NewOutput - create a transaction output, used with MsgMultiSend
func NewOutput(addr sdk.AccAddress, coins sdk.Coins) Output {
return Output{
Address: addr,
Coins: coins,
}
}
// ValidateInputsOutputs validates that each respective input and output is
// valid and that the sum of inputs is equal to the sum of outputs.
func ValidateInputsOutputs(inputs []Input, outputs []Output) error {
var totalIn, totalOut sdk.Coins
for _, in := range inputs {
if err := in.ValidateBasic(); err != nil {
return err
}
totalIn = totalIn.Add(in.Coins...)
}
for _, out := range outputs {
if err := out.ValidateBasic(); err != nil {
return err
}
totalOut = totalOut.Add(out.Coins...)
}
// make sure inputs and outputs match
if !totalIn.IsEqual(totalOut) {
return ErrInputOutputMismatch
}
return nil
}