-
Notifications
You must be signed in to change notification settings - Fork 0
/
appdata.go
648 lines (610 loc) · 23.6 KB
/
appdata.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
/**********************************************************************
* Copyright (c) 2017 Jayamine Alupotha *
* Distributed under the MIT software license, see the accompanying *
* file COPYING or http://www.opensource.org/licenses/mit-license.php.*
**********************************************************************/
package txhelper
import (
"bytes"
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"golang.org/x/crypto/sha3"
"log"
rand2 "math/rand"
)
type InputData struct {
Header []byte `json:"h"` // identifier like a hash
u User
}
type OutputData struct {
Pk []byte `json:"p"` // public key
N uint8 `json:"n"`
Data []byte `json:"d"` // new application data
header []byte // new application header (Origami)
u User // updated user data
}
type AppData struct {
Inputs []InputData `json:"i"` // inputs as a byte array
Outputs []OutputData `json:"o"` // outputs as a byte array
}
// computeOutIdentifier computes an unique identifer for each output via hashing
func (ctx *ExeContext) computeOutIdentifier(pk []byte, n uint8, data []byte) []byte {
hasher := sha3.New256()
nByte := make([]byte, 1)
nByte[0] = n
hasher.Write(pk)
hasher.Write(nByte)
hasher.Write(data)
return hasher.Sum(nil)
}
// RandomAppData creates an application data change for randomly chosen users
func (ctx *ExeContext) RandomAppData(data *AppData, inSize uint8, outSize uint8, averageSize uint16) {
switch ctx.txModel {
case 1:
ctx.utxoAppData(data, inSize, outSize, averageSize)
case 2:
ctx.accAppData(data, inSize, outSize, averageSize)
case 3:
ctx.utxoAppData(data, inSize, outSize, averageSize)
case 4:
ctx.accAppData(data, inSize, outSize, averageSize)
case 5:
ctx.utxoAppData(data, inSize, outSize, averageSize)
case 6:
ctx.accAppData(data, inSize, outSize, averageSize)
default:
log.Fatal("unknown txModel")
}
}
// PrepareAppDataClient get user details for inputs using the header
func (ctx *ExeContext) PrepareAppDataClient(data *AppData) (bool, error) {
i := 0
// arrange inputs
for i = 0; i < len(data.Inputs); i++ {
ok, err := ctx.getClientOutFromH(data.Inputs[i].Header, &data.Inputs[i].u)
if !ok {
fmt.Println("could not find h:", data.Inputs[i].Header)
return false, err
}
// copy public key
if i < len(data.Inputs) && (ctx.txModel == 2 || ctx.txModel == 4 || ctx.txModel == 6) {
data.Outputs[i].Pk = make([]byte, ctx.sigContext.PkSize)
copy(data.Outputs[i].Pk, data.Inputs[i].u.Keys)
data.Outputs[i].N = data.Inputs[i].u.N + 1
}
}
for i = 0; i < len(data.Outputs); i++ {
//update client db with new data
data.Outputs[i].u.H = ctx.computeOutIdentifier(data.Outputs[i].Pk, data.Outputs[i].N, data.Outputs[i].Data)
}
return true, nil
}
// PrepareAppDataPeer get output details for inputs using the header
func (ctx *ExeContext) PrepareAppDataPeer(data *AppData) (bool, error) {
i := 0
used := -1
found := false
var err error
// arrange inputs
for i = 0; i < len(data.Inputs); i++ {
found, data.Inputs[i].u.id, used, err = ctx.getPeerOut(data.Inputs[i].Header, &data.Inputs[i].u)
if found == false {
return false, err
}
if used != 0 {
return false, errors.New("TXHELPER_REUSED_IN" + err.Error())
}
// copy public key
if i < len(data.Inputs) && (ctx.txModel == 2 || ctx.txModel == 4 || ctx.txModel == 6) {
data.Outputs[i].Pk = make([]byte, ctx.sigContext.PkSize)
copy(data.Outputs[i].Pk, data.Inputs[i].u.Keys)
data.Outputs[i].N = data.Inputs[i].u.N + 1
}
}
for i = 0; i < len(data.Outputs); i++ {
//update client db with new data
data.Outputs[i].header = ctx.computeOutIdentifier(data.Outputs[i].Pk, data.Outputs[i].N, data.Outputs[i].Data)
}
// arrange ids of outputs for txHeader insertion
if ctx.txModel >= 1 && ctx.txModel <= 4 {
for i = 0; i < len(data.Outputs); i++ {
data.Outputs[i].u.id = ctx.CurrentOutputs + i // must save every output with new id
}
} else if ctx.txModel == 5 {
for i = 0; i < len(data.Outputs); i++ {
data.Outputs[i].u.id = ctx.outputPointer // must save every output with new id even though input ids will be deleted
ctx.outputPointer++
}
} else if ctx.txModel == 6 { // must save every new output pk with new id
j := 0
for i = len(data.Inputs); i < len(data.Outputs); i++ {
data.Outputs[i].u.id = ctx.CurrentUsers + j // must save every new pk (user) with new id
j++
}
} else {
log.Fatal("unknown txModel:", ctx.txModel)
}
return true, nil
}
// PrepareAppDataPeerWithTemps get output details for inputs using the header. Note that it also checks temporary users
func (ctx *ExeContext) PrepareAppDataPeerWithTemps(data *AppData) (bool, error) {
i := 0
used := -1
foundDB := false
foundtemp := false
var err error
// arrange inputs
for i = 0; i < len(data.Inputs); i++ {
// first check temps
foundtemp, data.Inputs[i].u.id, used, err = ctx.getTempPeerOut(data.Inputs[i].Header, &data.Inputs[i].u)
if !foundtemp {
// then check db
foundDB, data.Inputs[i].u.id, used, err = ctx.getPeerOut(data.Inputs[i].Header, &data.Inputs[i].u)
if !foundDB {
fmt.Println("not found:", data.Inputs[i].Header[:5])
for h, user := range ctx.TempUsers {
fmt.Println(h[:5], user.u.Keys[:5])
}
return false, err
}
}
if used != 0 {
return false, errors.New("TXHELPER_REUSED_IN" + err.Error())
}
// copy public key
if i < len(data.Inputs) && (ctx.txModel == 2 || ctx.txModel == 4 || ctx.txModel == 6) {
data.Outputs[i].Pk = make([]byte, ctx.sigContext.PkSize)
copy(data.Outputs[i].Pk, data.Inputs[i].u.Keys)
data.Outputs[i].N = data.Inputs[i].u.N + 1
}
}
for i = 0; i < len(data.Outputs); i++ {
//update client db with new data
data.Outputs[i].header = ctx.computeOutIdentifier(data.Outputs[i].Pk, data.Outputs[i].N, data.Outputs[i].Data)
}
// arrange ids of outputs for txHeader insertion
if ctx.txModel >= 1 && ctx.txModel <= 4 {
for i = 0; i < len(data.Outputs); i++ {
data.Outputs[i].u.id = ctx.CurrentOutputsWithTemp + i // must save every output with new id
}
} else if ctx.txModel == 5 {
for i = 0; i < len(data.Outputs); i++ {
data.Outputs[i].u.id = ctx.outputPointer // must save every output with new id even though input ids will be deleted
ctx.outputPointer++
}
} else if ctx.txModel == 6 { // must save every new output pk with new id
j := 0
for i = len(data.Inputs); i < len(data.Outputs); i++ {
data.Outputs[i].u.id = ctx.CurrentUsersWithTemp + j // must save every new pk (user) with new id
j++
}
} else {
log.Fatal("unknown txModel:", ctx.txModel)
}
return true, nil
}
// UpdateAppDataClient update user details for new app data changes
func (ctx *ExeContext) UpdateAppDataClient(data *AppData) (bool, error) {
i := 0
// utxo
if ctx.txModel == 1 || ctx.txModel == 3 || ctx.txModel == 5 {
// save outputs
for i = 0; i < len(data.Outputs); i++ {
//update client db with new data
ok, err := ctx.updateClientOut(data.Outputs[i].u.id, &data.Outputs[i].u)
if !ok {
return false, err
}
}
}
// account
if ctx.txModel == 2 || ctx.txModel == 4 || ctx.txModel == 6 {
// save outputs
for i = 0; i < len(data.Inputs); i++ {
data.Inputs[i].u.N = data.Outputs[i].N
data.Inputs[i].u.H = ctx.computeOutIdentifier(data.Outputs[i].Pk, data.Outputs[i].N, data.Outputs[i].Data)
ok, err := ctx.updateClientOut(data.Inputs[i].u.id, &data.Inputs[i].u)
if !ok {
return false, err
}
}
for i = len(data.Inputs); i < len(data.Outputs); i++ {
data.Outputs[i].u.H = ctx.computeOutIdentifier(data.Outputs[i].Pk, data.Outputs[i].N, data.Outputs[i].Data)
ok, err := ctx.updateClientOut(data.Outputs[i].u.id, &data.Outputs[i].u)
if !ok {
return false, err
}
}
}
return true, nil
}
// UpdateAppDataPeer update output details for new app data changes
func (ctx *ExeContext) UpdateAppDataPeer(txNum int, tx *Transaction) (bool, *string) {
i := 0
//header := make([]byte, sha256.Size)
var errM string
// utxo
if ctx.txModel == 1 || ctx.txModel == 3 {
for i = 0; i < len(tx.Data.Inputs); i++ {
ok, err := ctx.updatePeerOut(tx.Data.Inputs[i].u.id, nil, 0, nil, nil, nil, 1) // update "used"
if !ok {
errM = "I couldn't find the input. Did you verify the app data?:" + string(rune(tx.Data.Inputs[i].u.id)) + " " + err.Error()
return false, &errM
}
}
// save outputs
for i = 0; i < len(tx.Data.Outputs); i++ {
//update client db with new data
//header = ctx.computeOutIdentifier(tx.Data.Outputs[i].Pk, tx.Data.Outputs[i].N, tx.Data.Outputs[i].Data)
ok, err := ctx.insertPeerOut(tx.Data.Outputs[i].u.id, tx.Data.Outputs[i].header, &tx.Data.Outputs[i], nil)
if !ok {
errM = "I couldn't insert the output" + string(rune(tx.Data.Outputs[i].u.id)) + " " + err.Error()
return false, &errM
}
}
ctx.CurrentOutputs += len(tx.Data.Outputs)
}
// account
if ctx.txModel == 2 || ctx.txModel == 4 {
// modify inputs' into ``used'' inputs
for i = 0; i < len(tx.Data.Inputs); i++ {
if ctx.sigContext.SigType == 1 || ctx.sigContext.SigType == 2 {
ok, err := ctx.updatePeerOut(tx.Data.Inputs[i].u.id, nil, 0, nil, nil, nil, 1) // update "used"
if !ok {
errM = "I couldn't update the input" + string(rune(tx.Data.Inputs[i].u.id)) + " " + err.Error()
return false, &errM
}
} else {
log.Fatal("unknown sigType:", ctx.sigContext.SigType)
}
}
// save outputs
for i = 0; i < len(tx.Data.Outputs); i++ {
//header = ctx.computeOutIdentifier(tx.Data.Outputs[i].Pk, tx.Data.Outputs[i].N, tx.Data.Outputs[i].Data)
ok, err := ctx.insertPeerOut(tx.Data.Outputs[i].u.id, tx.Data.Outputs[i].header, &tx.Data.Outputs[i], nil)
if !ok {
errM = "I couldn't update the output" + string(rune(tx.Data.Outputs[i].u.id)) + " " + err.Error()
return false, &errM
}
}
ctx.CurrentUsers += len(tx.Data.Outputs) - len(tx.Data.Inputs) // update the current user size
ctx.CurrentOutputs += len(tx.Data.Outputs) // update the current output number
} else if ctx.txModel == 5 {
// delete inputs
for i = 0; i < len(tx.Data.Inputs); i++ {
ok, err := ctx.deletePeerOut(tx.Data.Inputs[i].u.id)
if !ok {
errM = "couldn't delete the input. Does it exist" + string(rune(tx.Data.Inputs[i].u.id)) + " " + err.Error()
return false, &errM
}
}
// save outputs
for i = 0; i < len(tx.Data.Outputs); i++ {
//header = ctx.computeOutIdentifier(tx.Data.Outputs[i].Pk, tx.Data.Outputs[i].N, tx.Data.Outputs[i].Data)
ok, err := ctx.insertPeerOut(tx.Data.Outputs[i].u.id, tx.Data.Outputs[i].header, &tx.Data.Outputs[i], nil)
if !ok {
errM = "I couldn't insert the output" + string(rune(tx.Data.Outputs[i].u.id)) + " " + err.Error()
return false, &errM
}
}
ctx.CurrentOutputs += len(tx.Data.Outputs) - len(tx.Data.Inputs)
ctx.DeletedOutputs += len(tx.Data.Inputs)
} else if ctx.txModel == 6 {
// modify inputs (h, -, data, n, sig) including "used"
for i = 0; i < len(tx.Data.Inputs); i++ {
//header = ctx.computeOutIdentifier(tx.Data.Outputs[i].Pk, tx.Data.Outputs[i].N, tx.Data.Outputs[i].Data)
if ctx.sigContext.SigType == 1 || ctx.sigContext.SigType == 2 {
tx.Data.Inputs[i].u.Txns = append(tx.Data.Inputs[i].u.Txns, txNum)
ok, err := ctx.updatePeerOut(tx.Data.Inputs[i].u.id, tx.Data.Outputs[i].header, int(tx.Data.Outputs[i].N), tx.Data.Outputs[i].Data, tx.Txh.Kyber[i], tx.Data.Inputs[i].u.Txns, 0)
if !ok {
errM = "I couldn't update the input" + string(rune(tx.Data.Inputs[i].u.id)) + " " + err.Error()
return false, &errM
}
} else {
log.Fatal("unknown sigType:", ctx.sigContext.SigType)
}
//fmt.Println("updated:", tx.Data.Inputs[i].u.id, tx.Data.Outputs[i].Pk[:5], tx.Data.Inputs[i].u.Txns, txNum, tx.Txh.activityProof)
}
// save new outputs
for i = len(tx.Data.Inputs); i < len(tx.Data.Outputs); i++ {
//header = ctx.computeOutIdentifier(tx.Data.Outputs[i].Pk, tx.Data.Outputs[i].N, tx.Data.Outputs[i].Data)
tx.Data.Outputs[i].u.Txns = make([]int, 1)
tx.Data.Outputs[i].u.Txns[0] = txNum
ok, err := ctx.insertPeerOut(tx.Data.Outputs[i].u.id, tx.Data.Outputs[i].header, &tx.Data.Outputs[i], tx.Txh.Kyber[i])
if !ok {
errM = "I couldn't update the output" + string(rune(tx.Data.Outputs[i].u.id)) + " " + err.Error()
return false, &errM
}
//fmt.Println("inserted:", txNum, tx.Data.Outputs[i].u.id, tx.Data.Outputs[i].Pk[:5], tx.Data.Outputs[i].u.Txns, tx.Txh.activityProof)
}
ctx.CurrentUsers += len(tx.Data.Outputs) - len(tx.Data.Inputs)
ctx.CurrentOutputs += len(tx.Data.Outputs) - len(tx.Data.Inputs)
ctx.DeletedOutputs += len(tx.Data.Inputs)
}
// delete old outputs from temps
ctx.deleteTempOutputs(txNum)
delete(ctx.TempTxH, txNum)
return true, nil
}
// UpdateAppDataPeerToTemp update output details for new app data changes
func (ctx *ExeContext) UpdateAppDataPeerToTemp(txNum int, tx *Transaction) (bool, *string) {
i := 0
//header := make([]byte, sha256.Size)
var errM string
// utxo
if ctx.txModel == 1 || ctx.txModel == 3 {
for i = 0; i < len(tx.Data.Inputs); i++ {
ok, err := ctx.updateTempPeerOut(tx.Data.Inputs[i].Header, tx.Data.Inputs[i].Header, 0, nil, nil, nil, 1, txNum) // update "used"
if !ok && errors.Is(err, errors.New("TXHELPER_DUPLICATE_OUTPUTS")) {
errM = "invalid temp update:" + err.Error()
return false, &errM
}
}
// save outputs
for i = 0; i < len(tx.Data.Outputs); i++ {
//update client db with new data
//header = ctx.computeOutIdentifier(tx.Data.Outputs[i].Pk, tx.Data.Outputs[i].N, tx.Data.Outputs[i].Data)
ok, err := ctx.insertTempPeerOut(tx.Data.Outputs[i].u.id, tx.Data.Outputs[i].header, &tx.Data.Outputs[i], nil, txNum)
if !ok {
errM = "I couldn't insert the output" + string(rune(tx.Data.Outputs[i].u.id)) + " " + err.Error()
return false, &errM
}
}
ctx.CurrentOutputsWithTemp += len(tx.Data.Outputs)
}
// account
if ctx.txModel == 2 || ctx.txModel == 4 {
// modify inputs' into ``used'' inputs
for i = 0; i < len(tx.Data.Inputs); i++ {
// only update temps
ok, err := ctx.updateTempPeerOut(tx.Data.Inputs[i].Header, tx.Data.Inputs[i].Header, 0, nil, nil, nil, 1, txNum) // update "used"
if !ok && errors.Is(err, errors.New("TXHELPER_DUPLICATE_OUTPUTS")) {
errM = "invalid temp update:" + err.Error()
return false, &errM
}
}
// save outputs
for i = 0; i < len(tx.Data.Outputs); i++ {
//header = ctx.computeOutIdentifier(tx.Data.Outputs[i].Pk, tx.Data.Outputs[i].N, tx.Data.Outputs[i].Data)
ok, err := ctx.insertTempPeerOut(tx.Data.Outputs[i].u.id, tx.Data.Outputs[i].header, &tx.Data.Outputs[i], nil, txNum)
if !ok {
errM = "I couldn't update the output" + string(rune(tx.Data.Outputs[i].u.id)) + " " + err.Error()
return false, &errM
}
}
ctx.CurrentUsersWithTemp += len(tx.Data.Outputs) - len(tx.Data.Inputs) // update the current user size
ctx.CurrentOutputsWithTemp += len(tx.Data.Outputs) // update the current output number
} else if ctx.txModel == 5 {
// do not delete inputs with temp update
// save outputs
for i = 0; i < len(tx.Data.Outputs); i++ {
//header = ctx.computeOutIdentifier(tx.Data.Outputs[i].Pk, tx.Data.Outputs[i].N, tx.Data.Outputs[i].Data)
ok, err := ctx.insertTempPeerOut(tx.Data.Outputs[i].u.id, tx.Data.Outputs[i].header, &tx.Data.Outputs[i], nil, txNum)
if !ok {
errM = "I couldn't insert the output" + string(rune(tx.Data.Outputs[i].u.id)) + " " + err.Error()
return false, &errM
}
}
ctx.CurrentOutputsWithTemp += len(tx.Data.Outputs) - len(tx.Data.Inputs)
} else if ctx.txModel == 6 {
// modify inputs (h, -, data, n, sig) including "used"
for i = 0; i < len(tx.Data.Inputs); i++ {
tx.Data.Inputs[i].u.H = ctx.computeOutIdentifier(tx.Data.Outputs[i].Pk, tx.Data.Outputs[i].N, tx.Data.Outputs[i].Data)
if ctx.sigContext.SigType == 1 || ctx.sigContext.SigType == 2 {
txns := append(tx.Data.Inputs[i].u.Txns, txNum)
// instead use previous header
ok, err := ctx.updateTempPeerOut(tx.Data.Inputs[i].Header, tx.Data.Inputs[i].u.H, int(tx.Data.Outputs[i].N), tx.Data.Outputs[i].Data, tx.Txh.Kyber[i], txns, 0, txNum)
if !ok {
errM = "I couldn't update the input" + string(rune(tx.Data.Inputs[i].u.id)) + " " + err.Error()
return false, &errM
}
} else {
log.Fatal("unknown sigType:", ctx.sigContext.SigType)
}
//fmt.Println("updated temp:", txNum, tx.Data.Outputs[i].Pk[:5], tx.Data.Inputs[i].Header[:5], tx.Data.Inputs[i].u.H)
}
// save new outputs
for i = len(tx.Data.Inputs); i < len(tx.Data.Outputs); i++ {
tx.Data.Outputs[i].u.H = ctx.computeOutIdentifier(tx.Data.Outputs[i].Pk, tx.Data.Outputs[i].N, tx.Data.Outputs[i].Data)
tx.Data.Outputs[i].u.Txns = make([]int, 1)
tx.Data.Outputs[i].u.Txns[0] = txNum
ok, err := ctx.insertTempPeerOut(tx.Data.Outputs[i].u.id, tx.Data.Outputs[i].u.H, &tx.Data.Outputs[i], tx.Txh.Kyber[i], txNum)
if !ok {
errM = "I couldn't add the output" + string(rune(tx.Data.Outputs[i].u.id)) + " " + err.Error()
return false, &errM
}
//fmt.Println("insert temp:", txNum, tx.Data.Outputs[i].Pk[:5], tx.Data.Outputs[i].u.H[:5], tx.Data.Outputs[i].u.Txns)
}
// save txH
ctx.TempTxH[txNum] = tx.Txh.activityProof
ctx.CurrentUsersWithTemp += len(tx.Data.Outputs) - len(tx.Data.Inputs)
ctx.CurrentOutputsWithTemp += len(tx.Data.Outputs) - len(tx.Data.Inputs)
}
return true, nil
}
// utxoAppData returns a random application update for UTXO-based models
// Users can have more than one output
// We choose input users and output users in round-robin manner
func (ctx *ExeContext) utxoAppData(data *AppData, inSize uint8, outSize uint8, averageSize uint16) {
i := 0
dataSize := 0
// arrange inputs
// It takes users in round-robin manner according to the pointer.
data.Inputs = make([]InputData, inSize)
for i = 0; i < int(inSize); i++ {
if ctx.outputPointer <= ctx.inputPointer {
break
}
data.Inputs[i].u.id = ctx.inputPointer
// get user from client db
ok, err := ctx.getClientOut(data.Inputs[i].u.id, &data.Inputs[i].u)
if !ok {
log.Fatal("utxoAppData: could not find user id:", data.Inputs[i].u.id, err) // This should not happen
}
data.Inputs[i].Header = make([]byte, len(data.Inputs[i].u.H))
copy(data.Inputs[i].Header, data.Inputs[i].u.H)
ctx.inputPointer++
}
inSize = uint8(i & 0xff) // update the input size
data.Inputs = data.Inputs[:inSize]
// create outputs
keyBuf := new(bytes.Buffer)
data.Outputs = make([]OutputData, outSize)
var jsonBytes []byte
for i = 0; i < int(outSize); i++ {
// a new user with new pk can be created or the existing user with new N can be created
choice := rand2.Int() % ctx.PublicKeyReuse
if (choice == 0 || ctx.CurrentUsers >= ctx.TotalUsers) && int(inSize) > i { // use input pk with new n
jsonBytes, _ = json.Marshal(&data.Inputs[i].u)
if json.Unmarshal(jsonBytes, &data.Outputs[i].u) != nil {
log.Fatal("could not copy data")
}
} else { // create new user
var keys SigKeyPair
ctx.sigContext.generate(&keys)
ctx.sigContext.marshelKeys(&keys, keyBuf)
data.Outputs[i].u = User{
H: make([]byte, 32),
N: 0,
Keys: keyBuf.Bytes(),
Data: make([]byte, ctx.payloadSize),
UDelta: make([]byte, 0),
}
ctx.CurrentUsers++
}
data.Outputs[i].u.id = ctx.outputPointer // save for client db
ok, err := ctx.insertClientOut(ctx.outputPointer, &data.Outputs[i].u)
if !ok {
log.Fatal("couldn't insert outputs:", err)
}
keyBuf.Reset()
// compute the header
ok, err = ctx.getClientOut(data.Outputs[i].u.id, &data.Outputs[i].u)
if !ok {
log.Fatal("utxoAppData: could not find user id:", data.Outputs[i].u.id, err) // This should not happen
}
// copy the public key
data.Outputs[i].Pk = make([]byte, ctx.sigContext.PkSize)
copy(data.Outputs[i].Pk, data.Outputs[i].u.Keys)
if len(data.Outputs[i].u.Keys) != int(ctx.sigContext.PkSize+ctx.sigContext.SkSize) {
log.Fatal("invalid pk size")
}
// update n
data.Outputs[i].u.N += 1
data.Outputs[i].N = data.Outputs[i].u.N
// get random new data
dataSize, _ = rand.Read(data.Outputs[i].u.Data)
if dataSize != int(ctx.payloadSize) {
log.Fatal("invalid utxo data size")
}
data.Outputs[i].Data = make([]byte, averageSize)
copy(data.Outputs[i].Data, data.Outputs[i].u.Data)
// update variables
ctx.outputPointer++
ctx.CurrentOutputs++
}
}
// accAppData returns random application updates for account-based models
// All accounts [0, inSize] will be existing accounts and new accounts are in [inSize, OutSize].
// If there are not enough existing accounts, inSize will be updated
func (ctx *ExeContext) accAppData(data *AppData, inSize uint8, outSize uint8, averageSize uint16) {
i := uint8(0)
id := 0
dataSize := 0
if inSize > outSize {
inSize = outSize // all inputs should be in outputs
}
if ctx.CurrentUsers == 0 {
inSize = 0
outSize = ctx.AverageInputMax // to avoid overlapping between pk for the 2nd transaction
} else if ctx.CurrentUsers == ctx.TotalUsers { // should not add more accounts
inSize = outSize
}
//inSize = 0
// arrange inputs and outputs of existing users
// It takes users in round-robin manner according to the pointer.
data.Inputs = make([]InputData, inSize)
data.Outputs = make([]OutputData, outSize)
id = rand2.Int() % 0xff
for i = 0; i < inSize; i++ {
id += 1
data.Inputs[i].u.id = (id) % ctx.CurrentUsers // save for db
// get random user from client db
ok, err := ctx.getClientOut(data.Inputs[i].u.id, &data.Inputs[i].u)
if !ok {
log.Fatal("accAppData: could not find user id:", id, inSize, data.Inputs[i].u.id, ctx.CurrentUsers, err)
}
// if N = 0 is zero then no previous outputs were created
if data.Inputs[i].u.N == 0 {
break
}
// copy the header
data.Inputs[i].Header = make([]byte, len(data.Inputs[i].u.H))
copy(data.Inputs[i].Header, data.Inputs[i].u.H)
// copy the public key
data.Outputs[i].Pk = make([]byte, ctx.sigContext.PkSize)
copy(data.Outputs[i].Pk, data.Inputs[i].u.Keys)
if len(data.Inputs[i].u.Keys) != int(ctx.sigContext.PkSize+ctx.sigContext.SkSize) {
log.Fatal("invalid pk size")
}
// update n
data.Outputs[i].N = data.Inputs[i].u.N + 1
// get random new data
dataSize, _ = rand.Read(data.Inputs[i].u.Data)
if dataSize != int(ctx.payloadSize) {
log.Fatal("invalid app data size")
}
data.Outputs[i].Data = make([]byte, averageSize)
copy(data.Outputs[i].Data, data.Inputs[i].u.Data)
}
inSize = i // update the input size
data.Inputs = data.Inputs[:inSize]
// arrange outputs of non-existing users
// Next outputPointer will be the outputPointer + number of inputs
keyBuf := new(bytes.Buffer)
for i = inSize; i < outSize; i++ {
// create user for the
var keys SigKeyPair
ctx.sigContext.generate(&keys)
ctx.sigContext.marshelKeys(&keys, keyBuf)
data.Outputs[i].u = User{
H: make([]byte, 32),
N: 0,
Keys: keyBuf.Bytes(),
Data: make([]byte, ctx.payloadSize),
UDelta: make([]byte, 0),
}
data.Outputs[i].u.id = ctx.CurrentUsers // save for db
ok, err := ctx.insertClientOut(ctx.CurrentUsers, &data.Outputs[i].u)
if !ok {
log.Fatal("couldn't insert outputs:", err)
}
ctx.CurrentUsers += 1
keyBuf.Reset()
// tests
ok, err = ctx.getClientOut(data.Outputs[i].u.id, &data.Outputs[i].u)
if !ok {
log.Fatal("accAppData: could not find user id:", id, err)
}
// copy the public key
data.Outputs[i].Pk = make([]byte, ctx.sigContext.PkSize)
copy(data.Outputs[i].Pk, data.Outputs[i].u.Keys)
if len(data.Outputs[i].u.Keys) != int(ctx.sigContext.PkSize+ctx.sigContext.SkSize) {
log.Fatal("invalid pk size")
}
// update n
data.Outputs[i].u.N = 1
data.Outputs[i].N = data.Outputs[i].u.N
// get random new data
dataSize, _ = rand.Read(data.Outputs[i].u.Data)
if dataSize != int(ctx.payloadSize) {
log.Fatal("invalid app data size")
}
data.Outputs[i].Data = make([]byte, averageSize)
copy(data.Outputs[i].Data, data.Outputs[i].u.Data)
}
ctx.outputPointer += int(outSize)
ctx.outputPointer %= ctx.TotalUsers
}