-
Notifications
You must be signed in to change notification settings - Fork 38
/
db.go
731 lines (677 loc) · 24 KB
/
db.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
package main
import (
"encoding/json"
"errors"
"fmt"
"time"
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
_ "github.com/mattn/go-sqlite3"
)
var DBConn *xorm.Engine
func connectSQLite(dbpath string) (*xorm.Engine, error) {
Log := Logger.NewSessionLogger()
db, err := xorm.NewEngine("sqlite3", dbpath)
if err != nil {
Log.Errorf("failed to connect to db:%v", err.Error())
return nil, err
}
err = db.Ping()
if err != nil {
Log.Errorf("failed to connect to db: %v", err.Error())
return nil, err
}
Log.Debugf("ping db success")
db.SetMapper(core.SnakeMapper{})
db.ShowSQL(false)
db.Logger().SetLevel(core.LOG_DEBUG)
err = db.CreateTables(&BobTx{})
if err != nil {
Log.Errorf("failed to create table Bob_tx, err=%v", err)
return db, errors.New("failed to create table Bob_tx")
}
Log.Debugf("initialize table Bob_tx success")
err = db.CreateTables(&AliceTx{})
if err != nil {
Log.Errorf("failed to create table Alice_tx, err=%v", err)
return db, errors.New("failed to create table Alice_tx")
}
Log.Debugf("initialize table Alice_tx success")
err = db.CreateTables(&PodLog{})
if err != nil {
Log.Errorf("failed to create table pod_log, err=%v", err)
return db, errors.New("failed to create table pod_log")
}
Log.Debugf("initialize table pod_log success")
err = db.Sync2(new(BobTx))
if err != nil {
Log.Warnf("table Bob_tx sync error, err=%v", err)
return nil, err
}
err = db.Sync2(new(AliceTx))
if err != nil {
Log.Warnf("table Alice_tx sync error, err=%v", err)
return nil, err
}
err = db.Sync2(new(PodLog))
if err != nil {
Log.Warnf("table pod_log sync error, err=%v", err)
return nil, err
}
db.ShowSQL(false)
db.ShowExecTime(true)
return db, err
}
type BobTx struct {
SessionId string `json:"sessionId" xorm:"text pk not null"`
Status string `json:"status" xorm:"text not null"`
AliceIP string `json:"AliceIP" xorm:"text not null"`
AliceAddr string `json:"AliceAddr" xorm:"text not null"`
AliceSyncthingId string `json:"AliceSyncthingId" xorm:"text"`
BobAddr string `json:"BobAddr" xorm:"text not null"`
Mode string `json:"mode" xorm:"text not null"`
SubMode string `json:"subMode" xorm:"text not null"`
OT bool `json:"ot" xorm:"bool"`
Size string `json:"size" xorm:"text not null"`
S string `json:"s" xorm:"text not null"`
N string `json:"n" xorm:"text not null"`
SigmaMKLRoot string `json:"sigmaMklRoot" xorm:"text not null"`
Price int64 `json:"price" xorm:"INTEGER"`
UnitPrice int64 `json:"unit_price" xorm:"INTEGER"`
ExpireAt int64 `json:"expireAt" xorm:"INTEGER"`
Count int64 `json:"count" xorm:"INTEGER"`
Demands string `json:"demands" xorm:"text"`
Phantoms string `json:"phantoms" xorm:"text"`
KeyName string `json:"keyName" xorm:"text"`
KeyValue string `json:"keyValue" xorm:"text"`
PhantomKeyValue string `json:"phantomKeyValue" xorm:"text"`
CreateDate time.Time `json:"createDate" xorm:"created"`
UpdateDate time.Time `json:"updateDate" xorm:"updated"`
}
func insertBobTxToDB(transaction BobTransaction) error {
var tx BobTx
tx.SessionId = transaction.SessionID
tx.Status = transaction.Status
tx.AliceIP = transaction.AliceIP
tx.AliceAddr = transaction.AliceAddr
tx.BobAddr = transaction.BobAddr
tx.Mode = transaction.Mode
tx.SubMode = transaction.SubMode
tx.OT = transaction.OT
tx.Size = transaction.Bulletin.Size
tx.S = transaction.Bulletin.S
tx.N = transaction.Bulletin.N
tx.SigmaMKLRoot = transaction.Bulletin.SigmaMKLRoot
tx.Price = transaction.Price
tx.Count = transaction.Count
tx.ExpireAt = transaction.ExpireAt
if tx.Mode == TRANSACTION_MODE_TABLE_POD {
switch tx.SubMode {
case TRANSACTION_SUB_MODE_COMPLAINT:
if tx.OT {
demands, err := json.Marshal(&transaction.PlainOTComplaint.Demands)
if err != nil {
return fmt.Errorf("failed to parse demands: %v", err)
}
tx.Demands = string(demands)
phantoms, err := json.Marshal(&transaction.PlainOTComplaint.Phantoms)
if err != nil {
return fmt.Errorf("failed to parse phantoms: %v", err)
}
tx.Phantoms = string(phantoms)
} else {
demands, err := json.Marshal(&transaction.PlainComplaint.Demands)
if err != nil {
return fmt.Errorf("failed to parse demands: %v", err)
}
tx.Demands = string(demands)
}
case TRANSACTION_SUB_MODE_ATOMIC_SWAP:
demands, err := json.Marshal(&transaction.PlainAtomicSwap.Demands)
if err != nil {
return fmt.Errorf("failed to parse demands: %v", err)
}
tx.Demands = string(demands)
case TRANSACTION_SUB_MODE_ATOMIC_SWAP_VC:
demands, err := json.Marshal(&transaction.PlainAtomicSwapVc.Demands)
if err != nil {
return fmt.Errorf("failed to parse demands: %v", err)
}
tx.Demands = string(demands)
}
} else {
switch tx.SubMode {
case TRANSACTION_SUB_MODE_COMPLAINT:
if tx.OT {
demands, err := json.Marshal(&transaction.TableOTComplaint.Demands)
if err != nil {
return fmt.Errorf("failed to parse demands: %v", err)
}
tx.Demands = string(demands)
phantoms, err := json.Marshal(&transaction.TableOTComplaint.Phantoms)
if err != nil {
return fmt.Errorf("failed to parse phantoms: %v", err)
}
tx.Phantoms = string(phantoms)
} else {
demands, err := json.Marshal(&transaction.TableComplaint.Demands)
if err != nil {
return fmt.Errorf("failed to parse demands: %v", err)
}
tx.Demands = string(demands)
}
case TRANSACTION_SUB_MODE_ATOMIC_SWAP:
demands, err := json.Marshal(&transaction.TableAtomicSwap.Demands)
if err != nil {
return fmt.Errorf("failed to parse demands: %v", err)
}
tx.Demands = string(demands)
case TRANSACTION_SUB_MODE_ATOMIC_SWAP_VC:
demands, err := json.Marshal(&transaction.TableAtomicSwapVc.Demands)
if err != nil {
return fmt.Errorf("failed to parse demands: %v", err)
}
tx.Demands = string(demands)
case TRANSACTION_SUB_MODE_VRF:
if tx.OT {
tx.KeyName = transaction.TableOTVRF.KeyName
keyValue, err := json.Marshal(&transaction.TableOTVRF.KeyValue)
if err != nil {
return fmt.Errorf("failed to parse keyValue: %v", err)
}
tx.KeyValue = string(keyValue)
phantomKeyValue, err := json.Marshal(&transaction.TableOTVRF.PhantomKeyValue)
if err != nil {
return fmt.Errorf("failed to parse PhantomKeyValue: %v", err)
}
tx.PhantomKeyValue = string(phantomKeyValue)
} else {
tx.KeyName = transaction.TableVRF.KeyName
keyValue, err := json.Marshal(&transaction.TableVRF.KeyValue)
if err != nil {
return fmt.Errorf("failed to parse keyValue: %v", err)
}
tx.KeyValue = string(keyValue)
}
}
}
_, err := DBConn.Insert(&tx)
if err != nil {
return fmt.Errorf("failed to insert transaction to Bob_tx. err=%v", err)
}
return nil
}
func updateBobTxToDB(transaction BobTransaction) error {
var tx BobTx
tx.SessionId = transaction.SessionID
tx.Status = transaction.Status
tx.AliceIP = transaction.AliceIP
tx.AliceAddr = transaction.AliceAddr
tx.BobAddr = transaction.BobAddr
tx.Mode = transaction.Mode
tx.SubMode = transaction.SubMode
tx.OT = transaction.OT
tx.Size = transaction.Bulletin.Size
tx.S = transaction.Bulletin.S
tx.N = transaction.Bulletin.N
tx.SigmaMKLRoot = transaction.Bulletin.SigmaMKLRoot
tx.Price = transaction.Price
tx.UnitPrice = transaction.UnitPrice
tx.Count = transaction.Count
tx.ExpireAt = transaction.ExpireAt
if tx.Mode == TRANSACTION_MODE_TABLE_POD {
switch tx.SubMode {
case TRANSACTION_SUB_MODE_COMPLAINT:
if tx.OT {
demands, err := json.Marshal(&transaction.PlainOTComplaint.Demands)
if err != nil {
return fmt.Errorf("failed to parse demands: %v", err)
}
tx.Demands = string(demands)
phantoms, err := json.Marshal(&transaction.PlainOTComplaint.Phantoms)
if err != nil {
return fmt.Errorf("failed to parse phantoms: %v", err)
}
tx.Phantoms = string(phantoms)
} else {
demands, err := json.Marshal(&transaction.PlainComplaint.Demands)
if err != nil {
return fmt.Errorf("failed to parse demands: %v", err)
}
tx.Demands = string(demands)
}
case TRANSACTION_SUB_MODE_ATOMIC_SWAP:
demands, err := json.Marshal(&transaction.PlainAtomicSwap.Demands)
if err != nil {
return fmt.Errorf("failed to parse demands: %v", err)
}
tx.Demands = string(demands)
case TRANSACTION_SUB_MODE_ATOMIC_SWAP_VC:
demands, err := json.Marshal(&transaction.PlainAtomicSwapVc.Demands)
if err != nil {
return fmt.Errorf("failed to parse demands: %v", err)
}
tx.Demands = string(demands)
}
} else {
switch tx.SubMode {
case TRANSACTION_SUB_MODE_COMPLAINT:
if tx.OT {
demands, err := json.Marshal(&transaction.TableOTComplaint.Demands)
if err != nil {
return fmt.Errorf("failed to parse demands: %v", err)
}
tx.Demands = string(demands)
phantoms, err := json.Marshal(&transaction.TableOTComplaint.Phantoms)
if err != nil {
return fmt.Errorf("failed to parse phantoms: %v", err)
}
tx.Phantoms = string(phantoms)
} else {
demands, err := json.Marshal(&transaction.TableComplaint.Demands)
if err != nil {
return fmt.Errorf("failed to parse demands: %v", err)
}
tx.Demands = string(demands)
}
case TRANSACTION_SUB_MODE_ATOMIC_SWAP:
demands, err := json.Marshal(&transaction.TableAtomicSwap.Demands)
if err != nil {
return fmt.Errorf("failed to parse demands: %v", err)
}
tx.Demands = string(demands)
case TRANSACTION_SUB_MODE_ATOMIC_SWAP_VC:
demands, err := json.Marshal(&transaction.TableAtomicSwapVc.Demands)
if err != nil {
return fmt.Errorf("failed to parse demands: %v", err)
}
tx.Demands = string(demands)
case TRANSACTION_SUB_MODE_VRF:
if tx.OT {
tx.KeyName = transaction.TableOTVRF.KeyName
keyValue, err := json.Marshal(&transaction.TableOTVRF.KeyValue)
if err != nil {
return fmt.Errorf("failed to parse keyValue: %v", err)
}
tx.KeyValue = string(keyValue)
phantomKeyValue, err := json.Marshal(&transaction.TableOTVRF.PhantomKeyValue)
if err != nil {
return fmt.Errorf("failed to parse PhantomKeyValue: %v", err)
}
tx.PhantomKeyValue = string(phantomKeyValue)
} else {
tx.KeyName = transaction.TableVRF.KeyName
keyValue, err := json.Marshal(&transaction.TableVRF.KeyValue)
if err != nil {
return fmt.Errorf("failed to parse keyValue: %v", err)
}
tx.KeyValue = string(keyValue)
}
}
}
_, err := DBConn.Where("session_id = ?", tx.SessionId).Update(&tx)
if err != nil {
return fmt.Errorf("failed to insert transaction to Bob_tx. err=%v", err)
}
return nil
}
func loadBobTxFromDB(sessionID string) (BobTransaction, error) {
var tx BobTx
var transaction BobTransaction
rs, err := DBConn.Where("session_id = ?", sessionID).Get(&tx)
if err != nil {
return transaction, fmt.Errorf("failed to read transaction for Bob. sessionID=%v, err=%v", sessionID, err)
}
if !rs {
return transaction, nil
}
transaction.SessionID = tx.SessionId
transaction.Status = tx.Status
transaction.AliceIP = tx.AliceIP
transaction.AliceAddr = tx.AliceAddr
transaction.BobAddr = tx.BobAddr
transaction.Mode = tx.Mode
transaction.SubMode = tx.SubMode
transaction.OT = tx.OT
transaction.Bulletin.Size = tx.Size
transaction.Bulletin.S = tx.S
transaction.Bulletin.N = tx.N
transaction.Bulletin.SigmaMKLRoot = tx.SigmaMKLRoot
transaction.Bulletin.Mode = tx.Mode
transaction.Price = tx.Price
transaction.UnitPrice = tx.UnitPrice
transaction.Count = tx.Count
transaction.ExpireAt = tx.ExpireAt
if transaction.Mode == TRANSACTION_MODE_TABLE_POD {
switch transaction.SubMode {
case TRANSACTION_SUB_MODE_COMPLAINT:
if transaction.OT {
err := json.Unmarshal([]byte(tx.Demands), &transaction.PlainOTComplaint.Demands)
if err != nil {
return transaction, fmt.Errorf("failed to parse demands: %v", err)
}
err = json.Unmarshal([]byte(tx.Phantoms), &transaction.PlainOTComplaint.Phantoms)
if err != nil {
return transaction, fmt.Errorf("failed to parse phantoms: %v", err)
}
} else {
err := json.Unmarshal([]byte(tx.Demands), &transaction.PlainComplaint.Demands)
if err != nil {
return transaction, fmt.Errorf("failed to parse demands: %v", err)
}
}
case TRANSACTION_SUB_MODE_ATOMIC_SWAP:
err := json.Unmarshal([]byte(tx.Demands), &transaction.PlainAtomicSwap.Demands)
if err != nil {
return transaction, fmt.Errorf("failed to parse demands: %v", err)
}
case TRANSACTION_SUB_MODE_ATOMIC_SWAP_VC:
err := json.Unmarshal([]byte(tx.Demands), &transaction.PlainAtomicSwapVc.Demands)
if err != nil {
return transaction, fmt.Errorf("failed to parse demands: %v", err)
}
}
} else {
switch transaction.SubMode {
case TRANSACTION_SUB_MODE_COMPLAINT:
if transaction.OT {
err := json.Unmarshal([]byte(tx.Demands), &transaction.TableOTComplaint.Demands)
if err != nil {
return transaction, fmt.Errorf("failed to parse demands: %v", err)
}
err = json.Unmarshal([]byte(tx.Phantoms), &transaction.TableOTComplaint.Phantoms)
if err != nil {
return transaction, fmt.Errorf("failed to parse phantoms: %v", err)
}
} else {
err := json.Unmarshal([]byte(tx.Demands), &transaction.TableComplaint.Demands)
if err != nil {
return transaction, fmt.Errorf("failed to parse demands: %v", err)
}
}
case TRANSACTION_SUB_MODE_ATOMIC_SWAP:
err := json.Unmarshal([]byte(tx.Demands), &transaction.TableAtomicSwap.Demands)
if err != nil {
return transaction, fmt.Errorf("failed to parse demands: %v", err)
}
case TRANSACTION_SUB_MODE_ATOMIC_SWAP_VC:
err := json.Unmarshal([]byte(tx.Demands), &transaction.TableAtomicSwapVc.Demands)
if err != nil {
return transaction, fmt.Errorf("failed to parse demands: %v", err)
}
case TRANSACTION_SUB_MODE_VRF:
if transaction.OT {
transaction.TableOTVRF.KeyName = tx.KeyName
err := json.Unmarshal([]byte(tx.KeyValue), &transaction.TableOTVRF.KeyValue)
if err != nil {
return transaction, fmt.Errorf("failed to parse keyvalue: %v", err)
}
err = json.Unmarshal([]byte(tx.PhantomKeyValue), &transaction.TableOTVRF.PhantomKeyValue)
if err != nil {
return transaction, fmt.Errorf("failed to parse phantomKeyValue: %v", err)
}
} else {
transaction.TableVRF.KeyName = tx.KeyName
err := json.Unmarshal([]byte(tx.KeyValue), &transaction.TableVRF.KeyValue)
if err != nil {
return transaction, fmt.Errorf("failed to parse keyvalue: %v", err)
}
}
}
}
return transaction, nil
}
func loadBobTxListFromDB() ([]BobTransaction, error) {
var txs []BobTx
err := DBConn.Find(&txs)
if err != nil {
return nil, fmt.Errorf("failed to read transaction for Bob. err=%v", err)
}
var transactions = make([]BobTransaction, len(txs))
for i, tx := range txs {
transactions[i].SessionID = tx.SessionId
transactions[i].Status = tx.Status
transactions[i].AliceIP = tx.AliceIP
transactions[i].AliceAddr = tx.AliceAddr
transactions[i].BobAddr = tx.BobAddr
transactions[i].Mode = tx.Mode
transactions[i].SubMode = tx.SubMode
transactions[i].OT = tx.OT
transactions[i].Bulletin.Size = tx.Size
transactions[i].Bulletin.S = tx.S
transactions[i].Bulletin.N = tx.N
transactions[i].Bulletin.SigmaMKLRoot = tx.SigmaMKLRoot
transactions[i].Bulletin.Mode = tx.Mode
transactions[i].Price = tx.Price
transactions[i].UnitPrice = tx.UnitPrice
transactions[i].Count = tx.Count
transactions[i].ExpireAt = tx.ExpireAt
if transactions[i].Mode == TRANSACTION_MODE_TABLE_POD {
switch transactions[i].SubMode {
case TRANSACTION_SUB_MODE_COMPLAINT:
if transactions[i].OT {
err := json.Unmarshal([]byte(tx.Demands), &transactions[i].PlainOTComplaint.Demands)
if err != nil {
return transactions, fmt.Errorf("failed to parse demands: %v", err)
}
err = json.Unmarshal([]byte(tx.Phantoms), &transactions[i].PlainOTComplaint.Phantoms)
if err != nil {
return transactions, fmt.Errorf("failed to parse phantoms: %v", err)
}
} else {
err := json.Unmarshal([]byte(tx.Demands), &transactions[i].PlainComplaint.Demands)
if err != nil {
return transactions, fmt.Errorf("failed to parse demands: %v", err)
}
}
case TRANSACTION_SUB_MODE_ATOMIC_SWAP:
err := json.Unmarshal([]byte(tx.Demands), &transactions[i].PlainAtomicSwap.Demands)
if err != nil {
return transactions, fmt.Errorf("failed to parse demands: %v", err)
}
case TRANSACTION_SUB_MODE_ATOMIC_SWAP_VC:
err := json.Unmarshal([]byte(tx.Demands), &transactions[i].PlainAtomicSwapVc.Demands)
if err != nil {
return transactions, fmt.Errorf("failed to parse demands: %v", err)
}
}
} else {
switch transactions[i].SubMode {
case TRANSACTION_SUB_MODE_COMPLAINT:
if transactions[i].OT {
err := json.Unmarshal([]byte(tx.Demands), &transactions[i].TableOTComplaint.Demands)
if err != nil {
return transactions, fmt.Errorf("failed to parse demands: %v", err)
}
err = json.Unmarshal([]byte(tx.Phantoms), &transactions[i].TableOTComplaint.Phantoms)
if err != nil {
return transactions, fmt.Errorf("failed to parse phantoms: %v", err)
}
} else {
err := json.Unmarshal([]byte(tx.Demands), &transactions[i].TableComplaint.Demands)
if err != nil {
return transactions, fmt.Errorf("failed to parse demands: %v", err)
}
}
case TRANSACTION_SUB_MODE_ATOMIC_SWAP:
err := json.Unmarshal([]byte(tx.Demands), &transactions[i].TableAtomicSwap.Demands)
if err != nil {
return transactions, fmt.Errorf("failed to parse demands: %v", err)
}
case TRANSACTION_SUB_MODE_ATOMIC_SWAP_VC:
err := json.Unmarshal([]byte(tx.Demands), &transactions[i].TableAtomicSwapVc.Demands)
if err != nil {
return transactions, fmt.Errorf("failed to parse demands: %v", err)
}
case TRANSACTION_SUB_MODE_VRF:
if transactions[i].OT {
transactions[i].TableOTVRF.KeyName = tx.KeyName
err := json.Unmarshal([]byte(tx.KeyValue), &transactions[i].TableOTVRF.KeyValue)
if err != nil {
return transactions, fmt.Errorf("failed to parse keyvalue: %v", err)
}
err = json.Unmarshal([]byte(tx.PhantomKeyValue), &transactions[i].TableOTVRF.PhantomKeyValue)
if err != nil {
return transactions, fmt.Errorf("failed to parse phantomKeyValue: %v", err)
}
} else {
transactions[i].TableVRF.KeyName = tx.KeyName
err := json.Unmarshal([]byte(tx.KeyValue), &transactions[i].TableVRF.KeyValue)
if err != nil {
return transactions, fmt.Errorf("failed to parse keyvalue: %v", err)
}
}
}
}
}
return transactions, nil
}
type AliceTx struct {
SessionId string `json:"sessionId" xorm:"text pk not null"`
Status string `json:"status" xorm:"text not null"`
AliceAddr string `json:"AliceAddr" xorm:"text not null"`
BobPubKey string `json:"BobPubkey" xorm:"text not null"`
BobAddr string `json:"BobAddr" xorm:"text not null"`
Mode string `json:"mode" xorm:"text not null"`
SubMode string `json:"subMode" xorm:"text not null"`
OT bool `json:"ot" xorm:"bool"`
Size string `json:"size" xorm:"text not null"`
S string `json:"s" xorm:"text not null"`
N string `json:"n" xorm:"text not null"`
SigmaMKLRoot string `json:"sigmaMklRoot" xorm:"text not null"`
Price int64 `json:"price" xorm:"INTEGER"`
UnitPrice int64 `json:"unit_price" xorm:"INTEGER"`
ExpireAt int64 `json:"expireAt" xorm:"text"`
Count int64 `json:"count" xorm:"INTEGER"`
CreateDate time.Time `json:"createDate" xorm:"created"`
UpdateDate time.Time `json:"updateDate" xorm:"updated"`
}
func insertAliceTxToDB(transaction Transaction) error {
var tx AliceTx
tx.SessionId = transaction.SessionID
tx.Status = transaction.Status //StatusAtoi(transaction.Status)
tx.AliceAddr = transaction.AliceAddr
// tx.BobPubKey = fmt.Sprintf("0x%x", crypto.FromECDSAPub(transaction.BobPubKey)[1:])
tx.BobAddr = transaction.BobAddr
tx.Mode = transaction.Mode
tx.SubMode = transaction.SubMode
tx.OT = transaction.OT
tx.Size = transaction.Bulletin.Size
tx.S = transaction.Bulletin.S
tx.N = transaction.Bulletin.N
tx.SigmaMKLRoot = transaction.Bulletin.SigmaMKLRoot
tx.Price = transaction.Price
tx.UnitPrice = transaction.UnitPrice
tx.ExpireAt = transaction.ExpireAt
tx.Count = transaction.Count
_, err := DBConn.Insert(&tx)
if err != nil {
return fmt.Errorf("failed to insert transaction to Alice_tx. err=%v", err)
}
return nil
}
func updateAliceTxToDB(transaction Transaction) error {
var tx AliceTx
tx.SessionId = transaction.SessionID
tx.Status = transaction.Status //StatusAtoi(transaction.Status)
// tx.AliceIP = transaction.AliceIP
tx.AliceAddr = transaction.AliceAddr
// tx.BobPubKey = transaction.BobPubKey
tx.BobAddr = transaction.BobAddr
tx.Mode = transaction.Mode
tx.SubMode = transaction.SubMode
tx.OT = transaction.OT
tx.Size = transaction.Bulletin.Size
tx.S = transaction.Bulletin.S
tx.N = transaction.Bulletin.N
tx.SigmaMKLRoot = transaction.Bulletin.SigmaMKLRoot
tx.Price = transaction.Price
tx.UnitPrice = transaction.UnitPrice
tx.ExpireAt = transaction.ExpireAt
tx.Count = transaction.Count
_, err := DBConn.Where("session_id = ?", tx.SessionId).Update(&tx)
if err != nil {
return fmt.Errorf("failed to insert transaction to Alice_tx. err=%v", err)
}
return nil
}
func loadAliceFromDB(sessionID string) (Transaction, bool, error) {
var tx AliceTx
var transaction Transaction
rs, err := DBConn.Where("session_id = ?", sessionID).Get(&tx)
if err != nil {
return transaction, false, fmt.Errorf("failed to read transaction for Bob. sessionID=%v, err=%v", sessionID, err)
}
if !rs {
return transaction, false, nil
}
transaction.SessionID = tx.SessionId
transaction.Status = tx.Status
transaction.AliceAddr = tx.AliceAddr
transaction.BobAddr = tx.BobAddr
// transaction.BobPubKey = tx.BobPubKey
transaction.Mode = tx.Mode
transaction.SubMode = tx.SubMode
transaction.OT = tx.OT
transaction.Bulletin.Size = tx.Size
transaction.Bulletin.S = tx.S
transaction.Bulletin.N = tx.N
transaction.Bulletin.SigmaMKLRoot = tx.SigmaMKLRoot
transaction.Bulletin.Mode = tx.Mode
transaction.Price = tx.Price
transaction.UnitPrice = tx.UnitPrice
transaction.ExpireAt = tx.ExpireAt
transaction.Count = tx.Count
return transaction, true, nil
}
func loadAliceTxListToDB() ([]Transaction, error) {
var txs []BobTx
err := DBConn.Find(&txs)
if err != nil {
return nil, fmt.Errorf("failed to read transaction for Bob. err=%v", err)
}
var transactions = make([]Transaction, len(txs))
for i, tx := range txs {
transactions[i].SessionID = tx.SessionId
transactions[i].Status = tx.Status
// transactions[i].AliceIP = tx.AliceIP
transactions[i].AliceAddr = tx.AliceAddr
transactions[i].BobAddr = tx.BobAddr
transactions[i].Mode = tx.Mode
transactions[i].SubMode = tx.SubMode
transactions[i].OT = tx.OT
transactions[i].Bulletin.Size = tx.Size
transactions[i].Bulletin.S = tx.S
transactions[i].Bulletin.N = tx.N
transactions[i].Bulletin.SigmaMKLRoot = tx.SigmaMKLRoot
transactions[i].Bulletin.Mode = tx.Mode
transactions[i].Price = tx.Price
transactions[i].UnitPrice = tx.UnitPrice
transactions[i].ExpireAt = tx.ExpireAt
transactions[i].Count = tx.Count
}
return transactions, nil
}
type PodLog struct {
Id int64 `json:"id" xorm:"autoincr pk"`
SessionId string `json:"session_id" xorm:"text"`
Operation int `json:"operation" xorm:"INTEGER not null"`
Detail string `json:"detail" xorm:"text"`
Result int `json:"result" xorm:"INTEGER not null"`
CreatedAt time.Time `json:"created_at" xorm:"created"`
}
func insertLogToDB(log PodLog) error {
_, err := DBConn.Insert(&log)
if err != nil {
return fmt.Errorf("failed to insert log to pod_log. err=%v", err)
}
return nil
}
func loadLogListByOperatorFromDB(operator string) ([]PodLog, error) {
var list []PodLog
err := DBConn.Where("operator = ?", operator).Find(&list)
if err != nil {
return list, fmt.Errorf("failed to load logs. err=%v", err)
}
return list, nil
}