forked from sbshrey/DiPETrans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workerServer.cpp
804 lines (625 loc) · 31.2 KB
/
workerServer.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
800
801
802
803
804
// This autogenerated skeleton file illustrates how to build a server.
// You should copy it to another filename to avoid overwriting it.
#include "gen-cpp/MasterService.h"
#include "gen-cpp/WorkerService.h"
#include "gen-cpp/SharedService.h"
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/transport/TSocket.h>
#include <thrift/concurrency/ThreadManager.h>
#include <thrift/server/TThreadPoolServer.h>
#include <thrift/concurrency/PosixThreadFactory.h>
#include "Logger.h"
#include <iostream>
#include <bits/stdc++.h>
#include <openssl/sha.h>
#include <sstream>
#include <iomanip>
#include "contract.h"
#include "utils.h"
using namespace std;
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
using namespace ::apache::thrift::concurrency;
using namespace ::MasterService;
using namespace ::WorkerService;
using namespace ::SharedService;
std::map<string, int64_t> GlobalDataItemsMap;
//std::vector<string> addresses;
string WID = "1";
string dir_path;
string MSG="WorkerServer";
int masterPort = 8090;
string masterIP = "192.168.0.11";
//string masterIP = "localhost";
string convert_block_to_string(Block block) {
stringstream ss;
ss << block.number << block.prevHash << block.nonce; // << block.transactionsList;
//ss << block.finalDataItemMap << block.sendTxnMap;
for (auto& tx : block.transactionsList) {
ss << tx.transactionID;
ss << tx.toAddress;
ss << tx.fromAddress;
ss << tx.value;
ss << tx.input;
ss << tx.creates;
//str.append(to_string(tx.gas));
//str.append(to_string(tx.gasPrice));
}
return ss.str();
}
Block createBlock(Block b) {
Block block;
block.number = b.number; //stoi(data.key());//atoi(d.key().c_str());
block.prevHash = b.prevHash;
for (auto& tx: b.transactionsList) {
Transaction transaction;
transaction.transactionID = tx.transactionID;
transaction.fromAddress = tx.fromAddress;
transaction.toAddress = tx.toAddress;
transaction.value = tx.value;
transaction.input = tx.input; // (double)tx["gas"];
transaction.creates = tx.creates; // (double)tx["gasPrice"];
block.transactionsList.push_back(transaction);
}
return block;
}
struct mine_data {
Block block;
int64_t nonce;
int16_t interval;
};
string difficulty = "00011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111";
/*
bool execute (Transaction transaction, int status) {
bool stat = false;
string cmd = "";
// status == 0 contract creation
// status == 1 toAddress is the contract address
// status == 2 fromAddress is the contract address
if (status == 0) {
cmd = "./contract_erc20 " + transaction.creates + " " + transaction.fromAddress + " " + transaction.toAddress;
} else if (status == 1) {
cmd = "./contract_erc20 " + transaction.toAddress + " " + transaction.fromAddress + " " + transaction.input;
} else {
cmd = "./contract_erc20 " + transaction.fromAddress + " " + transaction.toAddress + " " + transaction.input;
}
const char *command = cmd.c_str();
stat = system(command);
return true;
}
*/
void *mine(void *threadarg) {
struct mine_data *td;
td = (struct mine_data *) threadarg;
Block newBlock = createBlock(td->block);
newBlock.nonce = td->nonce;
string hash;
//cout << "thread mine starts" << endl;
while (true) {
string block_str = convert_block_to_string(newBlock);
hash = sha256(block_str);
//cout << newBlock.nonce << ",";
if (hash.compare(difficulty) < 0) {
//cout << newBlock.nonce << endl;
//cout << block_str << endl;
cout << "Success W" << WID << " " << newBlock.nonce << " " << hash << endl;
break;
}
newBlock.nonce+=td->interval;
}
//cout << "thread mine ends" << endl;
std::shared_ptr<TTransport> socket(new TSocket(masterIP, masterPort));
std::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
std::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
MasterServiceClient masterClient(protocol);
//Logger::instance().log(MSG+" Block "+to_string(block.number) +" connection to master "+WID+" starts", Logger::kLogLevelInfo);
transport->open();
//Logger::instance().log(MSG+" Block "+to_string(block.number) +" worker "+WID+" mineBlock() starts", Logger::kLogLevelInfo);
// sent transaction to execute
masterClient.recvMiningStatus(newBlock.nonce, newBlock.number); // returns local worker response
cout << "Worker " << WID << "sent to master " << newBlock.number << " " << newBlock.nonce << endl;
//cout << worker->threadID << ":" << sendTransactionMap[worker->workerID].size() << "\t";
//Logger::instance().log(MSG+" Block "+to_string(block.number) +" WorkerID "+WID+" mineBlock() ends", Logger::kLogLevelInfo);
transport->close();
//Logger::instance().log(MSG+" Block "+to_string(block.number) +" connection to master "+WID+" ends", Logger::kLogLevelInfo);
//free(td);
delete td;
pthread_exit(NULL);
}
ofstream nttfile;
ofstream cttfile;
ofstream valnttfile;
ofstream valcttfile;
ofstream val2nttfile;
ofstream val2cttfile;
struct mine_data td;
class WorkerServiceHandler : public WorkerServiceIf {
public:
WorkerServiceHandler() {
// Your initialization goes here
/*Logger::instance().log(MSG+" Initializing accounts with 100000000000000000000000 wei to execute transactions starts", Logger::kLogLevelInfo);
string line;
ifstream accounts_file ("data/bigquery/addresses.json");
//ifstream dataItem_file ("data/bigquery/dataItems.json");
json accounts;
accounts_file >> accounts;
// iterate the array
for (json::iterator it = accounts.begin(); it != accounts.end(); ++it) {
dataItemMap[it.key()].value = it.value();
}
Logger::instance().log(MSG+" Initializing accounts with 100000000000000000000000 wei to execute transactions ends", Logger::kLogLevelInfo);
*/
nttfile.open(dir_path + "be_ntt_"+WID+".log",std::ofstream::out | std::ofstream::trunc);
cttfile.open(dir_path + "be_ctt_"+WID+".log",std::ofstream::out | std::ofstream::trunc);
valnttfile.open(dir_path + "be_val_ntt_"+WID+".log",std::ofstream::out | std::ofstream::trunc);
valcttfile.open(dir_path + "be_val_ctt_"+WID+".log",std::ofstream::out | std::ofstream::trunc);
val2nttfile.open(dir_path + "be_val2_ntt_"+WID+".log",std::ofstream::out | std::ofstream::trunc);
val2cttfile.open(dir_path + "be_val2_ctt_"+WID+".log",std::ofstream::out | std::ofstream::trunc);
}
void recvTransactions( ::SharedService::WorkerResponse& _return, const std::vector< ::SharedService::Transaction> & TransactionsList, const std::map<std::string, ::SharedService::DataItem> & dataItemMap, const std::set<std::string> & contractAddresses) {
// Your implementation goes here
double normal_txn_time = 0;
double contract_txn_time = 0;
//Logger::instance().log(MSG+" worker "+ WID +" recvTransactions starts", Logger::kLogLevelInfo);
auto start = chrono::steady_clock::now();
int successful_transactions = 0;
int failed_transactions = 0;
int total_transactions = 0;
int sc_cnt = 0;
int nsc_cnt = 0;
double tx_fees = 0;
//Logger::instance().log(MSG+" dataItemMap starts", Logger::kLogLevelInfo);
for (auto const& address: dataItemMap)
{
_return.dataItemMap[address.first] = address.second;
//cout << "txn value at worker " << _return.dataItemMap[address.first].value << "\t" << address.second.value << endl;
}
//Logger::instance().log(MSG+" dataItemMap ends", Logger::kLogLevelInfo);
//Logger::instance().log(MSG+" contractAddresses starts", Logger::kLogLevelInfo);
//cout << "contractAddresses size: " << contractAddresses.size() << endl;
for (auto const& address: contractAddresses)
{
//cout << address << endl;
_return.contractAddresses.insert(address);
//cout << "txn value at worker " << _return.dataItemMap[address.first].value << "\t" << address.second.value << endl;
}
//Logger::instance().log(MSG+" contractAddresses ends", Logger::kLogLevelInfo);
//Logger::instance().log(MSG+" TransactionsList starts", Logger::kLogLevelInfo);
//cout << endl;
for (auto const& tx: TransactionsList) {
//cout << "tx" << tx.transactionID << "\t";
auto txn_start = chrono::steady_clock::now();
auto txn_end = chrono::steady_clock::now();
double fee;
bool status= false;
if (tx.toAddress == "creates") {
//contract_addresses.push_back(tx.creates);
//cout << "creates" << endl;
_return.contractAddresses.insert(tx.creates);
DataItem localDataItem;
call_contract(&localDataItem, tx.creates, tx.fromAddress, tx.toAddress, tx.value);
//cout << "size before " << _return.dataItemMap[tx.creates].balances.size() << endl;
//_return.dataItemMap[tx.creates].value = localDataItem.value;
_return.dataItemMap[tx.creates] = localDataItem;
//_return.dataItemMap[tx.creates].balances = localDataItem.balances;
//_return.dataItemMap[tx.creates].allowed = localDataItem.allowed;
//_return.dataItemMap[tx.creates].votes = localDataItem.votes;
//_return.dataItemMap[tx.creates].balances = localDataItem.balances;
//cout << "size after " << _return.dataItemMap[tx.creates].balances.size() << endl;
//_return.dataItemMap[tx.creates].balances = localDataItem.balances;
txn_end = chrono::steady_clock::now();
contract_txn_time += chrono::duration_cast<chrono::microseconds>(txn_end - txn_start).count();
sc_cnt++;
} else {
bool flag = false;
std::set<string>::iterator it = std::find (_return.contractAddresses.begin(), _return.contractAddresses.end(), tx.toAddress);
if (it != _return.contractAddresses.end()) {
//cout << "contractAddress found in to" << endl;
DataItem localDataItem = _return.dataItemMap[tx.toAddress];
call_contract(&localDataItem, tx.toAddress, tx.fromAddress, tx.input, tx.value);
_return.dataItemMap[tx.toAddress] = localDataItem;
//_return.dataItemMap[tx.toAddress].value = localDataItem.value;
//strcpy(_return.dataItemMap[tx.toAddress].owner,localDataItem.owner);
//_return.dataItemMap[tx.toAddress].owner = localDataItem.owner;
//_return.dataItemMap[tx.toAddress].balances = localDataItem.balances;
//_return.dataItemMap[tx.toAddress].allowed = localDataItem.allowed;
//_return.dataItemMap[tx.toAddress].votes = localDataItem.votes;
//_return.dataItemMap[tx.toAddress].balances = localDataItem.balances;
txn_end = chrono::steady_clock::now();
contract_txn_time += chrono::duration_cast<chrono::microseconds>(txn_end - txn_start).count();
sc_cnt++;
flag = true;
}
it = std::find (_return.contractAddresses.begin(), _return.contractAddresses.end(), tx.fromAddress);
if (it != _return.contractAddresses.end() and !flag) {
//cout << "contractAddress found in from" << endl;
DataItem localDataItem = _return.dataItemMap[tx.fromAddress];
call_contract(&localDataItem, tx.fromAddress, tx.toAddress, tx.input, tx.value);
_return.dataItemMap[tx.fromAddress] = localDataItem;
//_return.dataItemMap[tx.fromAddress].value = localDataItem.value;
//_return.dataItemMap[tx.fromAddress].owner = localDataItem.owner;
//_return.dataItemMap[tx.fromAddress].balances = localDataItem.balances;
//_return.dataItemMap[tx.fromAddress].allowed = localDataItem.allowed;
//_return.dataItemMap[tx.fromAddress].votes = localDataItem.votes;
//_return.dataItemMap[tx.fromAddress].balances = localDataItem.balances;
txn_end = chrono::steady_clock::now();
contract_txn_time += chrono::duration_cast<chrono::microseconds>(txn_end - txn_start).count();
sc_cnt++;
flag = true;
}
if (!flag) {
//cout << "contractAddress not found" << endl;
if (_return.dataItemMap[tx.fromAddress].value >= tx.value) {
_return.dataItemMap[tx.fromAddress].value -= tx.value;
_return.dataItemMap[tx.toAddress].value += tx.value;
status = true;
} else {
//cout << "nsc txn " << _return.dataItemMap[tx.fromAddress].value << "\t" << tx.value << endl;
status = false;
}
txn_end = chrono::steady_clock::now();
normal_txn_time += chrono::duration_cast<chrono::microseconds>(txn_end - txn_start).count();
nsc_cnt++;
}
}
//cout << endl;
if (status) {
successful_transactions++;
} else {
failed_transactions++;
}
tx_fees += fee;
_return.transactionIDList.push_back(tx.transactionID);
total_transactions++;
}
cout << "WID " << WID << "\tsc " << sc_cnt << "\tnsc " << nsc_cnt << endl;
//cout << "_return.dataItemMap size " << _return.dataItemMap.size() << endl;
//ofstream scfile;
//scfile.open(dir_path+"sc_call.csv",std::ofstream::out | std::ofstream::trunc);
cttfile << contract_txn_time << endl;
nttfile << normal_txn_time << endl;
//_return.transactionFees = tx_fees;
//Logger::instance().log(MSG+" TransactionsList ends", Logger::kLogLevelInfo);
//Logger::instance().log(MSG+" worker "+ WID +" recvTransactions ends", Logger::kLogLevelInfo);
auto end = chrono::steady_clock::now();
}
void recvValTransactions( ::SharedService::WorkerResponse& _return, const std::vector< ::SharedService::Transaction> & TransactionsList, const std::map<std::string, ::SharedService::DataItem> & dataItemMap, const std::set<std::string> & contractAddresses) {
// Your implementation goes here
double normal_txn_time = 0;
double contract_txn_time = 0;
//Logger::instance().log(MSG+" worker "+ WID +" recvTransactions starts", Logger::kLogLevelInfo);
auto start = chrono::steady_clock::now();
int successful_transactions = 0;
int failed_transactions = 0;
int total_transactions = 0;
int sc_cnt = 0;
int nsc_cnt = 0;
double tx_fees = 0;
//Logger::instance().log(MSG+" dataItemMap starts", Logger::kLogLevelInfo);
for (auto const& address: dataItemMap)
{
_return.dataItemMap[address.first] = address.second;
//cout << "txn value at worker " << _return.dataItemMap[address.first].value << "\t" << address.second.value << endl;
}
//Logger::instance().log(MSG+" dataItemMap ends", Logger::kLogLevelInfo);
//Logger::instance().log(MSG+" contractAddresses starts", Logger::kLogLevelInfo);
//cout << "contractAddresses size: " << contractAddresses.size() << endl;
for (auto const& address: contractAddresses)
{
//cout << address << endl;
_return.contractAddresses.insert(address);
//cout << "txn value at worker " << _return.dataItemMap[address.first].value << "\t" << address.second.value << endl;
}
//Logger::instance().log(MSG+" contractAddresses ends", Logger::kLogLevelInfo);
//Logger::instance().log(MSG+" TransactionsList starts", Logger::kLogLevelInfo);
//cout << endl;
for (auto const& tx: TransactionsList) {
//cout << "tx" << tx.transactionID << "\t";
auto txn_start = chrono::steady_clock::now();
auto txn_end = chrono::steady_clock::now();
double fee;
bool status= false;
if (tx.toAddress == "creates") {
//contract_addresses.push_back(tx.creates);
//cout << "creates" << endl;
_return.contractAddresses.insert(tx.creates);
DataItem localDataItem;
call_contract(&localDataItem, tx.creates, tx.fromAddress, tx.toAddress, tx.value);
//cout << "size before " << _return.dataItemMap[tx.creates].balances.size() << endl;
//_return.dataItemMap[tx.creates].value = localDataItem.value;
_return.dataItemMap[tx.creates] = localDataItem;
//_return.dataItemMap[tx.creates].balances = localDataItem.balances;
//_return.dataItemMap[tx.creates].allowed = localDataItem.allowed;
//_return.dataItemMap[tx.creates].votes = localDataItem.votes;
//_return.dataItemMap[tx.creates].balances = localDataItem.balances;
//cout << "size after " << _return.dataItemMap[tx.creates].balances.size() << endl;
//_return.dataItemMap[tx.creates].balances = localDataItem.balances;
txn_end = chrono::steady_clock::now();
contract_txn_time += chrono::duration_cast<chrono::microseconds>(txn_end - txn_start).count();
sc_cnt++;
} else {
bool flag = false;
std::set<string>::iterator it = std::find (_return.contractAddresses.begin(), _return.contractAddresses.end(), tx.toAddress);
if (it != _return.contractAddresses.end()) {
//cout << "contractAddress found in to" << endl;
DataItem localDataItem = _return.dataItemMap[tx.toAddress];
call_contract(&localDataItem, tx.toAddress, tx.fromAddress, tx.input, tx.value);
_return.dataItemMap[tx.toAddress] = localDataItem;
//_return.dataItemMap[tx.toAddress].value = localDataItem.value;
//strcpy(_return.dataItemMap[tx.toAddress].owner,localDataItem.owner);
//_return.dataItemMap[tx.toAddress].owner = localDataItem.owner;
//_return.dataItemMap[tx.toAddress].balances = localDataItem.balances;
//_return.dataItemMap[tx.toAddress].allowed = localDataItem.allowed;
//_return.dataItemMap[tx.toAddress].votes = localDataItem.votes;
//_return.dataItemMap[tx.toAddress].balances = localDataItem.balances;
txn_end = chrono::steady_clock::now();
contract_txn_time += chrono::duration_cast<chrono::microseconds>(txn_end - txn_start).count();
sc_cnt++;
flag = true;
}
it = std::find (_return.contractAddresses.begin(), _return.contractAddresses.end(), tx.fromAddress);
if (it != _return.contractAddresses.end() and !flag) {
//cout << "contractAddress found in from" << endl;
DataItem localDataItem = _return.dataItemMap[tx.fromAddress];
call_contract(&localDataItem, tx.fromAddress, tx.toAddress, tx.input, tx.value);
_return.dataItemMap[tx.fromAddress] = localDataItem;
//_return.dataItemMap[tx.fromAddress].value = localDataItem.value;
//_return.dataItemMap[tx.fromAddress].owner = localDataItem.owner;
//_return.dataItemMap[tx.fromAddress].balances = localDataItem.balances;
//_return.dataItemMap[tx.fromAddress].allowed = localDataItem.allowed;
//_return.dataItemMap[tx.fromAddress].votes = localDataItem.votes;
//_return.dataItemMap[tx.fromAddress].balances = localDataItem.balances;
txn_end = chrono::steady_clock::now();
contract_txn_time += chrono::duration_cast<chrono::microseconds>(txn_end - txn_start).count();
sc_cnt++;
flag = true;
}
if (!flag) {
//cout << "contractAddress not found" << endl;
if (_return.dataItemMap[tx.fromAddress].value >= tx.value) {
_return.dataItemMap[tx.fromAddress].value -= tx.value;
_return.dataItemMap[tx.toAddress].value += tx.value;
status = true;
} else {
//cout << "nsc txn " << _return.dataItemMap[tx.fromAddress].value << "\t" << tx.value << endl;
status = false;
}
txn_end = chrono::steady_clock::now();
normal_txn_time += chrono::duration_cast<chrono::microseconds>(txn_end - txn_start).count();
nsc_cnt++;
}
}
//cout << endl;
if (status) {
successful_transactions++;
} else {
failed_transactions++;
}
tx_fees += fee;
_return.transactionIDList.push_back(tx.transactionID);
total_transactions++;
}
cout << "WID " << WID << "\tsc " << sc_cnt << "\tnsc " << nsc_cnt << endl;
//cout << "_return.dataItemMap size " << _return.dataItemMap.size() << endl;
//ofstream scfile;
//scfile.open(dir_path+"sc_call.csv",std::ofstream::out | std::ofstream::trunc);
valcttfile << contract_txn_time << endl;
valnttfile << normal_txn_time << endl;
//_return.transactionFees = tx_fees;
//Logger::instance().log(MSG+" TransactionsList ends", Logger::kLogLevelInfo);
//Logger::instance().log(MSG+" worker "+ WID +" recvTransactions ends", Logger::kLogLevelInfo);
auto end = chrono::steady_clock::now();
}
void recvVal2Transactions( ::SharedService::WorkerResponse& _return, const std::vector< ::SharedService::Transaction> & TransactionsList, const std::map<std::string, ::SharedService::DataItem> & dataItemMap, const std::set<std::string> & contractAddresses) {
// Your implementation goes here
double normal_txn_time = 0;
double contract_txn_time = 0;
//Logger::instance().log(MSG+" worker "+ WID +" recvTransactions starts", Logger::kLogLevelInfo);
auto start = chrono::steady_clock::now();
int successful_transactions = 0;
int failed_transactions = 0;
int total_transactions = 0;
int sc_cnt = 0;
int nsc_cnt = 0;
double tx_fees = 0;
//Logger::instance().log(MSG+" dataItemMap starts", Logger::kLogLevelInfo);
for (auto const& address: dataItemMap)
{
_return.dataItemMap[address.first] = address.second;
//cout << "txn value at worker " << _return.dataItemMap[address.first].value << "\t" << address.second.value << endl;
}
//Logger::instance().log(MSG+" dataItemMap ends", Logger::kLogLevelInfo);
//Logger::instance().log(MSG+" contractAddresses starts", Logger::kLogLevelInfo);
//cout << "contractAddresses size: " << contractAddresses.size() << endl;
for (auto const& address: contractAddresses)
{
//cout << address << endl;
_return.contractAddresses.insert(address);
//cout << "txn value at worker " << _return.dataItemMap[address.first].value << "\t" << address.second.value << endl;
}
//Logger::instance().log(MSG+" contractAddresses ends", Logger::kLogLevelInfo);
//Logger::instance().log(MSG+" TransactionsList starts", Logger::kLogLevelInfo);
//cout << endl;
for (auto const& tx: TransactionsList) {
//cout << "tx" << tx.transactionID << "\t";
auto txn_start = chrono::steady_clock::now();
auto txn_end = chrono::steady_clock::now();
double fee;
bool status= false;
if (tx.toAddress == "creates") {
//contract_addresses.push_back(tx.creates);
//cout << "creates" << endl;
_return.contractAddresses.insert(tx.creates);
DataItem localDataItem;
call_contract(&localDataItem, tx.creates, tx.fromAddress, tx.toAddress, tx.value);
//cout << "size before " << _return.dataItemMap[tx.creates].balances.size() << endl;
//_return.dataItemMap[tx.creates].value = localDataItem.value;
_return.dataItemMap[tx.creates] = localDataItem;
//_return.dataItemMap[tx.creates].balances = localDataItem.balances;
//_return.dataItemMap[tx.creates].allowed = localDataItem.allowed;
//_return.dataItemMap[tx.creates].votes = localDataItem.votes;
//_return.dataItemMap[tx.creates].balances = localDataItem.balances;
//cout << "size after " << _return.dataItemMap[tx.creates].balances.size() << endl;
//_return.dataItemMap[tx.creates].balances = localDataItem.balances;
txn_end = chrono::steady_clock::now();
contract_txn_time += chrono::duration_cast<chrono::microseconds>(txn_end - txn_start).count();
sc_cnt++;
} else {
bool flag = false;
std::set<string>::iterator it = std::find (_return.contractAddresses.begin(), _return.contractAddresses.end(), tx.toAddress);
if (it != _return.contractAddresses.end()) {
//cout << "contractAddress found in to" << endl;
DataItem localDataItem = _return.dataItemMap[tx.toAddress];
call_contract(&localDataItem, tx.toAddress, tx.fromAddress, tx.input, tx.value);
_return.dataItemMap[tx.toAddress] = localDataItem;
//_return.dataItemMap[tx.toAddress].value = localDataItem.value;
//strcpy(_return.dataItemMap[tx.toAddress].owner,localDataItem.owner);
//_return.dataItemMap[tx.toAddress].owner = localDataItem.owner;
//_return.dataItemMap[tx.toAddress].balances = localDataItem.balances;
//_return.dataItemMap[tx.toAddress].allowed = localDataItem.allowed;
//_return.dataItemMap[tx.toAddress].votes = localDataItem.votes;
//_return.dataItemMap[tx.toAddress].balances = localDataItem.balances;
txn_end = chrono::steady_clock::now();
contract_txn_time += chrono::duration_cast<chrono::microseconds>(txn_end - txn_start).count();
sc_cnt++;
flag = true;
}
it = std::find (_return.contractAddresses.begin(), _return.contractAddresses.end(), tx.fromAddress);
if (it != _return.contractAddresses.end() and !flag) {
//cout << "contractAddress found in from" << endl;
DataItem localDataItem = _return.dataItemMap[tx.fromAddress];
call_contract(&localDataItem, tx.fromAddress, tx.toAddress, tx.input, tx.value);
_return.dataItemMap[tx.fromAddress] = localDataItem;
//_return.dataItemMap[tx.fromAddress].value = localDataItem.value;
//_return.dataItemMap[tx.fromAddress].owner = localDataItem.owner;
//_return.dataItemMap[tx.fromAddress].balances = localDataItem.balances;
//_return.dataItemMap[tx.fromAddress].allowed = localDataItem.allowed;
//_return.dataItemMap[tx.fromAddress].votes = localDataItem.votes;
//_return.dataItemMap[tx.fromAddress].balances = localDataItem.balances;
txn_end = chrono::steady_clock::now();
contract_txn_time += chrono::duration_cast<chrono::microseconds>(txn_end - txn_start).count();
sc_cnt++;
flag = true;
}
if (!flag) {
//cout << "contractAddress not found" << endl;
if (_return.dataItemMap[tx.fromAddress].value >= tx.value) {
_return.dataItemMap[tx.fromAddress].value -= tx.value;
_return.dataItemMap[tx.toAddress].value += tx.value;
status = true;
} else {
//cout << "nsc txn " << _return.dataItemMap[tx.fromAddress].value << "\t" << tx.value << endl;
status = false;
}
txn_end = chrono::steady_clock::now();
normal_txn_time += chrono::duration_cast<chrono::microseconds>(txn_end - txn_start).count();
nsc_cnt++;
}
}
//cout << endl;
if (status) {
successful_transactions++;
} else {
failed_transactions++;
}
tx_fees += fee;
_return.transactionIDList.push_back(tx.transactionID);
total_transactions++;
}
cout << "WID " << WID << "\tsc " << sc_cnt << "\tnsc " << nsc_cnt << endl;
//cout << "_return.dataItemMap size " << _return.dataItemMap.size() << endl;
//ofstream scfile;
//scfile.open(dir_path+"sc_call.csv",std::ofstream::out | std::ofstream::trunc);
val2cttfile << contract_txn_time << endl;
val2nttfile << normal_txn_time << endl;
//_return.transactionFees = tx_fees;
//Logger::instance().log(MSG+" TransactionsList ends", Logger::kLogLevelInfo);
//Logger::instance().log(MSG+" worker "+ WID +" recvTransactions ends", Logger::kLogLevelInfo);
auto end = chrono::steady_clock::now();
}
void mineBlock(const ::SharedService::Block& block, const int64_t nonce, const int16_t interval) {
// Your implementation goes here
//block.nonce = nonce;
cout << "worker mining " << WID << endl;
pthread_t thread;
//pthread_attr_t attr;
struct mine_data *td = NULL;
td = new(nothrow) struct mine_data; //(struct mine_data *)malloc(sizeof(struct mine_data));
//struct mine_data td;
if (!td) {
cout << "allocation failed" << endl;
} else {
td->block = block;
td->nonce = nonce;
td->interval = interval;
}
int rc;
//int thID = 0;
void *status;
cout << "creating thread" << endl;
rc = pthread_create(&thread, NULL, mine, (void *)td);
if (rc) {
cout << "WS create failed" << endl;
exit(-1);
} else {
cout << "mine thread created" << endl;
}
//cout << "Exiting" << endl;
//pthread_exit(NULL);
/*
Block newBlock = createBlock(block);
newBlock.nonce = nonce;
string hash;
while (true) {
string block_str = convert_block_to_string(newBlock);
hash = sha256(block_str);
cout << newBlock.nonce << ",";
if (hash.compare(difficulty) < 0) {
cout << newBlock.nonce << endl;
cout << block_str << endl;
cout << "Success W" << WID << " " << newBlock.nonce << " " << hash << endl;
break;
}
newBlock.nonce+=interval;
}
std::shared_ptr<TTransport> socket(new TSocket(masterIP, masterPort));
std::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
std::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
MasterServiceClient masterClient(protocol);
Logger::instance().log(MSG+" Block "+to_string(block.number) +" connection to master "+WID+" starts", Logger::kLogLevelInfo);
transport->open();
Logger::instance().log(MSG+" Block "+to_string(block.number) +" worker "+WID+" mineBlock() starts", Logger::kLogLevelInfo);
sent transaction to execute
masterClient.recvMiningStatus(newBlock.nonce, newBlock.number); // returns local worker response
cout << worker->threadID << ":" << sendTransactionMap[worker->workerID].size() << "\t";
Logger::instance().log(MSG+" Block "+to_string(block.number) +" WorkerID "+WID+" mineBlock() ends", Logger::kLogLevelInfo);
transport->close();
Logger::instance().log(MSG+" Block "+to_string(block.number) +" connection to master "+WID+" ends", Logger::kLogLevelInfo);
//_return.number = newBlock.number;
//_return.nonce = newBlock.nonce;
*/
//printf("mineBlock\n");
}
};
int main(int argc, char **argv) {
int port = atoi(argv[1]);
WID = argv[2];
dir_path = argv[3];
::apache::thrift::stdcxx::shared_ptr<WorkerServiceHandler> handler(new WorkerServiceHandler());
::apache::thrift::stdcxx::shared_ptr<TProcessor> processor(new WorkerServiceProcessor(handler));
::apache::thrift::stdcxx::shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
::apache::thrift::stdcxx::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
::apache::thrift::stdcxx::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(5);
shared_ptr<PosixThreadFactory> threadFactory = shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
threadManager->threadFactory(threadFactory);
threadManager->start();
TThreadPoolServer server(processor, serverTransport, transportFactory, protocolFactory, threadManager);
server.serve();
return 0;
}