-
Notifications
You must be signed in to change notification settings - Fork 31
/
comm.go
609 lines (551 loc) · 15.4 KB
/
comm.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
// Copyright (c) 2014-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"errors"
"fmt"
"log"
"math"
"math/rand"
"os"
"sync"
"time"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
rpc "github.com/btcsuite/btcd/rpcclient"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
)
// Block contains the block hash and height as received in a
// OnBlockConnected notification
type Block struct {
hash *chainhash.Hash
height int32
}
// blockQueue is a queue of blocks received from OnBlockConnected
// waiting to be processed
type blockQueue struct {
enqueue chan *Block
dequeue chan *Block
processed chan *Block
}
// Communication is consisted of the necessary primitives used
// for communication between the main goroutine and actors.
type Communication struct {
wg sync.WaitGroup
downstream chan btcutil.Address
timeReceived chan time.Time
blockTxCount chan int
exit chan struct{}
errChan chan struct{}
height chan int32
split chan int
txpool chan struct{}
coinbaseQueue chan *wire.MsgTx
blockQueue *blockQueue
}
// NewCommunication creates a new data structure with all the
// necessary primitives for a fully functional simulation to
// happen.
func NewCommunication() *Communication {
return &Communication{
downstream: make(chan btcutil.Address, *numActors),
timeReceived: make(chan time.Time, *numActors),
blockTxCount: make(chan int, *numActors),
height: make(chan int32),
split: make(chan int),
txpool: make(chan struct{}),
coinbaseQueue: make(chan *wire.MsgTx, chaincfg.SimNetParams.CoinbaseMaturity),
exit: make(chan struct{}),
errChan: make(chan struct{}, *numActors),
blockQueue: &blockQueue{
enqueue: make(chan *Block),
dequeue: make(chan *Block),
processed: make(chan *Block),
},
}
}
// Start handles the main part of a simulation by starting
// all the necessary goroutines.
func (com *Communication) Start(actors []*Actor, node *Node, txCurve map[int32]*Row) (tpsChan chan float64, tpbChan chan int) {
tpsChan = make(chan float64, 1)
tpbChan = make(chan int, 1)
// Start actors
for _, a := range actors {
com.wg.Add(1)
go func(a *Actor, com *Communication) {
defer com.wg.Done()
if err := a.Start(os.Stderr, os.Stdout, com); err != nil {
log.Printf("%s: Cannot start actor: %v", a, err)
a.Shutdown()
node.Shutdown()
}
}(a, com)
}
// Start a goroutine to check if all actors have failed
com.wg.Add(1)
go com.failedActors()
miningAddrs := make([]btcutil.Address, *numActors)
for i, a := range actors {
select {
case miningAddrs[i] = <-a.miningAddr:
case <-a.quit:
// This actor has quit
select {
case <-com.exit:
close(tpsChan)
close(tpbChan)
return
default:
}
}
}
// Start mining.
miner, err := NewMiner(miningAddrs, com.exit, com.height, com.txpool)
if err != nil {
close(com.exit)
close(tpsChan)
close(tpbChan)
com.wg.Add(1)
go com.Shutdown(miner, actors, node)
return
}
// Add mining node listen interface as a node
node.client.AddNode("localhost:18550", rpc.ANAdd)
// Start a goroutine to estimate tps
com.wg.Add(1)
go com.estimateTps(tpsChan, txCurve)
// Start a goroutine to find max tpb
com.wg.Add(1)
go com.estimateTpb(tpbChan)
// Start a goroutine to coordinate transactions
com.wg.Add(1)
go com.Communicate(txCurve, miner, actors)
com.wg.Add(1)
go com.queueBlocks()
com.wg.Add(1)
go com.poolUtxos(node.client, actors)
// Start a goroutine for shuting down the simulation when appropriate
com.wg.Add(1)
go com.Shutdown(miner, actors, node)
return
}
// queueBlocks queues blocks in the order they are received
func (com *Communication) queueBlocks() {
defer com.wg.Done()
var blocks []*Block
enqueue := com.blockQueue.enqueue
var dequeue chan *Block
var next *Block
out:
for {
select {
case n, ok := <-enqueue:
if !ok {
// If no blocks are queued for handling,
// the queue is finished.
if len(blocks) == 0 {
break out
}
// nil channel so no more reads can occur.
enqueue = nil
continue
}
if len(blocks) == 0 {
next = n
dequeue = com.blockQueue.dequeue
}
blocks = append(blocks, n)
case dequeue <- next:
blocks[0] = nil
blocks = blocks[1:]
if len(blocks) != 0 {
next = blocks[0]
} else {
// If no more blocks can be enqueued, the
// queue is finished.
if enqueue == nil {
break out
}
dequeue = nil
}
case <-com.exit:
break out
}
}
close(com.blockQueue.dequeue)
}
// poolUtxos receives a new block notification from the node server
// and pools the newly mined utxos to the corresponding actor's a.utxo
func (com *Communication) poolUtxos(client *rpc.Client, actors []*Actor) {
defer com.wg.Done()
// Update utxo pool on each block connected
for {
select {
case b, ok := <-com.blockQueue.dequeue:
if !ok {
return
}
block, err := client.GetBlock(b.hash)
if err != nil {
log.Printf("Cannot get block: %v", err)
return
}
// add new outputs to unspent pool
for i, tx := range block.Transactions {
next:
for n, vout := range tx.TxOut {
if i == 0 {
// in case of coinbase tx, add it to coinbase queue
// if the chan is full, the first tx would be mature
// so add it to the pool
select {
case com.coinbaseQueue <- tx:
break next
default:
// dequeue the first mature tx
mTx := <-com.coinbaseQueue
// enqueue the latest tx
com.coinbaseQueue <- tx
// we'll process the mature tx next
// so point tx to mTx
tx = mTx
// reset vout as per the new tx
vout = tx.TxOut[n]
}
}
// fetch actor who owns this output
var actor *Actor
if len(actors) == 1 {
actor = actors[0]
} else {
actor, err = com.getActor(actors, vout)
if err != nil {
log.Printf("Cannot get actor: %v", err)
continue next
}
}
txout := com.getUtxo(tx, vout, uint32(n))
// to be usable, the utxo amount should be
// split-able after deducting the fee
if txout.Amount > btcutil.Amount((*maxSplit))*(minFee) {
// if it's usable, add utxo to actor's pool
select {
case actor.utxoQueue.enqueue <- txout:
case <-com.exit:
}
}
}
}
// allow Communicate to sync with the processed block
if b.height == int32(*startBlock)-1 {
select {
case com.blockQueue.processed <- b:
case <-com.exit:
return
}
}
if b.height >= int32(*startBlock) {
var txCount, utxoCount int
for _, a := range actors {
utxoCount += len(a.utxoQueue.utxos)
}
txCount = len(block.Transactions)
log.Printf("Block %s (height %d) attached with %d transactions", b.hash, b.height, txCount)
log.Printf("%d transaction outputs available to spend", utxoCount)
select {
case com.blockQueue.processed <- b:
case <-com.exit:
return
}
select {
case com.blockTxCount <- txCount:
case <-com.exit:
return
}
}
case <-com.exit:
return
}
}
}
// getActor returns the actor to which this vout belongs to
func (com *Communication) getActor(actors []*Actor,
vout *wire.TxOut) (*Actor, error) {
// get addrs which own this utxo
_, addrs, _, err := txscript.ExtractPkScriptAddrs(vout.PkScript,
&chaincfg.SimNetParams)
if err != nil {
return nil, err
}
// we're expecting only 1 addr since we created a standard p2pkh tx
addr := addrs[0].String()
// find which actor this addr belongs to
// TODO: could probably be optimized by creating
// a global addr -> actor index rather than looking
// up each actor addrs
for _, actor := range actors {
for _, actorAddr := range actor.ownedAddresses {
if addr == actorAddr.String() {
return actor, nil
}
}
}
err = errors.New("cannot find any actor who owns this tx output")
return nil, err
}
// getUtxo returns a TxOut from Tx and Vout
func (com *Communication) getUtxo(tx *wire.MsgTx,
vout *wire.TxOut, index uint32) *TxOut {
txHash := tx.TxHash()
op := wire.NewOutPoint(&txHash, index)
unspent := TxOut{
OutPoint: op,
Amount: btcutil.Amount(vout.Value),
}
return &unspent
}
// failedActors checks for actors that aborted the simulation
func (com *Communication) failedActors() {
defer com.wg.Done()
var failedActors int
for {
select {
case <-com.errChan:
failedActors++
// All actors have failed
if failedActors == *numActors {
close(com.exit)
return
}
case <-com.exit:
return
}
}
}
// estimateTps estimates the average transactions per second of
// the simulation.
func (com *Communication) estimateTps(tpsChan chan<- float64, txCurve map[int32]*Row) {
defer com.wg.Done()
var first, last time.Time
var diff, curveDiff time.Duration
var txnCount, curveCount, block int
firstTx := true
for {
select {
case last = <-com.timeReceived:
if firstTx {
first = last
firstTx = false
}
txnCount++
diff = last.Sub(first)
curveCount++
if c, ok := txCurve[int32(block)]; ok && curveCount == c.txCount {
// A block has been mined; reset necessary variables
curveCount = 0
firstTx = true
curveDiff += diff
diff = curveDiff
// Use next block's desired transaction count
block++
}
case <-com.exit:
tpsChan <- float64(txnCount) / diff.Seconds()
return
}
}
}
// estimateTpb sends the maximum transactions over the returned chan
func (com *Communication) estimateTpb(tpbChan chan<- int) {
defer com.wg.Done()
var maxTpb int
for {
select {
case last := <-com.blockTxCount:
if last > maxTpb {
maxTpb = last
}
case <-com.exit:
tpbChan <- maxTpb
return
}
}
}
// Communicate generates tx and controls the mining according
// to the input block height vs tx count curve
func (com *Communication) Communicate(txCurve map[int32]*Row, miner *Miner, actors []*Actor) {
defer com.wg.Done()
for {
select {
case h := <-com.height:
// stop simulation if we're at the last block
if h > int32(*stopBlock) {
close(com.exit)
return
}
// disable mining until the required no. of tx are in mempool
if err := miner.StopMining(); err != nil {
close(com.exit)
return
}
// wait until this block is processed
select {
case <-com.blockQueue.processed:
case <-com.exit:
return
}
var wg sync.WaitGroup
// count the number of utxos available in total
var utxoCount int
for _, a := range actors {
utxoCount += len(a.utxoQueue.utxos)
}
// the required transactions are divided into two groups because we need some of them to
// contribute to the utxo count required for the next block and the rest to contribute to
// the tx count
//
// it is possible to keep dividing the same utxo until it's broken into the required
// number of pieces but we want to stay close to the real world scenario and maximize
// the number of utxos used
//
// E.g: Assume the following CSV
//
// block,utxos,tx
// 20000,40000,20000
// 20001,50000,25000
//
// at block 19999, we need to ensure that next block has 40K utxos
// we have 19999 - blockchain.CoinbaseMaturity = 19899 utxos
// we need to create 40K-19899 = 20101 utxos so in this case, so
// we create 20101 tx which give 1 net utxo output
//
// at block 20000, we need to ensure that next block has 50K utxos
// we already have 40K by the previous iteration, so we need 50-40 = 10K utxos
// we also need to generate 20K tx before the next block, so
// create 10000 tx which generate 1 net utxo plus 10000 tx without any net utxo
//
// since we cannot generate more tx than the no of available utxos, the no of tx
// that can be generated at any iteration is limited by the utxos available
// in case the next row doesn't exist, we initialize the required no of utxos to zero
// so we keep the utxoCount same as current count
next, ok := txCurve[h+2]
if !ok {
next = &Row{}
next.utxoCount = utxoCount
}
// reqUtxoCount is the number of utxos required
reqUtxoCount := 0
if next.utxoCount > utxoCount {
reqUtxoCount = next.utxoCount - utxoCount
}
// in case this row doesn't exist, we initialize the required no of tx to reqUtxoCount
// i.e one tx per utxo required
row, ok := txCurve[h+1]
if !ok {
row = &Row{}
row.txCount = reqUtxoCount
}
// reqTxCount is the number of tx that will generate reqUtxoCount
// no of utxos
reqTxCount := row.txCount
if reqTxCount > utxoCount {
log.Printf("Warning: capping no of transactions at %v based on no of available utxos", utxoCount)
// cap the total no of tx at the no of available utxos
reqTxCount = utxoCount
}
var multiplier, totalUtxos, totalTx int
// skip if we already have more than the no of utxos required
if reqUtxoCount > 0 {
// e.g: if we need 18K utxos in 12K tx
// multiplier = [18000/12000] = [1.5] = 2
// totalUtxos = 18000/2 = 9000
// totalTx = 120000 - 9000 = 3000
multiplier = int(math.Ceil(float64(reqUtxoCount) / float64(reqTxCount)))
if multiplier > *maxSplit {
// cap maximum splits at maxSplit
multiplier = *maxSplit
}
totalUtxos = reqUtxoCount / multiplier
}
// if we're not already covered by the utxo transactions, generate additional tx
if reqTxCount > totalUtxos {
totalTx = reqTxCount - totalUtxos
}
if reqTxCount > 0 {
log.Printf("Generating %v transactions ...", reqTxCount)
}
if totalTx > 0 {
for i := 0; i < totalTx; i++ {
fmt.Printf("\r%d/%d", i+1, reqTxCount)
a := actors[rand.Int()%len(actors)]
addr := a.ownedAddresses[rand.Int()%len(a.ownedAddresses)]
select {
case com.downstream <- addr:
// For every address sent downstream (one transaction about to happen),
// spawn a goroutine to listen for an accepted transaction in the mempool
wg.Add(1)
go com.txPoolRecv(&wg)
case <-com.exit:
return
}
}
}
if totalUtxos > 0 {
for i := 0; i < totalUtxos; i++ {
fmt.Printf("\r%d/%d", i+totalTx+1, reqTxCount)
select {
case com.split <- multiplier:
// For every address sent downstream (one transaction about to happen),
// spawn a goroutine to listen for an accepted transaction in the mempool
wg.Add(1)
go com.txPoolRecv(&wg)
case <-com.exit:
return
}
}
}
fmt.Printf("\n")
log.Printf("Waiting for miner...")
wg.Wait()
// mine the above tx in the next block
if err := miner.StartMining(); err != nil {
close(com.exit)
return
}
case <-com.exit:
return
}
}
}
// Shutdown shuts down the simulation by killing the mining and the
// initial node processes and shuts down all actors.
func (com *Communication) Shutdown(miner *Miner, actors []*Actor, node *Node) {
defer com.wg.Done()
<-com.exit
if miner != nil {
miner.Shutdown()
}
for _, a := range actors {
a.Shutdown()
}
if node != nil {
node.Shutdown()
}
}
// WaitForShutdown waits until every goroutine inside com.Start
// has returned.
func (com *Communication) WaitForShutdown() {
com.wg.Wait()
}
// txPoolRecv listens for transactions accepted in the miner mempool
// or errors happened during the creation or send of a transaction.
func (com *Communication) txPoolRecv(wg *sync.WaitGroup) {
defer wg.Done()
select {
case <-com.txpool:
case <-com.exit:
}
}