Skip to content

Commit

Permalink
Major update: Seed now changes through configuration:
Browse files Browse the repository at this point in the history
- It used to be that the common (across simulations) workload (i.e., up
  to the last transactino of interest) had to be loaded from a file. It
  can now be recreated by a shared key that is updated once a
  transaction appears.
  • Loading branch information
liaskos committed Dec 12, 2024
1 parent cd99cee commit 4511f41
Show file tree
Hide file tree
Showing 21 changed files with 514 additions and 58 deletions.
2 changes: 2 additions & 0 deletions comp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
mvn clean package
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.2</version>
<configuration>
<!--<redirectTestOutputToFile>false</redirectTestOutputToFile>
<forkCount>0</forkCount>-->
<redirectTestOutputToFile>false</redirectTestOutputToFile>
<forkCount>0</forkCount>
<excludedGroups>exclude</excludedGroups>
<excludes>
<exclude>**/commandline/ConfigInitializerTest.java</exclude>
Expand Down
2 changes: 1 addition & 1 deletion run
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
java -jar target/cnsim-0.0.1-SNAPSHOT.jar -c src/main/resources/bitcoin-config/light1.bitcoin.application.properties
java -jar target/cnsim-0.0.1-SNAPSHOT.jar -c src/main/resources/bitcoin-config/bitcoin.lowrate1.application.properties
12 changes: 10 additions & 2 deletions src/main/java/ca/yorku/cmg/cnsim/bitcoin/BitcoinMainDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import ca.yorku.cmg.cnsim.engine.node.Node;
import ca.yorku.cmg.cnsim.engine.node.NodeSet;
import ca.yorku.cmg.cnsim.engine.reporter.ReportEventFactory;
import ca.yorku.cmg.cnsim.engine.reporter.Reporter;
import ca.yorku.cmg.cnsim.engine.transaction.Transaction;
import ca.yorku.cmg.cnsim.engine.transaction.TransactionWorkload;

Expand Down Expand Up @@ -49,6 +50,12 @@ private void run(String[] args) {
System.exit(1);
}


//INitialize Bitcoin reporter
BitcoinReporter.reportBlockEvents(Config.getPropertyBoolean("reporter.reportBlockEvents"));
BitcoinReporter.reportStructureEvents(Config.getPropertyBoolean("reporter.reportStructureEvents"));


// Get the number of simulations to run
int numSimulations = Config.getPropertyInt("sim.numSimulations");

