Export simulation
whiteyhat opened this issue · 1 comments
whiteyhat commented
whiteyhat commented
This issue has been successfully solver. The outcome of this solution is displayed below:
public class Save {
public static void main(String[] args) throws IOException {
Random rand = new Random();
init(10, rand.nextInt(10), 2);
}
public void createNetwork(int nodeSize, int channelsPerNode){
Random rand = new Random();
rand.setSeed(System.currentTimeMillis());
// TODO Save seed in config file to replicate scenario
try {
init(nodeSize, channelsPerNode, 2);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void init(int nodeSize, int channelsPerNode, int tx) throws IOException {
JSONObject json = new JSONObject();
JSONArray config = new JSONArray();
for (int i = 0; i < nodeSize; i++) {
JSONObject nodes = new JSONObject();
JSONArray channelsArray = new JSONArray();
Random rand = new Random();
nodes.put("id",String.valueOf(i));
nodes.put("alias",("Node" + (i)));
nodes.put("balance" , String.valueOf(rand.nextInt(1000)));
for (int j = 0; j < channelsPerNode; j++) {
JSONObject channels = new JSONObject();
JSONArray transactionArray = new JSONArray();
Random random = new Random();
channels.put("id",String.valueOf(j));
channels.put("from",String.valueOf(i));
int randomNumber = random.nextInt(nodeSize);
boolean valid = false;
do {
if (randomNumber == i){
randomNumber = random.nextInt(nodeSize);
} else {
valid = true;
}
}while (!valid);
channels.put("to",String.valueOf(randomNumber));
channels.put("capacity",String.valueOf(random.nextInt(100)));
channels.put("fee", String.valueOf(random.nextInt(7)));
for (int k = 0; k < tx ; k++) {
JSONObject transaction = new JSONObject();
transaction.put("secret", "");
transaction.put("paymentRequest", "");
transaction.put("tokens", String.valueOf(random.nextInt(10000)));
transaction.put("createdAt", "");
transaction.put("expiredAt", "");
transactionArray.add(transaction);
}
channels.put("transactions" ,transactionArray);
channelsArray.add(channels);
}
nodes.put("channels", channelsArray);
config.add(nodes);
}
json.put("Nodes", config);
System.out.println(config);
// try-with-resources statement based on post comment below :)
try (FileWriter file = new FileWriter("src/main/resources/config/custom.json")) {
file.write(json.toJSONString());
}
}
}
Also, you can find more available information about the exporting the network simulation in the wiki.