Skip to content

Commit 97f1f6f

Browse files
authored
feat(event): optimize the event cache solid block ID update logic (tronprotocol#6531)
* feat(event): optimize the event cache solidId update logic * add unit test
1 parent 34f25e0 commit 97f1f6f

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

framework/src/main/java/org/tron/core/services/event/BlockEventCache.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,14 @@ public static void add(BlockEvent blockEvent) throws EventException {
6666
}
6767

6868
if (blockEvent.getSolidId().getNum() > solidId.getNum()) {
69-
solidId = blockEvent.getSolidId();
69+
BlockCapsule.BlockId headBlockId = head.getBlockId();
70+
if (blockEvent.getSolidId().getNum() <= headBlockId.getNum()) {
71+
solidId = blockEvent.getSolidId();
72+
} else if (blockEvent.getBlockId().equals(headBlockId)) {
73+
// Fork chains needs to be considered to ensure that the head is on the main chain.
74+
logger.info("Set solidId to head {}", headBlockId.getString());
75+
solidId = headBlockId;
76+
}
7077
}
7178
}
7279

framework/src/test/java/org/tron/core/event/BlockEventCacheTest.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public class BlockEventCacheTest {
1313

1414
@Test
1515
public void test() throws Exception {
16+
BlockCapsule.BlockId solidID = new BlockCapsule.BlockId(getBlockId(), 100);
17+
1618
BlockEvent be1 = new BlockEvent();
1719
BlockCapsule.BlockId b1 = new BlockCapsule.BlockId(getBlockId(), 1);
1820
be1.setBlockId(b1);
@@ -36,32 +38,46 @@ public void test() throws Exception {
3638

3739
BlockEventCache.init(b1);
3840

41+
BlockEvent event = new BlockEvent();
42+
BlockCapsule.BlockId blockId = new BlockCapsule.BlockId(getBlockId(), 2);
43+
event.setBlockId(blockId);
44+
event.setParentId(b1);
45+
event.setSolidId(blockId);
46+
BlockEventCache.add(event);
47+
Assert.assertEquals(event, BlockEventCache.getHead());
48+
Assert.assertEquals(blockId, BlockEventCache.getSolidId());
49+
Assert.assertEquals(event, BlockEventCache.getBlockEvent(blockId));
50+
51+
BlockEventCache.init(b1);
52+
3953
BlockEvent be2 = new BlockEvent();
4054
BlockCapsule.BlockId b2 = new BlockCapsule.BlockId(getBlockId(), 2);
4155
be2.setBlockId(b2);
4256
be2.setParentId(b1);
43-
be2.setSolidId(b1);
57+
be2.setSolidId(solidID);
4458
BlockEventCache.add(be2);
4559
Assert.assertEquals(be2, BlockEventCache.getHead());
60+
Assert.assertEquals(b2, BlockEventCache.getSolidId());
4661
Assert.assertEquals(be2, BlockEventCache.getBlockEvent(b2));
4762

4863
BlockEvent be22 = new BlockEvent();
4964
BlockCapsule.BlockId b22 = new BlockCapsule.BlockId(getBlockId(), 2);
5065
be22.setBlockId(b22);
5166
be22.setParentId(b1);
52-
be22.setSolidId(b22);
67+
be22.setSolidId(solidID);
5368
BlockEventCache.add(be22);
5469
Assert.assertEquals(be2, BlockEventCache.getHead());
5570
Assert.assertEquals(be22, BlockEventCache.getBlockEvent(b22));
56-
Assert.assertEquals(b22, BlockEventCache.getSolidId());
71+
Assert.assertEquals(b2, BlockEventCache.getSolidId());
5772

5873
BlockEvent be3 = new BlockEvent();
5974
BlockCapsule.BlockId b3 = new BlockCapsule.BlockId(getBlockId(), 3);
6075
be3.setBlockId(b3);
6176
be3.setParentId(b22);
62-
be3.setSolidId(b22);
77+
be3.setSolidId(solidID);
6378
BlockEventCache.add(be3);
6479
Assert.assertEquals(be3, BlockEventCache.getHead());
80+
Assert.assertEquals(b3, BlockEventCache.getSolidId());
6581

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

0 commit comments

Comments
 (0)