Skip to content

Commit 5f29501

Browse files
committed
refactor(net): when multi master occurs, the master with bigger blockId is elected to produce block
1 parent 8b6beb8 commit 5f29501

2 files changed

Lines changed: 49 additions & 4 deletions

File tree

consensus/src/main/java/org/tron/consensus/dpos/StateManager.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,14 @@ public void receiveBlock(BlockCapsule blockCapsule) {
9090
return;
9191
}
9292

93-
if (dupBlockCount.get() == 0) {
94-
dupBlockCount.set(new Random().nextInt(10));
95-
} else {
96-
dupBlockCount.set(10);
93+
//multi master occurs. If currentBlockId is bigger, we continue to produce block at next cycle.
94+
if (null != currentBlockId
95+
&& currentBlockId.toString().compareTo(blockCapsule.getBlockId().toString()) > 0) {
96+
return;
9797
}
9898

99+
//currentBlockId is smaller, we may pause one cycle
100+
dupBlockCount.set(1);
99101
dupBlockTime.set(System.currentTimeMillis());
100102

101103
logger.warn("Dup block produced: {}", blockCapsule);

framework/src/test/java/org/tron/common/overlay/discover/table/NodeEntryTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.junit.Assert;
44
import org.junit.Test;
55
import org.tron.common.overlay.discover.node.Node;
6+
import org.tron.common.utils.ByteArray;
67

78
public class NodeEntryTest {
89

@@ -23,4 +24,46 @@ public void test() throws InterruptedException {
2324
Assert.assertTrue(isDif);
2425
}
2526

27+
@Test
28+
public void testDistance() {
29+
byte[] randomId = Node.getNodeId();
30+
String hexRandomIdStr = ByteArray.toHexString(randomId);
31+
Assert.assertEquals(128, hexRandomIdStr.length());
32+
33+
byte[] nodeId1 = ByteArray.fromHexString(
34+
"0000000000000000000000000000000000000000000000000000000000000000"
35+
+ "0000000000000000000000000000000000000000000000000000000000000000");
36+
byte[] nodeId2 = ByteArray.fromHexString(
37+
"a000000000000000000000000000000000000000000000000000000000000000"
38+
+ "0000000000000000000000000000000000000000000000000000000000000000");
39+
Assert.assertEquals(256, NodeEntry.distance(nodeId1, nodeId2));
40+
41+
byte[] nodeId3 = ByteArray.fromHexString(
42+
"0000000000000000000000000000000000000000000000000000000000000001"
43+
+ "0000000000000000000000000000000000000000000000000000000000000000");
44+
Assert.assertEquals(1, NodeEntry.distance(nodeId1, nodeId3));
45+
46+
byte[] nodeId4 = ByteArray.fromHexString(
47+
"0000000000000000000000000000000000000000000000000000000000000000"
48+
+ "8000000000000000000000000000000000000000000000000000000000000000");
49+
Assert.assertEquals(0, NodeEntry.distance(nodeId1, nodeId4)); // => 0
50+
51+
byte[] nodeId5 = ByteArray.fromHexString(
52+
"0000000000000000000000000000000000000000000000000000000000000000"
53+
+ "4000000000000000000000000000000000000000000000000000000000000000");
54+
Assert.assertEquals(-1, NodeEntry.distance(nodeId1, nodeId5)); // => 0
55+
56+
byte[] nodeId6 = ByteArray.fromHexString(
57+
"0000000000000000000000000000000000000000000000000000000000000000"
58+
+ "2000000000000000000000000000000000000000000000000000000000000000");
59+
Assert.assertEquals(-2, NodeEntry.distance(nodeId1, nodeId6)); // => 0
60+
61+
byte[] nodeId7 = ByteArray.fromHexString(
62+
"0000000000000000000000000000000000000000000000000000000000000000"
63+
+ "0000000000000000000000000000000000000000000000000000000000000001");
64+
Assert.assertEquals(-255, NodeEntry.distance(nodeId1, nodeId7)); // => 0
65+
66+
Assert.assertEquals(-256, NodeEntry.distance(nodeId1, nodeId1)); // => 0
67+
}
68+
2669
}

0 commit comments

Comments
 (0)