-
Notifications
You must be signed in to change notification settings - Fork 15
/
tx-gen.go
380 lines (321 loc) · 10.2 KB
/
tx-gen.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
package main
import (
"fmt"
"log"
"math/rand"
"os"
"strconv"
"sync"
"time"
)
func genUsers(flagArgs *FlagArgs) *[]PrivKey {
users := make([]PrivKey, flagArgs.nUsers)
for i := range users {
privKey := PrivKey{}
privKey.gen()
users[i] = privKey
}
return &users
}
func genGenesisBlock(flagArgs *FlagArgs, committeeInfos []committeeInfo, users *[]PrivKey) []*FinalBlock {
per := uint(float64(flagArgs.totalCoins) / float64(flagArgs.nUsers))
ctx := new(NodeCtx)
ctx.committeeList = make([][32]byte, len(committeeInfos))
for i, c := range committeeInfos {
ctx.committeeList[i] = c.id
}
finalBlocks := make([]*FinalBlock, len(committeeInfos))
// genesis block, one per committtee
for i := range committeeInfos {
genesisTx := new(Transaction)
genesisTx.Outputs = make([]*OutTx, len(*users))
for j, u := range *users {
tx := new(OutTx)
tx.Value = per
tx.N = uint(j)
tx.PubKey = u.Pub
genesisTx.Outputs[j] = tx
}
txHash := [32]byte{}
// need to set a txHash that will point to the committee
for {
tmpp := make([]byte, 32)
rand.Read(tmpp)
tmp := hash(tmpp)
if committeeInfos[i].id == txFindClosestCommittee(ctx, tmp) {
// fmt.Println("Closets committee ", bytes32ToString(txFindClosestCommittee(ctx, tmp)), bytes32ToString(committeeInfos[i].id))
txHash = tmp
break
}
}
genesisTx.Hash = txHash
// fmt.Println(genesisTx)
// genesisTx.setHash()
genesisBlock := new(ProposedBlock)
// since there is only one transaction set merkle root to hash of gensisTx
genesisBlock.MerkleRoot = genesisTx.Hash
genesisBlock.Transactions = []*Transaction{genesisTx}
genesisBlock.CommitteeID = committeeInfos[i].id
// since the only thing in this block is the genesis tx, use that hash
// genesisBlock.GossipHash = genesisTx.Hash
genesisBlock.GossipHash = txHash
genesisFinalBlock := new(FinalBlock)
genesisFinalBlock.ProposedBlock = genesisBlock
finalBlocks[i] = genesisFinalBlock
// fmt.Println(i, genesisFinalBlock)
}
//fmt.Println("Block: ", genesisBlock)
// fmt.Println(finalBlocks)
return finalBlocks
}
type Tracker struct {
t *Transaction
sent time.Time
recived time.Time
dur time.Duration
crossTxes uint64
}
func (t *Tracker) completeTx(files []*os.File) {
t.recived = time.Now()
t.dur = t.recived.Sub(t.sent)
dur := strconv.FormatFloat(t.dur.Seconds(), 'f', 4, 64)
cross := strconv.FormatUint(t.crossTxes, 10)
s := prepareResultString(dur + "," + cross)
files[0].WriteString(s)
files[0].Sync()
}
type UserSets struct {
m map[[32]byte]*UTXOSet
mux sync.Mutex
}
type TransactionTracker struct {
m map[[32]byte]*Tracker
mux sync.Mutex
}
func txGenerator(flagArgs *FlagArgs, allNodes []NodeAllInfo, users *[]PrivKey, gensisBlocks []*FinalBlock, finalBlockChan chan FinalBlock, files []*os.File) {
// Emulates users by continously generating transactions
if flagArgs.tps == 0 {
return
}
// make a UTXO set for each user, such that we can easily look up UTXO for each user
userSets := new(UserSets)
userSets.m = make(map[[32]byte]*UTXOSet)
for _, u := range *users {
id := u.Pub.Bytes
userSets.m[id] = new(UTXOSet)
userSets.m[id].init()
}
// add gensis block output to the main UTXO set
for _, b := range gensisBlocks {
for _, out := range b.ProposedBlock.Transactions[0].Outputs {
userSets.m[out.PubKey.Bytes].add(b.ProposedBlock.Transactions[0].Hash, out)
}
}
// find committee id list and add it to nodeCtx
cMap := make(map[[32]byte]bool)
for _, node := range allNodes {
cMap[node.CommitteeID] = true
}
cList := make([][32]byte, len(cMap))
ii := 0
for k := range cMap {
cList[ii] = k
ii++
}
nodeCtx := new(NodeCtx)
nodeCtx.committeeList = cList
transactionTracker := new(TransactionTracker)
transactionTracker.m = make(map[[32]byte]*Tracker)
if flagArgs.local {
time.Sleep(10 * time.Second)
} else {
time.Sleep(3 * time.Second)
}
log.Println("starting tx-gen")
rand.Seed(42)
for {
before := time.Now()
l := len(finalBlockChan)
for i := 0; i < l; i++ {
fmt.Println("Recived finalblock")
finalBlock := <-finalBlockChan
fmt.Println(finalBlock.ProposedBlock)
for _, t := range finalBlock.ProposedBlock.Transactions {
if t.Hash == [32]byte{} && t.OrigTxHash != [32]byte{} && t.Outputs == nil {
fmt.Println("crosstx")
// return "crosstx"
transactionTracker.mux.Lock()
if _, ok := transactionTracker.m[t.OrigTxHash]; !ok {
fmt.Println("T: ", t)
fmt.Println("Tracker: ", transactionTracker.m[t.OrigTxHash])
errFatal(nil, "transaction in recived finalblock not in transactionTracker")
}
// increase crosstx counter for this transaction
transactionTracker.m[t.OrigTxHash].crossTxes++
transactionTracker.mux.Unlock()
continue
} else if t.Hash == [32]byte{} && t.OrigTxHash != [32]byte{} && t.Outputs != nil {
// return "originaltx"
fmt.Println("originaltx")
continue
} else if t.Hash != [32]byte{} && t.OrigTxHash != [32]byte{} && txFindClosestCommittee(nodeCtx, t.OrigTxHash) != finalBlock.ProposedBlock.CommitteeID {
// return "crosstxresponse"
fmt.Println("crosstxresponse_C_in")
continue
} else if t.Hash != [32]byte{} && t.OrigTxHash != [32]byte{} && t.ProofOfConsensus != nil {
// TODO ADD crosstxresponse_C_out or not
fmt.Println("crosstxresponse_C_out")
continue
}
id := t.ifOrigRetOrigIfNotRetHash()
transactionTracker.mux.Lock()
if _, ok := transactionTracker.m[id]; !ok {
fmt.Println("id", id)
fmt.Println("T: ", t)
fmt.Println("Tracker: ", transactionTracker.m[id])
errFatal(nil, "transaction in recived finalblock not in transactionTracker")
}
transactionTracker.m[id].completeTx(files)
var normalorfinal string
if t.Hash != [32]byte{} && t.OrigTxHash == [32]byte{} {
normalorfinal = "normal"
} else if t.Hash != [32]byte{} && t.OrigTxHash != [32]byte{} {
normalorfinal = "final"
} else {
errFatal(nil, t.String())
}
log.Println(normalorfinal, " tx finished in ", transactionTracker.m[id].dur.Seconds(), " seconds, with ", transactionTracker.m[id].crossTxes, " crosstxes.")
transactionTracker.mux.Unlock()
userSets.mux.Lock()
for _, out := range t.Outputs {
userSets.m[out.PubKey.Bytes].add(id, out)
}
userSets.mux.Unlock()
}
}
after := time.Now()
go _txGenerator(flagArgs, &allNodes, users, userSets, transactionTracker)
// Sleep such that time used to process finishedblock and create new tx is subtracted such that we emulate near perfect tps.
// fmt.Println("Sleep for: ", (time.Second/time.Duration(flagArgs.tps))-after.Sub(before))
dur := (time.Second / time.Duration(flagArgs.tps)) - after.Sub(before)
// log.Println("sleeping for ", dur)
if dur > 0 {
time.Sleep(dur)
}
}
}
func _txGenerator(flagArgs *FlagArgs, allNodes *[]NodeAllInfo, users *[]PrivKey, userSets *UserSets, transactionTracker *TransactionTracker) {
// pick random user to send transaction from
rnd := rand.Intn(len(*users))
user := (*users)[rnd]
// pick a random value from the users total value
userSets.mux.Lock()
totVal := userSets.m[user.Pub.Bytes]._totalValue()
userSets.mux.Unlock()
timeout := 0
for {
if totVal == 0 {
// no value in this user unfortuantly, so start again
rnd = rand.Intn(len(*users))
user = (*users)[rnd]
// pick a random value from the users total value
userSets.mux.Lock()
totVal = userSets.m[user.Pub.Bytes]._totalValue()
userSets.mux.Unlock()
time.Sleep(10 * time.Millisecond)
timeout++
if timeout >= 10 {
return
}
} else {
break
}
}
_value := totVal / 4
if _value < 1 {
return
}
value := int(_value)
var valueToSend uint
if value <= 1 {
valueToSend = 1
} else {
valueToSend = uint(rand.Intn(value) + 1)
}
// get all outputs required to fill that value
userSets.mux.Lock()
outputs, ok := userSets.m[user.Pub.Bytes].getOutputsToFillValue(valueToSend)
// userSets.mux.Unlock()
if !ok {
userSets.mux.Unlock()
return
}
// fmt.Println(outputs)
// pick random user to send transaction to, aka output of tx
rndTo := rand.Intn(len(*users))
userTo := (*users)[rndTo].Pub
// create transaction
totalInputValue := uint(0)
t := new(Transaction)
inputs := make([]*InTx, len(outputs))
// userSets.mux.Lock()
for i, o := range outputs {
outTx := userSets.m[user.Pub.Bytes]._getAndRemove(o.txID, o.n)
totalInputValue += outTx.Value
newInTx := new(InTx)
newInTx.TxHash = o.txID
newInTx.N = outTx.N
inputs[i] = newInTx
}
userSets.mux.Unlock()
// only one or two outputs (if there is some value left over, then send it back to user)
txOutputs := []*OutTx{}
newOutTx := new(OutTx)
newOutTx.Value = valueToSend
newOutTx.N = 0
newOutTx.PubKey = userTo
txOutputs = append(txOutputs, newOutTx)
if totalInputValue != valueToSend {
// there is a rest that we must send back to user
rest := totalInputValue - valueToSend
if rest <= 0 {
errFatal(nil, "rest was not positive")
}
newOutput := new(OutTx)
newOutput.Value = rest
newOutput.N = 1
newOutput.PubKey = user.Pub
txOutputs = append(txOutputs, newOutput)
}
t.Inputs = inputs
t.Outputs = txOutputs
t.setHash()
t.signInputs(&user)
// fmt.Println("newTx", bytes32ToString(t.Hash), bytes32ToString(t.OrigTxHash), bytes32ToString(t.id()))
// fmt.Println(bytes32ToString(t.Inputs[0].TxHash), bytes32ToString(t.Inputs[0].OrigTxHash), bytes32ToString(t.id()), t.Inputs[0].N, t.Inputs[0].Sig)
// fmt.Println(bytes32ToString(t.Outputs[0].PubKey.Bytes), t.Outputs[0].Value, t.Outputs[0].N)
// fmt.Println("Sent tx: ", t)
// pick random node to send tx to
rndNode := rand.Intn(len(*allNodes))
node := (*allNodes)[rndNode]
// send transaction
msg := Msg{"transaction", t, user.Pub}
go dialAndSend(node.IP, msg)
transactionTracker.mux.Lock()
if _, ok := transactionTracker.m[t.Hash]; ok {
fmt.Println("Previous tx: ", transactionTracker.m[t.Hash])
fmt.Println("New tx: ", t)
transactionTracker.mux.Unlock()
errFatal(nil, "transaction allready sent")
}
track := new(Tracker)
track.t = t
track.sent = time.Now()
transactionTracker.m[t.Hash] = track
transactionTracker.mux.Unlock()
// add output to sets
// for _, out := range t.Outputs {
// userSets[out.PubKey.Bytes].add(t.Hash, out)
// }
// log.Println("Sent tx")
}