-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolution.cpp
118 lines (111 loc) · 3.25 KB
/
solution.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
#include <iostream>
#include <bits/stdc++.h>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
set<string> included_tx_id;
string fileName = "mempool.csv";
double maxweight = 4000000.0;
class Transaction
{
public:
string tx_id;
vector<string> parents;
int fee;
int weight;
};
pair<string, Transaction *> createTransaction(vector<string> &row)
{
auto ans = new Transaction();
ans->tx_id = row[0];
ans->fee = stoi(row[1]);
ans->weight = stoi(row[2]);
vector<string> p;
for (int i = 3; i < row.size(); i++)
{
p.push_back(row[i]);
}
ans->parents = p;
return {row[0], ans};
}
void readCSV(string fileName, unordered_map<string, Transaction *> &umap)
{
ifstream fin(fileName);
vector<string> row;
string temp, line, word;
getline(fin, line);
while (getline(fin, line))
{
row.clear();
stringstream s(line);
while (getline(s, word, ','))
{
row.push_back(word);
}
pair<string, Transaction *> p = createTransaction(row);
umap[p.first] = p.second;
}
fin.close();
cout << "Total number of transactions read: " << umap.size() << endl;
}
bool isValidTx(Transaction *tx, set<string> &included_tx_set)
{
for (auto parent : tx->parents)
{
if (included_tx_set.find(parent) == included_tx_set.end())
return false;
}
return true;
}
void writeOutput(vector<string> &included_tx_vector, string fn)
{
ofstream myfile(fn);
for (auto s : included_tx_vector)
myfile << s << endl;
myfile.close();
}
int main()
{
unordered_map<string, Transaction *> umap;
readCSV(fileName, umap);
set<pair<float, Transaction *>, greater<pair<float, Transaction *>>> tx_set;
set<string> included_tx_set;
vector<string> included_tx_vector;
for (auto p : umap)
{
tx_set.insert({(float)p.second->fee / (float)p.second->weight, p.second});
}
double currBlockWeight = 0.0;
int totalFee = 0;
while (!tx_set.empty() && currBlockWeight < maxweight)
{
bool found = false;
for (auto itr = tx_set.begin(); itr != tx_set.end(); itr++)
{
Transaction *curr_tx = (*itr).second;
int currFee = curr_tx->fee;
int currWeight = curr_tx->weight;
if (isValidTx(curr_tx, included_tx_set) && (currBlockWeight + currWeight) <= maxweight)
{
currBlockWeight += currWeight;
included_tx_set.insert(curr_tx->tx_id);
included_tx_vector.push_back(curr_tx->tx_id);
totalFee = totalFee+currFee;
tx_set.erase(itr);
found = true;
break;
}
}
if (!found)
break;
}
cout << "Number of tx in final block " << included_tx_set.size() << "\n";
cout << "Total fee in curr block : " << totalFee<<"\n";
cout << "Total weight : " << currBlockWeight<<"\n";
double percentage=(currBlockWeight/maxweight)*100.0;
cout<<"Percentage of weight: "<<percentage<<" %"<<"\n";
cout<<"\n";
writeOutput(included_tx_vector, "block.txt");
}