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
10 changes: 10 additions & 0 deletions framework/src/main/java/org/tron/core/db/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,16 @@ private void switchFork(BlockCapsule newHead)
}

if (CollectionUtils.isNotEmpty(binaryTree.getValue())) {
long ancestorNum = binaryTree.getValue().peekLast().getNum() - 1;
long solidifiedNum = getDynamicPropertiesStore().getLatestSolidifiedBlockNum();
if (ancestorNum < solidifiedNum) {
Metrics.counterInc(MetricKeys.Counter.BLOCK_FORK, 1, MetricLabels.FAIL);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[NIT] The BLOCK_FORK/FAIL counter plus the BLOCKCHAIN_FAIL_FORK_COUNT meter now appear three times in switchFork. A small private helper would keep them in sync.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two metric updates are straightforward at each of the three failure sites. I'd prefer to keep this fix focused and retain the current structure for now. We can consider extracting a helper if more failure paths are added.

MetricsUtil.meterMark(MetricsKey.BLOCKCHAIN_FAIL_FORK_COUNT);
binaryTree.getKey().forEach(block -> khaosDb.removeBlk(block.getId()));
khaosDb.setHead(binaryTree.getValue().peekFirst());
throw new BadBlockException(String.format(
"Fork common ancestor %d is below solidified block %d", ancestorNum, solidifiedNum));
}
while (!getDynamicPropertiesStore()
.getLatestBlockHeaderHash()
.equals(binaryTree.getValue().peekLast().getParentHash())) {
Expand Down
95 changes: 95 additions & 0 deletions framework/src/test/java/org/tron/core/db/ManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2158,6 +2158,101 @@ public void switchForkShouldResetTransactionSignVerifiedOnNewBranch() throws Exc
verify(spyTx, atLeastOnce()).setVerified(false);
}

@Test
public void switchForkShouldRejectAncestorBelowSolidifiedBlock() throws Exception {
checkForkSolidifiedBoundary(1, false);
}

@Test
public void switchForkShouldRemoveProcessedBranchBelowSolidifiedBlock() throws Exception {
checkForkSolidifiedBoundary(1, true);
}

@Test
public void switchForkShouldAllowAncestorAtSolidifiedBlock() throws Exception {
checkForkSolidifiedBoundary(0, false);
}

@Test
public void switchForkShouldAllowAncestorAboveSolidifiedBlock() throws Exception {
checkForkSolidifiedBoundary(-1, false);
}

private void checkForkSolidifiedBoundary(int solidifiedOffset, boolean processedFork)
throws Exception {
dbManager.stopRePushThread();
byte[] witnessKey = ByteArray.fromHexString(privateKey);
ByteString witness = ByteString.copyFrom(ECKey.fromPrivate(witnessKey).getAddress());
chainManager.getWitnessScheduleStore().saveActiveWitnesses(new ArrayList<>());
chainManager.addWitness(witness);
long time = 1533529947843L;
dbManager.pushBlock(new BlockCapsule(
blockGenerate.getSignedBlock(witness, time, witnessKey)));
Map<ByteString, String> keys = addTestWitnessAndAccount();
keys.put(witness, privateKey);

long base = chainManager.getHeadBlockNum();
BlockCapsule parent = createTestBlockCapsule(time + 3000, base + 1,
chainManager.getHeadBlockId().getByteString(), keys);
dbManager.pushBlock(parent);
BlockCapsule forkBlock = createTestBlockCapsule(time + 6001, base + 2,
parent.getBlockId().getByteString(), keys);
BlockCapsule forkHead = createTestBlockCapsule(time + 9001, base + 3,
forkBlock.getBlockId().getByteString(), keys);
BlockCapsule newHead = createTestBlockCapsule(time + 12000, base + 4,
forkHead.getBlockId().getByteString(), keys);
BlockCapsule oldBlock = createTestBlockCapsule(time + 6000, base + 2,
parent.getBlockId().getByteString(), keys);
dbManager.pushBlock(oldBlock);
BlockCapsule oldHead = createTestBlockCapsule(time + 9000, base + 3,
oldBlock.getBlockId().getByteString(), keys);
dbManager.pushBlock(oldHead);

dbManager.pushBlock(forkBlock);
dbManager.pushBlock(forkHead);
long solidified = parent.getNum() + solidifiedOffset;
chainManager.getDynamicPropertiesStore().saveLatestSolidifiedBlockNum(solidified);
KhaosDatabase khaos = chainManager.getKhaosDb();
if (processedFork) {
// Model a cached prefix retained from a previously executed branch.
khaos.getMiniStore().getByHash(forkBlock.getBlockId()).commit(forkBlock);
}
// The ancestor can still be cached after the solidified height advances past it.
Assert.assertNotNull(khaos.getMiniStore().getByHash(parent.getBlockId()));

if (solidifiedOffset > 0) {
int snapshots = dbManager.getRevokingStore().size();
assertThrows(BadBlockException.class, () -> dbManager.pushBlock(newHead));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[NIT] No test covers khaosDb.setHead(binaryTree.getValue().peekFirst()) yet. With that line removed, all switchForkShould* tests still pass. Once the branch is removed, the highest block left in Khaos is oldHead anyway, so removeBlk's own head recompute already lands on it. If it's cheap to build, a case where Khaos still holds a non-canonical block at or above the old head's height when the guard fires would pin this down.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, the current fixture does not independently verify the explicit head restoration. The tests primarily cover rejection at the solidified boundary, preservation of the original chain, and continued processing of subsequent blocks. The additional cached-branch scenario you suggested would strengthen coverage; I'd prefer to keep the existing tests for this fix and consider adding that case separately.


Assert.assertEquals(oldHead.getBlockId(), chainManager.getHeadBlockId());
Assert.assertEquals(solidified,
chainManager.getDynamicPropertiesStore().getLatestSolidifiedBlockNum());
Assert.assertEquals(snapshots, dbManager.getRevokingStore().size());
Assert.assertEquals(oldHead.getBlockId(), khaos.getHeadId());
for (BlockCapsule block : Arrays.asList(parent, oldBlock, oldHead)) {
Assert.assertTrue(chainManager.getBlockStore().has(block.getBlockId().getBytes()));
Assert.assertEquals(block.getBlockId(), chainManager.getBlockIdByNum(block.getNum()));
Assert.assertNotNull(khaos.getMiniStore().getByHash(block.getBlockId()));
}
for (BlockCapsule block : Arrays.asList(forkBlock, forkHead, newHead)) {
Assert.assertNull(khaos.getMiniStore().getByHash(block.getBlockId()));
}

BlockCapsule honestNext = createTestBlockCapsule(time + 12000, base + 4,
oldHead.getBlockId().getByteString(), keys);
dbManager.pushBlock(honestNext);
Assert.assertEquals(honestNext.getBlockId(), chainManager.getHeadBlockId());
Assert.assertEquals(honestNext.getBlockId(), khaos.getHeadId());
} else {
dbManager.pushBlock(newHead);
Assert.assertEquals(newHead.getBlockId(), chainManager.getHeadBlockId());
Assert.assertEquals(newHead.getBlockId(), khaos.getHeadId());
Assert.assertEquals(forkBlock.getBlockId(),
chainManager.getBlockIdByNum(forkBlock.getNum()));
Assert.assertEquals(forkHead.getBlockId(), chainManager.getBlockIdByNum(forkHead.getNum()));
}
}

@Test
public void switchForkShouldRestoreOldBranchAfterRuntimeException() throws Exception {
checkForkRecovery(false, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ public void revalidatesAdmittedForkAgainstRewoundParentPermission() throws Excep
BlockCapsule parent = nextBlock(signer);
dbManager.pushBlock(parent);
BlockCapsule oldHead = blockRevokingWitness(parent);
// Keep the head reversible in this single-witness fixture.
chainBaseManager.getDynamicPropertiesStore().saveLatestSolidifiedBlockNum(parent.getNum());
BlockCapsule fork = signedBlock(parent.getNum() + 1, parent.getBlockId(),
parent.getTimeStamp() + 3001, replacement);
dbManager.pushBlock(fork);
Expand Down Expand Up @@ -321,6 +323,8 @@ public void updatesMetricsAndShutdownTargetOnceAfterSuccessfulFork() throws Exce
BlockCapsule parent = nextBlock(signer);
dbManager.pushBlock(parent);
dbManager.pushBlock(nextBlock(signer));
// Keep the head reversible in this single-witness fixture.
chainBaseManager.getDynamicPropertiesStore().saveLatestSolidifiedBlockNum(parent.getNum());
BlockCapsule fork = signedBlock(parent.getNum() + 1, parent.getBlockId(),
parent.getTimeStamp() + 3001, signer);
BlockCapsule forkHead = signedBlock(fork.getNum() + 1, fork.getBlockId(),
Expand Down
Loading