This repository has been archived by the owner on Aug 27, 2022. It is now read-only.
/
tokentype.go
190 lines (167 loc) · 5.54 KB
/
tokentype.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
package token
import (
"fmt"
"github.com/Qitmeer/qitmeer/core/serialization"
"github.com/Qitmeer/qitmeer/core/types"
"github.com/Qitmeer/qitmeer/engine/txscript"
)
const MaxTokenNameLength = 6
type TokenType struct {
Id types.CoinID
Owners []byte
UpLimit uint64
Enable bool
Name string
FeeCfg TokenFeeConfig
}
func (tt *TokenType) Serialize() ([]byte, error) {
serializeSize := serialization.SerializeSizeVLQ(uint64(tt.Id))
serializeSize += serialization.SerializeSizeVLQ(uint64(len(tt.Owners)))
serializeSize += len(tt.Owners)
serializeSize += serialization.SerializeSizeVLQ(tt.UpLimit)
serializeSize += 1
serializeSize += serialization.SerializeSizeVLQ(uint64(len(tt.Name)))
serializeSize += len(tt.Name)
serializeSize += serialization.SerializeSizeVLQ(uint64(tt.FeeCfg.Type))
serializeSize += serialization.SerializeSizeVLQ(uint64(tt.FeeCfg.Value))
serialized := make([]byte, serializeSize)
offset := 0
offset += serialization.PutVLQ(serialized[offset:], uint64(tt.Id))
offset += serialization.PutVLQ(serialized[offset:], uint64(len(tt.Owners)))
copy(serialized[offset:offset+len(tt.Owners)], tt.Owners)
offset += len(tt.Owners)
offset += serialization.PutVLQ(serialized[offset:], tt.UpLimit)
if tt.Enable {
offset += serialization.PutVLQ(serialized[offset:], uint64(1))
} else {
offset += serialization.PutVLQ(serialized[offset:], uint64(0))
}
offset += serialization.PutVLQ(serialized[offset:], uint64(len(tt.Name)))
copy(serialized[offset:offset+len(tt.Name)], tt.Name)
offset += len(tt.Name)
offset += serialization.PutVLQ(serialized[offset:], uint64(tt.FeeCfg.Type))
offset += serialization.PutVLQ(serialized[offset:], uint64(tt.FeeCfg.Value))
return serialized, nil
}
func (tt *TokenType) Deserialize(data []byte) (int, error) {
offset := 0
//tokenId
Id, bytesRead := serialization.DeserializeVLQ(data[offset:])
if bytesRead == 0 {
return offset, fmt.Errorf("unexpected end of data while reading coin id")
}
offset += bytesRead
//Owners
ownersLength, bytesRead := serialization.DeserializeVLQ(data[offset:])
if bytesRead == 0 {
return offset, fmt.Errorf("unexpected end of data while reading owners")
}
offset += bytesRead
Owners := data[offset : offset+int(ownersLength)]
bytesRead = len(Owners)
offset += bytesRead
//UpLimit
UpLimit, bytesRead := serialization.DeserializeVLQ(data[offset:])
if bytesRead == 0 {
return offset, fmt.Errorf("unexpected end of data while reading UpLimit")
}
offset += bytesRead
//Enable
enableB, bytesRead := serialization.DeserializeVLQ(data[offset:])
if bytesRead == 0 {
return offset, fmt.Errorf("unexpected end of data while reading Enable")
}
offset += bytesRead
//Name
nameLength, bytesRead := serialization.DeserializeVLQ(data[offset:])
if bytesRead == 0 {
return offset, fmt.Errorf("unexpected end of data while reading owners")
}
offset += bytesRead
Name := data[offset : offset+int(nameLength)]
bytesRead = len(Name)
offset += bytesRead
//fee type
feeType, bytesRead := serialization.DeserializeVLQ(data[offset:])
if bytesRead == 0 {
return offset, fmt.Errorf("unexpected end of data while reading fee type")
}
offset += bytesRead
//fee value
feeValue, bytesRead := serialization.DeserializeVLQ(data[offset:])
if bytesRead == 0 {
return offset, fmt.Errorf("unexpected end of data while reading fee value")
}
offset += bytesRead
tt.Id = types.CoinID(Id)
tt.Owners = Owners
tt.UpLimit = UpLimit
tt.Enable = enableB > 0
tt.Name = string(Name)
tt.FeeCfg = TokenFeeConfig{Type: types.FeeType(feeType), Value: int64(feeValue)}
return offset, nil
}
func (tt *TokenType) GetAddress() types.Address {
script, err := txscript.ParsePkScript(tt.Owners)
if err != nil {
log.Error(err.Error())
return nil
}
if tnScript, ok := script.(*txscript.TokenScript); ok {
addr := tnScript.GetAddresses()
if len(addr) > 0 {
return addr[0]
}
}
return nil
}
type TokenTypesMap map[types.CoinID]TokenType
func (ttm *TokenTypesMap) Update(update *TypeUpdate) error {
switch update.Typ {
case types.TxTypeTokenNew:
_, ok := (*ttm)[update.Tt.Id]
if ok {
return fmt.Errorf("It already exists:%s\n", update.Tt.Name)
}
(*ttm)[update.Tt.Id] = update.Tt
log.Trace(fmt.Sprintf("Token type update: new %s(%d)", update.Tt.Name, update.Tt.Id))
case types.TxTypeTokenRenew:
tt, ok := (*ttm)[update.Tt.Id]
if !ok {
return fmt.Errorf("It doesn't exist: Coin id (%d)\n", update.Tt.Id)
}
if tt.Enable {
return fmt.Errorf("Renew is allowed only when disable: Coin id (%d)\n", update.Tt.Id)
}
tt.Owners = update.Tt.Owners
tt.UpLimit = update.Tt.UpLimit
tt.Name = update.Tt.Name
(*ttm)[tt.Id] = tt
log.Trace(fmt.Sprintf("Token type update: renew %s(%d)", update.Tt.Name, update.Tt.Id))
case types.TxTypeTokenValidate:
tt, ok := (*ttm)[update.Tt.Id]
if !ok {
return fmt.Errorf("It doesn't exist: Coin id (%d)\n", update.Tt.Id)
}
if tt.Enable {
return fmt.Errorf("Validate is allowed only when disable: Coin id (%d)\n", update.Tt.Id)
}
tt.Enable = true
(*ttm)[tt.Id] = tt
log.Trace(fmt.Sprintf("Token type update: validate %s(%d)", update.Tt.Name, update.Tt.Id))
case types.TxTypeTokenInvalidate:
tt, ok := (*ttm)[update.Tt.Id]
if !ok {
return fmt.Errorf("It doesn't exist: Coin id (%d)\n", update.Tt.Id)
}
if !tt.Enable {
return fmt.Errorf("Invalidate is allowed only when enable: Coin id (%d)\n", update.Tt.Id)
}
tt.Enable = false
(*ttm)[tt.Id] = tt
log.Trace(fmt.Sprintf("Token type update: invalidate %s(%d)", update.Tt.Name, update.Tt.Id))
default:
return fmt.Errorf("unknown update type %v", update.Typ)
}
return nil
}