forked from DNAProject/DNA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
param.go
1042 lines (970 loc) · 33.5 KB
/
param.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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2018 The DNA Authors
* This file is part of The DNA library.
*
* The DNA is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The DNA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The DNA. If not, see <http://www.gnu.org/licenses/>.
*/
package governance
import (
"fmt"
"io"
"math"
"github.com/dnaproject2/DNA/common"
"github.com/dnaproject2/DNA/common/serialization"
"github.com/dnaproject2/DNA/smartcontract/service/native/utils"
)
type RegisterCandidateParam struct {
PeerPubkey string
Address common.Address
InitPos uint32
Caller []byte
KeyNo uint32
}
func (this *RegisterCandidateParam) Serialize(w io.Writer) error {
if err := serialization.WriteString(w, this.PeerPubkey); err != nil {
return fmt.Errorf("serialization.WriteString, request peerPubkey error: %v", err)
}
if err := serialization.WriteVarBytes(w, this.Address[:]); err != nil {
return fmt.Errorf("serialization.WriteVarBytes, address address error: %v", err)
}
if err := utils.WriteVarUint(w, uint64(this.InitPos)); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize initPos error: %v", err)
}
if err := serialization.WriteVarBytes(w, this.Caller); err != nil {
return fmt.Errorf("serialization.WriteVarBytes, serialize caller error: %v", err)
}
if err := utils.WriteVarUint(w, uint64(this.KeyNo)); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize keyNo error: %v", err)
}
return nil
}
func (this *RegisterCandidateParam) Deserialize(r io.Reader) error {
peerPubkey, err := serialization.ReadString(r)
if err != nil {
return fmt.Errorf("serialization.ReadString, deserialize peerPubkey error: %v", err)
}
address, err := utils.ReadAddress(r)
if err != nil {
return fmt.Errorf("utils.ReadAddress, deserialize address error: %v", err)
}
initPos, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize initPos error: %v", err)
}
if initPos > math.MaxUint32 {
return fmt.Errorf("initPos larger than max of uint32")
}
caller, err := serialization.ReadVarBytes(r)
if err != nil {
return fmt.Errorf("serialization.ReadVarBytes, deserialize caller error: %v", err)
}
keyNo, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize keyNo error: %v", err)
}
if keyNo > math.MaxUint32 {
return fmt.Errorf("initPos larger than max of uint32")
}
this.PeerPubkey = peerPubkey
this.Address = address
this.InitPos = uint32(initPos)
this.Caller = caller
this.KeyNo = uint32(keyNo)
return nil
}
type UnRegisterCandidateParam struct {
PeerPubkey string
Address common.Address
}
func (this *UnRegisterCandidateParam) Serialize(w io.Writer) error {
if err := serialization.WriteString(w, this.PeerPubkey); err != nil {
return fmt.Errorf("serialization.WriteString, request peerPubkey error: %v", err)
}
if err := serialization.WriteVarBytes(w, this.Address[:]); err != nil {
return fmt.Errorf("serialization.WriteVarBytes, address address error: %v", err)
}
return nil
}
func (this *UnRegisterCandidateParam) Deserialize(r io.Reader) error {
peerPubkey, err := serialization.ReadString(r)
if err != nil {
return fmt.Errorf("serialization.ReadString, deserialize peerPubkey error: %v", err)
}
address, err := utils.ReadAddress(r)
if err != nil {
return fmt.Errorf("utils.ReadAddress, deserialize address error: %v", err)
}
this.PeerPubkey = peerPubkey
this.Address = address
return nil
}
type QuitNodeParam struct {
PeerPubkey string
Address common.Address
}
func (this *QuitNodeParam) Serialize(w io.Writer) error {
if err := serialization.WriteString(w, this.PeerPubkey); err != nil {
return fmt.Errorf("serialization.WriteString, deserialize peerPubkey error: %v", err)
}
if err := serialization.WriteVarBytes(w, this.Address[:]); err != nil {
return fmt.Errorf("serialization.WriteVarBytes, address address error: %v", err)
}
return nil
}
func (this *QuitNodeParam) Deserialize(r io.Reader) error {
peerPubkey, err := serialization.ReadString(r)
if err != nil {
return fmt.Errorf("serialization.ReadString, deserialize peerPubkey error: %v", err)
}
address, err := utils.ReadAddress(r)
if err != nil {
return fmt.Errorf("utils.ReadAddress, deserialize address error: %v", err)
}
this.PeerPubkey = peerPubkey
this.Address = address
return nil
}
type ApproveCandidateParam struct {
PeerPubkey string
}
func (this *ApproveCandidateParam) Serialize(w io.Writer) error {
if err := serialization.WriteString(w, this.PeerPubkey); err != nil {
return fmt.Errorf("serialization.WriteString, serialize peerPubkey error: %v", err)
}
return nil
}
func (this *ApproveCandidateParam) Deserialize(r io.Reader) error {
peerPubkey, err := serialization.ReadString(r)
if err != nil {
return fmt.Errorf("serialization.ReadString, deserialize peerPubkey error: %v", err)
}
this.PeerPubkey = peerPubkey
return nil
}
type RejectCandidateParam struct {
PeerPubkey string
}
func (this *RejectCandidateParam) Serialize(w io.Writer) error {
if err := serialization.WriteString(w, this.PeerPubkey); err != nil {
return fmt.Errorf("serialization.WriteString, serialize peerPubkey error: %v", err)
}
return nil
}
func (this *RejectCandidateParam) Deserialize(r io.Reader) error {
peerPubkey, err := serialization.ReadString(r)
if err != nil {
return fmt.Errorf("serialization.ReadString, deserialize peerPubkey error: %v", err)
}
this.PeerPubkey = peerPubkey
return nil
}
type BlackNodeParam struct {
PeerPubkeyList []string
}
func (this *BlackNodeParam) Serialize(w io.Writer) error {
if err := utils.WriteVarUint(w, uint64(len(this.PeerPubkeyList))); err != nil {
return fmt.Errorf("serialization.WriteVarUint, serialize peerPubkeyList length error: %v", err)
}
for _, v := range this.PeerPubkeyList {
if err := serialization.WriteString(w, v); err != nil {
return fmt.Errorf("serialization.WriteString, serialize peerPubkey error: %v", err)
}
}
return nil
}
func (this *BlackNodeParam) Deserialize(r io.Reader) error {
n, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("serialization.ReadVarUint, deserialize peerPubkeyList length error: %v", err)
}
peerPubkeyList := make([]string, 0)
for i := 0; uint64(i) < n; i++ {
k, err := serialization.ReadString(r)
if err != nil {
return fmt.Errorf("serialization.ReadString, deserialize peerPubkey error: %v", err)
}
peerPubkeyList = append(peerPubkeyList, k)
}
this.PeerPubkeyList = peerPubkeyList
return nil
}
type WhiteNodeParam struct {
PeerPubkey string
}
func (this *WhiteNodeParam) Serialize(w io.Writer) error {
if err := serialization.WriteString(w, this.PeerPubkey); err != nil {
return fmt.Errorf("serialization.WriteString, serialize peerPubkey error: %v", err)
}
return nil
}
func (this *WhiteNodeParam) Deserialize(r io.Reader) error {
peerPubkey, err := serialization.ReadString(r)
if err != nil {
return fmt.Errorf("serialization.ReadString, deserialize peerPubkey error: %v", err)
}
this.PeerPubkey = peerPubkey
return nil
}
type AuthorizeForPeerParam struct {
Address common.Address
PeerPubkeyList []string
PosList []uint32
}
func (this *AuthorizeForPeerParam) Serialize(w io.Writer) error {
if len(this.PeerPubkeyList) > 1024 {
return fmt.Errorf("length of input list > 1024")
}
if len(this.PeerPubkeyList) != len(this.PosList) {
return fmt.Errorf("length of PeerPubkeyList != length of PosList")
}
if err := serialization.WriteVarBytes(w, this.Address[:]); err != nil {
return fmt.Errorf("serialization.WriteVarBytes, address address error: %v", err)
}
if err := utils.WriteVarUint(w, uint64(len(this.PeerPubkeyList))); err != nil {
return fmt.Errorf("serialization.WriteVarUint, serialize peerPubkeyList length error: %v", err)
}
for _, v := range this.PeerPubkeyList {
if err := serialization.WriteString(w, v); err != nil {
return fmt.Errorf("serialization.WriteString, serialize peerPubkey error: %v", err)
}
}
if err := utils.WriteVarUint(w, uint64(len(this.PosList))); err != nil {
return fmt.Errorf("serialization.WriteVarUint, serialize posList length error: %v", err)
}
for _, v := range this.PosList {
if err := utils.WriteVarUint(w, uint64(v)); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize pos error: %v", err)
}
}
return nil
}
func (this *AuthorizeForPeerParam) Deserialize(r io.Reader) error {
address, err := utils.ReadAddress(r)
if err != nil {
return fmt.Errorf("utils.ReadAddress, deserialize address error: %v", err)
}
n, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("serialization.ReadVarUint, deserialize peerPubkeyList length error: %v", err)
}
if n > 1024 {
return fmt.Errorf("length of input list > 1024")
}
peerPubkeyList := make([]string, 0)
for i := 0; uint64(i) < n; i++ {
k, err := serialization.ReadString(r)
if err != nil {
return fmt.Errorf("serialization.ReadString, deserialize peerPubkey error: %v", err)
}
peerPubkeyList = append(peerPubkeyList, k)
}
m, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("serialization.ReadVarUint, deserialize posList length error: %v", err)
}
posList := make([]uint32, 0)
for i := 0; uint64(i) < m; i++ {
k, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize pos error: %v", err)
}
if k > math.MaxUint32 {
return fmt.Errorf("pos larger than max of uint32")
}
posList = append(posList, uint32(k))
}
if m != n {
return fmt.Errorf("length of PeerPubkeyList != length of PosList")
}
this.Address = address
this.PeerPubkeyList = peerPubkeyList
this.PosList = posList
return nil
}
type WithdrawParam struct {
Address common.Address
PeerPubkeyList []string
WithdrawList []uint32
}
func (this *WithdrawParam) Serialize(w io.Writer) error {
if len(this.PeerPubkeyList) > 1024 {
return fmt.Errorf("length of input list > 1024")
}
if len(this.PeerPubkeyList) != len(this.WithdrawList) {
return fmt.Errorf("length of PeerPubkeyList != length of WithdrawList, contract params error")
}
if err := serialization.WriteVarBytes(w, this.Address[:]); err != nil {
return fmt.Errorf("serialization.WriteVarBytes, address address error: %v", err)
}
if err := utils.WriteVarUint(w, uint64(len(this.PeerPubkeyList))); err != nil {
return fmt.Errorf("serialization.WriteVarUint, serialize peerPubkeyList length error: %v", err)
}
for _, v := range this.PeerPubkeyList {
if err := serialization.WriteString(w, v); err != nil {
return fmt.Errorf("serialization.WriteString, serialize peerPubkey error: %v", err)
}
}
if err := utils.WriteVarUint(w, uint64(len(this.WithdrawList))); err != nil {
return fmt.Errorf("serialization.WriteVarUint, serialize withdrawList length error: %v", err)
}
for _, v := range this.WithdrawList {
if err := utils.WriteVarUint(w, uint64(v)); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize withdraw error: %v", err)
}
}
return nil
}
func (this *WithdrawParam) Deserialize(r io.Reader) error {
address, err := utils.ReadAddress(r)
if err != nil {
return fmt.Errorf("utils.ReadAddress, deserialize address error: %v", err)
}
n, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("serialization.ReadVarUint, deserialize peerPubkeyList length error: %v", err)
}
if n > 1024 {
return fmt.Errorf("length of input list > 1024")
}
peerPubkeyList := make([]string, 0)
for i := 0; uint64(i) < n; i++ {
k, err := serialization.ReadString(r)
if err != nil {
return fmt.Errorf("serialization.ReadString, deserialize peerPubkey error: %v", err)
}
peerPubkeyList = append(peerPubkeyList, k)
}
m, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("serialization.ReadVarUint, deserialize withdrawList length error: %v", err)
}
withdrawList := make([]uint32, 0)
for i := 0; uint64(i) < m; i++ {
k, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize withdraw error: %v", err)
}
if k > math.MaxUint32 {
return fmt.Errorf("pos larger than max of uint32")
}
withdrawList = append(withdrawList, uint32(k))
}
if m != n {
return fmt.Errorf("length of PeerPubkeyList != length of WithdrawList, contract params error")
}
this.Address = address
this.PeerPubkeyList = peerPubkeyList
this.WithdrawList = withdrawList
return nil
}
type Configuration struct {
N uint32
C uint32
K uint32
L uint32
BlockMsgDelay uint32
HashMsgDelay uint32
PeerHandshakeTimeout uint32
MaxBlockChangeView uint32
}
func (this *Configuration) Serialize(w io.Writer) error {
if err := utils.WriteVarUint(w, uint64(this.N)); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize n error: %v", err)
}
if err := utils.WriteVarUint(w, uint64(this.C)); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize c error: %v", err)
}
if err := utils.WriteVarUint(w, uint64(this.K)); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize k error: %v", err)
}
if err := utils.WriteVarUint(w, uint64(this.L)); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize l error: %v", err)
}
if err := utils.WriteVarUint(w, uint64(this.BlockMsgDelay)); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize block_msg_delay error: %v", err)
}
if err := utils.WriteVarUint(w, uint64(this.HashMsgDelay)); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize hash_msg_delay error: %v", err)
}
if err := utils.WriteVarUint(w, uint64(this.PeerHandshakeTimeout)); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize peer_handshake_timeout error: %v", err)
}
if err := utils.WriteVarUint(w, uint64(this.MaxBlockChangeView)); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize max_block_change_view error: %v", err)
}
return nil
}
func (this *Configuration) Deserialize(r io.Reader) error {
n, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize n error: %v", err)
}
c, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize c error: %v", err)
}
k, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize k error: %v", err)
}
l, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize l error: %v", err)
}
blockMsgDelay, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize blockMsgDelay error: %v", err)
}
hashMsgDelay, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize hashMsgDelay error: %v", err)
}
peerHandshakeTimeout, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize peerHandshakeTimeout error: %v", err)
}
maxBlockChangeView, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize maxBlockChangeView error: %v", err)
}
if n > math.MaxUint32 {
return fmt.Errorf("n larger than max of uint32")
}
if c > math.MaxUint32 {
return fmt.Errorf("c larger than max of uint32")
}
if k > math.MaxUint32 {
return fmt.Errorf("k larger than max of uint32")
}
if l > math.MaxUint32 {
return fmt.Errorf("l larger than max of uint32")
}
if blockMsgDelay > math.MaxUint32 {
return fmt.Errorf("blockMsgDelay larger than max of uint32")
}
if hashMsgDelay > math.MaxUint32 {
return fmt.Errorf("hashMsgDelay larger than max of uint32")
}
if peerHandshakeTimeout > math.MaxUint32 {
return fmt.Errorf("peerHandshakeTimeout larger than max of uint32")
}
if maxBlockChangeView > math.MaxUint32 {
return fmt.Errorf("maxBlockChangeView larger than max of uint32")
}
this.N = uint32(n)
this.C = uint32(c)
this.K = uint32(k)
this.L = uint32(l)
this.BlockMsgDelay = uint32(blockMsgDelay)
this.HashMsgDelay = uint32(hashMsgDelay)
this.PeerHandshakeTimeout = uint32(peerHandshakeTimeout)
this.MaxBlockChangeView = uint32(maxBlockChangeView)
return nil
}
type PreConfig struct {
Configuration *Configuration
SetView uint32
}
func (this *PreConfig) Serialize(w io.Writer) error {
if err := this.Configuration.Serialize(w); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize configuration error: %v", err)
}
if err := utils.WriteVarUint(w, uint64(this.SetView)); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize setView error: %v", err)
}
return nil
}
func (this *PreConfig) Deserialize(r io.Reader) error {
config := new(Configuration)
err := config.Deserialize(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize configuration error: %v", err)
}
setView, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize setView error: %v", err)
}
if setView > math.MaxUint32 {
return fmt.Errorf("setView larger than max of uint32")
}
this.Configuration = config
this.SetView = uint32(setView)
return nil
}
type GlobalParam struct {
CandidateFee uint64 //unit: 10^-9 ong
MinInitStake uint32 //min init pos
CandidateNum uint32 //num of candidate and consensus node
PosLimit uint32 //authorize pos limit is initPos*posLimit
A uint32 //fee split to all consensus node
B uint32 //fee split to all candidate node
Yita uint32 //split curve coefficient
Penalty uint32 //authorize pos penalty percentage
}
func (this *GlobalParam) Serialize(w io.Writer) error {
if err := utils.WriteVarUint(w, this.CandidateFee); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize candidateFee error: %v", err)
}
if err := utils.WriteVarUint(w, uint64(this.MinInitStake)); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize minInitStake error: %v", err)
}
if err := utils.WriteVarUint(w, uint64(this.CandidateNum)); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize candidateNum error: %v", err)
}
if err := utils.WriteVarUint(w, uint64(this.PosLimit)); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize posLimit error: %v", err)
}
if err := utils.WriteVarUint(w, uint64(this.A)); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize a error: %v", err)
}
if err := utils.WriteVarUint(w, uint64(this.B)); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize b error: %v", err)
}
if err := utils.WriteVarUint(w, uint64(this.Yita)); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize yita error: %v", err)
}
if err := utils.WriteVarUint(w, uint64(this.Penalty)); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize penalty error: %v", err)
}
return nil
}
func (this *GlobalParam) Deserialize(r io.Reader) error {
candidateFee, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize candidateFee error: %v", err)
}
minInitStake, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize minInitStake error: %v", err)
}
candidateNum, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize candidateNum error: %v", err)
}
posLimit, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize posLimit error: %v", err)
}
a, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize a error: %v", err)
}
b, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize b error: %v", err)
}
yita, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize yita error: %v", err)
}
penalty, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize penalty error: %v", err)
}
if minInitStake > math.MaxUint32 {
return fmt.Errorf("minInitStake larger than max of uint32")
}
if candidateNum > math.MaxUint32 {
return fmt.Errorf("candidateNum larger than max of uint32")
}
if posLimit > math.MaxUint32 {
return fmt.Errorf("posLimit larger than max of uint32")
}
if a > math.MaxUint32 {
return fmt.Errorf("a larger than max of uint32")
}
if b > math.MaxUint32 {
return fmt.Errorf("b larger than max of uint32")
}
if yita > math.MaxUint32 {
return fmt.Errorf("yita larger than max of uint32")
}
if penalty > math.MaxUint32 {
return fmt.Errorf("penalty larger than max of uint32")
}
this.CandidateFee = candidateFee
this.MinInitStake = uint32(minInitStake)
this.CandidateNum = uint32(candidateNum)
this.PosLimit = uint32(posLimit)
this.A = uint32(a)
this.B = uint32(b)
this.Yita = uint32(yita)
this.Penalty = uint32(penalty)
return nil
}
type GlobalParam2 struct {
MinAuthorizePos uint32 //min ONT of each authorization, 500 default
CandidateFeeSplitNum uint32 //num of peer can receive motivation(include consensus and candidate)
DappFee uint32 //fee split to dapp bonus
Field2 []byte //reserved field
Field3 []byte //reserved field
Field4 []byte //reserved field
Field5 []byte //reserved field
Field6 []byte //reserved field
}
func (this *GlobalParam2) Serialize(w io.Writer) error {
if this.MinAuthorizePos == 0 {
return fmt.Errorf("globalParam2.MinAuthorizePos can not be 0")
}
if this.DappFee > 100 {
return fmt.Errorf("globalParam2.DappFee must <= 100")
}
if err := utils.WriteVarUint(w, uint64(this.MinAuthorizePos)); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize minAuthorizePos error: %v", err)
}
if err := utils.WriteVarUint(w, uint64(this.CandidateFeeSplitNum)); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize candidateFeeSplitNum error: %v", err)
}
if err := utils.WriteVarUint(w, uint64(this.DappFee)); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize dappFee error: %v", err)
}
if err := serialization.WriteVarBytes(w, this.Field2); err != nil {
return fmt.Errorf("serialization.WriteVarBytes, serialize field2 error: %v", err)
}
if err := serialization.WriteVarBytes(w, this.Field3); err != nil {
return fmt.Errorf("serialization.WriteVarBytes, serialize field3 error: %v", err)
}
if err := serialization.WriteVarBytes(w, this.Field4); err != nil {
return fmt.Errorf("serialization.WriteVarBytes, serialize field4 error: %v", err)
}
if err := serialization.WriteVarBytes(w, this.Field5); err != nil {
return fmt.Errorf("serialization.WriteVarBytes, serialize field5 error: %v", err)
}
if err := serialization.WriteVarBytes(w, this.Field6); err != nil {
return fmt.Errorf("serialization.WriteVarBytes, serialize field6 error: %v", err)
}
return nil
}
func (this *GlobalParam2) Deserialize(r io.Reader) error {
minAuthorizePos, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize minAuthorizePos error: %v", err)
}
candidateFeeSplitNum, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize candidateFeeSplitNum error: %v", err)
}
dappFee, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize dappFee error: %v", err)
}
field2, err := serialization.ReadVarBytes(r)
if err != nil {
return fmt.Errorf("serialization.ReadVarBytes, deserialize field2 error: %v", err)
}
field3, err := serialization.ReadVarBytes(r)
if err != nil {
return fmt.Errorf("serialization.ReadVarBytes, deserialize field3 error: %v", err)
}
field4, err := serialization.ReadVarBytes(r)
if err != nil {
return fmt.Errorf("serialization.ReadVarBytes, deserialize field4 error: %v", err)
}
field5, err := serialization.ReadVarBytes(r)
if err != nil {
return fmt.Errorf("serialization.ReadVarBytes, deserialize field5 error: %v", err)
}
field6, err := serialization.ReadVarBytes(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize field6 error: %v", err)
}
if minAuthorizePos > math.MaxUint32 {
return fmt.Errorf("minAuthorizePos larger than max of uint32")
}
if candidateFeeSplitNum > math.MaxUint32 {
return fmt.Errorf("candidateFeeSplitNum larger than max of uint32")
}
if minAuthorizePos == 0 {
return fmt.Errorf("globalParam2.MinAuthorizePos can not be 0")
}
if dappFee > 100 {
return fmt.Errorf("globalParam2.DappFee must <= 100")
}
this.MinAuthorizePos = uint32(minAuthorizePos)
this.CandidateFeeSplitNum = uint32(candidateFeeSplitNum)
this.DappFee = uint32(dappFee)
this.Field2 = field2
this.Field3 = field3
this.Field4 = field4
this.Field5 = field5
this.Field6 = field6
return nil
}
type SplitCurve struct {
Yi []uint32
}
func (this *SplitCurve) Serialize(w io.Writer) error {
if len(this.Yi) != 101 {
return fmt.Errorf("length of split curve != 101")
}
if err := utils.WriteVarUint(w, uint64(len(this.Yi))); err != nil {
return fmt.Errorf("serialization.WriteVarUint, serialize Yi length error: %v", err)
}
for _, v := range this.Yi {
if err := utils.WriteVarUint(w, uint64(v)); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize splitCurve error: %v", err)
}
}
return nil
}
func (this *SplitCurve) Deserialize(r io.Reader) error {
n, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("serialization.ReadVarUint, deserialize Yi length error: %v", err)
}
yi := make([]uint32, 0)
for i := 0; uint64(i) < n; i++ {
k, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize splitCurve error: %v", err)
}
if k > math.MaxUint32 {
return fmt.Errorf("yi larger than max of uint32")
}
yi = append(yi, uint32(k))
}
this.Yi = yi
return nil
}
type TransferPenaltyParam struct {
PeerPubkey string
Address common.Address
}
func (this *TransferPenaltyParam) Serialize(w io.Writer) error {
if err := serialization.WriteString(w, this.PeerPubkey); err != nil {
return fmt.Errorf("serialization.WriteString, serialize peerPubkey error: %v", err)
}
if err := serialization.WriteVarBytes(w, this.Address[:]); err != nil {
return fmt.Errorf("serialization.WriteVarBytes, serialize address error: %v", err)
}
return nil
}
func (this *TransferPenaltyParam) Deserialize(r io.Reader) error {
peerPubkey, err := serialization.ReadString(r)
if err != nil {
return fmt.Errorf("serialization.ReadString, deserialize peerPubkey error: %v", err)
}
address, err := utils.ReadAddress(r)
if err != nil {
return fmt.Errorf("utils.ReadAddress, deserialize address error: %v", err)
}
this.PeerPubkey = peerPubkey
this.Address = address
return nil
}
type WithdrawOngParam struct {
Address common.Address
}
func (this *WithdrawOngParam) Serialize(w io.Writer) error {
if err := serialization.WriteVarBytes(w, this.Address[:]); err != nil {
return fmt.Errorf("serialization.WriteVarBytes, serialize address error: %v", err)
}
return nil
}
func (this *WithdrawOngParam) Deserialize(r io.Reader) error {
address, err := utils.ReadAddress(r)
if err != nil {
return fmt.Errorf("utils.ReadAddress, deserialize address error: %v", err)
}
this.Address = address
return nil
}
type ChangeMaxAuthorizationParam struct {
PeerPubkey string
Address common.Address
MaxAuthorize uint32
}
func (this *ChangeMaxAuthorizationParam) Serialize(w io.Writer) error {
if err := serialization.WriteString(w, this.PeerPubkey); err != nil {
return fmt.Errorf("serialization.WriteString, serialize peerPubkey error: %v", err)
}
if err := serialization.WriteVarBytes(w, this.Address[:]); err != nil {
return fmt.Errorf("serialization.WriteVarBytes, serialize address error: %v", err)
}
if err := utils.WriteVarUint(w, uint64(this.MaxAuthorize)); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize maxAuthorize error: %v", err)
}
return nil
}
func (this *ChangeMaxAuthorizationParam) Deserialize(r io.Reader) error {
peerPubkey, err := serialization.ReadString(r)
if err != nil {
return fmt.Errorf("serialization.ReadString, deserialize peerPubkey error: %v", err)
}
address, err := utils.ReadAddress(r)
if err != nil {
return fmt.Errorf("utils.ReadAddress, deserialize address error: %v", err)
}
maxAuthorize, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize maxAuthorize error: %v", err)
}
if maxAuthorize > math.MaxUint32 {
return fmt.Errorf("peerCost larger than max of uint32")
}
this.PeerPubkey = peerPubkey
this.Address = address
this.MaxAuthorize = uint32(maxAuthorize)
return nil
}
type SetPeerCostParam struct {
PeerPubkey string
Address common.Address
PeerCost uint32
}
func (this *SetPeerCostParam) Serialize(w io.Writer) error {
if err := serialization.WriteString(w, this.PeerPubkey); err != nil {
return fmt.Errorf("serialization.WriteString, serialize peerPubkey error: %v", err)
}
if err := serialization.WriteVarBytes(w, this.Address[:]); err != nil {
return fmt.Errorf("serialization.WriteVarBytes, serialize address error: %v", err)
}
if err := utils.WriteVarUint(w, uint64(this.PeerCost)); err != nil {
return fmt.Errorf("serialization.WriteBool, serialize peerCost error: %v", err)
}
return nil
}
func (this *SetPeerCostParam) Deserialize(r io.Reader) error {
peerPubkey, err := serialization.ReadString(r)
if err != nil {
return fmt.Errorf("serialization.ReadString, deserialize peerPubkey error: %v", err)
}
address, err := utils.ReadAddress(r)
if err != nil {
return fmt.Errorf("utils.ReadAddress, deserialize address error: %v", err)
}
peerCost, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("serialization.ReadBool, deserialize peerCost error: %v", err)
}
if peerCost > math.MaxUint32 {
return fmt.Errorf("peerCost larger than max of uint32")
}
this.PeerPubkey = peerPubkey
this.Address = address
this.PeerCost = uint32(peerCost)
return nil
}
type WithdrawFeeParam struct {
Address common.Address
}
func (this *WithdrawFeeParam) Serialize(w io.Writer) error {
if err := serialization.WriteVarBytes(w, this.Address[:]); err != nil {
return fmt.Errorf("serialization.WriteVarBytes, serialize address error: %v", err)
}
return nil
}
func (this *WithdrawFeeParam) Deserialize(r io.Reader) error {
address, err := utils.ReadAddress(r)
if err != nil {
return fmt.Errorf("utils.ReadAddress, deserialize address error: %v", err)
}
this.Address = address
return nil
}
type PromisePos struct {
PeerPubkey string
PromisePos uint64
}
func (this *PromisePos) Serialize(w io.Writer) error {
if err := serialization.WriteString(w, this.PeerPubkey); err != nil {
return fmt.Errorf("serialization.WriteString, serialize peerPubkey error: %v", err)
}
if err := utils.WriteVarUint(w, this.PromisePos); err != nil {
return fmt.Errorf("serialization.WriteBool, serialize promisePos error: %v", err)
}
return nil
}
func (this *PromisePos) Deserialize(r io.Reader) error {
peerPubkey, err := serialization.ReadString(r)
if err != nil {
return fmt.Errorf("serialization.ReadString, deserialize peerPubkey error: %v", err)
}
promisePos, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("serialization.ReadBool, deserialize promisePos error: %v", err)
}
this.PeerPubkey = peerPubkey
this.PromisePos = promisePos
return nil
}
type ChangeInitPosParam struct {
PeerPubkey string
Address common.Address
Pos uint32
}
func (this *ChangeInitPosParam) Serialize(w io.Writer) error {
if err := serialization.WriteString(w, this.PeerPubkey); err != nil {
return fmt.Errorf("serialization.WriteString, serialize peerPubkey error: %v", err)
}
if err := serialization.WriteVarBytes(w, this.Address[:]); err != nil {
return fmt.Errorf("serialization.WriteVarBytes, serialize address error: %v", err)
}
if err := utils.WriteVarUint(w, uint64(this.Pos)); err != nil {
return fmt.Errorf("serialization.WriteBool, serialize pos error: %v", err)
}
return nil
}
func (this *ChangeInitPosParam) Deserialize(r io.Reader) error {
peerPubkey, err := serialization.ReadString(r)
if err != nil {
return fmt.Errorf("serialization.ReadString, deserialize peerPubkey error: %v", err)
}
address, err := utils.ReadAddress(r)
if err != nil {
return fmt.Errorf("utils.ReadAddress, deserialize address error: %v", err)
}
pos, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("serialization.ReadBool, deserialize pos error: %v", err)
}
if pos > math.MaxUint32 {
return fmt.Errorf("pos larger than max of uint32")
}