Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.tron.consensus.dpos;

import static org.tron.core.config.Parameter.ChainConstant.BLOCK_PRODUCED_INTERVAL;
import static org.tron.core.config.Parameter.ChainConstant.MAX_ACTIVE_WITNESS_NUM;
import static org.tron.core.config.Parameter.ChainConstant.SOLIDIFIED_THRESHOLD;

Expand Down Expand Up @@ -116,6 +117,12 @@ public boolean validBlock(BlockCapsule blockCapsule) {
}
ByteString witnessAddress = blockCapsule.getWitnessAddress();
long timeStamp = blockCapsule.getTimeStamp();
if (timeStamp % BLOCK_PRODUCED_INTERVAL != 0
&& consensusDelegate.getDynamicPropertiesStore().allowConsensusLogicOptimization()) {
logger.warn("ValidBlock failed: witness: {}, timeStamp: {}",
ByteArray.toHexString(witnessAddress.toByteArray()), timeStamp);
return false;
}
long bSlot = dposSlot.getAbSlot(timeStamp);
long hSlot = dposSlot.getAbSlot(consensusDelegate.getLatestBlockHeaderTimestamp());
if (bSlot <= hSlot) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.tron.core.consensus;

import static org.mockito.Mockito.mock;

import java.lang.reflect.Field;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import org.tron.consensus.ConsensusDelegate;
import org.tron.consensus.dpos.DposService;
import org.tron.core.capsule.BlockCapsule;
import org.tron.core.store.DynamicPropertiesStore;
import org.tron.protos.Protocol;

public class DposServiceTest {
DposService service = new DposService();

@Test
public void test() throws Exception {
long headTime = 1724036757000L;

ConsensusDelegate consensusDelegate = mock(ConsensusDelegate.class);
Field field = service.getClass().getDeclaredField("consensusDelegate");
field.setAccessible(true);
field.set(service, consensusDelegate);

DynamicPropertiesStore store = mock(DynamicPropertiesStore.class);
Mockito.when(consensusDelegate.getDynamicPropertiesStore()).thenReturn(store);

Mockito.when(consensusDelegate.getLatestBlockHeaderNumber()).thenReturn(0L);
boolean f = service.validBlock(null);
Assert.assertTrue(f);

Protocol.BlockHeader.raw raw = Protocol.BlockHeader.raw.newBuilder()
.setTimestamp(headTime + 3001).build();
Protocol.BlockHeader header = Protocol.BlockHeader.newBuilder().setRawData(raw).build();
Protocol.Block block = Protocol.Block.newBuilder().setBlockHeader(header).build();

Mockito.when(consensusDelegate.getLatestBlockHeaderNumber()).thenReturn(100L);
Mockito.when(store.allowConsensusLogicOptimization()).thenReturn(true);

Mockito.when(consensusDelegate.getLatestBlockHeaderTimestamp()).thenReturn(headTime + 3000);
f = service.validBlock(new BlockCapsule(block));
Assert.assertTrue(!f);

}
}