forked from bourbaki-czz/classzz
/
czzState.go
585 lines (563 loc) · 16 KB
/
czzState.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
package cross
import (
"bytes"
"errors"
"fmt"
"io"
"log"
"math/big"
"sort"
"github.com/classzz/classzz/chaincfg/chainhash"
"github.com/classzz/classzz/rlp"
"github.com/classzz/czzutil"
)
type EntangleState struct {
EnInfos map[string]*BeaconAddressInfo
EnEntitys map[uint64]UserEntangleInfos
PoolAmount1 *big.Int
PoolAmount2 *big.Int
CurExchangeID uint64
}
type StoreBeaconAddress struct {
Address string
Lh *BeaconAddressInfo
}
type StoreUserInfos struct {
EID uint64
UserInfos UserEntangleInfos
}
type SortStoreBeaconAddress []*StoreBeaconAddress
func (vs SortStoreBeaconAddress) Len() int {
return len(vs)
}
func (vs SortStoreBeaconAddress) Less(i, j int) bool {
return bytes.Compare([]byte(vs[i].Address), []byte(vs[j].Address)) == -1
}
func (vs SortStoreBeaconAddress) Swap(i, j int) {
it := vs[i]
vs[i] = vs[j]
vs[j] = it
}
type SortStoreUserInfos []*StoreUserInfos
func (vs SortStoreUserInfos) Len() int {
return len(vs)
}
func (vs SortStoreUserInfos) Less(i, j int) bool {
return vs[i].EID < vs[j].EID
}
func (vs SortStoreUserInfos) Swap(i, j int) {
it := vs[i]
vs[i] = vs[j]
vs[j] = it
}
/////////////////////////////////////////////////////////////////
func (es *EntangleState) toSlice() (SortStoreBeaconAddress, SortStoreUserInfos) {
v1, v2 := make([]*StoreBeaconAddress, 0, 0), make([]*StoreUserInfos, 0, 0)
for k, v := range es.EnInfos {
v1 = append(v1, &StoreBeaconAddress{
Address: k,
Lh: v,
})
}
for k, v := range es.EnEntitys {
v2 = append(v2, &StoreUserInfos{
EID: k,
UserInfos: v,
})
}
sort.Sort(SortStoreBeaconAddress(v1))
sort.Sort(SortStoreUserInfos(v2))
return SortStoreBeaconAddress(v1), SortStoreUserInfos(v2)
}
func (es *EntangleState) fromSlice(v1 SortStoreBeaconAddress, v2 SortStoreUserInfos) {
enInfos := make(map[string]*BeaconAddressInfo)
entitys := make(map[uint64]UserEntangleInfos)
for _, v := range v1 {
enInfos[v.Address] = v.Lh
}
for _, v := range v2 {
entitys[v.EID] = v.UserInfos
}
es.EnInfos, es.EnEntitys = enInfos, entitys
}
func (es *EntangleState) DecodeRLP(s *rlp.Stream) error {
type Store1 struct {
ID uint64
Value1 SortStoreBeaconAddress
Value2 SortStoreUserInfos
}
var eb Store1
if err := s.Decode(&eb); err != nil {
return err
}
es.CurExchangeID = eb.ID
es.fromSlice(eb.Value1, eb.Value2)
return nil
}
func (es *EntangleState) EncodeRLP(w io.Writer) error {
type Store1 struct {
ID uint64
Value1 SortStoreBeaconAddress
Value2 SortStoreUserInfos
}
s1, s2 := es.toSlice()
return rlp.Encode(w, &Store1{
ID: es.CurExchangeID,
Value1: s1,
Value2: s2,
})
}
func (es *EntangleState) getBeaconByID(eid uint64) *BeaconAddressInfo {
for _, v := range es.EnInfos {
if v.ExchangeID == eid {
return v
}
}
return nil
}
func (es *EntangleState) getBeaconAddressFromTo(to []byte) *BeaconAddressInfo {
for _, v := range es.EnInfos {
if bytes.Equal(v.ToAddress, to) {
return v
}
}
return nil
}
/////////////////////////////////////////////////////////////////
// keep staking enough amount asset
func (es *EntangleState) RegisterBeaconAddress(addr string, to []byte, amount *big.Int,
fee, keeptime uint64, assetType uint32, wu []*WhiteUnit, cba []string) error {
if !validFee(big.NewInt(int64(fee))) || !validKeepTime(big.NewInt(int64(keeptime))) ||
!ValidAssetType(assetType) {
return ErrInvalidParam
}
if amount.Cmp(MinStakingAmountForBeaconAddress) < 0 {
return ErrLessThanMin
}
if _, ok := es.EnInfos[addr]; ok {
return ErrRepeatRegister
}
if info := es.getBeaconAddressFromTo(to); info != nil {
return ErrRepeatToAddress
}
info := &BeaconAddressInfo{
ExchangeID: es.CurExchangeID + 1,
Address: addr,
ToAddress: to,
StakingAmount: new(big.Int).Set(amount),
AssetFlag: assetType,
Fee: fee,
KeepTime: keeptime,
EnAssets: make([]*EnAssetItem, 0, 0),
EntangleAmount: big.NewInt(0),
WhiteList: wu,
CoinBaseAddress: cba,
}
es.CurExchangeID = info.ExchangeID
es.EnInfos[addr] = info
return nil
}
func (es *EntangleState) AppendWhiteList(addr string, wlist []*WhiteUnit) error {
if val, ok := es.EnInfos[addr]; ok {
cnt := len(val.WhiteList)
if cnt+len(wlist) >= MaxWhiteListCount {
return errors.New("more than max white list")
}
for _, v := range wlist {
if ValidAssetType(v.AssetType) && ValidPK(v.Pk) {
val.WhiteList = append(val.WhiteList, v)
}
}
return nil
} else {
return ErrNoRegister
}
}
func (es *EntangleState) AppendCoinbase(addr string, coinbases []string) error {
if val, ok := es.EnInfos[addr]; ok {
cnt := len(val.CoinBaseAddress)
if cnt+len(coinbases) >= MaxCoinBase {
return errors.New("more than max coinbase")
}
for _, v := range coinbases {
if v != "" {
val.CoinBaseAddress = append(val.CoinBaseAddress, v)
}
}
return nil
} else {
return ErrNoRegister
}
}
func (es *EntangleState) AppendAmountForBeaconAddress(addr string, amount *big.Int) error {
if info, ok := es.EnInfos[addr]; !ok {
return ErrRepeatRegister
} else {
info.StakingAmount = new(big.Int).Add(info.StakingAmount, amount)
return nil
}
}
func (es *EntangleState) UpdateCoinbase(addr, update, newAddr string) error {
if val, ok := es.EnInfos[addr]; ok {
for i, v := range val.CoinBaseAddress {
if v == update {
val.CoinBaseAddress[i] = newAddr
}
}
return nil
} else {
return ErrNoRegister
}
}
func (es *EntangleState) UpdateCfgForBeaconAddress(addr string, fee, keeptime uint64, assetType uint32) error {
if !validFee(big.NewInt(int64(fee))) || !validKeepTime(big.NewInt(int64(keeptime))) ||
!ValidAssetType(assetType) {
return ErrInvalidParam
}
if info, ok := es.EnInfos[addr]; ok {
return ErrRepeatRegister
} else {
info.Fee, info.AssetFlag, info.KeepTime = fee, assetType, keeptime
}
return nil
}
func (es *EntangleState) GetCoinbase(addr string) []string {
if val, ok := es.EnInfos[addr]; ok {
res := make([]string, 0, 0)
res = append(res, val.CoinBaseAddress[:]...)
}
return nil
}
// UnregisterBeaconAddress need to check all the proves and handle all the user's burn coins
func (es *EntangleState) UnregisterBeaconAddress(addr string) error {
if val, ok := es.EnInfos[addr]; ok {
last := new(big.Int).Sub(val.StakingAmount, val.EntangleAmount)
redeemAmount(addr, last)
} else {
return ErrNoRegister
}
return nil
}
// AddEntangleItem add item in the state, keep BeaconAddress have enough amount to entangle,
func (es *EntangleState) AddEntangleItem(addr string, aType uint32, lightID uint64,
height, amount *big.Int) (*big.Int, error) {
if es.AddressInWhiteList(addr, true) {
return nil, ErrAddressInWhiteList
}
lh := es.getBeaconAddress(lightID)
if lh == nil {
return nil, ErrNoRegister
}
if !isValidAsset(aType, lh.AssetFlag) {
return nil, ErrNoUserAsset
}
sendAmount := big.NewInt(0)
var err error
lhEntitys, ok := es.EnEntitys[lightID]
if !ok {
lhEntitys = UserEntangleInfos(make(map[string]EntangleEntitys))
}
if lhEntitys != nil {
userEntitys, ok1 := lhEntitys[addr]
if !ok1 {
userEntitys = EntangleEntitys(make([]*EntangleEntity, 0, 0))
}
found := false
var userEntity *EntangleEntity
for _, v := range userEntitys {
if aType == v.AssetType {
found = true
v.EnOutsideAmount = new(big.Int).Add(v.EnOutsideAmount, amount)
userEntity = v
break
}
}
if !found {
userEntity = &EntangleEntity{
ExchangeID: lightID,
Address: addr,
AssetType: aType,
Height: new(big.Int).Set(height),
OldHeight: new(big.Int).Set(height), // init same the Height
EnOutsideAmount: new(big.Int).Set(amount),
BurnAmount: newBurnInfos(),
MaxRedeem: big.NewInt(0),
OriginAmount: big.NewInt(0),
}
userEntitys = append(userEntitys, userEntity)
}
// calc the send amount
reserve := es.getEntangledAmount(lightID, aType)
sendAmount, err = calcEntangleAmount(reserve, amount, aType)
if err != nil {
return nil, err
}
userEntity.increaseOriginAmount(sendAmount)
userEntity.updateFreeQuotaOfHeight(height, amount)
lh.addEnAsset(aType, amount)
lh.recordEntangleAmount(sendAmount)
lhEntitys[addr] = userEntitys
es.EnEntitys[lightID] = lhEntitys
}
return sendAmount, nil
}
// BurnAsset user burn the czz asset to exchange the outside asset,the caller keep the burn was true.
// verify the txid,keep equal amount czz
// returns the amount czz by user's burnned, took out fee by beaconaddress
func (es *EntangleState) BurnAsset(addr string, aType uint32, lightID, height uint64,
amount *big.Int) (*big.Int, error) {
light := es.getBeaconAddress(lightID)
if light == nil {
return nil, ErrNoRegister
}
lhEntitys, ok := es.EnEntitys[lightID]
if !ok {
return nil, ErrNoRegister
}
userEntitys, ok1 := lhEntitys[addr]
if !ok1 {
return nil, ErrNoUserReg
}
// self redeem amount, maybe add the free quota in the BeaconAddress
validAmount := userEntitys.getAllRedeemableAmount()
if amount.Cmp(validAmount) > 0 {
return nil, ErrNotEnouthBurn
}
var userEntity *EntangleEntity
for _, v := range userEntitys {
if aType == v.AssetType {
userEntity = v
break
}
}
if userEntity == nil {
return nil, ErrNoUserAsset
}
reserve := es.getEntangledAmount(lightID, aType)
base, divisor, err := getRedeemRateByBurnCzz(reserve, aType)
if err != nil {
return nil, err
}
// get out asset for burn czz
outAmount := new(big.Int).Div(new(big.Int).Mul(amount, base), divisor)
userEntity.BurnAmount.addBurnItem(height, amount, outAmount)
res := new(big.Int).Div(new(big.Int).Mul(amount, big.NewInt(int64(light.Fee))), big.NewInt(int64(MAXBASEFEE)))
return new(big.Int).Sub(amount, res), nil
}
func (es *EntangleState) SetInitPoolAmount(amount1, amount2 *big.Int) {
es.PoolAmount1, es.PoolAmount2 = new(big.Int).Set(amount1), new(big.Int).Set(amount2)
}
func (es *EntangleState) AddPoolAmount(amount1, amount2 *big.Int) {
es.PoolAmount1 = new(big.Int).Add(es.PoolAmount1, amount1)
es.PoolAmount2 = new(big.Int).Add(es.PoolAmount2, amount2)
}
func (es *EntangleState) SubPoolAmount1(amount *big.Int) {
es.PoolAmount1 = new(big.Int).Sub(es.PoolAmount1, amount)
}
func (es *EntangleState) SubPoolAmount2(amount *big.Int) {
es.PoolAmount2 = new(big.Int).Sub(es.PoolAmount2, amount)
}
//////////////////////////////////////////////////////////////////////
func redeemAmount(addr string, amount *big.Int) error {
if amount.Sign() > 0 {
}
return nil
}
func calcEntangleAmount(reserve, reqAmount *big.Int, atype uint32) (*big.Int, error) {
switch atype {
case ExpandedTxEntangle_Doge:
return toDoge2(reserve, reqAmount), nil
case ExpandedTxEntangle_Ltc:
return toLtc2(reserve, reqAmount), nil
case ExpandedTxEntangle_Btc:
return toBtc(reserve, reqAmount), nil
case ExpandedTxEntangle_Bsv, ExpandedTxEntangle_Bch:
return toBchOrBsv(reserve, reqAmount), nil
default:
return nil, ErrNoUserAsset
}
}
func getRedeemRateByBurnCzz(reserve *big.Int, atype uint32) (*big.Int, *big.Int, error) {
return nil, nil, ErrNoUserAsset
}
func (es *EntangleState) AddressInWhiteList(addr string, self bool) bool {
for k, val := range es.EnInfos {
if self && equalAddress(k, addr) {
return true
}
if val.addressInWhiteList(addr) {
return true
}
}
return false
}
func (es *EntangleState) getEntangledAmount(lightID uint64, atype uint32) *big.Int {
aa := big.NewInt(0)
if lhEntitys, ok := es.EnEntitys[lightID]; ok {
for _, userEntitys := range lhEntitys {
for _, vv := range userEntitys {
if atype == vv.AssetType {
aa = aa.Add(aa, vv.EnOutsideAmount)
break
}
}
}
}
return aa
}
func (es *EntangleState) getBeaconAddress(id uint64) *BeaconAddressInfo {
for _, val := range es.EnInfos {
if val.ExchangeID == id {
return val
}
}
return nil
}
func (es *EntangleState) getAllEntangleAmount(atype uint32) *big.Int {
all := big.NewInt(0)
for _, val := range es.EnInfos {
for _, v := range val.EnAssets {
if v.AssetType == atype {
all = all.Add(all, v.Amount)
break
}
}
}
return all
}
//Minimum pledge amount = 1 million CZZ + (cumulative cross-chain buying CZZ - cumulative cross-chain selling CZZ) x exchange rate ratio
func (es *EntangleState) LimitStakingAmount(eid uint64, atype uint32) *big.Int {
lh := es.getBeaconAddress(eid)
if lh != nil {
l := new(big.Int).Sub(lh.StakingAmount, lh.EntangleAmount)
if l.Sign() > 0 {
l = new(big.Int).Sub(l, MinStakingAmountForBeaconAddress)
if l.Sign() > 0 {
return l
}
}
}
return nil
}
//////////////////////////////////////////////////////////////////////
// UpdateQuotaOnBlock called in insertBlock for update user's quota state
func (es *EntangleState) UpdateQuotaOnBlock(height uint64) error {
for _, lh := range es.EnInfos {
userEntitys, ok := es.EnEntitys[lh.ExchangeID]
if !ok {
fmt.Println("cann't found the BeaconAddress id:", lh.ExchangeID)
} else {
for _, userEntity := range userEntitys {
res := userEntity.updateFreeQuotaForAllType(big.NewInt(int64(height)), big.NewInt(int64(lh.KeepTime)))
lh.updateFreeQuota(res)
}
}
}
return nil
}
// TourAllUserBurnInfo Tours all user's burned asset and check which is timeout to redeem
func (es *EntangleState) TourAllUserBurnInfo(height uint64) map[uint64]UserTimeOutBurnInfo {
// maybe get cache for recently burned user
res := make(map[uint64]UserTimeOutBurnInfo)
for k, users := range es.EnEntitys {
userItems := make(map[string]TypeTimeOutBurnInfo)
for k1, entitys := range users {
items := entitys.getBurnTimeout(height, true)
if len(items) > 0 {
userItems[k1] = items
}
}
if len(userItems) > 0 {
res[k] = UserTimeOutBurnInfo(userItems)
}
}
return res
}
func (es *EntangleState) UpdateStateToPunished(infos map[uint64]UserTimeOutBurnInfo) {
for eid, items := range infos {
userEntitys, ok := es.EnEntitys[eid]
if ok {
// set state=3 after be punished by system consensus
userEntitys.updateBurnState(3, items)
}
}
}
func SummayPunishedInfos(infos map[uint64]UserTimeOutBurnInfo) map[uint64]LHPunishedItems {
res := make(map[uint64]LHPunishedItems)
for k, userInfos := range infos {
items := make([]*LHPunishedItem, 0, 0)
for addr, val := range userInfos {
items = append(items, &LHPunishedItem{
User: addr,
All: val.getAll(),
})
}
res[k] = LHPunishedItems(items)
}
return res
}
func (es *EntangleState) FinishBeaconAddressPunished(eid uint64, amount *big.Int) error {
beacon := es.getBeaconByID(eid)
if beacon == nil {
return ErrNoRegister
}
// get limit staking warnning message
return beacon.updatePunished(amount)
}
func (es *EntangleState) verifyBurnProof(info *BurnProofInfo) error {
userEntitys, ok := es.EnEntitys[info.LightID]
if !ok {
fmt.Println("verifyBurnProof:cann't found the BeaconAddress id:", info.LightID)
return ErrNoRegister
} else {
for addr1, userEntity := range userEntitys {
if bytes.Equal(info.Address.ScriptAddress(), []byte(addr1)) {
return userEntity.verifyBurnProof(info)
} else {
return ErrNotMatchUser
}
}
}
return nil
}
// FinishHandleUserBurn the BeaconAddress finish the burn item
func (es *EntangleState) FinishHandleUserBurn(lightID, height uint64, addr czzutil.Address, atype uint32, amount *big.Int) error {
userEntitys, ok := es.EnEntitys[lightID]
if !ok {
fmt.Println("FinishHandleUserBurn:cann't found the BeaconAddress id:", lightID)
return ErrNoRegister
} else {
for addr1, userEntity := range userEntitys {
if bytes.Equal(addr.ScriptAddress(), []byte(addr1)) {
userEntity.finishBurnState(height, amount, atype)
}
}
}
return nil
}
func (es *EntangleState) ToBytes() []byte {
// maybe rlp encode
data, err := rlp.EncodeToBytes(es)
if err != nil {
log.Fatal("Failed to RLP encode EntangleState", "err", err)
}
return data
}
func (es *EntangleState) Save() error {
return nil
}
func (es *EntangleState) Load() error {
return nil
}
func Hash(es *EntangleState) chainhash.Hash {
return chainhash.HashH(es.ToBytes())
}
func NewEntangleState() *EntangleState {
return &EntangleState{
EnInfos: make(map[string]*BeaconAddressInfo),
EnEntitys: make(map[uint64]UserEntangleInfos),
CurExchangeID: 0,
PoolAmount1: big.NewInt(0),
PoolAmount2: big.NewInt(0),
}
}