-
Notifications
You must be signed in to change notification settings - Fork 1
/
sim.cpp
799 lines (693 loc) · 26 KB
/
sim.cpp
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
// ########################################################################################
// #### Bitcoin Lightning Network Simulator of Payment Forwarding Requests #############
// ########################################################################################
// #### Developed by Bence Ladoczki <ladoczki@tmit.bme.hu> 2022-23 #######################
// #### All rights reserved #######################
// ########################################################################################
// # install: sudo apt install libomp-dev
// # run: export NUMBER_OF_VERTICES=300; export NUM_SIM=10; export NUMBER_OF_PAYMENTS=100; export CONNECTIVITY=10; export TOPOLOGY_FILE=topologies/graph_${CONNECTIVITY}; export OMP_NUM_THREADS=10 && g++ -o sim sim.cpp -fopenmp && ./sim
// ########################################################################################
// ########################################################################################
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <time.h>
#include <omp.h>
#define TR_AMT 100000 /* 0.001 BTC */
#define INIT_CAP 1000000 /* 0.010 BTC */
#define AM_BND 1.1 /* bound for large/small amounts */
//#define CAPACITY_LIMIT 800
#define SATOSHI_TO_BTC 100000000 /* 10^8 */
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
int count_lines(const char *filename) {
FILE *file = fopen(filename, "r"); // Fájl megnyitása olvasásra
if (file == NULL) {
perror("Hiba a fájl megnyitásakor");
exit(EXIT_FAILURE);
}
int line_count = 0;
char ch;
// Sorok számlálása
while ((ch = fgetc(file)) != EOF) {
if (ch == '\n') {
line_count++;
}
}
// Ha a fájl nem üres, akkor egy extra sort számolunk, ha a fájl nem végződik új sor karakterrel.
if (ch != '\n' && ftell(file) != 0) {
line_count++;
}
fclose(file); // Fájl bezárása
return line_count;
}
struct Edge
{
int source;
int destination;
int routing_fee;
int capacity;
int base_fee;
int variable_fee;
int routing_revenue;
int number_of_routed_payments;
int vf_lrg; /* variable fee for large transactions */
int vf_sml; /* variable fee for smaller transactions */
};
struct d_up
{
int at, from;
};
struct payment
{
int from, to, amount;
};
struct path
{
int len;
int* nodes;
};
struct Graph
{
int V, E;
struct Edge* edge;
};
struct Graph* createGraph(int V, int E)
{
struct Graph* graph = (struct Graph*) malloc( sizeof(struct Graph));
graph->V = V;
graph->E = E;
graph->edge = (struct Edge*) malloc( graph->E * sizeof( struct Edge ) );
for (int j = 0; j < graph->E; j++)
{
graph->edge[j].source = INT_MAX;
graph->edge[j].destination = INT_MAX;
}
return graph;
}
struct payment* create_payment(int f, int t, int a)
{
struct payment* pm = (struct payment*) malloc( sizeof(struct payment));
pm->from = f;
pm->to = t;
pm->amount = a;
return pm;
}
void FinalSolution(int dist[], int n)
{
printf("\nVertex\tDistance from Source Vertex\n");
int i;
for (i = 0; i < n; ++i){
printf("%d \t\t %d\n", i, dist[i]);
}
}
void findpaths(int tonode, struct d_up* dup, int dcnt, struct path* pth, struct Graph* G){
// printf("now at: %d \n",tonode);
if(pth->len > G->E){
printf(" too much recursion in findpaths... \n");
// printf("dcnt: %d \n",dcnt);
for(int i=0; i< dcnt; i++){
// printf("at: %d from: %d \n",dup[i].at, dup[i].from);
}
exit(1);
return;
}
if(tonode == 0) {
pth->nodes[pth->len] = 0;
return;
}
for(int ii = dcnt - 1; ii > -1; ii--){
if(tonode == dup[ii].at) {
pth->nodes[pth->len] = dup[ii].at;
pth->len = pth->len + 1;
findpaths(dup[ii].from, dup, dcnt, pth, G);
}
}
}
void swap(int* xp, int* yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void selectionSort(int arr[], int n)
{
int i, j, min_idx;
for (i = 0; i < n - 1; i++) {
min_idx = i;
for (j = i + 1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
swap(&arr[min_idx], &arr[i]);
}
}
int map_vertice(int vertice, int offset, int num_ver){
int new_vertice;
new_vertice = vertice - offset;
if(new_vertice < 0) {
new_vertice = new_vertice + num_ver;
}
return new_vertice;
}
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int map_vertices(struct Graph* graph, struct Graph* new_graph, int from, int to){
// The SP algoirthm works only when the edges are sorted in ascending order
// Here we reorder the edges if the source is not '0' and return a new graph object
int new_to;
int* source_vertices, *source_vertices_original, *target_vertices_original, *tv, *fees, *caps;
source_vertices = (int*) malloc(sizeof(int)*graph->E);
source_vertices_original = (int*) malloc(sizeof(int)*graph->E);
target_vertices_original = (int*) malloc(sizeof(int)*graph->E);
fees = (int*) malloc(sizeof(int)*graph->E);
caps = (int*) malloc(sizeof(int)*graph->E);
tv = (int*) malloc(sizeof(int)*graph->E);
int* rev = (int*) malloc(sizeof(int)*graph->E);
int* pms = (int*) malloc(sizeof(int)*graph->E);
int* vfees = (int*) malloc(sizeof(int)*graph->E);
int* vf_lrg = (int*) malloc(sizeof(int)*graph->E);
int* vf_sml = (int*) malloc(sizeof(int)*graph->E);
for (int j = 0; j < graph->E; j++){
int u = graph->edge[j].source;
int v = graph->edge[j].destination;
// printf(" u: %d v: %d new u: %d new v: %d \n",u,v, map_vertice(u, from, graph->V), map_vertice(v, from, graph->V));
source_vertices[j] = map_vertice(u, from, graph->V);
source_vertices_original[j] = map_vertice(u, from, graph->V);
target_vertices_original[j] = map_vertice(v, from, graph->V);
fees[j] = graph->edge[j].base_fee;
vfees[j] = graph->edge[j].variable_fee;
vf_lrg[j] = graph->edge[j].vf_lrg;
vf_sml[j] = graph->edge[j].vf_sml;
caps[j] = graph->edge[j].capacity;
rev[j] = graph->edge[j].routing_revenue;
pms[j] = graph->edge[j].number_of_routed_payments;
}
selectionSort(source_vertices, graph->E);
int vcnt = 0;
for (int j = 0; j < graph->E; j++){
int ln = 0;
for (int jj = 0; jj < graph->E; jj++){
tv[jj] = 0;
}
for (int jj = 0; jj < graph->E; jj++){
if(source_vertices_original[jj] == source_vertices[j]){
tv[ln] = target_vertices_original[jj];
ln = ln + 1;
}
}
selectionSort(tv, ln);
new_graph->edge[j].source = source_vertices[j];
new_graph->edge[j].destination = tv[vcnt];
for (int jj = 0; jj < graph->E; jj++){
if(source_vertices_original[jj] == new_graph->edge[j].source && new_graph->edge[j].destination == target_vertices_original[jj]){
new_graph->edge[j].base_fee = fees[jj];
new_graph->edge[j].variable_fee = vfees[jj];
new_graph->edge[j].vf_lrg = vf_lrg[jj];
new_graph->edge[j].vf_sml = vf_sml[jj];
new_graph->edge[j].capacity = caps[jj];
new_graph->edge[j].routing_revenue = rev[jj];
new_graph->edge[j].number_of_routed_payments = pms[jj];
}
}
vcnt = vcnt + 1;
if(j < graph->E - 1){
if(source_vertices[j] != source_vertices[j+1]){
vcnt = 0;
}
}
}
free(source_vertices);
free(source_vertices_original);
free(target_vertices_original);
free(fees);
free(vfees);
free(vf_lrg);
free(vf_sml);
free(caps);
free(tv);
free(rev);
free(pms);
return map_vertice(to, from, graph->V);
}
void print_graph(struct Graph* graph){
for (int j = 0; j < graph->E; j++){
printf("G.add_edge(\"%d\", \"%d\", weight=%d) \n", graph->edge[j].source, graph->edge[j].destination, graph->edge[j].capacity);
// printf("G.add_edge(\"%d\", \"%d\", weight=%d) \n", graph->edge[j].source, graph->edge[j].destination, graph->edge[j].base_fee);
}
return;
}
struct path* BellmanFord(struct Graph* graph, int from, int to, int amt)
{
int V = graph->V;
int E = graph->E;
int StoreDistance[V];
int StoreAmount[V];
int i,j;
int fee;
// print_graph(graph);
if(from != 0){
// The SP algoirthm works only when the edges are sorted in ascending order
struct Graph* new_graph = createGraph(graph->V, graph->E);
new_graph->V = graph->V;
new_graph->E = graph->E;
int new_to = map_vertices(graph, new_graph, from, to);
struct path* pth2 = BellmanFord(new_graph, 0, new_to, amt);
free(new_graph->edge);
free(new_graph);
for(int jj=0; jj < pth2->len + 1 ; jj++){
pth2->nodes[jj] = pth2->nodes[jj] + from;
if(pth2->nodes[jj] > graph->V - 1){
pth2->nodes[jj] = pth2->nodes[jj] - from;
pth2->nodes[jj] = pth2->nodes[jj] + from - graph->V ;
}
}
return pth2;
}
for (i = 0; i < V; i++)
StoreDistance[i] = INT_MAX;
StoreDistance[from] = 0;
for (i = 0; i < V; i++)
StoreAmount[i] = 0;
StoreAmount[from] = amt;
int dcnt = 0;
for (i = 1; i <= V-1; i++)
{
for (j = 0; j < E; j++)
{
int u = graph->edge[j].source;
int v = graph->edge[j].destination;
// if(graph->edge[j].capacity > CAPACITY_LIMIT){
// graph->edge[j].base_fee = 0;
// }
if(StoreAmount[u] < TR_AMT/AM_BND && StoreDistance[u] != INT_MAX){
/* small amounts */
fee = StoreAmount[u]*((double)graph->edge[j].vf_sml/1000000) + graph->edge[j].base_fee; // This is the dynamic cost of the edge depending on the transaction amount for small amounts
}
else if(StoreAmount[u] > TR_AMT*AM_BND && StoreDistance[u] != INT_MAX){
/* large amounts */
fee = StoreAmount[u]*((double)graph->edge[j].vf_lrg/1000000) + graph->edge[j].base_fee; // This is the dynamic cost of the edge depending on the transaction amount for large amounts
}
else{
fee = StoreAmount[u]*((double)graph->edge[j].variable_fee/1000000) + graph->edge[j].base_fee; // This is the dynamic cost of the edge depending on the transaction amount for averaged size transactions
}
if(fee < 0){
printf("StoreAmount[u] %d \n",StoreAmount[u]);
printf("graph->edge[j].variable_fee %d \n",graph->edge[j].variable_fee);
printf("graph->edge[j].base_fee %d \n",graph->edge[j].base_fee);
exit(1);
}
// printf("%d. trying edge: %d from: %d %d \n",j, v, u, base_fee);
if(graph->edge[j].capacity > amt){ /* use channels with enough capacity */
if (StoreDistance[u] + fee < StoreDistance[v] && StoreDistance[u] != INT_MAX){
StoreDistance[v] = StoreDistance[u] + fee;
StoreAmount[v] = StoreAmount[u] - fee; // This is the remaining amount when v is reached from u
// printf("%d. distance update at: %d from: %d %d %d \n",j, v, u, base_fee, StoreDistance[u]);
dcnt = dcnt + 1;
}
}
}
}
// printf("dcnt1 %d \n",dcnt);
struct d_up* dup = (d_up*)malloc(sizeof(d_up)*(dcnt));
if(dup == NULL){
printf(" memory error in BellmanFord...\n");
exit(1);
}
else{
// printf("mem allocated (Kb): %lu dcnt: %d \n",sizeof(d_up)*(dcnt+1)/(1024), dcnt);
}
for(i=0;i< dcnt;i++){
dup[i].at = INT_MAX;
dup[i].from = INT_MAX;
}
for (i = 0; i < V; i++)
StoreDistance[i] = INT_MAX;
StoreDistance[from] = 0;
for (i = 0; i < V; i++)
StoreAmount[i] = 0;
StoreAmount[from] = amt;
dcnt = 0;
for (i = 1; i <= V-1; i++)
{
for (j = 0; j < E; j++)
{
int u = graph->edge[j].source;
int v = graph->edge[j].destination;
// if(graph->edge[j].capacity > CAPACITY_LIMIT){
// graph->edge[j].base_fee = 0;
// }
if(StoreAmount[u] < TR_AMT/AM_BND && StoreDistance[u] != INT_MAX){
/* small amounts */
fee = StoreAmount[u]*((double)graph->edge[j].vf_sml/1000000) + graph->edge[j].base_fee; // This is the dynamic cost of the edge depending on the transaction amount for small amounts
}
else if(StoreAmount[u] > TR_AMT*AM_BND && StoreDistance[u] != INT_MAX){
/* large amounts */
fee = StoreAmount[u]*((double)graph->edge[j].vf_lrg/1000000) + graph->edge[j].base_fee; // This is the dynamic cost of the edge depending on the transaction amount for large amounts
}
else{
fee = StoreAmount[u]*((double)graph->edge[j].variable_fee/1000000) + graph->edge[j].base_fee; // This is the dynamic cost of the edge depending on the transaction amount for averaged size transactions
}
if(graph->edge[j].capacity > amt){ /* use channels with enough capacity */
if (StoreDistance[u] + fee < StoreDistance[v] && StoreDistance[u] != INT_MAX){
StoreDistance[v] = StoreDistance[u] + fee;
StoreAmount[v] = StoreAmount[u] - fee; // This is the remaining amount when v is reached from u
// printf("%d. trying edge: %d from: %d %d \n",j, v, u, fee);
int newelem = 0;
for(int ii = 0; ii < dcnt; ii++){
if(dup[ii].at == v){
dup[ii].from = u;
newelem = 1;
}
}
if(newelem == 0){
dup[dcnt].at = v;
dup[dcnt].from = u;
dcnt = dcnt + 1;
}
}
}
}
}
// printf("dcnt2 %d \n",dcnt);
struct path* pth = (path*)malloc(sizeof(path) );
pth->nodes = (int*)malloc(sizeof(int) * graph->E );
for(i = 0; i < graph->E; i++){
pth->nodes[i] = 0;
}
pth->len = 0;
findpaths(to, dup, dcnt, pth, graph);
free(dup);
// for (i = 0; i < E; i++)
// {
// int u = graph->edge[i].source;
//
// int v = graph->edge[i].destination;
//
// int routing_fee = graph->edge[i].routing_fee;
//
// // if (StoreDistance[u] + routing_fee < StoreDistance[v])
// // printf("This graph contains negative edge cycle\n");
// }
//
// // FinalSolution(StoreDistance, V);
return pth;
}
void load_topology(const char* file_name, Graph* graph, int cap)
{
FILE* file = fopen (file_name, "r");
int i = 0;
int counter = 0;
int ii = 0;
while (!feof (file))
{
fscanf (file, "%d", &i);
if(ii % 4 == 0){
if( counter > graph->E - 1){
fclose (file);
return;
}
graph->edge[counter].source = i;
}
if(ii % 4 == 1){
graph->edge[counter].destination = i;
}
if(ii % 4 == 2){
graph->edge[counter].routing_fee = i;
graph->edge[counter].base_fee = 1000;
graph->edge[counter].capacity = cap;
graph->edge[counter].routing_revenue = 0;
graph->edge[counter].number_of_routed_payments = 0;
}
if(ii % 4 == 3){
graph->edge[counter].variable_fee = 1000;
graph->edge[counter].vf_lrg = 1000;
graph->edge[counter].vf_sml = 1000;
counter = counter + 1;
}
ii = ii + 1;
}
fclose (file);
}
int reduce_cap(struct Graph* graph, int source, int destination, int amt, int FEE_CORRECTION, int FEE_CORRECTION_LARGE, int FEE_CORRECTION_SMALL ){
for (int j = 0; j < graph->E; j++)
{
if(graph->edge[j].source == source && graph->edge[j].destination == destination) {
graph->edge[j].capacity = graph->edge[j].capacity - amt;
if(graph->edge[j].capacity < INIT_CAP/2) {
// graph->edge[j].base_fee = graph->edge[j].base_fee * 2;
graph->edge[j].variable_fee = graph->edge[j].variable_fee + FEE_CORRECTION;
graph->edge[j].vf_lrg = graph->edge[j].vf_lrg + FEE_CORRECTION_LARGE;
graph->edge[j].vf_sml = graph->edge[j].vf_sml + FEE_CORRECTION_SMALL;
return 1;
}
return 0;
}
}
printf(" No such edge: %d %d \n", source,destination);
return 0;
}
void increase_cap(struct Graph* graph, int source, int destination, int amt, int FEE_CORRECTION, int FEE_CORRECTION_LARGE, int FEE_CORRECTION_SMALL){
for (int j = 0; j < graph->E; j++)
{
if(graph->edge[j].source == source && graph->edge[j].destination == destination) {
graph->edge[j].capacity = graph->edge[j].capacity + amt;
if(graph->edge[j].capacity > INIT_CAP + INIT_CAP/2) {
// graph->edge[j].base_fee = MAX(graph->edge[j].base_fee / 2, 1 );
graph->edge[j].variable_fee = MAX(graph->edge[j].variable_fee - FEE_CORRECTION,0);
graph->edge[j].vf_lrg = MAX(graph->edge[j].vf_lrg - FEE_CORRECTION_LARGE, 0);
graph->edge[j].vf_sml = MAX(graph->edge[j].vf_sml - FEE_CORRECTION_SMALL, 0);
}
return;
}
}
printf(" No such edge: %d %d \n", source,destination);
return;
}
int get_channel_capacity(int from, int to, struct Graph* graph){
for (int j = 0; j < graph->E; j++)
{
if(graph->edge[j].source == from && graph->edge[j].destination == to) {
return graph->edge[j].capacity;
// return graph->edge[j].routing_fee;
}
}
printf(" No such edge: %d %d \n", from,to);
return 0;
}
int get_routing_revenue(int from, int to, struct Graph* graph, int amt){
for (int j = 0; j < graph->E; j++)
{
if(graph->edge[j].source == from && graph->edge[j].destination == to) {
graph->edge[j].number_of_routed_payments += 1;
if(amt < TR_AMT/AM_BND){
graph->edge[j].routing_revenue += graph->edge[j].base_fee + amt*((double)graph->edge[j].vf_sml/1000000);
return graph->edge[j].base_fee + amt*((double)graph->edge[j].vf_sml/1000000);
}
if(amt > TR_AMT*AM_BND){
graph->edge[j].routing_revenue += graph->edge[j].base_fee + amt*((double)graph->edge[j].vf_lrg/1000000);
return graph->edge[j].base_fee + amt*((double)graph->edge[j].vf_lrg/1000000);
}
else{
graph->edge[j].routing_revenue += graph->edge[j].base_fee + amt*((double)graph->edge[j].variable_fee/1000000);
return graph->edge[j].base_fee + amt*((double)graph->edge[j].variable_fee/1000000);
}
}
}
printf(" No such edge: %d %d \n", from,to);
return 0;
}
int send_payment(struct payment* pm, struct Graph* graph, int* nofp, int FEE_CORRECTION, int FEE_CORRECTION_LARGE, int FEE_CORRECTION_SMALL){
int capacity_critical;
printf("sending payment from %d to %d amount: %d \n",pm->from,pm->to,pm->amount);
clock_t t;
t = clock();
struct path* pth = BellmanFord(graph, pm->from, pm->to, pm->amount);
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds
// printf("BellmanFord() took %f seconds to execute \n", time_taken);
capacity_critical = 0;
// printf("sp len: %d ",pth->len);
int routing_revenue = 0;
if(pth->len > 0){
printf("(%d) %d ->",pth->len, pth->nodes[0]);
for(int jj=1; jj < pth->len+1 ; jj++){
if(jj == pth->len){
printf(" %d ", pth->nodes[jj]);
}
else{
printf(" %d ->", pth->nodes[jj]);
nofp[pth->nodes[jj]] = nofp[pth->nodes[jj]] + 1;
}
capacity_critical = reduce_cap(graph, pth->nodes[jj], pth->nodes[jj-1], pm->amount, FEE_CORRECTION, FEE_CORRECTION_LARGE, FEE_CORRECTION_SMALL );
increase_cap(graph, pth->nodes[jj-1], pth->nodes[jj], pm->amount, FEE_CORRECTION, FEE_CORRECTION_LARGE, FEE_CORRECTION_SMALL );
routing_revenue += get_routing_revenue(pth->nodes[jj] , pth->nodes[jj-1] , graph, pm->amount);
}
}
printf(" \n");
// printf("routing_revenue: %d \n",routing_revenue);
free(pm);
free(pth->nodes);
free(pth);
return routing_revenue;
}
int get_random_number(){
int randomvalue;
FILE *fpointer;
fpointer = fopen("/dev/urandom", "rb");
if(fpointer == NULL){
printf("fpointer == NULL in get_random_number \n");
}
fread(&randomvalue,sizeof(int),1,fpointer);
fclose(fpointer);
return abs(randomvalue);
}
int main(int argc, char *argv[])
{
int V,E;
int from,to;
clock_t start, end;
time_t mytime = time(NULL);
char * time_str = ctime(&mytime);
const char *env_var = "CONNECTIVITY"; // Környezeti változó neve
const char *cn = getenv(env_var); // Környezeti változó lekérése
int topology_number = strtol(cn, NULL, 10);
char command[256];
snprintf(command, sizeof(command), "python3 generate_graph.py %d topologies/graph_%d", topology_number, topology_number);
int result = system(command);
if (result == -1) {
perror("system hívás hibája");
return EXIT_FAILURE;
}
env_var = "TOPOLOGY_FILE"; // Környezeti változó neve
const char *topology_file = getenv(env_var); // Környezeti változó lekérése
time_str[strlen(time_str)-1] = '\0';
printf("Start Time : %s\n", time_str);
// sscanf (argv[1],"%d",&V);
env_var = "NUMBER_OF_VERTICES";
cn = getenv(env_var); // Környezeti változó lekérése
V = strtol(cn, NULL, 10);
// int** sim_res = (int**)malloc(sizeof(int*)*NUM_SIM);
// for(int ii=0; ii < NUM_SIM; ii++) sim_res[ii] = (int*)malloc(sizeof(int)*NUMBER_OF_PAYMENTS);
env_var = "NUM_SIM";
cn = getenv(env_var); // Környezeti változó lekérése
int NUM_SIM = strtol(cn, NULL, 10);
env_var = "NUMBER_OF_PAYMENTS";
cn = getenv(env_var); // Környezeti változó lekérése
int NUMBER_OF_PAYMENTS = strtol(cn, NULL, 10);
env_var = "FEE_CORRECTION";
cn = getenv(env_var); // Környezeti változó lekérése
int FEE_CORRECTION = strtol(cn, NULL, 10);
env_var = "FEE_CORRECTION_LARGE";
cn = getenv(env_var); // Környezeti változó lekérése
int FEE_CORRECTION_LARGE = strtol(cn, NULL, 10);
env_var = "FEE_CORRECTION_SMALL";
cn = getenv(env_var); // Környezeti változó lekérése
int FEE_CORRECTION_SMALL = strtol(cn, NULL, 10);
E = count_lines(topology_file);
#pragma omp parallel for
for(int isim = 0; isim < NUM_SIM; isim++){
struct Graph* graph = createGraph(V, E);
int i;
int* number_of_forwarded_payments = (int*)malloc(sizeof(int)*V);
for(int ii=0; ii < V; ii++){
number_of_forwarded_payments[ii] = 0;
}
clock_t t;
#pragma omp critical
{
printf("Loading topology... %d %d \n", omp_get_thread_num(), isim);
t = clock();
load_topology(topology_file, graph, INIT_CAP);
printf("Topology loaded... \n");
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds
printf("topology() took %f seconds to execute \n", time_taken);
}
int revs = 0;
// Simulate payments:
for(int ii=1; ii < NUMBER_OF_PAYMENTS + 1; ii++){
// sim_res[isim][ii] = get_channel_capacity(6,8, graph);
from = get_random_number() % V;
to = get_random_number() % V;
while(to == from){
to = get_random_number() % V;
}
int amt = get_random_number() % TR_AMT;
while(amt == 0){
amt = get_random_number() % TR_AMT;
}
printf("%d. ",ii);
revs = revs + send_payment(create_payment(from, to, amt), graph, number_of_forwarded_payments, FEE_CORRECTION, FEE_CORRECTION_LARGE, FEE_CORRECTION_SMALL);
int maxrr = 0;
int maxnr = 0;
for (int j = 0; j < graph->E; j++)
{
maxrr = MAX(graph->edge[j].routing_revenue, maxrr);
maxnr = MAX(graph->edge[j].number_of_routed_payments, maxnr);
}
for (int j = 0; j < graph->E; j++)
{
if(graph->edge[j].routing_revenue == maxrr && graph->edge[j].number_of_routed_payments > 5){
printf("%d/%d u: %d v: %d rr: %f BTC r_payments: %d vf_lrg: %d vf_sml: %d maxrevenue \n",omp_get_thread_num(), ii, graph->edge[j].source, graph->edge[j].destination, (double)maxrr/SATOSHI_TO_BTC, graph->edge[j].number_of_routed_payments, graph->edge[j].vf_lrg, graph->edge[j].vf_sml);
}
if(graph->edge[j].number_of_routed_payments > maxnr - 2 && graph->edge[j].number_of_routed_payments > 5){
printf("%d/%d u: %d v: %d r_payments: %d rr: %f BTC vf_lrg: %d vf_sml: %d maxnumber \n",omp_get_thread_num(), ii, graph->edge[j].source, graph->edge[j].destination, maxnr, (double)graph->edge[j].routing_revenue/SATOSHI_TO_BTC, graph->edge[j].vf_lrg, graph->edge[j].vf_sml);
}
}
}
// print_graph(graph);
printf("%d. sum_rev: %f BTC \n",isim, (double)revs/SATOSHI_TO_BTC);
// printf(" %5.3f \n",(float)(100*cc) / (float)NUMBER_OF_PAYMENTS);
// for(int ii=0; ii < V; ii++){
// printf("id %d number_of_forwarded_payments: %d \n",ii, number_of_forwarded_payments[ii]);
// }
free(graph->edge);
free(graph);
}
//
// FILE *out_file = fopen("vis", "w"); // write only
// fprintf(out_file, "[");
// for(int ii = 0; ii < NUMBER_OF_PAYMENTS; ii++){
// int res0 = sim_res[0][ii];
// int res1 = sim_res[1][ii];
// int res2 = sim_res[2][ii];
// int res3 = sim_res[3][ii];
// int res4 = sim_res[4][ii];
// int res5 = sim_res[5][ii];
// int res6 = sim_res[6][ii];
// int res7 = sim_res[7][ii];
// int res8 = sim_res[8][ii];
// int res9 = sim_res[9][ii];
// int av = (res0+res1+res2+res3+res4+res5+res6+res7+res8+res9)/10;
// fprintf(out_file, "[%d,",ii );
// // fprintf(out_file, "%d, ",res0 );
// // fprintf(out_file, "%d, ",res1 );
// // fprintf(out_file, "%d, ",res2 );
// // fprintf(out_file, "%d, ",res3 );
// // fprintf(out_file, "%d, ",res4 );
// // fprintf(out_file, "%d, ",res5 );
// // fprintf(out_file, "%d, ",res6 );
// // fprintf(out_file, "%d, ",res7 );
// // fprintf(out_file, "%d, ",res8 );
// if(ii < NUMBER_OF_PAYMENTS - 1){
// fprintf(out_file, "%d ], \n ",av );
// }
// else{
// fprintf(out_file, "%d ]\n ",av );
// }
// }
//
// fprintf(out_file, "]");
// fclose(out_file);
// print_graph(graph);
mytime = time(NULL);
time_str = ctime(&mytime);
time_str[strlen(time_str)-1] = '\0';
printf("End Time : %s\n", time_str);
return 0;
}