forked from ripple/simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimRun.cpp
194 lines (165 loc) · 6.8 KB
/
SimRun.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
//------------------------------------------------------------------------------
/*
This file is part of consensus-sim
Copyright (c) 2013, Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
#include <random>
#include <jsoncpp/json/json.h>
#include "src/Event.h"
#include "src/Link.h"
#include "src/Message.h"
#include "src/Network.h"
#include "src/Node.h"
#include "src/NodeState.h"
#define CONSENSUS_PERCENT 80
// Latencies in milliseconds
// E2C - End to core, the latency from a node to a nearby node
// C2C - Core to core, the additional latency when nodes are far
int main(int argc, char* argv[]) {
std::string fileName;
if (argc == 2) {
fileName = argv[1];
} else {
std::cerr << "usage: sim input_file" << std::endl;
return EXIT_FAILURE;
}
// Read parameters and network from json file
std::cout << "Loading network from file: " << fileName << std::endl;
std::ifstream file(fileName.c_str());
Json::Reader reader;
Json::Value json;
if (!reader.parse(file, json)) {
std::cerr << "Error: " << reader.getFormattedErrorMessages() << std::endl;
return EXIT_FAILURE;
}
int countParams = 0;
for (auto const& id : json.getMemberNames()) {
if (id == "numNodes" || id == "unlThresh" || id == "nodes" || id == "links") {
countParams++;
}
}
if (countParams != 4) {
std::cerr << "Json data: 'numNodes', 'unlThresh', 'nodes' or 'links' not found. Exiting..." << std::endl;
return EXIT_FAILURE;
}
int numNodes = json["numNodes"].asInt();
std::cout << "Reading NUM_NODES = " << numNodes << std::endl;
int unlThresh = json["unlThresh"].asInt();
std::cout << "Reading UNL_THRESH = " << unlThresh << std::endl;
// Create nodes
std::cout << "Creating nodes" << std::endl;
Node* nodes[numNodes];
const Json::Value& networkNodes = json["nodes"];
for (const auto& element : networkNodes) {
// NodeIds must be from 0 until numNodes - 1
int nodeId = element["nodeId"].asInt();
int vote = element["vote"].asInt();
int latency = element["latency"].asInt();
nodes[nodeId] = new Node(nodeId, numNodes, latency);
nodes[nodeId]->getNodeStates()[nodeId] = vote;
nodes[nodeId]->getNodeTimeStamps()[nodeId] = 1;
nodes[nodeId]->setVote(vote);
// Build our UNL
const Json::Value& uniqueNodeList = element["uniqueNodeList"];
for (const auto& unlNode : uniqueNodeList) {
nodes[nodeId]->getUniqueNodeList().push_back(unlNode.asInt());
}
}
// Create links
std::cout << "Creating links" << std::endl;
const Json::Value& networkLinks = json["links"];
for (const auto& link : networkLinks) {
int i = link["from"].asInt();
int lt = link["to"].asInt();
int latency = link["latency"].asInt();
int ll = nodes[i]->getLatency() + nodes[lt]->getLatency() + latency;
nodes[i]->getLinks().push_back(Link(lt, ll));
nodes[lt]->getLinks().push_back(Link(i, ll));
}
Network network;
// Trigger all nodes to make initial broadcasts of their own positions
std::cout << "Creating initial messages" << std::endl;
for (const auto& node : nodes) {
for (auto& link : node->getLinks()) {
Message message(node->getNodeId(), link.getToNodeId());
message.insertData(node->getNodeId(), node->getNodeStates()[node->getNodeId()]);
network.sendMessage(message, link, 0);
}
}
std::cout << "Created " << network.countMessagesOnTheWire() << " events" << std::endl;
// Run simulation
std::cout << " Time (ms)\t Positive\t Negative" << std::endl
<< " ---------\t --------\t --------" << std::endl;
do {
int nodesPositive = 0;
int nodesNegative = 0;
// Count nodes and check convergence
for (const auto& node : nodes) {
if (node->getVote() > 0) {
nodesPositive++;
} else if (node->getVote() < 0) {
nodesNegative++;
}
}
if (nodesPositive > (numNodes * CONSENSUS_PERCENT / 100)) {
break;
}
if (nodesNegative > (numNodes * CONSENSUS_PERCENT / 100)) {
break;
}
if (network.getMessages().empty()) {
std::cerr << "Fatal: Radio Silence. Exiting..." << std::endl;
return EXIT_FAILURE;
}
EventMap::iterator event = network.getMessages().begin();
if ((event->first / 100) > (network.getMasterTime() / 100)) {
std::cout << "\t" << event->first << ";\t\t" << nodesPositive << ";\t\t"
<< nodesNegative << std::endl;
}
network.setMasterTime(event->first);
for (const Message& message : event->second.getMessages()) {
if (message.hasEmptyData()) {
// Message was never sent
nodes[message.getFromNodeId()]->decreaseMessagesSent();
} else {
nodes[message.getToNodeId()]->receiveMessage(message, network, unlThresh);
}
}
network.getMessages().erase(event);
} while (true);
int nodesPositive = 0;
int nodesNegative = 0;
// Count nodes and check convergence
for (const auto& node : nodes) {
if (node->getVote() > 0) {
nodesPositive++;
} else if (node->getVote() < 0) {
nodesNegative++;
}
}
std::cout << "\t" << network.getMasterTime() << ";\t\t" << nodesPositive << ";\t\t" << nodesNegative << std::endl;
std::cout << "Consensus reached in " << network.getMasterTime() << " ms with "
<< network.countMessagesOnTheWire() << " messages on the wire" << std::endl;
// Output result
long totalMsgsSent = 0;
for (const auto& node : nodes) {
totalMsgsSent += node->getMessagesSent();
}
std::cout << "The average node sent " << totalMsgsSent / numNodes << " messages" << std::endl;
return EXIT_SUCCESS;
}