forked from ariel-waisburg/p2p-network-simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.cpp
98 lines (88 loc) · 1.92 KB
/
models.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
#ifndef MODELS_HPP
#define MODELS_HPP
#include <vector>
#include <map>
#include <set>
#include <queue>
using namespace std;
// Class representing a transaction
class Txn
{
public:
int txn_id;
int sender_id;
int receiver_id;
int amount;
int sender_bal;
bool coinbase = false;
};
// Class representing a block in the blockchain
class Block
{
public:
int blk_id; // 0 to ... (0 being for genesis)
vector<Txn> txn_tree;
int crt_time;
};
// Enum defining different types of tasks
enum task_type
{
blk_crt, // Create block task
blk_rcv, // Recieve block task
txn_crt, // Transaction create task
txn_rcv, // Transaction recieved task
};
// Class representing a task to be performed by a node
class Task
{
public:
task_type type;
int trigger_time;
vector<Block> blockchain;
Txn txn;
};
// Functor class to compare tasks for priority queue
class Compare
{
public:
bool operator()(Task below, Task above)
{
if (below.trigger_time > above.trigger_time)
{
return true;
}
else if (below.trigger_time == above.trigger_time)
{
return compareType(below.type, above.type); // Comparing type of tasks for race cases.
}
else
{
return false;
}
}
bool compareType(task_type below, task_type above)
{
if (below == blk_crt && above == blk_rcv)
return true;
if (below == txn_rcv && above == txn_crt)
return true;
return false;
}
};
// Class representing a node in the network
class Node
{
public:
int peer_id; // 0 to ...
int cpu = 0; // 0 or 1
int speed = 0; // 0 or 1
int amnt;
double hashPower;
bool blk_crt_pending = false;
vector<int> peer_nbh;
set<int> knownTxns;
queue<Txn> validatedTxns;
priority_queue<Task, vector<Task>, Compare> tasks;
vector<Block> blockchain;
};
#endif