churnNodes = makeRandomList(CHURN_NODE_RATE);
- for (int id = 1; id <= numNodes; id++) {
+ // Selfish Miner --> Node ID 1
+ //Others Start From 2
+ selfishNode = new SelfishNode(
+ 1, degreeList.get(0) + 1, regionList.get(0), getSelfishMiningPower(), TABLE,
+ ALGO, useCBRNodes.get(0), churnNodes.get(0)
+ );
+ addNode(selfishNode);
+
+ for (int id = 2; id <= numNodes; id++) {
// Each node gets assigned a region, its degree, mining power, routing table and
// consensus algorithm
- Node node = new Node(
- id, degreeList.get(id - 1) + 1, regionList.get(id - 1), genMiningPower(), TABLE,
+ Node node = new HonestNode(
+ id, degreeList.get(id - 1) + 1, regionList.get(id - 1), getHonestMiningPower(), TABLE,
ALGO, useCBRNodes.get(id - 1), churnNodes.get(id - 1)
);
// Add the node to the list of simulated nodes
addNode(node);
-
- OUT_JSON_FILE.print("{");
- OUT_JSON_FILE.print("\"kind\":\"add-node\",");
- OUT_JSON_FILE.print("\"content\":{");
- OUT_JSON_FILE.print("\"timestamp\":0,");
- OUT_JSON_FILE.print("\"node-id\":" + id + ",");
- OUT_JSON_FILE.print("\"region-id\":" + regionList.get(id - 1));
- OUT_JSON_FILE.print("}");
- OUT_JSON_FILE.print("},");
- OUT_JSON_FILE.flush();
-
}
// Link newly generated nodes
@@ -369,33 +263,26 @@ public static void constructNetworkWithAllNodes(int numNodes) {
getSimulatedNodes().get(0).genesisBlock();
}
- /**
- * Network information when block height is blockHeight, in format:
- *
- * nodeID_1, nodeID_2
- *
- *
meaning there is a connection from nodeID_1 to right nodeID_1.
- *
- * @param blockHeight the index of the graph and the current block height
- */
- //TODO use logger
- public static void writeGraph(int blockHeight) {
- try {
- FileWriter fw = new FileWriter(
- new File(OUT_FILE_URI.resolve("./graph/" + blockHeight + ".txt")), false);
- PrintWriter pw = new PrintWriter(new BufferedWriter(fw));
-
- for (int index = 1; index <= getSimulatedNodes().size(); index++) {
- Node node = getSimulatedNodes().get(index - 1);
- for (int i = 0; i < node.getNeighbors().size(); i++) {
- Node neighbor = node.getNeighbors().get(i);
- pw.println(node.getNodeID() + " " + neighbor.getNodeID());
+ public static void startSimulation(){
+
+ for(int i=0; i < Iteration_Number; i++){
+ // Do message passing task!
+ Task currentTask = getTask();
+ while(currentTask != null){
+ if (currentTask instanceof AbstractMintingTask) {
+ AbstractMintingTask task = (AbstractMintingTask) getTask();
}
+ runTask();
+ currentTask = getTask();
}
- pw.close();
- } catch (IOException ex) {
- ex.printStackTrace();
+ float generatedRandom = (float)Math.random();
+ if(generatedRandom < SELFISH_MINER_ALPHA){
+ selfishNode.selfishMinting();
+ }
+ else{
+ getSimulatedNodes().get(1).minting();
+ }
}
}
diff --git a/simulator/src/main/java/simblock/simulator/Network.java b/simulator/src/main/java/simblock/simulator/Network.java
index 0d908635..a79f47f4 100644
--- a/simulator/src/main/java/simblock/simulator/Network.java
+++ b/simulator/src/main/java/simblock/simulator/Network.java
@@ -22,7 +22,6 @@
import static simblock.settings.NetworkConfiguration.REGION_DISTRIBUTION;
import static simblock.settings.NetworkConfiguration.REGION_LIST;
import static simblock.settings.NetworkConfiguration.UPLOAD_BANDWIDTH;
-import static simblock.simulator.Main.STATIC_JSON_FILE;
import static simblock.simulator.Main.random;
import java.util.List;
@@ -91,27 +90,4 @@ public static double[] getDegreeDistribution() {
return DEGREE_DISTRIBUTION;
}
- /**
- * Prints the currently active regions to outfile.
- */
- //TODO
- public static void printRegion() {
- STATIC_JSON_FILE.print("{\"region\":[");
-
- int id = 0;
- for (; id < REGION_LIST.size() - 1; id++) {
- STATIC_JSON_FILE.print("{");
- STATIC_JSON_FILE.print("\"id\":" + id + ",");
- STATIC_JSON_FILE.print("\"name\":\"" + REGION_LIST.get(id) + "\"");
- STATIC_JSON_FILE.print("},");
- }
-
- STATIC_JSON_FILE.print("{");
- STATIC_JSON_FILE.print("\"id\":" + id + ",");
- STATIC_JSON_FILE.print("\"name\":\"" + REGION_LIST.get(id) + "\"");
- STATIC_JSON_FILE.print("}");
- STATIC_JSON_FILE.print("]}");
- STATIC_JSON_FILE.flush();
- STATIC_JSON_FILE.close();
- }
}
diff --git a/simulator/src/main/java/simblock/simulator/Simulator.java b/simulator/src/main/java/simblock/simulator/Simulator.java
index 1ab2016d..8ef5a470 100644
--- a/simulator/src/main/java/simblock/simulator/Simulator.java
+++ b/simulator/src/main/java/simblock/simulator/Simulator.java
@@ -18,9 +18,8 @@
import static simblock.simulator.Timer.getCurrentTime;
-import java.util.ArrayList;
-import java.util.LinkedHashMap;
-import java.util.Map;
+import java.util.*;
+
import simblock.block.Block;
import simblock.node.Node;
@@ -107,6 +106,7 @@ public static void addNodeWithConnection(Node node) {
* A list of observed {@link Block} instances.
*/
private static final ArrayList observedBlocks = new ArrayList<>();
+ private static final Set mainChain = new LinkedHashSet<>();
/**
* A list of observed block propagation times. The map key represents the id of the node that
@@ -140,7 +140,7 @@ public static void arriveBlock(Block block, Node node) {
//TODO move magic number to constant
if (observedBlocks.size() > 10) {
// After the observed blocks limit is reached, log and remove old blocks by FIFO principle
- printPropagation(observedBlocks.get(0), observedPropagations.get(0));
+ //printPropagation(observedBlocks.get(0), observedPropagations.get(0));
observedBlocks.remove(0);
observedPropagations.remove(0);
}
@@ -149,6 +149,7 @@ public static void arriveBlock(Block block, Node node) {
propagation.put(node.getNodeID(), getCurrentTime() - block.getTime());
// Record the block as seen
observedBlocks.add(block);
+ mainChain.add(block.getHeight());
// Record the propagation time
observedPropagations.add(propagation);
}
@@ -186,4 +187,8 @@ public static void printAllPropagation() {
printPropagation(observedBlocks.get(i), observedPropagations.get(i));
}
}
+
+ public static int getMainChainSize(){
+ return mainChain.size();
+ }
}
diff --git a/simulator/src/main/java/simblock/task/BlockMessageTask.java b/simulator/src/main/java/simblock/task/BlockMessageTask.java
index 964691b6..ff586bc0 100644
--- a/simulator/src/main/java/simblock/task/BlockMessageTask.java
+++ b/simulator/src/main/java/simblock/task/BlockMessageTask.java
@@ -16,7 +16,6 @@
package simblock.task;
-import static simblock.simulator.Main.OUT_JSON_FILE;
import static simblock.simulator.Network.getLatency;
import static simblock.simulator.Timer.getCurrentTime;
@@ -65,19 +64,6 @@ public long getInterval() {
public void run() {
this.getFrom().sendNextBlockMessage();
-
- OUT_JSON_FILE.print("{");
- OUT_JSON_FILE.print("\"kind\":\"flow-block\",");
- OUT_JSON_FILE.print("\"content\":{");
- OUT_JSON_FILE.print("\"transmission-timestamp\":" + (getCurrentTime() - this.interval) + ",");
- OUT_JSON_FILE.print("\"reception-timestamp\":" + getCurrentTime() + ",");
- OUT_JSON_FILE.print("\"begin-node-id\":" + getFrom().getNodeID() + ",");
- OUT_JSON_FILE.print("\"end-node-id\":" + getTo().getNodeID() + ",");
- OUT_JSON_FILE.print("\"block-id\":" + block.getId());
- OUT_JSON_FILE.print("}");
- OUT_JSON_FILE.print("},");
- OUT_JSON_FILE.flush();
-
super.run();
}
diff --git a/simulator/src/main/java/simblock/task/CmpctBlockMessageTask.java b/simulator/src/main/java/simblock/task/CmpctBlockMessageTask.java
index fb08b62b..c31f0f7d 100644
--- a/simulator/src/main/java/simblock/task/CmpctBlockMessageTask.java
+++ b/simulator/src/main/java/simblock/task/CmpctBlockMessageTask.java
@@ -16,7 +16,6 @@
package simblock.task;
-import static simblock.simulator.Main.OUT_JSON_FILE;
import static simblock.simulator.Network.getLatency;
import static simblock.simulator.Timer.getCurrentTime;
@@ -65,19 +64,6 @@ public long getInterval() {
public void run() {
this.getFrom().sendNextBlockMessage();
-
- OUT_JSON_FILE.print("{");
- OUT_JSON_FILE.print("\"kind\":\"flow-block\",");
- OUT_JSON_FILE.print("\"content\":{");
- OUT_JSON_FILE.print("\"transmission-timestamp\":" + (getCurrentTime() - this.interval) + ",");
- OUT_JSON_FILE.print("\"reception-timestamp\":" + getCurrentTime() + ",");
- OUT_JSON_FILE.print("\"begin-node-id\":" + getFrom().getNodeID() + ",");
- OUT_JSON_FILE.print("\"end-node-id\":" + getTo().getNodeID() + ",");
- OUT_JSON_FILE.print("\"block-id\":" + block.getId());
- OUT_JSON_FILE.print("}");
- OUT_JSON_FILE.print("},");
- OUT_JSON_FILE.flush();
-
super.run();
}
diff --git a/simulator/src/main/java/simblock/task/SelfishMiningTask.java b/simulator/src/main/java/simblock/task/SelfishMiningTask.java
new file mode 100644
index 00000000..628e16dc
--- /dev/null
+++ b/simulator/src/main/java/simblock/task/SelfishMiningTask.java
@@ -0,0 +1,32 @@
+package simblock.task;
+
+import simblock.block.ProofOfWorkBlock;
+import simblock.node.Node;
+import simblock.node.SelfishNode;
+
+import java.math.BigInteger;
+
+import static simblock.simulator.Timer.getCurrentTime;
+
+public class SelfishMiningTask extends AbstractMintingTask {
+ private final BigInteger difficulty;
+ /**
+ * Instantiates a new Abstract minting task.
+ *
+ * @param minter the minter
+ * @param interval the interval in milliseconds
+ */
+ public SelfishMiningTask(Node minter, long interval, BigInteger difficulty) {
+ super(minter, interval);
+ this.difficulty = difficulty;
+ }
+
+ @Override
+ public void run() {
+ ProofOfWorkBlock createdBlock = new ProofOfWorkBlock(
+ (ProofOfWorkBlock) this.getParent(), this.getMinter(), getCurrentTime(),
+ this.difficulty
+ );
+ ((SelfishNode)this.getMinter()).receiveSelfishBlock(createdBlock);
+ }
+}
diff --git a/simulator/src/main/java/utility/graph/DAG.java b/simulator/src/main/java/utility/graph/DAG.java
new file mode 100644
index 00000000..f6c4745a
--- /dev/null
+++ b/simulator/src/main/java/utility/graph/DAG.java
@@ -0,0 +1,85 @@
+package utility.graph;
+
+
+
+import simblock.block.Block;
+
+import java.util.ArrayList;
+
+public class DAG {
+ private ArrayList>> graph;
+ private int height = -1;
+
+ public DAG(){
+ graph = new ArrayList>>();
+ }
+
+ public void setRoot(T root){
+ DAGNode node = new DAGNode(root);
+ height = 0;
+
+ graph.add(new ArrayList>());
+ graph.get(height).add(node);
+
+ return;
+ }
+
+ public void addToDAG(T newValue, int newHeight){
+ DAGNode node = new DAGNode(newValue);
+
+ if(height >= newHeight){
+ graph.get(newHeight).add(node);
+ }
+ else{
+ graph.add(new ArrayList>());
+ height++;
+ graph.get(newHeight).add(node);
+ }
+
+ return;
+ }
+
+ public ArrayList>> getDAG(){
+ return graph;
+ }
+
+ public int getCurrentHeight(){
+ return height;
+ }
+
+ public ArrayList getNodesByHeight(int height){
+ ArrayList result = new ArrayList();
+
+ for(DAGNode node : graph.get(height)){
+ result.add(node.getData());
+ }
+
+ return result;
+ }
+
+ public T getNodeByHeightRow(int height, int row){
+ return graph.get(height).get(row).getData();
+ }
+
+ public int getTotalNumberOfNodes(){
+ int counter = 0;
+
+ for(int i=0; i < graph.size(); i++){
+ for(int j=0; j < graph.get(i).size(); j++){
+ counter++;
+ }
+ }
+
+ return counter;
+ }
+
+ public void setDAGHeight(int height, ArrayList inputNodes){
+ ArrayList> result = new ArrayList<>();
+
+ for(T node : inputNodes){
+ result.add(new DAGNode(node));
+ }
+
+ graph.set(height, result);
+ }
+}
diff --git a/simulator/src/main/java/utility/graph/DAGNode.java b/simulator/src/main/java/utility/graph/DAGNode.java
new file mode 100644
index 00000000..999ea02c
--- /dev/null
+++ b/simulator/src/main/java/utility/graph/DAGNode.java
@@ -0,0 +1,13 @@
+package utility.graph;
+
+public class DAGNode {
+ private T data;
+
+ public DAGNode(T data){
+ this.data = data;
+ }
+
+ public T getData(){
+ return this.data;
+ }
+}