Skip to content

Commit 2b657e9

Browse files
committed
Update TendermintReplica.java
1 parent a3d2683 commit 2b657e9

File tree

1 file changed

+19
-45
lines changed

1 file changed

+19
-45
lines changed

simulator/src/main/java/byzzbench/simulator/protocols/tendermint/TendermintReplica.java

+19-45
Original file line numberDiff line numberDiff line change
@@ -2,68 +2,48 @@
22

33
import byzzbench.simulator.LeaderBasedProtocolReplica;
44
import byzzbench.simulator.Scenario;
5-
import byzzbench.simulator.protocols.tendermint.message.ReplyMessage;
6-
import byzzbench.simulator.protocols.tendermint.message.RequestMessage;
75
import byzzbench.simulator.protocols.tendermint.message.*;
86
import byzzbench.simulator.state.TotalOrderCommitLog;
97
import byzzbench.simulator.transport.DefaultClientRequestPayload;
108
import byzzbench.simulator.transport.MessagePayload;
11-
12-
13-
import java.io.Serializable;
14-
import java.time.Duration;
15-
import java.util.*;
16-
179
import lombok.Getter;
1810
import lombok.extern.java.Log;
1911
import org.apache.commons.lang3.tuple.Pair;
2012

13+
import java.time.Duration;
14+
import java.util.*;
15+
2116
@Log
2217
@Getter
2318
public class TendermintReplica extends LeaderBasedProtocolReplica {
2419

20+
public static final Block NULL_BLOCK = new Block(Long.MIN_VALUE, "NULL VALUE", null);
21+
public final int TIMEOUT = 50;
22+
private final long tolerance = 1;
23+
// Assigned powers of each replica in the network
24+
private final Map<String, Integer> votingPower = new HashMap<>();
25+
private final MessageLog messageLog;
26+
private final SortedSet<Pair<Long, Long>> hasBroadcasted = new TreeSet<>();
27+
public Random rand = new Random(2137L);
2528
// Blockchain height: the current index of the chain being decided
2629
private long height;
27-
2830
// Sequence: the current sequence within the height, where multiple sequences might be needed to finalize a block
2931
private long sequence;
30-
3132
private long totalSequences;
32-
3333
// Step: the current step within the sequence (PROPOSE, PREVOTE, PRECOMMIT)
3434
private Step step;
35-
3635
// Hash of the block this replica is "locked" on (used to ensure no conflicting decisions are made)
3736
private Block lockedValue;
38-
3937
// Sequence number of the block this replica is locked on
4038
private long lockedSequence;
41-
4239
// Hash of the block this replica has validated
4340
private Block validValue;
44-
4541
// Sequence number of the block this replica has validated
4642
private long validSequence;
47-
48-
private MessageLog messageLog;
49-
50-
private final long tolerance = 1;
51-
52-
// Assigned powers of each replica in the network
53-
private final Map<String, Integer> votingPower = new HashMap<>();
54-
5543
private boolean precommitRule0Check;
5644
private boolean prevoteRule1Check;
5745
private boolean prevoteRule2Check;
5846

59-
public static final Block NULL_BLOCK = new Block(Long.MIN_VALUE, "NULL VALUE", null);
60-
61-
public final int TIMEOUT = 50;
62-
63-
public Random rand = new Random(2137L);
64-
65-
private SortedSet<Pair<Long, Long>> hasBroadcasted = new TreeSet<>();
66-
6747

6848
public TendermintReplica(String nodeId, SortedSet<String> nodeIds, Scenario scenario) {
6949
// Initialize replica with node ID, a list of other nodes, transport, and a commit log
@@ -316,7 +296,7 @@ private void broadcastGossipProposal(ProposalMessage proposalMessage) {
316296
}
317297

318298
protected void broadcastProposal(long height, long sequence, Block proposal, long validSequence) {
319-
if(hasBroadcasted.contains(Pair.of(height, sequence))) {
299+
if (hasBroadcasted.contains(Pair.of(height, sequence))) {
320300
return;
321301
}
322302
ProposalMessage proposalMessage = new ProposalMessage(getId(), height, sequence, totalSequences, validSequence, proposal);
@@ -621,8 +601,7 @@ private void onTimeoutPrecommit(long height, long sequence) {
621601
if (this.height == height
622602
&& this.sequence == sequence) {
623603
startRound(this.sequence + 1);
624-
}
625-
else
604+
} else
626605
log.info("Timeout Precommit called but height and round do not match");
627606
}
628607

@@ -666,9 +645,9 @@ protected boolean validateMessage(MessagePayload message) {
666645

667646
@Override
668647
public void handleMessage(String sender, MessagePayload message) throws Exception {
669-
if (message instanceof DefaultClientRequestPayload) {
670-
String clientId = ((DefaultClientRequestPayload) message).getOperation().toString().split("/")[0];
671-
receiveRequest(sender, new RequestMessage(((DefaultClientRequestPayload) message).getOperation(), System.currentTimeMillis(), clientId));
648+
if (message instanceof DefaultClientRequestPayload clientRequest) {
649+
String clientId = clientRequest.getOperation().toString().split("/")[0];
650+
receiveRequest(sender, new RequestMessage(clientRequest.getOperation(), System.currentTimeMillis(), clientId));
672651
return;
673652
} else if (message instanceof GossipRequest) {
674653
handleGossipRequest((GossipRequest) message);
@@ -719,11 +698,6 @@ private void handleGossipMessage(GossipMessage message) {
719698
}
720699
}
721700

722-
@Override
723-
public void handleClientRequest(String clientId, Serializable request) {
724-
startRound(0);
725-
}
726-
727701
private void receiveRequest(String sender, RequestMessage m) {
728702
messageLog.bufferRequest(m);
729703
broadcastMessage(new GossipRequest(this.getId(), m));
@@ -803,7 +777,7 @@ private String proposer(long height, long sequence) {
803777
// Calculate the index based on the height and round
804778
// Using both height and round ensures determinism across rounds and heights
805779
int index = (int) ((height + sequence) % proposerList.size());
806-
if(index < 0) {
780+
if (index < 0) {
807781
index = 0;
808782
}
809783
return proposerList.get(index);
@@ -832,8 +806,8 @@ public void print() {
832806
log.info("Locked Value: " + this.lockedValue);
833807
log.info("Locked Seq: " + this.lockedSequence);
834808
log.info("Valid Value: " + this.validValue);
835-
log.info("Valid Seq: " + this.validSequence);;
809+
log.info("Valid Seq: " + this.validSequence);
836810
log.info("Tolerance: " + this.tolerance);
837811

838812
}
839-
}
813+
}

0 commit comments

Comments
 (0)