-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat(net): stop tx broadcasting if block cannot solidified #5643
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
Changes from all commits
0d48db4
79e978e
bf8de17
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
import org.tron.core.capsule.BlockCapsule.BlockId; | ||
import org.tron.core.capsule.PbftSignCapsule; | ||
import org.tron.core.capsule.TransactionCapsule; | ||
import org.tron.core.config.args.Args; | ||
import org.tron.core.db.Manager; | ||
import org.tron.core.exception.AccountResourceInsufficientException; | ||
import org.tron.core.exception.BadBlockException; | ||
|
@@ -98,6 +99,11 @@ public class TronNetDelegate { | |
@Setter | ||
private volatile boolean exit = true; | ||
|
||
private int maxUnsolidifiedBlocks = Args.getInstance().getMaxUnsolidifiedBlocks(); | ||
|
||
private boolean unsolidifiedBlockCheck | ||
= Args.getInstance().isUnsolidifiedBlockCheck(); | ||
|
||
private Cache<BlockId, Long> freshBlockId = CacheBuilder.newBuilder() | ||
.maximumSize(blockIdCacheSize).expireAfterWrite(1, TimeUnit.HOURS) | ||
.recordStats().build(); | ||
|
@@ -365,4 +371,13 @@ public long getMaintenanceTimeInterval() { | |
return chainBaseManager.getDynamicPropertiesStore().getMaintenanceTimeInterval(); | ||
} | ||
|
||
public boolean isBlockUnsolidified() { | ||
if (!unsolidifiedBlockCheck) { | ||
return false; | ||
} | ||
long headNum = chainBaseManager.getHeadBlockNum(); | ||
long solidNum = chainBaseManager.getSolidBlockId().getNum(); | ||
return headNum - solidNum >= maxUnsolidifiedBlocks; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please rename the method unsolidifiedBlockCheck. It should not be the same as varible, it is confusing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. well, done! |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.tron.core.net; | ||
|
||
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.common.parameter.CommonParameter; | ||
import org.tron.common.utils.Sha256Hash; | ||
import org.tron.core.ChainBaseManager; | ||
import org.tron.core.Constant; | ||
import org.tron.core.capsule.BlockCapsule; | ||
import org.tron.core.config.args.Args; | ||
|
||
public class TronNetDelegateTest { | ||
|
||
@Test | ||
public void test() throws Exception { | ||
Args.setParam(new String[] {"-w"}, Constant.TEST_CONF); | ||
CommonParameter parameter = Args.getInstance(); | ||
Args.logConfig(); | ||
|
||
BlockCapsule.BlockId blockId = new BlockCapsule.BlockId(Sha256Hash.ZERO_HASH, 10000L); | ||
|
||
TronNetDelegate tronNetDelegate = new TronNetDelegate(); | ||
|
||
ChainBaseManager chainBaseManager = mock(ChainBaseManager.class); | ||
Mockito.when(chainBaseManager.getHeadBlockNum()).thenReturn(10000L); | ||
Mockito.when(chainBaseManager.getSolidBlockId()).thenReturn(blockId); | ||
|
||
Field field = tronNetDelegate.getClass().getDeclaredField("chainBaseManager"); | ||
field.setAccessible(true); | ||
field.set(tronNetDelegate, chainBaseManager); | ||
|
||
Assert.assertTrue(!tronNetDelegate.isBlockUnsolidified()); | ||
|
||
blockId = new BlockCapsule.BlockId(Sha256Hash.ZERO_HASH, 1L); | ||
Mockito.when(chainBaseManager.getSolidBlockId()).thenReturn(blockId); | ||
Assert.assertTrue(tronNetDelegate.isBlockUnsolidified()); | ||
|
||
parameter.setUnsolidifiedBlockCheck(false); | ||
tronNetDelegate = new TronNetDelegate(); | ||
|
||
field = tronNetDelegate.getClass().getDeclaredField("unsolidifiedBlockCheck"); | ||
field.setAccessible(true); | ||
field.set(tronNetDelegate, false); | ||
|
||
Assert.assertTrue(!tronNetDelegate.isBlockUnsolidified()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1051,6 +1051,8 @@ message Return { | |
SERVER_BUSY = 9; | ||
NO_CONNECTION = 10; | ||
NOT_ENOUGH_EFFECTIVE_CONNECTION = 11; | ||
BLOCK_UNSOLIDIFIED = 12; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May be the reason TOO_MANY_BLOCK_UNSOLIDIFIED more clearful ? We generally believe that less than a certain number of unsodlified blocks is normal. It's tiny. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Briefly describe what happened, |
||
|
||
OTHER_ERROR = 20; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default value is determined how?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Referring to the issue test data, I personally think 1000 is OK.