Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(consensus): optimize block production logic #5833

Merged
merged 2 commits into from
May 30, 2024
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
5 changes: 5 additions & 0 deletions consensus/src/main/java/org/tron/consensus/dpos/DposTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ private State produceBlock() {
try {
synchronized (dposService.getBlockHandle().getLock()) {

state = stateManager.getState();
if (!State.OK.equals(state)) {
return state;
}

long slot = dposSlot.getSlot(System.currentTimeMillis() + 50);
if (slot == 0) {
return State.NOT_TIME_YET;
Expand Down
64 changes: 64 additions & 0 deletions framework/src/test/java/org/tron/core/consensus/DposTaskTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.tron.core.consensus;

import static org.mockito.Mockito.mock;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import org.tron.consensus.base.BlockHandle;
import org.tron.consensus.base.State;
import org.tron.consensus.dpos.DposService;
import org.tron.consensus.dpos.DposSlot;
import org.tron.consensus.dpos.DposTask;
import org.tron.consensus.dpos.StateManager;

public class DposTaskTest {
private DposTask dposTask = new DposTask();

@Test
public void tet() throws Exception {
StateManager stateManager = mock(StateManager.class);
Mockito.when(stateManager.getState()).thenReturn(State.BACKUP_IS_NOT_MASTER);

Field field = dposTask.getClass().getDeclaredField("stateManager");
field.setAccessible(true);
field.set(dposTask, stateManager);

Method method = dposTask.getClass().getDeclaredMethod("produceBlock");
method.setAccessible(true);
State state = (State) method.invoke(dposTask);

Assert.assertEquals(State.BACKUP_IS_NOT_MASTER, state);


Mockito.when(stateManager.getState()).thenReturn(State.OK);

DposSlot dposSlot = mock(DposSlot.class);
Mockito.when(dposSlot.getTime(1)).thenReturn(Long.MAX_VALUE);

field = dposTask.getClass().getDeclaredField("dposSlot");
field.setAccessible(true);
field.set(dposTask, dposSlot);


Mockito.when(stateManager.getState()).thenReturn(State.OK);

BlockHandle blockHandle = mock(BlockHandle.class);
Mockito.when(blockHandle.getLock()).thenReturn(new Object());


DposService dposService = mock(DposService.class);
Mockito.when(dposService.getBlockHandle()).thenReturn(blockHandle);

field = dposTask.getClass().getDeclaredField("dposService");
field.setAccessible(true);
field.set(dposTask, dposService);

state = (State) method.invoke(dposTask);

Assert.assertEquals(State.NOT_TIME_YET, state);
}

}