Skip to content

Commit 83de387

Browse files
authored
Merge pull request #4630 from 317787106/dup_witness_fix
optimize SR’s production order when multi masters exist
2 parents f67bec2 + a3102a1 commit 83de387

2 files changed

Lines changed: 47 additions & 4 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,12 @@ 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+
if (null != currentBlockId
94+
&& currentBlockId.toString().compareTo(blockCapsule.getBlockId().toString()) > 0) {
95+
return;
9796
}
9897

98+
dupBlockCount.set(1);
9999
dupBlockTime.set(System.currentTimeMillis());
100100

101101
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)