Skip to content

Commit

Permalink
feat(event): handle event subscribe exceptions carefully
Browse files Browse the repository at this point in the history
  • Loading branch information
tomatoishealthy authored and halibobo1205 committed Jun 19, 2024
1 parent d383177 commit 9d398c1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
27 changes: 17 additions & 10 deletions framework/src/main/java/org/tron/core/db/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -1321,23 +1321,17 @@ public void pushBlock(final BlockCapsule block)

return;
}
long oldSolidNum = getDynamicPropertiesStore().getLatestSolidifiedBlockNum();
try (ISession tmpSession = revokingStore.buildSession()) {

long oldSolidNum =
chainBaseManager.getDynamicPropertiesStore().getLatestSolidifiedBlockNum();

applyBlock(newBlock, txs);
tmpSession.commit();
// if event subscribe is enabled, post block trigger to queue
postBlockTrigger(newBlock);
// if event subscribe is enabled, post solidity trigger to queue
postSolidityTrigger(oldSolidNum,
getDynamicPropertiesStore().getLatestSolidifiedBlockNum());
} catch (Throwable throwable) {
logger.error(throwable.getMessage(), throwable);
khaosDb.removeBlk(block.getBlockId());
throw throwable;
}
long newSolidNum = getDynamicPropertiesStore().getLatestSolidifiedBlockNum();
blockTrigger(newBlock, oldSolidNum, newSolidNum);
}
logger.info(SAVE_BLOCK, newBlock);
}
Expand Down Expand Up @@ -1367,6 +1361,19 @@ public void pushBlock(final BlockCapsule block)
}
}

void blockTrigger(final BlockCapsule block, long oldSolid, long newSolid) {
try {
// if event subscribe is enabled, post block trigger to queue
postBlockTrigger(block);
// if event subscribe is enabled, post solidity trigger to queue
postSolidityTrigger(oldSolid, newSolid);
} catch (Exception e) {
logger.error("Block trigger failed. head: {}, oldSolid: {}, newSolid: {}",
block.getNum(), oldSolid, newSolid, e);
System.exit(1);
}
}

public void updateDynamicProperties(BlockCapsule block) {

chainBaseManager.getDynamicPropertiesStore()
Expand Down Expand Up @@ -2206,7 +2213,7 @@ private void postLogsFilter(final BlockCapsule blockCapsule, boolean solidified,
}
}

private void postBlockTrigger(final BlockCapsule blockCapsule) {
void postBlockTrigger(final BlockCapsule blockCapsule) {
// post block and logs for jsonrpc
if (CommonParameter.getInstance().isJsonRpcHttpFullNodeEnable()) {
postBlockFilter(blockCapsule, false);
Expand Down
14 changes: 14 additions & 0 deletions framework/src/test/java/org/tron/core/db/ManagerTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.tron.core.db;

import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.spy;
import static org.tron.common.utils.Commons.adjustAssetBalanceV2;
import static org.tron.common.utils.Commons.adjustBalance;
import static org.tron.common.utils.Commons.adjustTotalShieldedPoolValue;
Expand All @@ -27,6 +30,7 @@
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.ExpectedSystemExit;
import org.junit.rules.TemporaryFolder;
import org.tron.common.application.TronApplicationContext;
import org.tron.common.crypto.ECKey;
Expand Down Expand Up @@ -107,6 +111,8 @@ public class ManagerTest extends BlockGenerate {
private static BlockCapsule blockCapsule2;
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@Rule
public final ExpectedSystemExit exit = ExpectedSystemExit.none();
private static AtomicInteger port = new AtomicInteger(0);
private static String accountAddress =
Wallet.getAddressPreFixString() + "548794500882809695a8a687866e76d4271a1abc";
Expand Down Expand Up @@ -1151,4 +1157,12 @@ public void testTooBigTransaction() {
TooBigTransactionException.class, () -> dbManager.validateCommon(trx));

}

@Test
public void blockTrigger(){
exit.expectSystemExitWithStatus(1);
Manager manager = spy(new Manager());
doThrow(new RuntimeException("postBlockTrigger mock")).when(manager).postBlockTrigger(any());
manager.blockTrigger(new BlockCapsule(Block.newBuilder().build()), 1, 1);
}
}

0 comments on commit 9d398c1

Please sign in to comment.