Skip to content

Commit 6bbbdd2

Browse files
authored
Merge pull request #17 from joaomlneto/add-code-comments
Add brief comments to every class
2 parents 4342370 + 6c79c8b commit 6bbbdd2

34 files changed

+976
-783
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,99 @@
11
package byzzbench.simulator;
22

3-
import byzzbench.simulator.service.DigestService;
43
import byzzbench.simulator.state.CommitLog;
54
import byzzbench.simulator.transport.MessagePayload;
65
import byzzbench.simulator.transport.Transport;
76
import com.fasterxml.jackson.annotation.JsonIgnore;
8-
import lombok.Getter;
9-
import lombok.ToString;
10-
import lombok.extern.java.Log;
11-
127
import java.io.Serializable;
138
import java.security.MessageDigest;
149
import java.security.NoSuchAlgorithmException;
1510
import java.util.Set;
11+
import lombok.Getter;
12+
import lombok.ToString;
13+
import lombok.extern.java.Log;
1614

15+
/**
16+
* Superclass for all replicas in the system.
17+
* <p>
18+
* Each replica has a unique node ID, a set of known node IDs in the system, a
19+
* reference to the {@link Transport} layer, and a {@link CommitLog}.
20+
*
21+
* @param <T> The type of the entries in the commit log of each {@link Replica}.
22+
*/
1723
@Log
1824
@Getter
1925
@ToString
2026
public abstract class Replica<T extends Serializable> implements Serializable {
21-
@JsonIgnore
22-
static MessageDigest md;
23-
24-
static {
25-
try {
26-
md = MessageDigest.getInstance("SHA-1");
27-
} catch (NoSuchAlgorithmException e) {
28-
throw new RuntimeException(e);
29-
}
27+
@JsonIgnore static MessageDigest md;
28+
29+
static {
30+
try {
31+
md = MessageDigest.getInstance("SHA-1");
32+
} catch (NoSuchAlgorithmException e) {
33+
throw new RuntimeException(e);
3034
}
35+
}
3136

32-
@Getter
33-
private final transient CommitLog<T> commitLog;
37+
@Getter private final transient CommitLog<T> commitLog;
3438

35-
private final transient String nodeId;
39+
private final transient String nodeId;
3640

37-
@JsonIgnore
38-
private final transient Set<String> nodeIds;
41+
@JsonIgnore private final transient Set<String> nodeIds;
3942

40-
@JsonIgnore
41-
private final transient Transport<T> transport;
43+
@JsonIgnore private final transient Transport<T> transport;
4244

43-
@JsonIgnore
44-
private DigestService digestService;
45+
protected Replica(String nodeId, Set<String> nodeIds, Transport<T> transport,
46+
CommitLog<T> commitLog) {
47+
this.nodeId = nodeId;
48+
this.nodeIds = nodeIds;
49+
this.transport = transport;
50+
this.commitLog = commitLog;
51+
}
4552

46-
protected Replica(String nodeId, Set<String> nodeIds, Transport<T> transport, CommitLog<T> commitLog) {
47-
this.nodeId = nodeId;
48-
this.nodeIds = nodeIds;
49-
this.transport = transport;
50-
this.commitLog = commitLog;
51-
}
53+
protected void sendMessage(MessagePayload message, String recipient) {
54+
this.transport.sendMessage(this.nodeId, message, recipient);
55+
}
5256

53-
protected void sendMessage(MessagePayload message, String recipient) {
54-
this.transport.sendMessage(this.nodeId, message, recipient);
55-
}
57+
protected void multicastMessage(MessagePayload message,
58+
Set<String> recipients) {
59+
this.transport.multicast(this.nodeId, recipients, message);
60+
}
5661

57-
protected void multicastMessage(MessagePayload message, Set<String> recipients) {
58-
this.transport.multicast(this.nodeId, recipients, message);
59-
}
62+
/**
63+
* Send message to all nodes in the system (except self)
64+
*
65+
* @param message the message to broadcast
66+
*/
67+
protected void broadcastMessage(MessagePayload message) {
68+
Set<String> otherNodes = this.nodeIds.stream()
69+
.filter(nodeId -> !nodeId.equals(this.nodeId))
70+
.collect(java.util.stream.Collectors.toSet());
71+
this.transport.multicast(this.nodeId, otherNodes, message);
72+
}
6073

61-
/**
62-
* Send message to all nodes in the system (except self)
63-
*
64-
* @param message the message to broadcast
65-
*/
66-
protected void broadcastMessage(MessagePayload message) {
67-
Set<String> otherNodes = this.nodeIds
68-
.stream()
69-
.filter(nodeId -> !nodeId.equals(this.nodeId))
70-
.collect(java.util.stream.Collectors.toSet());
71-
this.transport.multicast(this.nodeId, otherNodes, message);
72-
}
74+
protected void broadcastMessageIncludingSelf(MessagePayload message) {
75+
this.transport.multicast(this.nodeId, this.nodeIds, message);
76+
}
7377

74-
protected void broadcastMessageIncludingSelf(MessagePayload message) {
75-
this.transport.multicast(this.nodeId, this.nodeIds, message);
76-
}
78+
public byte[] digest(Serializable message) {
79+
return md.digest(message.toString().getBytes());
80+
}
7781

78-
public byte[] digest(Serializable message) {
79-
return md.digest(message.toString().getBytes());
80-
}
82+
@JsonIgnore
83+
public Serializable getState() {
84+
return this;
85+
}
8186

82-
@JsonIgnore
83-
public Serializable getState() {
84-
return this;
85-
}
87+
public abstract void initialize();
8688

87-
public abstract void initialize();
89+
public abstract void handleMessage(String sender, MessagePayload message)
90+
throws Exception;
8891

89-
public abstract void handleMessage(String sender, MessagePayload message) throws Exception;
92+
public void commitOperation(T message) { this.commitLog.add(message); }
9093

91-
public void commitOperation(T message) {
92-
this.commitLog.add(message);
93-
}
94-
95-
public long setTimeout(Runnable r, long timeout) {
96-
return this.transport.setTimeout(this, r, timeout);
97-
}
94+
public long setTimeout(Runnable r, long timeout) {
95+
return this.transport.setTimeout(this, r, timeout);
96+
}
9897

99-
public void clearAllTimeouts() {
100-
this.transport.clearReplicaTimeouts(this);
101-
}
98+
public void clearAllTimeouts() { this.transport.clearReplicaTimeouts(this); }
10299
}

simulator/src/main/java/byzzbench/simulator/ScenarioExecutor.java

+27-22
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,45 @@
33
import byzzbench.simulator.scheduler.BaseScheduler;
44
import byzzbench.simulator.scheduler.RandomScheduler;
55
import byzzbench.simulator.transport.Transport;
6-
import lombok.Getter;
7-
86
import java.io.Serializable;
97
import java.util.HashMap;
108
import java.util.Map;
9+
import lombok.Getter;
1110

11+
/**
12+
* Abstract class for running a scenario, which consists of a set of {@link
13+
* Replica} and a {@link Transport} layer.
14+
*
15+
* @param <T> The type of the entries in the commit log of each {@link Replica}.
16+
*/
1217
@Getter
1318
public abstract class ScenarioExecutor<T extends Serializable> {
14-
protected final Transport<T> transport;
19+
protected final Transport<T> transport;
1520

16-
protected final BaseScheduler<T> scheduler;
21+
protected final BaseScheduler<T> scheduler;
1722

18-
protected final Map<String, Replica<T>> nodes = new HashMap<>();
23+
protected final Map<String, Replica<T>> nodes = new HashMap<>();
1924

20-
public ScenarioExecutor(Transport<T> transport) {
21-
this.transport = transport;
22-
this.scheduler = new RandomScheduler<>(transport);
23-
this.setup();
24-
}
25+
public ScenarioExecutor(Transport<T> transport) {
26+
this.transport = transport;
27+
this.scheduler = new RandomScheduler<>(transport);
28+
this.setup();
29+
}
2530

26-
public void reset() {
27-
this.transport.reset();
28-
this.nodes.clear();
29-
this.setup();
30-
for (Replica<T> r : this.nodes.values()) {
31-
r.initialize();
32-
}
31+
public void reset() {
32+
this.transport.reset();
33+
this.nodes.clear();
34+
this.setup();
35+
for (Replica<T> r : this.nodes.values()) {
36+
r.initialize();
3337
}
38+
}
3439

35-
public void addNode(Replica<T> replica) {
36-
this.nodes.put(replica.getNodeId(), replica);
37-
}
40+
public void addNode(Replica<T> replica) {
41+
this.nodes.put(replica.getNodeId(), replica);
42+
}
3843

39-
public abstract void setup();
44+
public abstract void setup();
4045

41-
public abstract void run();
46+
public abstract void run();
4247
}

simulator/src/main/java/byzzbench/simulator/SimulatorApplication.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
@SpringBootApplication
77
public class SimulatorApplication {
88

9-
public static void main(String[] args) {
10-
SpringApplication.run(SimulatorApplication.class, args);
11-
}
12-
9+
public static void main(String[] args) {
10+
SpringApplication.run(SimulatorApplication.class, args);
11+
}
1312
}
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,45 @@
11
package byzzbench.simulator.adob;
22

33
import byzzbench.simulator.Replica;
4-
54
import java.io.Serializable;
65

6+
/**
7+
* The distributed state of the BFT consensus protocol for the current
8+
* simulation.
9+
*
10+
* @see <a href="https://flint.cs.yale.edu/flint/publications/adob-tr.pdf">AdoB
11+
* paper</a>
12+
*/
713
public class AdobDistributedState {
8-
private AdobCache root;
9-
10-
public CommitCache getLatestCommitCache() {
11-
AdobCache cache = root;
12-
while (cache instanceof CommitCache) {
13-
cache = cache.getParent();
14-
}
15-
return (CommitCache) cache;
16-
}
17-
18-
// TODO: Whenever a replica changes its leader, create or update the respective ECache
19-
public void onLeaderChange(Replica r, String newLeaderId) {
20-
throw new RuntimeException("Not yet implemented");
21-
}
14+
private AdobCache root;
2215

23-
// TODO: Whenever the leader does a local commit, create a new MCache
24-
public void onLocalCommit(Replica r, Serializable operation) {
25-
throw new RuntimeException("Not yet implemented");
16+
public CommitCache getLatestCommitCache() {
17+
AdobCache cache = root;
18+
while (cache instanceof CommitCache) {
19+
cache = cache.getParent();
2620
}
27-
28-
// TODO: Whenever a replica times out and triggers an election, create a TCache
29-
public void onTimeout(Replica r) {
30-
throw new RuntimeException("Not yet implemented");
31-
}
32-
33-
// TODO: Whenever the leader forms a quorum, create a new CCache
34-
public void onQuorum(Replica r) {
35-
throw new RuntimeException("Not yet implemented");
36-
}
37-
21+
return (CommitCache)cache;
22+
}
23+
24+
// TODO: Whenever a replica changes its leader, create or update the
25+
// respective ECache
26+
public void onLeaderChange(Replica r, String newLeaderId) {
27+
throw new RuntimeException("Not yet implemented");
28+
}
29+
30+
// TODO: Whenever the leader does a local commit, create a new MCache
31+
public void onLocalCommit(Replica r, Serializable operation) {
32+
throw new RuntimeException("Not yet implemented");
33+
}
34+
35+
// TODO: Whenever a replica times out and triggers an election, create a
36+
// TCache
37+
public void onTimeout(Replica r) {
38+
throw new RuntimeException("Not yet implemented");
39+
}
40+
41+
// TODO: Whenever the leader forms a quorum, create a new CCache
42+
public void onQuorum(Replica r) {
43+
throw new RuntimeException("Not yet implemented");
44+
}
3845
}

0 commit comments

Comments
 (0)