Skip to content

Commit 2d2200c

Browse files
committed
feat(consensus): check block header time is an integer multiple of 3s
1 parent 1f0aa38 commit 2d2200c

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
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) {
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+
}

0 commit comments

Comments
 (0)