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
Expand Up @@ -66,7 +66,14 @@ public static void add(BlockEvent blockEvent) throws EventException {
}

if (blockEvent.getSolidId().getNum() > solidId.getNum()) {
solidId = blockEvent.getSolidId();
BlockCapsule.BlockId headBlockId = head.getBlockId();
if (blockEvent.getSolidId().getNum() <= headBlockId.getNum()) {
solidId = blockEvent.getSolidId();
} else if (blockEvent.getBlockId().equals(headBlockId)) {
Copy link
Contributor

@bladehan1 bladehan1 Jan 29, 2026

Choose a reason for hiding this comment

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

From the perspective of the blockEvent event:
blockEvent.getBlockId().getNum() >= blockEvent.getSolidId().getNum()
From the perspective of the head assignment:
head.getBlockId().getNum() >= blockEvent.getBlockId().getNum()

Therefore, it should always be true:
headBlockId.getNum()>= blockEvent.getBlockId().getNum()>= blockEvent.getSolidId().getNum()

That is,
headBlockId.getNum()>= blockEvent.getSolidId().getNum()

In any case, what situation might cause it to not be true?

Copy link
Contributor

Choose a reason for hiding this comment

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

blockEvent.getSolidId() represents the current solidified block height. For example, if 218 blocks are currently synchronized, when loading the 100th block, the current solidified block height might be 200.

// Fork chains needs to be considered to ensure that the head is on the main chain.
logger.info("Set solidId to head {}", headBlockId.getString());
solidId = headBlockId;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class BlockEventCacheTest {

@Test
public void test() throws Exception {
BlockCapsule.BlockId solidID = new BlockCapsule.BlockId(getBlockId(), 100);

BlockEvent be1 = new BlockEvent();
BlockCapsule.BlockId b1 = new BlockCapsule.BlockId(getBlockId(), 1);
be1.setBlockId(b1);
Expand All @@ -36,32 +38,46 @@ public void test() throws Exception {

BlockEventCache.init(b1);

BlockEvent event = new BlockEvent();
BlockCapsule.BlockId blockId = new BlockCapsule.BlockId(getBlockId(), 2);
event.setBlockId(blockId);
event.setParentId(b1);
event.setSolidId(blockId);
BlockEventCache.add(event);
Assert.assertEquals(event, BlockEventCache.getHead());
Assert.assertEquals(blockId, BlockEventCache.getSolidId());
Assert.assertEquals(event, BlockEventCache.getBlockEvent(blockId));

BlockEventCache.init(b1);

BlockEvent be2 = new BlockEvent();
BlockCapsule.BlockId b2 = new BlockCapsule.BlockId(getBlockId(), 2);
be2.setBlockId(b2);
be2.setParentId(b1);
be2.setSolidId(b1);
be2.setSolidId(solidID);
BlockEventCache.add(be2);
Assert.assertEquals(be2, BlockEventCache.getHead());
Assert.assertEquals(b2, BlockEventCache.getSolidId());
Assert.assertEquals(be2, BlockEventCache.getBlockEvent(b2));

BlockEvent be22 = new BlockEvent();
BlockCapsule.BlockId b22 = new BlockCapsule.BlockId(getBlockId(), 2);
be22.setBlockId(b22);
be22.setParentId(b1);
be22.setSolidId(b22);
be22.setSolidId(solidID);
BlockEventCache.add(be22);
Assert.assertEquals(be2, BlockEventCache.getHead());
Assert.assertEquals(be22, BlockEventCache.getBlockEvent(b22));
Assert.assertEquals(b22, BlockEventCache.getSolidId());
Assert.assertEquals(b2, BlockEventCache.getSolidId());

BlockEvent be3 = new BlockEvent();
BlockCapsule.BlockId b3 = new BlockCapsule.BlockId(getBlockId(), 3);
be3.setBlockId(b3);
be3.setParentId(b22);
be3.setSolidId(b22);
be3.setSolidId(solidID);
BlockEventCache.add(be3);
Assert.assertEquals(be3, BlockEventCache.getHead());
Assert.assertEquals(b3, BlockEventCache.getSolidId());

List<BlockEvent> list = BlockEventCache.getSolidBlockEvents(b2);
Assert.assertEquals(1, list.size());
Expand Down