/
msg.go
290 lines (234 loc) · 8.1 KB
/
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
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
package msg
import (
"encoding/json"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/Sotatek-huytran2/go-sdk/common/types"
"github.com/pkg/errors"
"github.com/Sotatek-huytran2/go-sdk/common"
)
// constants
const (
DotBSuffix = ".B"
NativeToken = "AXC"
NativeTokenDotBSuffixed = "AXC" + DotBSuffix
Decimals int8 = 8
MaxTotalSupply int64 = 9000000000000000000 // 90 billions with 8 decimal digits
TokenSymbolMaxLen = 8
TokenSymbolMinLen = 3
TokenSymbolTxHashSuffixLen = 3
MiniTokenSymbolMaxLen = 8
MiniTokenSymbolMinLen = 3
MiniTokenSymbolSuffixLen = 4
MiniTokenSymbolMSuffix = "M"
MiniTokenSymbolTxHashSuffixLen = 3
MaxMiniTokenNameLength = 32
MaxTokenURILength = 2048
)
// Msg - 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() []types.AccAddress
// Get involved addresses of this msg so that we can publish account balance change
GetInvolvedAddresses() []types.AccAddress
}
// ValidateSymbol utility
func ValidateSymbol(symbol string) error {
if len(symbol) == 0 {
return errors.New("suffixed token symbol cannot be empty")
}
// suffix exception for native token (less drama in existing tests)
if symbol == NativeToken ||
symbol == NativeTokenDotBSuffixed {
return nil
}
parts, err := splitSuffixedTokenSymbol(symbol)
if err != nil {
return err
}
symbolPart := parts[0]
// since the native token was given a suffix exception above, do not allow it to have a suffix
if symbolPart == NativeToken ||
symbolPart == NativeTokenDotBSuffixed {
return errors.New("native token symbol should not be suffixed with tx hash")
}
if strings.HasSuffix(symbolPart, DotBSuffix) {
symbolPart = strings.TrimSuffix(symbolPart, DotBSuffix)
}
// check len without .B suffix
if len(symbolPart) < TokenSymbolMinLen {
return fmt.Errorf("token symbol part is too short, got %d chars", len(symbolPart))
}
if len(symbolPart) > TokenSymbolMaxLen {
return fmt.Errorf("token symbol part is too long, got %d chars", len(symbolPart))
}
if !common.IsAlphaNum(symbolPart) {
return errors.New("token symbol part should be alphanumeric")
}
txHashPart := parts[1]
if len(txHashPart) != TokenSymbolTxHashSuffixLen {
return fmt.Errorf("token symbol tx hash suffix must be %d chars in length, got %d", TokenSymbolTxHashSuffixLen, len(txHashPart))
}
// prohibit non-hexadecimal chars in the suffix part
isHex, err := regexp.MatchString(fmt.Sprintf("[0-9A-F]{%d}", TokenSymbolTxHashSuffixLen), txHashPart)
if err != nil {
return err
}
if !isHex {
return fmt.Errorf("token symbol tx hash suffix must be hex with a length of %d", TokenSymbolTxHashSuffixLen)
}
return nil
}
func splitSuffixedTokenSymbol(suffixed string) ([]string, error) {
// as above, the native token symbol is given an exception - it is not required to be suffixed
if suffixed == NativeToken ||
suffixed == NativeTokenDotBSuffixed {
return []string{suffixed, ""}, nil
}
split := strings.SplitN(suffixed, "-", 2)
if len(split) != 2 {
return nil, errors.New("suffixed token symbol must contain a hyphen ('-')")
}
if strings.Contains(split[1], "-") {
return nil, errors.New("suffixed token symbol must contain just one hyphen ('-')")
}
return split, nil
}
type StatusText int
const (
PendingStatusText StatusText = iota
SuccessStatusText
FailedStatusText
)
var StatusTextToString = [...]string{"pending", "success", "failed"}
var StringToStatusText = map[string]StatusText{
"pending": PendingStatusText,
"success": SuccessStatusText,
"failed": FailedStatusText,
}
func (text StatusText) String() string {
return StatusTextToString[text]
}
func (text StatusText) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("\"%v\"", text.String())), nil
}
func (text *StatusText) UnmarshalJSON(b []byte) error {
var j string
err := json.Unmarshal(b, &j)
if err != nil {
return err
}
stringKey, err := strconv.Unquote(string(b))
if err != nil {
return err
}
// Note that if the string cannot be found then it will be set to the zero value, 'pending' in this case.
*text = StringToStatusText[stringKey]
return nil
}
type Status struct {
Text StatusText `json:"text"`
FinalClaim string `json:"final_claim"`
}
type Prophecy struct {
ID string `json:"id"`
Status Status `json:"status"`
//WARNING: Mappings are nondeterministic in Amino,
// an so iterating over them could result in consensus failure. New code should not iterate over the below 2 mappings.
//This is a mapping from a claim to the list of validators that made that claim.
ClaimValidators map[string][]types.ValAddress `json:"claim_validators"`
//This is a mapping from a validator bech32 address to their claim
ValidatorClaims map[string]string `json:"validator_claims"`
}
// DBProphecy is what the prophecy becomes when being saved to the database.
//
// Tendermint/Amino does not support maps so we must serialize those variables into bytes.
type DBProphecy struct {
ID string `json:"id"`
Status Status `json:"status"`
ValidatorClaims []byte `json:"validator_claims"`
}
// SerializeForDB serializes a prophecy into a DBProphecy
func (prophecy Prophecy) SerializeForDB() (DBProphecy, error) {
validatorClaims, err := json.Marshal(prophecy.ValidatorClaims)
if err != nil {
return DBProphecy{}, err
}
return DBProphecy{
ID: prophecy.ID,
Status: prophecy.Status,
ValidatorClaims: validatorClaims,
}, nil
}
// DeserializeFromDB deserializes a DBProphecy into a prophecy
func (dbProphecy DBProphecy) DeserializeFromDB() (Prophecy, error) {
var validatorClaims map[string]string
if err := json.Unmarshal(dbProphecy.ValidatorClaims, &validatorClaims); err != nil {
return Prophecy{}, err
}
var claimValidators = map[string][]types.ValAddress{}
for addr, claim := range validatorClaims {
valAddr, err := types.ValAddressFromBech32(addr)
if err != nil {
panic(fmt.Errorf("unmarshal validator address err, address=%s", addr))
}
claimValidators[claim] = append(claimValidators[claim], valAddr)
}
return Prophecy{
ID: dbProphecy.ID,
Status: dbProphecy.Status,
ClaimValidators: claimValidators,
ValidatorClaims: validatorClaims,
}, nil
}
func ValidateMiniTokenSymbol(symbol string) error {
if len(symbol) == 0 {
return errors.New("suffixed token symbol cannot be empty")
}
parts, err := splitSuffixedTokenSymbol(symbol)
if err != nil {
return err
}
symbolPart := parts[0]
// check len without suffix
if len(symbolPart) < MiniTokenSymbolMinLen {
return fmt.Errorf("mini-token symbol part is too short, got %d chars", len(symbolPart))
}
if len(symbolPart) > MiniTokenSymbolMaxLen {
return fmt.Errorf("mini-token symbol part is too long, got %d chars", len(symbolPart))
}
if !common.IsAlphaNum(symbolPart) {
return errors.New("mini-token symbol part should be alphanumeric")
}
suffixPart := parts[1]
if len(suffixPart) != MiniTokenSymbolSuffixLen {
return fmt.Errorf("mini-token symbol suffix must be %d chars in length, got %d", MiniTokenSymbolSuffixLen, len(suffixPart))
}
if suffixPart[len(suffixPart)-1:] != MiniTokenSymbolMSuffix {
return fmt.Errorf("mini-token symbol suffix must end with M")
}
// prohibit non-hexadecimal chars in the suffix part
isHex, err := regexp.MatchString(fmt.Sprintf("[0-9A-F]{%d}M", MiniTokenSymbolTxHashSuffixLen), suffixPart)
if err != nil {
return err
}
if !isHex {
return fmt.Errorf("mini-token symbol tx hash suffix must be hex with a length of %d", MiniTokenSymbolTxHashSuffixLen)
}
return nil
}