Skip to content
Closed
32 changes: 28 additions & 4 deletions framework/src/main/java/org/tron/program/SolidityNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import static org.tron.core.config.Parameter.ChainConstant.BLOCK_PRODUCED_INTERVAL;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
Expand All @@ -11,6 +13,7 @@
import org.tron.common.application.ApplicationFactory;
import org.tron.common.application.TronApplicationContext;
import org.tron.common.client.DatabaseGrpcClient;
import org.tron.common.es.ExecutorServiceManager;
import org.tron.common.parameter.CommonParameter;
import org.tron.common.prometheus.Metrics;
import org.tron.core.ChainBaseManager;
Expand Down Expand Up @@ -39,6 +42,9 @@ public class SolidityNode {

private volatile boolean flag = true;

private ExecutorService getBlockEs;
private ExecutorService processBlockEs;

public SolidityNode(Manager dbManager) {
this.dbManager = dbManager;
this.chainBaseManager = dbManager.getChainBaseManager();
Expand Down Expand Up @@ -72,13 +78,25 @@ public static void start() {
appT.startup();
SolidityNode node = new SolidityNode(appT.getDbManager());
node.run();
appT.blockUntilShutdown();
awaitShutdown(appT, node);
}

static void awaitShutdown(Application appT, SolidityNode node) {
try {
appT.blockUntilShutdown();
} finally {
// SolidityNode is created manually rather than managed by Spring/Application,
// so its executors must be shut down explicitly on exit.
node.shutdown();
}
}

private void run() {
try {
new Thread(this::getBlock).start();
new Thread(this::processBlock).start();
getBlockEs = ExecutorServiceManager.newSingleThreadExecutor("solid-get-block");
processBlockEs = ExecutorServiceManager.newSingleThreadExecutor("solid-process-block");
getBlockEs.execute(this::getBlock);
processBlockEs.execute(this::processBlock);
logger.info("Success to start solid node, ID: {}, remoteBlockNum: {}.", ID.get(),
remoteBlockNum);
} catch (Exception e) {
Expand All @@ -88,6 +106,12 @@ private void run() {
}
}

public void shutdown() {
flag = false;
ExecutorServiceManager.shutdownAndAwaitTermination(getBlockEs, "solid-get-block");
ExecutorServiceManager.shutdownAndAwaitTermination(processBlockEs, "solid-process-block");
}

private void getBlock() {
long blockNum = ID.incrementAndGet();
while (flag) {
Expand Down Expand Up @@ -193,4 +217,4 @@ private void resolveCompatibilityIssueIfUsingFullNodeDatabase() {
chainBaseManager.getDynamicPropertiesStore().saveLatestSolidifiedBlockNum(headBlockNum);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.tron.common;

import io.grpc.ManagedChannel;
import java.util.concurrent.TimeUnit;
import org.tron.common.application.ApplicationFactory;
import org.tron.common.application.TronApplicationContext;
import org.tron.core.config.DefaultConfig;

/**
* Shared class-level fixture for tests that manually manage a TronApplicationContext.
*/
public class ClassLevelAppContextFixture {

private TronApplicationContext context;

public TronApplicationContext createContext() {
context = new TronApplicationContext(DefaultConfig.class);
return context;
}

public TronApplicationContext createAndStart() {
createContext();
startApp();
return context;
}

public void startApp() {
ApplicationFactory.create(context).startup();
}

public TronApplicationContext getContext() {
return context;
}

public void close() {
if (context != null) {
context.close();
context = null;
}
}

public static void shutdownChannel(ManagedChannel channel) {
if (channel == null) {
return;
}
try {
channel.shutdown();
if (!channel.awaitTermination(5, TimeUnit.SECONDS)) {
channel.shutdownNow();
}
} catch (InterruptedException e) {
channel.shutdownNow();
Thread.currentThread().interrupt();
}
}

public static void shutdownChannels(ManagedChannel... channels) {
for (ManagedChannel channel : channels) {
shutdownChannel(channel);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public synchronized void testEventParser() {

for (int i = 0; i < entryArr.size(); i++) {
JSONObject e = entryArr.getJSONObject(i);
System.out.println(e.getString("name"));
if (e.getString("name") != null) {
if (e.getString("name").equalsIgnoreCase("eventBytesL")) {
entry = e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public synchronized void testEventParser() {

ABI.Entry entry = null;
for (ABI.Entry e : abi.getEntrysList()) {
System.out.println(e.getName());
if (e.getName().equalsIgnoreCase("eventBytesL")) {
entry = e;
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package org.tron.common.logsfilter;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
import org.tron.common.es.ExecutorServiceManager;
import org.tron.common.logsfilter.nativequeue.NativeMessageQueue;
import org.zeromq.SocketType;
import org.zeromq.ZContext;
Expand All @@ -13,6 +17,21 @@ public class NativeMessageQueueTest {
public String dataToSend = "################";
public String topic = "testTopic";

private ExecutorService subscriberExecutor;

@After
public void tearDown() {
if (subscriberExecutor != null) {
subscriberExecutor.shutdownNow();
try {
subscriberExecutor.awaitTermination(2, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
subscriberExecutor = null;
}
}

@Test
public void invalidBindPort() {
boolean bRet = NativeMessageQueue.getInstance().start(-1111, 0);
Expand All @@ -39,22 +58,23 @@ public void publishTrigger() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}

NativeMessageQueue.getInstance().publishTrigger(dataToSend, topic);

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}

NativeMessageQueue.getInstance().stop();
}

public void startSubscribeThread() {
Thread thread = new Thread(() -> {
subscriberExecutor = ExecutorServiceManager.newSingleThreadExecutor("zmq-subscriber");
subscriberExecutor.execute(() -> {
try (ZContext context = new ZContext()) {
ZMQ.Socket subscriber = context.createSocket(SocketType.SUB);

Expand All @@ -70,6 +90,5 @@ public void startSubscribeThread() {
// ZMQ.Socket will be automatically closed when ZContext is closed
}
});
thread.start();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public void setUp() {
public void testSetAndGetBlockHash() {
blockFilterCapsule
.setBlockHash("e58f33f9baf9305dc6f82b9f1934ea8f0ade2defb951258d50167028c780351f");
System.out.println(blockFilterCapsule);
Assert.assertEquals("e58f33f9baf9305dc6f82b9f1934ea8f0ade2defb951258d50167028c780351f",
blockFilterCapsule.getBlockHash());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,17 +289,9 @@ public void testWithCallerEnergyChangedInTx() throws Exception {

TVMTestResult result = freezeForOther(userA, contractAddr, userA, frozenBalance, 1);

System.out.println(result.getReceipt().getEnergyUsageTotal());
System.out.println(accountStore.get(userA));
System.out.println(accountStore.get(owner));

clearDelegatedExpireTime(contractAddr, userA);

result = unfreezeForOther(userA, contractAddr, userA, 1);

System.out.println(result.getReceipt().getEnergyUsageTotal());
System.out.println(accountStore.get(userA));
System.out.println(accountStore.get(owner));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void testWriteProperty() {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
PropUtil.writeProperty(filename, "key", "value");
Assert.assertTrue("value".equals(PropUtil.readProperty(filename, "key")));
Expand All @@ -30,11 +30,11 @@ public void testReadProperty() {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
PropUtil.writeProperty(filename, "key", "value");
Assert.assertTrue("value".equals(PropUtil.readProperty(filename, "key")));
file.delete();
Assert.assertTrue("".equals(PropUtil.readProperty(filename, "key")));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.tron.core.capsule.DelegatedResourceAccountIndexCapsule;
import org.tron.core.capsule.DelegatedResourceCapsule;
import org.tron.core.capsule.TransactionResultCapsule;
import org.tron.core.config.Parameter.ChainConstant;
import org.tron.core.config.args.Args;
import org.tron.core.exception.ContractExeException;
import org.tron.core.exception.ContractValidateException;
Expand Down Expand Up @@ -621,35 +620,6 @@ public void frozenNumTest() {
}
}

//@Test
public void moreThanFrozenNumber() {
long frozenBalance = 1_000_000_000L;
long duration = 3;
FreezeBalanceActuator actuator = new FreezeBalanceActuator();
actuator.setChainBaseManager(dbManager.getChainBaseManager())
.setAny(getContractForBandwidth(OWNER_ADDRESS, frozenBalance, duration));

TransactionResultCapsule ret = new TransactionResultCapsule();
try {
actuator.validate();
actuator.execute(ret);
} catch (ContractValidateException | ContractExeException e) {
Assert.fail();
}
try {
actuator.validate();
actuator.execute(ret);
fail("cannot run here.");
} catch (ContractValidateException e) {
long maxFrozenNumber = ChainConstant.MAX_FROZEN_NUMBER;
Assert.assertEquals("max frozen number is: " + maxFrozenNumber, e.getMessage());

} catch (ContractExeException e) {
Assert.fail();
}
}


@Test
public void commonErrorCheck() {
FreezeBalanceActuator actuator = new FreezeBalanceActuator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void test0HashNum() {
List<Sha256Hash> hashList = getHash(0); //Empty list.
try {
MerkleTree.getInstance().createTree(hashList);
Assert.assertFalse(true);
Assert.fail("Expected IndexOutOfBoundsException for empty hash list");
} catch (Exception e) {
Assert.assertTrue(e instanceof IndexOutOfBoundsException);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private long createAsset(String tokenName) {
try {
ownerCapsule.addAssetV2(ByteArray.fromString(String.valueOf(id)), TOTAL_SUPPLY);
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
accountStore.put(ownerCapsule.getAddress().toByteArray(), ownerCapsule);
return id;
Expand Down
6 changes: 3 additions & 3 deletions framework/src/test/java/org/tron/core/db/BlockStoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void testPut() {
Assert.assertNotNull(blockCapsule1);
Assert.assertEquals(number, blockCapsule1.getNum());
} catch (ItemNotFoundException | BadItemException e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}

Expand All @@ -63,7 +63,7 @@ public void testGet() {

Assert.assertEquals(number, blockCapsule1.getNum());
} catch (ItemNotFoundException | BadItemException e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}

Expand All @@ -83,7 +83,7 @@ public void testDelete() {
BlockCapsule blockCapsule2 = blockStore.getUnchecked(blockId);
Assert.assertNull(blockCapsule2);
} catch (ItemNotFoundException | BadItemException e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}

Expand Down
Loading
Loading