2
2
3
3
import byzzbench .simulator .LeaderBasedProtocolReplica ;
4
4
import byzzbench .simulator .Scenario ;
5
- import byzzbench .simulator .protocols .tendermint .message .ReplyMessage ;
6
- import byzzbench .simulator .protocols .tendermint .message .RequestMessage ;
7
5
import byzzbench .simulator .protocols .tendermint .message .*;
8
6
import byzzbench .simulator .state .TotalOrderCommitLog ;
9
7
import byzzbench .simulator .transport .DefaultClientRequestPayload ;
10
8
import byzzbench .simulator .transport .MessagePayload ;
11
-
12
-
13
- import java .io .Serializable ;
14
- import java .time .Duration ;
15
- import java .util .*;
16
-
17
9
import lombok .Getter ;
18
10
import lombok .extern .java .Log ;
19
11
import org .apache .commons .lang3 .tuple .Pair ;
20
12
13
+ import java .time .Duration ;
14
+ import java .util .*;
15
+
21
16
@ Log
22
17
@ Getter
23
18
public class TendermintReplica extends LeaderBasedProtocolReplica {
24
19
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 );
25
28
// Blockchain height: the current index of the chain being decided
26
29
private long height ;
27
-
28
30
// Sequence: the current sequence within the height, where multiple sequences might be needed to finalize a block
29
31
private long sequence ;
30
-
31
32
private long totalSequences ;
32
-
33
33
// Step: the current step within the sequence (PROPOSE, PREVOTE, PRECOMMIT)
34
34
private Step step ;
35
-
36
35
// Hash of the block this replica is "locked" on (used to ensure no conflicting decisions are made)
37
36
private Block lockedValue ;
38
-
39
37
// Sequence number of the block this replica is locked on
40
38
private long lockedSequence ;
41
-
42
39
// Hash of the block this replica has validated
43
40
private Block validValue ;
44
-
45
41
// Sequence number of the block this replica has validated
46
42
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
-
55
43
private boolean precommitRule0Check ;
56
44
private boolean prevoteRule1Check ;
57
45
private boolean prevoteRule2Check ;
58
46
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
-
67
47
68
48
public TendermintReplica (String nodeId , SortedSet <String > nodeIds , Scenario scenario ) {
69
49
// Initialize replica with node ID, a list of other nodes, transport, and a commit log
@@ -316,7 +296,7 @@ private void broadcastGossipProposal(ProposalMessage proposalMessage) {
316
296
}
317
297
318
298
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 ))) {
320
300
return ;
321
301
}
322
302
ProposalMessage proposalMessage = new ProposalMessage (getId (), height , sequence , totalSequences , validSequence , proposal );
@@ -621,8 +601,7 @@ private void onTimeoutPrecommit(long height, long sequence) {
621
601
if (this .height == height
622
602
&& this .sequence == sequence ) {
623
603
startRound (this .sequence + 1 );
624
- }
625
- else
604
+ } else
626
605
log .info ("Timeout Precommit called but height and round do not match" );
627
606
}
628
607
@@ -666,9 +645,9 @@ protected boolean validateMessage(MessagePayload message) {
666
645
667
646
@ Override
668
647
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 ));
672
651
return ;
673
652
} else if (message instanceof GossipRequest ) {
674
653
handleGossipRequest ((GossipRequest ) message );
@@ -719,11 +698,6 @@ private void handleGossipMessage(GossipMessage message) {
719
698
}
720
699
}
721
700
722
- @ Override
723
- public void handleClientRequest (String clientId , Serializable request ) {
724
- startRound (0 );
725
- }
726
-
727
701
private void receiveRequest (String sender , RequestMessage m ) {
728
702
messageLog .bufferRequest (m );
729
703
broadcastMessage (new GossipRequest (this .getId (), m ));
@@ -803,7 +777,7 @@ private String proposer(long height, long sequence) {
803
777
// Calculate the index based on the height and round
804
778
// Using both height and round ensures determinism across rounds and heights
805
779
int index = (int ) ((height + sequence ) % proposerList .size ());
806
- if (index < 0 ) {
780
+ if (index < 0 ) {
807
781
index = 0 ;
808
782
}
809
783
return proposerList .get (index );
@@ -832,8 +806,8 @@ public void print() {
832
806
log .info ("Locked Value: " + this .lockedValue );
833
807
log .info ("Locked Seq: " + this .lockedSequence );
834
808
log .info ("Valid Value: " + this .validValue );
835
- log .info ("Valid Seq: " + this .validSequence );;
809
+ log .info ("Valid Seq: " + this .validSequence );
836
810
log .info ("Tolerance: " + this .tolerance );
837
811
838
812
}
839
- }
813
+ }
0 commit comments