Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ a.out
./logs
logs
**/.DS_Store
build
build
/.vscode
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ add_executable(simulation
models/BlockchainManagement/GlobalOrchestration/Bitcoin.cpp
models/BlockchainManagement/GlobalOrchestration/GlobalOrchestration.cpp
models/LocalProtocols/BitcoinMiner.cpp
models/LocalProtocols/DummyProtocol.cpp
models/LocalProtocols/Protocol.cpp
models/Networking/BitcoinRoutingTable.cpp
models/Networking/FlexibleRoutingTable.cpp
Expand Down Expand Up @@ -115,6 +116,7 @@ add_executable(test
models/BlockchainManagement/GlobalOrchestration/Bitcoin.cpp
models/BlockchainManagement/GlobalOrchestration/GlobalOrchestration.cpp
models/LocalProtocols/BitcoinMiner.cpp
models/LocalProtocols/DummyProtocol.cpp
models/LocalProtocols/Protocol.cpp
models/Networking/BitcoinRoutingTable.cpp
models/Networking/FlexibleRoutingTable.cpp
Expand Down
10 changes: 8 additions & 2 deletions config/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

const int NUM_REGIONS = 6;

std::string REGIONS[NUM_REGIONS] = {"ASIA", "JAPAN", "AUSTRALIA", "NORTH_AMERICA", "SOUTH_AMERICA", "EUROPE"};
const std::string REGIONS[NUM_REGIONS] = {"ASIA", "JAPAN", "AUSTRALIA", "NORTH_AMERICA", "SOUTH_AMERICA", "EUROPE"};

const double REGION_DISTRIBUTION_OF_NODES[NUM_REGIONS] = {0.1177, 0.0224, 0.0195, 0.3316, 0.009, 0.4998};

Expand All @@ -18,10 +18,16 @@ const long long INTER_REGION_LATENCY[NUM_REGIONS][NUM_REGIONS] = {
{1, 1, 1, 1, 1, 1}
};

const int NUM_NODES = 15;
const int NUM_NODES = 14;

const int NUM_CLUSTERS = 3;

const int CLUSTER_SIZE = 3;

const std::string CONSENSUS_TYPE = "PoW";

const int NUM_MINERS = 5;

const int BLOCK_TIME = 600;

#endif /*CONFIG_H_*/
10 changes: 7 additions & 3 deletions core/EventManagement/Event/EventTypes/MessageToNodeEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "../../../Networking/Subnet.h"
#include "../../../Networking/NetworkLayer/NetworkLayer.h"
#include "../../../../models/LocalProtocols/BitcoinMiner.h"
#include "../../../../models/LocalProtocols/Protocol.h"


MessageToNodeEvent::MessageToNodeEvent(std::shared_ptr<Message> _message, int _forNodeId, int _fromNodeId, long long _durationInTicks)
: message(_message), forNodeId(_forNodeId), fromNodeId(_fromNodeId), Event(_durationInTicks, EventType::MESSAGE_TO_NODE) {}
Expand All @@ -18,22 +20,24 @@ bool MessageToNodeEvent::execute(Network& _network, std::shared_ptr<BlockCache>
<< "[From:" << std::setw(8) << std::right << std::to_string(fromNodeId) << "]"
<< "[To:" << std::setw(8) << std::right << std::to_string(forNodeId) << "]"
<< "[Type:" << std::setw(15) << std::right << message->getType() << "]";

switch(message->getMessageType()) {
case MessageType::NEW_BLOCK_ID: {
// std::cout << 1 << std::endl;
auto node = _network.getNodes()[forNodeId];
std::static_pointer_cast<BitcoinMiner>(node->getProtocols()[0])->onNewBlockIdMessage(std::dynamic_pointer_cast<NewBlockIdMessage>(message), _blockCache, _newEvents);
std::static_pointer_cast<Protocol>(node->getProtocols()[0])->onNewBlockIdMessage(std::dynamic_pointer_cast<NewBlockIdMessage>(message), _blockCache, _newEvents);
break;
}
case MessageType::NEW_BLOCK_MINED: {
std::cout << 2 << std::endl;
auto node = _network.getNodes()[forNodeId];
std::static_pointer_cast<BitcoinMiner>(node->getProtocols()[0])->onNewBlockMinedMessage(std::dynamic_pointer_cast<NewBlockMinedMessage>(message), _blockCache, _newEvents, _currentTick);
std::static_pointer_cast<Protocol>(node->getProtocols()[0])->onNewBlockMinedMessage(std::dynamic_pointer_cast<NewBlockMinedMessage>(message), _blockCache, _newEvents, _currentTick);
break;
}
case MessageType::NEW_BLOCK_BODY: {
break;
}
case MessageType::SUBNET_MSG: {
std::cout << 3 << std::endl;
_subnet->onSubnetMessage(std::static_pointer_cast<SubnetMessage>(message), _currentTick, _newEvents);
break;
}
Expand Down
23 changes: 16 additions & 7 deletions core/Network/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ double Network::getTotalLambda() const {

double Network::recaculateTotalLambda() {
double sum = 0.0;

if (nodes.size() != 0 && nodes[0] != nullptr) { // TODO: make getLambda a function of Node if required for other subclasses as well
for(std::shared_ptr<Node> node: nodes) {
auto n = std::static_pointer_cast<BitcoinMiner>(node->getProtocols()[0]);
if(node->getProtocols().empty()) {
continue;
}
auto n = std::static_pointer_cast<Protocol>(node->getProtocols()[0]);
sum += n->getLambda();
}
}
Expand All @@ -51,12 +53,15 @@ std::vector<double> Network::recaculateCumulativeLambdaVector() {

if (nodes.size() != 0 && nodes[0] != nullptr) { // TODO: make getLambda a function of Node if required for other subclasses as well
for(int i=0; i<nodes.size(); i++) {
auto miner = std::static_pointer_cast<BitcoinMiner>(nodes[i]->getProtocols()[0]);

if(i==0)
if(nodes[i]->getProtocols().empty()) {
continue;
}
auto miner = std::static_pointer_cast<Protocol>(nodes[i]->getProtocols()[0]);
// LOG(INFO) << "debug here";
if(v.empty())
v.push_back(miner->getLambda());
else
v.push_back(v[i-1] + miner->getLambda());
v.push_back(v.back() + miner->getLambda());

// LOG(DEBUG) << "[" << std::setw(35) << std::left << "BitcoinModel::getNextBlockProducer]"
// << "[NodeId: " << i << ", CumulativeHash: " << hashpowerFraction[i] << "]";
Expand All @@ -76,11 +81,15 @@ std::shared_ptr<Node> Network::pickLambdaWeightedNode() {
std::shared_ptr<Node> node;

double randomNumber = getRandomDouble() * totalLambda;
std::cout << randomNumber << ' ' << totalLambda << ' ' << std::endl;
for(double x: cumulativeLambdaVector) {
std::cout << x << ' '<< std::endl;
}

auto it = std::upper_bound(cumulativeLambdaVector.begin(),
cumulativeLambdaVector.end(), randomNumber);

int index = it - cumulativeLambdaVector.begin();

std::cout << nodes.size() << ' ' << index << std::endl;
return nodes[index];
}
8 changes: 6 additions & 2 deletions core/Network/Node/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#include "../../../models/Networking/BitcoinRoutingTable.h"
#include "../../../models/Networking/FlexibleRoutingTable.h"

Node::Node(int _nodeId, bool _isAlive, int _region, std::shared_ptr<BlockCache> _blockCache,
Node::Node(int _nodeId, bool _isAlive, int _region, int _cityIdx, std::shared_ptr<BlockCache> _blockCache,
std::shared_ptr<Subnet> _subnet, std::string _routingTable)
: nodeId(_nodeId), isAlive(_isAlive), region(_region), blockCache(_blockCache),
: nodeId(_nodeId), isAlive(_isAlive), region(_region), cityIdx(_cityIdx), blockCache(_blockCache),
networkLayer(std::make_shared<NetworkLayer>(NetworkLayer(5000, 5000, _subnet, _nodeId))),
blockchain(std::make_shared<Blockchain>(Blockchain(std::const_pointer_cast<const BlockCache>(_blockCache))))
{
Expand Down Expand Up @@ -51,6 +51,10 @@ int Node::getNodeId() const {
return nodeId;
}

int Node::getCityIdx() {
return cityIdx;
}

std::string Node::getCountryCode() {
return countryCode;
}
Expand Down
4 changes: 3 additions & 1 deletion core/Network/Node/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Node {
NodeId nodeId;
bool isAlive;
int region;
int cityIdx;
std::string countryCode;
std::shared_ptr<RoutingTable> routingTable;
std::vector<std::shared_ptr<Protocol>> protocols;
Expand All @@ -31,12 +32,13 @@ class Node {
std::shared_ptr<NetworkLayer> networkLayer;

public:
Node(int _nodeId, bool _isAlive, int _region, std::shared_ptr<BlockCache> _blockCache,
Node(int _nodeId, bool _isAlive, int _region, int _cityIdx, std::shared_ptr<BlockCache> _blockCache,
std::shared_ptr<Subnet> _subnet, std::string _routingTable);
Node(int _nodeId, double _maxDownBandwidth, double _upDownBandwidth, std::shared_ptr<Subnet> _subnet,
std::string _countryCode, std::string _routingTable);
int getRegion() const;
int getNodeId() const;
int getCityIdx();
std::string getCountryCode();
std::shared_ptr<Blockchain> getBlockchain();
std::shared_ptr<BlockCache> getBlockCache();
Expand Down
16 changes: 14 additions & 2 deletions core/Networking/LatencyModels/City.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
#include "City.h"

City::City() : code("-1"), name(""), country("") {};
City::City() : code(-1), name(""), country("") {};

City::City(std::string _code, std::string _name, std::string _country) :
City::City(int _code, std::string _name, std::string _country) :
code(_code), name(_name), country(_country) {};

City::City(int _code, std::string _name) :
code(_code), name(_name) {};



void City::setCountry(std::string _country) {
country = _country;
}

void City::setRegion(std::string _region) {
region = _region;
}

std::string City::getName() {
return name;
}

std::string City::getCountry() {
return country;
}

std::string City::getRegion() {
return region;
}
10 changes: 7 additions & 3 deletions core/Networking/LatencyModels/City.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@

class City {
private:
std::string code;
int code;
std::string name;
std::string country;
std::string country; // not yet set.
std::string region;

public:
City();
City(std::string _code, std::string _name, std::string _country);
City(int _code, std::string _name, std::string _country);
City(int _code, std::string _name);
void setCountry(std::string _country);
void setRegion(std::string _region);
std::string getName();
std::string getCountry();
std::string getRegion();
};

#endif /* CORE_NETWORKING_LATENCYMODELS_CITY_H_ */
24 changes: 10 additions & 14 deletions core/Networking/LatencyModels/GnpLatencyModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ int GnpLatencyModel::MSS;

GnpLatencyModel::GnpLatencyModel(Network& _network) : network(_network) {
MSS = IPv4Message::MTU_SIZE - IPv4Message::HEADER_SIZE - TCPMessage::HEADER_SIZE;
wonderNetwork = wonderNetwork->getInstance();
}

//void GnpLatencyModel::setPingER(PingER& _pingER) {
// pingER = _pingER;
//}

PingER& GnpLatencyModel::getPingER() {
return pingER;
}
// PingER& GnpLatencyModel::getPingER() {
// return pingER;
// }

double GnpLatencyModel::getMinRTT(NodeId senderId, NodeId receiverId) {
std::shared_ptr<Node> sender = network.getNode(senderId);
std::shared_ptr<Node> receiver = network.getNode(receiverId);

double rtt = 0;

rtt = pingER.getLinkMetrics(sender->getCountryCode(), receiver->getCountryCode()).getMinRTT();
rtt = wonderNetwork->getLinkMetricsBetweenCities(sender->getCityIdx(), receiver->getCityIdx()).getMinRTT();

return rtt;
}
Expand All @@ -39,7 +39,7 @@ double GnpLatencyModel::getPacketLossProbability(NodeId senderId, NodeId receive

double oneWayLossRate = 0;

double twoWayLossRate = pingER.getLinkMetrics(sender->getCountryCode(), receiver->getCountryCode()).getPacketLoss();
double twoWayLossRate = wonderNetwork->getLinkMetricsBetweenCities(sender->getCityIdx(), receiver->getCityIdx()).getPacketLoss();
twoWayLossRate /= 100;
oneWayLossRate = 1 - sqrt(1 - twoWayLossRate);

Expand All @@ -57,8 +57,8 @@ double GnpLatencyModel::getAvgJitter(NodeId senderId, NodeId receiverId) {
std::shared_ptr<Node> sender = network.getNode(senderId);
std::shared_ptr<Node> receiver = network.getNode(receiverId);

double minRTT = pingER.getLinkMetrics(sender->getCountryCode(), receiver->getCountryCode()).getMinRTT();
double avgRTT = pingER.getLinkMetrics(sender->getCountryCode(), receiver->getCountryCode()).getAvgRTT();
double minRTT = wonderNetwork->getLinkMetricsBetweenCities(sender->getCityIdx(), receiver->getCityIdx()).getMinRTT();
double avgRTT = wonderNetwork->getLinkMetricsBetweenCities(sender->getCityIdx(), receiver->getCityIdx()).getAvgRTT();

return avgRTT - minRTT;
}
Expand All @@ -83,12 +83,8 @@ uint64_t GnpLatencyModel::getPropagationDelay(NodeId senderId, NodeId receiverId
std::shared_ptr<Node> sender = network.getNode(senderId);
std::shared_ptr<Node> receiver = network.getNode(receiverId);

auto rtt = pingER.getLinkMetrics(sender->getCountryCode(), receiver->getCountryCode()).getAvgRTT();

if(rtt != 0)
return rtt / 2;

return pingER.getLinkMetrics(receiver->getCountryCode(), sender->getCountryCode()).getAvgRTT() / 2;
auto rtt = wonderNetwork->getLinkMetricsBetweenCities(sender->getCityIdx(), receiver->getCityIdx()).getAvgRTT();
return rtt / 2;
}

uint64_t GnpLatencyModel::getTCPThroughput(NodeId senderId, NodeId receiverId, bool _useJitter) {
Expand Down
4 changes: 1 addition & 3 deletions core/Networking/LatencyModels/GnpLatencyModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@ class GnpLatencyModel : public LatencyModel {
private:
static int MSS;
Network& network;
PingER pingER;
// WonderNetwork wonderNetwork;
WonderNetwork *wonderNetwork;

public:
GnpLatencyModel(Network& _network);
// void setPingER(PingER& _pingER);
PingER& getPingER();
double getMinRTT(NodeId sender, NodeId receiver);
double getPacketLossProbability(NodeId senderId, NodeId receiverId);
Expand Down
Loading