From 1efa67462ef4771422367b9537eabf6066828e16 Mon Sep 17 00:00:00 2001 From: zeusoo001 Date: Wed, 28 Jan 2026 19:24:35 +0800 Subject: [PATCH 1/3] feat(event): optimize the event cache solidId update logic --- .../org/tron/core/services/event/BlockEventCache.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/framework/src/main/java/org/tron/core/services/event/BlockEventCache.java b/framework/src/main/java/org/tron/core/services/event/BlockEventCache.java index 3548859262e..e92bf6c8f1a 100644 --- a/framework/src/main/java/org/tron/core/services/event/BlockEventCache.java +++ b/framework/src/main/java/org/tron/core/services/event/BlockEventCache.java @@ -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)) { + // 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; + } } } From 83e1201de10baa4a587a7f5dd4989f0a0accfcc2 Mon Sep 17 00:00:00 2001 From: zeusoo001 Date: Thu, 29 Jan 2026 15:56:44 +0800 Subject: [PATCH 2/3] add unit test --- .../tron/core/event/BlockEventCacheTest.java | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/framework/src/test/java/org/tron/core/event/BlockEventCacheTest.java b/framework/src/test/java/org/tron/core/event/BlockEventCacheTest.java index e99433db3c6..1cc4a6eeb74 100644 --- a/framework/src/test/java/org/tron/core/event/BlockEventCacheTest.java +++ b/framework/src/test/java/org/tron/core/event/BlockEventCacheTest.java @@ -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); @@ -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(b1); + BlockEventCache.add(event); + Assert.assertEquals(event, BlockEventCache.getHead()); + Assert.assertEquals(b1, 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 list = BlockEventCache.getSolidBlockEvents(b2); Assert.assertEquals(1, list.size()); From 430e9b0468f94bd1880e9a46f259c7995993553d Mon Sep 17 00:00:00 2001 From: zeusoo001 Date: Thu, 29 Jan 2026 16:01:09 +0800 Subject: [PATCH 3/3] add unit test --- .../test/java/org/tron/core/event/BlockEventCacheTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/framework/src/test/java/org/tron/core/event/BlockEventCacheTest.java b/framework/src/test/java/org/tron/core/event/BlockEventCacheTest.java index 1cc4a6eeb74..82c887fad53 100644 --- a/framework/src/test/java/org/tron/core/event/BlockEventCacheTest.java +++ b/framework/src/test/java/org/tron/core/event/BlockEventCacheTest.java @@ -42,10 +42,10 @@ public void test() throws Exception { BlockCapsule.BlockId blockId = new BlockCapsule.BlockId(getBlockId(), 2); event.setBlockId(blockId); event.setParentId(b1); - event.setSolidId(b1); + event.setSolidId(blockId); BlockEventCache.add(event); Assert.assertEquals(event, BlockEventCache.getHead()); - Assert.assertEquals(b1, BlockEventCache.getSolidId()); + Assert.assertEquals(blockId, BlockEventCache.getSolidId()); Assert.assertEquals(event, BlockEventCache.getBlockEvent(blockId)); BlockEventCache.init(b1);