-
Notifications
You must be signed in to change notification settings - Fork 38
/
file.go
814 lines (696 loc) · 21.2 KB
/
file.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/big"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/crypto"
"github.com/miguelmota/go-solidity-sha3"
)
// ComplaintRequest is the structure of request for complaint request
type ComplaintRequest struct {
S string `json:"s"`
D []string `json:"d"`
}
// OtComplaintRequest is the structure of request for ot complaint request
type OtComplaintRequest struct {
S string `json:"s"`
P []string `json:"p"`
}
// AtomicSwapRequest is the structure of request for atomic_swap request
type AtomicSwapRequest struct {
S string `json:"s"`
P []string `json:"p"`
}
// AtomicSwapVcRequest is the structure of request for atomic_swap_vc request
type AtomicSwapVcRequest struct {
S string `json:"s"`
P []string `json:"p"`
}
//ComplaintSecret is the structure of secret for complaint mode
type ComplaintSecret struct {
S string `json:"s"`
}
//AtomicSwapSecret is the structure of secret for atomic swap mode
type AtomicSwapSecret struct {
S string `json:"s"`
}
//AtomicSwapVcSecret is the structure of secret for atomic swap mode
type AtomicSwapVcSecret struct {
S string `json:"s"`
R string `json:"r"`
}
//VRFSecret is the structure of secret for vrf mode
type VRFSecret struct {
R string `json:"r"`
}
//Claim is the structure of claim
type Claim struct {
I uint64 `json:"i"`
J uint64 `json:"j"`
K string `json:"k"`
M []string `json:"m"`
}
//ComplaintReceipt is the struct of receipt for complaint mode
type ComplaintReceipt struct {
S string `json:"s"`
K string `json:"k"`
C uint64 `json:"c"`
}
//AtomicSwapReceipt is the struct of receipt for atomic swap mode
type AtomicSwapReceipt struct {
S string `json:"s"`
VW string `json:"vw"`
C uint64 `json:"c"`
}
//AtomicSwapVcReceipt is the struct of receipt for atomic swap mode
type AtomicSwapVcReceipt struct {
D string `json:"d"`
}
//VRFReceipt is the struct of receipt for vrf mode
type VRFReceipt struct {
G string `json:"g"`
}
//Bulletin is the struct of bulletin.
type Bulletin struct {
Mode string `json:"mode"`
Size string `json:"size"` //plain
S string `json:"s"`
N string `json:"n"`
SigmaMKLRoot string `json:"sigma_mkl_root"`
VRFMetaDigest string `json:"vrf_meta_digest"` // table
}
func pathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func copyKeyStore(dir string, keyStore string) error {
rd, err := ioutil.ReadDir(dir)
if err != nil {
return err
}
defer func() {
os.RemoveAll(dir)
}()
exist := false
for _, fi := range rd {
if fi.IsDir() {
continue
} else {
exist = true
err = copyFile(dir+"/"+fi.Name(), keyStore)
if err != nil {
return err
}
}
}
if !exist {
return fmt.Errorf("keystore file does not exist")
}
return nil
}
func readBulletinFile(filePath string, Log ILogger) (Bulletin, error) {
var b Bulletin
bl, err := ioutil.ReadFile(filePath)
if err != nil {
Log.Warnf("failed to open bulletin file. err=%v", err)
return b, errors.New("failed to open bulletin file")
}
Log.Debugf("success to read bulletin info from file %v", filePath)
err = json.Unmarshal(bl, &b)
if err != nil {
Log.Warnf("failed to parse bulletin file. err=%v", err)
return b, errors.New("failed to parse bulletin file")
}
return b, nil
}
func readReceiptForComplaint(filePath string, Log ILogger) ([]byte, ComplaintReceipt, error) {
var r ComplaintReceipt
rf, err := ioutil.ReadFile(filePath)
if err != nil {
Log.Warnf("failed to read receipt file. err=%v", err)
return rf, r, errors.New("failed to read receipt file")
}
Log.Debugf("success to read receipt info from file %v", filePath)
err = json.Unmarshal(rf, &r)
if err != nil {
Log.Warnf("failed to parse receipt file. err=%v", err)
return rf, r, errors.New("failed to parse receipt file")
}
Log.Debugf("success to parse receipt info")
return rf, r, nil
}
func signRecptForComplaint(key *keystore.Key, AliceAddr string, sessionID string, receipt ComplaintReceipt, price int64, expireAt int64, bulletin Bulletin, Log ILogger) ([]byte, error) {
sessionInt := new(big.Int)
sessionInt, rs := sessionInt.SetString(sessionID, 10)
if !rs {
Log.Warnf("failed to convert sessionId.")
return nil, errors.New("convert sessionId error")
}
bltKey, err := calcuBltKey(bulletin)
if err != nil {
Log.Warnf("failed to calculate blt key. tx.Bulletin=%v, err=%v", bulletin, err)
return nil, errors.New("failed to calculate blt key")
}
receiptHash := solsha3.SoliditySHA3( // types
[]string{"uint256", "address", "address", "bytes32", "bytes32", "bytes32", "uint64", "uint256", "uint256"},
// values
[]interface{}{
sessionInt,
fmt.Sprintf("%v", key.Address.Hex()),
AliceAddr,
bltKey,
"0x" + receipt.S,
"0x" + receipt.K,
receipt.C,
big.NewInt(price),
big.NewInt(expireAt),
})
Log.Debugf("generate receipt receiptHash=0x%02x\n", receiptHash)
receiptHash = solsha3.SoliditySHA3WithPrefix(receiptHash)
// Bob sign the receipt
sig, err := crypto.Sign(receiptHash, key.PrivateKey)
if err != nil {
Log.Warnf("generate signature for receipt hash error. err=%v", err)
return nil, errors.New("signature error")
}
Log.Debugf("success to generate signature")
return sig, nil
}
func readReceiptForAtomicSwap(filePath string, Log ILogger) ([]byte, AtomicSwapReceipt, error) {
var r AtomicSwapReceipt
rf, err := ioutil.ReadFile(filePath)
if err != nil {
Log.Warnf("failed to read receipt file. err=%v", err)
return rf, r, errors.New("failed to read receipt file")
}
// config := string(conf)
err = json.Unmarshal(rf, &r)
if err != nil {
Log.Warnf("failed to parse receipt file. err=%v", err)
return rf, r, errors.New("failed to parse receipt file")
}
return rf, r, nil
}
func signRecptForAtomicSwap(key *keystore.Key, AliceAddr string, sessionID string, receipt AtomicSwapReceipt, price int64, expireAt int64, Log ILogger) ([]byte, error) {
sessionInt := new(big.Int)
sessionInt, rs := sessionInt.SetString(sessionID, 10)
if !rs {
Log.Warnf("failed to convert sessionId.")
return nil, errors.New("convert sessionId error")
}
vwInt := new(big.Int)
vwInt, rs = vwInt.SetString(receipt.VW, 10)
if !rs {
Log.Warnf("failed to convert vw. vw=%v", receipt.VW)
return nil, errors.New("failed to convert vw")
}
receiptHash := solsha3.SoliditySHA3( // types
[]string{"uint256", "address", "address", "bytes32", "uint256", "uint64", "uint256", "uint256"},
// values
[]interface{}{
sessionInt,
fmt.Sprintf("%v", key.Address.Hex()),
AliceAddr,
"0x" + receipt.S,
vwInt,
receipt.C,
big.NewInt(price),
big.NewInt(expireAt),
})
// Log.Debugf("sessionInt=%v, addr=%v,S=%v,vwInt=%v,count=%v,price=%v,expireAt=%v",
// sessionInt,
// fmt.Sprintf("%v", key.Address.Hex()),
// "0x"+receipt.S,
// vwInt,
// receipt.C,
// big.NewInt(price),
// big.NewInt(expireAt))
Log.Debugf("generate receipt receiptHash=0x%02x\n", receiptHash)
receiptHash = solsha3.SoliditySHA3WithPrefix(receiptHash)
// Bob sign the receipt
sig, err := crypto.Sign(receiptHash, key.PrivateKey)
if err != nil {
Log.Warnf("generate signature for receipt hash error. err=%v", err)
return nil, errors.New("signature error")
}
Log.Debugf("generate receipt signature=%v", hexutil.Encode(sig))
return sig, nil
}
func readReceiptForAtomicSwapVc(filePath string, Log ILogger) ([]byte, AtomicSwapVcReceipt, error) {
var r AtomicSwapVcReceipt
rf, err := ioutil.ReadFile(filePath)
if err != nil {
Log.Warnf("failed to read receipt file. err=%v", err)
return rf, r, errors.New("failed to read receipt file")
}
// config := string(conf)
err = json.Unmarshal(rf, &r)
if err != nil {
Log.Warnf("failed to parse receipt file. err=%v", err)
return rf, r, errors.New("failed to parse receipt file")
}
return rf, r, nil
}
func signRecptForAtomicSwapVc(key *keystore.Key, AliceAddr string, sessionID string, receipt AtomicSwapVcReceipt, price int64, expireAt int64, Log ILogger) ([]byte, error) {
sessionInt := new(big.Int)
sessionInt, rs := sessionInt.SetString(sessionID, 10)
if !rs {
Log.Warnf("failed to convert sessionId.")
return nil, errors.New("convert sessionId error")
}
digestInt := new(big.Int)
digestInt, rs = digestInt.SetString(receipt.D, 10)
if !rs {
Log.Warnf("failed to convert digestInt. receipt.D=%v", receipt.D)
return nil, errors.New("failed to convert digestInt")
}
receiptHash := solsha3.SoliditySHA3( // types
[]string{"uint256", "address", "address", "uint256", "uint256", "uint256"},
// values
[]interface{}{
sessionInt,
fmt.Sprintf("%v", key.Address.Hex()),
AliceAddr,
digestInt,
big.NewInt(price),
big.NewInt(expireAt),
})
Log.Debugf("generate receipt receiptHash=0x%02x\n", receiptHash)
receiptHash = solsha3.SoliditySHA3WithPrefix(receiptHash)
// Bob sign the receipt
sig, err := crypto.Sign(receiptHash, key.PrivateKey)
if err != nil {
Log.Warnf("generate signature for receipt hash error. err=%v", err)
return nil, errors.New("signature error")
}
Log.Debugf("generate receipt signature=%v", hexutil.Encode(sig))
return sig, nil
}
func readVRFReceipt(filePath string, Log ILogger) ([]byte, VRFReceipt, error) {
var r VRFReceipt
rf, err := ioutil.ReadFile(filePath)
if err != nil {
Log.Warnf("failed to read receipt file. err=%v", err)
return rf, r, errors.New("failed to read receipt file")
}
// config := string(conf)
err = json.Unmarshal(rf, &r)
if err != nil {
Log.Warnf("failed to parse receipt file. err=%v", err)
return rf, r, errors.New("failed to parse receipt file")
}
return rf, r, nil
}
func signRecptForVRFQ(key *keystore.Key, AliceAddr string, sessionID string, receipt VRFReceipt, price int64, expireAt int64, Log ILogger) ([]byte, error) {
sessionInt := new(big.Int)
sessionInt, rs := sessionInt.SetString(sessionID, 10)
if !rs {
Log.Warnf("failed to convert sessionId.")
return nil, errors.New("convert sessionId error")
}
gs := strings.Split(receipt.G, " ")
if len(gs) != 3 {
Log.Warnf("invalid g. g=%v", receipt.G)
return nil, errors.New("invalid g. ")
}
var _g_exp_r [2]*big.Int
_g_exp_r[0] = new(big.Int)
_g_exp_r[0], rs = _g_exp_r[0].SetString(gs[1], 10)
if !rs {
Log.Warnf("failed to convert g[1]. g[1] = %v", gs[1])
return nil, errors.New("failed to convert g[1]")
}
_g_exp_r[1] = new(big.Int)
_g_exp_r[1], rs = _g_exp_r[1].SetString(gs[2], 10)
if !rs {
Log.Warnf("failed to convert g[2]. g[2] = %v", gs[2])
return nil, errors.New("failed to convert g[2]")
}
receiptHash := solsha3.SoliditySHA3( // types
[]string{"uint256", "address", "address", "uint256", "uint256", "uint256", "uint256"},
// values
[]interface{}{
sessionInt,
fmt.Sprintf("%v", key.Address.Hex()),
AliceAddr,
_g_exp_r[0],
_g_exp_r[1],
big.NewInt(price),
big.NewInt(expireAt),
})
Log.Debugf("generate receipt receiptHash=0x%02x\n", receiptHash)
receiptHash = solsha3.SoliditySHA3WithPrefix(receiptHash)
// Bob sign the receipt
sig, err := crypto.Sign(receiptHash, key.PrivateKey)
if err != nil {
Log.Warnf("generate signature for receipt hash error. err=%v", err)
return nil, errors.New("signature error")
}
return sig, nil
}
func readSeed0ForComplaint(filePath string, Log ILogger) (s ComplaintSecret, err error) {
sf, err := ioutil.ReadFile(filePath)
if err != nil {
Log.Warnf("failed to open seed2 file. err=%v", err)
return s, errors.New("failed to open seed2 file")
}
// config := string(conf)
err = json.Unmarshal(sf, &s)
if err != nil {
Log.Warnf("failed to parse seed2 file. err=%v", err)
return s, errors.New("failed to parse seed2 file")
}
return
}
func BobSaveSecretForComplaint(secret ComplaintSecret, filePath string, Log ILogger) error {
sByte, err := json.Marshal(&secret)
if err != nil {
Log.Warnf("Failed to save secret for Bob. err=%v")
return errors.New("Failed to save secret for Bob")
}
err = ioutil.WriteFile(filePath, sByte, 0644)
if err != nil {
Log.Errorf("Failed to save bulletin file. err=%v", err)
return errors.New("Save bulletin file error")
}
return nil
}
func readSeed0ForAtomicSwap(filePath string, Log ILogger) (s AtomicSwapSecret, err error) {
sf, err := ioutil.ReadFile(filePath)
if err != nil {
Log.Warnf("failed to open seed2 file. err=%v", err)
return s, errors.New("failed to open seed2 file")
}
// config := string(conf)
err = json.Unmarshal(sf, &s)
if err != nil {
Log.Warnf("failed to parse seed2 file. err=%v", err)
return s, errors.New("failed to parse seed2 file")
}
return
}
func BobSaveSecretForAtomicSwap(secret AtomicSwapSecret, filePath string, Log ILogger) error {
sByte, err := json.Marshal(&secret)
if err != nil {
Log.Warnf("Failed to save secret for Bob. err=%v")
return errors.New("Failed to save secret for Bob")
}
err = ioutil.WriteFile(filePath, sByte, 0644)
if err != nil {
Log.Errorf("Failed to save bulletin file. err=%v", err)
return errors.New("Save bulletin file error")
}
return nil
}
func readSeed0ForAtomicSwapVc(filePath string, Log ILogger) (s AtomicSwapVcSecret, err error) {
sf, err := ioutil.ReadFile(filePath)
if err != nil {
Log.Warnf("failed to open seed2 file. err=%v", err)
return s, errors.New("failed to open seed2 file")
}
// config := string(conf)
err = json.Unmarshal(sf, &s)
if err != nil {
Log.Warnf("failed to parse seed2 file. err=%v", err)
return s, errors.New("failed to parse seed2 file")
}
return
}
func BobSaveSecretForAtomicSwapVc(secret AtomicSwapVcSecret, filePath string, Log ILogger) error {
sByte, err := json.Marshal(&secret)
if err != nil {
Log.Warnf("failed to save secret for Bob. err=%v")
return errors.New("failed to save secret for Bob")
}
err = ioutil.WriteFile(filePath, sByte, 0644)
if err != nil {
Log.Errorf("failed to save bulletin file. err=%v", err)
return errors.New("save bulletin file error")
}
return nil
}
func readSeed0ForVRFQ(filePath string, Log ILogger) (s VRFSecret, err error) {
sf, err := ioutil.ReadFile(filePath)
if err != nil {
Log.Warnf("failed to open seed2 file. err=%v", err)
return s, errors.New("failed to open seed2 file")
}
err = json.Unmarshal(sf, &s)
if err != nil {
Log.Warnf("failed to parse seed2 file. err=%v", err)
return s, errors.New("failed to parse seed2 file")
}
return
}
func BobSaveSecretForVRFQ(secret VRFSecret, filePath string, Log ILogger) error {
sByte, err := json.Marshal(&secret)
if err != nil {
Log.Warnf("Failed to save secret for Bob. err=%v")
return errors.New("Failed to save secret for Bob")
}
err = ioutil.WriteFile(filePath, sByte, 0644)
if err != nil {
Log.Errorf("Failed to save bulletin file. err=%v", err)
return errors.New("Save bulletin file error")
}
return nil
}
func readClaim(filePath string, Log ILogger) (Claim, error) {
var c Claim
clamin, err := ioutil.ReadFile(filePath)
if err != nil {
Log.Warnf("failed to read claim file. err=%v", err)
return c, errors.New("failed to read claim file")
}
err = json.Unmarshal(clamin, &c)
if err != nil {
Log.Warnf("failed to parse claim file. err=%v", err)
return c, errors.New("failed to parse claim file")
}
return c, nil
}
func copyFile(src string, dest string) error {
inputSrc, err := ioutil.ReadFile(src)
if err != nil {
return err
}
err = ioutil.WriteFile(dest, inputSrc, 0644)
if err != nil {
return err
}
return nil
}
func saveBasicConfig(config BasicConfig, basicFile string) error {
configByte, err := json.Marshal(&config)
if err != nil {
return fmt.Errorf("failed to marshal config, err=%v", err)
}
f, err := os.Create(basicFile)
if err != nil {
return fmt.Errorf("failed to open basic file=%v. err=%v", basicFile, err)
}
defer f.Close()
_, err = f.Write(configByte)
if err != nil {
return fmt.Errorf("failed to write config to basic file. file=%v, err=%v", basicFile, err)
}
return nil
}
func savePublishRawFile(fileByte []byte, AlicePublishDir string, dataFile string, Log ILogger) (string, error) {
ts := fmt.Sprintf("%v", time.Now().UnixNano())
rs, err := pathExists(AlicePublishDir)
if err != nil {
Log.Errorf("check path exist error. err=%v", err)
return "", errors.New("save publish file error")
}
if !rs {
err = os.Mkdir(AlicePublishDir, os.ModePerm)
if err != nil {
Log.Errorf("create dictionary %v error. err=%v", AlicePublishDir, err)
return "", errors.New("save publish file error")
}
Log.Debugf("success to create dictionary. dir=%v", AlicePublishDir)
}
dir := AlicePublishDir + "/" + ts
err = os.Mkdir(dir, os.ModePerm)
if err != nil {
Log.Errorf("create dictionary %v error. err=%v", dir, err)
return dir, errors.New("save publish file error")
}
Log.Debugf("success to create dictionary. dir=%v", dir)
filePath := dir + "/" + dataFile
f, err := os.Create(filePath)
if err != nil {
return dir, fmt.Errorf("failed to open publish data file=%v. err=%v", filePath, err)
}
defer f.Close()
_, err = f.Write(fileByte)
if err != nil {
return dir, fmt.Errorf("failed to write file to publish data file. file=%v, err=%v", filePath, err)
}
return dir, nil
}
func publishRawData(filePath string, fileName string, mode string, column string, keys []int) error {
var cmd *exec.Cmd
if mode == TRANSACTION_MODE_PLAIN_POD {
cmd = exec.Command(BConf.PublishBINPath, "-m", mode, "-f", filePath+"/"+fileName, "-o", filePath, "-c", column)
} else {
fs := strings.Split(fileName, ".")
parameters := []string{"-m", mode, "-f", filePath + "/" + fileName, "-o", filePath, "-t", fs[len(fs)-1], "-k"}
for _, k := range keys {
parameters = append(parameters, fmt.Sprintf("%v", k))
}
cmd = exec.Command(BConf.PublishBINPath, parameters...)
}
// fmt.Printf("exec command: %v\n", cmd)
rs, err := cmd.Output()
if err != nil {
return fmt.Errorf("publish execution error! %v, %v", err, string(rs))
}
return nil
}
func reNameFolder(filePath string, newPath string) error {
err := os.Rename(filePath, newPath)
if err != nil {
return err
}
return nil
}
func savePublishExtraInfo(extra PublishExtraInfo, path string) error {
extraByte, err := json.Marshal(&extra)
if err != nil {
return fmt.Errorf("failed to marshal publish extra info, err=%v", err)
}
f, err := os.Create(path)
if err != nil {
return fmt.Errorf("failed to create extra file=%v. err=%v", path, err)
}
defer f.Close()
_, err = f.Write(extraByte)
if err != nil {
return fmt.Errorf("failed to write config to extra file. file=%v, err=%v", path, err)
}
return nil
}
func readExtraFile(filepath string) (extra PublishExtraInfo, err error) {
ex, err := ioutil.ReadFile(filepath)
if err != nil {
return
}
err = json.Unmarshal(ex, &extra)
if err != nil {
return
}
return
}
func calcuCntforComplaint(requestFile string) (int64, error) {
var req ComplaintRequest
request, err := ioutil.ReadFile(requestFile)
if err != nil {
return 0, fmt.Errorf("failed to read request file. err=%v", err)
}
err = json.Unmarshal(request, &req)
if err != nil {
return 0, fmt.Errorf("failed to read request file. err=%v", err)
}
var count int64
for _, d := range req.D {
cs := strings.Split(d, "-")
if len(cs) != 2 {
return 0, fmt.Errorf("invalid demand. %v", d)
}
c, err := strconv.ParseInt(cs[len(cs)-1], 10, 64)
if err != nil {
return 0, fmt.Errorf("invalid demand. %v", d)
}
count += c
}
return count, nil
}
func calcuCntforOtComplaint(requestFile string) (int64, error) {
var req OtComplaintRequest
request, err := ioutil.ReadFile(requestFile)
if err != nil {
return 0, fmt.Errorf("failed to read request file. err=%v", err)
}
err = json.Unmarshal(request, &req)
if err != nil {
return 0, fmt.Errorf("failed to read request file. err=%v", err)
}
var count int64
for _, p := range req.P {
cs := strings.Split(p, "-")
if len(cs) != 2 {
return 0, fmt.Errorf("invalid phantoms. %v", p)
}
c, err := strconv.ParseInt(cs[len(cs)-1], 10, 64)
if err != nil {
return 0, fmt.Errorf("invalid phantoms. %v", p)
}
count += c
}
return count, nil
}
func calcuCntforAS(requestFile string) (int64, error) {
var req AtomicSwapRequest
request, err := ioutil.ReadFile(requestFile)
if err != nil {
return 0, fmt.Errorf("failed to read request file. err=%v", err)
}
err = json.Unmarshal(request, &req)
if err != nil {
return 0, fmt.Errorf("failed to read request file. err=%v", err)
}
var count int64
for _, p := range req.P {
cs := strings.Split(p, "-")
if len(cs) != 2 {
return 0, fmt.Errorf("invalid demands. %v", p)
}
c, err := strconv.ParseInt(cs[len(cs)-1], 10, 64)
if err != nil {
return 0, fmt.Errorf("invalid demands. %v", p)
}
count += c
}
return count, nil
}
func calcuCntforASVC(requestFile string) (int64, error) {
var req AtomicSwapRequest
request, err := ioutil.ReadFile(requestFile)
if err != nil {
return 0, fmt.Errorf("failed to read request file. err=%v", err)
}
err = json.Unmarshal(request, &req)
if err != nil {
return 0, fmt.Errorf("failed to read request file. err=%v", err)
}
var count int64
for _, p := range req.P {
cs := strings.Split(p, "-")
if len(cs) != 2 {
return 0, fmt.Errorf("invalid demands. %v", p)
}
c, err := strconv.ParseInt(cs[len(cs)-1], 10, 64)
if err != nil {
return 0, fmt.Errorf("invalid demands. %v", p)
}
count += c
}
return count, nil
}