Expand Down Expand Up @@ -137,8 +144,9 @@ private void runSingleSimulation(int simID) {
sampler.setTransactionSampler(
new TransactionSamplerFactory().getSampler(
Config.getPropertyString("workload.sampler.file"),
(Config.hasProperty("workload.sampler.seed") ? Config.getPropertyLong("workload.sampler.seed") : null),
(Config.hasProperty("workload.sampler.seed.updateSeed") ? Config.getPropertyBoolean("workload.sampler.seed.updateSeed") : null),
//(Config.hasProperty("workload.sampler.seed") ? Config.getPropertyLong("workload.sampler.seed") : null),
//(Config.hasProperty("workload.sampler.seed.updateSeed") ? Config.getPropertyBoolean("workload.sampler.seed.updateSeed") : null),
//(Config.hasProperty("workload.sampler.seed.updateTransaction") ? Config.getPropertyLong("workload.sampler.seed.updateTransaction") : null),
sampler,
s));
} catch (Exception e) {
Expand Down
32 changes: 24 additions & 8 deletions src/main/java/ca/yorku/cmg/cnsim/bitcoin/BitcoinReporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ public class BitcoinReporter extends Reporter {
protected static FileWriter blockWriter;


protected static boolean reportBlockEvents;
protected static boolean reportStructureEvents;

public static void reportBlockEvents(boolean reportBlockEvents) {
BitcoinReporter.reportBlockEvents = reportBlockEvents;
}


public static void reportStructureEvents(boolean reportStructureEvents) {
BitcoinReporter.reportStructureEvents = reportStructureEvents;
}


static {
blockLog.add("SimID, SimTime,SysTime,NodeID,"
+ "BlockID,ParentID,Height,BlockContent,"
Expand All @@ -36,13 +49,15 @@ public class BitcoinReporter extends Reporter {
* @author Sotirios Liaskos
*/
public static void reportBlockChainState(String[] blockchain, String[] orphans) {
for (String s :blockchain) {
//s = SimTime + "," + SysTime + "," + blockID + "," + s + ",blockchain";
structureLog.add(s);
}
for (String s :orphans) {
//s = SimTime + "," + SysTime + "," + blockID + "," + s + ",orphans";
structureLog.add(s);
if (BitcoinReporter.reportStructureEvents) {
for (String s :blockchain) {
//s = SimTime + "," + SysTime + "," + blockID + "," + s + ",blockchain";
structureLog.add(s);
}
for (String s :orphans) {
//s = SimTime + "," + SysTime + "," + blockID + "," + s + ",orphans";
structureLog.add(s);
}
}
}

Expand All @@ -67,7 +82,8 @@ public static void reportBlockEvent(
int blockID, int parentID, int height, String txInvolved,
String blockEvt,
double difficulty, //Difficulty: the difficulty under which the block was validated.
double cycles) { //Cycles: the number of cycles dedicated to validate the block.
double cycles) { //Cycles: the number of cycles dedicated to validate the block.
if (BitcoinReporter.reportBlockEvents)
blockLog.add(simID + "," +
simTime + "," +
sysTime + "," +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ public void LoadConfig() {
*/
public abstract float getNextTransactionArrivalInterval() throws Exception;



/**
* Get a sample transaction fee value.
* @return Transaction fee value (local tokens).
Expand All @@ -230,6 +232,15 @@ public void LoadConfig() {
*/
public abstract int getRandomNum(int min, int max);

//
//
// Seed management
//
//

public abstract void updateSeed();
public abstract long getSeedChangeTx();
public abstract boolean seedUpdateEnabled();


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ca.yorku.cmg.cnsim.engine;

import ca.yorku.cmg.cnsim.engine.commandline.CommandLineParser;
import ca.yorku.cmg.cnsim.engine.reporter.Reporter;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -59,6 +60,13 @@ public static void initialize(String[] args) throws IOException {

// Initialize Config prop with the properties
Config.prop.putAll(properties);

Reporter.reportEvents(Config.getPropertyBoolean("reporter.reportEvents"));
Reporter.reportTransactions(Config.getPropertyBoolean("reporter.reportTransactions"));
Reporter.reportNodes(Config.getPropertyBoolean("reporter.reportNodes"));
Reporter.reportNetEvents(Config.getPropertyBoolean("reporter.reportNetEvents"));
Reporter.reportBeliefs(Config.getPropertyBoolean("reporter.reportBeliefs"));

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,23 @@ public long getNextTransactionSize() throws Exception {
public int getRandomNum(int min, int max) {
return(alternativeSampler.getRandomNum(min, max));
}

@Override
public void updateSeed() {
alternativeSampler.updateSeed();
}

@Override
public long getSeedChangeTx() {
return(alternativeSampler.getSeedChangeTx());
}

@Override
public boolean seedUpdateEnabled() {
return(alternativeSampler.seedUpdateEnabled());
}




}
Original file line number Diff line number Diff line change
@@ -1,29 +1,63 @@
package ca.yorku.cmg.cnsim.engine;

import ca.yorku.cmg.cnsim.engine.transaction.Transaction;

public class StandardTransactionSampler extends AbstractTransactionSampler {


private int simID;

private long seedSwitchTx;
private long initialSeed;
private long currentSeed;

private boolean seedUpdateEnabled = false;

public StandardTransactionSampler(Sampler s) {
this.sampler = s;
}

public void updateSeed() {
if ((seedUpdateEnabled) && (seedSwitchTx < Transaction.currID - 1)) {
currentSeed = this.initialSeed + this.simID;
super.random.setSeed(currentSeed);
seedUpdateEnabled = false;
}
}

public long getCurrentSeed() {
return this.currentSeed;
}


public StandardTransactionSampler(Sampler s, long seed, boolean updateFlag, int simID) {
super.random.setSeed(seed + (updateFlag ? simID :0));
@Override
public long getSeedChangeTx() {
return (this.seedSwitchTx);
}

@Override
public boolean seedUpdateEnabled() {
return (this.seedUpdateEnabled);
}



public StandardTransactionSampler(Sampler s) {
this.sampler = s;
LoadConfig();
}

public StandardTransactionSampler(Sampler s, int simID) {
this(s);
this.simID = simID;
}


/**
* See parent. Use Poisson distribution.
*/
@Override
public float getNextTransactionArrivalInterval() {
return (float) sampler.getPoissonInterval(txArrivalIntervalRate,random)*1000;
}

@Override
public float getNextTransactionArrivalInterval() throws Exception {
updateSeed();
return (float) sampler.getPoissonInterval(txArrivalIntervalRate,random)*1000;
}

/**
* See parent. Use Normal distribution.
Expand Down Expand Up @@ -63,9 +97,25 @@ public long getNextTransactionSize() {
*/
@Override
public int getRandomNum(int min, int max) {
return(random.nextInt((max - min) + 1) + min);
return(sampler.getTransactionSampler().getRandom().nextInt((max - min) + 1) + min);
}

@Override
public void LoadConfig() {
super.LoadConfig();
this.seedUpdateEnabled = (Config.hasProperty("workload.sampler.seed.updateSeed") ? Config.getPropertyBoolean("workload.sampler.seed.updateSeed") : false);
this.seedSwitchTx = (Config.hasProperty("workload.sampler.seed.updateTransaction") ? Config.getPropertyLong("workload.sampler.seed.updateTransaction") : 0);
this.currentSeed = (Config.hasProperty("workload.sampler.seed") ? Config.getPropertyLong("workload.sampler.seed") : 0);
this.initialSeed = this.currentSeed;
}



//For testing
public void nailConfig(long initSeed, boolean seedUpdateEnabled, long seedSwitchTx) {
this.seedUpdateEnabled = seedUpdateEnabled;
this.seedSwitchTx = seedSwitchTx;
this.currentSeed = initSeed;
this.initialSeed = this.currentSeed;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,14 @@
public class TransactionSamplerFactory {

public AbstractTransactionSampler getSampler(String path,
Long seed, Boolean updateFlag,
Sampler outerSampler, Simulation sim) throws Exception {

boolean hasWorkloadSeed = (seed != null);

if (path != null) {
Debug.p(" Creating file-based workload sampler");
if (hasWorkloadSeed) {
return(new FileBasedTransactionSampler(path, new StandardTransactionSampler(outerSampler, seed, updateFlag, sim.getSimID())));
} else {
return(new FileBasedTransactionSampler(path, new StandardTransactionSampler(outerSampler)));
}
return(new FileBasedTransactionSampler(path, new StandardTransactionSampler(outerSampler, sim.getSimID())));
} else {
Debug.p(" Creating random workload sampler");
if (hasWorkloadSeed) {
return(new StandardTransactionSampler(outerSampler, seed, updateFlag, sim.getSimID()));
} else {
return(new StandardTransactionSampler(outerSampler));
}
return(new StandardTransactionSampler(outerSampler, sim.getSimID()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public void happen(Simulation sim) {
getTime());

ProgressBar.printProgress((int) transaction.getID(),sim.totalqueuedTransactions,4);

// If the transaction has been marked (at TransactionWorkload) as seed changing.
// update the node seeds. (Transaction Sampler seeds have been updated at workload creation).
if (transaction.isSeedChanging()) {
sim.getSampler().getNodeSampler().updateSeed();
}

}
}
Loading

0 comments on commit 4511f41

Please sign in to comment.