Skip to content

Commit bbc51c6

Browse files
committed
Merge branch 'release_v4.8.0' of github.com:tronprotocol/java-tron into event-22
2 parents fb2f371 + 791ee6c commit bbc51c6

File tree

5 files changed

+75
-2
lines changed

5 files changed

+75
-2
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.tron.consensus.dpos;
22

3+
import static org.tron.core.config.Parameter.ChainConstant.BLOCK_PRODUCED_INTERVAL;
34
import static org.tron.core.config.Parameter.ChainConstant.MAX_ACTIVE_WITNESS_NUM;
45
import static org.tron.core.config.Parameter.ChainConstant.SOLIDIFIED_THRESHOLD;
56

@@ -116,6 +117,12 @@ public boolean validBlock(BlockCapsule blockCapsule) {
116117
}
117118
ByteString witnessAddress = blockCapsule.getWitnessAddress();
118119
long timeStamp = blockCapsule.getTimeStamp();
120+
if (timeStamp % BLOCK_PRODUCED_INTERVAL != 0
121+
&& consensusDelegate.getDynamicPropertiesStore().allowConsensusLogicOptimization()) {
122+
logger.warn("ValidBlock failed: witness: {}, timeStamp: {}",
123+
ByteArray.toHexString(witnessAddress.toByteArray()), timeStamp);
124+
return false;
125+
}
119126
long bSlot = dposSlot.getAbSlot(timeStamp);
120127
long hSlot = dposSlot.getAbSlot(consensusDelegate.getLatestBlockHeaderTimestamp());
121128
if (bSlot <= hSlot) {

framework/src/main/java/org/tron/core/net/TronNetDelegate.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ public BlockId getKhaosDbHeadBlockId() {
156156
return chainBaseManager.getKhaosDbHead().getBlockId();
157157
}
158158

159+
public long getSolidifiedBlockNum() {
160+
return chainBaseManager.getDynamicPropertiesStore().getLatestSolidifiedBlockNum();
161+
}
162+
159163
public BlockId getSolidBlockId() {
160164
return chainBaseManager.getSolidBlockId();
161165
}

framework/src/main/java/org/tron/core/net/service/adv/AdvService.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,16 @@ public boolean addInv(Item item) {
121121
if (item.getType().equals(InventoryType.TRX) && trxCache.getIfPresent(item) != null) {
122122
return false;
123123
}
124-
if (item.getType().equals(InventoryType.BLOCK) && blockCache.getIfPresent(item) != null) {
125-
return false;
124+
125+
if (item.getType().equals(InventoryType.BLOCK)) {
126+
if (blockCache.getIfPresent(item) != null) {
127+
return false;
128+
}
129+
130+
long solidNum = tronNetDelegate.getSolidifiedBlockNum();
131+
if (new BlockId(item.getHash()).getNum() <= solidNum) {
132+
return false;
133+
}
126134
}
127135

128136
synchronized (this) {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.tron.core.consensus;
2+
3+
import static org.mockito.Mockito.mock;
4+
5+
import java.lang.reflect.Field;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
import org.mockito.Mockito;
9+
import org.tron.consensus.ConsensusDelegate;
10+
import org.tron.consensus.dpos.DposService;
11+
import org.tron.core.capsule.BlockCapsule;
12+
import org.tron.core.store.DynamicPropertiesStore;
13+
import org.tron.protos.Protocol;
14+
15+
public class DposServiceTest {
16+
DposService service = new DposService();
17+
18+
@Test
19+
public void test() throws Exception {
20+
long headTime = 1724036757000L;
21+
22+
ConsensusDelegate consensusDelegate = mock(ConsensusDelegate.class);
23+
Field field = service.getClass().getDeclaredField("consensusDelegate");
24+
field.setAccessible(true);
25+
field.set(service, consensusDelegate);
26+
27+
DynamicPropertiesStore store = mock(DynamicPropertiesStore.class);
28+
Mockito.when(consensusDelegate.getDynamicPropertiesStore()).thenReturn(store);
29+
30+
Mockito.when(consensusDelegate.getLatestBlockHeaderNumber()).thenReturn(0L);
31+
boolean f = service.validBlock(null);
32+
Assert.assertTrue(f);
33+
34+
Protocol.BlockHeader.raw raw = Protocol.BlockHeader.raw.newBuilder()
35+
.setTimestamp(headTime + 3001).build();
36+
Protocol.BlockHeader header = Protocol.BlockHeader.newBuilder().setRawData(raw).build();
37+
Protocol.Block block = Protocol.Block.newBuilder().setBlockHeader(header).build();
38+
39+
Mockito.when(consensusDelegate.getLatestBlockHeaderNumber()).thenReturn(100L);
40+
Mockito.when(store.allowConsensusLogicOptimization()).thenReturn(true);
41+
42+
Mockito.when(consensusDelegate.getLatestBlockHeaderTimestamp()).thenReturn(headTime + 3000);
43+
f = service.validBlock(new BlockCapsule(block));
44+
Assert.assertTrue(!f);
45+
46+
}
47+
}

framework/src/test/java/org/tron/core/net/services/AdvServiceTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,17 @@ private void testAddInv() {
7474

7575
Item itemBlock = new Item(Sha256Hash.ZERO_HASH, InventoryType.BLOCK);
7676
flag = service.addInv(itemBlock);
77+
Assert.assertFalse(flag);
78+
79+
BlockCapsule.BlockId blockId = new BlockCapsule.BlockId(Sha256Hash.ZERO_HASH, 1000L);
80+
itemBlock = new Item(blockId, InventoryType.BLOCK);
81+
flag = service.addInv(itemBlock);
7782
Assert.assertTrue(flag);
7883
flag = service.addInv(itemBlock);
7984
Assert.assertFalse(flag);
8085

86+
blockId = new BlockCapsule.BlockId(Sha256Hash.ZERO_HASH, 10000L);
87+
itemBlock = new Item(blockId, InventoryType.BLOCK);
8188
service.addInvToCache(itemBlock);
8289
flag = service.addInv(itemBlock);
8390
Assert.assertFalse(flag);

0 commit comments

Comments
 (0)