Skip to content

Commit 01502f3

Browse files
authored
Merge pull request #5862 from halibobo1205/feature/optimize-event-exception
feat(event): optimize event subscription exception handling
2 parents bcba5c1 + 28a0768 commit 01502f3

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

framework/src/main/java/org/tron/core/db/Manager.java

+17-10
Original file line numberDiff line numberDiff line change
@@ -1321,23 +1321,17 @@ public void pushBlock(final BlockCapsule block)
13211321

13221322
return;
13231323
}
1324+
long oldSolidNum = getDynamicPropertiesStore().getLatestSolidifiedBlockNum();
13241325
try (ISession tmpSession = revokingStore.buildSession()) {
1325-
1326-
long oldSolidNum =
1327-
chainBaseManager.getDynamicPropertiesStore().getLatestSolidifiedBlockNum();
1328-
13291326
applyBlock(newBlock, txs);
13301327
tmpSession.commit();
1331-
// if event subscribe is enabled, post block trigger to queue
1332-
postBlockTrigger(newBlock);
1333-
// if event subscribe is enabled, post solidity trigger to queue
1334-
postSolidityTrigger(oldSolidNum,
1335-
getDynamicPropertiesStore().getLatestSolidifiedBlockNum());
13361328
} catch (Throwable throwable) {
13371329
logger.error(throwable.getMessage(), throwable);
13381330
khaosDb.removeBlk(block.getBlockId());
13391331
throw throwable;
13401332
}
1333+
long newSolidNum = getDynamicPropertiesStore().getLatestSolidifiedBlockNum();
1334+
blockTrigger(newBlock, oldSolidNum, newSolidNum);
13411335
}
13421336
logger.info(SAVE_BLOCK, newBlock);
13431337
}
@@ -1367,6 +1361,19 @@ public void pushBlock(final BlockCapsule block)
13671361
}
13681362
}
13691363

1364+
void blockTrigger(final BlockCapsule block, long oldSolid, long newSolid) {
1365+
try {
1366+
// if event subscribe is enabled, post block trigger to queue
1367+
postBlockTrigger(block);
1368+
// if event subscribe is enabled, post solidity trigger to queue
1369+
postSolidityTrigger(oldSolid, newSolid);
1370+
} catch (Exception e) {
1371+
logger.error("Block trigger failed. head: {}, oldSolid: {}, newSolid: {}",
1372+
block.getNum(), oldSolid, newSolid, e);
1373+
System.exit(1);
1374+
}
1375+
}
1376+
13701377
public void updateDynamicProperties(BlockCapsule block) {
13711378

13721379
chainBaseManager.getDynamicPropertiesStore()
@@ -2206,7 +2213,7 @@ private void postLogsFilter(final BlockCapsule blockCapsule, boolean solidified,
22062213
}
22072214
}
22082215

2209-
private void postBlockTrigger(final BlockCapsule blockCapsule) {
2216+
void postBlockTrigger(final BlockCapsule blockCapsule) {
22102217
// post block and logs for jsonrpc
22112218
if (CommonParameter.getInstance().isJsonRpcHttpFullNodeEnable()) {
22122219
postBlockFilter(blockCapsule, false);

framework/src/test/java/org/tron/core/db/ManagerTest.java

+14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package org.tron.core.db;
22

33
import static org.junit.Assert.assertThrows;
4+
import static org.mockito.ArgumentMatchers.any;
5+
import static org.mockito.Mockito.doThrow;
6+
import static org.mockito.Mockito.spy;
47
import static org.tron.common.utils.Commons.adjustAssetBalanceV2;
58
import static org.tron.common.utils.Commons.adjustBalance;
69
import static org.tron.common.utils.Commons.adjustTotalShieldedPoolValue;
@@ -27,6 +30,7 @@
2730
import org.junit.Before;
2831
import org.junit.Rule;
2932
import org.junit.Test;
33+
import org.junit.contrib.java.lang.system.ExpectedSystemExit;
3034
import org.junit.rules.TemporaryFolder;
3135
import org.tron.common.application.TronApplicationContext;
3236
import org.tron.common.crypto.ECKey;
@@ -107,6 +111,8 @@ public class ManagerTest extends BlockGenerate {
107111
private static BlockCapsule blockCapsule2;
108112
@Rule
109113
public TemporaryFolder temporaryFolder = new TemporaryFolder();
114+
@Rule
115+
public final ExpectedSystemExit exit = ExpectedSystemExit.none();
110116
private static AtomicInteger port = new AtomicInteger(0);
111117
private static String accountAddress =
112118
Wallet.getAddressPreFixString() + "548794500882809695a8a687866e76d4271a1abc";
@@ -1151,4 +1157,12 @@ public void testTooBigTransaction() {
11511157
TooBigTransactionException.class, () -> dbManager.validateCommon(trx));
11521158

11531159
}
1160+
1161+
@Test
1162+
public void blockTrigger() {
1163+
exit.expectSystemExitWithStatus(1);
1164+
Manager manager = spy(new Manager());
1165+
doThrow(new RuntimeException("postBlockTrigger mock")).when(manager).postBlockTrigger(any());
1166+
manager.blockTrigger(new BlockCapsule(Block.newBuilder().build()), 1, 1);
1167+
}
11541168
}

0 commit comments

Comments
 (0)